Merge branch 'master' of git://git.kernel.org/pub/scm/linux/storage/multipath-tools/
[platform/upstream/multipath-tools.git] / libmultipath / parser.c
1 /*
2  * Part:        Configuration file parser/reader. Place into the dynamic
3  *              data structure representation the conf file
4  *
5  * Version:     $Id: parser.c,v 1.0.3 2003/05/11 02:28:03 acassen Exp $
6  *
7  * Author:      Alexandre Cassen, <acassen@linux-vs.org>
8  *
9  *              This program is distributed in the hope that it will be useful,
10  *              but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *              See the GNU General Public License for more details.
13  *
14  *              This program is free software; you can redistribute it and/or
15  *              modify it under the terms of the GNU General Public License
16  *              as published by the Free Software Foundation; either version
17  *              2 of the License, or (at your option) any later version.
18  */
19
20 #include <syslog.h>
21
22 #include "parser.h"
23 #include "memory.h"
24
25 /* local vars */
26 static int sublevel = 0;
27 vector keywords = NULL;
28 vector *keywords_addr = NULL;
29
30 void set_current_keywords (vector *k)
31 {
32         keywords_addr = k;
33         keywords = NULL;
34 }
35
36 int
37 keyword_alloc(vector keywords, char *string, int (*handler) (vector),
38                 int (*print) (char *, int, void *))
39 {
40         struct keyword *keyword;
41
42         keyword = (struct keyword *) MALLOC(sizeof (struct keyword));
43
44         if (!keyword)
45                 return 1;
46
47         if (!vector_alloc_slot(keywords)) {
48                 FREE(keyword);
49                 return 1;
50         }
51         keyword->string = string;
52         keyword->handler = handler;
53         keyword->print = print;
54
55         vector_set_slot(keywords, keyword);
56
57         return 0;
58 }
59
60 int
61 install_keyword_root(char *string, int (*handler) (vector))
62 {
63         int r = keyword_alloc(keywords, string, handler, NULL);
64         if (!r)
65                 *keywords_addr = keywords;
66         return r;
67 }
68
69 void
70 install_sublevel(void)
71 {
72         sublevel++;
73 }
74
75 void
76 install_sublevel_end(void)
77 {
78         sublevel--;
79 }
80
81 int
82 install_keyword(char *string, int (*handler) (vector),
83                 int (*print) (char *, int, void *))
84 {
85         int i = 0;
86         struct keyword *keyword;
87
88         /* fetch last keyword */
89         keyword = VECTOR_SLOT(keywords, VECTOR_SIZE(keywords) - 1);
90
91         /* position to last sub level */
92         for (i = 0; i < sublevel; i++)
93                 keyword =
94                     VECTOR_SLOT(keyword->sub, VECTOR_SIZE(keyword->sub) - 1);
95
96         /* First sub level allocation */
97         if (!keyword->sub)
98                 keyword->sub = vector_alloc();
99
100         if (!keyword->sub)
101                 return 1;
102
103         /* add new sub keyword */
104         return keyword_alloc(keyword->sub, string, handler, print);
105 }
106
107 void
108 free_keywords(vector keywords)
109 {
110         struct keyword *keyword;
111         int i;
112
113         if (!keywords)
114                 return;
115
116         for (i = 0; i < VECTOR_SIZE(keywords); i++) {
117                 keyword = VECTOR_SLOT(keywords, i);
118                 if (keyword->sub)
119                         free_keywords(keyword->sub);
120                 FREE(keyword);
121         }
122         vector_free(keywords);
123 }
124
125 struct keyword *
126 find_keyword(vector v, char * name)
127 {
128         struct keyword *keyword;
129         int i;
130         int len;
131
132         if (!name || !keywords)
133                 return NULL;
134
135         if (!v)
136                 v = keywords;
137
138         len = strlen(name);
139
140         for (i = 0; i < VECTOR_SIZE(v); i++) {
141                 keyword = VECTOR_SLOT(v, i);
142                 if ((strlen(keyword->string) == len) &&
143                     !strcmp(keyword->string, name))
144                         return keyword;
145                 if (keyword->sub) {
146                         keyword = find_keyword(keyword->sub, name);
147                         if (keyword)
148                                 return keyword;
149                 }
150         }
151         return NULL;
152 }
153
154 int
155 snprint_keyword(char *buff, int len, char *fmt, struct keyword *kw, void *data)
156 {
157         int r;
158         int fwd = 0;
159         char *f = fmt;
160
161         if (!kw || !kw->print)
162                 return 0;
163
164         do {
165                 if (fwd == len || *f == '\0')
166                         break;
167                 if (*f != '%') {
168                         *(buff + fwd) = *f;
169                         fwd++;
170                         continue;
171                 }
172                 f++;
173                 switch(*f) {
174                 case 'k':
175                         fwd += snprintf(buff + fwd, len - fwd, kw->string);
176                         break;
177                 case 'v':
178                         r = kw->print(buff + fwd, len - fwd, data);
179                         if (!r) { /* no output if no value */
180                                 buff = '\0';
181                                 return 0;
182                         }
183                         fwd += r;
184                         break;
185                 }
186                 if (fwd > len)
187                         fwd = len;
188         } while (*f++);
189         return fwd;
190 }
191
192 vector
193 alloc_strvec(char *string)
194 {
195         char *cp, *start, *token;
196         int strlen;
197         int in_string;
198         vector strvec;
199
200         if (!string)
201                 return NULL;
202
203         cp = string;
204
205         /* Skip white spaces */
206         while ((isspace((int) *cp) || !isascii((int) *cp)) && *cp != '\0')
207                 cp++;
208
209         /* Return if there is only white spaces */
210         if (*cp == '\0')
211                 return NULL;
212
213         /* Return if string begin with a comment */
214         if (*cp == '!' || *cp == '#')
215                 return NULL;
216
217         /* Create a vector and alloc each command piece */
218         strvec = vector_alloc();
219
220         if (!strvec)
221                 return NULL;
222
223         in_string = 0;
224         while (1) {
225                 if (!vector_alloc_slot(strvec))
226                         goto out;
227
228                 start = cp;
229                 if (*cp == '"') {
230                         cp++;
231                         token = MALLOC(2);
232
233                         if (!token)
234                                 goto out;
235
236                         *(token) = '"';
237                         *(token + 1) = '\0';
238                         if (in_string)
239                                 in_string = 0;
240                         else
241                                 in_string = 1;
242
243                 } else {
244                         while ((in_string ||
245                                 (!isspace((int) *cp) && isascii((int) *cp) &&
246                                  *cp != '!' && *cp != '#')) &&
247                                *cp != '\0' && *cp != '"')
248                                 cp++;
249                         strlen = cp - start;
250                         token = MALLOC(strlen + 1);
251
252                         if (!token)
253                                 goto out;
254
255                         memcpy(token, start, strlen);
256                         *(token + strlen) = '\0';
257                 }
258                 vector_set_slot(strvec, token);
259
260                 while ((isspace((int) *cp) || !isascii((int) *cp))
261                        && *cp != '\0')
262                         cp++;
263                 if (*cp == '\0' || *cp == '!' || *cp == '#')
264                         return strvec;
265         }
266 out:
267         vector_free(strvec);
268         return NULL;
269 }
270
271 int
272 read_line(char *buf, int size)
273 {
274         int ch;
275         int count = 0;
276
277         while ((ch = fgetc(stream)) != EOF && (int) ch != '\n'
278                && (int) ch != '\r') {
279                 if (count < size)
280                         buf[count] = (int) ch;
281                 else
282                         break;
283                 count++;
284         }
285         return (ch == EOF) ? 0 : 1;
286 }
287
288 vector
289 read_value_block(void)
290 {
291         char *buf;
292         int i;
293         char *str = NULL;
294         char *dup;
295         vector vec = NULL;
296         vector elements = vector_alloc();
297
298         buf = (char *) MALLOC(MAXBUF);
299
300         if (!buf)
301                 return NULL;
302
303         if (!elements)
304                 goto out;
305
306         while (read_line(buf, MAXBUF)) {
307                 vec = alloc_strvec(buf);
308                 if (vec) {
309                         str = VECTOR_SLOT(vec, 0);
310                         if (!strcmp(str, EOB)) {
311                                 free_strvec(vec);
312                                 break;
313                         }
314
315                         if (VECTOR_SIZE(vec))
316                                 for (i = 0; i < VECTOR_SIZE(vec); i++) {
317                                         str = VECTOR_SLOT(vec, i);
318                                         dup = (char *) MALLOC(strlen(str) + 1);
319                                         memcpy(dup, str, strlen(str));
320
321                                         if (!vector_alloc_slot(elements))
322                                                 goto out1;
323
324                                         vector_set_slot(elements, dup);
325                                 }
326                         free_strvec(vec);
327                 }
328                 memset(buf, 0, MAXBUF);
329         }
330         FREE(buf);
331         return elements;
332 out1:
333         FREE(dup);
334 out:
335         FREE(buf);
336         return NULL;
337 }
338
339 int
340 alloc_value_block(vector strvec, void (*alloc_func) (vector))
341 {
342         char *buf;
343         char *str = NULL;
344         vector vec = NULL;
345
346         buf = (char *) MALLOC(MAXBUF);
347
348         if (!buf)
349                 return 1;
350
351         while (read_line(buf, MAXBUF)) {
352                 vec = alloc_strvec(buf);
353                 if (vec) {
354                         str = VECTOR_SLOT(vec, 0);
355                         if (!strcmp(str, EOB)) {
356                                 free_strvec(vec);
357                                 break;
358                         }
359
360                         if (VECTOR_SIZE(vec))
361                                 (*alloc_func) (vec);
362
363                         free_strvec(vec);
364                 }
365                 memset(buf, 0, MAXBUF);
366         }
367         FREE(buf);
368         return 0;
369 }
370
371 void *
372 set_value(vector strvec)
373 {
374         char *str = VECTOR_SLOT(strvec, 1);
375         int size = strlen(str);
376         int i = 0;
377         int len = 0;
378         char *alloc = NULL;
379         char *tmp;
380
381         if (*str == '"') {
382                 for (i = 2; i < VECTOR_SIZE(strvec); i++) {
383                         str = VECTOR_SLOT(strvec, i);
384                         len += strlen(str);
385                         if (!alloc)
386                                 alloc =
387                                     (char *) MALLOC(sizeof (char *) *
388                                                     (len + 1));
389                         else {
390                                 alloc =
391                                     REALLOC(alloc, sizeof (char *) * (len + 1));
392                                 tmp = VECTOR_SLOT(strvec, i-1);
393                                 if (*str != '"' && *tmp != '"')
394                                         strncat(alloc, " ", 1);
395                         }
396
397                         if (i != VECTOR_SIZE(strvec)-1)
398                                 strncat(alloc, str, strlen(str));
399                 }
400         } else {
401                 alloc = MALLOC(sizeof (char *) * (size + 1));
402                 memcpy(alloc, str, size);
403         }
404         return alloc;
405 }
406
407 /* non-recursive configuration stream handler */
408 static int kw_level = 0;
409 int
410 process_stream(vector keywords)
411 {
412         int i;
413         int r = 0;
414         struct keyword *keyword;
415         char *str;
416         char *buf;
417         vector strvec;
418
419         buf = MALLOC(MAXBUF);
420
421         if (!buf)
422                 return 1;
423
424         while (read_line(buf, MAXBUF)) {
425                 strvec = alloc_strvec(buf);
426                 memset(buf,0, MAXBUF);
427
428                 if (!strvec)
429                         continue;
430
431                 str = VECTOR_SLOT(strvec, 0);
432
433                 if (!strcmp(str, EOB) && kw_level > 0) {
434                         free_strvec(strvec);
435                         break;
436                 }
437
438                 for (i = 0; i < VECTOR_SIZE(keywords); i++) {
439                         keyword = VECTOR_SLOT(keywords, i);
440
441                         if (!strcmp(keyword->string, str)) {
442                                 if (keyword->handler)
443                                         r += (*keyword->handler) (strvec);
444
445                                 if (keyword->sub) {
446                                         kw_level++;
447                                         r += process_stream(keyword->sub);
448                                         kw_level--;
449                                 }
450                                 break;
451                         }
452                 }
453
454                 free_strvec(strvec);
455         }
456
457         FREE(buf);
458         return r;
459 }
460
461 /* Data initialization */
462 int
463 init_data(char *conf_file, void (*init_keywords) (void))
464 {
465         int r;
466
467         if (!keywords)
468                 keywords = vector_alloc();
469         if (!keywords)
470                 return 1;
471         stream = fopen(conf_file, "r");
472         if (!stream) {
473                 syslog(LOG_WARNING, "Configuration file open problem");
474                 return 1;
475         }
476
477         /* Init Keywords structure */
478         (*init_keywords) ();
479
480 /* Dump configuration *
481   vector_dump(keywords);
482   dump_keywords(keywords, 0);
483 */
484
485         /* Stream handling */
486         r = process_stream(keywords);
487         fclose(stream);
488         //free_keywords(keywords);
489
490         return r;
491 }