remove internal usage of redundant uci_ptr.last
[project/uci.git] / cli.c
diff --git a/cli.c b/cli.c
index 41a78cbccd34d4dfef1b20bb5042d0a85ed492eb..f169e429478b12a56333aa0494cffe64ea616318 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -14,6 +14,8 @@
 #include <strings.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdarg.h>
+#include <errno.h>
 #include <unistd.h>
 #include "uci.h"
 
@@ -27,7 +29,6 @@ static enum {
        CLI_FLAG_NOCOMMIT = (1 << 2),
        CLI_FLAG_BATCH =    (1 << 3),
        CLI_FLAG_SHOW_EXT = (1 << 4),
-       CLI_FLAG_NOPLUGINS= (1 << 5),
 } flags;
 
 static FILE *input;
@@ -86,9 +87,10 @@ static char *
 uci_lookup_section_ref(struct uci_section *s)
 {
        struct uci_type_list *ti = type_list;
+       char *ret;
        int maxlen;
 
-       if (!s->anonymous || !(flags & CLI_FLAG_SHOW_EXT))
+       if (!(flags & CLI_FLAG_SHOW_EXT))
                return s->e.name;
 
        /* look up in section type list */
@@ -98,28 +100,41 @@ uci_lookup_section_ref(struct uci_section *s)
                ti = ti->next;
        }
        if (!ti) {
-               ti = malloc(sizeof(struct uci_type_list));
+               ti = calloc(1, sizeof(struct uci_type_list));
                if (!ti)
                        return NULL;
-               memset(ti, 0, sizeof(struct uci_type_list));
                ti->next = type_list;
                type_list = ti;
                ti->name = s->type;
        }
 
-       maxlen = strlen(s->type) + 1 + 2 + 10;
-       if (!typestr) {
-               typestr = malloc(maxlen);
+       if (s->anonymous) {
+               maxlen = strlen(s->type) + 1 + 2 + 10;
+               if (!typestr) {
+                       typestr = malloc(maxlen);
+                       if (!typestr)
+                               return NULL;
+               } else {
+                       void *p = realloc(typestr, maxlen);
+                       if (!p) {
+                               free(typestr);
+                               return NULL;
+                       }
+
+                       typestr = p;
+               }
+
+               if (typestr)
+                       sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
+
+               ret = typestr;
        } else {
-               typestr = realloc(typestr, maxlen);
+               ret = s->e.name;
        }
 
-       if (typestr)
-               sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
-
        ti->idx++;
 
-       return typestr;
+       return ret;
 }
 
 static void uci_usage(void)
@@ -147,12 +162,12 @@ static void uci_usage(void)
                "\t-c <path>  set the search path for config files (default: /etc/config)\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-L         do not load any plugins\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"
                "\t-p <path>  add a search path for config change files\n"
                "\t-P <path>  add a search path for config change files and use as default\n"
+               "\t-t <path>  set save path for config change files\n"
                "\t-q         quiet mode (don't print error messages)\n"
                "\t-s         force strict mode (stop on parser errors, default)\n"
                "\t-S         disable strict mode\n"
@@ -170,18 +185,54 @@ static void cli_perror(void)
        uci_perror(ctx, appname);
 }
 
-static void uci_show_value(struct uci_option *o)
+__attribute__((format(printf, 1, 2)))
+static void cli_error(const char *fmt, ...)
+{
+       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:
-               printf("%s\n", o->v.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%s", (sep ? delimiter : ""), e->name);
+                       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");
@@ -192,13 +243,13 @@ static void uci_show_value(struct uci_option *o)
        }
 }
 
-static void uci_show_option(struct uci_option *o)
+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);
+       uci_show_value(o, quote);
 }
 
 static void uci_show_section(struct uci_section *s)
@@ -211,7 +262,7 @@ static void uci_show_section(struct uci_section *s)
        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));
+               uci_show_option(uci_to_option(e), true);
        }
 }
 
@@ -253,62 +304,63 @@ static void uci_show_changes(struct uci_package *p)
                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%s", op, h->value);
+               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 = 0;
+       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)
-                       return 0;
+               if (flags & CLI_FLAG_NOCOMMIT) {
+                       ret = 0;
+                       goto out;
+               }
                if (uci_commit(ctx, &ptr.p, false) != UCI_OK) {
                        cli_perror();
-                       ret = 1;
+                       goto out;
                }
                break;
        case CMD_EXPORT:
-               uci_export(ctx, stdout, ptr.p, true);
+               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();
-                       ret = 1;
-               }
-               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);
-                               break;
-                       default:
-                               /* should not happen */
-                               return 1;
+                       goto out;
                }
+               if (ptr.o)
+                       uci_show_option(ptr.o, true);
+               else if (ptr.s)
+                       uci_show_section(ptr.s);
+               else if (ptr.p)
+                       uci_show_package(ptr.p);
+               else
+                       goto out; /* should not happen */
                break;
        }
 
+       ret = 0;
+
+out:
        if (ptr.p)
                uci_unload(ctx, ptr.p);
        return ret;
