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