diff options
| author | Philipp Janda <siffiejoe@gmx.net> | 2015-05-20 07:25:48 +0200 |
|---|---|---|
| committer | Philipp Janda <siffiejoe@gmx.net> | 2015-05-20 07:25:48 +0200 |
| commit | ba065189ae67822ac9c69cc6ba633e3e8e123ef6 (patch) | |
| tree | 8f064b13129834749ae72c03c15e46ab20adadc2 | |
| parent | ae91294546c2edc60e3349e909740f15c939f1af (diff) | |
| download | lua-compat-5.3-ba065189ae67822ac9c69cc6ba633e3e8e123ef6.tar.gz lua-compat-5.3-ba065189ae67822ac9c69cc6ba633e3e8e123ef6.tar.bz2 lua-compat-5.3-ba065189ae67822ac9c69cc6ba633e3e8e123ef6.zip | |
Add luaL_execresult to C API.
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | c-api/compat-5.3.c | 40 | ||||
| -rw-r--r-- | c-api/compat-5.3.h | 4 | ||||
| -rwxr-xr-x | tests/test.lua | 5 | ||||
| -rw-r--r-- | tests/testmod.c | 7 |
5 files changed, 55 insertions, 3 deletions
| @@ -149,6 +149,7 @@ For Lua 5.1 additionally: | |||
| 149 | * `luaL_setmetatable` | 149 | * `luaL_setmetatable` |
| 150 | * `luaL_getsubtable` | 150 | * `luaL_getsubtable` |
| 151 | * `luaL_traceback` | 151 | * `luaL_traceback` |
| 152 | * `luaL_execresult` | ||
| 152 | * `luaL_fileresult` | 153 | * `luaL_fileresult` |
| 153 | * `luaL_checkversion` (with empty body, only to avoid compile errors, | 154 | * `luaL_checkversion` (with empty body, only to avoid compile errors, |
| 154 | see [here][20]) | 155 | see [here][20]) |
| @@ -177,7 +178,6 @@ For Lua 5.1 additionally: | |||
| 177 | * `lua_upvaluejoin` (5.1) | 178 | * `lua_upvaluejoin` (5.1) |
| 178 | * `lua_version` (5.1) | 179 | * `lua_version` (5.1) |
| 179 | * `lua_yieldk` (5.1) | 180 | * `lua_yieldk` (5.1) |
| 180 | * `luaL_execresult` (5.1) | ||
| 181 | * `luaL_loadbufferx` (5.1) | 181 | * `luaL_loadbufferx` (5.1) |
| 182 | * `luaL_loadfilex` (5.1) | 182 | * `luaL_loadfilex` (5.1) |
| 183 | 183 | ||
diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c index 962b951..0e06176 100644 --- a/c-api/compat-5.3.c +++ b/c-api/compat-5.3.c | |||
| @@ -14,7 +14,7 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | /* definitions for Lua 5.1 only */ | 16 | /* 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 |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | COMPAT53_API int lua_absindex (lua_State *L, int i) { | 20 | 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) { | |||
| 366 | return 3; | 366 | return 3; |
| 367 | } | 367 | } |
| 368 | } | 368 | } |
| 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 | } | ||
| 369 | #endif /* not COMPAT53_IS_LUAJIT */ | 407 | #endif /* not COMPAT53_IS_LUAJIT */ |
| 370 | 408 | ||
| 371 | 409 | ||
diff --git a/c-api/compat-5.3.h b/c-api/compat-5.3.h index dc8d3ea..cd6764c 100644 --- a/c-api/compat-5.3.h +++ b/c-api/compat-5.3.h | |||
| @@ -45,7 +45,6 @@ | |||
| 45 | * lua_upvaluejoin | 45 | * lua_upvaluejoin |
| 46 | * lua_version | 46 | * lua_version |
| 47 | * lua_yieldk | 47 | * lua_yieldk |
| 48 | * luaL_execresult | ||
| 49 | * luaL_loadbufferx | 48 | * luaL_loadbufferx |
| 50 | * luaL_loadfilex | 49 | * luaL_loadfilex |
| 51 | */ | 50 | */ |
| @@ -150,6 +149,9 @@ COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, | |||
| 150 | 149 | ||
| 151 | #define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult) | 150 | #define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult) |
| 152 | COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname); | 151 | 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); | ||
| 153 | #endif /* COMPAT53_IS_LUAJIT */ | 155 | #endif /* COMPAT53_IS_LUAJIT */ |
| 154 | 156 | ||
| 155 | #define lua_callk(L, na, nr, ctx, cont) \ | 157 | #define lua_callk(L, na, nr, ctx, cont) \ |
diff --git a/tests/test.lua b/tests/test.lua index 856230b..f8fa894 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
| @@ -700,5 +700,10 @@ print(pcall(mod.tolstring, t)) | |||
| 700 | 700 | ||
| 701 | ___'' | 701 | ___'' |
| 702 | print(mod.buffer()) | 702 | print(mod.buffer()) |
| 703 | |||
| 704 | ___'' | ||
| 705 | print(mod.exec("exit 0")) | ||
| 706 | print(mod.exec("exit 1")) | ||
| 707 | print(mod.exec("exit 25")) | ||
| 703 | ___'' | 708 | ___'' |
| 704 | 709 | ||
diff --git a/tests/testmod.c b/tests/testmod.c index f89b10a..c2dc715 100644 --- a/tests/testmod.c +++ b/tests/testmod.c | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | #include <stdio.h> | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> | ||
| 2 | #include <lua.h> | 3 | #include <lua.h> |
| 3 | #include <lauxlib.h> | 4 | #include <lauxlib.h> |
| 4 | #include "compat-5.3.h" | 5 | #include "compat-5.3.h" |
| @@ -256,6 +257,11 @@ static int test_buffer (lua_State *L) { | |||
| 256 | return 1; | 257 | return 1; |
| 257 | } | 258 | } |
| 258 | 259 | ||
| 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 | |||
| 259 | 265 | ||
| 260 | static const luaL_Reg funcs[] = { | 266 | static const luaL_Reg funcs[] = { |
| 261 | { "isinteger", test_isinteger }, | 267 | { "isinteger", test_isinteger }, |
| @@ -277,6 +283,7 @@ static const luaL_Reg funcs[] = { | |||
| 277 | { "globals", test_globals }, | 283 | { "globals", test_globals }, |
| 278 | { "tolstring", test_tolstring }, | 284 | { "tolstring", test_tolstring }, |
| 279 | { "buffer", test_buffer }, | 285 | { "buffer", test_buffer }, |
| 286 | { "exec", test_exec }, | ||
| 280 | { NULL, NULL } | 287 | { NULL, NULL } |
| 281 | }; | 288 | }; |
| 282 | 289 | ||
