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.
54 * List of operations used on items.
55 * This macro is used to generate internal source code,
56 * but you may be interested in the actions it creates.
58 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(ALL) \
59 T(APPEND) T(PREPEND) T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY)
60 /* Closing brace finishes previous block.
61 * Basic attributes (static, dynamic, parsed) can be used with SET.
62 * Dynamic arrays can be used with SET, APPEND, PREPEND.
63 * Sections can be used with SET.
64 * Lists can be used with everything. */
66 enum cf_operation { CF_OPERATIONS }; /** Allowed operations on items. See <<def_CF_OPERATIONS,`CF_OPERATIONS`>>. **/
71 * Searches for a configuration item called @name.
72 * If it is found, it is copied into @item and NULL is returned.
73 * Otherwise, an error is returned and @item is zeroed.
75 char *cf_find_item(const char *name, struct cf_item *item);
77 * Performs a single operation on a given item.
79 char *cf_modify_item(struct cf_item *item, enum cf_operation op, int number, char **pars);
89 * Take everything and write it into @fb.
91 void cf_dump_sections(struct fastbuf *fb);
98 * The configuration system uses journaling to safely reload
99 * configuration. It begins a transaction and tries to load the
100 * configuration. If it fails, it restores the original state.
102 * The behaviour of journal is described in <<reload,reloading configuration>>.
105 struct cf_journal_item; /** Opaque identifier of the journal state. **/
107 * Starts a new transaction. It returns the current state so you can
108 * get back to it. The @new_pool parameter tells if a new memory pool
109 * should be created and used from now.
111 struct cf_journal_item *cf_journal_new_transaction(uns new_pool);
113 * Marks current state as a complete transaction. The @new_pool
114 * parameter tells if the transaction was created with new memory pool
115 * (the parameter must be the same as the one with
116 * @cf_journal_new_transaction() was called with). The @oldj parameter
117 * is the journal state returned from last
118 * @cf_journal_new_transaction() call.
120 void cf_journal_commit_transaction(uns new_pool, struct cf_journal_item *oldj);
122 * Returns to an old journal state, reverting anything the current
123 * transaction did. The @new_pool parameter must be the same as the
124 * one you used when you created the transaction. The @oldj parameter
125 * is the journal state you got from @cf_journal_new_transaction() --
126 * it is the state to return to.
128 void cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj);
132 * Loading by @cf_getopt()
133 * ~~~~~~~~~~~~~~~~~~~~~~~
137 * Short options for loading configuration by @cf_getopt().
138 * Prepend to your own options.
140 #define CF_SHORT_OPTS "C:S:"
142 * Long options for loading configuration by @cf_getopt().
143 * Prepend to your own options.
145 #define CF_LONG_OPTS {"config", 1, 0, 'C'}, {"set", 1, 0, 'S'}, CF_LONG_OPTS_DEBUG
147 * Use this constant as @long_opts parameter of @cf_getopt() if you do
148 * not have any long options in your program.
150 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
152 #define CF_USAGE_TAB ""
155 * This macro provides text describing usage of the configuration
156 * loading options. Concatenate with description of your options and
157 * write to the user, if he/she provides invalid options.
160 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
161 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG
164 #define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } ,
165 #define CF_USAGE_DEBUG " --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n"
167 #define CF_LONG_OPTS_DEBUG
168 #define CF_USAGE_DEBUG
172 * Takes care of parsing the command-line arguments, loading the
173 * default configuration file (<<var_cf_def_file,`cf_def_file`>>) and processing
174 * configuration options. The calling convention is the same as with GNU getopt_long(),
175 * but you must prefix your own short/long options by the
176 * <<def_CF_LONG_OPTS,`CF_LONG_OPTS`>> or <<def_CF_SHORT_OPTS,`CF_SHORT_OPTS`>>or
177 * pass <<def_CF_NO_LONG_OPTS,`CF_NO_LONG_OPTS`>> if there are no long options.
179 * The default configuration file can be overwritten by the --config options,
180 * which must come first. During parsing of all other options, the configuration
181 * is already available.
183 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);