Line data Source code
1 : /*
2 : * License:
3 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
4 : *
5 : * https://snapwebsites.org/
6 : * contact@m2osw.com
7 : *
8 : * This program is free software; you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation; either version 2 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License along
19 : * with this program; if not, write to the Free Software Foundation, Inc.,
20 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 : *
22 : * Authors:
23 : * Alexis Wilke alexis@m2osw.com
24 : * Doug Barbieri doug@m2osw.com
25 : */
26 :
27 :
28 : /** \file
29 : * \brief Implementation of the option_info class.
30 : *
31 : * This is the implementation of the class used to load and save
32 : * configuration files.
33 : */
34 :
35 : // self
36 : //
37 : #include "advgetopt/conf_file.h"
38 :
39 :
40 : // advgetopt lib
41 : //
42 : #include "advgetopt/exception.h"
43 : #include "advgetopt/utils.h"
44 :
45 :
46 : // snapdev lib
47 : //
48 : #include <snapdev/safe_variable.h>
49 : #include <snapdev/tokenize_string.h>
50 :
51 :
52 : // cppthread lib
53 : //
54 : #include <cppthread/guard.h>
55 : #include <cppthread/log.h>
56 : #include <cppthread/mutex.h>
57 :
58 :
59 : // boost lib
60 : //
61 : #include <boost/algorithm/string/join.hpp>
62 : #include <boost/algorithm/string/replace.hpp>
63 :
64 :
65 : // C++ lib
66 : //
67 : #include <algorithm>
68 : #include <fstream>
69 :
70 :
71 : // C lib
72 : //
73 : #include <sys/stat.h>
74 :
75 :
76 : // last include
77 : //
78 : #include <snapdev/poison.h>
79 :
80 :
81 :
82 : namespace advgetopt
83 : {
84 :
85 :
86 : // from utils.cpp
87 : //
88 : // (it's here because we do not want to make cppthread public in
89 : // out header files--we could have an advgetopt_private.h, though)
90 : //
91 : cppthread::mutex & get_global_mutex();
92 :
93 :
94 :
95 : /** \brief Private conf_file data.
96 : *
97 : * The conf_file has a few globals used to cache configuration files.
98 : * Since it has to work in a multi-thread environment, we also have
99 : * a mutex.
100 : */
101 : namespace
102 : {
103 :
104 :
105 :
106 : /** \brief A map of configuration files.
107 : *
108 : * This typedef defines a type used to hold all the configuration files
109 : * that were loaded so far.
110 : *
111 : * The map is indexed by a string representing the full path to the
112 : * configuration file.
113 : *
114 : * The value is a shared pointer to configuration file. Since we may
115 : * share that data between multiple users, it made sense to force you
116 : * to use a configuration file smart pointer. Note, though, that we
117 : * never destroy the pointer until we quit (i.e. you cannot force a
118 : * re-load of the configuration file. Changes that happen in memory
119 : * are visible to all users, but changes to the actual configuration
120 : * file are complete invisible to use.)
121 : */
122 : typedef std::map<std::string, conf_file::pointer_t> conf_file_map_t;
123 :
124 :
125 : /** \brief The configuration files.
126 : *
127 : * This global defines a list of configuration files indexed by
128 : * filename (full path, but not the URL, just a path.)
129 : *
130 : * Whenever a configuration file is being retrieved with the
131 : * conf_file::get_conf_file() function, it is first searched
132 : * in this map. If it exists in the map, that version gets
133 : * used (if the URL of the two setups match one to one.)
134 : * If there is no such file in the map, then a new one is
135 : * created by loading the corresponding file.
136 : */
137 2 : conf_file_map_t g_conf_files = conf_file_map_t();
138 :
139 :
140 : } // no name namespace
141 :
142 :
143 :
144 :
145 :
146 : /** \brief Initialize the file setup object.
147 : *
148 : * This constructor initializes the setup object which can later be used
149 : * to search for an existing conf_file or creating a new conf_file.
150 : *
151 : * The setup holds the various parameters used to know how to load a
152 : * configuration file in memory. The parameters include
153 : *
154 : * \li \p filename -- the name of the file to read as a configuration file.
155 : * \li \p line_continuation -- how lines in the files are being read; in
156 : * most cases a line in a text file ends when a newline character (`\\n`)
157 : * is found; this parameter allows for lines that span (continue) on
158 : * multiple text lines. Only one type of continuation or no continue
159 : * (a.k.a. "single line") can be used per file.
160 : * \li \p assignment_operator -- the character(s) accepted between the
161 : * name of a variable and its value; by default this is the equal sign
162 : * (`=`). Multiple operators can be accepted.
163 : * \li \p comment -- how comments are introduced when supported. Multiple
164 : * introducers can be accepted within one file. By default we accept the
165 : * Unix Shell (`#`) and INI file (`;`) comment introducers.
166 : * \li \p section_operator -- the set of characters accepted as section
167 : * separator. By default we accept the INI file syntax (the `[section]`
168 : * syntax.)
169 : *
170 : * \note
171 : * If the filename represent an existing file, then the name is going to
172 : * get canonicalized before it gets saved in the structure. Otherwise it
173 : * gets saved as is.
174 : *
175 : * \param[in] filename A valid filename.
176 : * \param[in] line_continue One of the line_continuation_t values.
177 : * \param[in] assignment_operator A set of assignment operator flags.
178 : * \param[in] comment A set of comment flags.
179 : * \param[in] section_operator A set of section operator flags.
180 : */
181 28535 : conf_file_setup::conf_file_setup(
182 : std::string const & filename
183 : , line_continuation_t line_continuation
184 : , assignment_operator_t assignment_operator
185 : , comment_t comment
186 28535 : , section_operator_t section_operator)
187 : : f_original_filename(filename)
188 : , f_line_continuation(line_continuation)
189 28535 : , f_assignment_operator(assignment_operator == 0
190 28535 : ? ASSIGNMENT_OPERATOR_EQUAL
191 : : assignment_operator)
192 : , f_comment(comment)
193 57071 : , f_section_operator(section_operator)
194 : {
195 28535 : if(filename.empty())
196 : {
197 1 : throw getopt_invalid("trying to load a configuration file using an empty filename.");
198 : }
199 :
200 57068 : std::unique_ptr<char, decltype(&::free)> fn(realpath(filename.c_str(), nullptr), &::free);
201 28534 : if(fn != nullptr)
202 : {
203 28053 : f_filename = fn.get();
204 : }
205 : else
206 : {
207 481 : f_filename = filename;
208 : }
209 28534 : }
210 :
211 :
212 : /** \brief Check whether the setup is considered valid.
213 : *
214 : * This function is used to check whether the conf_file_setup is valid or
215 : * not. It is valid when everything is in order, which at this point means
216 : * the filename is not empty.
217 : *
218 : * All the other parameters are always viewed as being valid.
219 : *
220 : * \warning
221 : * The is_valid() always returns true at this time. We always save the
222 : * filename. I'm not totally sure why I wanted to not have a way to get
223 : * a valid configuration file by viewing a non-existing file as the same
224 : * as an empty file. Now that's what happens.
225 : *
226 : * \return true if the conf_file_setup is considered valid.
227 : */
228 25908 : bool conf_file_setup::is_valid() const
229 : {
230 25908 : return !f_filename.empty();
231 : }
232 :
233 :
234 : /** \brief Get the original filename.
235 : *
236 : * When creating a new conf_file_setup, you have to specify a filename.
237 : * This function returns that string exactly, without canonicalization.
238 : *
239 : * \return The filename as specified at the time of construction.
240 : *
241 : * \sa get_filename()
242 : */
243 25226 : std::string const & conf_file_setup::get_original_filename() const
244 : {
245 25226 : return f_original_filename;
246 : }
247 :
248 :
249 : /** \brief Get the filename.
250 : *
251 : * When creating a new conf_file_setup, you have to specify a filename.
252 : * This function returns that filename after it was canonicalized by
253 : * the constructor.
254 : *
255 : * The canonicalization process computes the full path to the real
256 : * file. If such does not exist then no filename is defined, so this
257 : * function may return an empty string.
258 : *
259 : * \return The filename or an empty string if the realpath() could not
260 : * be calculated.
261 : *
262 : * \sa get_original_filename()
263 : */
264 29277 : std::string const & conf_file_setup::get_filename() const
265 : {
266 29277 : return f_filename;
267 : }
268 :
269 :
270 : /** \brief Get the line continuation setting.
271 : *
272 : * This function returns the line continuation for this setup.
273 : *
274 : * This parameter is not a set of flags. We only support one type of
275 : * line continuation per file. Many continuations could be contradictory
276 : * if used simultaneously.
277 : *
278 : * The continuation setting is one of the following:
279 : *
280 : * \li line_continuation_t::single_line -- no continuation support; any
281 : * definition must be on one single line.
282 : * \li line_continuation_t::rfc_822 -- like email/HTTP, whitespace at
283 : * the start of the next line means that the current line continues there;
284 : * those whitespaces get removed from the value so if you want a space
285 : * between two lines, make sure to finish the current line with a space.
286 : * \li line_continuation_t::msdos -- `&` at end of the line.
287 : * \li line_continuation_t::unix -- `\` at end of the line.
288 : * \li line_continuation_t::fortran -- `&` at the start of the next line;
289 : * there cannot be any spaces, the `&` has to be the very first character.
290 : * \li line_continuation_t::semicolon -- `;` ends the _line_; when reading
291 : * a line with this continuation mode, the reader stops only when it finds
292 : * the `;` or EOF (also if a comment is found.)
293 : *
294 : * \return a line continuation mode.
295 : */
296 26287 : line_continuation_t conf_file_setup::get_line_continuation() const
297 : {
298 26287 : return f_line_continuation;
299 : }
300 :
301 :
302 : /** \brief Get the accepted assignment operators.
303 : *
304 : * This function returns the set of flags describing the list of
305 : * accepted operators one can use to do assignments.
306 : *
307 : * Right now we support the follow:
308 : *
309 : * \li ASSIGNMENT_OPERATOR_EQUAL -- the equal (`=`) character, like in
310 : * most Unix configuration files and shell scripts.
311 : * \li ASSIGNMENT_OPERATOR_COLON -- the colon (`:`) character, like in
312 : * email and HTTP headers.
313 : * \li ASSIGNMENT_OPERATOR_SPACE -- the space (` `) character; this is
314 : * less used, but many Unix configuration files still use this scheme.
315 : *
316 : * \todo
317 : * Add support for additional operators such as:
318 : * \todo
319 : * \li `+=` -- append data
320 : * \li `?=` -- set to this value if not yet set
321 : *
322 : * \return The set of accepted assignment operators.
323 : *
324 : * \sa is_assignment_operator()
325 : */
326 1144764 : assignment_operator_t conf_file_setup::get_assignment_operator() const
327 : {
328 1144764 : return f_assignment_operator;
329 : }
330 :
331 :
332 : /** Get the comment flags.
333 : *
334 : * This function returns the comment flags. These describe which type
335 : * of comments are supported in this configuration file.
336 : *
337 : * Currently we support:
338 : *
339 : * \li COMMENT_INI -- INI file like comments, these are introduced with
340 : * a semi-colon (`;`) and end with a newline.
341 : * \li COMMENT_SHELL -- Unix shell like comments, these are introduced
342 : * with a hash (`#`) and end with a newline.
343 : * \li COMMENT_CPP -- C++ like comments, these are introduced with two
344 : * slashes (`//`) and end with a newline.
345 : *
346 : * Right now we only support line comments. Configuration entries cannot
347 : * include comments. A comment character can be preceeded by spaces and
348 : * tabs.
349 : *
350 : * Line continuation is taken in account with comments. So the following
351 : * when the line continuation is set to Unix is one long comment:
352 : *
353 : * \code
354 : * # line continuation works with comments \
355 : * just like with any other line... because the \
356 : * continuation character and the newline characters \
357 : * just get removed before the get_line() function \
358 : * returns...
359 : * \endcode
360 : *
361 : * \return The comment flags.
362 : *
363 : * \sa is_comment()
364 : */
365 26216 : comment_t conf_file_setup::get_comment() const
366 : {
367 26216 : return f_comment;
368 : }
369 :
370 :
371 : /** \brief Get the accepted section operators.
372 : *
373 : * This function returns the flags representing which of the
374 : * section operators are accepted.
375 : *
376 : * We currently support the following types of sections:
377 : *
378 : * \li SECTION_OPERATOR_NONE -- no sections are accepted.
379 : * \li SECTION_OPERATOR_C -- the period (`.`) is viewed as a section/name
380 : * separator as when you access a variable member in a structure.
381 : * \li SECTION_OPERATOR_CPP -- the scope operator (`::`) is viewed as a
382 : * section/name separator; if used at the very beginning, it is viewed
383 : * as "global scope" and whatever other section is currently active is
384 : * ignored.
385 : * \li SECTION_OPERATOR_BLOCK -- the configuration files can include
386 : * opening (`{`) and closing (`}`) curvly brackets to group parameters
387 : * together; a name must preceed the opening bracket, it represents
388 : * the section name.
389 : * \li SECTION_OPERATOR_INI_FILE -- like in the MS-DOS .ini files, the
390 : * configuration file can include square brackets to mark sections; this
391 : * method limits the number of section names to one level.
392 : *
393 : * \bug
394 : * The INI file support does not verify that a section name does not
395 : * itself include more sub-sections. For example, the following would
396 : * be three section names:
397 : * \bug
398 : * \code
399 : * [a::b::c]
400 : * var=123
401 : * \endcode
402 : * \bug
403 : * So in effect, the variable named `var` ends up in section `a`,
404 : * sub-section `b`, and sub-sub-section `c` (or section `a::b::c`.)
405 : * Before saving the results in the parameters, all section operators
406 : * get transformed to the C++ scope (`::`) operator, which is why that
407 : * operator used in any name ends up looking like a section separator.
408 : */
409 44521 : section_operator_t conf_file_setup::get_section_operator() const
410 : {
411 44521 : return f_section_operator;
412 : }
413 :
414 :
415 : /** \brief Transform the setup in a URL.
416 : *
417 : * This function transforms the configuration file setup in a unique URL.
418 : * This URL allows us to verify that two setup are the same so when
419 : * attempting to reload the same configuration file, we can make sure
420 : * you are attempting to do so with the same URL.
421 : *
422 : * This is because trying to read the same file with, for example, line
423 : * continuation set to Unix the first time and then set to MS-DOS the
424 : * second time would not load the same thing is either line continuation
425 : * was used.
426 : *
427 : * \todo
428 : * We should look into have a set_config_url() or have a constructor
429 : * which accepts a URL.
430 : *
431 : * \return The URL representing this setup.
432 : */
433 42038 : std::string conf_file_setup::get_config_url() const
434 : {
435 42038 : if(f_url.empty())
436 : {
437 57124 : std::stringstream ss;
438 :
439 : ss << "file://"
440 57124 : << (f_filename.empty()
441 57124 : ? "/<empty>"
442 28562 : : f_filename);
443 :
444 57124 : std::vector<std::string> params;
445 28562 : if(f_line_continuation != line_continuation_t::line_continuation_unix)
446 : {
447 46274 : std::string name;
448 23137 : switch(f_line_continuation)
449 : {
450 4223 : case line_continuation_t::line_continuation_single_line:
451 4223 : name = "single-line";
452 4223 : break;
453 :
454 4727 : case line_continuation_t::line_continuation_rfc_822:
455 4727 : name = "rfc-822";
456 4727 : break;
457 :
458 4727 : case line_continuation_t::line_continuation_msdos:
459 4727 : name = "msdos";
460 4727 : break;
461 :
462 : // we should not ever receive this one since we don't enter
463 : // this block when the value is "unix"
464 : //
465 : //case line_continuation_t::line_continuation_unix:
466 : // name = "unix";
467 : // break;
468 :
469 4728 : case line_continuation_t::line_continuation_fortran:
470 4728 : name = "fortran";
471 4728 : break;
472 :
473 4727 : case line_continuation_t::line_continuation_semicolon:
474 4727 : name = "semi-colon";
475 4727 : break;
476 :
477 5 : default:
478 5 : throw getopt_logic_error("unexpected line continuation.");
479 :
480 : }
481 23132 : params.push_back("line-continuation=" + name);
482 : }
483 :
484 28557 : if(f_assignment_operator != ASSIGNMENT_OPERATOR_EQUAL)
485 : {
486 42326 : std::vector<std::string> assignments;
487 21163 : if((f_assignment_operator & ASSIGNMENT_OPERATOR_EQUAL) != 0)
488 : {
489 10577 : assignments.push_back("equal");
490 : }
491 21163 : if((f_assignment_operator & ASSIGNMENT_OPERATOR_COLON) != 0)
492 : {
493 14111 : assignments.push_back("colon");
494 : }
495 21163 : if((f_assignment_operator & ASSIGNMENT_OPERATOR_SPACE) != 0)
496 : {
497 14104 : assignments.push_back("space");
498 : }
499 21163 : if(!assignments.empty())
500 : {
501 21163 : params.push_back("assignment-operator=" + boost::algorithm::join(assignments, ","));
502 : }
503 : }
504 :
505 : if(f_comment != COMMENT_INI | COMMENT_SHELL)
506 : {
507 57114 : std::vector<std::string> comment;
508 28557 : if((f_comment & COMMENT_INI) != 0)
509 : {
510 12818 : comment.push_back("ini");
511 : }
512 28557 : if((f_comment & COMMENT_SHELL) != 0)
513 : {
514 12361 : comment.push_back("shell");
515 : }
516 28557 : if((f_comment & COMMENT_CPP) != 0)
517 : {
518 12379 : comment.push_back("cpp");
519 : }
520 28557 : if(comment.empty())
521 : {
522 3816 : params.push_back("comment=none");
523 : }
524 : else
525 : {
526 24741 : params.push_back("comment=" + boost::algorithm::join(comment, ","));
527 : }
528 : }
529 :
530 28557 : if(f_section_operator != SECTION_OPERATOR_INI_FILE)
531 : {
532 53106 : std::vector<std::string> section_operator;
533 26553 : if((f_section_operator & SECTION_OPERATOR_C) != 0)
534 : {
535 13005 : section_operator.push_back("c");
536 : }
537 26553 : if((f_section_operator & SECTION_OPERATOR_CPP) != 0)
538 : {
539 12996 : section_operator.push_back("cpp");
540 : }
541 26553 : if((f_section_operator & SECTION_OPERATOR_BLOCK) != 0)
542 : {
543 12991 : section_operator.push_back("block");
544 : }
545 26553 : if((f_section_operator & SECTION_OPERATOR_INI_FILE) != 0)
546 : {
547 11427 : section_operator.push_back("ini-file");
548 : }
549 26553 : if(!section_operator.empty())
550 : {
551 24439 : params.push_back("section-operator=" + boost::algorithm::join(section_operator, ","));
552 : }
553 : }
554 :
555 57114 : std::string const query_string(boost::algorithm::join(params, "&"));
556 28557 : if(!query_string.empty())
557 : {
558 : ss << '?'
559 28557 : << query_string;
560 : }
561 :
562 28557 : f_url = ss.str();
563 : }
564 :
565 42033 : return f_url;
566 : }
567 :
568 :
569 :
570 :
571 : /** \brief Create and read a conf_file.
572 : *
573 : * This function creates a new conf_file object unless one with the same
574 : * filename already exists.
575 : *
576 : * If the configuration file was already loaded, then that pointer gets
577 : * returned instead of reloading the file. There is currently no API to
578 : * allow for the removal because another thread or function may have
579 : * the existing pointer cached and we want all instances of a configuration
580 : * file to be the same (i.e. if you update the value of a parameter then
581 : * that new value should be visible by all the users of that configuration
582 : * file.) Therefore, you can think of a configuration file as a global
583 : * variable.
584 : *
585 : * \note
586 : * Any number of call this function to load a given file always returns
587 : * exactly the same pointer.
588 : *
589 : * \todo
590 : * With the communicator, we will at some point implement a class
591 : * used to detect that a file changed, allowing us to get a signal
592 : * and reload the file as required. This get_conf_file() function
593 : * will greatly benefit from such since that way we can automatically
594 : * reload the configuration file. In other words, process A could
595 : * make a change, then process B reloads and sees the change that
596 : * process A made. Such an implementation will require a proper
597 : * locking mechanism of the configuration files while modifications
598 : * are being performed.
599 : *
600 : * \param[in] setup The settings to be used in this configuration file reader.
601 : *
602 : * \return A pointer to the configuration file data.
603 : */
604 3328 : conf_file::pointer_t conf_file::get_conf_file(conf_file_setup const & setup)
605 : {
606 6656 : cppthread::guard lock(get_global_mutex());
607 :
608 3328 : auto it(g_conf_files.find(setup.get_filename()));
609 3328 : if(it != g_conf_files.end())
610 : {
611 3018 : if(it->second->get_setup().get_config_url() != setup.get_config_url())
612 : {
613 : throw getopt_logic_error("trying to load configuration file \""
614 5250 : + setup.get_config_url()
615 5250 : + "\" but an existing configuration file with the same name was loaded with URL: \""
616 10500 : + it->second->get_setup().get_config_url()
617 7875 : + "\".");
618 : }
619 393 : return it->second;
620 : }
621 :
622 : // TODO: look into not blocking forever?
623 : //
624 620 : conf_file::pointer_t cf(new conf_file(setup));
625 310 : g_conf_files[setup.get_filename()] = cf;
626 310 : return cf;
627 : }
628 :
629 :
630 : /** \brief Save the configuration file.
631 : *
632 : * This function saves the current data from this configuration file to
633 : * the file. It overwrites the existing file.
634 : *
635 : * Note that when you load the configuration, you may get data from
636 : * many different configuration files. This very file will only
637 : * include the data that was loaded from this file, though, and whatever
638 : * modifications you made.
639 : *
640 : * If the conf is not marked as modified, the function returns immediately
641 : * with true.
642 : *
643 : * \param[in] create_backup Whether to create a backup or not.
644 : *
645 : * \return true if the save worked as expected.
646 : */
647 2 : bool conf_file::save_configuration(bool create_backup)
648 : {
649 2 : if(f_modified)
650 : {
651 : // create backup?
652 : //
653 1 : if(create_backup)
654 : {
655 : // TODO: offer means to set the backup extension
656 : //
657 2 : std::string const backup_filename(f_setup.get_filename() + ".bak");
658 :
659 2 : if(unlink(backup_filename.c_str()) != 0
660 1 : && errno != ENOENT)
661 : {
662 : f_errno = errno; // LCOV_EXCL_LINE
663 : return false; // LCOV_EXCL_LINE
664 : }
665 :
666 1 : if(rename(f_setup.get_filename().c_str(), backup_filename.c_str()) != 0)
667 : {
668 : f_errno = errno; // LCOV_EXCL_LINE
669 : return false; // LCOV_EXCL_LINE
670 : }
671 : }
672 :
673 : // save parameters to file
674 : //
675 2 : std::ofstream conf;
676 1 : conf.open(f_setup.get_filename().c_str());
677 1 : if(!conf.is_open())
678 : {
679 : f_errno = errno; // LCOV_EXCL_LINE
680 : return false; // LCOV_EXCL_LINE
681 : }
682 :
683 1 : time_t const now(time(nullptr));
684 : tm t;
685 1 : gmtime_r(&now, &t);
686 : char str_date[16];
687 1 : strftime(str_date, sizeof(str_date), "%Y/%m/%d", &t);
688 : char str_time[16];
689 1 : strftime(str_time, sizeof(str_time), "%H:%M:%S", &t);
690 :
691 : // header warning with date & time
692 : //
693 1 : conf << "# This file was auto-generated by snap_config.cpp on " << str_date << " at " << str_time << "." << std::endl
694 1 : << "# Making modifications here is likely safe unless the tool handling this" << std::endl
695 1 : << "# configuration file is actively working on it while you do the edits." << std::endl;
696 4 : for(auto p : f_parameters)
697 : {
698 3 : conf << p.first << "=";
699 :
700 : // prevent saving \r and \n characters as is when part of the
701 : // value; also double \ otherwise reading those back would fail
702 : //
703 6 : std::string value(p.second);
704 3 : boost::replace_all(value, "\\", "\\\\");
705 3 : boost::replace_all(value, "\r", "\\r");
706 3 : boost::replace_all(value, "\n", "\\n");
707 3 : boost::replace_all(value, "\t", "\\t");
708 3 : conf << value << std::endl;
709 :
710 3 : if(!conf)
711 : {
712 : return false; // LCOV_EXCL_LINE
713 : }
714 : }
715 :
716 : // it all worked, it's considered saved now
717 : //
718 1 : f_modified = false;
719 : }
720 :
721 2 : return true;
722 : }
723 :
724 :
725 : /** \brief Initialize and read a configuration file.
726 : *
727 : * This constructor initializes this conf_file object and then reads the
728 : * corresponding configuration file.
729 : *
730 : * Note that you have to use the create_conf_file() function for you
731 : * to be able to create a configuration file. It is done that way became
732 : * a file can be read only once. Once loaded, it gets cached until your
733 : * application quits.
734 : *
735 : * \param[in] setup The configuration file setup.
736 : */
737 310 : conf_file::conf_file(conf_file_setup const & setup)
738 310 : : f_setup(setup)
739 : {
740 310 : read_configuration();
741 310 : }
742 :
743 :
744 : /** \brief Get the configuration file setup.
745 : *
746 : * This function returns a copy of the setup used to load this
747 : * configuration file.
748 : *
749 : * \note
750 : * This function has no mutex protection because the setup can't
751 : * change so there is no multi-thread protection necessary (the
752 : * fact that you hold a shared pointer to the conf_file object
753 : * is enough protection in this case.)
754 : *
755 : * \return A reference to this configuration file setup.
756 : */
757 5791 : conf_file_setup const & conf_file::get_setup() const
758 : {
759 5791 : return f_setup;
760 : }
761 :
762 :
763 : /** \brief Set a callback to detect when changes happen.
764 : *
765 : * This function is used to attach a callback to this file. This is
766 : * useful if you'd like to know when a change happen to a parameter
767 : * in this configuration file.
768 : *
769 : * The callback gets called when:
770 : *
771 : * \li The set_parameter() is called and the parameter gets created.
772 : * \li The set_parameter() is called and the parameter gets updated.
773 : * \li The erase_parameter() is called and the parameter gets erased.
774 : *
775 : * You can cancel your callback by calling this function again without
776 : * a target (i.e. `cf->set_callback(callback_t());`).
777 : *
778 : * To attach another object to your callback, you can either create
779 : * a callback which is attached to your object and a function
780 : * member or use std::bind() to attach the object to the function
781 : * call.
782 : *
783 : * \param[in] callback The new callback std::function.
784 : */
785 1 : void conf_file::set_callback(callback_t callback)
786 : {
787 1 : f_callback = callback;
788 1 : }
789 :
790 :
791 : /** \brief Get the error number opening/reading the configuration file.
792 : *
793 : * The class registers the errno value whenever an I/O error happens
794 : * while handling the configuration file. In most cases the function
795 : * is expected to return 0.
796 : *
797 : * The ENOENT error should not happen since the setup is going to be
798 : * marked as invalid when a configuration file does not exist and
799 : * you should not end up creation a conf_file object when that
800 : * happens. However, it is expected when you want to make some
801 : * changes to a few parameters and save them back to file (i.e.
802 : * the very first time there will be no file under the writable
803 : * configuration folder.)
804 : *
805 : * \return The last errno detected while accessing the configuration file.
806 : */
807 152 : int conf_file::get_errno() const
808 : {
809 304 : cppthread::guard lock(get_global_mutex());
810 :
811 304 : return f_errno;
812 : }
813 :
814 :
815 : /** \brief Get a list of sections.
816 : *
817 : * This function returns a copy of the list of sections defined in
818 : * this configuration file. In most cases, you should not need this
819 : * function since you are expected to know what parameters may be
820 : * defined. There are times though when it can be very practical.
821 : * For example, the options_config.cpp makes use of it since each
822 : * section is a parameter which we do not know the name of until
823 : * we have access to this array of sections.
824 : *
825 : * \note
826 : * We return a list because in a multithread environment another thread
827 : * may decide to make changes to the list of parameters which has the
828 : * side effect of eventually adding a section.
829 : *
830 : * \return A copy of the list of sections.
831 : */
832 697 : conf_file::sections_t conf_file::get_sections() const
833 : {
834 1394 : cppthread::guard lock(get_global_mutex());
835 :
836 1394 : return f_sections;
837 : }
838 :
839 :
840 : /** \brief Get a list of parameters.
841 : *
842 : * This function returns a copy of the list of parameters defined in
843 : * this configuration file.
844 : *
845 : * \note
846 : * We return a list because in a multithread environment another thread
847 : * may decide to make changes to the list of parameters (including
848 : * erasing a parameter.)
849 : *
850 : * \return A copy of the list of parameters.
851 : */
852 382 : conf_file::parameters_t conf_file::get_parameters() const
853 : {
854 764 : cppthread::guard lock(get_global_mutex());
855 :
856 764 : return f_parameters;
857 : }
858 :
859 :
860 : /** \brief Check whether a parameter is defined.
861 : *
862 : * This function checks for the existance of a parameter. It is a good
863 : * idea to first check for the existance of a parameter since the
864 : * get_parameter() function may otherwise return an empty string and
865 : * you cannot know whether that empty string means that the parameter
866 : * was not defined or it was set to the empty string.
867 : *
868 : * \param[in] name The name of the parameter to check.
869 : *
870 : * \return true if the parameter is defined, false otherwise.
871 : *
872 : * \sa get_parameter()
873 : * \sa set_parameter()
874 : */
875 622 : bool conf_file::has_parameter(std::string name) const
876 : {
877 622 : std::replace(name.begin(), name.end(), '_', '-');
878 :
879 1244 : cppthread::guard lock(get_global_mutex());
880 :
881 622 : auto it(f_parameters.find(name));
882 1244 : return it != f_parameters.end();
883 : }
884 :
885 :
886 : /** \brief Get the named parameter.
887 : *
888 : * This function searches for the specified parameter. If that parameter
889 : * exists, then its value is returned. Note that the value of a parameter
890 : * may be the empty string.
891 : *
892 : * If the parameter does not exist, the function returns the empty string.
893 : * To distinguish between an undefined parameter and a parameter set to
894 : * the empty string, use the has_parameter() function.
895 : *
896 : * \param[in] name The name of the parameter to retrieve.
897 : *
898 : * \return The current value of the parameter or an empty string.
899 : *
900 : * \sa has_parameter()
901 : * \sa set_parameter()
902 : */
903 609 : std::string conf_file::get_parameter(std::string name) const
904 : {
905 609 : std::replace(name.begin(), name.end(), '_', '-');
906 :
907 1218 : cppthread::guard lock(get_global_mutex());
908 :
909 609 : auto it(f_parameters.find(name));
910 609 : if(it != f_parameters.end())
911 : {
912 466 : return it->second;
913 : }
914 143 : return std::string();
915 : }
916 :
917 :
918 : /** \brief Set a parameter.
919 : *
920 : * This function sets a parameter to the specified value.
921 : *
922 : * The name of the value includes the \p section names and the \p name
923 : * parameter concatenated with a C++ scopre operator (::) in between
924 : * (unless \p section is the empty string in which case no scope operator
925 : * gets added.)
926 : *
927 : * When the \p name parameter starts with a scope parameter, the \p section
928 : * parameter is ignored. This allows one to ignore the current section
929 : * (i.e. the last '[...]' or any '\<name> { ... }').
930 : *
931 : * The \p section parameter is a list of section names separated by
932 : * the C++ scope operator (::).
933 : *
934 : * The \p name parameter may include C (.) and/or C++ (::) section
935 : * separators when the configuration file supports those. Internally,
936 : * those get moved to the \p section parameter. That allows us to
937 : * verify that the number of sections is valid.
938 : *
939 : * This function may be called any number of time. The last value is
940 : * the one kept. While reading the configuration file, though, a warning
941 : * is generated when a parameter gets overwritten since this is often the
942 : * source of a problem.
943 : *
944 : * In the following configuration file:
945 : *
946 : * \code
947 : * var=name
948 : * var=twice
949 : * \endcode
950 : *
951 : * The variable named `var` will be set to `twice` on return and a warning
952 : * will have been generated warning about the fact that the variable was
953 : * modified while reading the configuration file.
954 : *
955 : * The full name of the parameter (i.e. section + name) cannot include any
956 : * of the following characters:
957 : *
958 : * \li control characters (any character between 0x00 and 0x1F)
959 : * \li a space (0x20)
960 : * \li a backslash (`\`)
961 : * \li quotation (`"` and `'`)
962 : * \li comment (';', '#', '/')
963 : * \li assignment ('=', ':', '?', '+')
964 : *
965 : * \note
966 : * The \p section and \p name parameters have any underscore (`_`)
967 : * replaced with dashes (`-`) before getting used. The very first
968 : * character can be a dash. This allows you to therefore create
969 : * parameters which cannot appear in a configuration file, an
970 : * environment variable or on the command line (where parameter are
971 : * not allowed to start with a dash.)
972 : *
973 : * \warning
974 : * It is important to note that when a \p name includes a C++ scope
975 : * operator, the final parameter name looks like it includes a section
976 : * name (i.e. the name "a::b", when the C++ section flag is not set,
977 : * is accepted as is; so the final parameter name is going to be "a::b"
978 : * and therefore it will include what looks like a section name.)
979 : * There should not be any concern about this small \em glitch though
980 : * since you do not have to accept any such parameter.
981 : *
982 : * \param[in] section The list of section or an empty string.
983 : * \param[in] name The name of the parameter.
984 : * \param[in] value The value of the parameter.
985 : */
986 676 : bool conf_file::set_parameter(std::string section, std::string name, std::string const & value)
987 : {
988 : // use the tokenize_string() function because we do not want to support
989 : // quoted strings in this list of sections which our split_string()
990 : // does automatically
991 : //
992 1352 : string_list_t section_list;
993 :
994 676 : std::replace(section.begin(), section.end(), '_', '-');
995 676 : std::replace(name.begin(), name.end(), '_', '-');
996 :
997 676 : char const * n(name.c_str());
998 :
999 : // global scope? if so ignore the section parameter
1000 : //
1001 1352 : if((f_setup.get_section_operator() & SECTION_OPERATOR_CPP) != 0
1002 32 : && n[0] == ':'
1003 678 : && n[1] == ':')
1004 : {
1005 2 : do
1006 : {
1007 4 : ++n;
1008 : }
1009 4 : while(*n == ':');
1010 : }
1011 : else
1012 : {
1013 674 : snap::tokenize_string(section_list
1014 : , section
1015 : , "::"
1016 : , true
1017 1348 : , std::string()
1018 : , &snap::string_predicate<string_list_t>);
1019 : }
1020 :
1021 676 : char const * s(n);
1022 7832 : while(*n != '\0')
1023 : {
1024 7160 : if((f_setup.get_section_operator() & SECTION_OPERATOR_C) != 0
1025 3580 : && *n == '.')
1026 : {
1027 32 : if(s == n)
1028 : {
1029 2 : cppthread::log << cppthread::log_level_t::error
1030 1 : << "option name \""
1031 1 : << name
1032 1 : << "\" cannot start with a period (.)."
1033 1 : << cppthread::end;
1034 1 : return false;
1035 : }
1036 31 : section_list.push_back(std::string(s, n - s));
1037 8 : do
1038 : {
1039 39 : ++n;
1040 : }
1041 39 : while(*n == '.');
1042 31 : s = n;
1043 : }
1044 7096 : else if((f_setup.get_section_operator() & SECTION_OPERATOR_CPP) != 0
1045 66 : && n[0] == ':'
1046 3560 : && n[1] == ':')
1047 : {
1048 12 : if(s == n)
1049 : {
1050 2 : cppthread::log << cppthread::log_level_t::error
1051 1 : << "option name \""
1052 1 : << name
1053 1 : << "\" cannot start with a scope operator (::)."
1054 1 : << cppthread::end;
1055 1 : return false;
1056 : }
1057 11 : section_list.push_back(std::string(s, n - s));
1058 11 : do
1059 : {
1060 22 : ++n;
1061 : }
1062 22 : while(*n == ':');
1063 11 : s = n;
1064 : }
1065 : else
1066 : {
1067 3536 : ++n;
1068 : }
1069 : }
1070 674 : if(s == n)
1071 : {
1072 4 : cppthread::log << cppthread::log_level_t::error
1073 2 : << "option name \""
1074 2 : << name
1075 2 : << "\" cannot end with a section operator or be empty."
1076 2 : << cppthread::end;
1077 2 : return false;
1078 : }
1079 1344 : std::string param_name(s, n - s);
1080 :
1081 1344 : std::string const section_name(boost::algorithm::join(section_list, "::"));
1082 :
1083 1344 : if(f_setup.get_section_operator() == SECTION_OPERATOR_NONE
1084 672 : && !section_list.empty())
1085 : {
1086 2 : cppthread::log << cppthread::log_level_t::error
1087 1 : << "option name \""
1088 1 : << name
1089 1 : << "\" cannot be added to section \""
1090 1 : << section_name
1091 1 : << "\" because there is no section support for this configuration file."
1092 1 : << cppthread::end;
1093 1 : return false;
1094 : }
1095 1342 : if((f_setup.get_section_operator() & SECTION_OPERATOR_ONE_SECTION) != 0
1096 671 : && section_list.size() > 1)
1097 : {
1098 10 : cppthread::log << cppthread::log_level_t::error
1099 5 : << "option name \""
1100 5 : << name
1101 5 : << "\" cannot be added to section \""
1102 5 : << section_name
1103 5 : << "\" because this configuration only accepts one section level."
1104 5 : << cppthread::end;
1105 5 : return false;
1106 : }
1107 :
1108 666 : section_list.push_back(param_name);
1109 1332 : std::string const full_name(boost::algorithm::join(section_list, "::"));
1110 :
1111 : // verify that each section name only includes characters we accept
1112 : // for a parameter name
1113 : //
1114 : // WARNING: we do not test with full_name because it includes ':'
1115 : //
1116 1379 : for(auto sn : section_list)
1117 : {
1118 4453 : for(char const * f(sn.c_str()); *f != '\0'; ++f)
1119 : {
1120 3740 : switch(*f)
1121 : {
1122 109 : case '\001': // forbid controls
1123 : case '\002':
1124 : case '\003':
1125 : case '\004':
1126 : case '\005':
1127 : case '\006':
1128 : case '\007':
1129 : case '\010':
1130 : case '\011':
1131 : case '\012':
1132 : case '\013':
1133 : case '\014':
1134 : case '\015':
1135 : case '\016':
1136 : case '\017':
1137 : case '\020':
1138 : case '\021':
1139 : case '\022':
1140 : case '\023':
1141 : case '\024':
1142 : case '\025':
1143 : case '\026':
1144 : case '\027':
1145 : case '\030':
1146 : case '\031':
1147 : case '\032':
1148 : case '\033':
1149 : case '\034':
1150 : case '\035':
1151 : case '\036':
1152 : case '\037':
1153 : case ' ': // forbid spaces
1154 : case '\'': // forbid all quotes
1155 : case '"': // forbid all quotes
1156 : case ';': // forbid all comment operators
1157 : case '#': // forbid all comment operators
1158 : case '/': // forbid all comment operators
1159 : case '=': // forbid all assignment operators
1160 : case ':': // forbid all assignment operators
1161 : case '?': // forbid all assignment operators (for later)
1162 : case '+': // forbid all assignment operators (for later)
1163 : case '\\': // forbid backslashes
1164 218 : cppthread::log << cppthread::log_level_t::error
1165 109 : << "parameter \""
1166 109 : << full_name
1167 109 : << "\" on line "
1168 109 : << f_line
1169 109 : << " in configuration file \""
1170 109 : << f_setup.get_filename()
1171 109 : << "\" includes a character not acceptable for a section or parameter name (controls, space, quotes, and \";#/=:?+\\\")."
1172 109 : << cppthread::end;
1173 109 : return false;
1174 :
1175 : }
1176 : }
1177 : }
1178 :
1179 1114 : cppthread::guard lock(get_global_mutex());
1180 :
1181 : // add the section to the list of sections
1182 : //
1183 : // TODO: should we have a list of all the parent sections? Someone can
1184 : // write "a::b::c::d = 123" and we currently only get section
1185 : // "a::b::c", no section "a" and no section "a::b".
1186 : //
1187 557 : if(!section_name.empty())
1188 : {
1189 138 : f_sections.insert(section_name);
1190 : }
1191 :
1192 557 : callback_action_t action(callback_action_t::created);
1193 557 : auto it(f_parameters.find(full_name));
1194 557 : if(it == f_parameters.end())
1195 : {
1196 549 : f_parameters[full_name] = value;
1197 : }
1198 : else
1199 : {
1200 8 : if(f_reading)
1201 : {
1202 : // this is just a warning; it can be neat to know about such
1203 : // problems and fix them early
1204 : //
1205 4 : cppthread::log << cppthread::log_level_t::warning
1206 2 : << "parameter \""
1207 2 : << full_name
1208 2 : << "\" on line "
1209 2 : << f_line
1210 2 : << " in configuration file \""
1211 2 : << f_setup.get_filename()
1212 2 : << "\" was found twice in the same configuration file."
1213 2 : << cppthread::end;
1214 : }
1215 :
1216 8 : it->second = value;
1217 :
1218 8 : action = callback_action_t::updated;
1219 : }
1220 :
1221 557 : if(!f_reading)
1222 : {
1223 8 : f_modified = true;
1224 :
1225 8 : if(f_callback)
1226 : {
1227 4 : f_callback(shared_from_this(), action, full_name, value);
1228 : }
1229 : }
1230 :
1231 557 : return true;
1232 : }
1233 :
1234 :
1235 : /** \brief Erase the named parameter from this configuration file.
1236 : *
1237 : * This function can be used to remove the specified parameter from
1238 : * this configuration file.
1239 : *
1240 : * If that parameter is not defined in the file, then nothing happens.
1241 : *
1242 : * \param[in] name The name of the parameter to remove.
1243 : *
1244 : * \return true if the parameter was removed, false if it did not exist.
1245 : */
1246 2 : bool conf_file::erase_parameter(std::string name)
1247 : {
1248 2 : std::replace(name.begin(), name.end(), '_', '-');
1249 :
1250 2 : auto it(f_parameters.find(name));
1251 2 : if(it == f_parameters.end())
1252 : {
1253 1 : return false;
1254 : }
1255 :
1256 1 : f_parameters.erase(it);
1257 :
1258 1 : if(!f_reading)
1259 : {
1260 1 : f_modified = true;
1261 :
1262 1 : if(f_callback)
1263 : {
1264 1 : f_callback(shared_from_this(), callback_action_t::erased, name, std::string());
1265 : }
1266 : }
1267 :
1268 1 : return true;
1269 : }
1270 :
1271 :
1272 : /** \brief Check whether this configuration file was modified.
1273 : *
1274 : * This function returns the value of the f_modified flag which is true
1275 : * if any value was createed, updated, or erased from the configuration
1276 : * file since after it was loaded.
1277 : *
1278 : * This tells you whether you should call the save() function, assuming
1279 : * you want to keep such changes.
1280 : *
1281 : * \return true if changes were made to this file parameters.
1282 : */
1283 7 : bool conf_file::was_modified() const
1284 : {
1285 7 : return f_modified;
1286 : }
1287 :
1288 :
1289 : /** \brief Read one characte from the input stream.
1290 : *
1291 : * This function reads one character from the input stream and returns it
1292 : * as an `int`.
1293 : *
1294 : * If there is an ungotten character (i.e. ungetc() was called) then that
1295 : * character is returned.
1296 : *
1297 : * When the end of the file is reached, this function returns -1.
1298 : *
1299 : * \note
1300 : * This function is oblivious of UTF-8. It should not matter since any
1301 : * Unicode character would anyway be treated as is.
1302 : *
1303 : * \param[in,out] in The input stream.
1304 : *
1305 : * \return The character read or -1 when EOF is reached.
1306 : */
1307 13681 : int conf_file::getc(std::ifstream & in)
1308 : {
1309 13681 : if(f_unget_char != '\0')
1310 : {
1311 34 : int const r(f_unget_char);
1312 34 : f_unget_char = '\0';
1313 34 : return r;
1314 : }
1315 :
1316 : char c;
1317 13647 : in.get(c);
1318 :
1319 13647 : if(!in)
1320 : {
1321 217 : return EOF;
1322 : }
1323 :
1324 13430 : return static_cast<std::uint8_t>(c);
1325 : }
1326 :
1327 :
1328 : /** \brief Restore one character.
1329 : *
1330 : * This function is used whenever we read one additional character to
1331 : * know whether a certain character followed another. For example, we
1332 : * check for a `'\\n'` whenever we find a `'\\r'`. However, if the
1333 : * character right after the `'\\r'` is not a `'\\n'` we call this
1334 : * ungetc() function so next time we can re-read that same character.
1335 : *
1336 : * \note
1337 : * You can call ungetc() only once between calls to getc(). The
1338 : * current buffer is just one single character. Right now our
1339 : * parser doesn't need more than that.
1340 : *
1341 : * \param[in] c The character to restore.
1342 : */
1343 34 : void conf_file::ungetc(int c)
1344 : {
1345 34 : if(f_unget_char != '\0')
1346 : {
1347 : throw getopt_logic_error("conf_file::ungetc() called when the f_unget_char variable member is not '\\0'."); // LCOV_EXCL_LINE
1348 : }
1349 34 : f_unget_char = c;
1350 34 : }
1351 :
1352 :
1353 : /** \brief Get one line.
1354 : *
1355 : * This function reads one line. The function takes the line continuation
1356 : * setup in account. So for example a line that ends with a backslash
1357 : * continues on the next line when the line continuation is setup to Unix.
1358 : *
1359 : * Note that by default comments are also continued. So a backslash in
1360 : * Unix mode continues a comment on the next line.
1361 : *
1362 : * There is a special case with the semicolon continuation setup. When
1363 : * the line starts as a comment, it will end on the first standalone
1364 : * newline (i.e. a comment does not need to end with a semi-colon.)
1365 : *
1366 : * \param[in,out] in The input stream.
1367 : * \param[out] line Where the line gets saved.
1368 : *
1369 : * \return true if a line was read, false on EOF.
1370 : */
1371 1106 : bool conf_file::get_line(std::ifstream & in, std::string & line)
1372 : {
1373 1106 : line.clear();
1374 :
1375 : for(;;)
1376 : {
1377 13595 : int c(getc(in));
1378 13595 : if(c == EOF)
1379 : {
1380 216 : return false;
1381 : }
1382 13379 : if(c == ';'
1383 13379 : && f_setup.get_line_continuation() == line_continuation_t::line_continuation_semicolon)
1384 : {
1385 1 : return true;
1386 : }
1387 :
1388 13440 : while(c == '\n' || c == '\r')
1389 : {
1390 : // count the "\r\n" sequence as one line
1391 : //
1392 919 : if(c == '\r')
1393 : {
1394 19 : c = getc(in);
1395 19 : if(c != '\n')
1396 : {
1397 3 : ungetc(c);
1398 : }
1399 19 : c = '\n';
1400 : }
1401 :
1402 919 : ++f_line;
1403 919 : switch(f_setup.get_line_continuation())
1404 : {
1405 76 : case line_continuation_t::line_continuation_single_line:
1406 : // continuation support
1407 76 : return true;
1408 :
1409 17 : case line_continuation_t::line_continuation_rfc_822:
1410 17 : c = getc(in);
1411 17 : if(!iswspace(c))
1412 : {
1413 15 : ungetc(c);
1414 15 : return true;
1415 : }
1416 2 : do
1417 : {
1418 4 : c = getc(in);
1419 : }
1420 4 : while(iswspace(c));
1421 2 : break;
1422 :
1423 17 : case line_continuation_t::line_continuation_msdos:
1424 34 : if(line.empty()
1425 17 : || line.back() != '&')
1426 : {
1427 16 : return true;
1428 : }
1429 1 : line.pop_back();
1430 1 : c = getc(in);
1431 1 : break;
1432 :
1433 775 : case line_continuation_t::line_continuation_unix:
1434 1550 : if(line.empty()
1435 775 : || line.back() != '\\')
1436 : {
1437 764 : return true;
1438 : }
1439 11 : line.pop_back();
1440 11 : c = getc(in);
1441 11 : break;
1442 :
1443 17 : case line_continuation_t::line_continuation_fortran:
1444 17 : c = getc(in);
1445 17 : if(c != '&')
1446 : {
1447 16 : ungetc(c);
1448 16 : return true;
1449 : }
1450 1 : c = getc(in);
1451 1 : break;
1452 :
1453 17 : case line_continuation_t::line_continuation_semicolon:
1454 : // if we have a comment, we want to return immediately;
1455 : // at this time, the comments are not multi-line so
1456 : // the call can return true only if we were reading the
1457 : // very first line
1458 : //
1459 17 : if(is_comment(line.c_str()))
1460 : {
1461 1 : return true;
1462 : }
1463 : // the semicolon is checked earlier, just keep the newline
1464 : // in this case (but not at the start)
1465 : //
1466 16 : if(!line.empty() || c != '\n')
1467 : {
1468 15 : line += c;
1469 : }
1470 16 : c = getc(in);
1471 16 : break;
1472 :
1473 : }
1474 : }
1475 :
1476 : // we just read the last line
1477 12490 : if(c == EOF)
1478 : {
1479 1 : return true;
1480 : }
1481 :
1482 12489 : line += c;
1483 12489 : }
1484 : }
1485 :
1486 :
1487 : /** \brief Read a configuration file.
1488 : *
1489 : * This function reads a configuration file and saves all the parameters it
1490 : * finds in a map which can later be checked against an option table for
1491 : * validation.
1492 : *
1493 : * \todo
1494 : * Add support for quotes in configuration files as parameters are otherwise
1495 : * saved as a separated list of parameters losing the number of spaces between
1496 : * each entry.
1497 : */
1498 310 : void conf_file::read_configuration()
1499 : {
1500 526 : snap::safe_variable<decltype(f_reading)> safe_reading(f_reading, true);
1501 :
1502 526 : std::ifstream conf(f_setup.get_filename());
1503 310 : if(!conf)
1504 : {
1505 94 : f_errno = errno;
1506 94 : return;
1507 : }
1508 :
1509 432 : std::string current_section;
1510 432 : std::vector<std::string> sections;
1511 432 : std::string str;
1512 216 : f_line = 0;
1513 1996 : while(get_line(conf, str))
1514 : {
1515 890 : char const * s(str.c_str());
1516 986 : while(iswspace(*s))
1517 : {
1518 48 : ++s;
1519 : }
1520 1780 : if(*s == '\0'
1521 890 : || is_comment(s))
1522 : {
1523 : // skip empty lines and comments
1524 349 : continue;
1525 : }
1526 1449 : if((f_setup.get_section_operator() & SECTION_OPERATOR_BLOCK) != 0
1527 722 : && *s == '}')
1528 : {
1529 5 : current_section = sections.back();
1530 5 : sections.pop_back();
1531 5 : continue;
1532 : }
1533 717 : char const * str_name(s);
1534 717 : char const * e(nullptr);
1535 8467 : while(!is_assignment_operator(*s)
1536 3953 : && ((f_setup.get_section_operator() & SECTION_OPERATOR_BLOCK) == 0 || (*s != '{' && *s != '}'))
1537 3953 : && ((f_setup.get_section_operator() & SECTION_OPERATOR_INI_FILE) == 0 || *s != ']')
1538 3915 : && *s != '\0'
1539 8492 : && !iswspace(*s))
1540 : {
1541 3875 : ++s;
1542 : }
1543 717 : if(iswspace(*s))
1544 : {
1545 28 : e = s;
1546 164 : while(iswspace(*s))
1547 : {
1548 68 : ++s;
1549 : }
1550 59 : if(*s != '\0'
1551 28 : && !is_assignment_operator(*s)
1552 12 : && (f_setup.get_assignment_operator() & ASSIGNMENT_OPERATOR_SPACE) == 0
1553 37 : && ((f_setup.get_section_operator() & SECTION_OPERATOR_BLOCK) == 0 || (*s != '{' && *s != '}')))
1554 : {
1555 6 : cppthread::log << cppthread::log_level_t::error
1556 3 : << "option name from \""
1557 3 : << str
1558 3 : << "\" on line "
1559 3 : << f_line
1560 3 : << " in configuration file \""
1561 3 : << f_setup.get_filename()
1562 3 : << "\" cannot include a space, missing assignment operator?"
1563 3 : << cppthread::end;
1564 3 : continue;
1565 : }
1566 : }
1567 714 : if(e == nullptr)
1568 : {
1569 689 : e = s;
1570 : }
1571 715 : if(e - str_name == 0)
1572 : {
1573 2 : cppthread::log << cppthread::log_level_t::error
1574 1 : << "no option name in \""
1575 1 : << str
1576 1 : << "\" on line "
1577 1 : << f_line
1578 1 : << " from configuration file \""
1579 1 : << f_setup.get_filename()
1580 1 : << "\", missing name before the assignment operator?"
1581 1 : << cppthread::end;
1582 1 : continue;
1583 : }
1584 1422 : std::string name(str_name, e - str_name);
1585 713 : std::replace(name.begin(), name.end(), '_', '-');
1586 715 : if(name[0] == '-')
1587 : {
1588 4 : cppthread::log << cppthread::log_level_t::error
1589 2 : << "option names in configuration files cannot start with a dash or an underscore in \""
1590 2 : << str
1591 2 : << "\" on line "
1592 2 : << f_line
1593 2 : << " from configuration file \""
1594 2 : << f_setup.get_filename()
1595 2 : << "\"."
1596 2 : << cppthread::end;
1597 2 : continue;
1598 : }
1599 1422 : if((f_setup.get_section_operator() & SECTION_OPERATOR_INI_FILE) != 0
1600 222 : && name.length() >= 1
1601 222 : && name[0] == '['
1602 749 : && *s == ']')
1603 : {
1604 38 : ++s;
1605 39 : if(!sections.empty())
1606 : {
1607 2 : cppthread::log << cppthread::log_level_t::error
1608 1 : << "`[...]` sections can't be used within a `section { ... }` on line "
1609 1 : << f_line
1610 1 : << " from configuration file \""
1611 1 : << f_setup.get_filename()
1612 1 : << "\"."
1613 1 : << cppthread::end;
1614 1 : continue;
1615 : }
1616 41 : while(iswspace(*s))
1617 : {
1618 2 : ++s;
1619 : }
1620 75 : if(*s != '\0'
1621 37 : && !is_comment(s))
1622 : {
1623 2 : cppthread::log << cppthread::log_level_t::error
1624 1 : << "section names in configuration files cannot be followed by anything other than spaces in \""
1625 1 : << str
1626 1 : << "\" on line "
1627 1 : << f_line
1628 1 : << " from configuration file \""
1629 1 : << f_setup.get_filename()
1630 1 : << "\"."
1631 1 : << cppthread::end;
1632 1 : continue;
1633 : }
1634 36 : if(name.length() == 1)
1635 : {
1636 : // "[]" removes the section
1637 : //
1638 1 : current_section.clear();
1639 : }
1640 : else
1641 : {
1642 35 : current_section = name.substr(1);
1643 35 : current_section += "::";
1644 : }
1645 : }
1646 1346 : else if((f_setup.get_section_operator() & SECTION_OPERATOR_BLOCK) != 0
1647 673 : && *s == '{')
1648 : {
1649 6 : sections.push_back(current_section);
1650 6 : current_section += name;
1651 6 : current_section += "::";
1652 : }
1653 : else
1654 : {
1655 667 : if(is_assignment_operator(*s))
1656 : {
1657 649 : ++s;
1658 : }
1659 701 : while(iswspace(*s))
1660 : {
1661 17 : ++s;
1662 : }
1663 681 : for(e = str.c_str() + str.length(); e > s; --e)
1664 : {
1665 665 : if(!iswspace(e[-1]))
1666 : {
1667 651 : break;
1668 : }
1669 : }
1670 667 : size_t const len(e - s);
1671 1334 : std::string value(s, len);
1672 667 : boost::replace_all(value, "\\\\", "\\");
1673 667 : boost::replace_all(value, "\\r", "\r");
1674 667 : boost::replace_all(value, "\\n", "\n");
1675 667 : boost::replace_all(value, "\\t", "\t");
1676 667 : set_parameter(current_section, name, unquote(value));
1677 : }
1678 : }
1679 216 : if(!conf.eof())
1680 : {
1681 : f_errno = errno; // LCOV_EXCL_LINE
1682 : cppthread::log << cppthread::log_level_t::error // LCOV_EXCL_LINE
1683 : << "an error occurred while reading line " // LCOV_EXCL_LINE
1684 : << f_line // LCOV_EXCL_LINE
1685 : << " of configuration file \"" // LCOV_EXCL_LINE
1686 : << f_setup.get_filename() // LCOV_EXCL_LINE
1687 : << "\"." // LCOV_EXCL_LINE
1688 : << cppthread::end; // LCOV_EXCL_LINE
1689 : }
1690 216 : if(!sections.empty())
1691 : {
1692 2 : cppthread::log << cppthread::log_level_t::error
1693 1 : << "unterminated `section { ... }`, the `}` is missing in configuration file \""
1694 1 : << f_setup.get_filename()
1695 1 : << "\"."
1696 1 : << cppthread::end;
1697 : }
1698 : }
1699 :
1700 :
1701 : /** \brief Check whether `c` is an assignment operator.
1702 : *
1703 : * This function checks the \p c parameter to know whether it matches
1704 : * one of the character allowed as an assignment character.
1705 : *
1706 : * \param[in] c The character to be checked.
1707 : *
1708 : * \return true if c is considered to represent an assignment character.
1709 : */
1710 1119399 : bool conf_file::is_assignment_operator(int c) const
1711 : {
1712 1119399 : assignment_operator_t const assignment_operator(f_setup.get_assignment_operator());
1713 2238665 : return ((assignment_operator & ASSIGNMENT_OPERATOR_EQUAL) != 0 && c == '=')
1714 1118119 : || ((assignment_operator & ASSIGNMENT_OPERATOR_COLON) != 0 && c == ':')
1715 2237496 : || ((assignment_operator & ASSIGNMENT_OPERATOR_SPACE) != 0 && std::iswspace(c));
1716 : }
1717 :
1718 :
1719 : /** \brief Check whether the string starts with a comment introducer.
1720 : *
1721 : * This function checks whether the \p s string starts with a comment.
1722 : *
1723 : * We support different types of comment introducers. This function
1724 : * checks the flags as defined in the constructor and returns true
1725 : * if the type of character introducer defines a comment.
1726 : *
1727 : * We currently support:
1728 : *
1729 : * \li .ini file comments, introduced by a semi-colon (;)
1730 : *
1731 : * \li Shell file comments, introduced by a hash character (#)
1732 : *
1733 : * \li C++ comment, introduced by two slashes (//)
1734 : *
1735 : * \param[in] s The string to check for a comment.
1736 : *
1737 : * \return `true` if the string represents a comment.
1738 : */
1739 863 : bool conf_file::is_comment(char const * s) const
1740 : {
1741 863 : comment_t const comment(f_setup.get_comment());
1742 863 : if((comment & COMMENT_INI) != 0
1743 269 : && *s == ';')
1744 : {
1745 5 : return true;
1746 : }
1747 :
1748 858 : if((comment & COMMENT_SHELL) != 0
1749 527 : && *s == '#')
1750 : {
1751 114 : return true;
1752 : }
1753 :
1754 744 : if((comment & COMMENT_CPP) != 0
1755 10 : && s[0] == '/'
1756 5 : && s[1] == '/')
1757 : {
1758 5 : return true;
1759 : }
1760 :
1761 739 : return false;
1762 : }
1763 :
1764 :
1765 : /** \brief Returns true if \p c is considered to be a whitespace.
1766 : *
1767 : * Our iswspace() function is equivalent to the std::iswspace() function
1768 : * except that `'\\r'` and `'\\n'` are never viewed as white spaces.
1769 : *
1770 : * \return true if c is considered to be a white space character.
1771 : */
1772 1121172 : bool iswspace(int c)
1773 : {
1774 : return c != '\n'
1775 1121166 : && c != '\r'
1776 2242337 : && std::iswspace(c);
1777 : }
1778 :
1779 :
1780 6 : } // namespace advgetopt
1781 : // vim: ts=4 sw=4 et
|