diff mbox series

LUA: export SWUpdate function to LUA scripts

Message ID 1513935550-7082-1-git-send-email-sbabic@denx.de
State Accepted
Headers show
Series LUA: export SWUpdate function to LUA scripts | expand

Commit Message

Stefano Babic Dec. 22, 2017, 9:39 a.m. UTC
SWUpdate function are exported for LUA handlers, but not for LUA
scripts. A run_lua_script() function was prepared, but never used
and the LUA Script Handler duplicates most of code.

Factorize the LUA Script Handler to use run_lua_script() and exports
SWUpdate's function for scripts. This allows to use
	require("swupdate")

in own LUA scripts.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 corelib/lua_interface.c      | 39 +++++++++++++++++++++++------------
 handlers/lua_scripthandler.c | 48 ++------------------------------------------
 include/lua_util.h           |  2 +-
 3 files changed, 29 insertions(+), 60 deletions(-)
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 951ec80..f873aac 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -62,6 +62,7 @@  static int l_call_handler(lua_State *L);
 static void image2table(lua_State* L, struct img_type *img);
 static void table2image(lua_State* L, struct img_type *img);
 static void update_table(lua_State* L, struct img_type *img);
+static int luaopen_swupdate(lua_State *L);
 
 #ifdef CONFIG_HANDLER_IN_LUA
 static bool is_type(lua_State *L, uintptr_t type)
@@ -143,46 +144,58 @@  void LUAstackDump(lua_State *L)
 	}
 }
 
-int run_lua_script(char *script, char *function, char *parms)
+int run_lua_script(const char *script, const char *function, char *parms)
 {
 	int ret;
+	const char *output;
 
 	lua_State *L = luaL_newstate(); /* opens Lua */
 	luaL_openlibs(L); /* opens the standard libraries */
+	luaL_requiref(L, "swupdate", luaopen_swupdate, 1 );
 
 	if (luaL_loadfile(L, script)) {
 		ERROR("ERROR loading %s", script);
-		return 1;
+		lua_close(L);
+		return -1;
 	}
 
 	ret = lua_pcall(L, 0, 0, 0);
 	if (ret) {
 		LUAstackDump(L);
-		ERROR("ERROR preparing %s script %d", script, ret);
-		return 1;
+		ERROR("ERROR preparing Lua script %s %d",
+			script, ret);
+		lua_close(L);
+		return -1;
 	}
 
 	lua_getglobal(L, function);
+	if(!lua_isfunction(L,lua_gettop(L))) {
+		lua_close(L);
+		TRACE("Script : no %s in %s script, exiting", function, script);
+		return 0;
+	}
+
 	/* passing arguments */
 	lua_pushstring(L, parms);
 
-	if (lua_pcall(L, 1, 1, 0)) {
+	if (lua_pcall(L, 1, 2, 0)) {
 		LUAstackDump(L);
-		ERROR("ERROR running script");
-		return 1;
+		ERROR("ERROR Calling Lua script %s", script);
+		lua_close(L);
+		return -1;
 	}
 
-	if (lua_type(L, 1) != LUA_TNUMBER) {
-		ERROR("Lua script returns wrong type");
-		lua_close(L);
-		return 1;
+	if (lua_type(L, 1) == LUA_TBOOLEAN)
+		ret = lua_toboolean(L, 1) ? 0 : 1;
+
+	if (lua_type(L, 2) == LUA_TSTRING) {
+		output = lua_tostring(L, 2);
+		TRACE("Script output: %s script end", output);
 	}
 
-	ret = lua_tonumber(L, 1);
 	lua_close(L);
 
 	return ret;
-
 }
 
 /**
diff --git a/handlers/lua_scripthandler.c b/handlers/lua_scripthandler.c
index b4cbb38..f6117fc 100644
--- a/handlers/lua_scripthandler.c
+++ b/handlers/lua_scripthandler.c
@@ -42,9 +42,8 @@  static int start_lua_script(struct img_type *img, void *data)
 {
 	int ret;
 	const char *fnname;
-	const char *output;
 	script_fn scriptfn;
-	lua_State *L = luaL_newstate(); /* opens Lua */
+
 	const char* tmp = get_tmpdirscripts();
 	char filename[MAX_IMAGE_FNAME + strlen(tmp) + 2];
 
@@ -69,50 +68,7 @@  static int start_lua_script(struct img_type *img, void *data)
 		"%s%s", tmp, img->fname);
 	TRACE("Calling Lua %s", filename);
 
-	luaL_openlibs(L); /* opens the standard libraries */
-
-	if (luaL_loadfile(L, filename)) {
-		ERROR("ERROR loading %s", filename);
-		lua_close(L);
-		return -1;
-	}
-
-	ret = lua_pcall(L, 0, 0, 0);
-	if (ret) {
-		LUAstackDump(L);
-		ERROR("ERROR preparing Lua script %s %d",
-			filename, ret);
-		lua_close(L);
-		return -1;
-	}
-
-	lua_getglobal(L, fnname);
-
-	if(!lua_isfunction(L,lua_gettop(L))) {
-		lua_close(L);
-		TRACE("Script : no %s in %s script, exiting", fnname, filename);
-		return 0;
-	}
-
-	/* passing arguments */
-	lua_pushstring(L, filename);
-
-	if (lua_pcall(L, 1, 2, 0)) {
-		LUAstackDump(L);
-		ERROR("ERROR Calling Lua script %s", filename);
-		lua_close(L);
-		return -1;
-	}
-
-	if (lua_type(L, 1) == LUA_TBOOLEAN)
-		ret = lua_toboolean(L, 1) ? 0 : 1;
-
-	if (lua_type(L, 2) == LUA_TSTRING) {
-		output = lua_tostring(L, 2);
-		TRACE("Script output: %s script end", output);
-	}
-
-	lua_close(L);
+	ret = run_lua_script(filename, fnname, filename);
 
 	return ret;
 
diff --git a/include/lua_util.h b/include/lua_util.h
index 5be8c8d..ee43093 100644
--- a/include/lua_util.h
+++ b/include/lua_util.h
@@ -30,7 +30,7 @@ 
 #include "util.h"
 
 void LUAstackDump (lua_State *L);
-int run_lua_script(char *script, char *function, char *parms);
+int run_lua_script(const char *script, const char *function, char *parms);
 lua_State *lua_parser_init(const char *buf);
 int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img);
 int lua_handlers_init(void);