]> mj.ucw.cz Git - libucw.git/blob - lib/conf2.h
Fixed operator priorities in cf_parse_ip().
[libucw.git] / lib / conf2.h
1 /*
2  *      UCW Library -- Reading of configuration files
3  *
4  *      (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2006 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 _UCW_CONF2_H
12 #define _UCW_CONF2_H
13
14 enum cf_class {
15   CC_END,                               // end of list
16   CC_STATIC,                            // single variable or static array
17   CC_DYNAMIC,                           // dynamically allocated array
18   CC_PARSER,                            // arbitrary parser function
19   CC_SECTION,                           // section appears exactly once
20   CC_LIST                               // list with 0..many nodes
21 };
22
23 enum cf_type {
24   CT_INT, CT_U64, CT_DOUBLE,            // number types
25   CT_IP,                                // IP address
26   CT_STRING,                            // string type
27   CT_LOOKUP                             // in a string table
28 };
29
30 typedef byte *cf_parser(uns number, byte **pars, void *ptr);
31   /* A parser function gets an array of (strdup'ed) strings and a pointer with
32    * the customized information (most likely the target address).  It can store
33    * the parsed value anywhere in any way it likes, however it must first call
34    * cf_journal_block() on the overwritten memory block.  It returns an error
35    * message or NULL if everything is all right.  */
36 typedef byte *cf_hook(void *ptr);
37   /* An init- or commit-hook gets a pointer to the section or NULL if this
38    * is the global section.  It returns an error message or NULL if everything
39    * is all right.  The init-hook should fill in default values (needed for
40    * dynamically allocated nodes of link lists or for filling global variables
41    * that are run-time dependent).  The commit-hook should perform sanity
42    * checks and postprocess the parsed values.  Commit-hooks must call
43    * cf_journal_block() too.  Caveat! init-hooks for static sections must not
44    * use cf_malloc() but normal xmalloc().  */
45
46 struct cf_section;
47 struct cf_item {
48   byte *name;                           // case insensitive
49   int number;                           // length of an array or #parameters of a parser (negative means at most)
50   void *ptr;                            // pointer to a global variable or an offset in a section
51   union cf_union {
52     struct cf_section *sec;             // declaration of a section or a list
53     cf_parser *par;                     // parser function
54     char **lookup;                      // NULL-terminated sequence of allowed strings for lookups
55   } u;
56   enum cf_class cls:16;                 // attribute class
57   enum cf_type type:16;                 // type of a static or dynamic attribute
58 };
59
60 struct cf_section {
61   uns size;                             // 0 for a global block, sizeof(struct) for a section
62   cf_hook *init;                        // fills in default values (no need to bzero)
63   cf_hook *commit;                      // verifies parsed data (optional)
64   struct cf_item *cfg;                  // CC_END-terminated array of items
65   uns flags;                            // for internal use only
66 };
67
68 /* Declaration of cf_section */
69 #define CF_TYPE(s)      .size = sizeof(s)
70 #define CF_INIT(f)      .init = (cf_hook*) f
71 #define CF_COMMIT(f)    .commit = (cf_hook*) f
72 #define CF_ITEMS        .cfg = ( struct cf_item[] )
73 #define CF_END          { .cls = CC_END }
74 /* Configuration items */
75 struct clist;
76 #define CF_STATIC(n,p,T,t,c)    { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) }
77 #define CF_DYNAMIC(n,p,T,t,c)   { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) }
78 #define CF_PARSER(n,p,f,c)      { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f }
79 #define CF_SECTION(n,p,s)       { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s }
80 #define CF_LIST(n,p,s)          { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,struct clist*), .u.sec = s }
81 /* Configuration items for basic types */
82 #define CF_INT(n,p)             CF_STATIC(n,p,INT,int,1)
83 #define CF_INT_ARY(n,p,c)       CF_STATIC(n,p,INT,int,c)
84 #define CF_INT_DYN(n,p,c)       CF_DYNAMIC(n,p,INT,int,c)
85 #define CF_UNS(n,p)             CF_STATIC(n,p,INT,uns,1)
86 #define CF_UNS_ARY(n,p,c)       CF_STATIC(n,p,INT,uns,c)
87 #define CF_UNS_DYN(n,p,c)       CF_DYNAMIC(n,p,INT,uns,c)
88 #define CF_U64(n,p)             CF_STATIC(n,p,U64,u64,1)
89 #define CF_U64_ARY(n,p,c)       CF_STATIC(n,p,U64,u64,c)
90 #define CF_U64_DYN(n,p,c)       CF_DYNAMIC(n,p,U64,u64,c)
91 #define CF_DOUBLE(n,p)          CF_STATIC(n,p,DOUBLE,double,1)
92 #define CF_DOUBLE_ARY(n,p,c)    CF_STATIC(n,p,DOUBLE,double,c)
93 #define CF_DOUBLE_DYN(n,p,c)    CF_DYNAMIC(n,p,DOUBLE,double,c)
94 #define CF_IP(n,p)              CF_STATIC(n,p,IP,u32,1)
95 #define CF_IP_ARY(n,p,c)        CF_STATIC(n,p,IP,u32,c)
96 #define CF_IP_DYN(n,p,c)        CF_DYNAMIC(n,p,IP,u32,c)
97 #define CF_STRING(n,p)          CF_STATIC(n,p,STRING,byte*,1)
98 #define CF_STRING_ARY(n,p,c)    CF_STATIC(n,p,STRING,byte*,c)
99 #define CF_STRING_DYN(n,p,c)    CF_DYNAMIC(n,p,STRING,byte*,c)
100 #define CF_LOOKUP(n,p,t)        { .cls = CC_STATIC, .type = CT_LOOKUP, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,int*), .u.lookup = t }
101 #define CF_LOOKUP_ARY(n,p,t,c)  { .cls = CC_STATIC, .type = CT_LOOKUP, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,int*), .u.lookup = t }
102 #define CF_LOOKUP_DYN(n,p,t,c)  { .cls = CC_DYNAMIC, .type = CT_LOOKUP, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,int**), .u.lookup = t }
103
104 /* If you aren't picky about the number of parameters */
105 #define CF_ANY_NUM              -0x7fffffff
106
107 #define DARY_LEN(a) *(uns*)(a-1)
108   // length of a dynamic array
109 #define DARY_ALLOC(type,len,val...) (type[]) { (type)len, ##val } + 1
110   // creates a static instance of a dynamic array
111   // FIXME: overcast doesn't work for the double type
112
113 /* Memory allocation */
114 struct mempool;
115 extern struct mempool *cf_pool;
116 void *cf_malloc(uns size);
117 void *cf_malloc_zero(uns size);
118 byte *cf_strdup(byte *s);
119 byte *cf_printf(char *fmt, ...) FORMAT_CHECK(printf,1,2);
120
121 /* Undo journal for error recovery */
122 extern uns cf_need_journal;
123 void cf_journal_block(void *ptr, uns len);
124 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var))
125
126 /* Declaration */
127 void cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown);
128 void cf_init_section(byte *name, struct cf_section *sec, void *ptr, uns do_bzero);
129
130 /* Safe reloading and loading of configuration files */
131 extern byte *cf_def_file;
132 int cf_reload(byte *file);
133 int cf_load(byte *file);
134 int cf_set(byte *string);
135
136 /* Parsers for basic types */
137 byte *cf_parse_int(byte *str, int *ptr);
138 byte *cf_parse_u64(byte *str, u64 *ptr);
139 byte *cf_parse_double(byte *str, double *ptr);
140 byte *cf_parse_ip(byte *p, u32 *varp);
141
142 /* Direct access to configuration items */
143
144 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(APPEND) T(PREPEND) \
145   T(REMOVE) T(EDIT) T(AFTER) T(BEFORE)
146   /* Closing brace finishes previous block.
147    * Basic attributes (static, dynamic, parsed) can be used with SET.
148    * Dynamic arrays can be used with SET, APPEND, PREPEND.
149    * Sections can be used with SET.
150    * Lists can be used with everything. */
151 #define T(x) OP_##x,
152 enum cf_operation { CF_OPERATIONS };
153 #undef T
154
155 struct fastbuf;
156 byte *cf_find_item(byte *name, struct cf_item *item);
157 byte *cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars);
158 void cf_dump_sections(struct fastbuf *fb);
159
160 /*
161  * When using cf_get_opt(), you must prefix your own short/long options by the
162  * CF_(SHORT|LONG)_OPTS.
163  *
164  * cf_def_file contains the name of a configuration file that will be
165  * automatically loaded before the first --set option is executed.  If no --set
166  * option occurs, it will be loaded after getopt() returns -1 (i.e. at the end
167  * of the configuration options).  cf_def_file will be ignored if another
168  * configuration file has already been loaded using the --config option.  The
169  * initial value of cf_def_file is DEFAULT_CONFIG from config.h, but you can
170  * override it manually before calling cf_get_opt().
171  */
172
173 #define CF_SHORT_OPTS   "C:S:"
174 #define CF_LONG_OPTS    {"config",      1, 0, 'C'}, {"set",             1, 0, 'S'},
175 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
176 #ifndef CF_USAGE_TAB
177 #define CF_USAGE_TAB ""
178 #endif
179 #define CF_USAGE        \
180 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
181 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n"
182
183 struct option;
184 int cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
185
186 #endif