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
124 #define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) argument
129 static int io_type (lua_State *L) { in io_type() argument
131 luaL_checkany(L, 1); in io_type()
132 p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); in io_type()
134 lua_pushnil(L); /* not a file */ in io_type()
136 lua_pushliteral(L, "closed file"); in io_type()
138 lua_pushliteral(L, "file"); in io_type()
143 static int f_tostring (lua_State *L) { in f_tostring() argument
144 LStream *p = tolstream(L); in f_tostring()
146 lua_pushliteral(L, "file (closed)"); in f_tostring()
148 lua_pushfstring(L, "file (%p)", p->f); in f_tostring()
153 static FILE *tofile (lua_State *L) { in tofile() argument
154 LStream *p = tolstream(L); in tofile()
156 luaL_error(L, "attempt to use a closed file"); in tofile()
167 static LStream *newprefile (lua_State *L) { in newprefile() argument
168 LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); in newprefile()
170 luaL_setmetatable(L, LUA_FILEHANDLE); in newprefile()
175 static int aux_close (lua_State *L) { in aux_close() argument
176 LStream *p = tolstream(L); in aux_close()
179 return (*cf)(L); /* close it */ in aux_close()
183 static int io_close (lua_State *L) { in io_close() argument
184 if (lua_isnone(L, 1)) /* no argument? */ in io_close()
185 lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ in io_close()
186 tofile(L); /* make sure argument is an open stream */ in io_close()
187 return aux_close(L); in io_close()
191 static int f_gc (lua_State *L) { in f_gc() argument
192 LStream *p = tolstream(L); in f_gc()
194 aux_close(L); /* ignore closed and incompletely open files */ in f_gc()
202 static int io_fclose (lua_State *L) { in io_fclose() argument
203 LStream *p = tolstream(L); in io_fclose()
205 return luaL_fileresult(L, (res == 0), NULL); in io_fclose()
209 static LStream *newfile (lua_State *L) { in newfile() argument
210 LStream *p = newprefile(L); in newfile()
217 static void opencheck (lua_State *L, const char *fname, const char *mode) { in opencheck() argument
218 LStream *p = newfile(L); in opencheck()
221 luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno)); in opencheck()
225 static int io_open (lua_State *L) { in io_open() argument
226 const char *filename = luaL_checkstring(L, 1); in io_open()
227 const char *mode = luaL_optstring(L, 2, "r"); in io_open()
228 LStream *p = newfile(L); in io_open()
230 luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode"); in io_open()
232 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; in io_open()
239 static int io_pclose (lua_State *L) { in io_pclose() argument
240 LStream *p = tolstream(L); in io_pclose()
241 return luaL_execresult(L, lua_pclose(L, p->f)); in io_pclose()
245 static int io_popen (lua_State *L) { in io_popen() argument
246 const char *filename = luaL_checkstring(L, 1); in io_popen()
247 const char *mode = luaL_optstring(L, 2, "r"); in io_popen()
248 LStream *p = newprefile(L); in io_popen()
249 p->f = lua_popen(L, filename, mode); in io_popen()
251 return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; in io_popen()
255 static int io_tmpfile (lua_State *L) { in io_tmpfile() argument
256 LStream *p = newfile(L); in io_tmpfile()
258 return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; in io_tmpfile()
262 static FILE *getiofile (lua_State *L, const char *findex) { in getiofile() argument
264 lua_getfield(L, LUA_REGISTRYINDEX, findex); in getiofile()
265 p = (LStream *)lua_touserdata(L, -1); in getiofile()
267 luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX)); in getiofile()
272 static int g_iofile (lua_State *L, const char *f, const char *mode) { in g_iofile() argument
273 if (!lua_isnoneornil(L, 1)) { in g_iofile()
274 const char *filename = lua_tostring(L, 1); in g_iofile()
276 opencheck(L, filename, mode); in g_iofile()
278 tofile(L); /* check that it's a valid file handle */ in g_iofile()
279 lua_pushvalue(L, 1); in g_iofile()
281 lua_setfield(L, LUA_REGISTRYINDEX, f); in g_iofile()
284 lua_getfield(L, LUA_REGISTRYINDEX, f); in g_iofile()
289 static int io_input (lua_State *L) { in io_input() argument
290 return g_iofile(L, IO_INPUT, "r"); in io_input()
294 static int io_output (lua_State *L) { in io_output() argument
295 return g_iofile(L, IO_OUTPUT, "w"); in io_output()
299 static int io_readline (lua_State *L);
302 static void aux_lines (lua_State *L, int toclose) { in aux_lines() argument
304 int n = lua_gettop(L) - 1; /* number of arguments to read */ in aux_lines()
306 luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options"); in aux_lines()
307 lua_pushvalue(L, 1); /* file handle */ in aux_lines()
308 lua_pushinteger(L, n); /* number of arguments to read */ in aux_lines()
309 lua_pushboolean(L, toclose); /* close/not close file when finished */ in aux_lines()
310 for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */ in aux_lines()
311 lua_pushcclosure(L, io_readline, 3 + n); in aux_lines()
315 static int f_lines (lua_State *L) { in f_lines() argument
316 tofile(L); /* check that it's a valid file handle */ in f_lines()
317 aux_lines(L, 0); in f_lines()
322 static int io_lines (lua_State *L) { in io_lines() argument
324 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ in io_lines()
325 if (lua_isnil(L, 1)) { /* no file name? */ in io_lines()
326 lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ in io_lines()
327 lua_replace(L, 1); /* put it at index 1 */ in io_lines()
328 tofile(L); /* check that it's a valid file handle */ in io_lines()
332 const char *filename = luaL_checkstring(L, 1); in io_lines()
333 opencheck(L, filename, "r"); in io_lines()
334 lua_replace(L, 1); /* put file at index 1 */ in io_lines()
337 aux_lines(L, toclose); in io_lines()
349 static int read_number (lua_State *L, FILE *f) { in read_number() argument
352 lua_pushnumber(L, d); in read_number()
356 lua_pushnil(L); /* "result" to be removed */ in read_number()
362 static int test_eof (lua_State *L, FILE *f) { in test_eof() argument
365 lua_pushlstring(L, NULL, 0); in test_eof()
370 static int read_line (lua_State *L, FILE *f, int chop) { in read_line() argument
372 luaL_buffinit(L, &b); in read_line()
378 return (lua_rawlen(L, -1) > 0); /* check whether read something */ in read_line()
394 static void read_all (lua_State *L, FILE *f) { in read_all() argument
397 luaL_buffinit(L, &b); in read_all()
410 static int read_chars (lua_State *L, FILE *f, size_t n) { in read_chars() argument
414 luaL_buffinit(L, &b); in read_chars()
423 static int g_read (lua_State *L, FILE *f, int first) { in g_read() argument
424 int nargs = lua_gettop(L) - 1; in g_read()
429 success = read_line(L, f, 1); in g_read()
433 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); in g_read()
436 if (lua_type(L, n) == LUA_TNUMBER) { in g_read()
437 size_t l = (size_t)lua_tointeger(L, n); in g_read()
438 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); in g_read()
441 const char *p = lua_tostring(L, n); in g_read()
442 luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); in g_read()
445 success = read_number(L, f); in g_read()
448 success = read_line(L, f, 1); in g_read()
451 success = read_line(L, f, 0); in g_read()
454 read_all(L, f); /* read entire file */ in g_read()
458 return luaL_argerror(L, n, "invalid format"); in g_read()
464 return luaL_fileresult(L, 0, NULL); in g_read()
466 lua_pop(L, 1); /* remove last result */ in g_read()
467 lua_pushnil(L); /* push nil instead */ in g_read()
473 static int io_read (lua_State *L) { in io_read() argument
474 return g_read(L, getiofile(L, IO_INPUT), 1); in io_read()
478 static int f_read (lua_State *L) { in f_read() argument
479 return g_read(L, tofile(L), 2); in f_read()
483 static int io_readline (lua_State *L) { in io_readline() argument
484 LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); in io_readline()
486 int n = (int)lua_tointeger(L, lua_upvalueindex(2)); in io_readline()
488 return luaL_error(L, "file is already closed"); in io_readline()
489 lua_settop(L , 1); in io_readline()
491 lua_pushvalue(L, lua_upvalueindex(3 + i)); in io_readline()
492 n = g_read(L, p->f, 2); /* 'n' is number of results */ in io_readline()
494 if (!lua_isnil(L, -n)) /* read at least one value? */ in io_readline()
499 return luaL_error(L, "%s", lua_tostring(L, -n + 1)); in io_readline()
501 if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ in io_readline()
502 lua_settop(L, 0); in io_readline()
503 lua_pushvalue(L, lua_upvalueindex(1)); in io_readline()
504 aux_close(L); /* close it */ in io_readline()
513 static int g_write (lua_State *L, FILE *f, int arg) { in g_write() argument
514 int nargs = lua_gettop(L) - arg; in g_write()
517 if (lua_type(L, arg) == LUA_TNUMBER) { in g_write()
520 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; in g_write()
524 const char *s = luaL_checklstring(L, arg, &l); in g_write()
529 else return luaL_fileresult(L, status, NULL); in g_write()
533 static int io_write (lua_State *L) { in io_write() argument
534 return g_write(L, getiofile(L, IO_OUTPUT), 1); in io_write()
538 static int f_write (lua_State *L) { in f_write() argument
539 FILE *f = tofile(L); in f_write()
540 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ in f_write()
541 return g_write(L, f, 2); in f_write()
545 static int f_seek (lua_State *L) { in f_seek() argument
548 FILE *f = tofile(L); in f_seek()
549 int op = luaL_checkoption(L, 2, "cur", modenames); in f_seek()
550 lua_Number p3 = luaL_optnumber(L, 3, 0); in f_seek()
552 luaL_argcheck(L, (lua_Number)offset == p3, 3, in f_seek()
556 return luaL_fileresult(L, 0, NULL); /* error */ in f_seek()
558 lua_pushnumber(L, (lua_Number)l_ftell(f)); in f_seek()
564 static int f_setvbuf (lua_State *L) { in f_setvbuf() argument
567 FILE *f = tofile(L); in f_setvbuf()
568 int op = luaL_checkoption(L, 2, NULL, modenames); in f_setvbuf()
569 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); in f_setvbuf()
571 return luaL_fileresult(L, res == 0, NULL); in f_setvbuf()
576 static int io_flush (lua_State *L) { in io_flush() argument
577 return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); in io_flush()
581 static int f_flush (lua_State *L) { in f_flush() argument
582 return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); in f_flush()
622 static void createmeta (lua_State *L) { in createmeta() argument
623 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ in createmeta()
624 lua_pushvalue(L, -1); /* push metatable */ in createmeta()
625 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ in createmeta()
626 luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */ in createmeta()
627 lua_pop(L, 1); /* pop new metatable */ in createmeta()
634 static int io_noclose (lua_State *L) { in io_noclose() argument
635 LStream *p = tolstream(L); in io_noclose()
637 lua_pushnil(L); in io_noclose()
638 lua_pushliteral(L, "cannot close standard file"); in io_noclose()
643 static void createstdfile (lua_State *L, FILE *f, const char *k, in createstdfile() argument
645 LStream *p = newprefile(L); in createstdfile()
649 lua_pushvalue(L, -1); in createstdfile()
650 lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ in createstdfile()
652 lua_setfield(L, -2, fname); /* add file to module */ in createstdfile()
656 LUAMOD_API int luaopen_io (lua_State *L) { in luaopen_io() argument
657 luaL_newlib(L, iolib); /* new module */ in luaopen_io()
658 createmeta(L); in luaopen_io()
660 createstdfile(L, stdin, IO_INPUT, "stdin"); in luaopen_io()
661 createstdfile(L, stdout, IO_OUTPUT, "stdout"); in luaopen_io()
662 createstdfile(L, stderr, NULL, "stderr"); in luaopen_io()