- Now Lua interface is using a global state.
authorniemeyer <devnull@localhost>
Wed, 24 Mar 2004 19:47:11 +0000 (19:47 +0000)
committerniemeyer <devnull@localhost>
Wed, 24 Mar 2004 19:47:11 +0000 (19:47 +0000)
- /usr/lib/rpm/init.lua is called during intialization.

CVS patchset: 7188
CVS date: 2004/03/24 19:47:11

build/parseScript.c
lib/psm.c
lib/rpmts.c
lib/rpmts.h
rpmio/macro.c
rpmio/rpmlua.c
rpmio/rpmlua.h

index 03634c0..64325e0 100644 (file)
@@ -284,13 +284,11 @@ int parseScript(Spec spec, int parsePart)
     p = getStringBuf(sb);
 
     if (!strcmp(progArgv[0], "<lua>")) {
-       rpmlua lua = rpmluaNew();
+       rpmlua lua = NULL; /* Global state. */
        if (rpmluaCheckScript(lua, p, partname) != RPMRC_OK) {
-           rpmluaFree(lua);
            rc = RPMERR_BADSPEC;
            goto exit;
        }
-       rpmluaFree(lua);
        (void) rpmlibNeedsFeature(pkg->header,
                                  "BuiltinLuaScripts", "4.2.2-1");
     } else if (progArgv[0][0] == '<') {
index c64ef42..d8ea213 100644 (file)
--- a/lib/psm.c
+++ b/lib/psm.c
@@ -563,22 +563,22 @@ exit:
 /**
  * Run internal Lua script.
  */
-rpmRC runLuaScript(rpmlua lua, Header h, const char *sln,
+rpmRC runLuaScript(rpmpsm psm, Header h, const char *sln,
                   int progArgc, const char **progArgv,
                   const char *script, int arg1, int arg2)
 {
+    const rpmts ts = psm->ts;
     int rootFd = -1;
     const char *n, *v, *r;
     rpmRC rc = RPMRC_OK;
     int i;
     int xx;
-    rpmts ts;
+    rpmlua lua = NULL; /* Global state. */
     rpmluav var;
     
     xx = headerNVR(h, &n, &v, &r);
 
-    ts = rpmluaGetData(lua, "ts");
-    if (ts && !rpmtsChrootDone(ts)) {
+    if (!rpmtsChrootDone(ts)) {
        const char *rootDir = rpmtsRootDir(ts);
        if (rootDir != NULL && !(rootDir[0] == '/' && rootDir[1] == '\0')) {
            chdir("/");
@@ -692,7 +692,7 @@ static rpmRC runScript(rpmpsm psm, Header h, const char * sln,
        return rc;
 
     if (progArgv && strcmp(progArgv[0], "<lua>") == 0) {
-       return runLuaScript(ts->lua, h, sln, progArgc, progArgv,
+       return runLuaScript(psm, h, sln, progArgc, progArgv,
                            script, arg1, arg2);
     }
 
index 2d08198..6b38cc7 100644 (file)
@@ -796,8 +796,6 @@ rpmts rpmtsFree(rpmts ts)
     if (ts->nrefs > 1)
        return rpmtsUnlink(ts, "tsCreate");
 
-    ts->lua = rpmluaFree(ts->lua);
-
 /*@-nullstate@*/       /* FIX: partial annotations */
     rpmtsEmpty(ts);
 /*@=nullstate@*/
@@ -1511,9 +1509,6 @@ rpmts rpmtsCreate(void)
      */
     ts->score = NULL;
 
-    ts->lua = rpmluaNew();
-    rpmluaSetData(ts->lua, "ts", ts);
-
     ts->nrefs = 0;
 
     return rpmtsLink(ts, "tsCreate");
index 3d0341b..7281473 100644 (file)
@@ -96,7 +96,6 @@ typedef       enum rpmtsOpX_e {
 
 #include "rpmhash.h"   /* XXX hashTable */
 #include "rpmal.h"     /* XXX availablePackage/relocateFileList ,*/
-#include "rpmlua.h"    /* XXX lua state */
 
 /**********************
  * Transaction Scores *
@@ -320,8 +319,6 @@ struct rpmts_s {
 
     rpmtsScore score;          /*!< Transaction Score (autorollback). */
 
-    rpmlua lua;
-
 /*@refs@*/
     int nrefs;                 /*!< Reference count. */
 };
index 8c7a517..13172b1 100644 (file)
@@ -1366,7 +1366,7 @@ expandMacro(MacroBuf mb)
        }
 
        if (STREQ("lua", f, fn)) {
-               rpmlua lua = rpmluaNew();
+               rpmlua lua = NULL; /* Global state. */
                const char *ls = s+sizeof("{lua:")-1;
                const char *lse = se-sizeof("}")+1;
                char *scriptbuf = (char *)xmalloc((lse-ls)+1);
@@ -1385,8 +1385,8 @@ expandMacro(MacroBuf mb)
                    mb->t += len;
                    mb->nb -= len;
                }
+               rpmluaSetPrintBuffer(lua, 0);
                free(scriptbuf);
-               lua = rpmluaFree(lua);
                s = se;
                continue;
        }
index 1b30f7b..f1305d0 100644 (file)
@@ -1,4 +1,4 @@
-/*@-bounds -realcompare -sizeoftype @*/
+/*@-bounds -realcompare -sizeoftype -protoparammatch @*/
 #include "system.h"
 #include <rpmio.h>
 #include <rpmmacro.h>
@@ -27,6 +27,14 @@ static inline int vsnprintf(char * buf, /*@unused@*/ int nb,
 }
 #endif
 
+#define INITSTATE(_lua, lua) \
+       rpmlua lua = _lua ? _lua : \
+           (globalLuaState ? globalLuaState : \
+                             (globalLuaState = rpmluaNew()))
+
+/*@only@*/ /*@unchecked@*/ /*@null@*/
+static rpmlua globalLuaState = NULL;
+
 static int luaopen_rpm(lua_State *L)
        /*@modifies L @*/;
 static int rpm_print(lua_State *L)
@@ -37,6 +45,7 @@ rpmlua rpmluaNew()
 {
     rpmlua lua = (rpmlua) xcalloc(1, sizeof(*lua));
     lua_State *L = lua_open();
+    struct stat st;
     /*@-readonlytrans@*/
     /*@observer@*/ /*@unchecked@*/
     static const luaL_reg lualibs[] = {
@@ -67,6 +76,8 @@ rpmlua rpmluaNew()
     lua_pushcfunction(L, rpm_print);
     lua_rawset(L, LUA_GLOBALSINDEX);
     rpmluaSetData(lua, "lua", lua);
+    if (stat(RPMCONFIGDIR "/init.lua", &st) != -1)
+       (void)rpmluaRunScriptFile(lua, RPMCONFIGDIR "/init.lua");
     return lua;
 }
 
@@ -80,8 +91,9 @@ void *rpmluaFree(rpmlua lua)
     return NULL;
 }
 
-void rpmluaSetData(rpmlua lua, const char *key, const void *data)
+void rpmluaSetData(rpmlua _lua, const char *key, const void *data)
 {
+    INITSTATE(_lua, lua);
     lua_State *L = lua->L;
     lua_pushliteral(L, "rpm_");
     lua_pushstring(L, key);
@@ -109,21 +121,24 @@ static void *getdata(lua_State *L, const char *key)
     return ret;
 }
 
-void *rpmluaGetData(rpmlua lua, const char *key)
+void *rpmluaGetData(rpmlua _lua, const char *key)
 {
+    INITSTATE(_lua, lua);
     return getdata(lua->L, key);
 }
 
-void rpmluaSetPrintBuffer(rpmlua lua, int flag)
+void rpmluaSetPrintBuffer(rpmlua _lua, int flag)
 {
+    INITSTATE(_lua, lua);
     lua->storeprint = flag;
     free(lua->printbuf);
     lua->printbuf = NULL;
     lua->printbufsize = 0;
 }
 
-const char *rpmluaGetPrintBuffer(rpmlua lua)
+const char *rpmluaGetPrintBuffer(rpmlua _lua)
 {
+    INITSTATE(_lua, lua);
     return lua->printbuf;
 }
 
@@ -148,8 +163,9 @@ static int pushvar(lua_State *L, rpmluavType type, void *value)
     return ret;
 }
 
-void rpmluaSetVar(rpmlua lua, rpmluav var)
+void rpmluaSetVar(rpmlua _lua, rpmluav var)
 {
+    INITSTATE(_lua, lua);
     lua_State *L = lua->L;
     if (var->listmode && lua->pushsize > 0) {
        if (var->keyType != RPMLUAV_NUMBER || var->key.num == (double)0) {
@@ -194,8 +210,9 @@ static void popvar(lua_State *L, rpmluavType *type, void *value)
     lua_pop(L, 1);
 }
 
-void rpmluaGetVar(rpmlua lua, rpmluav var)
+void rpmluaGetVar(rpmlua _lua, rpmluav var)
 {
+    INITSTATE(_lua, lua);
     lua_State *L = lua->L;
     if (!var->listmode) {
        if (lua->pushsize == 0)
@@ -270,30 +287,34 @@ static int findkey(lua_State *L, int oper, const char *key, va_list va)
     return ret;
 }
 
-void rpmluaDelVar(rpmlua lua, const char *key, ...)
+void rpmluaDelVar(rpmlua _lua, const char *key, ...)
 {
+    INITSTATE(_lua, lua);
     va_list va;
     va_start(va, key);
     (void) findkey(lua->L, FINDKEY_REMOVE, key, va);
     va_end(va);
 }
 
-int rpmluaVarExists(rpmlua lua, const char *key, ...)
+int rpmluaVarExists(rpmlua _lua, const char *key, ...)
 {
+    INITSTATE(_lua, lua);
+    lua_State *L = lua->L;
     int ret = 0;
     va_list va;
     va_start(va, key);
-    if (findkey(lua->L, FINDKEY_RETURN, key, va) == 0) {
-       if (!lua_isnil(lua->L, -1))
+    if (findkey(L, FINDKEY_RETURN, key, va) == 0) {
+       if (!lua_isnil(L, -1))
            ret = 1;
-       lua_pop(lua->L, 1);
+       lua_pop(L, 1);
     }
     va_end(va);
     return ret;
 }
 
-void rpmluaPushTable(rpmlua lua, const char *key, ...)
+void rpmluaPushTable(rpmlua _lua, const char *key, ...)
 {
+    INITSTATE(_lua, lua);
     va_list va;
     va_start(va, key);
     (void) findkey(lua->L, FINDKEY_CREATE, key, va);
@@ -301,8 +322,9 @@ void rpmluaPushTable(rpmlua lua, const char *key, ...)
     va_end(va);
 }
 
-void rpmluaPop(rpmlua lua)
+void rpmluaPop(rpmlua _lua)
 {
+    INITSTATE(_lua, lua);
     assert(lua->pushsize > 0);
     lua->pushsize--;
     lua_pop(lua->L, 1);
@@ -434,8 +456,9 @@ int rpmluavValueIsNum(rpmluav var)
     return (var->valueType == RPMLUAV_NUMBER) ? 1 : 0;
 }
 
-int rpmluaCheckScript(rpmlua lua, const char *script, const char *name)
+int rpmluaCheckScript(rpmlua _lua, const char *script, const char *name)
 {
+    INITSTATE(_lua, lua);
     lua_State *L = lua->L;
     int ret = 0;
 /*@-branchstate@*/
@@ -452,8 +475,9 @@ int rpmluaCheckScript(rpmlua lua, const char *script, const char *name)
     return ret;
 }
 
-int rpmluaRunScript(rpmlua lua, const char *script, const char *name)
+int rpmluaRunScript(rpmlua _lua, const char *script, const char *name)
 {
+    INITSTATE(_lua, lua);
     lua_State *L = lua->L;
     int ret = 0;
 /*@-branchstate@*/
@@ -474,6 +498,25 @@ int rpmluaRunScript(rpmlua lua, const char *script, const char *name)
     return ret;
 }
 
+int rpmluaRunScriptFile(rpmlua _lua, const char *filename)
+{
+    INITSTATE(_lua, lua);
+    lua_State *L = lua->L;
+    int ret = 0;
+    if (luaL_loadfile(L, filename) != 0) {
+       rpmError(RPMERR_SCRIPT, _("invalid syntax in lua file: %s\n"),
+                lua_tostring(L, -1));
+       lua_pop(L, 1);
+       ret = -1;
+    } else if (lua_pcall(L, 0, 0, 0) != 0) {
+       rpmError(RPMERR_SCRIPT, _("lua script failed: %s\n"),
+                lua_tostring(L, -1));
+       lua_pop(L, 1);
+       ret = -1;
+    }
+    return ret;
+}
+
 /* From lua.c */
 static int rpmluaReadline(lua_State *L, const char *prompt)
        /*@globals fileSystem @*/
@@ -536,8 +579,9 @@ static void _rpmluaInteractive(lua_State *L)
    (void) fputs("\n", stdout);
 }
 
-void rpmluaInteractive(rpmlua lua)
+void rpmluaInteractive(rpmlua _lua)
 {
+    INITSTATE(_lua, lua);
     _rpmluaInteractive(lua->L);
 }
 
@@ -636,4 +680,8 @@ static int luaopen_rpm(lua_State *L)
     luaL_openlib(L, "rpm", rpmlib, 0);
     return 0;
 }
-/*@=bounds =realcompare =sizeoftype @*/
+
+/*@=bounds =realcompare =sizeoftype =protoparammatch @*/
+
+/* vim:sts=4:sw=4
+*/
index 9c7008a..80d53d0 100644 (file)
@@ -44,40 +44,47 @@ typedef /*@abstract@*/ struct rpmluav_s * rpmluav;
 
 /*@only@*/
 rpmlua rpmluaNew(void)
-       /*@*/;
+       /*@globals fileSystem @*/
+       /*@modifies fileSystem @*/;
 void *rpmluaFree(/*@only@*/ rpmlua lua)
        /*@modifies lua @*/;
 
-int rpmluaCheckScript(rpmlua lua, const char *script, const char *name)
+int rpmluaCheckScript(/*@null@*/ rpmlua lua, const char *script,
+                     /*@null@*/ const char *name)
        /*@modifies lua @*/;
-int rpmluaRunScript(rpmlua lua, const char *script, /*@null@*/ const char *name)
-       /*@modifies lua @*/;
-void rpmluaInteractive(rpmlua lua)
+int rpmluaRunScript(/*@null@*/ rpmlua lua, const char *script,
+                   /*@null@*/ const char *name)
+       /*@globals fileSystem @*/
+       /*@modifies lua, fileSystem @*/;
+int rpmluaRunScriptFile(/*@null@*/ rpmlua lua, const char *filename)
+       /*@globals fileSystem @*/
+       /*@modifies lua, fileSystem @*/;
+void rpmluaInteractive(/*@null@*/ rpmlua lua)
        /*@globals fileSystem @*/
        /*@modifies lua, fileSystem @*/;
 
-void rpmluaSetData(rpmlua lua, const char *key, const void *data)
+void rpmluaSetData(/*@null@*/ rpmlua lua, const char *key, const void *data)
        /*@modifies lua @*/;
-void *rpmluaGetData(rpmlua lua, const char *key)
+void *rpmluaGetData(/*@null@*/ rpmlua lua, const char *key)
        /*@modifies lua @*/;
 
-void rpmluaSetPrintBuffer(rpmlua lua, int flag)
+void rpmluaSetPrintBuffer(/*@null@*/ rpmlua lua, int flag)
        /*@modifies lua @*/;
 /*@exposed@*/
-const char *rpmluaGetPrintBuffer(rpmlua lua)
+const char *rpmluaGetPrintBuffer(/*@null@*/ rpmlua lua)
        /*@modifies lua @*/;
 
-void rpmluaSetVar(rpmlua lua, rpmluav var)
+void rpmluaSetVar(/*@null@*/ rpmlua lua, rpmluav var)
        /*@modifies lua, var @*/;
-void rpmluaGetVar(rpmlua lua, rpmluav var)
+void rpmluaGetVar(/*@null@*/ rpmlua lua, rpmluav var)
        /*@modifies lua, var @*/;
-void rpmluaDelVar(rpmlua lua, const char *key, ...)
+void rpmluaDelVar(/*@null@*/ rpmlua lua, const char *key, ...)
        /*@modifies lua @*/;
-int rpmluaVarExists(rpmlua lua, const char *key, ...)
+int rpmluaVarExists(/*@null@*/ rpmlua lua, const char *key, ...)
        /*@modifies lua @*/;
-void rpmluaPushTable(rpmlua lua, const char *key, ...)
+void rpmluaPushTable(/*@null@*/ rpmlua lua, const char *key, ...)
        /*@modifies lua @*/;
-void rpmluaPop(rpmlua lua)
+void rpmluaPop(/*@null@*/ rpmlua lua)
        /*@modifies lua @*/;
 
 /*@only@*/