Lines Matching refs:L

59 #define l_popen(L,c,m)		(fflush(NULL), popen(c,m))  argument
60 #define l_pclose(L,file) (pclose(file)) argument
64 #define l_popen(L,c,m) (_popen(c,m)) argument
65 #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
158 #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) argument
163 static int io_type (lua_State *L) { in io_type() argument
165 luaL_checkany(L, 1); in io_type()
166 p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); in io_type()
168 luaL_pushfail(L); /* not a file */ in io_type()
170 lua_pushliteral(L, "closed file"); in io_type()
172 lua_pushliteral(L, "file"); in io_type()
177 static int f_tostring (lua_State *L) { in f_tostring() argument
178 LStream *p = tolstream(L); in f_tostring()
180 lua_pushliteral(L, "file (closed)"); in f_tostring()
182 lua_pushfstring(L, "file (%p)", p->f); in f_tostring()
187 static FILE *tofile (lua_State *L) { in tofile() argument
188 LStream *p = tolstream(L); in tofile()
190 luaL_error(L, "attempt to use a closed file"); in tofile()
201 static LStream *newprefile (lua_State *L) { in newprefile() argument
202 LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0); in newprefile()
204 luaL_setmetatable(L, LUA_FILEHANDLE); in newprefile()
214 static int aux_close (lua_State *L) { in aux_close() argument
215 LStream *p = tolstream(L); in aux_close()
218 return (*cf)(L); /* close it */ in aux_close()
222 static int f_close (lua_State *L) { in f_close() argument
223 tofile(L); /* make sure argument is an open stream */ in f_close()
224 return aux_close(L); in f_close()
228 static int io_close (lua_State *L) { in io_close() argument
229 if (lua_isnone(L, 1)) /* no argument? */ in io_close()
230 lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */ in io_close()
231 return f_close(L); in io_close()
235 static int f_gc (lua_State *L) { in f_gc() argument
236 LStream *p = tolstream(L); in f_gc()
238 aux_close(L); /* ignore closed and incompletely open files */ in f_gc()
246 static int io_fclose (lua_State *L) { in io_fclose() argument
247 LStream *p = tolstream(L); in io_fclose()
249 return luaL_fileresult(L, (res == 0), NULL); in io_fclose()
253 static LStream *newfile (lua_State *L) { in newfile() argument
254 LStream *p = newprefile(L); in newfile()
261 static void opencheck (lua_State *L, const char *fname, const char *mode) { in opencheck() argument
262 LStream *p = newfile(L); in opencheck()
265 luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); in opencheck()
269 static int io_open (lua_State *L) { in io_open() argument
270 const char *filename = luaL_checkstring(L, 1); in io_open()
271 const char *mode = luaL_optstring(L, 2, "r"); in io_open()
272 LStream *p = newfile(L); in io_open()
274 luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); in io_open()
276 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; in io_open()
283 static int io_pclose (lua_State *L) { in io_pclose() argument
284 LStream *p = tolstream(L); in io_pclose()
286 return luaL_execresult(L, l_pclose(L, p->f)); in io_pclose()
290 static int io_popen (lua_State *L) { in io_popen() argument
291 const char *filename = luaL_checkstring(L, 1); in io_popen()
292 const char *mode = luaL_optstring(L, 2, "r"); in io_popen()
293 LStream *p = newprefile(L); in io_popen()
294 luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); in io_popen()
295 p->f = l_popen(L, filename, mode); in io_popen()
297 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; in io_popen()
301 static int io_tmpfile (lua_State *L) { in io_tmpfile() argument
302 LStream *p = newfile(L); in io_tmpfile()
304 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; in io_tmpfile()
308 static FILE *getiofile (lua_State *L, const char *findex) { in getiofile() argument
310 lua_getfield(L, LUA_REGISTRYINDEX, findex); in getiofile()
311 p = (LStream *)lua_touserdata(L, -1); in getiofile()
313 luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); in getiofile()
318 static int g_iofile (lua_State *L, const char *f, const char *mode) { in g_iofile() argument
319 if (!lua_isnoneornil(L, 1)) { in g_iofile()
320 const char *filename = lua_tostring(L, 1); in g_iofile()
322 opencheck(L, filename, mode); in g_iofile()
324 tofile(L); /* check that it's a valid file handle */ in g_iofile()
325 lua_pushvalue(L, 1); in g_iofile()
327 lua_setfield(L, LUA_REGISTRYINDEX, f); in g_iofile()
330 lua_getfield(L, LUA_REGISTRYINDEX, f); in g_iofile()
335 static int io_input (lua_State *L) { in io_input() argument
336 return g_iofile(L, IO_INPUT, "r"); in io_input()
340 static int io_output (lua_State *L) { in io_output() argument
341 return g_iofile(L, IO_OUTPUT, "w"); in io_output()
345 static int io_readline (lua_State *L);
363 static void aux_lines (lua_State *L, int toclose) { in aux_lines() argument
364 int n = lua_gettop(L) - 1; /* number of arguments to read */ in aux_lines()
365 luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); in aux_lines()
366 lua_pushvalue(L, 1); /* file */ in aux_lines()
367 lua_pushinteger(L, n); /* number of arguments to read */ in aux_lines()
368 lua_pushboolean(L, toclose); /* close/not close file when finished */ in aux_lines()
369 lua_rotate(L, 2, 3); /* move the three values to their positions */ in aux_lines()
370 lua_pushcclosure(L, io_readline, 3 + n); in aux_lines()
374 static int f_lines (lua_State *L) { in f_lines() argument
375 tofile(L); /* check that it's a valid file handle */ in f_lines()
376 aux_lines(L, 0); in f_lines()
386 static int io_lines (lua_State *L) { in io_lines() argument
388 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ in io_lines()
389 if (lua_isnil(L, 1)) { /* no file name? */ in io_lines()
390 lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ in io_lines()
391 lua_replace(L, 1); /* put it at index 1 */ in io_lines()
392 tofile(L); /* check that it's a valid file handle */ in io_lines()
396 const char *filename = luaL_checkstring(L, 1); in io_lines()
397 opencheck(L, filename, "r"); in io_lines()
398 lua_replace(L, 1); /* put file at index 1 */ in io_lines()
401 aux_lines(L, toclose); /* push iteration function */ in io_lines()
403 lua_pushnil(L); /* state */ in io_lines()
404 lua_pushnil(L); /* control */ in io_lines()
405 lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */ in io_lines()
477 static int read_number (lua_State *L, FILE *f) { in read_number() argument
502 if (l_likely(lua_stringtonumber(L, rn.buff))) in read_number()
505 lua_pushnil(L); /* "result" to be removed */ in read_number()
511 static int test_eof (lua_State *L, FILE *f) { in test_eof() argument
514 lua_pushliteral(L, ""); in test_eof()
519 static int read_line (lua_State *L, FILE *f, int chop) { in read_line() argument
522 luaL_buffinit(L, &b); in read_line()
536 return (c == '\n' || lua_rawlen(L, -1) > 0); in read_line()
540 static void read_all (lua_State *L, FILE *f) { in read_all() argument
543 luaL_buffinit(L, &b); in read_all()
553 static int read_chars (lua_State *L, FILE *f, size_t n) { in read_chars() argument
557 luaL_buffinit(L, &b); in read_chars()
566 static int g_read (lua_State *L, FILE *f, int first) { in g_read() argument
567 int nargs = lua_gettop(L) - 1; in g_read()
571 success = read_line(L, f, 1); in g_read()
576 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); in g_read()
579 if (lua_type(L, n) == LUA_TNUMBER) { in g_read()
580 size_t l = (size_t)luaL_checkinteger(L, n); in g_read()
581 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); in g_read()
584 const char *p = luaL_checkstring(L, n); in g_read()
588 success = read_number(L, f); in g_read()
591 success = read_line(L, f, 1); in g_read()
594 success = read_line(L, f, 0); in g_read()
597 read_all(L, f); /* read entire file */ in g_read()
601 return luaL_argerror(L, n, "invalid format"); in g_read()
607 return luaL_fileresult(L, 0, NULL); in g_read()
609 lua_pop(L, 1); /* remove last result */ in g_read()
610 luaL_pushfail(L); /* push nil instead */ in g_read()
616 static int io_read (lua_State *L) { in io_read() argument
617 return g_read(L, getiofile(L, IO_INPUT), 1); in io_read()
621 static int f_read (lua_State *L) { in f_read() argument
622 return g_read(L, tofile(L), 2); in f_read()
629 static int io_readline (lua_State *L) { in io_readline() argument
630 LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); in io_readline()
632 int n = (int)lua_tointeger(L, lua_upvalueindex(2)); in io_readline()
634 return luaL_error(L, "file is already closed"); in io_readline()
635 lua_settop(L , 1); in io_readline()
636 luaL_checkstack(L, n, "too many arguments"); in io_readline()
638 lua_pushvalue(L, lua_upvalueindex(3 + i)); in io_readline()
639 n = g_read(L, p->f, 2); /* 'n' is number of results */ in io_readline()
641 if (lua_toboolean(L, -n)) /* read at least one value? */ in io_readline()
646 return luaL_error(L, "%s", lua_tostring(L, -n + 1)); in io_readline()
648 if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ in io_readline()
649 lua_settop(L, 0); /* clear stack */ in io_readline()
650 lua_pushvalue(L, lua_upvalueindex(1)); /* push file at index 1 */ in io_readline()
651 aux_close(L); /* close it */ in io_readline()
660 static int g_write (lua_State *L, FILE *f, int arg) { in g_write() argument
661 int nargs = lua_gettop(L) - arg; in g_write()
664 if (lua_type(L, arg) == LUA_TNUMBER) { in g_write()
666 int len = lua_isinteger(L, arg) in g_write()
668 (LUAI_UACINT)lua_tointeger(L, arg)) in g_write()
670 (LUAI_UACNUMBER)lua_tonumber(L, arg)); in g_write()
675 const char *s = luaL_checklstring(L, arg, &l); in g_write()
681 else return luaL_fileresult(L, status, NULL); in g_write()
685 static int io_write (lua_State *L) { in io_write() argument
686 return g_write(L, getiofile(L, IO_OUTPUT), 1); in io_write()
690 static int f_write (lua_State *L) { in f_write() argument
691 FILE *f = tofile(L); in f_write()
692 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ in f_write()
693 return g_write(L, f, 2); in f_write()
697 static int f_seek (lua_State *L) { in f_seek() argument
700 FILE *f = tofile(L); in f_seek()
701 int op = luaL_checkoption(L, 2, "cur", modenames); in f_seek()
702 lua_Integer p3 = luaL_optinteger(L, 3, 0); in f_seek()
704 luaL_argcheck(L, (lua_Integer)offset == p3, 3, in f_seek()
708 return luaL_fileresult(L, 0, NULL); /* error */ in f_seek()
710 lua_pushinteger(L, (lua_Integer)l_ftell(f)); in f_seek()
716 static int f_setvbuf (lua_State *L) { in f_setvbuf() argument
719 FILE *f = tofile(L); in f_setvbuf()
720 int op = luaL_checkoption(L, 2, NULL, modenames); in f_setvbuf()
721 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); in f_setvbuf()
723 return luaL_fileresult(L, res == 0, NULL); in f_setvbuf()
728 static int io_flush (lua_State *L) { in io_flush() argument
729 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); in io_flush()
733 static int f_flush (lua_State *L) { in f_flush() argument
734 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); in f_flush()
784 static void createmeta (lua_State *L) { in createmeta() argument
785 luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */ in createmeta()
786 luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */ in createmeta()
787 luaL_newlibtable(L, meth); /* create method table */ in createmeta()
788 luaL_setfuncs(L, meth, 0); /* add file methods to method table */ in createmeta()
789 lua_setfield(L, -2, "__index"); /* metatable.__index = method table */ in createmeta()
790 lua_pop(L, 1); /* pop metatable */ in createmeta()
797 static int io_noclose (lua_State *L) { in io_noclose() argument
798 LStream *p = tolstream(L); in io_noclose()
800 luaL_pushfail(L); in io_noclose()
801 lua_pushliteral(L, "cannot close standard file"); in io_noclose()
806 static void createstdfile (lua_State *L, FILE *f, const char *k, in createstdfile() argument
808 LStream *p = newprefile(L); in createstdfile()
812 lua_pushvalue(L, -1); in createstdfile()
813 lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ in createstdfile()
815 lua_setfield(L, -2, fname); /* add file to module */ in createstdfile()
819 LUAMOD_API int luaopen_io (lua_State *L) { in luaopen_io() argument
820 luaL_newlib(L, iolib); /* new module */ in luaopen_io()
821 createmeta(L); in luaopen_io()
823 createstdfile(L, stdin, IO_INPUT, "stdin"); in luaopen_io()
824 createstdfile(L, stdout, IO_OUTPUT, "stdout"); in luaopen_io()
825 createstdfile(L, stderr, NULL, "stderr"); in luaopen_io()