From: Jo-Philipp Wich Date: Sun, 31 Dec 2017 10:08:22 +0000 (+0100) Subject: lua: support extended section notation X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=commitdiff_plain;h=ff33bb219bc409ce3071734a1c28d9165392d6ea 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 --- diff --git a/lua/uci.c b/lua/uci.c index 22055cb..f70ca50 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);