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