]> pilppa.org Git - uci.git/blobdiff - cli.c
add reorder to lua api
[uci.git] / cli.c
diff --git a/cli.c b/cli.c
index 61afa80ddfede1fbbe40eda65dcb97e116a30d95..0b2a76d5698f9a6c00365e3bfe18456baac3baaa 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -26,6 +26,7 @@ static enum {
        CLI_FLAG_QUIET =    (1 << 1),
        CLI_FLAG_NOCOMMIT = (1 << 2),
        CLI_FLAG_BATCH =    (1 << 3),
+       CLI_FLAG_SHOW_EXT = (1 << 4),
 } flags;
 
 static FILE *input;
@@ -39,6 +40,7 @@ enum {
        CMD_DEL,
        CMD_RENAME,
        CMD_REVERT,
+       CMD_REORDER,
        /* package cmds */
        CMD_SHOW,
        CMD_CHANGES,
@@ -84,7 +86,7 @@ uci_lookup_section_ref(struct uci_section *s)
        struct uci_type_list *ti = type_list;
        int maxlen;
 
-       if (!s->anonymous)
+       if (!s->anonymous || !(flags & CLI_FLAG_SHOW_EXT))
                return s->e.name;
 
        /* look up in section type list */
@@ -130,6 +132,7 @@ static void uci_usage(void)
                "\tdelete     <config>[.<section[.<option>]]\n"
                "\trename     <config>.<section>[.<option>]=<name>\n"
                "\trevert     <config>[.<section>[.<option>]]\n"
+               "\treorder    <config>.<section>=<position>\n"
                "\n"
                "Options:\n"
                "\t-c <path>  set the search path for config files (default: /etc/config)\n"
@@ -143,6 +146,7 @@ static void uci_usage(void)
                "\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"
+               "\t-X         do not use extended syntax on 'show'\n"
                "\n",
                appname
        );
@@ -402,7 +406,7 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
                return 1;
        }
 
-       if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME))
+       if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME) && (cmd != CMD_REORDER))
                return 1;
 
        e = ptr.last;
@@ -437,6 +441,14 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
        case CMD_ADD_LIST:
                ret = uci_add_list(ctx, &ptr);
                break;
+       case CMD_REORDER:
+               if (!ptr.s) {
+                       ctx->err = UCI_ERR_NOTFOUND;
+                       cli_perror();
+                       return 1;
+               }
+               ret = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
+               break;
        case CMD_DEL:
                ret = uci_delete(ctx, &ptr);
                break;
@@ -460,7 +472,7 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
 
 static int uci_batch_cmd(void)
 {
-       char *argv[MAX_ARGS];
+       char *argv[MAX_ARGS + 2];
        char *str = NULL;
        int ret = 0;
        int i, j;
@@ -548,6 +560,8 @@ static int uci_cmd(int argc, char **argv)
                cmd = CMD_RENAME;
        else if (!strcasecmp(argv[0], "revert"))
                cmd = CMD_REVERT;
+       else if (!strcasecmp(argv[0], "reorder"))
+               cmd = CMD_REORDER;
        else if (!strcasecmp(argv[0], "del") ||
                 !strcasecmp(argv[0], "delete"))
                cmd = CMD_DEL;
@@ -569,6 +583,7 @@ static int uci_cmd(int argc, char **argv)
                case CMD_DEL:
                case CMD_RENAME:
                case CMD_REVERT:
+               case CMD_REORDER:
                        return uci_do_section_cmd(cmd, argc, argv);
                case CMD_SHOW:
                case CMD_EXPORT:
@@ -592,6 +607,7 @@ int main(int argc, char **argv)
        int ret;
        int c;
 
+       flags = CLI_FLAG_SHOW_EXT;
        appname = argv[0];
        input = stdin;
        ctx = uci_alloc_context();
@@ -600,7 +616,7 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       while((c = getopt(argc, argv, "c:d:f:mnNp:P:sSq")) != -1) {
+       while((c = getopt(argc, argv, "c:d:f:mnNp:P:sSqX")) != -1) {
                switch(c) {
                        case 'c':
                                uci_set_confdir(ctx, optarg);
@@ -642,6 +658,9 @@ int main(int argc, char **argv)
                        case 'q':
                                flags |= CLI_FLAG_QUIET;
                                break;
+                       case 'X':
+                               flags &= ~CLI_FLAG_SHOW_EXT;
+                               break;
                        default:
                                uci_usage();
                                return 0;