Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | Lua 'print' statement is not working properly inside of RPM 5 |
| 2 | |
| 3 | The print statement should capture the output and send it to the script |
| 4 | processing engine, and not display it directly to the screen. |
| 5 | |
| 6 | This patch is from: http://rpm5.org/cvs/patchset?cn=17671 |
| 7 | |
| 8 | Upstream-Status: backport (patchset 17671 from rpm5.org) |
| 9 | |
| 10 | Signed-off-by: Mark Hatle <mark.hatle@windriver.com> |
| 11 | |
| 12 | Index: rpm-5.4.14/CHANGES |
| 13 | =================================================================== |
| 14 | --- rpm-5.4.14.orig/CHANGES |
| 15 | +++ rpm-5.4.14/CHANGES |
| 16 | @@ -1,3 +1,4 @@ |
| 17 | + - jbj: lua: fix: resurrect output capture with lua-5.2. |
| 18 | - jbj: verify: fix: broken logic for %ghost avoidance (Mark Hatle). |
| 19 | |
| 20 | 5.4.13 -> 5.4.14: |
| 21 | Index: rpm-5.4.14/rpmio/rpmlua.c |
| 22 | =================================================================== |
| 23 | --- rpm-5.4.14.orig/rpmio/rpmlua.c |
| 24 | +++ rpm-5.4.14/rpmio/rpmlua.c |
| 25 | @@ -175,7 +175,7 @@ rpmlua rpmluaNew(void) |
| 26 | }; |
| 27 | /*@=readonlytrans =nullassign @*/ |
| 28 | /*@observer@*/ /*@unchecked@*/ |
| 29 | - const luaL_Reg *lib = lualibs; |
| 30 | + const luaL_Reg *lib; |
| 31 | char *path_buf; |
| 32 | char *path_next; |
| 33 | char *path; |
| 34 | @@ -190,31 +190,34 @@ rpmlua rpmluaNew(void) |
| 35 | |
| 36 | luaL_openlibs(L); |
| 37 | |
| 38 | - for (; lib->name; lib++) { |
| 39 | + for (lib = lualibs; lib->name; lib++) { |
| 40 | luaL_requiref(L, lib->name, lib->func, 1); |
| 41 | + lua_pop(L, 1); |
| 42 | } |
| 43 | |
| 44 | { const char * _lua_path = rpmGetPath(rpmluaPath, NULL); |
| 45 | if (_lua_path != NULL) { |
| 46 | +#if defined(LUA_GLOBALSINDEX) |
| 47 | lua_pushliteral(L, "LUA_PATH"); |
| 48 | lua_pushstring(L, _lua_path); |
| 49 | + lua_rawset(L, LUA_GLOBALSINDEX); |
| 50 | +#else |
| 51 | + lua_pushstring(L, _lua_path); |
| 52 | + lua_setglobal(L, "LUA_PATH"); |
| 53 | +#endif |
| 54 | _lua_path = _free(_lua_path); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #if defined(LUA_GLOBALSINDEX) |
| 59 | - lua_rawset(L, LUA_GLOBALSINDEX); |
| 60 | -#else |
| 61 | - lua_pushglobaltable(L); |
| 62 | -#endif |
| 63 | lua_pushliteral(L, "print"); |
| 64 | lua_pushcfunction(L, rpm_print); |
| 65 | - |
| 66 | -#if defined(LUA_GLOBALSINDEX) |
| 67 | lua_rawset(L, LUA_GLOBALSINDEX); |
| 68 | #else |
| 69 | - lua_pushglobaltable(L); |
| 70 | + lua_pushcfunction(L, rpm_print); |
| 71 | + lua_setglobal(L, "print"); |
| 72 | #endif |
| 73 | + |
| 74 | rpmluaSetData(lua, "lua", lua); |
| 75 | |
| 76 | /* load all standard RPM Lua script files */ |
| 77 | @@ -351,6 +354,9 @@ void rpmluaSetVar(rpmlua _lua, rpmluav v |
| 78 | #if defined(LUA_GLOBALSINDEX) |
| 79 | if (lua->pushsize == 0) |
| 80 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 81 | +#else |
| 82 | + if (lua->pushsize == 0) |
| 83 | + lua_pushglobaltable(L); |
| 84 | #endif |
| 85 | if (pushvar(L, var->keyType, &var->key) != -1) { |
| 86 | if (pushvar(L, var->valueType, &var->value) != -1) |
| 87 | @@ -1039,14 +1045,15 @@ static int rpm_print (lua_State *L) |
| 88 | lua_getglobal(L, "tostring"); |
| 89 | for (i = 1; i <= n; i++) { |
| 90 | const char *s; |
| 91 | + size_t l; |
| 92 | lua_pushvalue(L, -1); /* function to be called */ |
| 93 | lua_pushvalue(L, i); /* value to print */ |
| 94 | lua_call(L, 1, 1); |
| 95 | - s = lua_tostring(L, -1); /* get result */ |
| 96 | + s = lua_tolstring(L, -1, &l); /* get result */ |
| 97 | if (s == NULL) |
| 98 | return luaL_error(L, "`tostring' must return a string to `print'"); |
| 99 | if (lua->storeprint) { |
| 100 | - size_t sl = lua_rawlen(L, -1); |
| 101 | + size_t sl = l; |
| 102 | if ((size_t)(lua->printbufused+sl+1) > lua->printbufsize) { |
| 103 | lua->printbufsize += sl+512; |
| 104 | lua->printbuf = (char *) xrealloc(lua->printbuf, lua->printbufsize); |