Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ static void uci_usage(void)
"\t-C <path> set the search path for config override files (default: "UCI_CONF2DIR")\n"
"\t-d <str> set the delimiter for list values in uci show\n"
"\t-f <file> use <file> as input instead of stdin\n"
"\t-k keep comments of unchanged entries (export, commit)\n"
"\t-m when importing, merge data into an existing package\n"
"\t-n name unnamed sections on export (default)\n"
"\t-N don't name unnamed sections\n"
Expand Down Expand Up @@ -697,7 +698,7 @@ int main(int argc, char **argv)
return 1;
}

while((c = getopt(argc, argv, "c:C:d:f:LmnNp:P:qsSt:X")) != -1) {
while((c = getopt(argc, argv, "c:C:d:f:LkmnNp:P:qsSt:X")) != -1) {
switch(c) {
case 'c':
uci_set_confdir(ctx, optarg);
Expand All @@ -721,6 +722,9 @@ int main(int argc, char **argv)
return 1;
}
break;
case 'k':
ctx->flags |= UCI_FLAG_COMMENTS;
break;
case 'm':
flags |= CLI_FLAG_MERGE;
break;
Expand Down
36 changes: 29 additions & 7 deletions delta.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

/* record a change that was done to a package */
void
uci_add_delta(struct uci_context *ctx, struct uci_list *list, int cmd, const char *section, const char *option, const char *value)
uci_add_delta(struct uci_context *ctx, struct uci_list *list, int cmd, const char *section, const char *option, const char *value, const char *comment)
{
struct uci_delta *h;
int size = strlen(section) + 1;
Expand All @@ -42,7 +42,7 @@ uci_add_delta(struct uci_context *ctx, struct uci_list *list, int cmd, const cha
if (value)
size += strlen(value) + 1;

h = uci_alloc_element(ctx, delta, option, size);
h = uci_alloc_element(ctx, (ctx->flags & UCI_FLAG_COMMENTS) ? comment : NULL, delta, option, size);
ptr = uci_dataptr(h);
h->cmd = cmd;
h->section = strcpy(ptr, section);
Expand All @@ -66,6 +66,24 @@ uci_free_delta(struct uci_delta *h)
uci_free_element(&h->e);
}

static void uci_fprintf_escaped_comment(FILE *f, const char *comment)
{
if (!comment || !comment[0])
return;

fputc('#', f);
for (const char *readptr = comment; *readptr; readptr++) {
if (*readptr == '\\') {
fputc('\\', f);
fputc('\\', f);
} else if (*readptr == '\n') {
fputc('\\', f);
fputc('n', f);
} else
fputc(*readptr, f);
}
}

static void uci_delta_save(struct uci_context *ctx, FILE *f,
const char *name, const struct uci_delta *h)
{
Expand All @@ -92,7 +110,9 @@ static void uci_delta_save(struct uci_context *ctx, FILE *f,
else
fprintf(f, "'\\''");
}
fprintf(f, "'\n");
fprintf(f, "'");
uci_fprintf_escaped_comment(f, e->comment);
fprintf(f, "\n");
}
}

Expand All @@ -114,7 +134,7 @@ int uci_set_savedir(struct uci_context *ctx, const char *dir)
}
}
if (!exists)
e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
e = uci_alloc_generic(ctx, NULL, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
uci_list_add(&ctx->delta_path, &e->list);

sdir = uci_strdup(ctx, dir);
Expand All @@ -138,7 +158,7 @@ int uci_add_delta_path(struct uci_context *ctx, const char *dir)
UCI_THROW(ctx, UCI_ERR_DUPLICATE);
}

e = uci_alloc_generic(ctx, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
e = uci_alloc_generic(ctx, NULL, UCI_TYPE_PATH, dir, sizeof(struct uci_element));
/* Keep savedir at the end of ctx->delta_path list */
savedir = ctx->delta_path.prev;
uci_list_insert(savedir->prev, &e->list);
Expand Down Expand Up @@ -177,6 +197,8 @@ static inline int uci_parse_delta_tuple(struct uci_context *ctx, struct uci_ptr
arg += 1;

UCI_INTERNAL(uci_parse_ptr, ctx, ptr, arg);
if (!ptr->comment && pctx->commentbuf)
ptr->comment = pctx->commentbuf;

if (!ptr->section)
goto error;
Expand Down Expand Up @@ -221,7 +243,7 @@ static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p)
goto error;

if (ctx->flags & UCI_FLAG_SAVED_DELTA)
uci_add_delta(ctx, &p->saved_delta, cmd, ptr.section, ptr.option, ptr.value);
uci_add_delta(ctx, &p->saved_delta, cmd, ptr.section, ptr.option, ptr.value, ptr.comment);

switch(cmd) {
case UCI_CMD_REORDER:
Expand Down Expand Up @@ -393,7 +415,7 @@ static void uci_filter_delta(struct uci_context *ctx, const char *name, const ch

if (!match && ptr.section) {
uci_add_delta(ctx, &list, c,
ptr.section, ptr.option, ptr.value);
ptr.section, ptr.option, ptr.value, ptr.comment);
}
}

Expand Down
Loading
Loading