return p;
}
-static void
-uci_lua_perror(lua_State *L, struct uci_context *ctx, char *name)
-{
- 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);
-}
-
static int
lookup_args(lua_State *L, struct uci_context *ctx, int offset, struct uci_ptr *ptr, char **buf)
{
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;
+}
+
static void
uci_push_option(lua_State *L, struct uci_option *o)
{
p = find_package(L, ctx, s, false);
if (p) {
uci_unload(ctx, p);
- lua_pushboolean(L, 1);
+ return uci_push_status(L, ctx, false);
} else {
lua_pushboolean(L, 0);
}
lua_pop(L, 1); /* bool ret value of unload */
s = lua_tostring(L, -1);
- if (uci_load(ctx, s, &p)) {
- uci_lua_perror(L, ctx, "uci.load");
- lua_pushboolean(L, 0);
- } else {
- lua_pushboolean(L, 1);
- }
-
- return 1;
+ uci_load(ctx, s, &p);
+ return uci_push_status(L, ctx, false);
}
if (s)
free(s);
- switch(err) {
- default:
- ctx->err = err;
- uci_lua_perror(L, ctx, "uci.get");
- /* fall through */
- case UCI_ERR_NOTFOUND:
- lua_pushnil(L);
- /* fall through */
- case 0:
- return 1;
- }
+ lua_pushnil(L);
+ return uci_push_status(L, ctx, true);
}
static int
fail:
lua_pushnil(L);
- return 1;
+ return uci_push_status(L, ctx, true);
}
static int
error:
if (s)
free(s);
- if (err)
- uci_lua_perror(L, ctx, "uci.delete");
- lua_pushboolean(L, (err == 0));
- return 1;
+ return uci_push_status(L, ctx, false);
}
static int
}
error:
- if (err)
- uci_lua_perror(L, ctx, "uci.set");
- lua_pushboolean(L, (err == 0));
- return 1;
+ return uci_push_status(L, ctx, false);
}
enum pkg_cmd {
}
err:
- lua_pushboolean(L, !failed);
- return 1;
+ return uci_push_status(L, ctx, false);
}
static int
ctx = find_context(L, &offset);
luaL_checkstring(L, 1 + offset);
ret = uci_set_confdir(ctx, lua_tostring(L, -1));
- lua_pushboolean(L, (ret == 0));
- return 1;
+ return uci_push_status(L, ctx, false);
}
static int
ctx = find_context(L, &offset);
luaL_checkstring(L, 1 + offset);
ret = uci_set_savedir(ctx, lua_tostring(L, -1));
- lua_pushboolean(L, (ret == 0));
-
- return 1;
+ return uci_push_status(L, ctx, false);
}
static int