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;
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) {
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) {
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;
}
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: <package>[.<section>[.<option>]][=<value>]
+ */
+extern int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value);
+
/**
* uci_alloc_context: Allocate a new uci context
*/
* This file contains wrappers to standard functions, which
* throw exceptions upon failure.
*/
+#include <stdbool.h>
+#include <ctype.h>
static void *uci_malloc(struct uci_context *ctx, size_t size)
{
return ptr;
}
+static bool 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 || !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;
+ }
+
+ if (*section && !validate_name(*section))
+ goto error;
+ if (*option && !validate_name(*option))
+ goto error;
+
+ goto done;
+
+error:
+ UCI_THROW(ctx, UCI_ERR_PARSE);
+
+done:
+ return 0;
+}