Add memtest support.
[syslinux-debian/hramrach.git] / com32 / lua / src / liolib.c
blobcf9dca229358c99b488da9626e686a349d292ef3
1 /*
2 ** $Id: liolib.c,v 2.73.1.4 2010/05/14 15:33:51 roberto Exp $
3 ** Standard I/O (and system) library
4 ** See Copyright Notice in lua.h
5 */
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
13 #define liolib_c
14 #define LUA_LIB
16 #include "lua.h"
18 #include "lauxlib.h"
19 #include "lualib.h"
21 #ifdef SYSLINUX
22 #define NO_TMP_FILE 1
23 #define NO_READ_NUMBER 1
24 #define NO_TEST_EOF 1
25 #define NO_CLEAR_ERR 1
26 #define NO_F_SEEK 1
27 #define NO_F_SETVBUF 1
28 #endif
30 #define IO_INPUT 1
31 #define IO_OUTPUT 2
33 static const char *const fnames[] = {"input", "output"};
36 static int pushresult (lua_State *L, int i, const char *filename) {
37 int en = errno; /* calls to Lua API may change this value */
38 if (i) {
39 lua_pushboolean(L, 1);
40 return 1;
42 else {
43 lua_pushnil(L);
44 if (filename)
45 lua_pushfstring(L, "%s: %s", filename, strerror(en));
46 else
47 lua_pushfstring(L, "%s", strerror(en));
48 lua_pushinteger(L, en);
49 return 3;
54 static void fileerror (lua_State *L, int arg, const char *filename) {
55 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
56 luaL_argerror(L, arg, lua_tostring(L, -1));
60 #define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
63 static int io_type (lua_State *L) {
64 void *ud;
65 luaL_checkany(L, 1);
66 ud = lua_touserdata(L, 1);
67 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
68 if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
69 lua_pushnil(L); /* not a file */
70 else if (*((FILE **)ud) == NULL)
71 lua_pushliteral(L, "closed file");
72 else
73 lua_pushliteral(L, "file");
74 return 1;
78 static FILE *tofile (lua_State *L) {
79 FILE **f = tofilep(L);
80 if (*f == NULL)
81 luaL_error(L, "attempt to use a closed file");
82 return *f;
88 ** When creating file handles, always creates a `closed' file handle
89 ** before opening the actual file; so, if there is a memory error, the
90 ** file is not left opened.
92 static FILE **newfile (lua_State *L) {
93 FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
94 *pf = NULL; /* file handle is currently `closed' */
95 luaL_getmetatable(L, LUA_FILEHANDLE);
96 lua_setmetatable(L, -2);
97 return pf;
102 ** function to (not) close the standard files stdin, stdout, and stderr
104 static int io_noclose (lua_State *L) {
105 lua_pushnil(L);
106 lua_pushliteral(L, "cannot close standard file");
107 return 2;
112 ** function to close 'popen' files
114 static int io_pclose (lua_State *L) {
115 FILE **p = tofilep(L);
116 int ok = lua_pclose(L, *p);
117 *p = NULL;
118 return pushresult(L, ok, NULL);
123 ** function to close regular files
125 static int io_fclose (lua_State *L) {
126 FILE **p = tofilep(L);
127 int ok = (fclose(*p) == 0);
128 *p = NULL;
129 return pushresult(L, ok, NULL);
133 static int aux_close (lua_State *L) {
134 lua_getfenv(L, 1);
135 lua_getfield(L, -1, "__close");
136 return (lua_tocfunction(L, -1))(L);
140 static int io_close (lua_State *L) {
141 if (lua_isnone(L, 1))
142 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
143 tofile(L); /* make sure argument is a file */
144 return aux_close(L);
148 static int io_gc (lua_State *L) {
149 FILE *f = *tofilep(L);
150 /* ignore closed files */
151 if (f != NULL)
152 aux_close(L);
153 return 0;
157 static int io_tostring (lua_State *L) {
158 FILE *f = *tofilep(L);
159 if (f == NULL)
160 lua_pushliteral(L, "file (closed)");
161 else
162 lua_pushfstring(L, "file (%p)", f);
163 return 1;
167 static int io_open (lua_State *L) {
168 const char *filename = luaL_checkstring(L, 1);
169 const char *mode = luaL_optstring(L, 2, "r");
170 FILE **pf = newfile(L);
171 *pf = fopen(filename, mode);
172 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
177 ** this function has a separated environment, which defines the
178 ** correct __close for 'popen' files
180 static int io_popen (lua_State *L) {
181 const char *filename = luaL_checkstring(L, 1);
182 const char *mode = luaL_optstring(L, 2, "r");
183 FILE **pf = newfile(L);
184 *pf = lua_popen(L, filename, mode);
185 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
189 #ifndef NO_TMP_FILE
190 static int io_tmpfile (lua_State *L) {
191 FILE **pf = newfile(L);
192 *pf = tmpfile();
193 return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
195 #endif
198 static FILE *getiofile (lua_State *L, int findex) {
199 FILE *f;
200 lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
201 f = *(FILE **)lua_touserdata(L, -1);
202 if (f == NULL)
203 luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
204 return f;
208 static int g_iofile (lua_State *L, int f, const char *mode) {
209 if (!lua_isnoneornil(L, 1)) {
210 const char *filename = lua_tostring(L, 1);
211 if (filename) {
212 FILE **pf = newfile(L);
213 *pf = fopen(filename, mode);
214 if (*pf == NULL)
215 fileerror(L, 1, filename);
217 else {
218 tofile(L); /* check that it's a valid file handle */
219 lua_pushvalue(L, 1);
221 lua_rawseti(L, LUA_ENVIRONINDEX, f);
223 /* return current value */
224 lua_rawgeti(L, LUA_ENVIRONINDEX, f);
225 return 1;
229 static int io_input (lua_State *L) {
230 return g_iofile(L, IO_INPUT, "r");
234 static int io_output (lua_State *L) {
235 return g_iofile(L, IO_OUTPUT, "w");
239 static int io_readline (lua_State *L);
242 static void aux_lines (lua_State *L, int idx, int toclose) {
243 lua_pushvalue(L, idx);
244 lua_pushboolean(L, toclose); /* close/not close file when finished */
245 lua_pushcclosure(L, io_readline, 2);
249 static int f_lines (lua_State *L) {
250 tofile(L); /* check that it's a valid file handle */
251 aux_lines(L, 1, 0);
252 return 1;
256 static int io_lines (lua_State *L) {
257 if (lua_isnoneornil(L, 1)) { /* no arguments? */
258 /* will iterate over default input */
259 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);
260 return f_lines(L);
262 else {
263 const char *filename = luaL_checkstring(L, 1);
264 FILE **pf = newfile(L);
265 *pf = fopen(filename, "r");
266 if (*pf == NULL)
267 fileerror(L, 1, filename);
268 aux_lines(L, lua_gettop(L), 1);
269 return 1;
275 ** {======================================================
276 ** READ
277 ** =======================================================
280 #ifndef NO_READ_NUMBER /* No fscanf() and thus no read_number() */
281 static int read_number (lua_State *L, FILE *f) {
282 lua_Number d;
283 if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
284 lua_pushnumber(L, d);
285 return 1;
287 else {
288 lua_pushnil(L); /* "result" to be removed */
289 return 0; /* read fails */
292 #endif
294 #ifndef NO_TEST_EOF /* no buffering -> no ungetc() -> no EOF test */
295 static int test_eof (lua_State *L, FILE *f) {
296 int c = getc(f);
297 ungetc(c, f);
298 lua_pushlstring(L, NULL, 0);
299 return (c != EOF);
301 #endif
304 static int read_line (lua_State *L, FILE *f) {
305 luaL_Buffer b;
306 luaL_buffinit(L, &b);
307 for (;;) {
308 size_t l;
309 char *p = luaL_prepbuffer(&b);
310 if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
311 luaL_pushresult(&b); /* close buffer */
312 return (lua_objlen(L, -1) > 0); /* check whether read something */
314 l = strlen(p);
315 if (l == 0 || p[l-1] != '\n')
316 luaL_addsize(&b, l);
317 else {
318 luaL_addsize(&b, l - 1); /* do not include `eol' */
319 luaL_pushresult(&b); /* close buffer */
320 return 1; /* read at least an `eol' */
325 static int read_chars (lua_State *L, FILE *f, size_t n) {
326 size_t rlen; /* how much to read */
327 size_t nr; /* number of chars actually read */
328 luaL_Buffer b;
329 luaL_buffinit(L, &b);
330 rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
331 do {
332 char *p = luaL_prepbuffer(&b);
333 if (rlen > n) rlen = n; /* cannot read more than asked */
334 nr = fread(p, sizeof(char), rlen, f);
335 luaL_addsize(&b, nr);
336 n -= nr; /* still have to read `n' chars */
337 } while (n > 0 && nr == rlen); /* until end of count or eof */
338 luaL_pushresult(&b); /* close buffer */
339 return (n == 0 || lua_objlen(L, -1) > 0);
342 static int g_read (lua_State *L, FILE *f, int first) {
343 int nargs = lua_gettop(L) - 1;
344 int success;
345 int n;
346 #ifndef NO_CLEAR_ERR
347 clearerr(f);
348 #endif
349 if (nargs == 0) { /* no arguments? */
350 success = read_line(L, f);
351 n = first+1; /* to return 1 result */
353 else { /* ensure stack space for all results and for auxlib's buffer */
354 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
355 success = 1;
356 for (n = first; nargs-- && success; n++) {
357 if (lua_type(L, n) == LUA_TNUMBER) {
358 size_t l = (size_t)lua_tointeger(L, n);
359 #ifndef NO_TEST_EOF
360 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
361 #else /* we don't have test_eof defined */
362 success = (l == 0) ? 1 : read_chars(L, f, l);
363 #endif
365 else {
366 const char *p = lua_tostring(L, n);
367 luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
368 switch (p[1]) {
369 case 'n': /* number */
370 #ifndef NO_READ_NUMBER
371 success = read_number(L, f);
372 #else
373 return luaL_argerror(L, n, "\"*number\" not supported");
374 #endif
375 break;
376 case 'l': /* line */
377 success = read_line(L, f);
378 break;
379 case 'a': /* file */
380 read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
381 success = 1; /* always success */
382 break;
383 default:
384 return luaL_argerror(L, n, "invalid format");
389 if (ferror(f))
390 return pushresult(L, 0, NULL);
391 if (!success) {
392 lua_pop(L, 1); /* remove last result */
393 lua_pushnil(L); /* push nil instead */
395 return n - first;
399 static int io_read (lua_State *L) {
400 return g_read(L, getiofile(L, IO_INPUT), 1);
404 static int f_read (lua_State *L) {
405 return g_read(L, tofile(L), 2);
409 static int io_readline (lua_State *L) {
410 FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
411 int sucess;
412 if (f == NULL) /* file is already closed? */
413 luaL_error(L, "file is already closed");
414 sucess = read_line(L, f);
415 if (ferror(f))
416 return luaL_error(L, "%s", strerror(errno));
417 if (sucess) return 1;
418 else { /* EOF */
419 if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
420 lua_settop(L, 0);
421 lua_pushvalue(L, lua_upvalueindex(1));
422 aux_close(L); /* close it */
424 return 0;
428 /* }====================================================== */
431 static int g_write (lua_State *L, FILE *f, int arg) {
432 int nargs = lua_gettop(L) - 1;
433 int status = 1;
434 for (; nargs--; arg++) {
435 if (lua_type(L, arg) == LUA_TNUMBER) {
436 /* optimization: could be done exactly as for strings */
437 status = status &&
438 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
440 else {
441 size_t l;
442 const char *s = luaL_checklstring(L, arg, &l);
443 status = status && (fwrite(s, sizeof(char), l, f) == l);
446 return pushresult(L, status, NULL);
450 static int io_write (lua_State *L) {
451 return g_write(L, getiofile(L, IO_OUTPUT), 1);
455 static int f_write (lua_State *L) {
456 return g_write(L, tofile(L), 2);
459 #ifndef NO_F_SEEK
460 static int f_seek (lua_State *L) {
461 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
462 static const char *const modenames[] = {"set", "cur", "end", NULL};
463 FILE *f = tofile(L);
464 int op = luaL_checkoption(L, 2, "cur", modenames);
465 long offset = luaL_optlong(L, 3, 0);
466 op = fseek(f, offset, mode[op]);
467 if (op)
468 return pushresult(L, 0, NULL); /* error */
469 else {
470 lua_pushinteger(L, ftell(f));
471 return 1;
474 #endif
476 #ifndef NO_F_SETVBUF
477 static int f_setvbuf (lua_State *L) {
478 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
479 static const char *const modenames[] = {"no", "full", "line", NULL};
480 FILE *f = tofile(L);
481 int op = luaL_checkoption(L, 2, NULL, modenames);
482 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
483 int res = setvbuf(f, NULL, mode[op], sz);
484 return pushresult(L, res == 0, NULL);
486 #endif
489 static int io_flush (lua_State *L) {
490 return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
494 static int f_flush (lua_State *L) {
495 return pushresult(L, fflush(tofile(L)) == 0, NULL);
499 static const luaL_Reg iolib[] = {
500 {"close", io_close},
501 {"flush", io_flush},
502 {"input", io_input},
503 {"lines", io_lines},
504 {"open", io_open},
505 {"output", io_output},
506 {"popen", io_popen},
507 {"read", io_read},
508 #ifndef NO_TMP_FILE
509 {"tmpfile", io_tmpfile},
510 #endif
511 {"type", io_type},
512 {"write", io_write},
513 {NULL, NULL}
517 static const luaL_Reg flib[] = {
518 {"close", io_close},
519 {"flush", f_flush},
520 {"lines", f_lines},
521 {"read", f_read},
522 #ifndef NO_F_SEEK
523 {"seek", f_seek},
524 #endif
525 #ifndef NO_F_SETVBUF
526 {"setvbuf", f_setvbuf},
527 #endif
528 {"write", f_write},
529 {"__gc", io_gc},
530 {"__tostring", io_tostring},
531 {NULL, NULL}
535 static void createmeta (lua_State *L) {
536 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
537 lua_pushvalue(L, -1); /* push metatable */
538 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
539 luaL_register(L, NULL, flib); /* file methods */
543 static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
544 *newfile(L) = f;
545 if (k > 0) {
546 lua_pushvalue(L, -1);
547 lua_rawseti(L, LUA_ENVIRONINDEX, k);
549 lua_pushvalue(L, -2); /* copy environment */
550 lua_setfenv(L, -2); /* set it */
551 lua_setfield(L, -3, fname);
555 static void newfenv (lua_State *L, lua_CFunction cls) {
556 lua_createtable(L, 0, 1);
557 lua_pushcfunction(L, cls);
558 lua_setfield(L, -2, "__close");
562 LUALIB_API int luaopen_io (lua_State *L) {
563 createmeta(L);
564 /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
565 newfenv(L, io_fclose);
566 lua_replace(L, LUA_ENVIRONINDEX);
567 /* open library */
568 luaL_register(L, LUA_IOLIBNAME, iolib);
569 /* create (and set) default files */
570 newfenv(L, io_noclose); /* close function for default files */
571 createstdfile(L, stdin, IO_INPUT, "stdin");
572 createstdfile(L, stdout, IO_OUTPUT, "stdout");
573 createstdfile(L, stderr, 0, "stderr");
574 lua_pop(L, 1); /* pop environment for default files */
575 lua_getfield(L, -1, "popen");
576 newfenv(L, io_pclose); /* create environment for 'popen' */
577 lua_setfenv(L, -2); /* set fenv for 'popen' */
578 lua_pop(L, 1); /* pop 'popen' */
579 return 1;