Lines Matching refs:L

30 static int pushresult (lua_State *L, int i, const char *filename) {  in pushresult()  argument
33 lua_pushboolean(L, 1); in pushresult()
37 lua_pushnil(L); in pushresult()
39 lua_pushfstring(L, "%s: %s", filename, strerror(en)); in pushresult()
41 lua_pushfstring(L, "%s", strerror(en)); in pushresult()
42 lua_pushinteger(L, en); in pushresult()
48 static void fileerror (lua_State *L, int arg, const char *filename) { in fileerror() argument
49 lua_pushfstring(L, "%s: %s", filename, strerror(errno)); in fileerror()
50 luaL_argerror(L, arg, lua_tostring(L, -1)); in fileerror()
54 #define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE)) argument
57 static int io_type (lua_State *L) { in io_type() argument
59 luaL_checkany(L, 1); in io_type()
60 ud = lua_touserdata(L, 1); in io_type()
61 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); in io_type()
62 if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1)) in io_type()
63 lua_pushnil(L); /* not a file */ in io_type()
65 lua_pushliteral(L, "closed file"); in io_type()
67 lua_pushliteral(L, "file"); in io_type()
72 static FILE *tofile (lua_State *L) { in tofile() argument
73 FILE **f = tofilep(L); in tofile()
75 luaL_error(L, "attempt to use a closed file"); in tofile()
86 static FILE **newfile (lua_State *L) { in newfile() argument
87 FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); in newfile()
89 luaL_getmetatable(L, LUA_FILEHANDLE); in newfile()
90 lua_setmetatable(L, -2); in newfile()
98 static int io_noclose (lua_State *L) { in io_noclose() argument
99 lua_pushnil(L); in io_noclose()
100 lua_pushliteral(L, "cannot close standard file"); in io_noclose()
108 static int io_pclose (lua_State *L) { in io_pclose() argument
109 FILE **p = tofilep(L); in io_pclose()
110 int ok = lua_pclose(L, *p); in io_pclose()
112 return pushresult(L, ok, NULL); in io_pclose()
119 static int io_fclose (lua_State *L) { in io_fclose() argument
120 FILE **p = tofilep(L); in io_fclose()
123 return pushresult(L, ok, NULL); in io_fclose()
127 static int aux_close (lua_State *L) { in aux_close() argument
128 lua_getfenv(L, 1); in aux_close()
129 lua_getfield(L, -1, "__close"); in aux_close()
130 return (lua_tocfunction(L, -1))(L); in aux_close()
134 static int io_close (lua_State *L) { in io_close() argument
135 if (lua_isnone(L, 1)) in io_close()
136 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT); in io_close()
137 tofile(L); /* make sure argument is a file */ in io_close()
138 return aux_close(L); in io_close()
142 static int io_gc (lua_State *L) { in io_gc() argument
143 FILE *f = *tofilep(L); in io_gc()
146 aux_close(L); in io_gc()
151 static int io_tostring (lua_State *L) { in io_tostring() argument
152 FILE *f = *tofilep(L); in io_tostring()
154 lua_pushliteral(L, "file (closed)"); in io_tostring()
156 lua_pushfstring(L, "file (%p)", f); in io_tostring()
161 static int io_open (lua_State *L) { in io_open() argument
162 const char *filename = luaL_checkstring(L, 1); in io_open()
163 const char *mode = luaL_optstring(L, 2, "r"); in io_open()
164 FILE **pf = newfile(L); in io_open()
166 return (*pf == NULL) ? pushresult(L, 0, filename) : 1; in io_open()
174 static int io_popen (lua_State *L) { in io_popen() argument
175 const char *filename = luaL_checkstring(L, 1); in io_popen()
176 const char *mode = luaL_optstring(L, 2, "r"); in io_popen()
177 FILE **pf = newfile(L); in io_popen()
178 *pf = lua_popen(L, filename, mode); in io_popen()
179 return (*pf == NULL) ? pushresult(L, 0, filename) : 1; in io_popen()
183 static int io_tmpfile (lua_State *L) { in io_tmpfile() argument
184 FILE **pf = newfile(L); in io_tmpfile()
186 return (*pf == NULL) ? pushresult(L, 0, NULL) : 1; in io_tmpfile()
190 static FILE *getiofile (lua_State *L, int findex) { in getiofile() argument
192 lua_rawgeti(L, LUA_ENVIRONINDEX, findex); in getiofile()
193 f = *(FILE **)lua_touserdata(L, -1); in getiofile()
195 luaL_error(L, "standard %s file is closed", fnames[findex - 1]); in getiofile()
200 static int g_iofile (lua_State *L, int f, const char *mode) { in g_iofile() argument
201 if (!lua_isnoneornil(L, 1)) { in g_iofile()
202 const char *filename = lua_tostring(L, 1); in g_iofile()
204 FILE **pf = newfile(L); in g_iofile()
207 fileerror(L, 1, filename); in g_iofile()
210 tofile(L); /* check that it's a valid file handle */ in g_iofile()
211 lua_pushvalue(L, 1); in g_iofile()
213 lua_rawseti(L, LUA_ENVIRONINDEX, f); in g_iofile()
216 lua_rawgeti(L, LUA_ENVIRONINDEX, f); in g_iofile()
221 static int io_input (lua_State *L) { in io_input() argument
222 return g_iofile(L, IO_INPUT, "r"); in io_input()
226 static int io_output (lua_State *L) { in io_output() argument
227 return g_iofile(L, IO_OUTPUT, "w"); in io_output()
231 static int io_readline (lua_State *L);
234 static void aux_lines (lua_State *L, int idx, int toclose) { in aux_lines() argument
235 lua_pushvalue(L, idx); in aux_lines()
236 lua_pushboolean(L, toclose); /* close/not close file when finished */ in aux_lines()
237 lua_pushcclosure(L, io_readline, 2); in aux_lines()
241 static int f_lines (lua_State *L) { in f_lines() argument
242 tofile(L); /* check that it's a valid file handle */ in f_lines()
243 aux_lines(L, 1, 0); in f_lines()
248 static int io_lines (lua_State *L) { in io_lines() argument
249 if (lua_isnoneornil(L, 1)) { /* no arguments? */ in io_lines()
251 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT); in io_lines()
252 return f_lines(L); in io_lines()
255 const char *filename = luaL_checkstring(L, 1); in io_lines()
256 FILE **pf = newfile(L); in io_lines()
259 fileerror(L, 1, filename); in io_lines()
260 aux_lines(L, lua_gettop(L), 1); in io_lines()
273 static int read_number (lua_State *L, FILE *f) { in read_number() argument
276 lua_pushnumber(L, d); in read_number()
280 lua_pushnil(L); /* "result" to be removed */ in read_number()
286 static int test_eof (lua_State *L, FILE *f) { in test_eof() argument
289 lua_pushlstring(L, NULL, 0); in test_eof()
294 static int read_line (lua_State *L, FILE *f) { in read_line() argument
296 luaL_buffinit(L, &b); in read_line()
302 return (lua_objlen(L, -1) > 0); /* check whether read something */ in read_line()
316 static int read_chars (lua_State *L, FILE *f, size_t n) { in read_chars() argument
320 luaL_buffinit(L, &b); in read_chars()
330 return (n == 0 || lua_objlen(L, -1) > 0); in read_chars()
334 static int g_read (lua_State *L, FILE *f, int first) { in g_read() argument
335 int nargs = lua_gettop(L) - 1; in g_read()
340 success = read_line(L, f); in g_read()
344 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); in g_read()
347 if (lua_type(L, n) == LUA_TNUMBER) { in g_read()
348 size_t l = (size_t)lua_tointeger(L, n); in g_read()
349 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); in g_read()
352 const char *p = lua_tostring(L, n); in g_read()
353 luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); in g_read()
356 success = read_number(L, f); in g_read()
359 success = read_line(L, f); in g_read()
362 read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ in g_read()
366 return luaL_argerror(L, n, "invalid format"); in g_read()
372 return pushresult(L, 0, NULL); in g_read()
374 lua_pop(L, 1); /* remove last result */ in g_read()
375 lua_pushnil(L); /* push nil instead */ in g_read()
381 static int io_read (lua_State *L) { in io_read() argument
382 return g_read(L, getiofile(L, IO_INPUT), 1); in io_read()
386 static int f_read (lua_State *L) { in f_read() argument
387 return g_read(L, tofile(L), 2); in f_read()
391 static int io_readline (lua_State *L) { in io_readline() argument
392 FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); in io_readline()
395 luaL_error(L, "file is already closed"); in io_readline()
396 sucess = read_line(L, f); in io_readline()
398 return luaL_error(L, "%s", strerror(errno)); in io_readline()
401 if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ in io_readline()
402 lua_settop(L, 0); in io_readline()
403 lua_pushvalue(L, lua_upvalueindex(1)); in io_readline()
404 aux_close(L); /* close it */ in io_readline()
413 static int g_write (lua_State *L, FILE *f, int arg) { in g_write() argument
414 int nargs = lua_gettop(L) - 1; in g_write()
417 if (lua_type(L, arg) == LUA_TNUMBER) { in g_write()
420 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; in g_write()
424 const char *s = luaL_checklstring(L, arg, &l); in g_write()
428 return pushresult(L, status, NULL); in g_write()
432 static int io_write (lua_State *L) { in io_write() argument
433 return g_write(L, getiofile(L, IO_OUTPUT), 1); in io_write()
437 static int f_write (lua_State *L) { in f_write() argument
438 return g_write(L, tofile(L), 2); in f_write()
442 static int f_seek (lua_State *L) { in f_seek() argument
445 FILE *f = tofile(L); in f_seek()
446 int op = luaL_checkoption(L, 2, "cur", modenames); in f_seek()
447 long offset = luaL_optlong(L, 3, 0); in f_seek()
450 return pushresult(L, 0, NULL); /* error */ in f_seek()
452 lua_pushinteger(L, ftell(f)); in f_seek()
458 static int f_setvbuf (lua_State *L) { in f_setvbuf() argument
461 FILE *f = tofile(L); in f_setvbuf()
462 int op = luaL_checkoption(L, 2, NULL, modenames); in f_setvbuf()
463 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); in f_setvbuf()
465 return pushresult(L, res == 0, NULL); in f_setvbuf()
470 static int io_flush (lua_State *L) { in io_flush() argument
471 return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); in io_flush()
475 static int f_flush (lua_State *L) { in f_flush() argument
476 return pushresult(L, fflush(tofile(L)) == 0, NULL); in f_flush()
510 static void createmeta (lua_State *L) { in createmeta() argument
511 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ in createmeta()
512 lua_pushvalue(L, -1); /* push metatable */ in createmeta()
513 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ in createmeta()
514 luaL_register(L, NULL, flib); /* file methods */ in createmeta()
518 static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) { in createstdfile() argument
519 *newfile(L) = f; in createstdfile()
521 lua_pushvalue(L, -1); in createstdfile()
522 lua_rawseti(L, LUA_ENVIRONINDEX, k); in createstdfile()
524 lua_pushvalue(L, -2); /* copy environment */ in createstdfile()
525 lua_setfenv(L, -2); /* set it */ in createstdfile()
526 lua_setfield(L, -3, fname); in createstdfile()
530 static void newfenv (lua_State *L, lua_CFunction cls) { in newfenv() argument
531 lua_createtable(L, 0, 1); in newfenv()
532 lua_pushcfunction(L, cls); in newfenv()
533 lua_setfield(L, -2, "__close"); in newfenv()
537 LUALIB_API int luaopen_io (lua_State *L) { in luaopen_io() argument
538 createmeta(L); in luaopen_io()
540 newfenv(L, io_fclose); in luaopen_io()
541 lua_replace(L, LUA_ENVIRONINDEX); in luaopen_io()
543 luaL_register(L, LUA_IOLIBNAME, iolib); in luaopen_io()
545 newfenv(L, io_noclose); /* close function for default files */ in luaopen_io()
546 createstdfile(L, stdin, IO_INPUT, "stdin"); in luaopen_io()
547 createstdfile(L, stdout, IO_OUTPUT, "stdout"); in luaopen_io()
548 createstdfile(L, stderr, 0, "stderr"); in luaopen_io()
549 lua_pop(L, 1); /* pop environment for default files */ in luaopen_io()
550 lua_getfield(L, -1, "popen"); in luaopen_io()
551 newfenv(L, io_pclose); /* create environment for 'popen' */ in luaopen_io()
552 lua_setfenv(L, -2); /* set fenv for 'popen' */ in luaopen_io()
553 lua_pop(L, 1); /* pop 'popen' */ in luaopen_io()