Skip to content

Commit ba06518

Browse files
committed
Add luaL_execresult to C API.
1 parent ae91294 commit ba06518

5 files changed

Lines changed: 55 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ For Lua 5.1 additionally:
149149
* `luaL_setmetatable`
150150
* `luaL_getsubtable`
151151
* `luaL_traceback`
152+
* `luaL_execresult`
152153
* `luaL_fileresult`
153154
* `luaL_checkversion` (with empty body, only to avoid compile errors,
154155
see [here][20])
@@ -177,7 +178,6 @@ For Lua 5.1 additionally:
177178
* `lua_upvaluejoin` (5.1)
178179
* `lua_version` (5.1)
179180
* `lua_yieldk` (5.1)
180-
* `luaL_execresult` (5.1)
181181
* `luaL_loadbufferx` (5.1)
182182
* `luaL_loadfilex` (5.1)
183183

c-api/compat-5.3.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
/* definitions for Lua 5.1 only */
17-
#if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM == 501
17+
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
1818

1919

2020
COMPAT53_API int lua_absindex (lua_State *L, int i) {
@@ -366,6 +366,44 @@ COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
366366
return 3;
367367
}
368368
}
369+
370+
371+
#if !defined(l_inspectstat) && \
372+
(defined(unix) || defined(__unix) || defined(__unix__) || \
373+
defined(__TOS_AIX__) || defined(_SYSTYPE_BSD))
374+
/* some form of unix; check feature macros in unistd.h for details */
375+
# include <unistd.h>
376+
/* check posix version; the relevant include files and macros probably
377+
* were available before 2001, but I'm not sure */
378+
# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
379+
# include <sys/wait.h>
380+
# define l_inspectstat(stat,what) \
381+
if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
382+
else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
383+
# endif
384+
#endif
385+
386+
/* provide default (no-op) version */
387+
#if !defined(l_inspectstat)
388+
# define l_inspectstat(stat,what) ((void)0)
389+
#endif
390+
391+
392+
COMPAT53_API int luaL_execresult (lua_State *L, int stat) {
393+
const char *what = "exit";
394+
if (stat == -1)
395+
return luaL_fileresult(L, 0, NULL);
396+
else {
397+
l_inspectstat(stat, what);
398+
if (*what == 'e' && stat == 0)
399+
lua_pushboolean(L, 1);
400+
else
401+
lua_pushnil(L);
402+
lua_pushstring(L, what);
403+
lua_pushinteger(L, stat);
404+
return 3;
405+
}
406+
}
369407
#endif /* not COMPAT53_IS_LUAJIT */
370408

371409

c-api/compat-5.3.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
* lua_upvaluejoin
4646
* lua_version
4747
* lua_yieldk
48-
* luaL_execresult
4948
* luaL_loadbufferx
5049
* luaL_loadfilex
5150
*/
@@ -150,6 +149,9 @@ COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
150149

151150
#define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult)
152151
COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname);
152+
153+
#define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult)
154+
COMPAT53_API int luaL_execresult (lua_State *L, int stat);
153155
#endif /* COMPAT53_IS_LUAJIT */
154156

155157
#define lua_callk(L, na, nr, ctx, cont) \

tests/test.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,5 +700,10 @@ print(pcall(mod.tolstring, t))
700700

701701
___''
702702
print(mod.buffer())
703+
704+
___''
705+
print(mod.exec("exit 0"))
706+
print(mod.exec("exit 1"))
707+
print(mod.exec("exit 25"))
703708
___''
704709

tests/testmod.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdio.h>
2+
#include <stdlib.h>
23
#include <lua.h>
34
#include <lauxlib.h>
45
#include "compat-5.3.h"
@@ -256,6 +257,11 @@ static int test_buffer (lua_State *L) {
256257
return 1;
257258
}
258259

260+
static int test_exec (lua_State *L) {
261+
const char *cmd = luaL_checkstring(L, 1);
262+
return luaL_execresult(L, system(cmd));
263+
}
264+
259265

260266
static const luaL_Reg funcs[] = {
261267
{ "isinteger", test_isinteger },
@@ -277,6 +283,7 @@ static const luaL_Reg funcs[] = {
277283
{ "globals", test_globals },
278284
{ "tolstring", test_tolstring },
279285
{ "buffer", test_buffer },
286+
{ "exec", test_exec },
280287
{ NULL, NULL }
281288
};
282289

0 commit comments

Comments
 (0)