better config file handling
[project/uci.git] / parse.c
1 /*
2  * libuci - Library for the Unified Configuration Interface
3  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4  *
5  * this program is free software; you can redistribute it and/or modify
6  * it under the terms of the gnu lesser general public license version 2.1
7  * as published by the free software foundation
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.  See the
12  * GNU General Public License for more details.
13  */
14
15 /*
16  * This file contains the code for parsing uci config files
17  */
18
19 #include <sys/stat.h>
20 #include <ctype.h>
21
22 #define LINEBUF 128
23 #define LINEBUF_MAX     4096
24
25 /*
26  * Fetch a new line from the input stream and resize buffer if necessary
27  */
28 static void uci_getln(struct uci_context *ctx)
29 {
30         struct uci_parse_context *pctx = ctx->pctx;
31         char *p;
32         int ofs;
33
34         if (pctx->buf == NULL) {
35                 pctx->buf = uci_malloc(ctx, LINEBUF);
36                 pctx->bufsz = LINEBUF;
37         }
38
39         ofs = 0;
40         do {
41                 p = &pctx->buf[ofs];
42                 p[ofs] = 0;
43
44                 p = fgets(p, pctx->bufsz - ofs, pctx->file);
45                 if (!p || !p[ofs])
46                         return;
47
48                 ofs += strlen(p);
49                 if (pctx->buf[ofs - 1] == '\n') {
50                         pctx->line++;
51                         pctx->buf[ofs - 1] = 0;
52                         return;
53                 }
54
55                 if (pctx->bufsz > LINEBUF_MAX/2) {
56                         pctx->byte = LINEBUF_MAX;
57                         UCI_THROW(ctx, UCI_ERR_PARSE);
58                 }
59
60                 pctx->bufsz *= 2;
61                 pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
62         } while (1);
63 }
64
65 /*
66  * Clean up all extra memory used by the parser
67  */
68 static void uci_parse_cleanup(struct uci_context *ctx)
69 {
70         struct uci_parse_context *pctx;
71
72         pctx = ctx->pctx;
73         if (!pctx)
74                 return;
75
76         if (pctx->cfg) {
77                 uci_list_del(&pctx->cfg->list);
78                 uci_drop_file(pctx->cfg);
79         }
80         if (pctx->buf)
81                 free(pctx->buf);
82         if (pctx->file)
83                 fclose(pctx->file);
84
85         free(pctx);
86 }
87
88 /*
89  * move the string pointer forward until a non-whitespace character or
90  * EOL is reached
91  */
92 static void skip_whitespace(char **str)
93 {
94         while (**str && isspace(**str))
95                 *str += 1;
96 }
97
98 /*
99  * parse a double quoted string argument from the command line
100  */
101 static char *parse_double_quote(char **str)
102 {
103         char *val;
104
105         *str += 1;
106         val = *str;
107         while (**str) {
108
109                 /* skip escaped characters */
110                 if (**str == '\\') {
111                         *str += 2;
112                         continue;
113                 }
114
115                 /* check for the end of the quoted string */
116                 if (**str == '"') {
117                         **str = 0;
118                         *str += 1;
119                         return val;
120                 }
121                 *str += 1;
122         }
123
124         return NULL;
125 }
126
127 /*
128  * parse a single quoted string argument from the command line
129  */
130 static char *parse_single_quote(char **str)
131 {
132         /* TODO: implement */
133         return NULL;
134 }
135
136 /*
137  * extract the next word from the command line (unquoted argument)
138  */
139 static char *parse_unquoted(char **str)
140 {
141         char *val;
142
143         val = *str;
144
145         while (**str && !isspace(**str))
146                 *str += 1;
147
148         if (**str) {
149                 **str = 0;
150                 *str += 1;
151         }
152
153         return val;
154 }
155
156 /*
157  * extract the next argument from the command line
158  */
159 static char *next_arg(struct uci_context *ctx, char **str, bool required)
160 {
161         char *val;
162
163         skip_whitespace(str);
164         switch (**str) {
165                 case '"':
166                         val = parse_double_quote(str);
167                         break;
168                 case '\'':
169                         val = parse_single_quote(str);
170                         break;
171                 case 0:
172                         val = NULL;
173                         break;
174                 default:
175                         val = parse_unquoted(str);
176                         break;
177         }
178
179         if (required && !val) {
180                 ctx->pctx->byte = *str - ctx->pctx->buf;
181                 UCI_THROW(ctx, UCI_ERR_PARSE);
182         }
183
184         return val;
185 }
186
187 /*
188  * verify that the end of the line or command is reached.
189  * throw an error if extra arguments are given on the command line
190  */
191 static void assert_eol(struct uci_context *ctx, char **str)
192 {
193         char *tmp;
194
195         tmp = next_arg(ctx, str, false);
196         if (tmp && *tmp) {
197                 ctx->pctx->byte = tmp - ctx->pctx->buf;
198                 UCI_THROW(ctx, UCI_ERR_PARSE);
199         }
200 }
201
202 /*
203  * parse the 'config' uci command (open a section)
204  */
205 static void uci_parse_config(struct uci_context *ctx, char **str)
206 {
207         char *type, *name;
208
209         *str += strlen(*str) + 1;
210
211         if (!*str) {
212                 ctx->pctx->byte = *str - ctx->pctx->buf;
213                 UCI_THROW(ctx, UCI_ERR_PARSE);
214         }
215
216         type = next_arg(ctx, str, true);
217         name = next_arg(ctx, str, false);
218         assert_eol(ctx, str);
219
220         DPRINTF("Section<%s>: %s\n", type, name);
221 }
222
223 /*
224  * parse the 'option' uci command (open a value)
225  */
226 static void uci_parse_option(struct uci_context *ctx, char **str)
227 {
228         char *name, *value;
229
230         *str += strlen(*str) + 1;
231
232         name = next_arg(ctx, str, true);
233         value = next_arg(ctx, str, true);
234         assert_eol(ctx, str);
235
236         DPRINTF("\tOption: %s=\"%s\"\n", name, value);
237 }
238
239 /*
240  * parse a complete input line, split up combined commands by ';'
241  */
242 static void uci_parse_line(struct uci_context *ctx)
243 {
244         struct uci_parse_context *pctx = ctx->pctx;
245         char *word, *brk;
246
247         for (word = strtok_r(pctx->buf, ";", &brk);
248                  word;
249                  word = strtok_r(NULL, ";", &brk)) {
250
251                 char *pbrk;
252                 word = strtok_r(word, " \t", &pbrk);
253
254                 switch(word[0]) {
255                         case 'c':
256                                 if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
257                                         uci_parse_config(ctx, &word);
258                                 break;
259                         case 'o':
260                                 if ((word[1] == 0) || !strcmp(word + 1, "ption"))
261                                         uci_parse_option(ctx, &word);
262                                 break;
263                         default:
264                                 pctx->byte = word - pctx->buf;
265                                 UCI_THROW(ctx, UCI_ERR_PARSE);
266                                 break;
267                 }
268         }
269 }
270
271 int uci_load(struct uci_context *ctx, const char *name)
272 {
273         struct uci_parse_context *pctx;
274         struct stat statbuf;
275         char *filename;
276
277         UCI_HANDLE_ERR(ctx);
278         UCI_ASSERT(ctx, name != NULL);
279
280         /* make sure no memory from previous parse attempts is leaked */
281         uci_parse_cleanup(ctx);
282
283         pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
284         ctx->pctx = pctx;
285
286         switch (name[0]) {
287         case '.':
288         case '/':
289                 /* absolute/relative path outside of /etc/config */
290                 filename = (char *) name;
291                 break;
292         default:
293                 filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
294                 sprintf(filename, UCI_CONFDIR "/%s", name);
295                 break;
296         }
297
298         if ((stat(filename, &statbuf) < 0) ||
299                 ((statbuf.st_mode &  S_IFMT) != S_IFREG))
300                 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
301
302         pctx->file = fopen(filename, "r");
303         if (!pctx->file)
304                 UCI_THROW(ctx, UCI_ERR_IO);
305
306         pctx->cfg = uci_alloc_file(ctx, name);
307
308         while (!feof(pctx->file)) {
309                 uci_getln(ctx);
310                 if (*(pctx->buf))
311                         uci_parse_line(ctx);
312         }
313
314         /* add to main config file list */
315         uci_list_add(&ctx->root, &pctx->cfg->list);
316         pctx->cfg = NULL;
317
318         /* if no error happened, we can get rid of the parser context now */
319         uci_parse_cleanup(ctx);
320
321         return 0;
322 }
323
324