+ va_list ap;
+
+ if (flags & CLI_FLAG_QUIET)
+ return;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+}
+
+static void uci_print_value(FILE *f, const char *v)
+{
+ fprintf(f, "'");
+ while (*v) {
+ if (*v != '\'')
+ fputc(*v, f);
+ else
+ fprintf(f, "'\\''");
+ v++;
+ }
+ fprintf(f, "'");
+}
+
+static void uci_show_value(struct uci_option *o, bool quote)
+{
+ struct uci_element *e;
+ bool sep = false;
+ char *space;
+
+ switch(o->type) {
+ case UCI_TYPE_STRING:
+ if (quote)
+ uci_print_value(stdout, o->v.string);
+ else
+ printf("%s", o->v.string);
+ printf("\n");
+ break;
+ case UCI_TYPE_LIST:
+ uci_foreach_element(&o->v.list, e) {
+ printf("%s", (sep ? delimiter : ""));
+ space = strpbrk(e->name, " \t\r\n");
+ if (!space && !quote)
+ printf("%s", e->name);
+ else
+ uci_print_value(stdout, e->name);
+ sep = true;
+ }
+ printf("\n");
+ break;
+ default:
+ printf("<unknown>\n");
+ break;
+ }
+}
+
+static void uci_show_option(struct uci_option *o, bool quote)
+{
+ printf("%s.%s.%s=",
+ o->section->package->e.name,
+ (cur_section_ref ? cur_section_ref : o->section->e.name),
+ o->e.name);
+ uci_show_value(o, quote);
+}
+
+static void uci_show_section(struct uci_section *s)
+{
+ struct uci_element *e;
+ const char *cname;
+ const char *sname;
+
+ cname = s->package->e.name;
+ sname = (cur_section_ref ? cur_section_ref : s->e.name);
+ printf("%s.%s=%s\n", cname, sname, s->type);
+ uci_foreach_element(&s->options, e) {
+ uci_show_option(uci_to_option(e), true);
+ }
+}
+
+static void uci_show_package(struct uci_package *p)
+{
+ struct uci_element *e;
+
+ uci_reset_typelist();
+ uci_foreach_element( &p->sections, e) {
+ struct uci_section *s = uci_to_section(e);
+ cur_section_ref = uci_lookup_section_ref(s);
+ uci_show_section(s);
+ }
+ uci_reset_typelist();
+}
+
+static void uci_show_changes(struct uci_package *p)
+{
+ struct uci_element *e;
+
+ uci_foreach_element(&p->saved_delta, e) {
+ struct uci_delta *h = uci_to_delta(e);
+ char *prefix = "";
+ char *op = "=";
+
+ switch(h->cmd) {
+ case UCI_CMD_REMOVE:
+ prefix = "-";
+ break;
+ case UCI_CMD_LIST_ADD:
+ op = "+=";
+ break;
+ case UCI_CMD_LIST_DEL:
+ op = "-=";
+ break;
+ default:
+ break;
+ }
+ printf("%s%s.%s", prefix, p->e.name, h->section);
+ if (e->name)
+ printf(".%s", e->name);
+ if (h->cmd != UCI_CMD_REMOVE) {
+ printf("%s", op);
+ uci_print_value(stdout, h->value);
+ }
+ printf("\n");
+ }
+}
+
+static int package_cmd(int cmd, char *tuple)
+{
+ struct uci_element *e = NULL;
+ struct uci_ptr ptr;
+ int ret = 1;
+
+ if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
+ cli_perror();
+ return 1;
+ }
+
+ e = ptr.last;
+ switch(cmd) {
+ case CMD_CHANGES:
+ uci_show_changes(ptr.p);
+ break;
+ case CMD_COMMIT:
+ if (flags & CLI_FLAG_NOCOMMIT) {
+ ret = 0;
+ goto out;
+ }
+ if (uci_commit(ctx, &ptr.p, false) != UCI_OK) {
+ cli_perror();
+ goto out;
+ }
+ break;
+ case CMD_EXPORT:
+ if (uci_export(ctx, stdout, ptr.p, true) != UCI_OK) {
+ goto out;
+ }
+ break;
+ case CMD_SHOW:
+ if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
+ ctx->err = UCI_ERR_NOTFOUND;
+ cli_perror();
+ goto out;
+ }
+ switch(e->type) {
+ case UCI_TYPE_PACKAGE:
+ uci_show_package(ptr.p);
+ break;
+ case UCI_TYPE_SECTION:
+ uci_show_section(ptr.s);
+ break;
+ case UCI_TYPE_OPTION:
+ uci_show_option(ptr.o, true);
+ break;
+ default:
+ /* should not happen */
+ goto out;
+ }
+ break;
+ }
+
+ ret = 0;
+
+out:
+ if (ptr.p)
+ uci_unload(ctx, ptr.p);
+ return ret;
+}
+
+static int uci_do_import(int argc, char **argv)
+{
+ struct uci_package *package = NULL;
+ char *name = NULL;
+ int ret = UCI_OK;
+ bool merge = false;
+
+ if (argc > 2)
+ return 255;
+
+ if (argc == 2)
+ name = argv[1];
+ else if (flags & CLI_FLAG_MERGE)
+ /* need a package to merge */
+ return 255;
+
+ if (flags & CLI_FLAG_MERGE) {
+ if (uci_load(ctx, name, &package) != UCI_OK)
+ package = NULL;
+ else
+ merge = true;
+ }
+ ret = uci_import(ctx, input, name, &package, (name != NULL));
+ if (ret == UCI_OK) {
+ if (merge) {
+ ret = uci_save(ctx, package);
+ } else {
+ struct uci_element *e;
+ /* loop through all config sections and overwrite existing data */
+ uci_foreach_element(&ctx->root, e) {
+ struct uci_package *p = uci_to_package(e);
+ ret = uci_commit(ctx, &p, true);
+ }
+ }
+ }
+
+ if (ret != UCI_OK) {
+ cli_perror();
+ return 1;
+ }
+
+ return 0;