- /usr/lib/rpm/init.lua is called during intialization.
CVS patchset: 7188
CVS date: 2004/03/24 19:47:11
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] == '<') {
/**
* 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("/");
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);
}
if (ts->nrefs > 1)
return rpmtsUnlink(ts, "tsCreate");
- ts->lua = rpmluaFree(ts->lua);
-
/*@-nullstate@*/ /* FIX: partial annotations */
rpmtsEmpty(ts);
/*@=nullstate@*/
*/
ts->score = NULL;
- ts->lua = rpmluaNew();
- rpmluaSetData(ts->lua, "ts", ts);
-
ts->nrefs = 0;
return rpmtsLink(ts, "tsCreate");
#include "rpmhash.h" /* XXX hashTable */
#include "rpmal.h" /* XXX availablePackage/relocateFileList ,*/
-#include "rpmlua.h" /* XXX lua state */
/**********************
* Transaction Scores *
rpmtsScore score; /*!< Transaction Score (autorollback). */
- rpmlua lua;
-
/*@refs@*/
int nrefs; /*!< Reference count. */
};
}
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);
mb->t += len;
mb->nb -= len;
}
+ rpmluaSetPrintBuffer(lua, 0);
free(scriptbuf);
- lua = rpmluaFree(lua);
s = se;
continue;
}
-/*@-bounds -realcompare -sizeoftype @*/
+/*@-bounds -realcompare -sizeoftype -protoparammatch @*/
#include "system.h"
#include <rpmio.h>
#include <rpmmacro.h>
}
#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)
{
rpmlua lua = (rpmlua) xcalloc(1, sizeof(*lua));
lua_State *L = lua_open();
+ struct stat st;
/*@-readonlytrans@*/
/*@observer@*/ /*@unchecked@*/
static const luaL_reg lualibs[] = {
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;
}
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);
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;
}
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) {
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)
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);
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);
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@*/
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@*/
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 @*/
(void) fputs("\n", stdout);
}
-void rpmluaInteractive(rpmlua lua)
+void rpmluaInteractive(rpmlua _lua)
{
+ INITSTATE(_lua, lua);
_rpmluaInteractive(lua->L);
}
luaL_openlib(L, "rpm", rpmlib, 0);
return 0;
}
-/*@=bounds =realcompare =sizeoftype @*/
+
+/*@=bounds =realcompare =sizeoftype =protoparammatch @*/
+
+/* vim:sts=4:sw=4
+*/
/*@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@*/