+ uci_parse_option(ctx, false);
+ else
+ goto invalid;
+ break;
+ case 'l':
+ if ((word[1] == 0) || !strcmp(word + 1, "ist"))
+ uci_parse_option(ctx, true);
+ else
+ goto invalid;
+ break;
+ default:
+ goto invalid;
+ }
+ continue;
+invalid:
+ uci_parse_error(ctx, "invalid command");
+ } while (1);
+}
+
+/* max number of characters that escaping adds to the string */
+#define UCI_QUOTE_ESCAPE "'\\''"
+
+/*
+ * escape an uci string for export
+ */
+static const char *uci_escape(struct uci_context *ctx, const char *str)
+{
+ const char *end;
+ int ofs = 0;
+
+ if (!ctx->buf) {
+ ctx->bufsz = LINEBUF;
+ ctx->buf = malloc(LINEBUF);
+
+ if (!ctx->buf)
+ return str;
+ }
+
+ while (1) {
+ int len;
+
+ end = strchr(str, '\'');
+ if (!end)
+ end = str + strlen(str);
+ len = end - str;
+
+ /* make sure that we have enough room in the buffer */
+ while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
+ ctx->bufsz *= 2;
+ ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
+ }
+
+ /* copy the string until the character before the quote */
+ memcpy(&ctx->buf[ofs], str, len);
+ ofs += len;
+
+ /* end of string? return the buffer */
+ if (*end == 0)
+ break;
+
+ memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
+ ofs += strlen(&ctx->buf[ofs]);
+ str = end + 1;
+ }
+
+ ctx->buf[ofs] = 0;
+ return ctx->buf;
+}
+
+/*
+ * export a single config package to a file stream
+ */
+static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
+{
+ struct uci_context *ctx = p->ctx;
+ struct uci_element *s, *o, *i;
+
+ if (header)
+ fprintf(stream, "package %s\n", uci_escape(ctx, p->e.name));
+ uci_foreach_element(&p->sections, s) {
+ struct uci_section *sec = uci_to_section(s);
+ fprintf(stream, "\nconfig %s", uci_escape(ctx, sec->type));
+ if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
+ fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
+ fprintf(stream, "\n");
+ uci_foreach_element(&sec->options, o) {
+ struct uci_option *opt = uci_to_option(o);
+ switch(opt->type) {
+ case UCI_TYPE_STRING:
+ fprintf(stream, "\toption %s", uci_escape(ctx, opt->e.name));
+ fprintf(stream, " '%s'\n", uci_escape(ctx, opt->v.string));
+ break;
+ case UCI_TYPE_LIST:
+ uci_foreach_element(&opt->v.list, i) {
+ fprintf(stream, "\tlist %s", uci_escape(ctx, opt->e.name));
+ fprintf(stream, " '%s'\n", uci_escape(ctx, i->name));
+ }