src/server/config/objconf.c

Sat, 12 Jan 2013 14:00:47 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 12 Jan 2013 14:00:47 +0100
changeset 46
636e05eb48f6
parent 44
3da1f7b6847f
child 62
c47e081b6c0f
permissions
-rw-r--r--

cleaning up resources after requests

16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "objconf.h"
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 /* dev notes:
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 *
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 * to free ObjectConfig, free:
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 * line dlist
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 * mempool
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 * object
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 */
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 ObjectConfig *load_object_config(char *file) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 FILE *in = fopen(file, "r");
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 if(in == NULL) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 return NULL;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 ObjectConfig *conf = malloc(sizeof(ObjectConfig));
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 conf->parser.parse = objconf_parse;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 conf->file = file;
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
50 conf->conditions = NULL;
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
51 conf->levels = NULL;
25
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
52 conf->objects = NULL;
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
53 conf->lines = NULL;
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 if(r != 0) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 // TODO: free
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 return NULL;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 return conf;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
64 void free_object_config(ObjectConfig *conf) {
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
65 // free all objects
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
66 UcxDlist *objects = conf->objects;
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
67 while(objects != NULL) {
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
68 ConfigObject *obj = objects->data;
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
69 // free all directive lists
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
70 for(int i=0;i<6;i++) {
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
71 if(obj->directives[i]) {
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
72 ucx_dlist_free(obj->directives[i]);
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
73 }
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
74 }
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
75
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
76 objects = objects->next;
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
77 }
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
78 if(conf->objects) {
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
79 ucx_dlist_free(conf->objects);
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
80 }
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
81
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
82 // free other lists
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
83 if(conf->levels) {
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
84 ucx_list_free(conf->levels);
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
85 }
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
86 if(conf->lines) {
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
87 ucx_dlist_free(conf->lines);
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
88 }
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
89 if(conf->conditions) {
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
90 ucx_dlist_free(conf->conditions);
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
91 }
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
92
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
93 // free mempool
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
94 ucx_mempool_free(conf->parser.mp);
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
95 free(conf);
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
96 }
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
97
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 int objconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 ObjectConfig *conf = p;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
103 begin->type = cfg_get_line_type(line);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 switch(begin->type) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 case LINE_BEGIN_TAG: {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
106 ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.mp);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 if(tag == NULL) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 fprintf(stderr, "Parse error!\n");
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 exit(-1); // TODO: better error handling
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 tag->begin = begin;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 tag->end = end;
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
113 tag->type_num = cfg_get_tag_type(tag->name);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 //printf("line {%s}\n", sstrdub(ll).ptr);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 if(objconf_on_begin_tag(conf, tag) != 0) {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
116 fprintf(stderr, "1error\n");
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 exit(-1);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 case LINE_END_TAG: {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
122 sstr_t tag = cfg_get_end_tag_name(line);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 if(objconf_on_end_tag(conf, tag) != 0) {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
124 fprintf(stderr, "2error\n");
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 exit(-1);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 case LINE_DIRECTIVE: {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
131 ConfigDirective *dir = cfg_parse_directive(
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 line,
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 conf->parser.mp);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 dir->begin = begin;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 dir->end = end;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 if(objconf_on_directive(conf, dir) != 0) {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
137 fprintf(stderr, "3error\n");
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 exit(-1);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 return 0;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 int objconf_on_begin_tag(ObjectConfig *conf, ConfigTag *tag) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 if(tag->type_num != TAG_OBJECT) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 ConfigParserLevel *l = conf->levels->data;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 if(l->tag->type_num != TAG_OBJECT) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 tag->parent = l->tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 switch(tag->type_num) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 case TAG_OBJECT: {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 ConfigObject *obj = OBJ_NEW_N(conf->parser.mp, ConfigObject);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 obj->begin = tag->begin;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 obj->end = tag->end;
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
159
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 obj->name = cfg_param_get(tag->param, sstr("name"));
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 obj->ppath = cfg_param_get(tag->param, sstr("ppath"));
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 conf->obj = obj;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 conf->objects = ucx_dlist_append(conf->objects, obj);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 // create tree level object
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 ConfigParserLevel *lvl = OBJ_NEW(conf->parser.mp, ConfigParserLevel);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 lvl->iftag = NULL;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 lvl->levelnum = 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 lvl->tag = tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 conf->levels = ucx_list_prepend(conf->levels, lvl);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 case TAG_IF: {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 // create tree level object
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 ConfigParserLevel *last_lvl = conf->levels->data;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
179 ConfigParserLevel *lvl = OBJ_NEW(
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
180 conf->parser.mp,
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
181 ConfigParserLevel);
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
182
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 lvl->iftag = NULL;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 lvl->levelnum = last_lvl->levelnum + 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 lvl->tag = tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 conf->levels = ucx_list_prepend(conf->levels, lvl);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 last_lvl->iftag = tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 case TAG_ELSEIF: {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 case TAG_ELSE: {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 // create tree level object
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 ConfigParserLevel *last_lvl = conf->levels->data;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 tag->iftag = last_lvl->iftag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 ConfigParserLevel *lvl = OBJ_NEW(
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 conf->parser.mp,
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 ConfigParserLevel);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 lvl->iftag = last_lvl->tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 lvl->levelnum = last_lvl->levelnum + 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 lvl->tag = tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 conf->levels = ucx_list_prepend(conf->levels, lvl);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 case TAG_CLIENT: {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 // create tree level object
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 // TODO
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 default: {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 printf("unknown tag\n");
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 return 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 return 0;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 int objconf_on_end_tag(ObjectConfig *conf, sstr_t tagname) {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
226 int type = cfg_get_tag_type(tagname);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 if(type == -1) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 fprintf(stderr, "unknown tag\n");
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 return 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 } else {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 if(type == TAG_OBJECT) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 conf->obj = NULL;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 // remove level
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 conf->levels = ucx_list_remove(conf->levels, conf->levels);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 return 0;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 int objconf_on_directive(ObjectConfig *conf, ConfigDirective *dir) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 ConfigParserLevel *lvl = conf->levels->data;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 // check if we have a condition for the directive
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 // if the level tag is not an object tag, use it as condition
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 if(lvl->tag->type_num != TAG_OBJECT) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 dir->condition = lvl->tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 // add directive to current object
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 conf->obj->directives[dir->type_num] = ucx_dlist_append(
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 conf->obj->directives[dir->type_num],
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 dir);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 return 0;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258

mercurial