summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2017-12-31 11:08:22 +0100
committerJo-Philipp Wich <jo@mein.io>2018-01-01 16:55:25 +0100
commit1e17f24644663c3f875c4248c135667f287900d8 (patch)
tree47a03f7fa60ff67534b7151721c05c9e239c23a8
parente1bf4356e1b513e158e1b5049147087ed6342cfd (diff)
downloaduci-1e17f24644663c3f875c4248c135667f287900d8.tar.gz
lua: support extended section notation
Wrap uci_lookup_ptr() to enable extended section format lookups when the passed section literal is in "@" notation. This allows calls in the form `cursor:get("conf", "@stype[0]", "opt")` which is useful to resolve options in numerically indexed anonymous sections. Signed-off-by: Jo-Philipp Wich <jo@mein.io> (cherry picked from commit ff33bb219bc409ce3071734a1c28d9165392d6ea)
-rw-r--r--lua/uci.c43
1 files changed, 36 insertions, 7 deletions
diff --git a/lua/uci.c b/lua/uci.c
index 47d59c8..54892b1 100644
--- a/lua/uci.c
+++ b/lua/uci.c
@@ -123,6 +123,35 @@ done:
}
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)
{
char *s = NULL;
@@ -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:
@@ -354,7 +383,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;
@@ -491,7 +520,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 +571,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;
@@ -605,7 +634,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 +705,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);