#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),
/* section cmds */
CMD_GET,
CMD_SET,
+ CMD_ADD_LIST,
CMD_DEL,
CMD_RENAME,
CMD_REVERT,
"\tchanges [<config>]\n"
"\tcommit [<config>]\n"
"\tadd <config> <section-type>\n"
+ "\tadd_list <config>.<section>.<option>=<string>\n"
"\tshow [<config>[.<section>[.<option>]]]\n"
"\tget <config>.<section>[.<option>]\n"
"\tset <config>.<section>[.<option>]=<value>\n"
+ "\tdelete <config>[.<section[.<option>]]\n"
"\trename <config>.<section>[.<option>]=<name>\n"
"\trevert <config>[.<section>[.<option>]]\n"
"\n"
"Options:\n"
"\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-m when importing, merge data into an existing package\n"
"\t-n name unnamed sections on export (default)\n"
uci_perror(ctx, appname);
}
+static void uci_show_value(struct uci_option *o)
+{
+ struct uci_element *e;
+ bool sep = false;
+
+ switch(o->type) {
+ case UCI_TYPE_STRING:
+ printf("%s\n", o->v.string);
+ break;
+ case UCI_TYPE_LIST:
+ uci_foreach_element(&o->v.list, e) {
+ printf("%s%s", (sep ? delimiter : ""), e->name);
+ sep = true;
+ }
+ printf("\n");
+ break;
+ default:
+ printf("<unknown>\n");
+ break;
+ }
+}
+
static void uci_show_option(struct uci_option *o)
{
- printf("%s.%s.%s=%s\n",
- o->section->package->e.name,
- o->section->e.name,
- o->e.name,
- o->value);
+ printf("%s.%s.%s=",
+ o->section->package->e.name,
+ o->section->e.name,
+ o->e.name);
+ uci_show_value(o);
}
static void uci_show_section(struct uci_section *p)
struct uci_package *p = NULL;
struct uci_section *s = NULL;
struct uci_element *e = NULL;
+ struct uci_option *o = NULL;
+ char *package = NULL;
char *section = NULL;
char *option = NULL;
char *value = NULL;
int ret = UCI_OK;
+ char *str;
if (argc != 2)
return 255;
return 1;
}
- if (value && (cmd != CMD_SET) && (cmd != CMD_RENAME))
+ str = strdup(argv[1]);
+ if (!str)
return 1;
+ if (value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME))
+ return 1;
+
+ if (uci_parse_tuple(ctx, str, &package, §ion, &option, NULL) != UCI_OK) {
+ cli_perror();
+ return 1;
+ }
+ sprintf(argv[1], "%s.%s", package, section);
+ free(str);
+
if (uci_lookup_ext(ctx, &e, argv[1]) != UCI_OK) {
cli_perror();
return 1;
}
switch(e->type) {
+ case UCI_TYPE_PACKAGE:
+ p = uci_to_package(e);
+ break;
case UCI_TYPE_SECTION:
s = uci_to_section(e);
break;
default:
return 1;
}
- section = s->e.name;
- p = s->package;
+ if (s) {
+ section = s->e.name;
+ p = s->package;
+ }
switch(cmd) {
case CMD_GET:
switch(e->type) {
case UCI_TYPE_SECTION:
- value = s->type;
+ printf("%s\n", s->type);
break;
case UCI_TYPE_OPTION:
- value = uci_to_option(e)->value;
+ o = uci_to_option(e);
+ uci_show_value(o);
break;
default:
break;
}
/* throw the value to stdout */
- printf("%s\n", value);
break;
case CMD_RENAME:
ret = uci_rename(ctx, p, section, option, value);
case CMD_SET:
ret = uci_set(ctx, p, section, option, value, NULL);
break;
+ case CMD_ADD_LIST:
+ ret = uci_add_list(ctx, p, section, option, value, NULL);
+ break;
case CMD_DEL:
ret = uci_delete(ctx, p, section, option);
break;
cmd = CMD_HELP;
else if (!strcasecmp(argv[0], "add"))
cmd = CMD_ADD;
+ else if (!strcasecmp(argv[0], "add_list"))
+ cmd = CMD_ADD_LIST;
else
cmd = -1;
switch(cmd) {
+ case CMD_ADD_LIST:
case CMD_GET:
case CMD_SET:
case CMD_DEL:
return 1;
}
- while((c = getopt(argc, argv, "c:f:mnNp:P:sSq")) != -1) {
+ while((c = getopt(argc, argv, "c:d:f:mnNp:P:sSq")) != -1) {
switch(c) {
case 'c':
uci_set_confdir(ctx, optarg);
break;
+ case 'd':
+ delimiter = optarg;
+ break;
case 'f':
input = fopen(optarg, "r");
if (!input) {