Lines Matching refs:L
58 #define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) argument
59 #define lua_pclose(L,file) ((void)L, pclose(file)) argument
63 #define lua_popen(L,c,m) ((void)L, _popen(c,m)) argument
64 #define lua_pclose(L,file) ((void)L, _pclose(file)) argument
69 #define lua_popen(L,c,m) ((void)((void)c, m), \ argument
70 luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
71 #define lua_pclose(L,file) ((void)((void)L, file), -1) argument
125 #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) argument
130 static int io_type (lua_State *L) { in io_type() argument
132 luaL_checkany(L, 1); in io_type()
133 p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); in io_type()
135 lua_pushnil(L); /* not a file */ in io_type()
137 lua_pushliteral(L, "closed file"); in io_type()
139 lua_pushliteral(L, "file"); in io_type()
144 static int f_tostring (lua_State *L) { in f_tostring() argument
145 LStream *p = tolstream(L); in f_tostring()
147 lua_pushliteral(L, "file (closed)"); in f_tostring()
149 lua_pushfstring(L, "file (%p)", p->f); in f_tostring()
154 static FILE *tofile (lua_State *L) { in tofile() argument
155 LStream *p = tolstream(L); in tofile()
157 luaL_error(L, "attempt to use a closed file"); in tofile()
168 static LStream *newprefile (lua_State *L) { in newprefile() argument
169 LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); in newprefile()
171 luaL_setmetatable(L, LUA_FILEHANDLE); in newprefile()
176 static int aux_close (lua_State *L) { in aux_close() argument
177 LStream *p = tolstream(L); in aux_close()
180 return (*cf)(L); /* close it */ in aux_close()
184 static int io_close (lua_State *L) { in io_close() argument
185 if (lua_isnone(L, 1)) /* no argument? */ in io_close()
186 lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ in io_close()
187 tofile(L); /* make sure argument is an open stream */ in io_close()
188 return aux_close(L); in io_close()
192 static int f_gc (lua_State *L) { in f_gc() argument
193 LStream *p = tolstream(L); in f_gc()
195 aux_close(L); /* ignore closed and incompletely open files */ in f_gc()
203 static int io_fclose (lua_State *L) { in io_fclose() argument
204 LStream *p = tolstream(L); in io_fclose()
206 return luaL_fileresult(L, (res == 0), NULL); in io_fclose()
210 static LStream *newfile (lua_State *L) { in newfile() argument
211 LStream *p = newprefile(L); in newfile()
218 static void opencheck (lua_State *L, const char *fname, const char *mode) { in opencheck() argument
219 LStream *p = newfile(L); in opencheck()
222 luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno)); in opencheck()
226 static int io_open (lua_State *L) { in io_open() argument
227 const char *filename = luaL_checkstring(L, 1); in io_open()
228 const char *mode = luaL_optstring(L, 2, "r"); in io_open()
229 LStream *p = newfile(L); in io_open()
231 luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode"); in io_open()
233 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; in io_open()
240 static int io_pclose (lua_State *L) { in io_pclose() argument
241 LStream *p = tolstream(L); in io_pclose()
242 return luaL_execresult(L, lua_pclose(L, p->f)); in io_pclose()
246 static int io_popen (lua_State *L) { in io_popen() argument
247 const char *filename = luaL_checkstring(L, 1); in io_popen()
248 const char *mode = luaL_optstring(L, 2, "r"); in io_popen()
249 LStream *p = newprefile(L); in io_popen()
250 p->f = lua_popen(L, filename, mode); in io_popen()
252 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; in io_popen()
257 static int io_tmpfile (lua_State *L) { in io_tmpfile() argument
258 LStream *p = newfile(L); in io_tmpfile()
260 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; in io_tmpfile()
265 static FILE *getiofile (lua_State *L, const char *findex) { in getiofile() argument
267 lua_getfield(L, LUA_REGISTRYINDEX, findex); in getiofile()
268 p = (LStream *)lua_touserdata(L, -1); in getiofile()
270 luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX)); in getiofile()
275 static int g_iofile (lua_State *L, const char *f, const char *mode) { in g_iofile() argument
276 if (!lua_isnoneornil(L, 1)) { in g_iofile()
277 const char *filename = lua_tostring(L, 1); in g_iofile()
279 opencheck(L, filename, mode); in g_iofile()
281 tofile(L); /* check that it's a valid file handle */ in g_iofile()
282 lua_pushvalue(L, 1); in g_iofile()
284 lua_setfield(L, LUA_REGISTRYINDEX, f); in g_iofile()
287 lua_getfield(L, LUA_REGISTRYINDEX, f); in g_iofile()
292 static int io_input (lua_State *L) { in io_input() argument
293 return g_iofile(L, IO_INPUT, "r"); in io_input()
297 static int io_output (lua_State *L) { in io_output() argument
298 return g_iofile(L, IO_OUTPUT, "w"); in io_output()
302 static int io_readline (lua_State *L);
305 static void aux_lines (lua_State *L, int toclose) { in aux_lines() argument
307 int n = lua_gettop(L) - 1; /* number of arguments to read */ in aux_lines()
309 luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options"); in aux_lines()
310 lua_pushvalue(L, 1); /* file handle */ in aux_lines()
311 lua_pushinteger(L, n); /* number of arguments to read */ in aux_lines()
312 lua_pushboolean(L, toclose); /* close/not close file when finished */ in aux_lines()
313 for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */ in aux_lines()
314 lua_pushcclosure(L, io_readline, 3 + n); in aux_lines()
318 static int f_lines (lua_State *L) { in f_lines() argument
319 tofile(L); /* check that it's a valid file handle */ in f_lines()
320 aux_lines(L, 0); in f_lines()
325 static int io_lines (lua_State *L) { in io_lines() argument
327 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ in io_lines()
328 if (lua_isnil(L, 1)) { /* no file name? */ in io_lines()
329 lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ in io_lines()
330 lua_replace(L, 1); /* put it at index 1 */ in io_lines()
331 tofile(L); /* check that it's a valid file handle */ in io_lines()
335 const char *filename = luaL_checkstring(L, 1); in io_lines()
336 opencheck(L, filename, "r"); in io_lines()
337 lua_replace(L, 1); /* put file at index 1 */ in io_lines()
340 aux_lines(L, toclose); in io_lines()
353 static int read_number (lua_State *L, FILE *f) { in read_number() argument
356 lua_pushnumber(L, d); in read_number()
360 lua_pushnil(L); /* "result" to be removed */ in read_number()
366 static int test_eof (lua_State *L, FILE *f) { in test_eof() argument
369 lua_pushlstring(L, NULL, 0); in test_eof()
373 static int test_eof (lua_State *L, FILE *f) { in test_eof() argument
375 lua_pushlstring(L, NULL, 0); in test_eof()
381 static int read_line (lua_State *L, FILE *f, int chop) { in read_line() argument
383 luaL_buffinit(L, &b); in read_line()
389 return (lua_rawlen(L, -1) > 0); /* check whether read something */ in read_line()
405 static void read_all (lua_State *L, FILE *f) { in read_all() argument
408 luaL_buffinit(L, &b); in read_all()
421 static int read_chars (lua_State *L, FILE *f, size_t n) { in read_chars() argument
425 luaL_buffinit(L, &b); in read_chars()
434 static int g_read (lua_State *L, FILE *f, int first) { in g_read() argument
435 int nargs = lua_gettop(L) - 1; in g_read()
440 success = read_line(L, f, 1); in g_read()
444 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); in g_read()
447 if (lua_type(L, n) == LUA_TNUMBER) { in g_read()
448 size_t l = (size_t)lua_tointeger(L, n); in g_read()
449 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); in g_read()
452 const char *p = lua_tostring(L, n); in g_read()
453 luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); in g_read()
457 success = read_number(L, f); in g_read()
459 return luaL_argerror(L, n, "\"*number\" not supported"); in g_read()
463 success = read_line(L, f, 1); in g_read()
466 success = read_line(L, f, 0); in g_read()
469 read_all(L, f); /* read entire file */ in g_read()
473 return luaL_argerror(L, n, "invalid format"); in g_read()
479 return luaL_fileresult(L, 0, NULL); in g_read()
481 lua_pop(L, 1); /* remove last result */ in g_read()
482 lua_pushnil(L); /* push nil instead */ in g_read()
488 static int io_read (lua_State *L) { in io_read() argument
489 return g_read(L, getiofile(L, IO_INPUT), 1); in io_read()
493 static int f_read (lua_State *L) { in f_read() argument
494 return g_read(L, tofile(L), 2); in f_read()
498 static int io_readline (lua_State *L) { in io_readline() argument
499 LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); in io_readline()
501 int n = (int)lua_tointeger(L, lua_upvalueindex(2)); in io_readline()
503 return luaL_error(L, "file is already closed"); in io_readline()
504 lua_settop(L , 1); in io_readline()
506 lua_pushvalue(L, lua_upvalueindex(3 + i)); in io_readline()
507 n = g_read(L, p->f, 2); /* 'n' is number of results */ in io_readline()
509 if (!lua_isnil(L, -n)) /* read at least one value? */ in io_readline()
514 return luaL_error(L, "%s", lua_tostring(L, -n + 1)); in io_readline()
516 if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ in io_readline()
517 lua_settop(L, 0); in io_readline()
518 lua_pushvalue(L, lua_upvalueindex(1)); in io_readline()
519 aux_close(L); /* close it */ in io_readline()
528 static int g_write (lua_State *L, FILE *f, int arg) { in g_write() argument
529 int nargs = lua_gettop(L) - arg; in g_write()
532 if (lua_type(L, arg) == LUA_TNUMBER) { in g_write()
535 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; in g_write()
539 const char *s = luaL_checklstring(L, arg, &l); in g_write()
544 else return luaL_fileresult(L, status, NULL); in g_write()
548 static int io_write (lua_State *L) { in io_write() argument
549 return g_write(L, getiofile(L, IO_OUTPUT), 1); in io_write()
553 static int f_write (lua_State *L) { in f_write() argument
554 FILE *f = tofile(L); in f_write()
555 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ in f_write()
556 return g_write(L, f, 2); in f_write()
561 static int f_seek (lua_State *L) { in f_seek() argument
564 FILE *f = tofile(L); in f_seek()
565 int op = luaL_checkoption(L, 2, "cur", modenames); in f_seek()
566 lua_Number p3 = luaL_optnumber(L, 3, 0); in f_seek()
568 luaL_argcheck(L, (lua_Number)offset == p3, 3, in f_seek()
572 return luaL_fileresult(L, 0, NULL); /* error */ in f_seek()
574 lua_pushnumber(L, (lua_Number)l_ftell(f)); in f_seek()
580 static int f_setvbuf (lua_State *L) { in f_setvbuf() argument
583 FILE *f = tofile(L); in f_setvbuf()
584 int op = luaL_checkoption(L, 2, NULL, modenames); in f_setvbuf()
585 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); in f_setvbuf()
587 return luaL_fileresult(L, res == 0, NULL); in f_setvbuf()
593 static int io_flush (lua_State *L) { in io_flush() argument
594 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); in io_flush()
598 static int f_flush (lua_State *L) { in f_flush() argument
599 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); in f_flush()
643 static void createmeta (lua_State *L) { in createmeta() argument
644 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ in createmeta()
645 lua_pushvalue(L, -1); /* push metatable */ in createmeta()
646 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ in createmeta()
647 luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */ in createmeta()
648 lua_pop(L, 1); /* pop new metatable */ in createmeta()
655 static int io_noclose (lua_State *L) { in io_noclose() argument
656 LStream *p = tolstream(L); in io_noclose()
658 lua_pushnil(L); in io_noclose()
659 lua_pushliteral(L, "cannot close standard file"); in io_noclose()
664 static void createstdfile (lua_State *L, FILE *f, const char *k, in createstdfile() argument
666 LStream *p = newprefile(L); in createstdfile()
670 lua_pushvalue(L, -1); in createstdfile()
671 lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ in createstdfile()
673 lua_setfield(L, -2, fname); /* add file to module */ in createstdfile()
677 LUAMOD_API int luaopen_io (lua_State *L) { in luaopen_io() argument
678 luaL_newlib(L, iolib); /* new module */ in luaopen_io()
679 createmeta(L); in luaopen_io()
681 createstdfile(L, stdin, IO_INPUT, "stdin"); in luaopen_io()
682 createstdfile(L, stdout, IO_OUTPUT, "stdout"); in luaopen_io()
683 createstdfile(L, stderr, NULL, "stderr"); in luaopen_io()