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