1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include "objconf.h"
30
31 #include <string.h>
32
33
34
35
36
37
38
39
40
41 ObjectConfig *load_object_config(
char *file) {
42 FILE *in = fopen(file,
"r");
43 if(in ==
NULL) {
44 return NULL;
45 }
46
47 ObjectConfig *conf = malloc(
sizeof(ObjectConfig));
48 conf->parser.parse = objconf_parse;
49 conf->file = file;
50 conf->conditions =
NULL;
51 conf->levels =
NULL;
52 conf->objects =
NULL;
53
54
55 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
56 if(r !=
0) {
57
58 return NULL;
59 }
60
61 fclose(in);
62
63 return conf;
64 }
65
66 void free_object_config(ObjectConfig *conf) {
67
68 if(conf->levels) {
69
70 }
71
72
73 ucx_mempool_destroy(conf->parser.mp->pool);
74 free(conf);
75 }
76
77
78
79 int objconf_parse(
void *p, ConfigLine *begin, ConfigLine *end,
sstr_t line) {
80 ObjectConfig *conf = p;
81
82 begin->type = cfg_get_line_type(line);
83 switch(begin->type) {
84 case LINE_BEGIN_TAG: {
85 ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.mp);
86 if(tag ==
NULL) {
87 log_ereport(
LOG_FAILURE,
"Parse error in %s", conf->file);
88 exit(-
1);
89 }
90 tag->begin = begin;
91 tag->end = end;
92 tag->type_num = cfg_get_tag_type(tag->name);
93
94 if(objconf_on_begin_tag(conf, tag) !=
0) {
95 fprintf(stderr,
"1error\n");
96 exit(-
1);
97 }
98 break;
99 }
100 case LINE_END_TAG: {
101 sstr_t tag = cfg_get_end_tag_name(line);
102 if(objconf_on_end_tag(conf, tag) !=
0) {
103 fprintf(stderr,
"2error\n");
104 exit(-
1);
105 }
106
107 break;
108 }
109 case LINE_DIRECTIVE: {
110 ConfigDirective *dir = cfg_parse_directive(
111 line,
112 conf->parser.mp);
113 dir->begin = begin;
114 dir->end = end;
115 if(objconf_on_directive(conf, dir) !=
0) {
116 fprintf(stderr,
"3error\n");
117 exit(-
1);
118 }
119 }
120 }
121 return 0;
122 }
123
124 int objconf_on_begin_tag(ObjectConfig *conf, ConfigTag *tag) {
125 UcxAllocator *mp = conf->parser.mp;
126 if(tag->type_num !=
TAG_OBJECT) {
127 ConfigParserLevel *l = conf->levels->data;
128 if(l->tag->type_num !=
TAG_OBJECT) {
129 tag->parent = l->tag;
130 }
131 }
132
133
134 switch(tag->type_num) {
135 case TAG_OBJECT: {
136 ConfigObject *obj =
OBJ_NEW_N(mp, ConfigObject);
137 obj->begin = tag->begin;
138 obj->end = tag->end;
139
140 obj->name = cfg_param_get(tag->param, sstr(
"name"));
141 obj->ppath = cfg_param_get(tag->param, sstr(
"ppath"));
142
143 conf->obj = obj;
144 conf->objects = ucx_list_append_a(mp, conf->objects, obj);
145
146
147 ConfigParserLevel *lvl =
OBJ_NEW(mp, ConfigParserLevel);
148 lvl->iftag =
NULL;
149 lvl->levelnum =
1;
150 lvl->tag = tag;
151 conf->levels = ucx_list_prepend_a(mp, conf->levels, lvl);
152
153 break;
154 }
155 case TAG_IF: {
156
157 ConfigParserLevel *last_lvl = conf->levels->data;
158
159 ConfigParserLevel *lvl =
OBJ_NEW(mp, ConfigParserLevel);
160
161 lvl->iftag =
NULL;
162 lvl->levelnum = last_lvl->levelnum +
1;
163 lvl->tag = tag;
164 conf->levels = ucx_list_prepend_a(mp, conf->levels, lvl);
165 last_lvl->iftag = tag;
166
167 break;
168 }
169 case TAG_ELSEIF: {
170 }
171 case TAG_ELSE: {
172
173 ConfigParserLevel *last_lvl = conf->levels->data;
174 tag->iftag = last_lvl->iftag;
175
176 ConfigParserLevel *lvl =
OBJ_NEW(
177 conf->parser.mp,
178 ConfigParserLevel);
179
180 lvl->iftag = last_lvl->tag;
181 lvl->levelnum = last_lvl->levelnum +
1;
182 lvl->tag = tag;
183 conf->levels = ucx_list_prepend(conf->levels, lvl);
184
185 break;
186 }
187 case TAG_CLIENT: {
188
189
190
191
192 break;
193 }
194 default: {
195 log_ereport(
LOG_FAILURE,
"objconf: unknown tag");
196 return 1;
197 }
198 }
199
200 return 0;
201 }
202
203 int objconf_on_end_tag(ObjectConfig *conf,
sstr_t tagname) {
204 int type = cfg_get_tag_type(tagname);
205 if(type == -
1) {
206 log_ereport(
LOG_FAILURE,
"objconf: unknown tag");
207 return 1;
208 }
else {
209 if(type ==
TAG_OBJECT) {
210 conf->obj =
NULL;
211 }
212
213
214 conf->levels = ucx_list_remove_a(
215 conf->parser.mp,
216 conf->levels,
217 conf->levels);
218 }
219
220 return 0;
221 }
222
223 int objconf_on_directive(ObjectConfig *conf, ConfigDirective *dir) {
224 ConfigParserLevel *lvl = conf->levels->data;
225
226
227
228 if(lvl->tag->type_num !=
TAG_OBJECT) {
229 dir->condition = lvl->tag;
230 }
231
232
233 conf->obj->directives[dir->type_num] = ucx_list_append_a(
234 conf->parser.mp,
235 conf->obj->directives[dir->type_num],
236 dir);
237
238 return 0;
239 }
240
241