cli.c 13 KB

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