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