From eadddde8591e5ab6a8d8dff349affc53d1d4021b Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Tue, 29 Jan 2008 18:55:31 +0100 Subject: [PATCH] cleanup, move parse_tuple to libuci, add some input validation --- cli.c | 37 ++-------------------------------- file.c | 11 +++++----- uci.h | 14 +++++++++++++ util.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 41 deletions(-) diff --git a/cli.c b/cli.c index 1fa1cb1..410d7e8 100644 --- a/cli.c +++ b/cli.c @@ -112,37 +112,6 @@ static int uci_do_export(int argc, char **argv) return 0; } -static void parse_tuple(char *str, char **package, char **section, char **option, char **value) -{ - char *last = NULL; - - *package = strtok(str, "."); - if (!*package) - goto done; - - last = *package; - *section = strtok(NULL, "."); - if (!*section) - goto done; - - last = *section; - *option = strtok(NULL, "."); - if (!*option) - goto done; - - last = *option; -done: - if (!value) - return; - - last = strtok(last, "="); - if (!last) - return; - - *value = last + strlen(last) + 1; -} - - static int uci_do_get(int argc, char **argv) { char *package = NULL; @@ -155,8 +124,7 @@ static int uci_do_get(int argc, char **argv) if (argc != 2) return 255; - parse_tuple(argv[1], &package, §ion, &option, NULL); - if (!package) + if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, NULL) != UCI_OK) return 1; if (uci_load(ctx, package, &p) != UCI_OK) { @@ -196,8 +164,7 @@ static int uci_do_set(int argc, char **argv) if (argc != 2) return 255; - parse_tuple(argv[1], &package, §ion, &option, &value); - if (!package) + if (uci_parse_tuple(ctx, argv[1], &package, §ion, &option, &value) != UCI_OK) return 1; if (uci_load(ctx, package, &p) != UCI_OK) { diff --git a/file.c b/file.c index 9a2e353..a78c5a7 100644 --- a/file.c +++ b/file.c @@ -470,15 +470,14 @@ int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *packag UCI_HANDLE_ERR(ctx); UCI_ASSERT(ctx, stream != NULL); - if (package) { + if (package) uci_export_package(package, stream, header); - goto done; + else { + uci_foreach_element(&ctx->root, e) { + uci_export_package(uci_to_package(e), stream, header); + } } - uci_foreach_element(&ctx->root, e) { - uci_export_package(uci_to_package(e), stream, header); - } -done: return 0; } diff --git a/uci.h b/uci.h index c7251ab..0cb352d 100644 --- a/uci.h +++ b/uci.h @@ -60,9 +60,23 @@ struct uci_package; struct uci_section; struct uci_option; struct uci_history; +struct uci_context; 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: [.
[.