]> mj.ucw.cz Git - libucw.git/blob - lib/conf.h
Use $(COPT2) instead of -O6.
[libucw.git] / lib / conf.h
1 /*
2  *      UCW Library -- Reading of configuration files
3  *
4  *      (c) 2001 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2005 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 _LIB_CONF_H
12 #define _LIB_CONF_H
13
14 #include <getopt.h>
15
16 /*
17  * Allocation in configuration memory pool.
18  */
19
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
25 /*
26  * Every module places its configuration setting into some section.  Section is
27  * an array of cfitem, whose first record is of type CT_SECTION and contains
28  * the name of the section.  The configuration sections are registered by
29  * calling cf_register().
30  *
31  * CT_INCOMPLETE_SECTION is identical to CT_SECTION, but when an unknown variable
32  * is spotted, we ignore it instead of bailing out with an error message.
33  *
34  * item->var is a pointer to the destination variable or to the special parsing
35  * function.
36  */
37
38 enum cftype { CT_STOP, CT_SECTION, CT_INCOMPLETE_SECTION, CT_INT, CT_STRING, CT_FUNCTION, CT_DOUBLE, CT_U64 };
39
40 struct cfitem {
41         byte *name;
42         enum cftype type;
43         void *var;
44 };
45
46 typedef byte *(*ci_func)(struct cfitem *, byte *);
47
48 void cf_register(struct cfitem *items);
49
50 /*
51  * Direct setting of configuration items and parsing the configuration file.
52  */
53
54 int cf_item_count(void);
55 struct cfitem *cf_get_item(byte *sect, byte *name);
56 byte *cf_set_item(byte *sect, byte *name, byte *value);
57 void cf_read(byte *filename);
58
59 /*
60  * Number parsing functions which could be useful in CT_FUNCTION callbacks.
61  */
62
63 byte *cf_parse_int(byte *value, uns *varp);
64 byte *cf_parse_u64(byte *value, u64 *varp);
65 byte *cf_parse_double(byte *value, double *varp);
66
67 /*
68  * When using cf_getopt, you must prefix your own short/long options by the
69  * CF_(SHORT|LONG)_OPTS.
70  *
71  * cfdeffile contains filename of config file automatically loaded before a
72  * first --set option is executed.  If none --set option occures, it will be
73  * loaded after getopt returns -1 (at the end of configuration options).  It
74  * will be ignored, if another config file is set by --config option at first.
75  * Its initial value is DEFAULT_CONFIG from config.h, but you can override it
76  * manually.
77  */
78
79 #define CF_SHORT_OPTS   "S:C:"
80 #define CF_LONG_OPTS    \
81         {"set",         1, 0, 'S'},\
82         {"config",      1, 0, 'C'},
83 #define CF_NO_LONG_OPTS (const struct option []){ CF_LONG_OPTS { NULL, 0, 0, 0 } }
84 #define CF_USAGE_TAB ""
85 #define CF_USAGE        \
86 "-S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n\
87 -C, --config filename\t" CF_USAGE_TAB "Overwrite default config filename\n"
88
89 extern byte *cfdeffile;
90
91 int cf_getopt(int argc,char * const argv[],
92                 const char *shortopts,const struct option *longopts,
93                 int *longindex);
94
95 #endif