2 * UCW Library -- Reading of configuration files
4 * (c) 2001 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2005 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.
17 * Allocation in configuration memory pool.
20 extern struct mempool *cfpool;
21 void *cfg_malloc(uns size);
22 void *cfg_malloc_zero(uns size);
23 byte *cfg_strdup(byte *s);
24 byte *cfg_printf(char *fmt, ...) FORMAT_CHECK(printf,1,2);
27 * Every module places its configuration setting into some section. Section is
28 * an array of cfitem, whose first record is of type CT_SECTION and contains
29 * the name of the section. The configuration sections are registered by
30 * calling cf_register().
32 * CT_INCOMPLETE_SECTION is identical to CT_SECTION, but when an unknown variable
33 * is spotted, we ignore it instead of bailing out with an error message.
35 * item->var is a pointer to the destination variable or to the special parsing
39 enum cftype { CT_STOP, CT_SECTION, CT_INCOMPLETE_SECTION, CT_INT, CT_STRING, CT_FUNCTION, CT_DOUBLE, CT_U64 };
47 typedef byte *(*ci_func)(struct cfitem *, byte *);
49 void cf_register(struct cfitem *items);
52 * Direct setting of configuration items and parsing the configuration file.
55 int cf_item_count(void);
56 struct cfitem *cf_get_item(byte *sect, byte *name);
57 byte *cf_set_item(byte *sect, byte *name, byte *value);
58 void cf_read(byte *filename);
61 * Number parsing functions which could be useful in CT_FUNCTION callbacks.
65 byte *cf_parse_int(byte *value, uns *varp);
66 byte *cf_parse_u64(byte *value, u64 *varp);
67 byte *cf_parse_double(byte *value, double *varp);
70 * Some useful parsing functions.
73 byte *cf_parse_ip(byte **value, u32 *varp);
77 * When using cf_getopt, you must prefix your own short/long options by the
78 * CF_(SHORT|LONG)_OPTS.
80 * cfdeffile contains filename of config file automatically loaded before a
81 * first --set option is executed. If none --set option occures, it will be
82 * loaded after getopt returns -1 (at the end of configuration options). It
83 * will be ignored, if another config file is set by --config option at first.
84 * Its initial value is DEFAULT_CONFIG from config.h, but you can override it
88 #define CF_SHORT_OPTS "S:C:"
89 #define CF_LONG_OPTS \
91 {"config", 1, 0, 'C'},
92 #define CF_NO_LONG_OPTS (const struct option []){ CF_LONG_OPTS { NULL, 0, 0, 0 } }
93 #define CF_USAGE_TAB ""
95 "-S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n\
96 -C, --config filename\t" CF_USAGE_TAB "Overwrite default config filename\n"
98 extern byte *cfdeffile;
100 int cf_getopt(int argc,char * const argv[],
101 const char *shortopts,const struct option *longopts,