improve the argument parser
[project/uci.git] / history.c
index 5b652b9..3a8b73e 100644 (file)
--- a/history.c
+++ b/history.c
@@ -52,6 +52,20 @@ int uci_add_history_path(struct uci_context *ctx, const char *dir)
        return 0;
 }
 
+static inline void uci_parse_history_tuple(struct uci_context *ctx, char **buf, char **package, char **section, char **option, char **value, bool *delete, bool *rename)
+{
+       if (**buf == '-') {
+               if (delete)
+                       *delete = true;
+               *buf += 1;
+       } else if (**buf == '@') {
+               if (rename)
+                       *rename = true;
+               *buf += 1;
+       }
+
+       UCI_INTERNAL(uci_parse_tuple, ctx, *buf, package, section, option, value);
+}
 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
 {
        bool delete = false;
@@ -61,22 +75,14 @@ static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *
        char *option = NULL;
        char *value = NULL;
 
-       if (buf[0] == '-') {
-               delete = true;
-               buf++;
-       } else if (buf[0] == '@') {
-               rename = true;
-               buf++;
-       }
-
-       UCI_INTERNAL(uci_parse_tuple, ctx, buf, &package, &section, &option, &value);
+       uci_parse_history_tuple(ctx, &buf, &package, &section, &option, &value, &delete, &rename);
        if (!package || (strcmp(package, p->e.name) != 0))
                goto error;
        if (!uci_validate_name(section))
                goto error;
        if (option && !uci_validate_name(option))
                goto error;
-       if ((rename || !delete) && !uci_validate_name(value))
+       if (rename && !uci_validate_str(value, (option || delete)))
                goto error;
 
        if (rename)
@@ -91,9 +97,11 @@ error:
        UCI_THROW(ctx, UCI_ERR_PARSE);
 }
 
-static void uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
+/* returns the number of changes that were successfully parsed */
+static int uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_package *p)
 {
        struct uci_parse_context *pctx;
+       int changes = 0;
 
        /* make sure no memory from previous parse attempts is leaked */
        ctx->internal = true;
@@ -115,6 +123,7 @@ static void uci_parse_history(struct uci_context *ctx, FILE *stream, struct uci_
                UCI_TRAP_SAVE(ctx, error);
                uci_parse_history_line(ctx, p, pctx->buf);
                UCI_TRAP_RESTORE(ctx);
+               changes++;
 error:
                continue;
        }
@@ -122,32 +131,38 @@ error:
        /* no error happened, we can get rid of the parser context now */
        ctx->internal = true;
        uci_cleanup(ctx);
+       return changes;
 }
 
-static void uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
+/* returns the number of changes that were successfully parsed */
+static int uci_load_history_file(struct uci_context *ctx, struct uci_package *p, char *filename, FILE **f, bool flush)
 {
        FILE *stream = NULL;
+       int changes = 0;
 
        UCI_TRAP_SAVE(ctx, done);
        stream = uci_open_stream(ctx, filename, SEEK_SET, flush, false);
        if (p)
-               uci_parse_history(ctx, stream, p);
+               changes = uci_parse_history(ctx, stream, p);
        UCI_TRAP_RESTORE(ctx);
 done:
        if (f)
                *f = stream;
        else if (stream)
                uci_close_stream(stream);
+       return changes;
 }
 
