rlm@1: /* rlm@1: ** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $ rlm@1: ** Standard I/O (and system) library rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: rlm@1: #define liolib_c rlm@1: #define LUA_LIB rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "lauxlib.h" rlm@1: #include "lualib.h" rlm@1: rlm@1: rlm@1: rlm@1: #define IO_INPUT 1 rlm@1: #define IO_OUTPUT 2 rlm@1: rlm@1: rlm@1: static const char *const fnames[] = {"input", "output"}; rlm@1: rlm@1: rlm@1: static int pushresult (lua_State *L, int i, const char *filename) { rlm@1: int en = errno; /* calls to Lua API may change this value */ rlm@1: if (i) { rlm@1: lua_pushboolean(L, 1); rlm@1: return 1; rlm@1: } rlm@1: else { rlm@1: lua_pushnil(L); rlm@1: if (filename) rlm@1: lua_pushfstring(L, "%s: %s", filename, strerror(en)); rlm@1: else rlm@1: lua_pushfstring(L, "%s", strerror(en)); rlm@1: lua_pushinteger(L, en); rlm@1: return 3; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static void fileerror (lua_State *L, int arg, const char *filename) { rlm@1: lua_pushfstring(L, "%s: %s", filename, strerror(errno)); rlm@1: luaL_argerror(L, arg, lua_tostring(L, -1)); rlm@1: } rlm@1: rlm@1: rlm@1: #define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE)) rlm@1: rlm@1: rlm@1: static int io_type (lua_State *L) { rlm@1: void *ud; rlm@1: luaL_checkany(L, 1); rlm@1: ud = lua_touserdata(L, 1); rlm@1: lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); rlm@1: if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1)) rlm@1: lua_pushnil(L); /* not a file */ rlm@1: else if (*((FILE **)ud) == NULL) rlm@1: lua_pushliteral(L, "closed file"); rlm@1: else rlm@1: lua_pushliteral(L, "file"); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static FILE *tofile (lua_State *L) { rlm@1: FILE **f = tofilep(L); rlm@1: if (*f == NULL) rlm@1: luaL_error(L, "attempt to use a closed file"); rlm@1: return *f; rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** When creating file handles, always creates a `closed' file handle rlm@1: ** before opening the actual file; so, if there is a memory error, the rlm@1: ** file is not left opened. rlm@1: */ rlm@1: static FILE **newfile (lua_State *L) { rlm@1: FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); rlm@1: *pf = NULL; /* file handle is currently `closed' */ rlm@1: luaL_getmetatable(L, LUA_FILEHANDLE); rlm@1: lua_setmetatable(L, -2); rlm@1: return pf; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** function to (not) close the standard files stdin, stdout, and stderr rlm@1: */ rlm@1: static int io_noclose (lua_State *L) { rlm@1: lua_pushnil(L); rlm@1: lua_pushliteral(L, "cannot close standard file"); rlm@1: return 2; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** function to close 'popen' files rlm@1: */ rlm@1: static int io_pclose (lua_State *L) { rlm@1: FILE **p = tofilep(L); rlm@1: int ok = lua_pclose(L, *p); rlm@1: *p = NULL; rlm@1: return pushresult(L, ok, NULL); rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** function to close regular files rlm@1: */ rlm@1: static int io_fclose (lua_State *L) { rlm@1: FILE **p = tofilep(L); rlm@1: int ok = (fclose(*p) == 0); rlm@1: *p = NULL; rlm@1: return pushresult(L, ok, NULL); rlm@1: } rlm@1: rlm@1: rlm@1: static int aux_close (lua_State *L) { rlm@1: lua_getfenv(L, 1); rlm@1: lua_getfield(L, -1, "__close"); rlm@1: return (lua_tocfunction(L, -1))(L); rlm@1: } rlm@1: rlm@1: rlm@1: static int io_close (lua_State *L) { rlm@1: if (lua_isnone(L, 1)) rlm@1: lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT); rlm@1: tofile(L); /* make sure argument is a file */ rlm@1: return aux_close(L); rlm@1: } rlm@1: rlm@1: rlm@1: static int io_gc (lua_State *L) { rlm@1: FILE *f = *tofilep(L); rlm@1: /* ignore closed files */ rlm@1: if (f != NULL) rlm@1: aux_close(L); rlm@1: return 0; rlm@1: } rlm@1: rlm@1: rlm@1: static int io_tostring (lua_State *L) { rlm@1: FILE *f = *tofilep(L); rlm@1: if (f == NULL) rlm@1: lua_pushliteral(L, "file (closed)"); rlm@1: else rlm@1: lua_pushfstring(L, "file (%p)", f); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int io_open (lua_State *L) { rlm@1: const char *filename = luaL_checkstring(L, 1); rlm@1: const char *mode = luaL_optstring(L, 2, "r"); rlm@1: FILE **pf = newfile(L); rlm@1: *pf = fopen(filename, mode); rlm@1: return (*pf == NULL) ? pushresult(L, 0, filename) : 1; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** this function has a separated environment, which defines the rlm@1: ** correct __close for 'popen' files rlm@1: */ rlm@1: static int io_popen (lua_State *L) { rlm@1: const char *filename = luaL_checkstring(L, 1); rlm@1: const char *mode = luaL_optstring(L, 2, "r"); rlm@1: FILE **pf = newfile(L); rlm@1: *pf = lua_popen(L, filename, mode); rlm@1: return (*pf == NULL) ? pushresult(L, 0, filename) : 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int io_tmpfile (lua_State *L) { rlm@1: FILE **pf = newfile(L); rlm@1: *pf = tmpfile(); rlm@1: return (*pf == NULL) ? pushresult(L, 0, NULL) : 1; rlm@1: } rlm@1: rlm@1: rlm@1: static FILE *getiofile (lua_State *L, int findex) { rlm@1: FILE *f; rlm@1: lua_rawgeti(L, LUA_ENVIRONINDEX, findex); rlm@1: f = *(FILE **)lua_touserdata(L, -1); rlm@1: if (f == NULL) rlm@1: luaL_error(L, "standard %s file is closed", fnames[findex - 1]); rlm@1: return f; rlm@1: } rlm@1: rlm@1: rlm@1: static int g_iofile (lua_State *L, int f, const char *mode) { rlm@1: if (!lua_isnoneornil(L, 1)) { rlm@1: const char *filename = lua_tostring(L, 1); rlm@1: if (filename) { rlm@1: FILE **pf = newfile(L); rlm@1: *pf = fopen(filename, mode); rlm@1: if (*pf == NULL) rlm@1: fileerror(L, 1, filename); rlm@1: } rlm@1: else { rlm@1: tofile(L); /* check that it's a valid file handle */ rlm@1: lua_pushvalue(L, 1); rlm@1: } rlm@1: lua_rawseti(L, LUA_ENVIRONINDEX, f); rlm@1: } rlm@1: /* return current value */ rlm@1: lua_rawgeti(L, LUA_ENVIRONINDEX, f); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int io_input (lua_State *L) { rlm@1: return g_iofile(L, IO_INPUT, "r"); rlm@1: } rlm@1: rlm@1: rlm@1: static int io_output (lua_State *L) { rlm@1: return g_iofile(L, IO_OUTPUT, "w"); rlm@1: } rlm@1: rlm@1: rlm@1: static int io_readline (lua_State *L); rlm@1: rlm@1: rlm@1: static void aux_lines (lua_State *L, int idx, int toclose) { rlm@1: lua_pushvalue(L, idx); rlm@1: lua_pushboolean(L, toclose); /* close/not close file when finished */ rlm@1: lua_pushcclosure(L, io_readline, 2); rlm@1: } rlm@1: rlm@1: rlm@1: static int f_lines (lua_State *L) { rlm@1: tofile(L); /* check that it's a valid file handle */ rlm@1: aux_lines(L, 1, 0); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int io_lines (lua_State *L) { rlm@1: if (lua_isnoneornil(L, 1)) { /* no arguments? */ rlm@1: /* will iterate over default input */ rlm@1: lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT); rlm@1: return f_lines(L); rlm@1: } rlm@1: else { rlm@1: const char *filename = luaL_checkstring(L, 1); rlm@1: FILE **pf = newfile(L); rlm@1: *pf = fopen(filename, "r"); rlm@1: if (*pf == NULL) rlm@1: fileerror(L, 1, filename); rlm@1: aux_lines(L, lua_gettop(L), 1); rlm@1: return 1; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** {====================================================== rlm@1: ** READ rlm@1: ** ======================================================= rlm@1: */ rlm@1: rlm@1: rlm@1: static int read_number (lua_State *L, FILE *f) { rlm@1: lua_Number d; rlm@1: if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { rlm@1: lua_pushnumber(L, d); rlm@1: return 1; rlm@1: } rlm@1: else return 0; /* read fails */ rlm@1: } rlm@1: rlm@1: rlm@1: static int test_eof (lua_State *L, FILE *f) { rlm@1: int c = getc(f); rlm@1: ungetc(c, f); rlm@1: lua_pushlstring(L, NULL, 0); rlm@1: return (c != EOF); rlm@1: } rlm@1: rlm@1: rlm@1: static int read_line (lua_State *L, FILE *f) { rlm@1: luaL_Buffer b; rlm@1: luaL_buffinit(L, &b); rlm@1: for (;;) { rlm@1: size_t l; rlm@1: char *p = luaL_prepbuffer(&b); rlm@1: if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ rlm@1: luaL_pushresult(&b); /* close buffer */ rlm@1: return (lua_objlen(L, -1) > 0); /* check whether read something */ rlm@1: } rlm@1: l = strlen(p); rlm@1: if (l == 0 || p[l-1] != '\n') rlm@1: luaL_addsize(&b, l); rlm@1: else { rlm@1: luaL_addsize(&b, l - 1); /* do not include `eol' */ rlm@1: luaL_pushresult(&b); /* close buffer */ rlm@1: return 1; /* read at least an `eol' */ rlm@1: } rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static int read_chars (lua_State *L, FILE *f, size_t n) { rlm@1: size_t rlen; /* how much to read */ rlm@1: size_t nr; /* number of chars actually read */ rlm@1: luaL_Buffer b; rlm@1: luaL_buffinit(L, &b); rlm@1: rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ rlm@1: do { rlm@1: char *p = luaL_prepbuffer(&b); rlm@1: if (rlen > n) rlen = n; /* cannot read more than asked */ rlm@1: nr = fread(p, sizeof(char), rlen, f); rlm@1: luaL_addsize(&b, nr); rlm@1: n -= nr; /* still have to read `n' chars */ rlm@1: } while (n > 0 && nr == rlen); /* until end of count or eof */ rlm@1: luaL_pushresult(&b); /* close buffer */ rlm@1: return (n == 0 || lua_objlen(L, -1) > 0); rlm@1: } rlm@1: rlm@1: rlm@1: static int g_read (lua_State *L, FILE *f, int first) { rlm@1: int nargs = lua_gettop(L) - 1; rlm@1: int success; rlm@1: int n; rlm@1: clearerr(f); rlm@1: if (nargs == 0) { /* no arguments? */ rlm@1: success = read_line(L, f); rlm@1: n = first+1; /* to return 1 result */ rlm@1: } rlm@1: else { /* ensure stack space for all results and for auxlib's buffer */ rlm@1: luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); rlm@1: success = 1; rlm@1: for (n = first; nargs-- && success; n++) { rlm@1: if (lua_type(L, n) == LUA_TNUMBER) { rlm@1: size_t l = (size_t)lua_tointeger(L, n); rlm@1: success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); rlm@1: } rlm@1: else { rlm@1: const char *p = lua_tostring(L, n); rlm@1: luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); rlm@1: switch (p[1]) { rlm@1: case 'n': /* number */ rlm@1: success = read_number(L, f); rlm@1: break; rlm@1: case 'l': /* line */ rlm@1: success = read_line(L, f); rlm@1: break; rlm@1: case 'a': /* file */ rlm@1: read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ rlm@1: success = 1; /* always success */ rlm@1: break; rlm@1: default: rlm@1: return luaL_argerror(L, n, "invalid format"); rlm@1: } rlm@1: } rlm@1: } rlm@1: } rlm@1: if (ferror(f)) rlm@1: return pushresult(L, 0, NULL); rlm@1: if (!success) { rlm@1: lua_pop(L, 1); /* remove last result */ rlm@1: lua_pushnil(L); /* push nil instead */ rlm@1: } rlm@1: return n - first; rlm@1: } rlm@1: rlm@1: rlm@1: static int io_read (lua_State *L) { rlm@1: return g_read(L, getiofile(L, IO_INPUT), 1); rlm@1: } rlm@1: rlm@1: rlm@1: static int f_read (lua_State *L) { rlm@1: return g_read(L, tofile(L), 2); rlm@1: } rlm@1: rlm@1: rlm@1: static int io_readline (lua_State *L) { rlm@1: FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); rlm@1: int sucess; rlm@1: if (f == NULL) /* file is already closed? */ rlm@1: luaL_error(L, "file is already closed"); rlm@1: sucess = read_line(L, f); rlm@1: if (ferror(f)) rlm@1: return luaL_error(L, "%s", strerror(errno)); rlm@1: if (sucess) return 1; rlm@1: else { /* EOF */ rlm@1: if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ rlm@1: lua_settop(L, 0); rlm@1: lua_pushvalue(L, lua_upvalueindex(1)); rlm@1: aux_close(L); /* close it */ rlm@1: } rlm@1: return 0; rlm@1: } rlm@1: } rlm@1: rlm@1: /* }====================================================== */ rlm@1: rlm@1: rlm@1: static int g_write (lua_State *L, FILE *f, int arg) { rlm@1: int nargs = lua_gettop(L) - 1; rlm@1: int status = 1; rlm@1: for (; nargs--; arg++) { rlm@1: if (lua_type(L, arg) == LUA_TNUMBER) { rlm@1: /* optimization: could be done exactly as for strings */ rlm@1: status = status && rlm@1: fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; rlm@1: } rlm@1: else { rlm@1: size_t l; rlm@1: const char *s = luaL_checklstring(L, arg, &l); rlm@1: status = status && (fwrite(s, sizeof(char), l, f) == l); rlm@1: } rlm@1: } rlm@1: return pushresult(L, status, NULL); rlm@1: } rlm@1: rlm@1: rlm@1: static int io_write (lua_State *L) { rlm@1: return g_write(L, getiofile(L, IO_OUTPUT), 1); rlm@1: } rlm@1: rlm@1: rlm@1: static int f_write (lua_State *L) { rlm@1: return g_write(L, tofile(L), 2); rlm@1: } rlm@1: rlm@1: rlm@1: static int f_seek (lua_State *L) { rlm@1: static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; rlm@1: static const char *const modenames[] = {"set", "cur", "end", NULL}; rlm@1: FILE *f = tofile(L); rlm@1: int op = luaL_checkoption(L, 2, "cur", modenames); rlm@1: long offset = luaL_optlong(L, 3, 0); rlm@1: op = fseek(f, offset, mode[op]); rlm@1: if (op) rlm@1: return pushresult(L, 0, NULL); /* error */ rlm@1: else { rlm@1: lua_pushinteger(L, ftell(f)); rlm@1: return 1; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static int f_setvbuf (lua_State *L) { rlm@1: static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; rlm@1: static const char *const modenames[] = {"no", "full", "line", NULL}; rlm@1: FILE *f = tofile(L); rlm@1: int op = luaL_checkoption(L, 2, NULL, modenames); rlm@1: lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); rlm@1: int res = setvbuf(f, NULL, mode[op], sz); rlm@1: return pushresult(L, res == 0, NULL); rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: static int io_flush (lua_State *L) { rlm@1: return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); rlm@1: } rlm@1: rlm@1: rlm@1: static int f_flush (lua_State *L) { rlm@1: return pushresult(L, fflush(tofile(L)) == 0, NULL); rlm@1: } rlm@1: rlm@1: rlm@1: static const luaL_Reg iolib[] = { rlm@1: {"close", io_close}, rlm@1: {"flush", io_flush}, rlm@1: {"input", io_input}, rlm@1: {"lines", io_lines}, rlm@1: {"open", io_open}, rlm@1: {"output", io_output}, rlm@1: {"popen", io_popen}, rlm@1: {"read", io_read}, rlm@1: {"tmpfile", io_tmpfile}, rlm@1: {"type", io_type}, rlm@1: {"write", io_write}, rlm@1: {NULL, NULL} rlm@1: }; rlm@1: rlm@1: rlm@1: static const luaL_Reg flib[] = { rlm@1: {"close", io_close}, rlm@1: {"flush", f_flush}, rlm@1: {"lines", f_lines}, rlm@1: {"read", f_read}, rlm@1: {"seek", f_seek}, rlm@1: {"setvbuf", f_setvbuf}, rlm@1: {"write", f_write}, rlm@1: {"__gc", io_gc}, rlm@1: {"__tostring", io_tostring}, rlm@1: {NULL, NULL} rlm@1: }; rlm@1: rlm@1: rlm@1: static void createmeta (lua_State *L) { rlm@1: luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ rlm@1: lua_pushvalue(L, -1); /* push metatable */ rlm@1: lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ rlm@1: luaL_register(L, NULL, flib); /* file methods */ rlm@1: } rlm@1: rlm@1: rlm@1: static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) { rlm@1: *newfile(L) = f; rlm@1: if (k > 0) { rlm@1: lua_pushvalue(L, -1); rlm@1: lua_rawseti(L, LUA_ENVIRONINDEX, k); rlm@1: } rlm@1: lua_pushvalue(L, -2); /* copy environment */ rlm@1: lua_setfenv(L, -2); /* set it */ rlm@1: lua_setfield(L, -3, fname); rlm@1: } rlm@1: rlm@1: rlm@1: static void newfenv (lua_State *L, lua_CFunction cls) { rlm@1: lua_createtable(L, 0, 1); rlm@1: lua_pushcfunction(L, cls); rlm@1: lua_setfield(L, -2, "__close"); rlm@1: } rlm@1: rlm@1: rlm@1: LUALIB_API int luaopen_io (lua_State *L) { rlm@1: createmeta(L); rlm@1: /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ rlm@1: newfenv(L, io_fclose); rlm@1: lua_replace(L, LUA_ENVIRONINDEX); rlm@1: /* open library */ rlm@1: luaL_register(L, LUA_IOLIBNAME, iolib); rlm@1: /* create (and set) default files */ rlm@1: newfenv(L, io_noclose); /* close function for default files */ rlm@1: createstdfile(L, stdin, IO_INPUT, "stdin"); rlm@1: createstdfile(L, stdout, IO_OUTPUT, "stdout"); rlm@1: createstdfile(L, stderr, 0, "stderr"); rlm@1: lua_pop(L, 1); /* pop environment for default files */ rlm@1: lua_getfield(L, -1, "popen"); rlm@1: newfenv(L, io_pclose); /* create environment for 'popen' */ rlm@1: lua_setfenv(L, -2); /* set fenv for 'popen' */ rlm@1: lua_pop(L, 1); /* pop 'popen' */ rlm@1: return 1; rlm@1: } rlm@1: