]> mj.ucw.cz Git - libucw.git/blob - lib/conf.h
Improved comments.
[libucw.git] / lib / 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 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 gets a string and a pointer to
40    * the destination variable.  It must store the value within [ptr,ptr+size),
41    * where size is fixed for each type.  It should not call cf_journal_block().  */
42 typedef byte *cf_hook(void *ptr);
43   /* An init- or commit-hook gets a pointer to the section or NULL if this
44    * is the global section.  It returns an error message or NULL if everything
45    * is all right.  The init-hook should fill in default values (needed for
46    * dynamically allocated nodes of link lists or for filling global variables
47    * that are run-time dependent).  The commit-hook should perform sanity
48    * checks and postprocess the parsed values.  Commit-hooks must call
49    * cf_journal_block() too.  Caveat! init-hooks for static sections must not
50    * use cf_malloc() but normal xmalloc().  */
51 typedef void cf_dumper1(struct fastbuf *fb, void *ptr);
52   /* Dumps the contents of a variable of a user-defined type.  */
53 typedef byte *cf_copier(void *dest, void *src);
54   /* Similar to init-hook, but it copies attributes from another list node
55    * instead of setting the attributes to default values.  You have to provide
56    * it if your node contains parsed values and/or sub-lists.  */
57
58 struct cf_user_type {
59   uns size;                             // of the parsed attribute
60   byte *name;                           // name of the type (for dumping)
61   cf_parser1 *parser;                   // how to parse it
62   cf_dumper1 *dumper;                   // how to dump the type
63 };
64
65 struct cf_section;
66 struct cf_item {
67   byte *name;                           // case insensitive
68   int number;                           // length of an array or #parameters of a parser (negative means at most)
69   void *ptr;                            // pointer to a global variable or an offset in a section
70   union cf_union {
71     struct cf_section *sec;             // declaration of a section or a list
72     cf_parser *par;                     // parser function
73     byte **lookup;                      // NULL-terminated sequence of allowed strings for lookups
74     struct cf_user_type *utype;         // specification of the user-defined type
75   } u;
76   enum cf_class cls:16;                 // attribute class
77   enum cf_type type:16;                 // type of a static or dynamic attribute
78 };
79
80 struct cf_section {
81   uns size;                             // 0 for a global block, sizeof(struct) for a section
82   cf_hook *init;                        // fills in default values (no need to bzero)
83   cf_hook *commit;                      // verifies parsed data (optional)
84   cf_copier *copy;                      // copies values from another instance (optional, no need to copy basic attributes)
85   struct cf_item *cfg;                  // CC_END-terminated array of items
86   uns flags;                            // for internal use only
87 };
88
89 /* Declaration of cf_section */
90 #define CF_TYPE(s)      .size = sizeof(s)
91 #define CF_INIT(f)      .init = (cf_hook*) f
92 #define CF_COMMIT(f)    .commit = (cf_hook*) f
93 #define CF_COPY(f)      .copy = (cf_copier*) f
94 #define CF_ITEMS        .cfg = ( struct cf_item[] )
95 #define CF_END          { .cls = CC_END }
96 /* Configuration items */
97 #define CF_STATIC(n,p,T,t,c)    { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) }
98 #define CF_DYNAMIC(n,p,T,t,c)   { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) }
99 #define CF_PARSER(n,p,f,c)      { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f }
100 #define CF_SECTION(n,p,s)       { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s }
101 #define CF_LIST(n,p,s)          { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,clist*), .u.sec = s }
102 /* Configuration items for basic types */
103 #define CF_INT(n,p)             CF_STATIC(n,p,INT,int,1)
104 #define CF_INT_ARY(n,p,c)       CF_STATIC(n,p,INT,int,c)
105 #define CF_INT_DYN(n,p,c)       CF_DYNAMIC(n,p,INT,int,c)
106 #define CF_UNS(n,p)             CF_STATIC(n,p,INT,uns,1)
107 #define CF_UNS_ARY(n,p,c)       CF_STATIC(n,p,INT,uns,c)
108 #define CF_UNS_DYN(n,p,c)       CF_DYNAMIC(n,p,INT,uns,c)
109 #define CF_U64(n,p)             CF_STATIC(n,p,U64,u64,1)
110 #define CF_U64_ARY(n,p,c)       CF_STATIC(n,p,U64,u64,c)
111 #define CF_U64_DYN(n,p,c)       CF_DYNAMIC(n,p,U64,u64,c)
112 #define CF_DOUBLE(n,p)          CF_STATIC(n,p,DOUBLE,double,1)
113 #define CF_DOUBLE_ARY(n,p,c)    CF_STATIC(n,p,DOUBLE,double,c)
114 #define CF_DOUBLE_DYN(n,p,c)    CF_DYNAMIC(n,p,DOUBLE,double,c)
115 #define CF_IP(n,p)              CF_STATIC(n,p,IP,u32,1)
116 #define CF_IP_ARY(n,p,c)        CF_STATIC(n,p,IP,u32,c)
117 #define CF_IP_DYN(n,p,c)        CF_DYNAMIC(n,p,IP,u32,c)
118 #define CF_STRING(n,p)          CF_STATIC(n,p,STRING,byte*,1)
119 #define CF_STRING_ARY(n,p,c)    CF_STATIC(n,p,STRING,byte*,c)
120 #define CF_STRING_DYN(n,p,c)    CF_DYNAMIC(n,p,STRING,byte*,c)
121 #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 }
122 #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 }
123 #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 }
124 #define CF_USER(n,p,t)          { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
125 #define CF_USER_ARY(n,p,t,c)    { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
126 #define CF_USER_DYN(n,p,t,c)    { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
127   // Beware that CF_USER_DYN can only be used on user-defined types of size at least 4
128
129 /* If you aren't picky about the number of parameters */
130 #define CF_ANY_NUM              -0x7fffffff
131
132 #define DARY_LEN(a) *(uns*)(a-1)
133   // length of a dynamic array
134 #define DARY_ALLOC(type,len,val...) (type[]) { (type)len, ##val } + 1
135   // creates a static instance of a dynamic array
136   // FIXME: overcast doesn't work for the double type
137
138 /* Memory allocation */
139 struct mempool;
140 extern struct mempool *cf_pool;
141 void *cf_malloc(uns size);
142 void *cf_malloc_zero(uns size);
143 byte *cf_strdup(byte *s);
144 byte *cf_printf(char *fmt, ...) FORMAT_CHECK(printf,1,2);
145
146 /* Undo journal for error recovery */
147 extern uns cf_need_journal;
148 void cf_journal_block(void *ptr, uns len);
149 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var))
150
151 struct cf_journal_item;
152 struct cf_journal_item *cf_journal_new_transaction(uns new_pool);
153 void cf_journal_commit_transaction(uns new_pool, struct cf_journal_item *oldj);
154 void cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj);
155
156 /* Declaration */
157 void cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown);
158 void cf_init_section(byte *name, struct cf_section *sec, void *ptr, uns do_bzero);
159
160 /* Parsers for basic types */
161 byte *cf_parse_int(byte *str, int *ptr);
162 byte *cf_parse_u64(byte *str, u64 *ptr);
163 byte *cf_parse_double(byte *str, double *ptr);
164 byte *cf_parse_ip(byte *p, u32 *varp);
165
166 #endif
167