diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-03-03 13:34:46 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-03-03 13:34:46 -0300 |
| commit | d806710ab5622487159f2a8aecf72003d831542b (patch) | |
| tree | 0a2c49e7ee732f17002ed086420f51ca7f42b221 /lauxlib.c | |
| parent | e049abb69a45e06ca5769a3956a0ba36a959d29e (diff) | |
| download | lua-d806710ab5622487159f2a8aecf72003d831542b.tar.gz lua-d806710ab5622487159f2a8aecf72003d831542b.tar.bz2 lua-d806710ab5622487159f2a8aecf72003d831542b.zip | |
returns for file-related functions and process-related functions
unified in 'auxlib'
Diffstat (limited to 'lauxlib.c')
| -rw-r--r-- | lauxlib.c | 60 |
1 files changed, 59 insertions, 1 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.227 2010/11/10 18:05:36 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.228 2011/01/10 15:51:42 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -203,6 +203,64 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { | |||
| 203 | return lua_error(L); | 203 | return lua_error(L); |
| 204 | } | 204 | } |
| 205 | 205 | ||
| 206 | |||
| 207 | LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { | ||
| 208 | int en = errno; /* calls to Lua API may change this value */ | ||
| 209 | if (stat) { | ||
| 210 | lua_pushboolean(L, 1); | ||
| 211 | return 1; | ||
| 212 | } | ||
| 213 | else { | ||
| 214 | lua_pushnil(L); | ||
| 215 | if (fname) | ||
| 216 | lua_pushfstring(L, "%s: %s", fname, strerror(en)); | ||
| 217 | else | ||
| 218 | lua_pushfstring(L, "%s", strerror(en)); | ||
| 219 | lua_pushinteger(L, en); | ||
| 220 | return 3; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | #if !defined(inspectstat) /* { */ | ||
| 226 | |||
| 227 | #if defined(LUA_USE_POSIX) | ||
| 228 | |||
| 229 | #include <sys/wait.h> | ||
| 230 | |||
| 231 | /* | ||
| 232 | ** use appropriate macros to interpret 'pclose' return status | ||
| 233 | */ | ||
| 234 | #define inspectstat(stat,what) \ | ||
| 235 | if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \ | ||
| 236 | else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; } | ||
| 237 | |||
| 238 | #else | ||
| 239 | |||
| 240 | #define inspectstat(stat,what) /* no op */ | ||
| 241 | |||
| 242 | #endif | ||
| 243 | |||
| 244 | #endif /* } */ | ||
| 245 | |||
| 246 | |||
| 247 | LUALIB_API int luaL_execresult (lua_State *L, int stat) { | ||
| 248 | const char *what = "exit"; /* type of termination */ | ||
| 249 | if (stat == -1) /* error? */ | ||
| 250 | return luaL_fileresult(L, 0, NULL); | ||
| 251 | else { | ||
| 252 | inspectstat(stat, what); /* interpret result */ | ||
| 253 | if (*what == 'e' && stat == 0) /* successful termination? */ | ||
| 254 | return luaL_fileresult(L, 1, NULL); | ||
| 255 | else { /* return nil,what,code */ | ||
| 256 | lua_pushnil(L); | ||
| 257 | lua_pushstring(L, what); | ||
| 258 | lua_pushinteger(L, stat); | ||
| 259 | return 3; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 206 | /* }====================================================== */ | 264 | /* }====================================================== */ |
| 207 | 265 | ||
| 208 | 266 | ||
