From 87e893157980abef163575ad7aedbcc96c41b0c6 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Fri, 22 Aug 2008 17:31:06 +0200 Subject: [PATCH] remove uci_parse_tuple, preparation for adding a new api for lookups --- Makefile | 2 +- history.c | 67 ++++++++++++++++++++++++++++--------------------------- list.c | 40 ++++++++++++++++----------------- uci.h | 62 +++++++++++++++++++++++++++++++------------------- util.c | 54 +++++++++++++++++++++----------------------- 5 files changed, 119 insertions(+), 106 deletions(-) diff --git a/Makefile b/Makefile index ea7d5c7..d16a987 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=0.5 +VERSION=0.6 # optional features PLUGIN_SUPPORT=1 diff --git a/history.c b/history.c index 2b6dbef..4e97524 100644 --- a/history.c +++ b/history.c @@ -88,7 +88,7 @@ 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, int *cmd) +static inline int uci_parse_history_tuple(struct uci_context *ctx, char **buf, struct uci_ptr *ptr) { int c = UCI_CMD_CHANGE; @@ -111,53 +111,57 @@ static inline void uci_parse_history_tuple(struct uci_context *ctx, char **buf, if (c != UCI_CMD_CHANGE) *buf += 1; - if (cmd) - *cmd = c; + UCI_INTERNAL(uci_parse_ptr, ctx, ptr, *buf); - UCI_INTERNAL(uci_parse_tuple, ctx, *buf, package, section, option, value); - if (!*section[0]) - UCI_THROW(ctx, UCI_ERR_PARSE); + if (!ptr->section) + goto error; + if (ptr->flags & UCI_LOOKUP_EXTENDED) + goto error; + + switch(c) { + case UCI_CMD_RENAME: + if (!ptr->value || !uci_validate_name(ptr->value)) + goto error; + break; + case UCI_CMD_LIST_ADD: + if (!ptr->option) + goto error; + } + return c; + +error: + UCI_THROW(ctx, UCI_ERR_INVAL); + return 0; } static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf) { struct uci_element *e = NULL; - bool delete = false; - bool rename = false; - char *package = NULL; - char *section = NULL; - char *option = NULL; - char *value = NULL; + struct uci_ptr ptr; int cmd; - uci_parse_history_tuple(ctx, &buf, &package, §ion, &option, &value, &cmd); - 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 && !uci_validate_str(value, (option || delete))) + cmd = uci_parse_history_tuple(ctx, &buf, &ptr); + if (strcmp(ptr.package, p->e.name) != 0) goto error; if (ctx->flags & UCI_FLAG_SAVED_HISTORY) - uci_add_history(ctx, &p->saved_history, cmd, section, option, value); + uci_add_history(ctx, &p->saved_history, cmd, ptr.section, ptr.option, ptr.value); switch(cmd) { case UCI_CMD_RENAME: - UCI_INTERNAL(uci_rename, ctx, p, section, option, value); + UCI_INTERNAL(uci_rename, ctx, p, ptr.section, ptr.option, ptr.value); break; case UCI_CMD_REMOVE: - UCI_INTERNAL(uci_delete, ctx, p, section, option); + UCI_INTERNAL(uci_delete, ctx, p, ptr.section, ptr.option); break; case UCI_CMD_LIST_ADD: - UCI_INTERNAL(uci_add_list, ctx, p, section, option, value, NULL); + UCI_INTERNAL(uci_add_list, ctx, p, ptr.section, ptr.option, ptr.value, NULL); break; case UCI_CMD_ADD: case UCI_CMD_CHANGE: - UCI_INTERNAL(uci_set, ctx, p, section, option, value, &e); - if (!option && e && (cmd == UCI_CMD_ADD)) + UCI_INTERNAL(uci_set, ctx, p, ptr.section, ptr.option, ptr.value, &e); + if (!ptr.option && e && (cmd == UCI_CMD_ADD)) uci_to_section(e)->anonymous = true; break; } @@ -260,10 +264,7 @@ static void uci_filter_history(struct uci_context *ctx, const char *name, const struct uci_element *e, *tmp; struct uci_list list; char *filename = NULL; - char *p = NULL; - char *s = NULL; - char *o = NULL; - char *v = NULL; + struct uci_ptr ptr; FILE *f = NULL; uci_list_init(&list); @@ -291,13 +292,13 @@ static void uci_filter_history(struct uci_context *ctx, const char *name, const 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); + uci_parse_history_tuple(ctx, &buf, &ptr); if (section) { - if (!s || (strcmp(section, s) != 0)) + if (!ptr.section || (strcmp(section, ptr.section) != 0)) continue; } if (option) { - if (!o || (strcmp(option, o) != 0)) + if (!ptr.option || (strcmp(option, ptr.option) != 0)) continue; } /* match, drop this element again */ diff --git a/list.c b/list.c index 2331c7e..d7b9f69 100644 --- a/list.c +++ b/list.c @@ -264,27 +264,25 @@ static struct uci_element *uci_lookup_list(struct uci_list *list, const char *na return NULL; } -int uci_lookup_ext(struct uci_context *ctx, struct uci_element **res, char *ptr) +int uci_lookup_ext(struct uci_context *ctx, struct uci_element **res, char *str) { struct uci_package *p = NULL; struct uci_element *e; struct uci_section *s; - char *package = NULL; - char *section = NULL; - char *option = NULL; + struct uci_ptr ptr; char *idxstr, *t; int idx, c; UCI_HANDLE_ERR(ctx); UCI_ASSERT(ctx, res != NULL); - UCI_ASSERT(ctx, ptr != NULL); + UCI_ASSERT(ctx, str != NULL); - UCI_INTERNAL(uci_parse_tuple, ctx, ptr, &package, §ion, &option, NULL); + UCI_INTERNAL(uci_parse_ptr, ctx, &ptr, str); /* look up the package first */ - e = uci_lookup_list(&ctx->root, package); + e = uci_lookup_list(&ctx->root, ptr.package); if (!e) { - UCI_INTERNAL(uci_load, ctx, package, &p); + UCI_INTERNAL(uci_load, ctx, ptr.package, &p); if (!p) goto notfound; e = &p->e; @@ -292,25 +290,25 @@ int uci_lookup_ext(struct uci_context *ctx, struct uci_element **res, char *ptr) p = uci_to_package(e); } - if (!section) + if (!ptr.section) goto done; /* if the section name validates as a regular name, pass through * to the regular uci_lookup function call */ - if (!*section || uci_validate_name(section)) { - UCI_INTERNAL(uci_lookup, ctx, &e, p, section, option); + if (!(ptr.flags & UCI_LOOKUP_EXTENDED)) { + UCI_INTERNAL(uci_lookup, ctx, &e, p, ptr.section, ptr.option); goto done; } /* name did not validate, that means we have an extended lookup call * parse it here. for now only the section index syntax is supported */ - if (section[0] != '@') + if (ptr.section[0] != '@') goto error; - section++; + ptr.section++; /* parse the section index part */ - idxstr = strchr(section, '['); + idxstr = strchr(ptr.section, '['); if (!idxstr) goto error; *idxstr = 0; @@ -328,9 +326,9 @@ int uci_lookup_ext(struct uci_context *ctx, struct uci_element **res, char *ptr) if (t && *t) goto error; - if (!*section) - section = NULL; - if (section && !uci_validate_str(section, false)) + if (!*ptr.section) + ptr.section = NULL; + if (ptr.section && !uci_validate_str(ptr.section, false)) goto error; /* if the given index is negative, it specifies the section number from @@ -339,7 +337,7 @@ int uci_lookup_ext(struct uci_context *ctx, struct uci_element **res, char *ptr) c = 0; uci_foreach_element(&p->sections, e) { s = uci_to_section(e); - if (section && (strcmp(s->type, section) != 0)) + if (ptr.section && (strcmp(s->type, ptr.section) != 0)) continue; c++; @@ -350,7 +348,7 @@ int uci_lookup_ext(struct uci_context *ctx, struct uci_element **res, char *ptr) c = 0; uci_foreach_element(&p->sections, e) { s = uci_to_section(e); - if (section && (strcmp(s->type, section) != 0)) + if (ptr.section && (strcmp(s->type, ptr.section) != 0)) continue; if (idx == c) @@ -360,8 +358,8 @@ int uci_lookup_ext(struct uci_context *ctx, struct uci_element **res, char *ptr) goto notfound; found: - if (option) - e = uci_lookup_list(&s->options, option); + if (ptr.option) + e = uci_lookup_list(&s->options, ptr.option); done: *res = e; return 0; diff --git a/uci.h b/uci.h index 198ae2a..fe05bb3 100644 --- a/uci.h +++ b/uci.h @@ -55,6 +55,7 @@ struct uci_list struct uci_list *prev; }; +struct uci_ptr; struct uci_element; struct uci_package; struct uci_section; @@ -65,19 +66,6 @@ struct uci_backend; struct uci_parse_context; -/** - * uci_parse_tuple: Parse an uci tuple - * @ctx: uci context - * @str: input string - * @package: output package pointer - * @section: output section pointer - * @option: output option pointer - * @value: output value pointer - * - * format: [.
[.