+static int uci_batch_cmd(void)
+{
+ char *argv[MAX_ARGS];
+ char *str = NULL;
+ int ret = 0;
+ int i, j;
+
+ for(i = 0; i <= MAX_ARGS; i++) {
+ if (i == MAX_ARGS) {
+ fprintf(stderr, "Too many arguments\n");
+ return 1;
+ }
+ argv[i] = NULL;
+ if ((ret = uci_parse_argument(ctx, input, &str, &argv[i])) != UCI_OK) {
+ cli_perror();
+ i = 0;
+ break;
+ }
+ if (!argv[i][0])
+ break;
+ argv[i] = strdup(argv[i]);
+ if (!argv[i]) {
+ perror("uci");
+ return 1;
+ }
+ }
+ argv[i] = NULL;
+
+ if (i > 0) {
+ if (!strcasecmp(argv[0], "exit"))
+ return 254;
+ ret = uci_cmd(i, argv);
+ } else
+ return 0;
+
+ for (j = 0; j < i; j++) {
+ if (argv[j])
+ free(argv[j]);
+ }
+
+ return ret;
+}
+
+static int uci_batch(void)
+{
+ int ret = 0;
+
+ while (!feof(input)) {
+ struct uci_element *e, *tmp;
+
+ ret = uci_batch_cmd();
+ if (ret == 254)
+ return 0;
+ else if (ret == 255)
+ fprintf(stderr, "Unknown command\n");
+
+ /* clean up */
+ uci_foreach_element_safe(&ctx->root, tmp, e) {
+ uci_unload(ctx, uci_to_package(e));
+ }
+ }
+ return 0;
+}
+