]> mj.ucw.cz Git - libucw.git/blob - lib/getopt.h
Improved comments.
[libucw.git] / lib / 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 #include <getopt.h>
15
16 /* Safe loading and reloading of configuration files */
17
18 extern byte *cf_def_file;               /* DEFAULT_CONFIG; NULL if already loaded */
19 int cf_reload(byte *file);
20 int cf_load(byte *file);
21 int cf_set(byte *string);
22
23 /* Direct access to configuration items */
24
25 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(APPEND) T(PREPEND) \
26   T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY)
27   /* Closing brace finishes previous block.
28    * Basic attributes (static, dynamic, parsed) can be used with SET.
29    * Dynamic arrays can be used with SET, APPEND, PREPEND.
30    * Sections can be used with SET.
31    * Lists can be used with everything. */
32 #define T(x) OP_##x,
33 enum cf_operation { CF_OPERATIONS };
34 #undef T
35
36 struct cf_item;
37 struct fastbuf;
38 byte *cf_find_item(byte *name, struct cf_item *item);
39 byte *cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars);
40 void cf_dump_sections(struct fastbuf *fb);
41
42 /*
43  * cf_getopt() takes care of parsing the command-line arguments, loading the
44  * default configuration file (cf_def_file) and processing configuration options.
45  * The calling convention is the same as with GNU getopt_long(), but you must prefix
46  * your own short/long options by the CF_(SHORT|LONG)_OPTS or pass CF_NO_LONG_OPTS
47  * of there are no long options.
48  *
49  * The default configuration file can be overriden by the --config options,
50  * which must come first. During parsing of all other options, the configuration
51  * is already available.
52  */
53
54 #define CF_SHORT_OPTS   "C:S:"
55 #define CF_LONG_OPTS    {"config",      1, 0, 'C'}, {"set",             1, 0, 'S'}, CF_LONG_OPTS_DEBUG
56 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
57 #ifndef CF_USAGE_TAB
58 #define CF_USAGE_TAB ""
59 #endif
60 #define CF_USAGE        \
61 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
62 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG
63
64 #ifdef CONFIG_DEBUG
65 #define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } ,
66 #define CF_USAGE_DEBUG "    --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n"
67 #else
68 #define CF_LONG_OPTS_DEBUG
69 #define CF_USAGE_DEBUG
70 #endif
71
72 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
73
74 #endif