cli.c 14 KB

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