]> mj.ucw.cz Git - libucw.git/blob - ucw/getopt.h
453d382ed83948c8b8defc77873c6250a385d201
[libucw.git] / ucw / getopt.h
1 /*
2  *      UCW Library -- Parsing of configuration and command-line options
3  *
4  *      (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2006 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_GETOPT_H
12 #define _UCW_GETOPT_H
13
14 #ifdef CONFIG_OWN_GETOPT
15 #include "ucw/getopt/getopt-sh.h"
16 #else
17 #include <getopt.h>
18 #endif
19
20 void reset_getopt(void);        /** If you want to start parsing of the arguments from the first one again. **/
21
22 /***
23  * [[conf_load]]
24  * Safe configuration loading
25  * ~~~~~~~~~~~~~~~~~~~~~~~~~~
26  *
27  * These functions can be used to to safely load or reload configuration.
28  */
29
30 /**
31  * The default config (DEFAULT_CONFIG config option) or NULL if already loaded.
32  * You can set it to something else manually.
33  */
34 extern char *cf_def_file;
35 /**
36  * Name of environment variable that can override what configuration
37  * is loaded.
38  **/
39 extern char *cf_env_file;
40 int cf_reload(const char *file);        /** Reload configuration from @file, replace the old one. **/
41 int cf_load(const char *file);          /** Load configuration from @file. **/
42 /**
43  * Parse some part of configuration passed in @string.
44  * The syntax is the same as in the <<config:,configuration file>>.
45  **/
46 int cf_set(const char *string);
47
48 /***
49  * [[conf_direct]]
50  * Direct access
51  * ~~~~~~~~~~~~~
52  *
53  * Direct access to configuration items.
54  * You probably should not need this.
55  ***/
56
57 /**
58  * List of operations used on items.
59  * This macro is used to generate internal source code,
60  * but you may be interested in the actions it creates.
61  **/
62 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(ALL) \
63   T(APPEND) T(PREPEND) T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY)
64   /* Closing brace finishes previous block.
65    * Basic attributes (static, dynamic, parsed) can be used with SET.
66    * Dynamic arrays can be used with SET, APPEND, PREPEND.
67    * Sections can be used with SET.
68    * Lists can be used with everything. */
69 #define T(x) OP_##x,
70 enum cf_operation { CF_OPERATIONS };    /** Allowed operations on items. See <<def_CF_OPERATIONS,`CF_OPERATIONS`>>. **/
71 #undef T
72
73 struct cf_item;
74 /**
75  * Searches for a configuration item called @name.
76  * If it is found, it is copied into @item and NULL is returned.
77  * Otherwise, an error is returned and @item is zeroed.
78  **/
79 char *cf_find_item(const char *name, struct cf_item *item);
80 /**
81  * Performs a single operation on a given item.
82  **/
83 char *cf_modify_item(struct cf_item *item, enum cf_operation op, int number, char **pars);
84
85 /***
86  * [[conf_dump]]
87  * Debug dumping
88  * ~~~~~~~~~~~~~
89  ***/
90
91 struct fastbuf;
92 /**
93  * Take everything and write it into @fb.
94  **/
95 void cf_dump_sections(struct fastbuf *fb);
96
97 /***
98  * [[conf_journal]]
99  * Journaling control
100  * ~~~~~~~~~~~~~~~~~~
101  *
102  * The configuration system uses journaling to safely reload
103  * configuration. It begins a transaction and tries to load the
104  * configuration. If it fails, it restores the original state.
105  *
106  * The behaviour of journal is described in <<reload,reloading configuration>>.
107  ***/
108
109 struct cf_journal_item;         /** Opaque identifier of the journal state. **/
110 /**
111  * Starts a new transaction. It returns the current state so you can
112  * get back to it. The @new_pool parameter tells if a new memory pool
113  * should be created and used from now.
114  **/
115 struct cf_journal_item *cf_journal_new_transaction(uns new_pool);
116 /**
117  * Marks current state as a complete transaction. The @new_pool
118  * parameter tells if the transaction was created with new memory pool
119  * (the parameter must be the same as the one with
120  * @cf_journal_new_transaction() was called with). The @oldj parameter
121  * is the journal state returned from last
122  * @cf_journal_new_transaction() call.
123  **/
124 void cf_journal_commit_transaction(uns new_pool, struct cf_journal_item *oldj);
125 /**
126  * Returns to an old journal state, reverting anything the current
127  * transaction did. The @new_pool parameter must be the same as the
128  * one you used when you created the transaction. The @oldj parameter
129  * is the journal state you got from @cf_journal_new_transaction() --
130  * it is the state to return to.
131  **/
132 void cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj);
133
134 /***
135  * [[conf_getopt]]
136  * Loading by @cf_getopt()
137  * ~~~~~~~~~~~~~~~~~~~~~~~
138  ***/
139
140 /**
141  * Short options for loading configuration by @cf_getopt().
142  * Prepend to your own options.
143  **/
144 #define CF_SHORT_OPTS   "C:S:"
145 /**
146  * Long options for loading configuration by @cf_getopt().
147  * Prepend to your own options.
148  **/
149 #define CF_LONG_OPTS    {"config",      1, 0, 'C'}, {"set",             1, 0, 'S'}, CF_LONG_OPTS_DEBUG
150 /**
151  * Use this constant as @long_opts parameter of @cf_getopt() if you do
152  * not have any long options in your program.
153  **/
154 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
155 #ifndef CF_USAGE_TAB
156 #define CF_USAGE_TAB ""
157 #endif
158 /**
159  * This macro provides text describing usage of the configuration
160  * loading options. Concatenate with description of your options and
161  * write to the user, if he/she provides invalid options.
162  **/
163 #define CF_USAGE        \
164 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
165 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG
166
167 #ifdef CONFIG_DEBUG
168 #define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } ,
169 #define CF_USAGE_DEBUG "    --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n"
170 #else
171 #define CF_LONG_OPTS_DEBUG
172 #define CF_USAGE_DEBUG
173 #endif
174
175 /**
176  * Takes care of parsing the command-line arguments, loading the
177  * default configuration file (<<var_cf_def_file,`cf_def_file`>>) and processing
178  * configuration options. The calling convention is the same as with GNU getopt_long(),
179  * but you must prefix your own short/long options by the
180  * <<def_CF_LONG_OPTS,`CF_LONG_OPTS`>> or <<def_CF_SHORT_OPTS,`CF_SHORT_OPTS`>> or
181  * pass <<def_CF_NO_LONG_OPTS,`CF_NO_LONG_OPTS`>> if there are no long options.
182  *
183  * The default configuration file can be overwritten by the --config options,
184  * which must come first. During parsing of all other options, the configuration
185  * is already available.
186  **/
187 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
188
189 #endif