瀏覽代碼

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 10 月之前
父節點
當前提交
789bdfb488
共有 1 個文件被更改,包括 11 次插入5 次删除
  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;
 }