]> mj.ucw.cz Git - libucw.git/blob - lib/getopt.h
split conf2.h into conf.h and getopt.h
[libucw.git] / lib / getopt.h
1 /*
2  *      UCW Library -- Reading of configuration files
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 /* Safe reloading and loading of configuration files */
15 extern byte *cf_def_file;
16 int cf_reload(byte *file);
17 int cf_load(byte *file);
18 int cf_set(byte *string);
19
20 /* Direct access to configuration items */
21
22 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(APPEND) T(PREPEND) \
23   T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY)
24   /* Closing brace finishes previous block.
25    * Basic attributes (static, dynamic, parsed) can be used with SET.
26    * Dynamic arrays can be used with SET, APPEND, PREPEND.
27    * Sections can be used with SET.
28    * Lists can be used with everything. */
29 #define T(x) OP_##x,
30 enum cf_operation { CF_OPERATIONS };
31 #undef T
32
33 struct cf_item;
34 struct fastbuf;
35 byte *cf_find_item(byte *name, struct cf_item *item);
36 byte *cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars);
37 void cf_dump_sections(struct fastbuf *fb);
38
39 /*
40  * When using cf_get_opt(), you must prefix your own short/long options by the
41  * CF_(SHORT|LONG)_OPTS.
42  *
43  * cf_def_file contains the name of a configuration file that will be
44  * automatically loaded before the first --set option is executed.  If no --set
45  * option occurs, it will be loaded after getopt() returns -1 (i.e. at the end
46  * of the configuration options).  cf_def_file will be ignored if another
47  * configuration file has already been loaded using the --config option.  The
48  * initial value of cf_def_file is DEFAULT_CONFIG from config.h, but you can
49  * override it manually before calling cf_get_opt().
50  */
51
52 #define CF_SHORT_OPTS   "C:S:"
53 #define CF_LONG_OPTS    {"config",      1, 0, 'C'}, {"set",             1, 0, 'S'}, CF_LONG_OPTS_DEBUG
54 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
55 #ifndef CF_USAGE_TAB
56 #define CF_USAGE_TAB ""
57 #endif
58 #define CF_USAGE        \
59 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
60 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG
61
62 #ifdef CONFIG_DEBUG
63 #define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } ,
64 #define CF_USAGE_DEBUG "    --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n"
65 #else
66 #define CF_LONG_OPTS_DEBUG
67 #define CF_USAGE_DEBUG
68 #endif
69
70 #include <getopt.h>
71 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
72
73 #endif