+static int
+lookup_args(lua_State *L, struct uci_ptr *ptr, char **buf)
+{
+ char *s = NULL;
+ 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 4:
+ 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:
+ luaL_error(L, "invalid argument count");
+ goto error;
+ }
+
+ *buf = s;
+ return 0;
+
+error:
+ if (s)
+ free(s);
+ return 1;
+}
+
+static void uci_push_option(lua_State *L, struct uci_option *o)
+{
+ struct uci_element *e;
+ int i = 0;
+
+ switch(o->type) {
+ case UCI_TYPE_STRING:
+ lua_pushstring(L, o->v.string);
+ break;
+ case UCI_TYPE_LIST:
+ lua_newtable(L);
+ uci_foreach_element(&o->v.list, e) {
+ i++;
+ lua_pushstring(L, e->name);
+ lua_rawseti(L, -2, i);
+ }
+ break;
+ default:
+ lua_pushnil(L);
+ break;
+ }
+}
+