]> mj.ucw.cz Git - libucw.git/blob - ucw/conf.h
Free dist.: Update installation documentation
[libucw.git] / ucw / conf.h
1 /*
2  *      UCW Library -- 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_CONF_H
12 #define _UCW_CONF_H
13
14 /*** === Data types [[conf_types]] ***/
15
16 enum cf_class {                         /** Class of the configuration item. **/
17   CC_END,                               // end of list
18   CC_STATIC,                            // single variable or static array
19   CC_DYNAMIC,                           // dynamically allocated array
20   CC_PARSER,                            // arbitrary parser function
21   CC_SECTION,                           // section appears exactly once
22   CC_LIST,                              // list with 0..many nodes
23   CC_BITMAP                             // of up to 32 items
24 };
25
26 enum cf_type {                          /** Type of a single value. **/
27   CT_INT, CT_U64, CT_DOUBLE,            // number types
28   CT_IP,                                // IP address
29   CT_STRING,                            // string type
30   CT_LOOKUP,                            // in a string table
31   CT_USER                               // user-defined type
32 };
33
34 struct fastbuf;
35
36 /**
37  * A parser function gets an array of (strdup'ed) strings and a pointer with
38  * the customized information (most likely the target address).  It can store
39  * the parsed value anywhere in any way it likes, however it must first call
40  * @cf_journal_block() on the overwritten memory block.  It returns an error
41  * message or NULL if everything is all right.
42  **/
43 typedef char *cf_parser(uns number, char **pars, void *ptr);
44 /**
45  * A parser function for user-defined types gets a string and a pointer to
46  * the destination variable.  It must store the value within [ptr,ptr+size),
47  * where size is fixed for each type.  It should not call @cf_journal_block().
48  **/
49 typedef char *cf_parser1(char *string, void *ptr);
50 /**
51  * An init- or commit-hook gets a pointer to the section or NULL if this
52  * is the global section.  It returns an error message or NULL if everything
53  * is all right.  The init-hook should fill in default values (needed for
54  * dynamically allocated nodes of link lists or for filling global variables
55  * that are run-time dependent).  The commit-hook should perform sanity
56  * checks and postprocess the parsed values.  Commit-hooks must call
57  * @cf_journal_block() too.  Caveat! init-hooks for static sections must not
58  * use @cf_malloc() but normal <<memory:xmalloc()>>.
59  **/
60 typedef char *cf_hook(void *ptr);
61 /**
62  * Dumps the contents of a variable of a user-defined type.
63  **/
64 typedef void cf_dumper1(struct fastbuf *fb, void *ptr);
65 /**
66  * Similar to init-hook, but it copies attributes from another list node
67  * instead of setting the attributes to default values.  You have to provide
68  * it if your node contains parsed values and/or sub-lists.
69  **/
70 typedef char *cf_copier(void *dest, void *src);
71
72 struct cf_user_type {                   /** Structure to store information about user-defined variable type. **/
73   uns size;                             // of the parsed attribute
74   char *name;                           // name of the type (for dumping)
75   cf_parser1 *parser;                   // how to parse it
76   cf_dumper1 *dumper;                   // how to dump the type
77 };
78
79 struct cf_section;
80 struct cf_item {                        /** Single configuration item. **/
81   const char *name;                     // case insensitive
82   int number;                           // length of an array or #parameters of a parser (negative means at most)
83   void *ptr;                            // pointer to a global variable or an offset in a section
84   union cf_union {
85     struct cf_section *sec;             // declaration of a section or a list
86     cf_parser *par;                     // parser function
87     const char * const *lookup;         // NULL-terminated sequence of allowed strings for lookups
88     struct cf_user_type *utype;         // specification of the user-defined type
89   } u;
90   enum cf_class cls:16;                 // attribute class
91   enum cf_type type:16;                 // type of a static or dynamic attribute
92 };
93
94 struct cf_section {                     /** A section. **/
95   uns size;                             // 0 for a global block, sizeof(struct) for a section
96   cf_hook *init;                        // fills in default values (no need to bzero)
97   cf_hook *commit;                      // verifies parsed data (optional)
98   cf_copier *copy;                      // copies values from another instance (optional, no need to copy basic attributes)
99   struct cf_item *cfg;                  // CC_END-terminated array of items
100   uns flags;                            // for internal use only
101 };
102
103 /***
104  * [[conf_macros]]
105  * Convenience macros
106  * ~~~~~~~~~~~~~~~~~~
107  *
108  * You could create the structures manually, but you can use these macros to
109  * save some typing.
110  */
111
112 // TODO
113
114 /* Declaration of cf_section */
115 #define CF_TYPE(s)      .size = sizeof(s)
116 #define CF_INIT(f)      .init = (cf_hook*) f
117 #define CF_COMMIT(f)    .commit = (cf_hook*) f
118 #define CF_COPY(f)      .copy = (cf_copier*) f
119 #define CF_ITEMS        .cfg = ( struct cf_item[] )
120 #define CF_END          { .cls = CC_END }
121 /* Configuration items */
122 #define CF_STATIC(n,p,T,t,c)    { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) }
123 #define CF_DYNAMIC(n,p,T,t,c)   { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) }
124 #define CF_PARSER(n,p,f,c)      { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f }
125 #define CF_SECTION(n,p,s)       { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s }
126 #define CF_LIST(n,p,s)          { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,clist*), .u.sec = s }
127 #define CF_BITMAP_INT(n,p)      { .cls = CC_BITMAP, .type = CT_INT, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u32*) }
128 #define CF_BITMAP_LOOKUP(n,p,t) { .cls = CC_BITMAP, .type = CT_LOOKUP, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u32*), .u.lookup = t }
129 /* Configuration items for basic types */
130 #define CF_INT(n,p)             CF_STATIC(n,p,INT,int,1)
131 #define CF_INT_ARY(n,p,c)       CF_STATIC(n,p,INT,int,c)
132 #define CF_INT_DYN(n,p,c)       CF_DYNAMIC(n,p,INT,int,c)
133 #define CF_UNS(n,p)             CF_STATIC(n,p,INT,uns,1)
134 #define CF_UNS_ARY(n,p,c)       CF_STATIC(n,p,INT,uns,c)
135 #define CF_UNS_DYN(n,p,c)       CF_DYNAMIC(n,p,INT,uns,c)
136 #define CF_U64(n,p)             CF_STATIC(n,p,U64,u64,1)
137 #define CF_U64_ARY(n,p,c)       CF_STATIC(n,p,U64,u64,c)
138 #define CF_U64_DYN(n,p,c)       CF_DYNAMIC(n,p,U64,u64,c)
139 #define CF_DOUBLE(n,p)          CF_STATIC(n,p,DOUBLE,double,1)
140 #define CF_DOUBLE_ARY(n,p,c)    CF_STATIC(n,p,DOUBLE,double,c)
141 #define CF_DOUBLE_DYN(n,p,c)    CF_DYNAMIC(n,p,DOUBLE,double,c)
142 #define CF_IP(n,p)              CF_STATIC(n,p,IP,u32,1)
143 #define CF_IP_ARY(n,p,c)        CF_STATIC(n,p,IP,u32,c)
144 #define CF_IP_DYN(n,p,c)        CF_DYNAMIC(n,p,IP,u32,c)
145 #define CF_STRING(n,p)          CF_STATIC(n,p,STRING,char*,1)
146 #define CF_STRING_ARY(n,p,c)    CF_STATIC(n,p,STRING,char*,c)
147 #define CF_STRING_DYN(n,p,c)    CF_DYNAMIC(n,p,STRING,char*,c)
148 #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 }
149 #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 }
150 #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 }
151 #define CF_USER(n,p,t)          { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
152 #define CF_USER_ARY(n,p,t,c)    { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
153 #define CF_USER_DYN(n,p,t,c)    { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
154
155 /* If you aren't picky about the number of parameters */
156 #define CF_ANY_NUM              -0x7fffffff
157
158 #define DARY_LEN(a) ((uns*)a)[-1]
159   // length of a dynamic array
160 #define DARY_ALLOC(type,len,val...) ((struct { uns l; type a[len]; }) { .l = len, .a = { val } }).a
161   // creates a static instance of a dynamic array
162
163 /***
164  * [[alloc]]
165  * Memory allocation
166  * ~~~~~~~~~~~~~~~~~
167  *
168  * Uses <<mempool:,memory pools>> for efficiency and journal recovery.
169  * You should use these routines when implementing custom parsers.
170  ***/
171 struct mempool;
172 extern struct mempool *cf_pool; /** A <<mempool:type_mempool,memory pool>> for configuration parser needs. **/
173 void *cf_malloc(uns size);      /** Returns @size bytes of memory. **/
174 void *cf_malloc_zero(uns size); /** Like @cf_malloc(), but zeroes the memory. **/
175 char *cf_strdup(const char *s); /** Copy a string into @cf_malloc()ed memory. **/
176 char *cf_printf(const char *fmt, ...) FORMAT_CHECK(printf,1,2); /** printf() into @cf_malloc()ed memory. **/
177
178 /***
179  * [[journal]]
180  * Undo journal
181  * ~~~~~~~~~~~~
182  *
183  * For error recovery
184  ***/
185 extern uns cf_need_journal;
186 void cf_journal_block(void *ptr, uns len);
187 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var))
188
189 /* Declaration: conf-section.c */
190 void cf_declare_section(const char *name, struct cf_section *sec, uns allow_unknown);
191 void cf_init_section(const char *name, struct cf_section *sec, void *ptr, uns do_bzero);
192
193 /*** === Parsers for basic types [[bparser]] ***/
194 char *cf_parse_int(const char *str, int *ptr);          /** Parser for integers. **/
195 char *cf_parse_u64(const char *str, u64 *ptr);          /** Parser for 64 unsigned integers. **/
196 char *cf_parse_double(const char *str, double *ptr);    /** Parser for doubles. **/
197 char *cf_parse_ip(const char *p, u32 *varp);            /** Parser for IP addresses. **/
198
199 #endif
200