Browse Source

lua: Script can set a return code by "return <num>"

Previously to generate return code Lua script used "os.exit()" but
"return" operation didn't affect the return value. Now the both mechanisms
can be used.
Serj Kalichev 2 months ago
parent
commit
789bdfb488
1 changed files with 11 additions and 5 deletions
  1. 11 5
      plugins/lua/klish_lua.c

+ 11 - 5
plugins/lua/klish_lua.c

@@ -56,7 +56,7 @@ static const luaL_Reg klish_lib[] = {
 };
 
 #if LUA_VERSION_NUM >= 502
-static int traceback (lua_State *L)
+static int traceback(lua_State *L)
 {
 	const char *msg = lua_tostring(L, 1);
 
@@ -70,7 +70,7 @@ static int traceback (lua_State *L)
 	return 1;
 }
 #else
-static int traceback (lua_State *L)
+static int traceback(lua_State *L)
 {
 	lua_getfield(L, LUA_GLOBALSINDEX, "debug");
 	if (!lua_istable(L, -1)) {
@@ -91,17 +91,23 @@ static int traceback (lua_State *L)
 #endif
 
 
-static int report (lua_State *L, int status)
+static int report(lua_State *L, int status)
 {
-	if (status && !lua_isnil(L, -1)) {
+	if (lua_isnil(L, -1))
+		return status;
+
+	if (status) {
 		const char *msg = lua_tostring(L, -1);
 		if (msg == NULL)
 			msg = "(error object is not a string)";
 		fprintf(stderr,"Error: %s\n", msg);
 		lua_pop(L, 1);
-		status = -1;
+		return -1;
 	}
 
+	status = lua_tointeger(L, -1);
+	lua_pop(L, 1);
+
 	return status;
 }