2 * UCW Library -- Parsing of configuration and command-line options
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2006 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 #ifdef CONFIG_OWN_GETOPT
15 #include "ucw/getopt/getopt-sh.h"
20 void reset_getopt(void); /** If you want to start parsing of the arguments from the first one again. **/
24 * Safe configuration loading
25 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
27 * These functions can be used to to safely load or reload configuration.
31 * The default config (DEFAULT_CONFIG config option) or NULL if already loaded.
32 * You can set it to something else manually.
34 extern char *cf_def_file; /* DEFAULT_CONFIG; NULL if already loaded */
35 extern char *cf_env_file; /* ENV_VAR_CONFIG */
36 int cf_reload(const char *file); /** Reload configuration from @file, replace the old one. **/
37 int cf_load(const char *file); /** Load configuration from @file. **/
39 * Parse some part of configuration passed in @string.
40 * The syntax is the same as in the <<config:,configuration file>>.
42 int cf_set(const char *string);
49 * Direct access to configuration items.
50 * You probably should not need this.
53 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(ALL) \
54 T(APPEND) T(PREPEND) T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY)
55 /* Closing brace finishes previous block.
56 * Basic attributes (static, dynamic, parsed) can be used with SET.
57 * Dynamic arrays can be used with SET, APPEND, PREPEND.
58 * Sections can be used with SET.
59 * Lists can be used with everything. */
61 enum cf_operation { CF_OPERATIONS };
66 * Searches for a configuration item called @name.
67 * If it is found, it is copied into @item and NULL is returned.
68 * Otherwise, an error is returned and @item is zeroed.
70 char *cf_find_item(const char *name, struct cf_item *item);
71 // TODO What does this do?
72 char *cf_write_item(struct cf_item *item, enum cf_operation op, int number, char **pars);
82 * Take everything and write it into @fb.
84 void cf_dump_sections(struct fastbuf *fb);
92 struct cf_journal_item;
94 struct cf_journal_item *cf_journal_new_transaction(uns new_pool);
95 void cf_journal_commit_transaction(uns new_pool, struct cf_journal_item *oldj);
96 void cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj);
100 * Loading by @cf_getopt()
101 * ~~~~~~~~~~~~~~~~~~~~~~~
105 * Short options for loading configuration by @cf_getopt().
106 * Prepend to your own options.
108 #define CF_SHORT_OPTS "C:S:"
110 * Long options for loading configuration by @cf_getopt().
111 * Prepend to your own options.
113 #define CF_LONG_OPTS {"config", 1, 0, 'C'}, {"set", 1, 0, 'S'}, CF_LONG_OPTS_DEBUG
115 * Use this constant as @long_opts parameter of @cf_getopt() if you do
116 * not have any long options in your program.
118 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
120 #define CF_USAGE_TAB ""
123 * This macro provides text describing usage of the configuration
124 * loading options. Concatenate with description of your options and
125 * write to the user, if he/she provides invalid options.
128 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
129 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG
132 #define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } ,
133 #define CF_USAGE_DEBUG " --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n"
135 #define CF_LONG_OPTS_DEBUG
136 #define CF_USAGE_DEBUG
140 * Takes care of parsing the command-line arguments, loading the
141 * default configuration file (<<var_cf_def_file,`cf_def_file`>>) and processing
142 * configuration options. The calling convention is the same as with GNU getopt_long(),
143 * but you must prefix your own short/long options by the
144 * <<def_CF_LONG_OPTS,`CF_LONG_OPTS`>> or <<def_CF_SHORT_OPTS,`CF_SHORT_OPTS`>>or
145 * pass <<def_CF_NO_LONG_OPTS,`CF_NO_LONG_OPTS`>> if there are no long options.
147 * The default configuration file can be overwriten by the --config options,
148 * which must come first. During parsing of all other options, the configuration
149 * is already available.
151 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);