export a function for parsing shell-style arguments in libuci
[project/uci.git] / cli.c
diff --git a/cli.c b/cli.c
index 0d663e1..946a905 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -1,4 +1,5 @@
 /*
+ * cli - Command Line Interface for the Unified Configuration Interface
  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  *
  * This program is free software; you can redistribute it and/or modify
 
 static const char *appname = "uci";
 static enum {
-       CLI_FLAG_MERGE = (1 << 0),
+       CLI_FLAG_MERGE =    (1 << 0),
+       CLI_FLAG_QUIET =    (1 << 1),
+       CLI_FLAG_NOCOMMIT = (1 << 2),
 } flags;
-static FILE *input = stdin;
+static FILE *input;
 
 static struct uci_context *ctx;
 enum {
@@ -28,6 +31,7 @@ enum {
        CMD_SET,
        CMD_DEL,
        CMD_RENAME,
+       CMD_REVERT,
        /* package cmds */
        CMD_SHOW,
        CMD_IMPORT,
@@ -46,11 +50,17 @@ static void uci_usage(int argc, char **argv)
                "\tget        <config>.<section>[.<option>]\n"
                "\tset        <config>.<section>[.<option>]=<value>\n"
                "\trename     <config>.<section>[.<option>]=<name>\n"
+               "\trevert     <config>[.<section>[.<option>]]\n"
                "\n"
                "Options:\n"
                "\t-f <file>  use <file> as input instead of stdin\n"
                "\t-m         when importing, merge data into an existing package\n"
-               "\t-s         force strict mode (stop on parser errors)\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-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"
                "\n",
                argv[0]
@@ -58,6 +68,14 @@ static void uci_usage(int argc, char **argv)
        exit(255);
 }
 
