cli.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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_ptr ptr;
  283. int ret = 1;
  284. if (uci_lookup_ptr(ctx, &ptr, tuple, true) != UCI_OK) {
  285. cli_perror();
  286. return 1;
  287. }
  288. switch(cmd) {
  289. case CMD_CHANGES:
  290. uci_show_changes(ptr.p);
  291. break;
  292. case CMD_COMMIT:
  293. if (flags & CLI_FLAG_NOCOMMIT) {
  294. ret = 0;
  295. goto out;
  296. }
  297. if (uci_commit(ctx, &ptr.p, false) != UCI_OK) {
  298. cli_perror();
  299. goto out;
  300. }
  301. break;
  302. case CMD_EXPORT:
  303. if (uci_export(ctx, stdout, ptr.p, true) != UCI_OK) {
  304. goto out;
  305. }
  306. break;
  307. case CMD_SHOW:
  308. if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
  309. ctx->err = UCI_ERR_NOTFOUND;
  310. cli_perror();
  311. goto out;
  312. }
  313. if (ptr.o)
  314. uci_show_option(ptr.o, true);
  315. else if (ptr.s)
  316. uci_show_section(ptr.s);
  317. else if (ptr.p)
  318. uci_show_package(ptr.p);
  319. else
  320. goto out; /* should not happen */
  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_ptr ptr;
  412. int ret = UCI_OK;
  413. int dummy;
  414. if (argc != 2)
  415. return 255;
  416. if (uci_lookup_ptr(ctx, &ptr, argv[1], true) != UCI_OK) {
  417. cli_perror();
  418. return 1;
  419. }
  420. if (ptr.value && (cmd != CMD_SET) && (cmd != CMD_DEL) &&
  421. (cmd != CMD_ADD_LIST) && (cmd != CMD_DEL_LIST) &&
  422. (cmd != CMD_RENAME) && (cmd != CMD_REORDER))
  423. return 1;
  424. switch(cmd) {
  425. case CMD_GET:
  426. if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
  427. ctx->err = UCI_ERR_NOTFOUND;
  428. cli_perror();
  429. return 1;
  430. }
  431. if (ptr.o)
  432. uci_show_value(ptr.o, false);
  433. else if (ptr.s)
  434. printf("%s\n", ptr.s->type);
  435. break;
  436. case CMD_RENAME:
  437. ret = uci_rename(ctx, &ptr);
  438. break;
  439. case CMD_REVERT:
  440. ret = uci_revert(ctx, &ptr);
  441. break;
  442. case CMD_SET:
  443. ret = uci_set(ctx, &ptr);
  444. break;
  445. case CMD_ADD_LIST:
  446. ret = uci_add_list(ctx, &ptr);
  447. break;
  448. case CMD_DEL_LIST:
  449. ret = uci_del_list(ctx, &ptr);
  450. break;
  451. case CMD_REORDER:
  452. if (!ptr.s || !ptr.value) {
  453. ctx->err = UCI_ERR_NOTFOUND;
  454. cli_perror();
  455. return 1;
  456. }
  457. ret = uci_reorder_section(ctx, ptr.s, strtoul(ptr.value, NULL, 10));
  458. break;
  459. case CMD_DEL:
  460. if (ptr.value && !sscanf(ptr.value, "%d", &dummy))
  461. return 1;
  462. ret = uci_delete(ctx, &ptr);
  463. break;
  464. }
  465. /* no save necessary for get */
  466. if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
  467. return 0;
  468. /* save changes, but don't commit them yet */
  469. if (ret == UCI_OK)
  470. ret = uci_save(ctx, ptr.p);
  471. if (ret != UCI_OK) {
  472. cli_perror();
  473. return 1;
  474. }
  475. return 0;
  476. }
  477. static int uci_batch_cmd(void)
  478. {
  479. char *argv[MAX_ARGS + 2];
  480. char *str = NULL;
  481. int ret = 0;
  482. int i, j;
  483. for(i = 0; i <= MAX_ARGS; i++) {
  484. if (i == MAX_ARGS) {
  485. cli_error("Too many arguments\n");
  486. return 1;
  487. }
  488. argv[i] = NULL;
  489. if (uci_parse_argument(ctx, input, &str, &argv[i]) != UCI_OK) {
  490. cli_perror();
  491. i = 0;
  492. break;
  493. }
  494. if (!argv[i][0])
  495. break;
  496. argv[i] = strdup(argv[i]);
  497. if (!argv[i]) {
  498. cli_error("uci: %s", strerror(errno));
  499. return 1;
  500. }
  501. }
  502. argv[i] = NULL;
  503. if (i > 0) {
  504. if (!strcasecmp(argv[0], "exit"))
  505. return 254;
  506. ret = uci_cmd(i, argv);
  507. } else
  508. return 0;
  509. for (j = 0; j < i; j++) {
  510. free(argv[j]);
  511. }
  512. return ret;
  513. }
  514. static int uci_batch(void)
  515. {
  516. int ret = 0;
  517. flags |= CLI_FLAG_BATCH;
  518. while (!feof(input)) {
  519. struct uci_element *e, *tmp;
  520. ret = uci_batch_cmd();
  521. if (ret == 254)
  522. return 0;
  523. else if (ret == 255)
  524. cli_error("Unknown command\n");
  525. /* clean up */
  526. uci_foreach_element_safe(&ctx->root, tmp, e) {
  527. uci_unload(ctx, uci_to_package(e));
  528. }
  529. }
  530. flags &= ~CLI_FLAG_BATCH;
  531. return 0;
  532. }
  533. static int uci_cmd(int argc, char **argv)
  534. {
  535. int cmd = 0;
  536. if (!strcasecmp(argv[0], "batch") && !(flags & CLI_FLAG_BATCH))
  537. return uci_batch();
  538. else if (!strcasecmp(argv[0], "show"))
  539. cmd = CMD_SHOW;
  540. else if (!strcasecmp(argv[0], "changes"))
  541. cmd = CMD_CHANGES;
  542. else if (!strcasecmp(argv[0], "export"))
  543. cmd = CMD_EXPORT;
  544. else if (!strcasecmp(argv[0], "commit"))
  545. cmd = CMD_COMMIT;
  546. else if (!strcasecmp(argv[0], "get"))
  547. cmd = CMD_GET;
  548. else if (!strcasecmp(argv[0], "set"))
  549. cmd = CMD_SET;
  550. else if (!strcasecmp(argv[0], "ren") ||
  551. !strcasecmp(argv[0], "rename"))
  552. cmd = CMD_RENAME;
  553. else if (!strcasecmp(argv[0], "revert"))
  554. cmd = CMD_REVERT;
  555. else if (!strcasecmp(argv[0], "reorder"))
  556. cmd = CMD_REORDER;
  557. else if (!strcasecmp(argv[0], "del") ||
  558. !strcasecmp(argv[0], "delete"))
  559. cmd = CMD_DEL;
  560. else if (!strcasecmp(argv[0], "import"))
  561. cmd = CMD_IMPORT;
  562. else if (!strcasecmp(argv[0], "help"))
  563. cmd = CMD_HELP;
  564. else if (!strcasecmp(argv[0], "add"))
  565. cmd = CMD_ADD;
  566. else if (!strcasecmp(argv[0], "add_list"))
  567. cmd = CMD_ADD_LIST;
  568. else if (!strcasecmp(argv[0], "del_list"))
  569. cmd = CMD_DEL_LIST;
  570. else
  571. cmd = -1;
  572. switch(cmd) {
  573. case CMD_ADD_LIST:
  574. case CMD_DEL_LIST:
  575. case CMD_GET:
  576. case CMD_SET:
  577. case CMD_DEL:
  578. case CMD_RENAME:
  579. case CMD_REVERT:
  580. case CMD_REORDER:
  581. return uci_do_section_cmd(cmd, argc, argv);
  582. case CMD_SHOW:
  583. case CMD_EXPORT:
  584. case CMD_COMMIT:
  585. case CMD_CHANGES:
  586. return uci_do_package_cmd(cmd, argc, argv);
  587. case CMD_IMPORT:
  588. return uci_do_import(argc, argv);
  589. case CMD_ADD:
  590. return uci_do_add(argc, argv);
  591. case CMD_HELP:
  592. uci_usage();
  593. return 0;
  594. default:
  595. return 255;
  596. }
  597. }
  598. int main(int argc, char **argv)
  599. {
  600. int ret;
  601. int c;
  602. flags = CLI_FLAG_SHOW_EXT;
  603. appname = argv[0];
  604. input = stdin;
  605. ctx = uci_alloc_context();
  606. if (!ctx) {
  607. cli_error("Out of memory\n");
  608. return 1;
  609. }
  610. while((c = getopt(argc, argv, "c:d:f:LmnNp:P:qsSt:X")) != -1) {
  611. switch(c) {
  612. case 'c':
  613. uci_set_confdir(ctx, optarg);
  614. break;
  615. case 'd':
  616. delimiter = optarg;
  617. break;
  618. case 'f':
  619. if (input != stdin) {
  620. fclose(input);
  621. cli_error("Too many input files.\n");
  622. return 1;
  623. }
  624. input = fopen(optarg, "r");
  625. if (!input) {
  626. cli_error("uci: %s", strerror(errno));
  627. return 1;
  628. }
  629. break;
  630. case 'm':
  631. flags |= CLI_FLAG_MERGE;
  632. break;
  633. case 's':
  634. ctx->flags |= UCI_FLAG_STRICT;
  635. break;
  636. case 'S':
  637. ctx->flags &= ~UCI_FLAG_STRICT;
  638. ctx->flags |= UCI_FLAG_PERROR;
  639. break;
  640. case 'n':
  641. ctx->flags |= UCI_FLAG_EXPORT_NAME;
  642. break;
  643. case 'N':
  644. ctx->flags &= ~UCI_FLAG_EXPORT_NAME;
  645. break;
  646. case 'p':
  647. uci_add_delta_path(ctx, optarg);
  648. break;
  649. case 'P':
  650. uci_set_savedir(ctx, optarg);
  651. flags |= CLI_FLAG_NOCOMMIT;
  652. break;
  653. case 'q':
  654. flags |= CLI_FLAG_QUIET;
  655. break;
  656. case 't':
  657. uci_set_savedir(ctx, optarg);
  658. break;
  659. case 'X':
  660. flags &= ~CLI_FLAG_SHOW_EXT;
  661. break;
  662. default:
  663. uci_usage();
  664. return 0;
  665. }
  666. }
  667. if (optind > 1)
  668. argv[optind - 1] = argv[0];
  669. argv += optind - 1;
  670. argc -= optind - 1;
  671. if (argc < 2) {
  672. uci_usage();
  673. return 0;
  674. }
  675. ret = uci_cmd(argc - 1, argv + 1);
  676. if (input != stdin)
  677. fclose(input);
  678. if (ret == 255)
  679. uci_usage();
  680. uci_free_context(ctx);
  681. return ret;
  682. }