cli.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * cli - Command Line Interface for the Unified Configuration Interface
  3. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <strings.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include "uci.h"
  19. #define MAX_ARGS 4 /* max command line arguments for batch mode */
  20. static const char *delimiter = " ";
  21. static const char *appname;
  22. static enum {
  23. CLI_FLAG_MERGE = (1 << 0),
  24. CLI_FLAG_QUIET = (1 << 1),
  25. CLI_FLAG_NOCOMMIT = (1 << 2),
  26. CLI_FLAG_BATCH = (1 << 3),
  27. CLI_FLAG_SHOW_EXT = (1 << 4),
  28. } flags;
  29. static FILE *input;
  30. static struct uci_context *ctx;
  31. enum {
  32. /* section cmds */
  33. CMD_GET,
  34. CMD_SET,
  35. CMD_ADD_LIST,
  36. CMD_DEL,
  37. CMD_RENAME,
  38. CMD_REVERT,
  39. /* package cmds */
  40. CMD_SHOW,
  41. CMD_CHANGES,
  42. CMD_EXPORT,
  43. CMD_COMMIT,
  44. /* other cmds */
  45. CMD_ADD,
  46. CMD_IMPORT,
  47. CMD_HELP,
  48. };
  49. struct uci_type_list {
  50. unsigned int idx;
  51. const char *name;
  52. struct uci_type_list *next;
  53. };
  54. static struct uci_type_list *type_list = NULL;
  55. static char *typestr = NULL;
  56. static const char *cur_section_ref = NULL;
  57. static int uci_cmd(int argc, char **argv);
  58. static void
  59. uci_reset_typelist(void)
  60. {
  61. struct uci_type_list *type;
  62. while (type_list != NULL) {
  63. type = type_list;
  64. type_list = type_list->next;
  65. free(type);
  66. }
  67. if (typestr) {
  68. free(typestr);
  69. typestr = NULL;
  70. }
  71. cur_section_ref = NULL;
  72. }
  73. static char *
  74. uci_lookup_section_ref(struct uci_section *s)
  75. {
  76. struct uci_type_list *ti = type_list;
  77. int maxlen;
  78. if (!s->anonymous || !(flags & CLI_FLAG_SHOW_EXT))
  79. return s->e.name;
  80. /* look up in section type list */
  81. while (ti) {
  82. if (strcmp(ti->name, s->type) == 0)
  83. break;
  84. ti = ti->next;
  85. }
  86. if (!ti) {
  87. ti = malloc(sizeof(struct uci_type_list));
  88. memset(ti, 0, sizeof(struct uci_type_list));
  89. ti->next = type_list;
  90. type_list = ti;
  91. ti->name = s->type;
  92. }
  93. maxlen = strlen(s->type) + 1 + 2 + 10;
  94. if (!typestr) {
  95. typestr = malloc(maxlen);
  96. } else {
  97. typestr = realloc(typestr, maxlen);
  98. }
  99. sprintf(typestr, "@%s[%d]", ti->name, ti->idx);
  100. ti->idx++;
  101. return typestr;
  102. }
  103. static void uci_usage(void)
  104. {
  105. fprintf(stderr,
  106. "Usage: %s [<options>] <command> [<arguments>]\n\n"
  107. "Commands:\n"
  108. "\tbatch\n"
  109. "\texport [<config>]\n"
  110. "\timport [<config>]\n"
  111. "\tchanges [<config>]\n"
  112. "\tcommit [<config>]\n"
  113. "\tadd <config> <section-type>\n"
  114. "\tadd_list <config>.<section>.<option>=<string>\n"
  115. "\tshow [<config>[.<section>[.<option>]]]\n"
  116. "\tget <config>.<section>[.<option>]\n"
  117. "\tset <config>.<section>[.<option>]=<value>\n"
  118. "\tdelete <config>[.<section[.<option>]]\n"
  119. "\trename <config>.<section>[.<option>]=<name>\n"
  120. "\trevert <config>[.<section>[.<option>]]\n"
  121. "\n"
  122. "Options:\n"
  123. "\t-c <path> set the search path for config files (default: /etc/config)\n"
  124. "\t-d <str> set the delimiter for list values in uci show\n"
  125. "\t-f <file> use <file> as input instead of stdin\n"
  126. "\t-m when importing, merge data into an existing package\n"
  127. "\t-n name unnamed sections on export (default)\n"
  128. "\t-N don't name unnamed sections\n"
  129. "\t-p <path> add a search path for config change files\n"
  130. "\t-P <path> add a search path for config change files and use as default\n"
  131. "\t-q quiet mode (don't print error messages)\n"
  132. "\t-s force strict mode (stop on parser errors, default)\n"
  133. "\t-S disable strict mode\n"
  134. "\t-X do not use extended syntax on 'show'\n"
  135. "\n",
  136. appname
  137. );
  138. }
  139. static void cli_perror(void)
  140. {
  141. if (flags & CLI_FLAG_QUIET)
  142. return;
  143. uci_perror(ctx, appname);
  144. }
  145. static void uci_show_value(struct uci_option *o)
  146. {
  147. struct uci_element *e;
  148. bool sep = false;
  149. switch(o->type) {
  150. case UCI_TYPE_STRING:
  151. printf("%s\n", o->v.string);
  152. break;
  153. case UCI_TYPE_LIST:
  154. uci_foreach_element(&o->v.list, e) {
  155. printf("%s%s", (sep ? delimiter : ""), e->name);
  156. sep = true;
  157. }
  158. printf("\n");
  159. break;
  160. default:
  161. printf("<unknown>\n");
  162. break;
  163. }
  164. }
  165. static void uci_show_option(struct uci_option *o)
  166. {
  167. printf("%s.%s.%s=",
  168. o->section->package->e.name,
  169. (cur_section_ref ? cur_section_ref : o->section->e.name),
  170. o->e.name);
  171. uci_show_value(o);
  172. }
  173. static void uci_show_section(struct uci_section *s)
  174. {
  175. struct uci_element *e;
  176. const char *cname;
  177. const char *sname;
  178. cname = s->package->e.name;
  179. sname = (cur_section_ref ? cur_section_ref : s->e.name);
  180. printf("%s.%s=%s\n", cname, sname, s->type);
  181. uci_foreach_element(&s->options, e) {
  182. uci_show_option(uci_to_option(e));
  183. }
  184. }
  185. static void uci_show_package(struct uci_package *p)
  186. {
  187. struct uci_element *e;
  188. uci_reset_typelist();
  189. uci_foreach_element( &p->sections, e) {
  190. struct uci_section *s = uci_to_section(e);
  191. cur_section_ref = uci_lookup_section_ref(s);
  192. uci_show_section(s);
  193. }
  194. uci_reset_typelist();
  195. }
  196. static void uci_show_changes(struct uci_package *p)
  197. {
  198. struct uci_element *e;
  199. uci_foreach_element(&p->saved_history, e) {
  200. struct uci_history *h = uci_to_history(e);
  201. char *prefix = "";
  202. char *op = "=";
  203. switch(h->cmd) {
  204. case UCI_CMD_REMOVE:
  205. prefix = "-";
  206. break;
  207. case UCI_CMD_LIST_ADD:
  208. op = "+=";
  209. break;
  210. default:
  211. break;
  212. }
  213. printf("%s%s.%s", prefix, p->e.name, h->section);
  214. if (e->name)
  215. printf(".%s", e->name);
  216. if (h->cmd != UCI_CMD_REMOVE)
  217. printf("%s%s", op, h->value);
  218. printf("\n");
  219. }
  220. }
  221. static int package_cmd(int cmd, char *tuple)
  222. {
  223. struct uci_element *e = NULL;
  224. struct uci_ptr ptr;
  225. if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
  226. cli_perror();
  227. return 1;
  228. }
  229. e = ptr.last;
  230. switch(cmd) {
  231. case CMD_CHANGES:
  232. uci_show_changes(ptr.p);
  233. break;
  234. case CMD_COMMIT:
  235. if (flags & CLI_FLAG_NOCOMMIT)
  236. return 0;
  237. if (uci_commit(ctx, &ptr.p, false) != UCI_OK)
  238. cli_perror();
  239. break;
  240. case CMD_EXPORT:
  241. uci_export(ctx, stdout, ptr.p, true);
  242. break;
  243. case CMD_SHOW:
  244. if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
  245. ctx->err = UCI_ERR_NOTFOUND;
  246. cli_perror();
  247. return 1;
  248. }
  249. switch(e->type) {
  250. case UCI_TYPE_PACKAGE:
  251. uci_show_package(ptr.p);
  252. break;
  253. case UCI_TYPE_SECTION:
  254. uci_show_section(ptr.s);
  255. break;
  256. case UCI_TYPE_OPTION:
  257. uci_show_option(ptr.o);
  258. break;
  259. default:
  260. /* should not happen */
  261. return 1;
  262. }
  263. break;
  264. }
  265. uci_unload(ctx, ptr.p);
  266. return 0;
  267. }
  268. static int uci_do_import(int argc, char **argv)
  269. {
  270. struct uci_package *package = NULL;
  271. char *name = NULL;
  272. int ret = UCI_OK;
  273. bool merge = false;
  274. if (argc > 2)
  275. return 255;
  276. if (argc == 2)
  277. name = argv[1];
  278. else if (flags & CLI_FLAG_MERGE)
  279. /* need a package to merge */
  280. return 255;
  281. if (flags & CLI_FLAG_MERGE) {
  282. if (uci_load(ctx, name, &package) != UCI_OK)
  283. package = NULL;
  284. else
  285. merge = true;
  286. }
  287. ret = uci_import(ctx, input, name, &package, (name != NULL));
  288. if (ret == UCI_OK) {
  289. if (merge) {
  290. ret = uci_save(ctx, package);
  291. } else {
  292. struct uci_element *e;
  293. /* loop through all config sections and overwrite existing data */
  294. uci_foreach_element(&ctx->root, e) {
  295. struct uci_package *p = uci_to_package(e);
  296. ret = uci_commit(ctx, &p, true);
  297. }
  298. }
  299. }
  300. if (ret != UCI_OK) {
  301. cli_perror();
  302. return 1;
  303. }
  304. return 0;
  305. }
  306. static int uci_do_package_cmd(int cmd, int argc, char **argv)
  307. {
  308. char **configs = NULL;
  309. char **p;
  310. if (argc > 2)
  311. return 255;
  312. if (argc == 2)
  313. return package_cmd(cmd, argv[1]);
  314. if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
  315. cli_perror();
  316. return 1;
  317. }
  318. for (p = configs; *p; p++) {
  319. package_cmd(cmd, *p);
  320. }
  321. return 0;
  322. }
  323. static int uci_do_add(int argc, char **argv)
  324. {
  325. struct uci_package *p = NULL;
  326. struct uci_section *s = NULL;
  327. int ret;
  328. if (argc != 3)
  329. return 255;
  330. ret = uci_load(ctx, argv[1], &p);
  331. if (ret != UCI_OK)
  332. goto done;
  333. ret = uci_add_section(ctx, p, argv[2], &s);
  334. if (ret != UCI_OK)
  335. goto done;
  336. ret = uci_save(ctx, p);
  337. done:
  338. if (ret != UCI_OK)
  339. cli_perror();
  340. else if (s)
  341. fprintf(stdout, "%s\n", s->e.name);
  342. return ret;
  343. }
  344. static int uci_do_section_cmd(int cmd, int argc, char **argv)
  345. {
  346. struct uci_element *e;
  347. struct uci_ptr ptr;
  348. int ret = UCI_OK;
  349. if (argc != 2)
  350. return 255;
  351. if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
  352. cli_perror();
  353. return 1;
  354. }
  355. if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_ADD_LIST) && (cmd != CMD_RENAME))
  356. return 1;
  357. e = ptr.last;
  358. switch(cmd) {
  359. case CMD_GET:
  360. if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
  361. ctx->err = UCI_ERR_NOTFOUND;
  362. cli_perror();
  363. return 1;
  364. }
  365. switch(e->type) {
  366. case UCI_TYPE_SECTION:
  367. printf("%s\n", ptr.s->type);
  368. break;
  369. case UCI_TYPE_OPTION:
  370. uci_show_value(ptr.o);
  371. break;
  372. default:
  373. break;
  374. }
  375. /* throw the value to stdout */
  376. break;
  377. case CMD_RENAME:
  378. ret = uci_rename(ctx, &ptr);
  379. break;
  380. case CMD_REVERT:
  381. ret = uci_revert(ctx, &ptr);
  382. break;
  383. case CMD_SET:
  384. ret = uci_set(ctx, &ptr);
  385. break;
  386. case CMD_ADD_LIST:
  387. ret = uci_add_list(ctx, &ptr);
  388. break;
  389. case CMD_DEL:
  390. ret = uci_delete(ctx, &ptr);
  391. break;
  392. }
  393. /* no save necessary for get */
  394. if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
  395. return 0;
  396. /* save changes, but don't commit them yet */
  397. if (ret == UCI_OK)
  398. ret = uci_save(ctx, ptr.p);
  399. if (ret != UCI_OK) {
  400. cli_perror();
  401. return 1;
  402. }
  403. return 0;
  404. }
  405. static int uci_batch_cmd(void)
  406. {
  407. char *argv[MAX_ARGS + 2];
  408. char *str = NULL;
  409. int ret = 0;
  410. int i, j;
  411. for(i = 0; i <= MAX_ARGS; i++) {
  412. if (i == MAX_ARGS) {
  413. fprintf(stderr, "Too many arguments\n");
  414. return 1;
  415. }
  416. argv[i] = NULL;
  417. if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
  418. cli_perror();
  419. i = 0;
  420. break;
  421. }
  422. if (!argv[i][0])
  423. break;
  424. argv[i] = strdup(argv[i]);
  425. if (!argv[i]) {
  426. perror("uci");
  427. return 1;
  428. }
  429. }
  430. argv[i] = NULL;
  431. if (i > 0) {
  432. if (!strcasecmp(argv[0], "exit"))
  433. return 254;
  434. ret = uci_cmd(i, argv);
  435. } else
  436. return 0;
  437. for (j = 0; j < i; j++) {
  438. if (argv[j])
  439. free(argv[j]);
  440. }
  441. return ret;
  442. }
  443. static int uci_batch(void)
  444. {
  445. int ret = 0;
  446. flags |= CLI_FLAG_BATCH;
  447. while (!feof(input)) {
  448. struct uci_element *e, *tmp;
  449. ret = uci_batch_cmd();
  450. if (ret == 254)
  451. return 0;
  452. else if (ret == 255)
  453. fprintf(stderr, "Unknown command\n");
  454. /* clean up */
  455. uci_foreach_element_safe(&ctx->root, tmp, e) {
  456. uci_unload(ctx, uci_to_package(e));
  457. }
  458. }
  459. flags &= ~CLI_FLAG_BATCH;
  460. return 0;
  461. }
  462. static int uci_cmd(int argc, char **argv)
  463. {
  464. int cmd = 0;
  465. if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
  466. return uci_batch();
  467. else if (!strcasecmp(argv[0], "show"))
  468. cmd = CMD_SHOW;
  469. else if (!strcasecmp(argv[0], "changes"))
  470. cmd = CMD_CHANGES;
  471. else if (!strcasecmp(argv[0], "export"))
  472. cmd = CMD_EXPORT;
  473. else if (!strcasecmp(argv[0], "commit"))
  474. cmd = CMD_COMMIT;
  475. else if (!strcasecmp(argv[0], "get"))
  476. cmd = CMD_GET;
  477. else if (!strcasecmp(argv[0], "set"))
  478. cmd = CMD_SET;
  479. else if (!strcasecmp(argv[0], "ren") ||
  480. !strcasecmp(argv[0], "rename"))
  481. cmd = CMD_RENAME;
  482. else if (!strcasecmp(argv[0], "revert"))
  483. cmd = CMD_REVERT;
  484. else if (!strcasecmp(argv[0], "del") ||
  485. !strcasecmp(argv[0], "delete"))
  486. cmd = CMD_DEL;
  487. else if (!strcasecmp(argv[0], "import"))
  488. cmd = CMD_IMPORT;
  489. else if (!strcasecmp(argv[0], "help"))
  490. cmd = CMD_HELP;
  491. else if (!strcasecmp(argv[0], "add"))
  492. cmd = CMD_ADD;
  493. else if (!strcasecmp(argv[0], "add_list"))
  494. cmd = CMD_ADD_LIST;
  495. else
  496. cmd = -1;
  497. switch(cmd) {
  498. case CMD_ADD_LIST:
  499. case CMD_GET:
  500. case CMD_SET:
  501. case CMD_DEL:
  502. case CMD_RENAME:
  503. case CMD_REVERT:
  504. return uci_do_section_cmd(cmd, argc, argv);
  505. case CMD_SHOW:
  506. case CMD_EXPORT:
  507. case CMD_COMMIT:
  508. case CMD_CHANGES:
  509. return uci_do_package_cmd(cmd, argc, argv);
  510. case CMD_IMPORT:
  511. return uci_do_import(argc, argv);
  512. case CMD_ADD:
  513. return uci_do_add(argc, argv);
  514. case CMD_HELP:
  515. uci_usage();
  516. return 0;
  517. default:
  518. return 255;
  519. }
  520. }
  521. int main(int argc, char **argv)
  522. {
  523. int ret;
  524. int c;
  525. flags = CLI_FLAG_SHOW_EXT;
  526. appname = argv[0];
  527. input = stdin;
  528. ctx = uci_alloc_context();
  529. if (!ctx) {
  530. fprintf(stderr, "Out of memory\n");
  531. return 1;
  532. }
  533. while((c = getopt(argc, argv, "c:d:f:mnNp:P:sSqX")) != -1) {
  534. switch(c) {
  535. case 'c':
  536. uci_set_confdir(ctx, optarg);
  537. break;
  538. case 'd':
  539. delimiter = optarg;
  540. break;
  541. case 'f':
  542. input = fopen(optarg, "r");
  543. if (!input) {
  544. perror("uci");
  545. return 1;
  546. }
  547. break;
  548. case 'm':
  549. flags |= CLI_FLAG_MERGE;
  550. break;
  551. case 's':
  552. ctx->flags |= UCI_FLAG_STRICT;
  553. break;
  554. case 'S':
  555. ctx->flags &= ~UCI_FLAG_STRICT;
  556. ctx->flags |= UCI_FLAG_PERROR;
  557. break;
  558. case 'n':
  559. ctx->flags |= UCI_FLAG_EXPORT_NAME;
  560. break;
  561. case 'N':
  562. ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
  563. break;
  564. case 'p':
  565. uci_add_history_path(ctx, optarg);
  566. break;
  567. case 'P':
  568. uci_add_history_path(ctx, ctx->savedir);
  569. uci_set_savedir(ctx, optarg);
  570. flags |= CLI_FLAG_NOCOMMIT;
  571. break;
  572. case 'q':
  573. flags |= CLI_FLAG_QUIET;
  574. break;
  575. case 'X':
  576. flags &= ~CLI_FLAG_SHOW_EXT;
  577. break;
  578. default:
  579. uci_usage();
  580. return 0;
  581. }
  582. }
  583. if (optind > 1)
  584. argv[optind - 1] = argv[0];
  585. argv += optind - 1;
  586. argc -= optind - 1;
  587. if (argc < 2) {
  588. uci_usage();
  589. return 0;
  590. }
  591. ret = uci_cmd(argc - 1, argv + 1);
  592. if (input != stdin)
  593. fclose(input);
  594. if (ret == 255) {
  595. uci_usage();
  596. return 0;
  597. }
  598. uci_free_context(ctx);
  599. return ret;
  600. }