+static int
+lookup_args(lua_State *L, struct uci_ptr *ptr, char **buf)
+{
+ char *s;
+ int n;
+
+ n = lua_gettop(L);
+ luaL_checkstring(L, 1);
+ s = strdup(lua_tostring(L, 1));
+ if (!s)
+ goto error;
+
+ memset(ptr, 0, sizeof(struct uci_ptr));
+ if (!find_package(L, s, AUTOLOAD_ON))
+ goto error;
+
+ switch (n) {
+ case 3:
+ ptr->option = luaL_checkstring(L, 3);
+ /* fall through */
+ case 2:
+ ptr->section = luaL_checkstring(L, 2);
+ ptr->package = luaL_checkstring(L, 1);
+ 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:
+ goto error;
+ }
+
+ *buf = s;
+ return 0;
+
+error:
+ if (s)
+ free(s);
+ return 1;
+}
+