X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=util.c;h=28cc5df2250c4b7b7d51350b45f6e98b293e52c7;hp=c9be29333239744a3d24dbe00bb2a1e2188c69f2;hb=4094cedb83a0b92a179af13d1781a8fd7d3d943e;hpb=171170de64e0faff8d0e93581d9971439f474cbd diff --git a/util.c b/util.c index c9be293..28cc5df 100644 --- a/util.c +++ b/util.c @@ -16,6 +16,8 @@ * This file contains wrappers to standard functions, which * throw exceptions upon failure. */ +#include +#include static void *uci_malloc(struct uci_context *ctx, size_t size) { @@ -49,4 +51,66 @@ static char *uci_strdup(struct uci_context *ctx, const char *str) return ptr; } +static bool uci_validate_name(char *str) +{ + if (!*str) + return false; + + while (*str) { + if (!isalnum(*str) && (*str != '_')) + return false; + str++; + } + return true; +} + +int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value) +{ + char *last = NULL; + + UCI_HANDLE_ERR(ctx); + UCI_ASSERT(ctx, str && package && section && option); + + *package = strtok(str, "."); + if (!*package || !uci_validate_name(*package)) + goto error; + + last = *package; + *section = strtok(NULL, "."); + if (!*section) + goto lastval; + + last = *section; + *option = strtok(NULL, "."); + if (!*option) + goto lastval; + + last = *option; + +lastval: + last = strchr(last, '='); + if (last) { + if (!value) + goto error; + + *last = 0; + last++; + if (!*last) + goto error; + *value = last; + } + + if (*section && !uci_validate_name(*section)) + goto error; + if (*option && !uci_validate_name(*option)) + goto error; + + goto done; + +error: + UCI_THROW(ctx, UCI_ERR_PARSE); + +done: + return 0; +}