cli.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 *appname;
  21. static enum {
  22. CLI_FLAG_MERGE = (1 << 0),
  23. CLI_FLAG_QUIET = (1 << 1),
  24. CLI_FLAG_NOCOMMIT = (1 << 2),
  25. CLI_FLAG_BATCH = (1 << 3),
  26. } flags;
  27. static FILE *input;
  28. static struct uci_context *ctx;
  29. enum {
  30. /* section cmds */
  31. CMD_GET,
  32. CMD_SET,
  33. CMD_DEL,
  34. CMD_RENAME,
  35. CMD_REVERT,
  36. /* package cmds */
  37. CMD_SHOW,
  38. CMD_CHANGES,
  39. CMD_EXPORT,
  40. CMD_COMMIT,
  41. /* other cmds */
  42. CMD_ADD,
  43. CMD_IMPORT,
  44. CMD_HELP,
  45. };
  46. static int uci_cmd(int argc, char **argv);
  47. static void uci_usage(void)
  48. {
  49. fprintf(stderr,
  50. "Usage: %s [<options>] <command> [<arguments>]\n\n"
  51. "Commands:\n"
  52. "\tbatch\n"
  53. "\texport [<config>]\n"
  54. "\timport [<config>]\n"
  55. "\tchanges [<config>]\n"
  56. "\tadd <config> <section-type>\n"
  57. "\tshow [<config>[.<section>[.<option>]]]\n"
  58. "\tget <config>.<section>[.<option>]\n"
  59. "\tset <config>.<section>[.<option>]=<value>\n"
  60. "\trename <config>.<section>[.<option>]=<name>\n"
  61. "\trevert <config>[.<section>[.<option>]]\n"
  62. "\n"
  63. "Options:\n"
  64. "\t-f <file> use <file> as input instead of stdin\n"
  65. "\t-m when importing, merge data into an existing package\n"
  66. "\t-n name unnamed sections on export (default)\n"
  67. "\t-N don't name unnamed sections\n"
  68. "\t-p <path> add a search path for config change files\n"
  69. "\t-P <path> add a search path for config change files and use as default\n"
  70. "\t-q quiet mode (don't print error messages)\n"
  71. "\t-s force strict mode (stop on parser errors, default)\n"
  72. "\t-S disable strict mode\n"
  73. "\n",
  74. appname
  75. );
  76. }
  77. static void cli_perror(void)
  78. {
  79. if (flags & CLI_FLAG_QUIET)
  80. return;
  81. uci_perror(ctx, appname);
  82. }
  83. static void uci_show_section(struct uci_section *p)
  84. {
  85. struct uci_element *e;
  86. const char *cname, *sname;
  87. cname = p->package->e.name;
  88. sname = p->e.name;
  89. printf("%s.%s=%s\n", cname, sname, p->type);
  90. uci_foreach_element(&p->options, e) {
  91. printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
  92. }
  93. }
  94. static void uci_show_package(struct uci_package *p)
  95. {
  96. struct uci_element *e;
  97. uci_foreach_element( &p->sections, e) {
  98. uci_show_section(uci_to_section(e));
  99. }
  100. }
  101. static void uci_show_changes(struct uci_package *p)
  102. {
  103. struct uci_element *e;
  104. uci_foreach_element(&p->saved_history, e) {
  105. struct uci_history *h = uci_to_history(e);
  106. if (h->cmd == UCI_CMD_REMOVE)
  107. printf("-");
  108. printf("%s.%s", p->e.name, h->section);
  109. if (e->name)
  110. printf(".%s", e->name);
  111. if (h->cmd != UCI_CMD_REMOVE)
  112. printf("=%s", h->value);
  113. printf("\n");
  114. }
  115. }
  116. static int package_cmd(int cmd, char *package)
  117. {
  118. struct uci_package *p = NULL;
  119. int ret;
  120. if (cmd == CMD_CHANGES)
  121. ctx->flags |= UCI_FLAG_SAVED_HISTORY;
  122. ret = uci_load(ctx, package, &p);
  123. if (cmd == CMD_CHANGES)
  124. ctx->flags &= ~UCI_FLAG_SAVED_HISTORY;
  125. if (ret != UCI_OK) {
  126. cli_perror();
  127. return 1;
  128. }
  129. if (!p)
  130. return 0;
  131. switch(cmd) {
  132. case CMD_CHANGES:
  133. uci_show_changes(p);
  134. break;
  135. case CMD_COMMIT:
  136. if (flags & CLI_FLAG_NOCOMMIT)
  137. return 0;
  138. if (uci_commit(ctx, &p, false) != UCI_OK)
  139. cli_perror();
  140. break;
  141. case CMD_EXPORT:
  142. uci_export(ctx, stdout, p, true);
  143. break;
  144. case CMD_SHOW:
  145. uci_show_package(p);
  146. break;
  147. }
  148. uci_unload(ctx, p);
  149. return 0;
  150. }
  151. static int uci_do_import(int argc, char **argv)
  152. {
  153. struct uci_package *package = NULL;
  154. char *name = NULL;
  155. int ret = UCI_OK;
  156. bool merge = false;
  157. if (argc > 2)
  158. return 255;
  159. if (argc == 2)
  160. name = argv[1];
  161. else if (flags & CLI_FLAG_MERGE)
  162. /* need a package to merge */
  163. return 255;
  164. if (flags & CLI_FLAG_MERGE) {
  165. if (uci_load(ctx, name, &package) != UCI_OK)
  166. package = NULL;
  167. else
  168. merge = true;
  169. }
  170. ret = uci_import(ctx, input, name, &package, (name != NULL));
  171. if (ret == UCI_OK) {
  172. if (merge) {
  173. ret = uci_save(ctx, package);
  174. } else {
  175. struct uci_element *e;
  176. /* loop through all config sections and overwrite existing data */
  177. uci_foreach_element(&ctx->root, e) {
  178. struct uci_package *p = uci_to_package(e);
  179. ret = uci_commit(ctx, &p, true);
  180. }
  181. }
  182. }
  183. if (ret != UCI_OK) {
  184. cli_perror();
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. static int uci_do_package_cmd(int cmd, int argc, char **argv)
  190. {
  191. char **configs = NULL;
  192. char **p;
  193. if (argc > 2)
  194. return 255;
  195. if (argc == 2)
  196. return package_cmd(cmd, argv[1]);
  197. if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
  198. cli_perror();
  199. return 1;
  200. }
  201. for (p = configs; *p; p++) {
  202. package_cmd(cmd, *p);
  203. }
  204. return 0;
  205. }
  206. static int uci_do_add(int argc, char **argv)
  207. {
  208. struct uci_package *p = NULL;
  209. struct uci_section *s = NULL;
  210. int ret;
  211. if (argc != 3)
  212. return 255;
  213. ret = uci_load(ctx, argv[1], &p);
  214. if (ret != UCI_OK)
  215. goto done;
  216. ret = uci_add_section(ctx, p, argv[2], &s);
  217. if (ret != UCI_OK)
  218. goto done;
  219. ret = uci_save(ctx, p);
  220. done:
  221. if (ret != UCI_OK)
  222. cli_perror();
  223. else if (s)
  224. fprintf(stdout, "%s\n", s->e.name);
  225. return ret;
  226. }
  227. static int uci_do_section_cmd(int cmd, int argc, char **argv)
  228. {
  229. struct uci_package *p = NULL;
  230. struct uci_element *e = NULL;
  231. char *package = NULL;
  232. char *section = NULL;
  233. char *option = NULL;
  234. char *value = NULL;
  235. char **ptr = NULL;
  236. int ret = UCI_OK;
  237. if (argc != 2)
  238. return 255;
  239. switch(cmd) {
  240. case CMD_SET:
  241. case CMD_RENAME:
  242. ptr = &value;
  243. break;
  244. default:
  245. break;
  246. }
  247. if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, ptr) != UCI_OK)
  248. return 1;
  249. if (section && !section[0])
  250. return 1;
  251. if (uci_load(ctx, package, &p) != UCI_OK) {
  252. cli_perror();
  253. return 1;
  254. }
  255. if (!p)
  256. return 0;
  257. switch(cmd) {
  258. case CMD_GET:
  259. if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
  260. return 1;
  261. switch(e->type) {
  262. case UCI_TYPE_SECTION:
  263. value = uci_to_section(e)->type;
  264. break;
  265. case UCI_TYPE_OPTION:
  266. value = uci_to_option(e)->value;
  267. break;
  268. default:
  269. /* should not happen */
  270. return 1;
  271. }
  272. /* throw the value to stdout */
  273. printf("%s\n", value);
  274. break;
  275. case CMD_RENAME:
  276. ret = uci_rename(ctx, p, section, option, value);
  277. break;
  278. case CMD_REVERT:
  279. ret = uci_revert(ctx, &p, section, option);
  280. break;
  281. case CMD_SET:
  282. ret = uci_set(ctx, p, section, option, value, NULL);
  283. break;
  284. case CMD_DEL:
  285. ret = uci_delete(ctx, p, section, option);
  286. break;
  287. }
  288. /* no save necessary for get */
  289. if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
  290. return 0;
  291. /* save changes, but don't commit them yet */
  292. if (ret == UCI_OK)
  293. ret = uci_save(ctx, p);
  294. if (ret != UCI_OK) {
  295. cli_perror();
  296. return 1;
  297. }
  298. return 0;
  299. }
  300. static int uci_batch_cmd(void)
  301. {
  302. char *argv[MAX_ARGS];
  303. char *str = NULL;
  304. int ret = 0;
  305. int i, j;
  306. for(i = 0; i <= MAX_ARGS; i++) {
  307. if (i == MAX_ARGS) {
  308. fprintf(stderr, "Too many arguments\n");
  309. return 1;
  310. }
  311. argv[i] = NULL;
  312. if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
  313. cli_perror();
  314. i = 0;
  315. break;
  316. }
  317. if (!argv[i][0])
  318. break;
  319. argv[i] = strdup(argv[i]);
  320. if (!argv[i]) {
  321. perror("uci");
  322. return 1;
  323. }
  324. }
  325. argv[i] = NULL;
  326. if (i > 0) {
  327. if (!strcasecmp(argv[0], "exit"))
  328. return 254;
  329. ret = uci_cmd(i, argv);
  330. } else
  331. return 0;
  332. for (j = 0; j < i; j++) {
  333. if (argv[j])
  334. free(argv[j]);
  335. }
  336. return ret;
  337. }
  338. static int uci_batch(void)
  339. {
  340. int ret = 0;
  341. while (!feof(input)) {
  342. struct uci_element *e, *tmp;
  343. ret = uci_batch_cmd();
  344. if (ret == 254)
  345. return 0;
  346. else if (ret == 255)
  347. fprintf(stderr, "Unknown command\n");
  348. /* clean up */
  349. uci_foreach_element_safe(&ctx->root, tmp, e) {
  350. uci_unload(ctx, uci_to_package(e));
  351. }
  352. }
  353. return 0;
  354. }
  355. static int uci_cmd(int argc, char **argv)
  356. {
  357. int cmd = 0;
  358. if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
  359. return uci_batch();
  360. else if (!strcasecmp(argv[0], "show"))
  361. cmd = CMD_SHOW;
  362. else if (!strcasecmp(argv[0], "changes"))
  363. cmd = CMD_CHANGES;
  364. else if (!strcasecmp(argv[0], "export"))
  365. cmd = CMD_EXPORT;
  366. else if (!strcasecmp(argv[0], "commit"))
  367. cmd = CMD_COMMIT;
  368. else if (!strcasecmp(argv[0], "get"))
  369. cmd = CMD_GET;
  370. else if (!strcasecmp(argv[0], "set"))
  371. cmd = CMD_SET;
  372. else if (!strcasecmp(argv[0], "ren") ||
  373. !strcasecmp(argv[0], "rename"))
  374. cmd = CMD_RENAME;
  375. else if (!strcasecmp(argv[0], "revert"))
  376. cmd = CMD_REVERT;
  377. else if (!strcasecmp(argv[0], "del"))
  378. cmd = CMD_DEL;
  379. else if (!strcasecmp(argv[0], "import"))
  380. cmd = CMD_IMPORT;
  381. else if (!strcasecmp(argv[0], "help"))
  382. cmd = CMD_HELP;
  383. else if (!strcasecmp(argv[0], "add"))
  384. cmd = CMD_ADD;
  385. else
  386. cmd = -1;
  387. switch(cmd) {
  388. case CMD_GET:
  389. case CMD_SET:
  390. case CMD_DEL:
  391. case CMD_RENAME:
  392. case CMD_REVERT:
  393. return uci_do_section_cmd(cmd, argc, argv);
  394. case CMD_SHOW:
  395. case CMD_EXPORT:
  396. case CMD_COMMIT:
  397. case CMD_CHANGES:
  398. return uci_do_package_cmd(cmd, argc, argv);
  399. case CMD_IMPORT:
  400. return uci_do_import(argc, argv);
  401. case CMD_ADD:
  402. return uci_do_add(argc, argv);
  403. case CMD_HELP:
  404. uci_usage();
  405. return 0;
  406. default:
  407. return 255;
  408. }
  409. }
  410. int main(int argc, char **argv)
  411. {
  412. int ret;
  413. int c;
  414. appname = argv[0];
  415. input = stdin;
  416. ctx = uci_alloc_context();
  417. if (!ctx) {
  418. fprintf(stderr, "Out of memory\n");
  419. return 1;
  420. }
  421. while((c = getopt(argc, argv, "f:mnNp:P:sSq")) != -1) {
  422. switch(c) {
  423. case 'f':
  424. input = fopen(optarg, "r");
  425. if (!input) {
  426. perror("uci");
  427. return 1;
  428. }
  429. break;
  430. case 'm':
  431. flags |= CLI_FLAG_MERGE;
  432. break;
  433. case 's':
  434. ctx->flags |= UCI_FLAG_STRICT;
  435. break;
  436. case 'S':
  437. ctx->flags &= ~UCI_FLAG_STRICT;
  438. ctx->flags |= UCI_FLAG_PERROR;
  439. break;
  440. case 'n':
  441. ctx->flags |= UCI_FLAG_EXPORT_NAME;
  442. break;
  443. case 'N':
  444. ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
  445. break;
  446. case 'p':
  447. uci_add_history_path(ctx, optarg);
  448. break;
  449. case 'P':
  450. uci_add_history_path(ctx, ctx->savedir);
  451. uci_set_savedir(ctx, optarg);
  452. flags |= CLI_FLAG_NOCOMMIT;
  453. break;
  454. case 'q':
  455. flags |= CLI_FLAG_QUIET;
  456. break;
  457. default:
  458. uci_usage();
  459. return 0;
  460. }
  461. }
  462. if (optind > 1)
  463. argv[optind - 1] = argv[0];
  464. argv += optind - 1;
  465. argc -= optind - 1;
  466. if (argc < 2) {
  467. uci_usage();
  468. return 0;
  469. }
  470. ret = uci_cmd(argc - 1, argv + 1);
  471. if (input != stdin)
  472. fclose(input);
  473. if (ret == 255) {
  474. uci_usage();
  475. return 0;
  476. }
  477. uci_free_context(ctx);
  478. return ret;
  479. }