@@ -362,6 +414,7 @@ static int uci_do_package_cmd(int cmd, int argc, char **argv)
 {
        char **configs = NULL;
        char **p;
+       int ret = 1;
 
        if (argc > 2)
                return 255;
@@ -371,14 +424,17 @@ static int uci_do_package_cmd(int cmd, int argc, char **argv)
 
        if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
                cli_perror();
-               return 1;
+               goto out;
        }
 
        for (p = configs; *p; p++) {
                package_cmd(cmd, *p);
        }
 
-       return 0;
+       ret = 0;
+out:
+       free(configs);
+       return ret;
 }
 
 static int uci_do_add(int argc, char **argv)
@@ -411,7 +467,6 @@ done:
 
 static int uci_do_section_cmd(int cmd, int argc, char **argv)
 {
-       struct uci_element *e;
        struct uci_ptr ptr;
        int ret = UCI_OK;
        int dummy;
@@ -429,7 +484,6 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
            (cmd != CMD_RENAME) && (cmd != CMD_REORDER))
                return 1;
 
-       e = ptr.last;
        switch(cmd) {
        case CMD_GET:
                if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
@@ -437,17 +491,10 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
                        cli_perror();
                        return 1;
                }
-               switch(e->type) {
-               case UCI_TYPE_SECTION:
+               if (ptr.o)
+                       uci_show_value(ptr.o, false);
+               else if (ptr.s)
                        printf("%s\n", ptr.s->type);
-                       break;
-               case UCI_TYPE_OPTION:
-                       uci_show_value(ptr.o);
-                       break;
-               default:
-                       break;
-               }
-               /* throw the value to stdout */
                break;
        case CMD_RENAME:
                ret = uci_rename(ctx, &ptr);
@@ -504,11 +551,11 @@ static int uci_batch_cmd(void)
 
        for(i = 0; i <= MAX_ARGS; i++) {
                if (i == MAX_ARGS) {
-                       fprintf(stderr, "Too many arguments\n");
+                       cli_error("Too many arguments\n");
                        return 1;
                }
                argv[i] = NULL;
-               if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
+               if (uci_parse_argument(ctx, input, &str, &argv[i]) != UCI_OK) {
                        cli_perror();
                        i = 0;
                        break;
@@ -517,7 +564,7 @@ static int uci_batch_cmd(void)
                        break;
                argv[i] = strdup(argv[i]);
                if (!argv[i]) {
-                       perror("uci");
+                       cli_error("uci: %s", strerror(errno));
                        return 1;
                }
        }
@@ -531,8 +578,7 @@ static int uci_batch_cmd(void)
                return 0;
 
        for (j = 0; j < i; j++) {
-               if (argv[j])
-                       free(argv[j]);
+               free(argv[j]);
        }
 
        return ret;
@@ -550,7 +596,7 @@ static int uci_batch(void)
                if (ret == 254)
                        return 0;
                else if (ret == 255)
-                       fprintf(stderr, "Unknown command\n");
+                       cli_error("Unknown command\n");
 
                /* clean up */
                uci_foreach_element_safe(&ctx->root, tmp, e) {
@@ -640,11 +686,11 @@ int main(int argc, char **argv)
        input = stdin;
        ctx = uci_alloc_context();
        if (!ctx) {
-               fprintf(stderr, "Out of memory\n");
+               cli_error("Out of memory\n");
                return 1;
        }
 
-       while((c = getopt(argc, argv, "c:d:f:LmnNp:P:sSqX")) != -1) {
+       while((c = getopt(argc, argv, "c:d:f:LmnNp:P:qsSt:X")) != -1) {
                switch(c) {
                        case 'c':
                                uci_set_confdir(ctx, optarg);
@@ -654,19 +700,17 @@ int main(int argc, char **argv)
                                break;
                        case 'f':
                                if (input != stdin) {
-                                       perror("uci");
+                                       fclose(input);
+                                       cli_error("Too many input files.\n");
                                        return 1;
                                }
 
                                input = fopen(optarg, "r");
                                if (!input) {
-                                       perror("uci");
+                                       cli_error("uci: %s", strerror(errno));
                                        return 1;
                                }
                                break;
-                       case 'L':
-                               flags |= CLI_FLAG_NOPLUGINS;
-                               break;
                        case 'm':
                                flags |= CLI_FLAG_MERGE;
                                break;
@@ -687,13 +731,15 @@ int main(int argc, char **argv)
                                uci_add_delta_path(ctx, optarg);
                                break;
                        case 'P':
-                               uci_add_delta_path(ctx, ctx->savedir);
                                uci_set_savedir(ctx, optarg);
                                flags |= CLI_FLAG_NOCOMMIT;
                                break;
                        case 'q':
                                flags |= CLI_FLAG_QUIET;
                                break;
+                       case 't':
+                               uci_set_savedir(ctx, optarg);
+                               break;
                        case 'X':
                                flags &= ~CLI_FLAG_SHOW_EXT;
                                break;
@@ -712,9 +758,6 @@ int main(int argc, char **argv)
                return 0;
        }
 
-       if (!(flags & CLI_FLAG_NOPLUGINS))
-               uci_load_plugins(ctx, NULL);
-
        ret = uci_cmd(argc - 1, argv + 1);
        if (input != stdin)
                fclose(input);