Imported Upstream version 4.14.1
[platform/upstream/rpm.git] / rpmio / rpmlua.c
index 0576318..c96fb6b 100644 (file)
@@ -60,6 +60,9 @@ struct rpmluapb_s {
 
 static rpmlua globalLuaState = NULL;
 
+static char *(*nextFileFunc)(void *) = NULL;
+static void *nextFileFuncParam = NULL;
+
 static int luaopen_rpm(lua_State *L);
 static int rpm_print(lua_State *L);
 
@@ -212,6 +215,12 @@ static int pushvar(lua_State *L, rpmluavType type, void *value)
     return ret;
 }
 
+void rpmluaSetNextFileFunc(char *(*func)(void *), void *funcParam)
+{
+    nextFileFunc = func;
+    nextFileFuncParam = funcParam;
+}
+
 void rpmluaSetVar(rpmlua _lua, rpmluav var)
 {
     INITSTATE(_lua, lua);
@@ -526,6 +535,8 @@ int rpmluaRunScript(rpmlua _lua, const char *script, const char *name)
     int ret = 0;
     if (name == NULL)
        name = "<lua>";
+    if (script == NULL)
+       script = "";
     if (luaL_loadbuffer(L, script, strlen(script), name) != 0) {
        rpmlog(RPMLOG_ERR, _("invalid syntax in lua script: %s\n"),
                 lua_tostring(L, -1));
@@ -656,7 +667,9 @@ static int rpm_b64decode(lua_State *L)
 static int rpm_expand(lua_State *L)
 {
     const char *str = luaL_checkstring(L, 1);
-    char *val = rpmExpand(str, NULL);
+    char *val = NULL;
+    if (rpmExpandMacros(NULL, str, &val, 0) < 0)
+       return luaL_error(L, "error expanding macro");
     lua_pushstring(L, val);
     free(val);
     return 1;
@@ -665,16 +678,42 @@ static int rpm_expand(lua_State *L)
 static int rpm_define(lua_State *L)
 {
     const char *str = luaL_checkstring(L, 1);
-    (void) rpmDefineMacro(NULL, str, 0);
+    if (rpmDefineMacro(NULL, str, 0))
+       return luaL_error(L, "error defining macro");
+    return 0;
+}
+
+static int rpm_undefine(lua_State *L)
+{
+    const char *str = luaL_checkstring(L, 1);
+    rpmPopMacro(NULL, str);
     return 0;
 }
 
+static int rpm_load(lua_State *L)
+{
+    const char *str = luaL_checkstring(L, 1);
+    int rc = rpmLoadMacroFile(NULL, str);
+    lua_pushnumber(L, rc);
+    return 1;
+}
+
 static int rpm_interactive(lua_State *L)
 {
     _rpmluaInteractive(L);
     return 0;
 }
 
+static int rpm_next_file(lua_State *L)
+{
+    if (nextFileFunc)
+       lua_pushstring(L, nextFileFunc(nextFileFuncParam));
+    else
+       lua_pushstring(L, NULL);
+
+    return 1;
+}
+
 typedef struct rpmluaHookData_s {
     lua_State *L;
     int funcRef;
@@ -861,10 +900,13 @@ static const luaL_Reg rpmlib[] = {
     {"b64decode", rpm_b64decode},
     {"expand", rpm_expand},
     {"define", rpm_define},
+    {"undefine", rpm_undefine},
+    {"load", rpm_load},
     {"register", rpm_register},
     {"unregister", rpm_unregister},
     {"call", rpm_call},
     {"interactive", rpm_interactive},
+    {"next_file", rpm_next_file},
     {NULL, NULL}
 };