UCI_HANDLE_ERR(ctx);
UCI_ASSERT(ctx, dir != NULL);
+ if (!strcmp(dir, ctx->savedir))
+ return -1;
e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
uci_list_add(&ctx->delta_path, &e->list);
return 0;
}
-static inline int uci_parse_delta_tuple(struct uci_context *ctx, char **buf, struct uci_ptr *ptr)
+char const uci_command_char[] = {
+ [UCI_CMD_ADD] = '+',
+ [UCI_CMD_REMOVE] = '-',
+ [UCI_CMD_CHANGE] = 0,
+ [UCI_CMD_RENAME] = '@',
+ [UCI_CMD_REORDER] = '^',
+ [UCI_CMD_LIST_ADD] = '|',
+ [UCI_CMD_LIST_DEL] = '~'
+};
+
+static inline int uci_parse_delta_tuple(struct uci_context *ctx, struct uci_ptr *ptr)
{
- int c = UCI_CMD_CHANGE;
+ struct uci_parse_context *pctx = ctx->pctx;
+ char *str = pctx_cur_str(pctx), *arg;
+ int c;
- switch(**buf) {
- case '^':
- c = UCI_CMD_REORDER;
- break;
- case '-':
- c = UCI_CMD_REMOVE;
- break;
- case '@':
- c = UCI_CMD_RENAME;
- break;
- case '+':
- /* UCI_CMD_ADD is used for anonymous sections or list values */
- c = UCI_CMD_ADD;
- break;
- case '|':
- c = UCI_CMD_LIST_ADD;
- break;
- case '~':
- c = UCI_CMD_LIST_DEL;
- break;
+ UCI_INTERNAL(uci_parse_argument, ctx, ctx->pctx->file, &str, &arg);
+ for (c = 0; c <= __UCI_CMD_LAST; c++) {
+ if (uci_command_char[c] == *arg)
+ break;
}
+ if (c > __UCI_CMD_LAST)
+ c = UCI_CMD_CHANGE;
if (c != UCI_CMD_CHANGE)
- *buf += 1;
+ arg += 1;
- UCI_INTERNAL(uci_parse_ptr, ctx, ptr, *buf);
+ UCI_INTERNAL(uci_parse_ptr, ctx, ptr, arg);
if (!ptr->section)
goto error;
return 0;
}
-static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p, char *buf)
+static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p)
{
struct uci_element *e = NULL;
struct uci_ptr ptr;
int cmd;
- cmd = uci_parse_delta_tuple(ctx, &buf, &ptr);
+ cmd = uci_parse_delta_tuple(ctx, &ptr);
if (strcmp(ptr.package, p->e.name) != 0)
goto error;
pctx->file = stream;
while (!feof(pctx->file)) {
+ pctx->pos = 0;
uci_getln(ctx, 0);
if (!pctx->buf[0])
continue;
* delta as possible
*/
UCI_TRAP_SAVE(ctx, error);
- uci_parse_delta_line(ctx, p, pctx->buf);
+ uci_parse_delta_line(ctx, p);
UCI_TRAP_RESTORE(ctx);
changes++;
error:
pctx->file = f;
while (!feof(f)) {
struct uci_element *e;
- char *buf;
+ pctx->pos = 0;
uci_getln(ctx, 0);
- buf = pctx->buf;
- if (!buf[0])
+ if (!pctx->buf[0])
continue;
/* NB: need to allocate the element before the call to
e = uci_alloc_generic(ctx, UCI_TYPE_DELTA, pctx->buf, sizeof(struct uci_element));
uci_list_add(&list, &e->list);
- uci_parse_delta_tuple(ctx, &buf, &ptr);
+ uci_parse_delta_tuple(ctx, &ptr);
if (section) {
if (!ptr.section || (strcmp(section, ptr.section) != 0))
continue;
uci_foreach_element_safe(&p->delta, tmp, e) {
struct uci_delta *h = uci_to_delta(e);
- char *prefix = "";
-
- switch(h->cmd) {
- case UCI_CMD_REMOVE:
- prefix = "-";
- break;
- case UCI_CMD_RENAME:
- prefix = "@";
- break;
- case UCI_CMD_ADD:
- prefix = "+";
- break;
- case UCI_CMD_REORDER:
- prefix = "^";
- break;
- case UCI_CMD_LIST_ADD:
- prefix = "|";
- break;
- case UCI_CMD_LIST_DEL:
- prefix = "~";
- break;
- default:
- break;
- }
+ char prefix[2] = {0, 0};
+ if (h->cmd <= __UCI_CMD_LAST)
+ prefix[0] = uci_command_char[h->cmd];
fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
if (e->name)
if (h->cmd == UCI_CMD_REMOVE && !h->value)
fprintf(f, "\n");
- else
- fprintf(f, "=%s\n", h->value);
+ else {
+ int i;
+
+ fprintf(f, "='");
+ for (i = 0; h->value[i]; i++) {
+ unsigned char c = h->value[i];
+ if (c != '\'')
+ fputc(c, f);
+ else
+ fprintf(f, "'\\''");
+ }
+ fprintf(f, "'\n");
+ }
uci_free_delta(h);
}