/* * cli - Command Line Interface for the Unified Configuration Interface * Copyright (C) 2008 Felix Fietkau * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include #include #include #include #include #include #include "uci.h" #define MAX_ARGS 4 /* max command line arguments for batch mode */ static const char *delimiter = " "; static const char *appname; static enum { CLI_FLAG_MERGE = (1 << 0), 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; static struct uci_context *ctx; enum { /* section cmds */ CMD_GET, CMD_SET, CMD_ADD_LIST, CMD_DEL_LIST, CMD_DEL, CMD_RENAME, CMD_REVERT, CMD_REORDER, /* package cmds */ CMD_SHOW, CMD_CHANGES, CMD_EXPORT, CMD_COMMIT, /* other cmds */ CMD_ADD, CMD_IMPORT, CMD_HELP, }; struct uci_type_list { unsigned int idx; const char *name; struct uci_type_list *next; }; static struct uci_type_list *type_list = NULL; static char *typestr = NULL; static const char *cur_section_ref = NULL; static int uci_cmd(int argc, char **argv); static void uci_reset_typelist(void) { struct uci_type_list *type; while (type_list != NULL) { type = type_list; type_list = type_list->next; free(type); } if (typestr) { free(typestr); typestr = NULL; } cur_section_ref = NULL; } static char * uci_lookup_section_ref(struct uci_section *s) { struct uci_type_list *ti = type_list; char *ret; int maxlen; if (!(flags & CLI_FLAG_SHOW_EXT)) return s->e.name; /* look up in section type list */ while (ti) { if (strcmp(ti->name, s->type) == 0) break; ti = ti->next; } if (!ti) { ti = calloc(1, sizeof(struct uci_type_list)); if (!ti) return NULL; ti->next = type_list; type_list = ti; ti->name = s->type; } 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 { ret = s->e.name; } ti->idx++; return ret; } static void uci_usage(void) { fprintf(stderr, "Usage: %s [] []\n\n" "Commands:\n" "\tbatch\n" "\texport []\n" "\timport []\n" "\tchanges []\n" "\tcommit []\n" "\tadd \n" "\tadd_list .
.