[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: os.exit()/os.execute() behaviour (Lua 5.1)
- From: Harley Laue <losinggeneration@...>
- Date: Fri, 17 May 2013 08:59:18 -0500
On 05/17/13 08:39, Luiz Henrique de Figueiredo wrote:
I ran into some issues testing for os.execute() results.
Lua 5.2 does a better job there.
Here's a patch to make loslib.c in Lua 5.1.5 match how it works in Lua
5.2 (this was a quick code import from Lua 5.2.1, so it may not be correct.)
Basically when LUA_USE_POSIX is defined, it uses WIFEXITED with
WEXITSTATUS to normalize the exit code a bit (the inspectstat macro.)
--- loslib.orig.c 2013-05-17 08:53:19.651764788 -0500
+++ loslib.c 2013-05-17 08:53:28.699764584 -0500
@@ -19,6 +19,60 @@
#include "lauxlib.h"
#include "lualib.h"
+/* From lua 5.2 */
+#if !defined(inspectstat) /* { */
+
+#if defined(LUA_USE_POSIX)
+
+#include <sys/wait.h>
+
+/*
+** use appropriate macros to interpret 'pclose' return status
+*/
+#define inspectstat(stat,what) \
+ if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
+ else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
+
+#else
+
+#define inspectstat(stat,what) /* no op */
+
+#endif
+
+#endif /* } */
+
+static int luaL_fileresult (lua_State *L, int stat, const char *fname) {
+ int en = errno; /* calls to Lua API may change this value */
+ if (stat) {
+ lua_pushboolean(L, 1);
+ return 1;
+ }
+ else {
+ lua_pushnil(L);
+ if (fname)
+ lua_pushfstring(L, "%s: %s", fname, strerror(en));
+ else
+ lua_pushfstring(L, "%s", strerror(en));
+ lua_pushinteger(L, en);
+ return 3;
+ }
+}
+
+static int luaL_execresult (lua_State *L, int stat) {
+ const char *what = "exit"; /* type of termination */
+ if (stat == -1) /* error? */
+ return luaL_fileresult(L, 0, NULL);
+ else {
+ inspectstat(stat, what); /* interpret result */
+ if (*what == 'e' && stat == 0) /* successful termination? */
+ lua_pushboolean(L, 1);
+ else
+ lua_pushnil(L);
+ lua_pushstring(L, what);
+ lua_pushinteger(L, stat);
+ return 3; /* return true/nil,what,code */
+ }
+}
static int os_pushresult (lua_State *L, int i, const char *filename) {
int en = errno; /* calls to Lua API may change this value */
@@ -34,11 +88,26 @@
}
}
+static int os_execute (lua_State *L) {
+ const char *cmd = luaL_optstring(L, 1, NULL);
+ int stat = system(cmd);
+ if (cmd != NULL)
+ return luaL_execresult(L, stat);
+ else {
+ lua_pushboolean(L, stat); /* true if there is a shell */
+ return 1;
+ }
+}
+
+/* End from lua 5.2 */
+/* Original os_execute from lua 5.1.5 */
+/*
static int os_execute (lua_State *L) {
lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
return 1;
}
+*/
static int os_remove (lua_State *L) {