]> mj.ucw.cz Git - libucw.git/blob - lib/conf.h
added cf_default_{init,done} for setting the default config filename, that
[libucw.git] / lib / conf.h
1 /*
2  *      Sherlock Library -- Reading configuration files
3  *
4  *      (c) 2001 Robert Spalek <robert@ucw.cz>
5  */
6
7 #include <getopt.h>
8
9 /*
10  * Allocation in configuration memory pool.
11  */
12
13 void *cfg_malloc(uns size);
14 byte *cfg_stralloc(byte *s);
15
16 /*
17  * Every module places its configuration setting into some section.  Section is
18  * an array of cfitem, whose first record is of type CT_SECTION and contains
19  * the name of the section.  The configuration sections are registered by
20  * calling cf_register().
21  *
22  * item->var is a pointer to the destination variable or to the special parsing
23  * function.
24  */
25
26 enum cftype { CT_STOP, CT_SECTION, CT_INT, CT_STRING, CT_FUNCTION };
27
28 struct cfitem {
29         byte *name;
30         enum cftype type;
31         void *var;
32 };
33
34 typedef byte *(*ci_func)(struct cfitem *, byte *);
35
36 void cf_register(struct cfitem *items);
37
38 /*
39  * Direct setting of configuration items and parsing the configuration file.
40  */
41
42 int cf_item_count(void);
43 struct cfitem *cf_get_item(byte *sect, byte *name);
44 byte *cf_set_item(byte *sect, byte *name, byte *value);
45 void cf_read(byte *filename);
46
47 /*
48  * When using cf_getopt, you must prefix your own short/long options by the
49  * CF_(SHORT|LONG)_OPTS.
50  *
51  * If you want to automatically load default config file before first option is
52  * overriden, register it by cf_default_init and call cf_default_done after
53  * parsing is done.  It will not be loaded if another config file is specified.
54  */
55
56 #define CF_SHORT_OPTS   "S:C:"
57 #define CF_LONG_OPTS    \
58         {"set",         1, 0, 'S'},\
59         {"config",      1, 0, 'C'},
60 #define CF_NO_LONG_OPTS (const struct option []){ CF_LONG_OPTS { NULL, 0, 0, 0 } }
61
62 void cf_default_init(byte *filename);
63 void cf_default_done(void);
64 int cf_getopt(int argc,char * const argv[],
65                 const char *shortopts,const struct option *longopts,
66                 int *longindex);
67