iron out all extra compiler warnings
[oweals/uci.git] / lua / uci.c
index 22055cb7e367e3db3f459a7604de4c7f83559baf..f4dce89b7c9f2703840da7f63ab9905d2d58bddc 100644 (file)
--- a/lua/uci.c
+++ b/lua/uci.c
@@ -122,6 +122,35 @@ done:
        return p;
 }
 
+static int
+lookup_extended(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
+{
+       int rv;
+       struct uci_ptr lookup;
+
+       /* use a copy of the passed ptr since failing lookups will
+        * clobber the state */
+       lookup = *ptr;
+       lookup.flags |= UCI_LOOKUP_EXTENDED;
+
+       rv = uci_lookup_ptr(ctx, &lookup, str, extended);
+
+       /* copy to passed ptr on success */
+       if (!rv)
+               *ptr = lookup;
+
+       return rv;
+}
+
+static int
+lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
+{
+       if (ptr && !ptr->s && ptr->section && *ptr->section == '@')
+               return lookup_extended(ctx, ptr, str, extended);
+
+       return uci_lookup_ptr(ctx, ptr, str, extended);
+}
+
 static int
 lookup_args(lua_State *L, struct uci_context *ctx, int offset, struct uci_ptr *ptr, char **buf)
 {
@@ -146,11 +175,11 @@ lookup_args(lua_State *L, struct uci_context *ctx, int offset, struct uci_ptr *p
        case 2:
                ptr->section = luaL_checkstring(L, 2 + offset);
                ptr->package = luaL_checkstring(L, 1 + offset);
-               if (uci_lookup_ptr(ctx, ptr, NULL, true) != UCI_OK)
+               if (lookup_ptr(ctx, ptr, NULL, true) != UCI_OK)
                        goto error;
                break;
        case 1:
-               if (uci_lookup_ptr(ctx, ptr, s, true) != UCI_OK)
+               if (lookup_ptr(ctx, ptr, s, true) != UCI_OK)
                        goto error;
                break;
        default:
@@ -346,6 +375,7 @@ uci_lua_get_any(lua_State *L, bool all)
        struct uci_element *e = NULL;
        struct uci_ptr ptr;
        int offset = 0;
+       int nret = 1;
        char *s = NULL;
        int err = UCI_ERR_NOTFOUND;
 
@@ -354,7 +384,7 @@ uci_lua_get_any(lua_State *L, bool all)
        if (lookup_args(L, ctx, offset, &ptr, &s))
                goto error;
 
-       uci_lookup_ptr(ctx, &ptr, NULL, true);
+       lookup_ptr(ctx, &ptr, NULL, true);
        if (!all && !ptr.s) {
                err = UCI_ERR_INVAL;
                goto error;
@@ -371,10 +401,14 @@ uci_lua_get_any(lua_State *L, bool all)
                        uci_push_package(L, ptr.p);
                        break;
                case UCI_TYPE_SECTION:
-                       if (all)
+                       if (all) {
                                uci_push_section(L, ptr.s, -1);
-                       else
+                       }
+                       else {
                                lua_pushstring(L, ptr.s->type);
+                               lua_pushstring(L, ptr.s->e.name);
+                               nret++;
+                       }
                        break;
                case UCI_TYPE_OPTION:
                        uci_push_option(L, ptr.o);
@@ -386,7 +420,7 @@ uci_lua_get_any(lua_State *L, bool all)
        if (s)
                free(s);
        if (!err)
-               return 1;
+               return nret;
 
 error:
        if (s)
@@ -491,7 +525,7 @@ uci_lua_rename(lua_State *L)
                goto error;
        }
 
-       err = uci_lookup_ptr(ctx, &ptr, NULL, true);
+       err = lookup_ptr(ctx, &ptr, NULL, true);
        if (err)
                goto error;
 
@@ -542,7 +576,7 @@ uci_lua_reorder(lua_State *L)
                goto error;
        }
 
-       err = uci_lookup_ptr(ctx, &ptr, NULL, true);
+       err = lookup_ptr(ctx, &ptr, NULL, true);
        if (err)
                goto error;
 
@@ -571,7 +605,8 @@ uci_lua_set(lua_State *L)
        int err = UCI_ERR_MEM;
        char *s = NULL;
        const char *v;
-       int i, nargs, offset = 0;
+       unsigned int i;
+       int nargs, offset = 0;
 
        ctx = find_context(L, &offset);
        nargs = lua_gettop(L);
@@ -605,7 +640,7 @@ uci_lua_set(lua_State *L)
                goto error;
        }
 
-       err = uci_lookup_ptr(ctx, &ptr, NULL, true);
+       err = lookup_ptr(ctx, &ptr, NULL, true);
        if (err)
                goto error;
 
@@ -676,7 +711,7 @@ uci_lua_package_cmd(lua_State *L, enum pkg_cmd cmd)
        if (lookup_args(L, ctx, offset, &ptr, &s))
                goto err;
 
-       uci_lookup_ptr(ctx, &ptr, NULL, true);
+       lookup_ptr(ctx, &ptr, NULL, true);
 
        uci_foreach_element_safe(&ctx->root, tmp, e) {
                struct uci_package *p = uci_to_package(e);