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