src/server/config/conf.c

changeset 16
a9bbd82d2dce
child 17
d2a97bbeb57d
equal deleted inserted replaced
15:cff9c4101dd7 16:a9bbd82d2dce
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2011 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "conf.h"
30
31 #include <string.h>
32
33 int cfg_parse_basic_file(ConfigParser *parser, FILE *in) {
34 parser->lines = NULL;
35 parser->mp = ucx_mempool_new(512);
36
37 // one logical line over many lines
38 sstr_t mline;
39 mline.ptr = NULL;
40 mline.length = 0;
41 ConfigLine *start_line;
42 ConfigLine *end_line;
43
44 // read file
45 sstr_t l;
46 while((l = cfg_readln(in)).ptr != NULL) {
47 // put the line to the list
48 ConfigLine *line = OBJ_NEW(parser->mp, ConfigLine);
49 line->line = sstrdub_mp(parser->mp, l);
50 line->object = NULL;
51 line->type = LINE_OTHER;
52 parser->lines = ucx_dlist_append(parser->lines, line);
53
54 // check if the line contains something
55 l = cfg_trim_comment(l);
56 line->type = cfg_get_basic_type(l);
57
58 if(line->type == LINE_OTHER) {
59 // check for multi line
60 if(mline.ptr != NULL) {
61 // concate lines
62 char *ptr = malloc(mline.length + l.length + 1);
63 memcpy(ptr, mline.ptr, mline.length);
64 memcpy(ptr + mline.length - 1, l.ptr, l.length);
65 mline.length += l.length;
66 free(mline.ptr);
67 mline.ptr = ptr;
68 mline.ptr[mline.length] = 0;
69
70 end_line = line;
71
72 line->type = LINE_MULTI;
73 }
74 if(l.ptr[l.length - 1] == '\\') {
75 if(mline.ptr == NULL) {
76 mline = sstrdub(l);
77 start_line = line;
78 }
79 } else {
80 // this line is complete so we can parse it
81 sstr_t ll; // we parse this line
82
83 if(mline.ptr == NULL) {
84 // single line
85 ll = l;
86 start_line = line;
87 end_line = line;
88 } else {
89 ll = mline;
90 }
91
92 // parse
93 int r = parser->parse(parser, start_line, end_line, ll);
94
95 // clean up
96 if(mline.ptr != NULL) {
97 free(mline.ptr);
98 mline.ptr = NULL;
99 mline.length = 0;
100 start_line = NULL;
101 end_line = NULL;
102 }
103
104 if(r != 0) {
105 return -1;
106 }
107 }
108 }
109 }
110
111 return 0;
112 }
113
114 sstr_t cfg_readln(FILE *file) {
115 sstr_t ns;
116 ns.ptr = NULL;
117 ns.length = 0;
118
119 if(!feof(file)) {
120 char buf[512];
121 buf[0] = 0;
122 int len = 512;
123
124 if(fgets(buf, len, file) == NULL) {
125 return ns;
126 }
127
128 if(*buf == 0) {
129 printf("???\n");
130 return ns;
131 }
132
133 char *ptr;
134 if((ptr = strrchr(buf, '\n'))) {
135 ptr[0] = 0;
136 }
137
138 sstr_t line = sstr(buf);
139 return line;
140 }
141
142 sstr_t s;
143 s.ptr = NULL;
144 s.length = 0;
145 return s;
146 }
147
148 /*
149 * checks if the line contains only a comment or space
150 */
151 int cfg_get_basic_type(sstr_t line) {
152 if(line.length == 0) {
153 return LINE_NOCONTENT;
154 } else if(line.ptr[0] == '#') {
155 return LINE_NOCONTENT;
156 }
157 return LINE_OTHER;
158 }
159
160 /*
161 * removes a comment from the line
162 */
163 sstr_t cfg_trim_comment(sstr_t line) {
164 sstr_t nl = line;
165 for(int i=0;i<line.length;i++) {
166 if(line.ptr[i] == '#') {
167 if(i > 0) {
168 nl.ptr = line.ptr + i - 1;
169 nl.length = i;
170 break;
171 } else {
172 nl.ptr = line.ptr;
173 nl.length = 0;
174 break;
175 }
176 }
177 }
178 return sstrtrim(nl);
179 }
180
181 /*
182 * gets the first parameter in the params string and returns a new string
183 * containing the other parameters or an empty string, if there are no more
184 * parameters
185 */
186 sstr_t cfg_param(sstr_t params, sstr_t *name, sstr_t *value) {
187 name->ptr = NULL;
188 name->length = 0;
189 value->ptr = NULL;
190 value->length = 0;
191
192 // get name
193 int i;
194 for(i=0;i<params.length;i++) {
195 char c = params.ptr[i];
196 if(c == '=') {
197 break;
198 } else if(c < 33) {
199 // no '=' means there is only a name, no value
200 name->ptr = params.ptr;
201 name->length = i;
202
203 params.ptr = params.ptr + i;
204 params.length -= i;
205 return sstrtrim(params);
206 }
207 }
208
209 name->ptr = params.ptr;
210 name->length = i;
211 i++;
212
213 // get value
214 if(i>=params.length) {
215 sstr_t ns;
216 ns.ptr = NULL;
217 ns.length = 0;
218 return ns;
219 }
220
221 int quote = 0;
222 value->ptr = params.ptr + i;
223 for(;i<params.length;i++) {
224 char c = params.ptr[i];
225 if(c == '"') {
226 if(quote) {
227 break; // end of quoted value
228 } else {
229 quote = 1;
230 value->ptr++;
231 }
232 } else if(!quote && c < 33) {
233 break; // end of value
234 }
235 }
236 value->length = i - name->length - 2;
237 i++;
238
239 if(value->length <= 0) {
240 value->length = 0;
241 value->ptr = NULL;
242 }
243
244 // create new params string
245 params.ptr += i;
246 params.length -= i;
247 return sstrtrim(params);
248 }
249
250 /*
251 * gets from a parameter list a value
252 */
253 sstr_t cfg_param_get(UcxList *list, sstr_t name) {
254 while(list != NULL) {
255 ConfigParam *param = list->data;
256 if(!sstrcmp(param->name, name)) {
257 return param->value;
258 }
259 list = list->next;
260 }
261 sstr_t ns;
262 ns.ptr = NULL;
263 ns.length = 0;
264 return ns;
265 }
266
267 /*
268 * gets the directive type number from a type string
269 * valid types are:
270 * AuthTrans 0
271 * NameTrans 1
272 * PathCheck 2
273 * ObjectType 3
274 * Service 4
275 * AddLog 5
276 * Init 6
277 */
278 int cfg_get_directive_type_num(sstr_t type) {
279 /* get nsapi function type */
280 int dt = -1;
281 if(sstrcmp(type, sstr("AuthTrans")) == 0) {
282 dt = 0;
283 } else if(sstrcmp(type, sstr("NameTrans")) == 0) {
284 dt = 1;
285 } else if(sstrcmp(type, sstr("PathCheck")) == 0) {
286 dt = 2;
287 } else if(sstrcmp(type, sstr("ObjectType")) == 0) {
288 dt = 3;
289 } else if(sstrcmp(type, sstr("Service")) == 0) {
290 dt = 4;
291 } else if(sstrcmp(type, sstr("AddLog")) == 0) {
292 dt = 5;
293 } else if(sstrcmp(type, sstr("Init")) == 0) {
294 dt = 6;
295 }
296 return dt;
297 }

mercurial