X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=parse.c;h=e63f900df6bc99358c7adf3d2d5446aa199d3bc4;hp=9c9dc59461592b637e744509108eb5ca3c430011;hb=854f7c5d08cd37aeb7414e26ed6666fad26f5f7a;hpb=a652efc7675db8b2c0571899609b42e539162141 diff --git a/parse.c b/parse.c index 9c9dc59..e63f900 100644 --- a/parse.c +++ b/parse.c @@ -11,11 +11,15 @@ * 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 */ +#include +#include +#include +#include #include #define LINEBUF 128 @@ -34,7 +38,7 @@ static void uci_getln(struct uci_context *ctx) pctx->buf = uci_malloc(ctx, LINEBUF); pctx->bufsz = LINEBUF; } - + ofs = 0; do { p = &pctx->buf[ofs]; @@ -43,7 +47,7 @@ static void uci_getln(struct uci_context *ctx) p = fgets(p, pctx->bufsz - ofs, pctx->file); if (!p || !p[ofs]) return; - + ofs += strlen(p); if (pctx->buf[ofs - 1] == '\n') { pctx->line++; @@ -52,6 +56,7 @@ static void uci_getln(struct uci_context *ctx) } if (pctx->bufsz > LINEBUF_MAX/2) { + pctx->reason = "line too long"; pctx->byte = LINEBUF_MAX; UCI_THROW(ctx, UCI_ERR_PARSE); } @@ -67,11 +72,16 @@ static void uci_getln(struct uci_context *ctx) static void uci_parse_cleanup(struct uci_context *ctx) { struct uci_parse_context *pctx; - + pctx = ctx->pctx; if (!pctx) return; + ctx->pctx = NULL; + if (pctx->cfg) { + uci_list_del(&pctx->cfg->list); + uci_drop_config(pctx->cfg); + } if (pctx->buf) free(pctx->buf); if (pctx->file) @@ -80,139 +90,202 @@ static void uci_parse_cleanup(struct uci_context *ctx) free(pctx); } +/* + * move the string pointer forward until a non-whitespace character or + * EOL is reached + */ static void skip_whitespace(char **str) { while (**str && isspace(**str)) *str += 1; } -static char *parse_double_quote(char **str) +static inline void addc(char **dest, char **src) { - char *val; - + **dest = **src; + *dest += 1; + *src += 1; +} + +static inline void parse_backslash(char **str, char **target) +{ + /* skip backslash */ *str += 1; - val = *str; - while (**str) { - - /* skip escaped characters */ - if (**str == '\\') { - *str += 2; + /* FIXME: decode escaped characters? */ + addc(target, str); +} + +/* + * parse a double quoted string argument from the command line + */ +static void parse_double_quote(struct uci_context *ctx, char **str, char **target) +{ + char c; + + /* skip quote character */ + *str += 1; + + while ((c = **str)) { + switch(c) { + case '\\': + parse_backslash(str, target); continue; - } - - /* check for the end of the quoted string */ - if (**str == '"') { - **str = 0; + case '"': + **target = 0; *str += 1; - return val; + return; + default: + addc(target, str); + break; } - *str += 1; } - - return NULL; + ctx->pctx->reason = "unterminated \""; + ctx->pctx->byte = *str - ctx->pctx->buf; + UCI_THROW(ctx, UCI_ERR_PARSE); } -static char *parse_single_quote(char **str) +/* + * parse a single quoted string argument from the command line + */ +static void parse_single_quote(struct uci_context *ctx, char **str, char **target) { - /* TODO: implement */ - return NULL; + char c; + /* skip quote character */ + *str += 1; + + while ((c = **str)) { + switch(c) { + case '\'': + **target = 0; + *str += 1; + return; + default: + addc(target, str); + } + } + ctx->pctx->reason = "unterminated '"; + ctx->pctx->byte = *str - ctx->pctx->buf; + UCI_THROW(ctx, UCI_ERR_PARSE); } -static char *parse_unquoted(char **str) +/* + * parse a string from the command line and detect the quoting style + */ +static void parse_str(struct uci_context *ctx, char **str, char **target) { - char *val; - - val = *str; - - while (**str && !isspace(**str)) - *str += 1; - - if (**str) { - **str = 0; + do { + switch(**str) { + case '\\': + parse_backslash(str, target); + continue; + case '\'': + parse_single_quote(ctx, str, target); + break; + case '"': + parse_double_quote(ctx, str, target); + break; + case 0: + goto done; + default: + addc(target, str); + break; + } + } while (**str && !isspace(**str)); +done: + + /* + * if the string was unquoted and we've stopped at a whitespace + * character, skip to the next one, because the whitespace will + * be overwritten by a null byte here + */ + if (**str) *str += 1; - } - return val; + /* terminate the parsed string */ + **target = 0; } +/* + * extract the next argument from the command line + */ static char *next_arg(struct uci_context *ctx, char **str, bool required) { char *val; - skip_whitespace(str); + char *ptr; - switch (**str) { - case '"': - val = parse_double_quote(str); - case '\'': - val = parse_single_quote(str); - case 0: - val = NULL; - default: - val = parse_unquoted(str); - } - - if (required && !val) { + val = ptr = *str; + skip_whitespace(str); + parse_str(ctx, str, &ptr); + if (required && !*val) { + ctx->pctx->reason = "insufficient arguments"; ctx->pctx->byte = *str - ctx->pctx->buf; UCI_THROW(ctx, UCI_ERR_PARSE); } - return val; + return uci_strdup(ctx, val); } +/* + * 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->reason = "too many arguments"; ctx->pctx->byte = tmp - ctx->pctx->buf; UCI_THROW(ctx, UCI_ERR_PARSE); } } +/* + * parse the 'config' uci command (open a section) + */ static void uci_parse_config(struct uci_context *ctx, char **str) { char *type, *name; - + + /* command string null-terminated by strtok */ *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); - - DPRINTF("Section<%s>: %s\n", type, name); + assert_eol(ctx, str); } +/* + * parse the 'option' uci command (open a value) + */ static void uci_parse_option(struct uci_context *ctx, char **str) { char *name, *value; - + + /* command string null-terminated by strtok */ *str += strlen(*str) + 1; - + name = next_arg(ctx, str, true); value = next_arg(ctx, str, true); - assert_eol(ctx, str); - - DPRINTF("\tOption: %s=\"%s\"\n", name, value); + assert_eol(ctx, str); } +/* + * parse a complete input line, split up combined commands by ';' + */ static void uci_parse_line(struct uci_context *ctx) { 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")) @@ -223,6 +296,7 @@ static void uci_parse_line(struct uci_context *ctx) uci_parse_option(ctx, &word); break; default: + pctx->reason = "unterminated command"; pctx->byte = word - pctx->buf; UCI_THROW(ctx, UCI_ERR_PARSE); break; @@ -230,34 +304,70 @@ static void uci_parse_line(struct uci_context *ctx) } } -int uci_parse(struct uci_context *ctx, const char *name) +int uci_load(struct uci_context *ctx, const char *name, struct uci_config **cfg) { struct uci_parse_context *pctx; - + struct stat statbuf; + char *filename; + bool confpath; + UCI_HANDLE_ERR(ctx); UCI_ASSERT(ctx, name != NULL); + UCI_TRAP_SAVE(ctx, ignore); + uci_unload(ctx, name); + UCI_TRAP_RESTORE(ctx); + +ignore: /* make sure no memory from previous parse attempts is leaked */ uci_parse_cleanup(ctx); 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) + + switch (name[0]) { + case '.': + case '/': + /* absolute/relative path outside of /etc/config */ + filename = (char *) name; + confpath = false; + break; + default: + filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2); + sprintf(filename, UCI_CONFDIR "/%s", name); + confpath = true; + break; + } + + if ((stat(filename, &statbuf) < 0) || + ((statbuf.st_mode & S_IFMT) != S_IFREG)) UCI_THROW(ctx, UCI_ERR_NOTFOUND); + pctx->file = fopen(filename, "r"); + if (filename != name) + free(filename); + + if (!pctx->file) + UCI_THROW(ctx, UCI_ERR_IO); + + pctx->cfg = uci_alloc_config(ctx, name); + while (!feof(pctx->file)) { uci_getln(ctx); - if (*(pctx->buf)) + if (pctx->buf[0]) uci_parse_line(ctx); } - /* if no error happened, we can get rid of the parser context now */ + /* add to main config file list */ + uci_list_add(&ctx->root, &pctx->cfg->list); + if (cfg) + *cfg = pctx->cfg; + + pctx->cfg = NULL; + + /* no error happened, we can get rid of the parser context now */ uci_parse_cleanup(ctx); return 0; } -