- lua_getfield(L, LUA_GLOBALSINDEX, "uci");
- lua_getfield(L, -1, "warn");
- if (!lua_isboolean(L, -1))
- goto done;
- if (lua_toboolean(L, -1) != 1)
- goto done;
- uci_perror(ctx, name);
-done:
- lua_pop(L, 2);
+ char *s = NULL;
+ int n;
+
+ n = lua_gettop(L);
+ luaL_checkstring(L, 1 + offset);
+ s = strdup(lua_tostring(L, 1 + offset));
+ if (!s)
+ goto error;
+
+ memset(ptr, 0, sizeof(struct uci_ptr));
+ if (!find_package(L, ctx, s, true))
+ goto error;
+
+ switch (n - offset) {
+ case 4:
+ case 3:
+ ptr->option = luaL_checkstring(L, 3 + offset);
+ /* fall through */
+ case 2:
+ ptr->section = luaL_checkstring(L, 2 + offset);
+ ptr->package = luaL_checkstring(L, 1 + offset);
+ if (uci_lookup_ptr(ctx, ptr, NULL, false) != UCI_OK)
+ goto error;
+ break;
+ case 1:
+ if (uci_lookup_ptr(ctx, ptr, s, false) != UCI_OK)
+ goto error;
+ break;
+ default:
+ luaL_error(L, "invalid argument count");
+ goto error;
+ }
+
+ *buf = s;
+ return 0;
+
+error:
+ if (s)
+ free(s);
+ return 1;
+}
+
+static int
+uci_push_status(lua_State *L, struct uci_context *ctx, bool hasarg)
+{
+ char *str = NULL;
+
+ if (!hasarg)
+ lua_pushboolean(L, (ctx->err == UCI_OK));
+ if (ctx->err) {
+ uci_get_errorstr(ctx, &str, MODNAME);
+ if (str) {
+ lua_pushstring(L, str);
+ free(str);
+ return 2;
+ }
+ }
+ return 1;