-static void uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
+/* returns the number of changes that were successfully parsed */
+static int uci_load_history(struct uci_context *ctx, struct uci_package *p, bool flush)
 {
        struct uci_element *e;
        char *filename = NULL;
        FILE *f = NULL;
+       int changes = 0;
 
        if (!p->confdir)
-               return;
+               return 0;
 
        uci_foreach_element(&ctx->history_path, e) {
                if ((asprintf(&filename, "%s/%s", e->name, p->e.name) < 0) || !filename)
@@ -160,8 +175,8 @@ static void uci_load_history(struct uci_context *ctx, struct uci_package *p, boo
        if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
                UCI_THROW(ctx, UCI_ERR_MEM);
 
-       uci_load_history_file(ctx, p, filename, &f, flush);
-       if (flush && f) {
+       changes = uci_load_history_file(ctx, p, filename, &f, flush);
+       if (flush && f && (changes > 0)) {
                rewind(f);
                ftruncate(fileno(f), 0);
        }
@@ -169,6 +184,115 @@ static void uci_load_history(struct uci_context *ctx, struct uci_package *p, boo
                free(filename);
        uci_close_stream(f);
        ctx->errno = 0;
+       return changes;
+}
+
+static void uci_filter_history(struct uci_context *ctx, const char *name, char *section, char *option)
+{
+       struct uci_parse_context *pctx;
+       struct uci_element *e, *tmp;
+       struct uci_list list;
+       char *filename = NULL;
+       char *p = NULL;
+       char *s = NULL;
+       char *o = NULL;
+       char *v = NULL;
+       FILE *f = NULL;
+
+       uci_list_init(&list);
+       uci_alloc_parse_context(ctx);
+       pctx = ctx->pctx;
+
+       if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
+               UCI_THROW(ctx, UCI_ERR_MEM);
+
+       UCI_TRAP_SAVE(ctx, done);
+       f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
+       pctx->file = f;
+       while (!feof(f)) {
+               struct uci_element *e;
+               char *buf;
+
+               uci_getln(ctx, 0);
+               buf = pctx->buf;
+               if (!buf[0])
+                       continue;
+
+               /* NB: need to allocate the element before the call to 
+                * uci_parse_history_tuple, otherwise the original string 
+                * gets modified before it is saved */
+               e = uci_alloc_generic(ctx, UCI_TYPE_HISTORY, pctx->buf, sizeof(struct uci_element));
+               uci_list_add(&list, &e->list);
+
+               uci_parse_history_tuple(ctx, &buf, &p, &s, &o, &v, NULL, NULL);
+               if (section) {
+                       if (!s || (strcmp(section, s) != 0))
+                               continue;
+               }
+               if (option) {
+                       if (!o || (strcmp(option, o) != 0))
+                               continue;
+               }
+               /* match, drop this element again */
+               uci_free_element(e);
+       }
+
+       /* rebuild the history file */
+       rewind(f);
+       ftruncate(fileno(f), 0);
+       uci_foreach_element_safe(&list, tmp, e) {
+               fprintf(f, "%s\n", e->name);
+               uci_free_element(e);
+       }
+       UCI_TRAP_RESTORE(ctx);
+
+done:
+       if (filename)
+               free(filename);
+       uci_close_stream(f);
+       uci_foreach_element_safe(&list, tmp, e) {
+               uci_free_element(e);
+       }
+       ctx->internal = true;
+       uci_cleanup(ctx);
+}
+
+int uci_revert(struct uci_context *ctx, struct uci_package **pkg, char *section, char *option)
+{
+       struct uci_package *p;
+       char *name = NULL;
+
+       UCI_HANDLE_ERR(ctx);
+       UCI_ASSERT(ctx, pkg != NULL);
+       p = *pkg;
+       UCI_ASSERT(ctx, p != NULL);
+       UCI_ASSERT(ctx, p->confdir);
+
+       /* 
+        * - flush unwritten changes
+        * - save the package name
+        * - unload the package
+        * - filter the history
+        * - reload the package
+        */
+       UCI_TRAP_SAVE(ctx, error);
+       UCI_INTERNAL(uci_save, ctx, p);
+       name = uci_strdup(ctx, p->e.name);
+
+       *pkg = NULL;
+       uci_free_package(&p);
+       uci_filter_history(ctx, name, section, option);
+
+       UCI_INTERNAL(uci_load, ctx, name, &p);
+       UCI_TRAP_RESTORE(ctx);
+       ctx->errno = 0;
+
+error:
+       if (name)
+               free(name);
+       if (ctx->errno)
+               UCI_THROW(ctx, ctx->errno);
+       return 0;
 }
 
 int uci_save(struct uci_context *ctx, struct uci_package *p)