diff mbox series

[2/3] Lua: Add support for script handlers

Message ID YfAfkpuJ36I7RlzI@decadent.org.uk
State Changes Requested
Headers show
Series [1/3] handlers: Change parameters to allow passing both data and scriptfn | expand

Commit Message

Ben Hutchings Jan. 25, 2022, 4:04 p.m. UTC
Currently only the image parameter is passed to handlers written in
Lua.  Add the scriptfn parameter, so that it is possible to write
useful script handlers.

This converts the enumerated value to a string value of either
"preinst" or "postinst", for consistency with the way package-
provided scripts are called.

Signed-off-by: Ben Hutchings <ben.hutchings_ext@softathome.com>
---
 corelib/lua_interface.c | 14 +++++++++++++-
 doc/source/handlers.rst |  6 +++++-
 2 files changed, 18 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 8045d73..8041567 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -1098,7 +1098,19 @@  static int l_handler_wrapper(struct img_type *img, void *data,
 	lua_rawgeti(gL, LUA_REGISTRYINDEX, l_func_ref );
 	image2table(gL, img);
 
-	if (LUA_OK != (res = lua_pcall(gL, 1, 1, 0))) {
+	switch (scriptfn) {
+	case PREINSTALL:
+		lua_pushstring(gL, "preinst");
+		break;
+	case POSTINSTALL:
+		lua_pushstring(gL, "postinst");
+		break;
+	default:
+		lua_pushnil(gL);
+		break;
+	}
+
+	if (LUA_OK != (res = lua_pcall(gL, 2, 1, 0))) {
 		ERROR("Error %d while executing the Lua callback: %s",
 			  res, lua_tostring(gL, -1));
 		return -1;
diff --git a/doc/source/handlers.rst b/doc/source/handlers.rst
index 6d5be62..2ceea6f 100644
--- a/doc/source/handlers.rst
+++ b/doc/source/handlers.rst
@@ -293,7 +293,7 @@  In analogy to C handlers, the prototype for a Lua handler is
 
 ::
 
-        function lua_handler(image)
+        function lua_handler(image, scriptfn)
             ...
         end
 
@@ -306,6 +306,10 @@  underscores for the Lua domain to make them idiomatic, e.g.,
 ``installed-directly`` becomes ``installed_directly`` in the
 Lua domain.
 
+For a script handler, ``scriptfn`` is either ``"preinst"`` or
+``"postinst"``.  For other handlers it is ``nil``, and does not need
+to be declared as a parameter.
+
 To register a Lua handler, the ``swupdate`` module provides the
 ``swupdate.register_handler()`` method that takes the handler's
 name, the Lua handler function to be registered under that name,