Lines Matching refs:L
65 #define l_popen(L,c,m) (fflush(NULL), popen(c,m)) argument
66 #define l_pclose(L,file) (pclose(file)) argument
70 #define l_popen(L,c,m) (_popen(c,m)) argument
71 #define l_pclose(L,file) (_pclose(file)) argument
76 #define l_popen(L,c,m) \ argument
78 luaL_error(L, "'popen' not supported"), \
80 #define l_pclose(L,file) ((void)L, (void)file, -1) argument
152 #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) argument
157 static int io_type (lua_State *L) { in io_type() argument
159 luaL_checkany(L, 1); in io_type()
160 p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); in io_type()
162 luaL_pushfail(L); /* not a file */ in io_type()
164 lua_pushliteral(L, "closed file"); in io_type()
166 lua_pushliteral(L, "file"); in io_type()
171 static int f_tostring (lua_State *L) { in f_tostring() argument
172 LStream *p = tolstream(L); in f_tostring()
174 lua_pushliteral(L, "file (closed)"); in f_tostring()
176 lua_pushfstring(L, "file (%p)", p->f); in f_tostring()
181 static FILE *tofile (lua_State *L) { in tofile() argument
182 LStream *p = tolstream(L); in tofile()
184 luaL_error(L, "attempt to use a closed file"); in tofile()
195 static LStream *newprefile (lua_State *L) { in newprefile() argument
196 LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0); in newprefile()
198 luaL_setmetatable(L, LUA_FILEHANDLE); in newprefile()
208 static int aux_close (lua_State *L) { in aux_close() argument
209 LStream *p = tolstream(L); in aux_close()
212 return (*cf)(L); /* close it */ in aux_close()
216 static int f_close (lua_State *L) { in f_close() argument
217 tofile(L); /* make sure argument is an open stream */ in f_close()
218 return aux_close(L); in f_close()
222 static int io_close (lua_State *L) { in io_close() argument
223 if (lua_isnone(L, 1)) /* no argument? */ in io_close()
224 lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */ in io_close()
225 return f_close(L); in io_close()
229 static int f_gc (lua_State *L) { in f_gc() argument
230 LStream *p = tolstream(L); in f_gc()
232 aux_close(L); /* ignore closed and incompletely open files */ in f_gc()
240 static int io_fclose (lua_State *L) { in io_fclose() argument
241 LStream *p = tolstream(L); in io_fclose()
243 return luaL_fileresult(L, (res == 0), NULL); in io_fclose()
247 static LStream *newfile (lua_State *L) { in newfile() argument
248 LStream *p = newprefile(L); in newfile()
255 static void opencheck (lua_State *L, const char *fname, const char *mode) { in opencheck() argument
256 LStream *p = newfile(L); in opencheck()
259 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); in opencheck()
263 static int io_open (lua_State *L) { in io_open() argument
264 const char *filename = luaL_checkstring(L, 1); in io_open()
265 const char *mode = luaL_optstring(L, 2, "r"); in io_open()
266 LStream *p = newfile(L); in io_open()
268 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); in io_open()
270 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; in io_open()
277 static int io_pclose (lua_State *L) { in io_pclose() argument
278 LStream *p = tolstream(L); in io_pclose()
280 return luaL_execresult(L, l_pclose(L, p->f)); in io_pclose()
284 static int io_popen (lua_State *L) { in io_popen() argument
285 const char *filename = luaL_checkstring(L, 1); in io_popen()
286 const char *mode = luaL_optstring(L, 2, "r"); in io_popen()
287 LStream *p = newprefile(L); in io_popen()
288 luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); in io_popen()
289 p->f = l_popen(L, filename, mode); in io_popen()
291 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; in io_popen()
295 static int io_tmpfile (lua_State *L) { in io_tmpfile() argument
296 LStream *p = newfile(L); in io_tmpfile()
298 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; in io_tmpfile()
302 static FILE *getiofile (lua_State *L, const char *findex) { in getiofile() argument
304 lua_getfield(L, LUA_REGISTRYINDEX, findex); in getiofile()
305 p = (LStream *)lua_touserdata(L, -1); in getiofile()
307 luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); in getiofile()
312 static int g_iofile (lua_State *L, const char *f, const char *mode) { in g_iofile() argument
313 if (!lua_isnoneornil(L, 1)) { in g_iofile()
314 const char *filename = lua_tostring(L, 1); in g_iofile()
316 opencheck(L, filename, mode); in g_iofile()
318 tofile(L); /* check that it's a valid file handle */ in g_iofile()
319 lua_pushvalue(L, 1); in g_iofile()
321 lua_setfield(L, LUA_REGISTRYINDEX, f); in g_iofile()
324 lua_getfield(L, LUA_REGISTRYINDEX, f); in g_iofile()
329 static int io_input (lua_State *L) { in io_input() argument
330 return g_iofile(L, IO_INPUT, "r"); in io_input()
334 static int io_output (lua_State *L) { in io_output() argument
335 return g_iofile(L, IO_OUTPUT, "w"); in io_output()
339 static int io_readline (lua_State *L);
357 static void aux_lines (lua_State *L, int toclose) { in aux_lines() argument
358 int n = lua_gettop(L) - 1; /* number of arguments to read */ in aux_lines()
359 luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); in aux_lines()
360 lua_pushvalue(L, 1); /* file */ in aux_lines()
361 lua_pushinteger(L, n); /* number of arguments to read */ in aux_lines()
362 lua_pushboolean(L, toclose); /* close/not close file when finished */ in aux_lines()
363 lua_rotate(L, 2, 3); /* move the three values to their positions */ in aux_lines()
364 lua_pushcclosure(L, io_readline, 3 + n); in aux_lines()
368 static int f_lines (lua_State *L) { in f_lines() argument
369 tofile(L); /* check that it's a valid file handle */ in f_lines()
370 aux_lines(L, 0); in f_lines()
380 static int io_lines (lua_State *L) { in io_lines() argument
382 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ in io_lines()
383 if (lua_isnil(L, 1)) { /* no file name? */ in io_lines()
384 lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ in io_lines()
385 lua_replace(L, 1); /* put it at index 1 */ in io_lines()
386 tofile(L); /* check that it's a valid file handle */ in io_lines()
390 const char *filename = luaL_checkstring(L, 1); in io_lines()
391 opencheck(L, filename, "r"); in io_lines()
392 lua_replace(L, 1); /* put file at index 1 */ in io_lines()
395 aux_lines(L, toclose); /* push iteration function */ in io_lines()
397 lua_pushnil(L); /* state */ in io_lines()
398 lua_pushnil(L); /* control */ in io_lines()
399 lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */ in io_lines()
471 static int read_number (lua_State *L, FILE *f) { in read_number() argument
496 if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ in read_number()
499 lua_pushnil(L); /* "result" to be removed */ in read_number()
505 static int test_eof (lua_State *L, FILE *f) { in test_eof() argument
508 lua_pushliteral(L, ""); in test_eof()
513 static int read_line (lua_State *L, FILE *f, int chop) { in read_line() argument
516 luaL_buffinit(L, &b); in read_line()
530 return (c == '\n' || lua_rawlen(L, -1) > 0); in read_line()
534 static void read_all (lua_State *L, FILE *f) { in read_all() argument
537 luaL_buffinit(L, &b); in read_all()
547 static int read_chars (lua_State *L, FILE *f, size_t n) { in read_chars() argument
551 luaL_buffinit(L, &b); in read_chars()
560 static int g_read (lua_State *L, FILE *f, int first) { in g_read() argument
561 int nargs = lua_gettop(L) - 1; in g_read()
565 success = read_line(L, f, 1); in g_read()
570 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); in g_read()
573 if (lua_type(L, n) == LUA_TNUMBER) { in g_read()
574 size_t l = (size_t)luaL_checkinteger(L, n); in g_read()
575 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); in g_read()
578 const char *p = luaL_checkstring(L, n); in g_read()
582 success = read_number(L, f); in g_read()
585 success = read_line(L, f, 1); in g_read()
588 success = read_line(L, f, 0); in g_read()
591 read_all(L, f); /* read entire file */ in g_read()
595 return luaL_argerror(L, n, "invalid format"); in g_read()
601 return luaL_fileresult(L, 0, NULL); in g_read()
603 lua_pop(L, 1); /* remove last result */ in g_read()
604 luaL_pushfail(L); /* push nil instead */ in g_read()
610 static int io_read (lua_State *L) { in io_read() argument
611 return g_read(L, getiofile(L, IO_INPUT), 1); in io_read()
615 static int f_read (lua_State *L) { in f_read() argument
616 return g_read(L, tofile(L), 2); in f_read()
623 static int io_readline (lua_State *L) { in io_readline() argument
624 LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); in io_readline()
626 int n = (int)lua_tointeger(L, lua_upvalueindex(2)); in io_readline()
628 return luaL_error(L, "file is already closed"); in io_readline()
629 lua_settop(L , 1); in io_readline()
630 luaL_checkstack(L, n, "too many arguments"); in io_readline()
632 lua_pushvalue(L, lua_upvalueindex(3 + i)); in io_readline()
633 n = g_read(L, p->f, 2); /* 'n' is number of results */ in io_readline()
635 if (lua_toboolean(L, -n)) /* read at least one value? */ in io_readline()
640 return luaL_error(L, "%s", lua_tostring(L, -n + 1)); in io_readline()
642 if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ in io_readline()
643 lua_settop(L, 0); /* clear stack */ in io_readline()
644 lua_pushvalue(L, lua_upvalueindex(1)); /* push file at index 1 */ in io_readline()
645 aux_close(L); /* close it */ in io_readline()
654 static int g_write (lua_State *L, FILE *f, int arg) { in g_write() argument
655 int nargs = lua_gettop(L) - arg; in g_write()
658 if (lua_type(L, arg) == LUA_TNUMBER) { in g_write()
660 int len = lua_isinteger(L, arg) in g_write()
662 (LUAI_UACINT)lua_tointeger(L, arg)) in g_write()
664 (LUAI_UACNUMBER)lua_tonumber(L, arg)); in g_write()
669 const char *s = luaL_checklstring(L, arg, &l); in g_write()
674 else return luaL_fileresult(L, status, NULL); in g_write()
678 static int io_write (lua_State *L) { in io_write() argument
679 return g_write(L, getiofile(L, IO_OUTPUT), 1); in io_write()
683 static int f_write (lua_State *L) { in f_write() argument
684 FILE *f = tofile(L); in f_write()
685 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ in f_write()
686 return g_write(L, f, 2); in f_write()
690 static int f_seek (lua_State *L) { in f_seek() argument
693 FILE *f = tofile(L); in f_seek()
694 int op = luaL_checkoption(L, 2, "cur", modenames); in f_seek()
695 lua_Integer p3 = luaL_optinteger(L, 3, 0); in f_seek()
697 luaL_argcheck(L, (lua_Integer)offset == p3, 3, in f_seek()
701 return luaL_fileresult(L, 0, NULL); /* error */ in f_seek()
703 lua_pushinteger(L, (lua_Integer)l_ftell(f)); in f_seek()
709 static int f_setvbuf (lua_State *L) { in f_setvbuf() argument
712 FILE *f = tofile(L); in f_setvbuf()
713 int op = luaL_checkoption(L, 2, NULL, modenames); in f_setvbuf()
714 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); in f_setvbuf()
716 return luaL_fileresult(L, res == 0, NULL); in f_setvbuf()
721 static int io_flush (lua_State *L) { in io_flush() argument
722 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); in io_flush()
726 static int f_flush (lua_State *L) { in f_flush() argument
727 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); in f_flush()
777 static void createmeta (lua_State *L) { in createmeta() argument
778 luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */ in createmeta()
779 luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */ in createmeta()
780 luaL_newlibtable(L, meth); /* create method table */ in createmeta()
781 luaL_setfuncs(L, meth, 0); /* add file methods to method table */ in createmeta()
782 lua_setfield(L, -2, "__index"); /* metatable.__index = method table */ in createmeta()
783 lua_pop(L, 1); /* pop metatable */ in createmeta()
790 static int io_noclose (lua_State *L) { in io_noclose() argument
791 LStream *p = tolstream(L); in io_noclose()
793 luaL_pushfail(L); in io_noclose()
794 lua_pushliteral(L, "cannot close standard file"); in io_noclose()
799 static void createstdfile (lua_State *L, FILE *f, const char *k, in createstdfile() argument
801 LStream *p = newprefile(L); in createstdfile()
805 lua_pushvalue(L, -1); in createstdfile()
806 lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ in createstdfile()
808 lua_setfield(L, -2, fname); /* add file to module */ in createstdfile()
812 LUAMOD_API int luaopen_io (lua_State *L) { in luaopen_io() argument
813 luaL_newlib(L, iolib); /* new module */ in luaopen_io()
814 createmeta(L); in luaopen_io()
816 createstdfile(L, stdin, IO_INPUT, "stdin"); in luaopen_io()
817 createstdfile(L, stdout, IO_OUTPUT, "stdout"); in luaopen_io()
818 createstdfile(L, stderr, NULL, "stderr"); in luaopen_io()