cli.c 15 KB

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