+static void cli_perror(void)
+{
+       if (flags & CLI_FLAG_QUIET)
+               return;
+
+       uci_perror(ctx, appname);
+}
+
 static void uci_show_section(struct uci_section *p)
 {
        struct uci_element *e;
@@ -86,13 +104,17 @@ static int package_cmd(int cmd, char *package)
        struct uci_package *p = NULL;
 
        if (uci_load(ctx, package, &p) != UCI_OK) {
-               uci_perror(ctx, appname);
+               cli_perror();
                return 1;
        }
+       if (!p)
+               return 0;
        switch(cmd) {
        case CMD_COMMIT:
-               if (uci_commit(ctx, &p) != UCI_OK)
-                       uci_perror(ctx, appname);
+               if (flags & CLI_FLAG_NOCOMMIT)
+                       return 0;
+               if (uci_commit(ctx, &p, false) != UCI_OK)
+                       cli_perror();
                break;
        case CMD_EXPORT:
                uci_export(ctx, stdout, p, true);
@@ -108,6 +130,42 @@ static int package_cmd(int cmd, char *package)
 
 static int uci_do_import(int argc, char **argv)
 {
+       struct uci_package *package = NULL;
+       char *name = NULL;
+       int ret = UCI_OK;
+
+       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;
+       }
+       ret = uci_import(ctx, input, name, &package, (name != NULL));
+       if (ret == UCI_OK) {
+               if (flags & CLI_FLAG_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;
 }
 
@@ -123,7 +181,7 @@ static int uci_do_package_cmd(int cmd, int argc, char **argv)
                return package_cmd(cmd, argv[1]);
 
        if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
-               uci_perror(ctx, appname);
+               cli_perror();
                return 1;
        }
 
@@ -137,13 +195,14 @@ static int uci_do_package_cmd(int cmd, int argc, char **argv)
 
 static int uci_do_section_cmd(int cmd, int argc, char **argv)
 {
+       struct uci_package *p = NULL;
+       struct uci_element *e = NULL;
        char *package = NULL;
        char *section = NULL;
        char *option = NULL;
        char *value = NULL;
        char **ptr = NULL;
-       struct uci_package *p = NULL;
-       struct uci_element *e = NULL;
+       int ret = UCI_OK;
 
        if (argc != 2)
                return 255;
@@ -160,9 +219,11 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
                return 1;
 
        if (uci_load(ctx, package, &p) != UCI_OK) {
-               uci_perror(ctx, appname);
+               cli_perror();
                return 1;
        }
+       if (!p)
+               return 0;
 
        switch(cmd) {
        case CMD_GET:
@@ -184,32 +245,29 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
                printf("%s\n", value);
                break;
        case CMD_RENAME:
-               if (uci_rename(ctx, p, section, option, value) != UCI_OK) {
-                       uci_perror(ctx, appname);
-                       return 1;
-               }
+               ret = uci_rename(ctx, p, section, option, value);
+               break;
+       case CMD_REVERT:
+               ret = uci_revert(ctx, &p, section, option);
                break;
        case CMD_SET:
-               if (uci_set(ctx, p, section, option, value) != UCI_OK) {
-                       uci_perror(ctx, appname);
-                       return 1;
-               }
+               ret = uci_set(ctx, p, section, option, value);
                break;
        case CMD_DEL:
-               if (uci_delete(ctx, p, section, option) != UCI_OK) {
-                       uci_perror(ctx, appname);
-                       return 1;
-               }
+               ret = uci_delete(ctx, p, section, option);
                break;
        }
 
        /* no save necessary for get */
-       if (cmd == CMD_GET)
+       if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
                return 0;
 
        /* save changes, but don't commit them yet */
-       if (uci_save(ctx, p) != UCI_OK) {
-               uci_perror(ctx, appname);
+       if (ret == UCI_OK)
+               ret = uci_save(ctx, p);
+
+       if (ret != UCI_OK) {
+               cli_perror();
                return 1;
        }
 
@@ -233,6 +291,8 @@ static int uci_cmd(int argc, char **argv)
        else if (!strcasecmp(argv[0], "ren") ||
                 !strcasecmp(argv[0], "rename"))
                cmd = CMD_RENAME;
+       else if (!strcasecmp(argv[0], "revert"))
+               cmd = CMD_REVERT;
        else if (!strcasecmp(argv[0], "del"))
                cmd = CMD_DEL;
        else if (!strcasecmp(argv[0], "import"))
@@ -245,6 +305,7 @@ static int uci_cmd(int argc, char **argv)
                case CMD_SET:
                case CMD_DEL:
                case CMD_RENAME:
+               case CMD_REVERT:
                        return uci_do_section_cmd(cmd, argc, argv);
                case CMD_SHOW:
                case CMD_EXPORT:
@@ -262,13 +323,14 @@ int main(int argc, char **argv)
        int ret;
        int c;
 
+       input = stdin;
        ctx = uci_alloc_context();
        if (!ctx) {
                fprintf(stderr, "Out of memory\n");
                return 1;
        }
 
-       while((c = getopt(argc, argv, "sS")) != -1) {
+       while((c = getopt(argc, argv, "f:mnNp:P:sSq")) != -1) {
                switch(c) {
                        case 'f':
                                input = fopen(optarg, "r");
@@ -287,6 +349,23 @@ int main(int argc, char **argv)
                                ctx->flags &= ~UCI_FLAG_STRICT;
                                ctx->flags |= UCI_FLAG_PERROR;
                                break;
+                       case 'n':
+                               ctx->flags |= UCI_FLAG_EXPORT_NAME;
+                               break;
+                       case 'N':
+                               ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
+                               break;
+                       case 'p':
+                               uci_add_history_path(ctx, optarg);
+                               break;
+                       case 'P':
+                               uci_add_history_path(ctx, ctx->savedir);
+                               uci_set_savedir(ctx, optarg);
+                               flags |= CLI_FLAG_NOCOMMIT;
+                               break;
+                       case 'q':
+                               flags |= CLI_FLAG_QUIET;
+                               break;
                        default:
                                uci_usage(argc, argv);
                                break;