2
0

cli.c 15 KB

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