-VERSION=0.5
+VERSION=0.6
# optional features
PLUGIN_SUPPORT=1
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;
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;
}
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);
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 */
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;
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;
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
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++;
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)
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;
struct uci_list *prev;
};
+struct uci_ptr;
struct uci_element;
struct uci_package;
struct uci_section;
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
*/
*/
extern int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *package, const char *section, const char *option);
+/**
+ * uci_lookup_ptr: Split an uci tuple string and look up elements
+ * @ctx: uci context
+ * @ptr: lookup result struct
+ * @str: uci tuple string to look up
+ * @extended: allow extended syntax lookup
+ */
+extern int uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended);
+
/**
* uci_lookup_ext: Extended lookup for an uci element
*
* @res: where to store the result
* @ptr: uci pointer tuple
*
- * Looks up an element using the extended syntax, which is a superset of what
- * uci_parse_tuple accepts. It can look up sections by an index with an optional
- * type.
+ * Looks up an element using the extended syntax.
+ * It can look up sections by an index with an optional type.
*
* Examples:
* network.@interface[0].ifname ('ifname' option of the first interface section)
/* UCI data structures */
enum uci_type {
- UCI_TYPE_HISTORY = 0,
- UCI_TYPE_PACKAGE = 1,
- UCI_TYPE_SECTION = 2,
- UCI_TYPE_OPTION = 3,
- UCI_TYPE_PATH = 4,
- UCI_TYPE_BACKEND = 5,
- UCI_TYPE_ITEM = 6,
+ UCI_TYPE_UNSPEC = 0,
+ UCI_TYPE_HISTORY = 1,
+ UCI_TYPE_PACKAGE = 2,
+ UCI_TYPE_SECTION = 3,
+ UCI_TYPE_OPTION = 4,
+ UCI_TYPE_PATH = 5,
+ UCI_TYPE_BACKEND = 6,
+ UCI_TYPE_ITEM = 7,
};
enum uci_option_type {
char *value;
};
+struct uci_ptr
+{
+ enum uci_type target;
+ enum {
+ UCI_LOOKUP_DONE = (1 << 0),
+ UCI_LOOKUP_COMPLETE = (1 << 1),
+ UCI_LOOKUP_EXTENDED = (1 << 2),
+ } flags;
+
+ struct uci_package *p;
+ struct uci_section *s;
+ struct uci_option *o;
+
+ char *package;
+ char *section;
+ char *option;
+ char *value;
+};
+
/* linked list handling */
#ifndef offsetof
ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
}
-int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value)
+int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
{
char *last = NULL;
- bool internal = ctx->internal;
UCI_HANDLE_ERR(ctx);
- UCI_ASSERT(ctx, str && package && section && option);
+ UCI_ASSERT(ctx, str);
+ UCI_ASSERT(ctx, ptr);
+ memset(ptr, 0, sizeof(struct uci_ptr));
+
+ /* value */
last = strchr(str, '=');
if (last) {
*last = 0;
last++;
+ ptr->value = last;
}
- *package = strsep(&str, ".");
- if (!*package || !uci_validate_str(*package, false))
+ ptr->package = strsep(&str, ".");
+ if (!ptr->package)
goto error;
- *section = strsep(&str, ".");
- *option = NULL;
- if (value)
- *value = NULL;
- if (!*section)
+ ptr->section = strsep(&str, ".");
+ if (!ptr->section) {
+ ptr->target = UCI_TYPE_PACKAGE;
goto lastval;
+ }
- *option = strsep(&str, ".");
- if (!*option)
+ ptr->option = strsep(&str, ".");
+ if (!ptr->option) {
+ ptr->target = UCI_TYPE_SECTION;
goto lastval;
-
-lastval:
- if (last) {
- if (!value)
- goto error;
-
- if (!*last)
- goto error;
- *value = last;
+ } else {
+ ptr->target = UCI_TYPE_OPTION;
}
- if (*section && *section[0] && !internal && !uci_validate_name(*section))
+lastval:
+ if (ptr->package && !uci_validate_str(ptr->package, false))
goto error;
- if (*option && !uci_validate_name(*option))
+ if (ptr->section && !uci_validate_name(ptr->section))
+ ptr->flags |= UCI_LOOKUP_EXTENDED;
+ if (ptr->option && !uci_validate_name(ptr->option))
goto error;
- if (value && *value && !uci_validate_text(*value))
+ if (ptr->value && !uci_validate_text(ptr->value))
goto error;
- goto done;
+ return 0;
error:
+ memset(ptr, 0, sizeof(struct uci_ptr));
UCI_THROW(ctx, UCI_ERR_PARSE);
-
-done:
- return 0;
}