+#include <glob.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "uci.h"
+#include "uci_internal.h"
+
+#define LINEBUF 32
+
+/*
+ * Fetch a new line from the input stream and resize buffer if necessary
+ */
+__private void uci_getln(struct uci_context *ctx, int offset)
+{
+ struct uci_parse_context *pctx = ctx->pctx;
+ char *p;
+ int ofs;
+
+ if (pctx->buf == NULL) {
+ pctx->buf = uci_malloc(ctx, LINEBUF);
+ pctx->bufsz = LINEBUF;
+ }
+ /* It takes 2 slots for fgets to read 1 char. */
+ if (offset >= pctx->bufsz - 1) {
+ pctx->bufsz *= 2;
+ pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
+ }
+
+ ofs = offset;
+ do {
+ p = &pctx->buf[ofs];
+ p[0] = 0;
+
+ p = fgets(p, pctx->bufsz - ofs, pctx->file);
+ if (!p || !*p)
+ return;
+
+ ofs += strlen(p);
+ if (pctx->buf[ofs - 1] == '\n') {
+ pctx->line++;
+ return;
+ }
+
+ pctx->bufsz *= 2;
+ pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
+ if (!pctx->buf)
+ UCI_THROW(ctx, UCI_ERR_MEM);
+ } while (1);
+}
+
+/*
+ * parse a character escaped by '\'
+ * returns true if the escaped character is to be parsed
+ * returns false if the escaped character is to be ignored
+ */
+static bool parse_backslash(struct uci_context *ctx)
+{
+ struct uci_parse_context *pctx = ctx->pctx;
+
+ /* skip backslash */
+ pctx->pos += 1;
+
+ /* undecoded backslash at the end of line, fetch the next line */
+ if (!pctx_cur_char(pctx) ||
+ pctx_cur_char(pctx) == '\n' ||
+ (pctx_cur_char(pctx) == '\r' &&
+ pctx_char(pctx, pctx_pos(pctx) + 1) == '\n' &&
+ !pctx_char(pctx, pctx_pos(pctx) + 2))) {
+ uci_getln(ctx, pctx->pos);
+ return false;
+ }
+
+ /* FIXME: decode escaped char, necessary? */
+ return true;
+}
+
+/*
+ * move the string pointer forward until a non-whitespace character or
+ * EOL is reached
+ */
+static void skip_whitespace(struct uci_context *ctx)
+{
+ struct uci_parse_context *pctx = ctx->pctx;
+
+ while (pctx_cur_char(pctx) && isspace(pctx_cur_char(pctx)))
+ pctx->pos += 1;
+}
+
+static inline void addc(struct uci_context *ctx, int *pos_dest, int *pos_src)
+{
+ struct uci_parse_context *pctx = ctx->pctx;
+
+ pctx_char(pctx, *pos_dest) = pctx_char(pctx, *pos_src);
+ *pos_dest += 1;
+ *pos_src += 1;
+}
+
+/*
+ * parse a double quoted string argument from the command line
+ */
+static void parse_double_quote(struct uci_context *ctx, int *target)
+{
+ struct uci_parse_context *pctx = ctx->pctx;
+ char c;
+
+ /* skip quote character */
+ pctx->pos += 1;
+
+ while (1) {
+ c = pctx_cur_char(pctx);
+ switch(c) {
+ case '"':
+ pctx->pos += 1;
+ return;
+ case 0:
+ /* Multi-line str value */
+ uci_getln(ctx, pctx->pos);
+ if (!pctx_cur_char(pctx)) {
+ uci_parse_error(ctx, "EOF with unterminated \"");
+ }
+ break;
+ case '\\':
+ if (!parse_backslash(ctx))
+ continue;
+ /* fall through */
+ default:
+ addc(ctx, target, &pctx->pos);
+ break;
+ }
+ }
+}
+
+/*
+ * parse a single quoted string argument from the command line
+ */
+static void parse_single_quote(struct uci_context *ctx, int *target)
+{
+ struct uci_parse_context *pctx = ctx->pctx;
+ char c;
+ /* skip quote character */
+ pctx->pos += 1;
+
+ while (1) {
+ c = pctx_cur_char(pctx);
+ switch(c) {
+ case '\'':
+ pctx->pos += 1;
+ return;
+ case 0:
+ /* Multi-line str value */
+ uci_getln(ctx, pctx->pos);
+ if (!pctx_cur_char(pctx))
+ uci_parse_error(ctx, "EOF with unterminated '");
+
+ break;
+ default:
+ addc(ctx, target, &pctx->pos);
+ }
+ }
+}
+
+/*
+ * parse a string from the command line and detect the quoting style
+ */
+static void parse_str(struct uci_context *ctx, int *target)
+{
+ struct uci_parse_context *pctx = ctx->pctx;
+ bool next = true;
+ do {
+ switch(pctx_cur_char(pctx)) {
+ case '\'':
+ parse_single_quote(ctx, target);
+ break;
+ case '"':
+ parse_double_quote(ctx, target);
+ break;
+ case '#':
+ pctx_cur_char(pctx) = 0;
+ /* fall through */
+ case 0:
+ goto done;
+ case ';':
+ next = false;
+ goto done;
+ case '\\':
+ if (!parse_backslash(ctx))
+ continue;
+ /* fall through */
+ default:
+ addc(ctx, target, &pctx->pos);
+ break;
+ }
+ } while (pctx_cur_char(pctx) && !isspace(pctx_cur_char(pctx)));
+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 (pctx_cur_char(pctx) && next)
+ pctx->pos += 1;
+
+ /* terminate the parsed string */
+ pctx_char(pctx, *target) = 0;
+}
+
+/*
+ * extract the next argument from the command line
+ */
+static int next_arg(struct uci_context *ctx, bool required, bool name, bool package)
+{
+ struct uci_parse_context *pctx = ctx->pctx;
+ int val, ptr;
+
+ skip_whitespace(ctx);
+ val = ptr = pctx_pos(pctx);
+ if (pctx_cur_char(pctx) == ';') {
+ pctx_cur_char(pctx) = 0;
+ pctx->pos += 1;
+ } else {
+ parse_str(ctx, &ptr);
+ }
+ if (!pctx_char(pctx, val)) {
+ if (required)
+ uci_parse_error(ctx, "insufficient arguments");
+ goto done;
+ }
+
+ if (name && !uci_validate_str(pctx_str(pctx, val), name, package))
+ uci_parse_error(ctx, "invalid character in name field");
+
+done:
+ return val;
+}
+
+int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result)
+{
+ int ofs_result;
+
+ UCI_HANDLE_ERR(ctx);
+ UCI_ASSERT(ctx, str != NULL);
+ UCI_ASSERT(ctx, result != NULL);
+
+ if (ctx->pctx && (ctx->pctx->file != stream))
+ uci_cleanup(ctx);
+
+ if (!ctx->pctx)
+ uci_alloc_parse_context(ctx);
+
+ ctx->pctx->file = stream;
+ if (!*str) {
+ ctx->pctx->pos = 0;
+ uci_getln(ctx, 0);
+ }
+
+ ofs_result = next_arg(ctx, false, false, false);
+ *result = pctx_str(ctx->pctx, ofs_result);
+ *str = pctx_cur_str(ctx->pctx);
+
+ return 0;
+}
+
+/*
+ * Fixup sections functions does the fixup of all sections for given package.
+ * It is used as a preprocessing step for fixing up existing anonymous sections
+ * from configurations.
+ *
+ * It uses uci_fixup_section() from list.c and then adds delta changes.
+ */
+static void
+uci_fixup_sections(struct uci_context *ctx, struct uci_package *p)
+{
+ struct uci_element *e;
+ struct uci_section *s;
+
+ uci_foreach_element(&p->sections, e) {
+ s = uci_to_section(e);
+ s->package->name_index++;
+ uci_fixup_section(ctx, s);
+ s->anonymous = false;
+ }
+}
+
+static int
+uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e)
+{
+ UCI_ASSERT(ctx, ptr != NULL);
+ UCI_ASSERT(ctx, e != NULL);
+
+ memset(ptr, 0, sizeof(struct uci_ptr));
+ switch(e->type) {
+ case UCI_TYPE_OPTION:
+ ptr->o = uci_to_option(e);
+ goto fill_option;
+ case UCI_TYPE_SECTION:
+ ptr->s = uci_to_section(e);
+ goto fill_section;
+ case UCI_TYPE_PACKAGE:
+ ptr->p = uci_to_package(e);
+ goto fill_package;
+ default:
+ UCI_THROW(ctx, UCI_ERR_INVAL);
+ }
+
+fill_option:
+ ptr->option = ptr->o->e.name;
+ ptr->s = ptr->o->section;
+fill_section:
+ ptr->section = ptr->s->e.name;
+ ptr->p = ptr->s->package;
+fill_package:
+ ptr->package = ptr->p->e.name;
+
+ ptr->flags |= UCI_LOOKUP_DONE;
+
+ return 0;
+}
+