From 1e17f24644663c3f875c4248c135667f287900d8 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sun, 31 Dec 2017 11:08:22 +0100 Subject: [PATCH] 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 (cherry picked from commit ff33bb219bc409ce3071734a1c28d9165392d6ea) --- lua/uci.c | 43 ++++++++++++++++++++++++++++++++++++------- 1 file 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 @@ -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: @@ -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); -- 2.25.1