src/server/config/objconf.c

changeset 628
c95f04c14112
parent 584
f3ddd6dc8e7b
child 629
1e1da9adc532
equal deleted inserted replaced
627:b30bf356dac4 628:c95f04c14112
198 // free mempool 198 // free mempool
199 //ucx_mempool_destroy(conf->parser.mp->pool); 199 //ucx_mempool_destroy(conf->parser.mp->pool);
200 free(conf); 200 free(conf);
201 } 201 }
202 202
203
204
205 int objconf_parse(void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) {
206 ObjectConfig *conf = p;
207
208 begin->type = cfg_get_line_type(line);
209 switch(begin->type) {
210 case LINE_BEGIN_TAG: {
211 ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.a);
212 if(tag == NULL) {
213 ws_cfg_log(LOG_FAILURE, "Parse error in %s", conf->file);
214 exit(-1); // TODO: better error handling
215 }
216 tag->begin = begin;
217 tag->end = end;
218 tag->type_num = cfg_get_tag_type(cx_strcast(tag->name));
219 //printf("line {%s}\n", cx_strdub(ll).ptr);
220 if(objconf_on_begin_tag(conf, tag) != 0) {
221 fprintf(stderr, "1error\n");
222 exit(-1);
223 }
224 break;
225 }
226 case LINE_END_TAG: {
227 cxmutstr tag = cfg_get_end_tag_name(line);
228 if(objconf_on_end_tag(conf, tag) != 0) {
229 fprintf(stderr, "2error\n");
230 exit(-1);
231 }
232
233 break;
234 }
235 case LINE_DIRECTIVE: {
236 ConfigDirective *dir = cfg_parse_directive(
237 line,
238 conf->parser.a);
239 dir->begin = begin;
240 dir->end = end;
241 if(objconf_on_directive(conf, dir) != 0) {
242 fprintf(stderr, "3error\n");
243 exit(-1);
244 }
245 }
246 }
247 return 0;
248 }
249
250 int objconf_on_begin_tag(ObjectConfig *conf, ConfigTag *tag) {
251 CxAllocator *mp = conf->parser.a;
252 if(tag->type_num != TAG_OBJECT) {
253 ConfigParserLevel *l = conf->levels;
254 if(l->tag->type_num != TAG_OBJECT) {
255 tag->parent = l->tag;
256 }
257 }
258
259
260 switch(tag->type_num) {
261 case TAG_OBJECT: {
262 ConfigObject *obj = OBJ_NEW_N(mp, ConfigObject);
263 obj->begin = tag->begin;
264 obj->end = tag->end;
265
266 obj->name = cfg_param_get(tag->param, cx_str("name"));
267 obj->ppath = cfg_param_get(tag->param, cx_str("ppath"));
268
269 conf->obj = obj;
270 //conf->objects = ucx_list_append_a(mp, conf->objects, obj);
271 cxListAdd(conf->objects, obj);
272
273 // create tree level object
274 ConfigParserLevel *lvl = OBJ_NEW(mp, ConfigParserLevel);
275 lvl->iftag = NULL;
276 lvl->levelnum = 1;
277 lvl->tag = tag;
278 lvl->next = NULL;
279 //conf->levels = ucx_list_prepend_a(mp, conf->levels, lvl);
280 CFG_LEVEL_PREPEND(&conf->levels, lvl);
281
282 break;
283 }
284 case TAG_IF: {
285 // create tree level object
286 ConfigParserLevel *last_lvl = conf->levels;
287
288 ConfigParserLevel *lvl = OBJ_NEW(mp, ConfigParserLevel);
289
290 lvl->iftag = NULL;
291 lvl->levelnum = last_lvl->levelnum + 1;
292 lvl->tag = tag;
293 //conf->levels = ucx_list_prepend_a(mp, conf->levels, lvl);
294 CFG_LEVEL_PREPEND(&conf->levels, lvl);
295 last_lvl->iftag = tag;
296
297 break;
298 }
299 case TAG_ELSEIF: {
300 }
301 case TAG_ELSE: {
302 // create tree level object
303 ConfigParserLevel *last_lvl = conf->levels;
304 tag->iftag = last_lvl->iftag;
305
306 ConfigParserLevel *lvl = OBJ_NEW(
307 conf->parser.a,
308 ConfigParserLevel);
309
310 lvl->iftag = last_lvl->tag;
311 lvl->levelnum = last_lvl->levelnum + 1;
312 lvl->tag = tag;
313 //conf->levels = ucx_list_prepend(conf->levels, lvl);
314 CFG_LEVEL_PREPEND(&conf->levels, lvl);
315
316 break;
317 }
318 case TAG_CLIENT: {
319 // create tree level object
320
321 // TODO
322
323 break;
324 }
325 default: {
326 ws_cfg_log(LOG_FAILURE, "objconf: unknown tag");
327 return 1;
328 }
329 }
330
331 return 0;
332 }
333
334 int objconf_on_end_tag(ObjectConfig *conf, cxmutstr tagname) {
335 int type = cfg_get_tag_type(cx_strcast(tagname));
336 if(type == -1) {
337 ws_cfg_log(LOG_FAILURE, "objconf: unknown tag");
338 return 1;
339 } else {
340 if(type == TAG_OBJECT) {
341 conf->obj = NULL;
342 }
343
344 // remove level
345 /*
346 conf->levels = ucx_list_remove_a(
347 conf->parser.mp,
348 conf->levels,
349 conf->levels);
350 */
351 conf->levels = conf->levels->next;
352 }
353
354 return 0;
355 }
356
357 int objconf_on_directive(ObjectConfig *conf, ConfigDirective *dir) {
358 ConfigParserLevel *lvl = conf->levels;
359
360 // check if we have a condition for the directive
361 // if the level tag is not an object tag, use it as condition
362 if(lvl->tag->type_num != TAG_OBJECT) {
363 dir->condition = lvl->tag;
364 }
365
366 // add directive to current object
367 /*
368 conf->obj->directives[dir->type_num] = ucx_list_append_a(
369 conf->parser.mp,
370 conf->obj->directives[dir->type_num],
371 dir);
372 */
373
374 ConfigDirectiveList *dir_entry = cxMalloc(conf->parser.a, sizeof(ConfigDirectiveList));
375 dir_entry->directive = dir;
376 dir_entry->next = NULL;
377 CFG_DIRECTIVES_ADD(&conf->obj->directives[dir->type_num], dir_entry);
378
379 return 0;
380 }
381

mercurial