2 ** $Id: liolib.c,v 2.148 2015/11/23 11:36:11 roberto Exp $
3 ** Standard I/O (and system) library
4 ** See Copyright Notice in lua.h
29 ** Change this macro to accept other modes for 'fopen' besides
32 #if !defined(l_checkmode)
34 /* accepted extensions to 'mode' in 'fopen' */
35 #if !defined(L_MODEEXT)
39 /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */
40 #define l_checkmode(mode) \
41 (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
42 (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ \
43 (strspn(mode, L_MODEEXT) == strlen(mode)))
48 ** {======================================================
49 ** l_popen spawns a new process connected to the current
50 ** one through the file streams.
51 ** =======================================================
54 #if !defined(l_popen) /* { */
56 #if defined(LUA_USE_POSIX) /* { */
58 #define l_popen(L,c,m) (fflush(NULL), popen(c,m))
59 #define l_pclose(L,file) (pclose(file))
61 #elif defined(LUA_USE_WINDOWS) /* }{ */
63 #define l_popen(L,c,m) (_popen(c,m))
64 #define l_pclose(L,file) (_pclose(file))
68 /* ISO C definitions */
69 #define l_popen(L,c,m) \
70 ((void)((void)c, m), \
71 luaL_error(L, "'popen' not supported"), \
73 #define l_pclose(L,file) ((void)L, (void)file, -1)
79 /* }====================================================== */
82 #if !defined(l_getc) /* { */
84 #if defined(LUA_USE_POSIX)
85 #define l_getc(f) getc_unlocked(f)
86 #define l_lockfile(f) flockfile(f)
87 #define l_unlockfile(f) funlockfile(f)
89 #define l_getc(f) getc(f)
90 #define l_lockfile(f) ((void)0)
91 #define l_unlockfile(f) ((void)0)
98 ** {======================================================
99 ** l_fseek: configuration for longer offsets
100 ** =======================================================
103 #if !defined(l_fseek) /* { */
105 #if defined(LUA_USE_POSIX) /* { */
107 #include <sys/types.h>
109 #define l_fseek(f,o,w) fseeko(f,o,w)
110 #define l_ftell(f) ftello(f)
111 #define l_seeknum off_t
113 #elif defined(LUA_USE_WINDOWS) && !defined(_CRTIMP_TYPEINFO) \
114 && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */
116 /* Windows (but not DDK) and Visual C++ 2005 or higher */
117 #define l_fseek(f,o,w) _fseeki64(f,o,w)
118 #define l_ftell(f) _ftelli64(f)
119 #define l_seeknum __int64
123 /* ISO C definitions */
124 #define l_fseek(f,o,w) fseek(f,o,w)
125 #define l_ftell(f) ftell(f)
126 #define l_seeknum long
132 /* }====================================================== */
135 #define IO_PREFIX "_IO_"
136 #define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1)
137 #define IO_INPUT (IO_PREFIX "input")
138 #define IO_OUTPUT (IO_PREFIX "output")
141 typedef luaL_Stream LStream;
144 #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE))
146 #define isclosed(p) ((p)->closef == NULL)
149 static int io_type (lua_State *L) {
152 p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
154 lua_pushnil(L); /* not a file */
155 else if (isclosed(p))
156 lua_pushliteral(L, "closed file");
158 lua_pushliteral(L, "file");
163 static int f_tostring (lua_State *L) {
164 LStream *p = tolstream(L);
166 lua_pushliteral(L, "file (closed)");
168 lua_pushfstring(L, "file (%p)", p->f);
173 static FILE *tofile (lua_State *L) {
174 LStream *p = tolstream(L);
176 luaL_error(L, "attempt to use a closed file");
183 ** When creating file handles, always creates a 'closed' file handle
184 ** before opening the actual file; so, if there is a memory error, the
185 ** handle is in a consistent state.
187 static LStream *newprefile (lua_State *L) {
188 LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
189 p->closef = NULL; /* mark file handle as 'closed' */
190 luaL_setmetatable(L, LUA_FILEHANDLE);
196 ** Calls the 'close' function from a file handle. The 'volatile' avoids
197 ** a bug in some versions of the Clang compiler (e.g., clang 3.0 for
200 static int aux_close (lua_State *L) {
201 LStream *p = tolstream(L);
202 volatile lua_CFunction cf = p->closef;
203 p->closef = NULL; /* mark stream as closed */
204 return (*cf)(L); /* close it */
208 static int io_close (lua_State *L) {
209 if (lua_isnone(L, 1)) /* no argument? */
210 lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
211 tofile(L); /* make sure argument is an open stream */
216 static int f_gc (lua_State *L) {
217 LStream *p = tolstream(L);
218 if (!isclosed(p) && p->f != NULL)
219 aux_close(L); /* ignore closed and incompletely open files */
225 ** function to close regular files
227 static int io_fclose (lua_State *L) {
228 LStream *p = tolstream(L);
229 int res = fclose(p->f);
230 return luaL_fileresult(L, (res == 0), NULL);
234 static LStream *newfile (lua_State *L) {
235 LStream *p = newprefile(L);
237 p->closef = &io_fclose;
242 static void opencheck (lua_State *L, const char *fname, const char *mode) {
243 LStream *p = newfile(L);
244 p->f = fopen(fname, mode);
246 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
250 static int io_open (lua_State *L) {
251 const char *filename = luaL_checkstring(L, 1);
252 const char *mode = luaL_optstring(L, 2, "r");
253 LStream *p = newfile(L);
254 const char *md = mode; /* to traverse/check mode */
255 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
256 p->f = fopen(filename, mode);
257 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
262 ** function to close 'popen' files
264 static int io_pclose (lua_State *L) {
265 LStream *p = tolstream(L);
266 return luaL_execresult(L, l_pclose(L, p->f));
270 static int io_popen (lua_State *L) {
271 const char *filename = luaL_checkstring(L, 1);
272 const char *mode = luaL_optstring(L, 2, "r");
273 LStream *p = newprefile(L);
274 p->f = l_popen(L, filename, mode);
275 p->closef = &io_pclose;
276 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
280 static int io_tmpfile (lua_State *L) {
281 LStream *p = newfile(L);
283 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
287 static FILE *getiofile (lua_State *L, const char *findex) {
289 lua_getfield(L, LUA_REGISTRYINDEX, findex);
290 p = (LStream *)lua_touserdata(L, -1);
292 luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN);
297 static int g_iofile (lua_State *L, const char *f, const char *mode) {
298 if (!lua_isnoneornil(L, 1)) {
299 const char *filename = lua_tostring(L, 1);
301 opencheck(L, filename, mode);
303 tofile(L); /* check that it's a valid file handle */
306 lua_setfield(L, LUA_REGISTRYINDEX, f);
308 /* return current value */
309 lua_getfield(L, LUA_REGISTRYINDEX, f);
314 static int io_input (lua_State *L) {
315 return g_iofile(L, IO_INPUT, "r");
319 static int io_output (lua_State *L) {
320 return g_iofile(L, IO_OUTPUT, "w");
324 static int io_readline (lua_State *L);
328 ** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit
329 ** in the limit for upvalues of a closure)
331 #define MAXARGLINE 250
333 static void aux_lines (lua_State *L, int toclose) {
334 int n = lua_gettop(L) - 1; /* number of arguments to read */
335 luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments");
336 lua_pushinteger(L, n); /* number of arguments to read */
337 lua_pushboolean(L, toclose); /* close/not close file when finished */
338 lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */
339 lua_pushcclosure(L, io_readline, 3 + n);
343 static int f_lines (lua_State *L) {
344 tofile(L); /* check that it's a valid file handle */
350 static int io_lines (lua_State *L) {
352 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
353 if (lua_isnil(L, 1)) { /* no file name? */
354 lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */
355 lua_replace(L, 1); /* put it at index 1 */
356 tofile(L); /* check that it's a valid file handle */
357 toclose = 0; /* do not close it after iteration */
359 else { /* open a new file */
360 const char *filename = luaL_checkstring(L, 1);
361 opencheck(L, filename, "r");
362 lua_replace(L, 1); /* put file at index 1 */
363 toclose = 1; /* close it after iteration */
365 aux_lines(L, toclose);
371 ** {======================================================
373 ** =======================================================
377 /* maximum length of a numeral */
380 /* auxiliary structure used by 'read_number' */
382 FILE *f; /* file being read */
383 int c; /* current character (look ahead) */
384 int n; /* number of elements in buffer 'buff' */
385 char buff[MAXRN + 1]; /* +1 for ending '\0' */
390 ** Add current char to buffer (if not out of space) and read next one
392 static int nextc (RN *rn) {
393 if (rn->n >= MAXRN) { /* buffer overflow? */
394 rn->buff[0] = '\0'; /* invalidate result */
398 rn->buff[rn->n++] = rn->c; /* save current char */
399 rn->c = l_getc(rn->f); /* read next one */
406 ** Accept current char if it is in 'set' (of size 1 or 2)
408 static int test2 (RN *rn, const char *set) {
409 if (rn->c == set[0] || (rn->c == set[1] && rn->c != '\0'))
416 ** Read a sequence of (hex)digits
418 static int readdigits (RN *rn, int hex) {
420 while ((hex ? isxdigit(rn->c) : isdigit(rn->c)) && nextc(rn))
427 ** Read a number: first reads a valid prefix of a numeral into a buffer.
428 ** Then it calls 'lua_stringtonumber' to check whether the format is
429 ** correct and to convert it to a Lua number
431 static int read_number (lua_State *L, FILE *f) {
437 decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
440 do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
441 test2(&rn, "-+"); /* optional signal */
442 if (test2(&rn, "0")) {
443 if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
444 else count = 1; /* count initial '0' as a valid digit */
446 count += readdigits(&rn, hex); /* integral part */
447 if (test2(&rn, decp)) /* decimal point? */
448 count += readdigits(&rn, hex); /* fractional part */
449 if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */
450 test2(&rn, "-+"); /* exponent signal */
451 readdigits(&rn, 0); /* exponent digits */
453 ungetc(rn.c, rn.f); /* unread look-ahead char */
455 rn.buff[rn.n] = '\0'; /* finish string */
456 if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */
458 else { /* invalid format */
459 lua_pushnil(L); /* "result" to be removed */
460 return 0; /* read fails */
465 static int test_eof (lua_State *L, FILE *f) {
467 ungetc(c, f); /* no-op when c == EOF */
468 lua_pushliteral(L, "");
473 static int read_line (lua_State *L, FILE *f, int chop) {
476 luaL_buffinit(L, &b);
477 while (c != EOF && c != '\n') { /* repeat until end of line */
478 char *buff = luaL_prepbuffer(&b); /* preallocate buffer */
480 l_lockfile(f); /* no memory errors can happen inside the lock */
481 while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
486 if (!chop && c == '\n') /* want a newline and have one? */
487 luaL_addchar(&b, c); /* add ending newline to result */
488 luaL_pushresult(&b); /* close buffer */
489 /* return ok if read something (either a newline or something else) */
490 return (c == '\n' || lua_rawlen(L, -1) > 0);
494 static void read_all (lua_State *L, FILE *f) {
497 luaL_buffinit(L, &b);
498 do { /* read file in chunks of LUAL_BUFFERSIZE bytes */
499 char *p = luaL_prepbuffer(&b);
500 nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);
501 luaL_addsize(&b, nr);
502 } while (nr == LUAL_BUFFERSIZE);
503 luaL_pushresult(&b); /* close buffer */
507 static int read_chars (lua_State *L, FILE *f, size_t n) {
508 size_t nr; /* number of chars actually read */
511 luaL_buffinit(L, &b);
512 p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */
513 nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */
514 luaL_addsize(&b, nr);
515 luaL_pushresult(&b); /* close buffer */
516 return (nr > 0); /* true iff read something */
520 static int g_read (lua_State *L, FILE *f, int first) {
521 int nargs = lua_gettop(L) - 1;
525 if (nargs == 0) { /* no arguments? */
526 success = read_line(L, f, 1);
527 n = first+1; /* to return 1 result */
529 else { /* ensure stack space for all results and for auxlib's buffer */
530 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
532 for (n = first; nargs-- && success; n++) {
533 if (lua_type(L, n) == LUA_TNUMBER) {
534 size_t l = (size_t)luaL_checkinteger(L, n);
535 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
538 const char *p = luaL_checkstring(L, n);
539 if (*p == '*') p++; /* skip optional '*' (for compatibility) */
541 case 'n': /* number */
542 success = read_number(L, f);
545 success = read_line(L, f, 1);
547 case 'L': /* line with end-of-line */
548 success = read_line(L, f, 0);
551 read_all(L, f); /* read entire file */
552 success = 1; /* always success */
555 return luaL_argerror(L, n, "invalid format");
561 return luaL_fileresult(L, 0, NULL);
563 lua_pop(L, 1); /* remove last result */
564 lua_pushnil(L); /* push nil instead */
570 static int io_read (lua_State *L) {
571 return g_read(L, getiofile(L, IO_INPUT), 1);
575 static int f_read (lua_State *L) {
576 return g_read(L, tofile(L), 2);
580 static int io_readline (lua_State *L) {
581 LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1));
583 int n = (int)lua_tointeger(L, lua_upvalueindex(2));
584 if (isclosed(p)) /* file is already closed? */
585 return luaL_error(L, "file is already closed");
587 luaL_checkstack(L, n, "too many arguments");
588 for (i = 1; i <= n; i++) /* push arguments to 'g_read' */
589 lua_pushvalue(L, lua_upvalueindex(3 + i));
590 n = g_read(L, p->f, 2); /* 'n' is number of results */
591 lua_assert(n > 0); /* should return at least a nil */
592 if (lua_toboolean(L, -n)) /* read at least one value? */
593 return n; /* return them */
594 else { /* first result is nil: EOF or error */
595 if (n > 1) { /* is there error information? */
596 /* 2nd result is error message */
597 return luaL_error(L, "%s", lua_tostring(L, -n + 1));
599 if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
601 lua_pushvalue(L, lua_upvalueindex(1));
602 aux_close(L); /* close it */
608 /* }====================================================== */
611 static int g_write (lua_State *L, FILE *f, int arg) {
612 int nargs = lua_gettop(L) - arg;
614 for (; nargs--; arg++) {
615 if (lua_type(L, arg) == LUA_TNUMBER) {
616 /* optimization: could be done exactly as for strings */
617 int len = lua_isinteger(L, arg)
618 ? fprintf(f, LUA_INTEGER_FMT, lua_tointeger(L, arg))
619 : fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg));
620 status = status && (len > 0);
624 const char *s = luaL_checklstring(L, arg, &l);
625 status = status && (fwrite(s, sizeof(char), l, f) == l);
628 if (status) return 1; /* file handle already on stack top */
629 else return luaL_fileresult(L, status, NULL);
633 static int io_write (lua_State *L) {
634 return g_write(L, getiofile(L, IO_OUTPUT), 1);
638 static int f_write (lua_State *L) {
640 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
641 return g_write(L, f, 2);
645 static int f_seek (lua_State *L) {
646 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
647 static const char *const modenames[] = {"set", "cur", "end", NULL};
649 int op = luaL_checkoption(L, 2, "cur", modenames);
650 lua_Integer p3 = luaL_optinteger(L, 3, 0);
651 l_seeknum offset = (l_seeknum)p3;
652 luaL_argcheck(L, (lua_Integer)offset == p3, 3,
653 "not an integer in proper range");
654 op = l_fseek(f, offset, mode[op]);
656 return luaL_fileresult(L, 0, NULL); /* error */
658 lua_pushinteger(L, (lua_Integer)l_ftell(f));
664 static int f_setvbuf (lua_State *L) {
665 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
666 static const char *const modenames[] = {"no", "full", "line", NULL};
668 int op = luaL_checkoption(L, 2, NULL, modenames);
669 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
670 int res = setvbuf(f, NULL, mode[op], (size_t)sz);
671 return luaL_fileresult(L, res == 0, NULL);
676 static int io_flush (lua_State *L) {
677 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
681 static int f_flush (lua_State *L) {
682 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
687 ** functions for 'io' library
689 static const luaL_Reg iolib[] = {
695 {"output", io_output},
698 {"tmpfile", io_tmpfile},
706 ** methods for file handles
708 static const luaL_Reg flib[] = {
714 {"setvbuf", f_setvbuf},
717 {"__tostring", f_tostring},
722 static void createmeta (lua_State *L) {
723 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
724 lua_pushvalue(L, -1); /* push metatable */
725 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
726 luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
727 lua_pop(L, 1); /* pop new metatable */
732 ** function to (not) close the standard files stdin, stdout, and stderr
734 static int io_noclose (lua_State *L) {
735 LStream *p = tolstream(L);
736 p->closef = &io_noclose; /* keep file opened */
738 lua_pushliteral(L, "cannot close standard file");
743 static void createstdfile (lua_State *L, FILE *f, const char *k,
745 LStream *p = newprefile(L);
747 p->closef = &io_noclose;
749 lua_pushvalue(L, -1);
750 lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */
752 lua_setfield(L, -2, fname); /* add file to module */
756 LUAMOD_API int luaopen_io (lua_State *L) {
757 luaL_newlib(L, iolib); /* new module */
759 /* create (and set) default files */
760 createstdfile(L, stdin, IO_INPUT, "stdin");
761 createstdfile(L, stdout, IO_OUTPUT, "stdout");
762 createstdfile(L, stderr, NULL, "stderr");