2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
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.
16 * This file contains the code for parsing uci config files
22 #define LINEBUF_MAX 4096
25 * Fetch a new line from the input stream and resize buffer if necessary
27 static void uci_getln(struct uci_context
*ctx
)
29 struct uci_parse_context
*pctx
= ctx
->pctx
;
33 if (pctx
->buf
== NULL
) {
34 pctx
->buf
= uci_malloc(ctx
, LINEBUF
);
35 pctx
->bufsz
= LINEBUF
;
43 p
= fgets(p
, pctx
->bufsz
- ofs
, pctx
->file
);
48 if (pctx
->buf
[ofs
- 1] == '\n') {
50 pctx
->buf
[ofs
- 1] = 0;
54 if (pctx
->bufsz
> LINEBUF_MAX
/2) {
55 pctx
->byte
= LINEBUF_MAX
;
56 UCI_THROW(ctx
, UCI_ERR_PARSE
);
60 pctx
->buf
= uci_realloc(ctx
, pctx
->buf
, pctx
->bufsz
);
65 * Clean up all extra memory used by the parser
67 static void uci_parse_cleanup(struct uci_context
*ctx
)
69 struct uci_parse_context
*pctx
;
83 static void skip_whitespace(char **str
)
85 while (**str
&& isspace(**str
))
89 static char *parse_double_quote(char **str
)
97 /* skip escaped characters */
103 /* check for the end of the quoted string */
115 static char *parse_single_quote(char **str
)
117 /* TODO: implement */
121 static char *parse_unquoted(char **str
)
127 while (**str
&& !isspace(**str
))
138 static char *next_arg(struct uci_context
*ctx
, char **str
, bool required
)
141 skip_whitespace(str
);
145 val
= parse_double_quote(str
);
147 val
= parse_single_quote(str
);
151 val
= parse_unquoted(str
);
154 if (required
&& !val
) {
155 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
156 UCI_THROW(ctx
, UCI_ERR_PARSE
);
162 static void assert_eol(struct uci_context
*ctx
, char **str
)
166 tmp
= next_arg(ctx
, str
, false);
168 ctx
->pctx
->byte
= tmp
- ctx
->pctx
->buf
;
169 UCI_THROW(ctx
, UCI_ERR_PARSE
);
173 static void uci_parse_config(struct uci_context
*ctx
, char **str
)
177 *str
+= strlen(*str
) + 1;
180 ctx
->pctx
->byte
= *str
- ctx
->pctx
->buf
;
181 UCI_THROW(ctx
, UCI_ERR_PARSE
);
184 type
= next_arg(ctx
, str
, true);
185 name
= next_arg(ctx
, str
, false);
186 assert_eol(ctx
, str
);
188 DPRINTF("Section<%s>: %s\n", type
, name
);
191 static void uci_parse_option(struct uci_context
*ctx
, char **str
)
195 *str
+= strlen(*str
) + 1;
197 name
= next_arg(ctx
, str
, true);
198 value
= next_arg(ctx
, str
, true);
199 assert_eol(ctx
, str
);
201 DPRINTF("\tOption: %s=\"%s\"\n", name
, value
);
204 static void uci_parse_line(struct uci_context
*ctx
)
206 struct uci_parse_context
*pctx
= ctx
->pctx
;
209 for (word
= strtok_r(pctx
->buf
, ";", &brk
);
211 word
= strtok_r(NULL
, ";", &brk
)) {
214 word
= strtok_r(word
, " \t", &pbrk
);
218 if ((word
[1] == 0) || !strcmp(word
+ 1, "onfig"))
219 uci_parse_config(ctx
, &word
);
222 if ((word
[1] == 0) || !strcmp(word
+ 1, "ption"))
223 uci_parse_option(ctx
, &word
);
226 pctx
->byte
= word
- pctx
->buf
;
227 UCI_THROW(ctx
, UCI_ERR_PARSE
);
233 int uci_parse(struct uci_context
*ctx
, const char *name
)
235 struct uci_parse_context
*pctx
;
238 UCI_ASSERT(ctx
, name
!= NULL
);
240 /* make sure no memory from previous parse attempts is leaked */
241 uci_parse_cleanup(ctx
);
243 pctx
= (struct uci_parse_context
*) uci_malloc(ctx
, sizeof(struct uci_parse_context
));
246 /* TODO: use /etc/config/ */
247 pctx
->file
= fopen(name
, "r");
249 UCI_THROW(ctx
, UCI_ERR_NOTFOUND
);
251 while (!feof(pctx
->file
)) {
257 /* if no error happened, we can get rid of the parser context now */
258 uci_parse_cleanup(ctx
);