extend uci_set so that it can store the resulting element in a variable
[project/uci.git] / list.c
diff --git a/list.c b/list.c
index e817118..c6b7ebc 100644 (file)
--- a/list.c
+++ b/list.c
@@ -218,6 +218,8 @@ int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_pac
        if (option) {
                s = uci_to_section(e);
                e = uci_lookup_list(ctx, &s->options, option);
+               if (!e)
+                       goto notfound;
        }
 
        *res = e;
@@ -384,7 +386,7 @@ int uci_delete(struct uci_context *ctx, struct uci_package *p, char *section, ch
        return uci_del_element(ctx, e);
 }
 
-int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value)
+int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char *option, char *value, struct uci_element **result)
 {
        /* NB: UCI_INTERNAL use means without history tracking */
        bool internal = ctx->internal;
@@ -430,9 +432,13 @@ int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char
                e = &o->e;
        else
                e = &s->e;
+       if (result)
+               *result = e;
+       else
+               result = &e;
 
        ctx->internal = internal;
-       return uci_set_element_value(ctx, &e, value);
+       return uci_set_element_value(ctx, result, value);
 
 notfound:
        /* 
@@ -447,10 +453,14 @@ notfound:
        /* now add the missing entry */
        if (!internal && p->confdir)
                uci_add_history(ctx, &p->history, UCI_CMD_ADD, section, option, value);
-       if (s)
-               uci_alloc_option(s, option, value);
-       else {
+       if (s) {
+               o = uci_alloc_option(s, option, value);
+               if (result)
+                       *result = &o->e;
+       } else {
                s = uci_alloc_section(p, value, section);
+               if (result)
+                       *result = &s->e;
                if (ctx->pctx && ctx->pctx->merge)
                        ctx->pctx->section = s;
        }