* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
-
+
/*
* This file contains the code for parsing uci config files
*/
pctx->buf = uci_malloc(ctx, LINEBUF);
pctx->bufsz = LINEBUF;
}
-
+
ofs = 0;
do {
p = &pctx->buf[ofs];
p = fgets(p, pctx->bufsz - ofs, pctx->file);
if (!p || !p[ofs])
return;
-
+
ofs += strlen(p);
if (pctx->buf[ofs - 1] == '\n') {
pctx->line++;
static void uci_parse_cleanup(struct uci_context *ctx)
{
struct uci_parse_context *pctx;
-
+
pctx = ctx->pctx;
if (!pctx)
return;
static char *parse_double_quote(char **str)
{
char *val;
-
+
*str += 1;
val = *str;
while (**str) {
-
+
/* skip escaped characters */
if (**str == '\\') {
*str += 2;
continue;
}
-
+
/* check for the end of the quoted string */
if (**str == '"') {
**str = 0;
static char *parse_unquoted(char **str)
{
char *val;
-
+
val = *str;
while (**str && !isspace(**str))
static char *next_arg(struct uci_context *ctx, char **str, bool required)
{
char *val;
- skip_whitespace(str);
+ skip_whitespace(str);
switch (**str) {
case '"':
val = parse_double_quote(str);
default:
val = parse_unquoted(str);
}
-
+
if (required && !val) {
ctx->pctx->byte = *str - ctx->pctx->buf;
UCI_THROW(ctx, UCI_ERR_PARSE);
/*
* verify that the end of the line or command is reached.
* throw an error if extra arguments are given on the command line
- */
+ */
static void assert_eol(struct uci_context *ctx, char **str)
{
char *tmp;
tmp = next_arg(ctx, str, false);
- if (tmp) {
+ if (tmp && *tmp) {
ctx->pctx->byte = tmp - ctx->pctx->buf;
UCI_THROW(ctx, UCI_ERR_PARSE);
}
static void uci_parse_config(struct uci_context *ctx, char **str)
{
char *type, *name;
-
+
*str += strlen(*str) + 1;
-
+
if (!*str) {
ctx->pctx->byte = *str - ctx->pctx->buf;
UCI_THROW(ctx, UCI_ERR_PARSE);
type = next_arg(ctx, str, true);
name = next_arg(ctx, str, false);
- assert_eol(ctx, str);
+ assert_eol(ctx, str);
DPRINTF("Section<%s>: %s\n", type, name);
}
static void uci_parse_option(struct uci_context *ctx, char **str)
{
char *name, *value;
-
+
*str += strlen(*str) + 1;
-
+
name = next_arg(ctx, str, true);
value = next_arg(ctx, str, true);
- assert_eol(ctx, str);
+ assert_eol(ctx, str);
DPRINTF("\tOption: %s=\"%s\"\n", name, value);
}
{
struct uci_parse_context *pctx = ctx->pctx;
char *word, *brk;
-
+
for (word = strtok_r(pctx->buf, ";", &brk);
word;
word = strtok_r(NULL, ";", &brk)) {
-
+
char *pbrk;
word = strtok_r(word, " \t", &pbrk);
-
+
switch(word[0]) {
case 'c':
if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
int uci_parse(struct uci_context *ctx, const char *name)
{
struct uci_parse_context *pctx;
-
+
UCI_HANDLE_ERR(ctx);
UCI_ASSERT(ctx, name != NULL);
pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
ctx->pctx = pctx;
-
+
/* TODO: use /etc/config/ */
pctx->file = fopen(name, "r");
if (!pctx->file)