cli.c 14 KB

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