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