aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lauxlib.c60
-rw-r--r--lauxlib.h5
-rw-r--r--liolib.c73
-rw-r--r--loslib.c29
4 files changed, 85 insertions, 82 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 22c9de35..777b1aaf 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -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
207LUALIB_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
247LUALIB_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
diff --git a/lauxlib.h b/lauxlib.h
index 0c8f0f28..3123ef25 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.113 2010/11/16 19:20:01 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.114 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*/
@@ -62,6 +62,9 @@ LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
62LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 62LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
63 const char *const lst[]); 63 const char *const lst[]);
64 64
65LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
66LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
67
65/* pre-defined references */ 68/* pre-defined references */
66#define LUA_NOREF (-2) 69#define LUA_NOREF (-2)
67#define LUA_REFNIL (-1) 70#define LUA_REFNIL (-1)
diff --git a/liolib.c b/liolib.c
index 85a4e0b2..a688ad93 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 2.97 2011/02/10 15:35:50 roberto Exp roberto $ 2** $Id: liolib.c,v 2.98 2011/02/21 19:12:54 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -33,20 +33,6 @@
33#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) 33#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
34#define lua_pclose(L,file) ((void)L, pclose(file)) 34#define lua_pclose(L,file) ((void)L, pclose(file))
35 35
36
37#if defined(LUA_USE_POSIX) /* { */
38
39#include <sys/wait.h>
40
41/*
42** use appropriate macros to interpret 'pclose' return status
43*/
44#define inspectstat(stat,what) \
45 if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
46 else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
47
48#endif /* } */
49
50#elif defined(LUA_WIN) /* }{ */ 36#elif defined(LUA_WIN) /* }{ */
51 37
52#define lua_popen(L,c,m) ((void)L, _popen(c,m)) 38#define lua_popen(L,c,m) ((void)L, _popen(c,m))
@@ -64,10 +50,6 @@
64 50
65#endif /* } */ 51#endif /* } */
66 52
67#if !defined(inspectstat)
68#define inspectstat(stat,what) /* no op */
69#endif
70
71 53
72#define IO_INPUT 1 54#define IO_INPUT 1
73#define IO_OUTPUT 2 55#define IO_OUTPUT 2
@@ -76,24 +58,6 @@
76static const char *const fnames[] = {"input", "output"}; 58static const char *const fnames[] = {"input", "output"};
77 59
78 60
79static int pushresult (lua_State *L, int i, const char *filename) {
80 int en = errno; /* calls to Lua API may change this value */
81 if (i) {
82 lua_pushboolean(L, 1);
83 return 1;
84 }
85 else {
86 lua_pushnil(L);
87 if (filename)
88 lua_pushfstring(L, "%s: %s", filename, strerror(en));
89 else
90 lua_pushfstring(L, "%s", strerror(en));
91 lua_pushinteger(L, en);
92 return 3;
93 }
94}
95
96
97static void fileerror (lua_State *L, int arg, const char *filename) { 61static void fileerror (lua_State *L, int arg, const char *filename) {
98 lua_pushfstring(L, "%s: %s", filename, strerror(errno)); 62 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
99 luaL_argerror(L, arg, lua_tostring(L, -1)); 63 luaL_argerror(L, arg, lua_tostring(L, -1));
@@ -162,21 +126,8 @@ static int io_noclose (lua_State *L) {
162static int io_pclose (lua_State *L) { 126static int io_pclose (lua_State *L) {
163 FILE **p = tofilep(L); 127 FILE **p = tofilep(L);
164 int stat = lua_pclose(L, *p); 128 int stat = lua_pclose(L, *p);
165 const char *what = "exit"; /* type of termination */
166 *p = NULL; /* mark stream as closed (for GC) */ 129 *p = NULL; /* mark stream as closed (for GC) */
167 if (stat == -1) /* error? */ 130 return luaL_execresult(L, stat);
168 return pushresult(L, 0, NULL);
169 else {
170 inspectstat(stat, what); /* interpret result from 'pclose' */
171 if (*what == 'e' && stat == 0) /* successful termination? */
172 return pushresult(L, 1, NULL);
173 else { /* return nil,what,code */
174 lua_pushnil(L);
175 lua_pushstring(L, what);
176 lua_pushinteger(L, stat);
177 return 3;
178 }
179 }
180} 131}
181 132
182 133
@@ -187,7 +138,7 @@ static int io_fclose (lua_State *L) {
187 FILE **p = tofilep(L); 138 FILE **p = tofilep(L);
188 int ok = (fclose(*p) == 0); 139 int ok = (fclose(*p) == 0);
189 *p = NULL; /* mark stream as closed (for GC) */ 140 *p = NULL; /* mark stream as closed (for GC) */
190 return pushresult(L, ok, NULL); 141 return luaL_fileresult(L, ok, NULL);
191} 142}
192 143
193 144
@@ -238,7 +189,7 @@ static int io_open (lua_State *L) {
238 " (should match " LUA_QL("[rwa]%%+?b?") ")", mode); 189 " (should match " LUA_QL("[rwa]%%+?b?") ")", mode);
239 pf = newfile(L); 190 pf = newfile(L);
240 *pf = fopen(filename, mode); 191 *pf = fopen(filename, mode);
241 return (*pf == NULL) ? pushresult(L, 0, filename) : 1; 192 return (*pf == NULL) ? luaL_fileresult(L, 0, filename) : 1;
242} 193}
243 194
244 195
@@ -251,14 +202,14 @@ static int io_popen (lua_State *L) {
251 const char *mode = luaL_optstring(L, 2, "r"); 202 const char *mode = luaL_optstring(L, 2, "r");
252 FILE **pf = newfile(L); 203 FILE **pf = newfile(L);
253 *pf = lua_popen(L, filename, mode); 204 *pf = lua_popen(L, filename, mode);
254 return (*pf == NULL) ? pushresult(L, 0, filename) : 1; 205 return (*pf == NULL) ? luaL_fileresult(L, 0, filename) : 1;
255} 206}
256 207
257 208
258static int io_tmpfile (lua_State *L) { 209static int io_tmpfile (lua_State *L) {
259 FILE **pf = newfile(L); 210 FILE **pf = newfile(L);
260 *pf = tmpfile(); 211 *pf = tmpfile();
261 return (*pf == NULL) ? pushresult(L, 0, NULL) : 1; 212 return (*pf == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
262} 213}
263 214
264 215
@@ -469,7 +420,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
469 } 420 }
470 } 421 }
471 if (ferror(f)) 422 if (ferror(f))
472 return pushresult(L, 0, NULL); 423 return luaL_fileresult(L, 0, NULL);
473 if (!success) { 424 if (!success) {
474 lua_pop(L, 1); /* remove last result */ 425 lua_pop(L, 1); /* remove last result */
475 lua_pushnil(L); /* push nil instead */ 426 lua_pushnil(L); /* push nil instead */
@@ -533,7 +484,7 @@ static int g_write (lua_State *L, FILE *f, int arg) {
533 } 484 }
534 } 485 }
535 if (status) return 1; /* file handle already on stack top */ 486 if (status) return 1; /* file handle already on stack top */
536 else return pushresult(L, status, NULL); 487 else return luaL_fileresult(L, status, NULL);
537} 488}
538 489
539 490
@@ -557,7 +508,7 @@ static int f_seek (lua_State *L) {
557 long offset = luaL_optlong(L, 3, 0); 508 long offset = luaL_optlong(L, 3, 0);
558 op = fseek(f, offset, mode[op]); 509 op = fseek(f, offset, mode[op]);
559 if (op) 510 if (op)
560 return pushresult(L, 0, NULL); /* error */ 511 return luaL_fileresult(L, 0, NULL); /* error */
561 else { 512 else {
562 lua_pushinteger(L, ftell(f)); 513 lua_pushinteger(L, ftell(f));
563 return 1; 514 return 1;
@@ -572,18 +523,18 @@ static int f_setvbuf (lua_State *L) {
572 int op = luaL_checkoption(L, 2, NULL, modenames); 523 int op = luaL_checkoption(L, 2, NULL, modenames);
573 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); 524 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
574 int res = setvbuf(f, NULL, mode[op], sz); 525 int res = setvbuf(f, NULL, mode[op], sz);
575 return pushresult(L, res == 0, NULL); 526 return luaL_fileresult(L, res == 0, NULL);
576} 527}
577 528
578 529
579 530
580static int io_flush (lua_State *L) { 531static int io_flush (lua_State *L) {
581 return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); 532 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
582} 533}
583 534
584 535
585static int f_flush (lua_State *L) { 536static int f_flush (lua_State *L) {
586 return pushresult(L, fflush(tofile(L)) == 0, NULL); 537 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
587} 538}
588 539
589 540
diff --git a/loslib.c b/loslib.c
index d4ec884c..fc7f5459 100644
--- a/loslib.c
+++ b/loslib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loslib.c,v 1.32 2010/10/05 12:18:03 roberto Exp roberto $ 2** $Id: loslib.c,v 1.33 2011/01/26 16:30:02 roberto Exp roberto $
3** Standard Operating System library 3** Standard Operating System library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -59,37 +59,28 @@
59 59
60 60
61 61
62static int os_pushresult (lua_State *L, int i, const char *filename) { 62static int os_execute (lua_State *L) {
63 int en = errno; /* calls to Lua API may change this value */ 63 const char *cmd = luaL_optstring(L, 1, NULL);
64 if (i) { 64 int stat = system(cmd);
65 lua_pushboolean(L, 1); 65 if (cmd != NULL)
66 return 1; 66 return luaL_execresult(L, stat);
67 }
68 else { 67 else {
69 lua_pushnil(L); 68 lua_pushboolean(L, stat); /* true if there is a shell */
70 lua_pushfstring(L, "%s: %s", filename, strerror(en)); 69 return 1;
71 lua_pushinteger(L, en);
72 return 3;
73 } 70 }
74} 71}
75 72
76 73
77static int os_execute (lua_State *L) {
78 lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
79 return 1;
80}
81
82
83static int os_remove (lua_State *L) { 74static int os_remove (lua_State *L) {
84 const char *filename = luaL_checkstring(L, 1); 75 const char *filename = luaL_checkstring(L, 1);
85 return os_pushresult(L, remove(filename) == 0, filename); 76 return luaL_fileresult(L, remove(filename) == 0, filename);
86} 77}
87 78
88 79
89static int os_rename (lua_State *L) { 80static int os_rename (lua_State *L) {
90 const char *fromname = luaL_checkstring(L, 1); 81 const char *fromname = luaL_checkstring(L, 1);
91 const char *toname = luaL_checkstring(L, 2); 82 const char *toname = luaL_checkstring(L, 2);
92 return os_pushresult(L, rename(fromname, toname) == 0, fromname); 83 return luaL_fileresult(L, rename(fromname, toname) == 0, fromname);
93} 84}
94 85
95 86