diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-04-30 17:15:55 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-04-30 17:15:55 -0300 |
commit | 26097b6c4b28ddbf0c0d869dd657c6e00fa57a37 (patch) | |
tree | dfe090ca02b6b7aa1b4bf9a5c428ea25afe8b31f | |
parent | f1f8f0ca227aba8ae589c8c22bf711191e92f129 (diff) | |
download | lua-26097b6c4b28ddbf0c0d869dd657c6e00fa57a37.tar.gz lua-26097b6c4b28ddbf0c0d869dd657c6e00fa57a37.tar.bz2 lua-26097b6c4b28ddbf0c0d869dd657c6e00fa57a37.zip |
uses integers as keys to standard i/o files
-rw-r--r-- | liolib.c | 63 |
1 files changed, 26 insertions, 37 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 2.38 2003/03/18 12:25:32 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.39 2003/03/19 21:16:12 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -65,8 +65,8 @@ | |||
65 | 65 | ||
66 | #define FILEHANDLE "FILE*" | 66 | #define FILEHANDLE "FILE*" |
67 | 67 | ||
68 | #define IO_INPUT "_input" | 68 | #define IO_INPUT 1 |
69 | #define IO_OUTPUT "_output" | 69 | #define IO_OUTPUT 2 |
70 | 70 | ||
71 | 71 | ||
72 | static int pushresult (lua_State *L, int i, const char *filename) { | 72 | static int pushresult (lua_State *L, int i, const char *filename) { |
@@ -127,23 +127,6 @@ static FILE **newfile (lua_State *L) { | |||
127 | } | 127 | } |
128 | 128 | ||
129 | 129 | ||
130 | /* | ||
131 | ** assumes that top of the stack is the `io' library, and next is | ||
132 | ** the `io' metatable | ||
133 | */ | ||
134 | static void registerfile (lua_State *L, FILE *f, const char *name, | ||
135 | const char *impname) { | ||
136 | lua_pushstring(L, name); | ||
137 | *newfile(L) = f; | ||
138 | if (impname) { | ||
139 | lua_pushstring(L, impname); | ||
140 | lua_pushvalue(L, -2); | ||
141 | lua_settable(L, -6); /* metatable[impname] = file */ | ||
142 | } | ||
143 | lua_settable(L, -3); /* io[name] = file */ | ||
144 | } | ||
145 | |||
146 | |||
147 | static int aux_close (lua_State *L) { | 130 | static int aux_close (lua_State *L) { |
148 | FILE *f = tofile(L, 1); | 131 | FILE *f = tofile(L, 1); |
149 | if (f == stdin || f == stdout || f == stderr) | 132 | if (f == stdin || f == stdout || f == stderr) |
@@ -158,10 +141,8 @@ static int aux_close (lua_State *L) { | |||
158 | 141 | ||
159 | 142 | ||
160 | static int io_close (lua_State *L) { | 143 | static int io_close (lua_State *L) { |
161 | if (lua_isnone(L, 1)) { | 144 | if (lua_isnone(L, 1)) |
162 | lua_pushstring(L, IO_OUTPUT); | 145 | lua_rawgeti(L, lua_upvalueindex(1), IO_OUTPUT); |
163 | lua_rawget(L, lua_upvalueindex(1)); | ||
164 | } | ||
165 | return pushresult(L, aux_close(L), NULL); | 146 | return pushresult(L, aux_close(L), NULL); |
166 | } | 147 | } |
167 | 148 | ||
@@ -216,17 +197,15 @@ static int io_tmpfile (lua_State *L) { | |||
216 | } | 197 | } |
217 | 198 | ||
218 | 199 | ||
219 | static FILE *getiofile (lua_State *L, const char *name) { | 200 | static FILE *getiofile (lua_State *L, int f) { |
220 | lua_pushstring(L, name); | 201 | lua_rawgeti(L, lua_upvalueindex(1), f); |
221 | lua_rawget(L, lua_upvalueindex(1)); | ||
222 | return tofile(L, -1); | 202 | return tofile(L, -1); |
223 | } | 203 | } |
224 | 204 | ||
225 | 205 | ||
226 | static int g_iofile (lua_State *L, const char *name, const char *mode) { | 206 | static int g_iofile (lua_State *L, int f, const char *mode) { |
227 | if (!lua_isnoneornil(L, 1)) { | 207 | if (!lua_isnoneornil(L, 1)) { |
228 | const char *filename = lua_tostring(L, 1); | 208 | const char *filename = lua_tostring(L, 1); |
229 | lua_pushstring(L, name); | ||
230 | if (filename) { | 209 | if (filename) { |
231 | FILE **pf = newfile(L); | 210 | FILE **pf = newfile(L); |
232 | *pf = fopen(filename, mode); | 211 | *pf = fopen(filename, mode); |
@@ -239,11 +218,10 @@ static int g_iofile (lua_State *L, const char *name, const char *mode) { | |||
239 | tofile(L, 1); /* check that it's a valid file handle */ | 218 | tofile(L, 1); /* check that it's a valid file handle */ |
240 | lua_pushvalue(L, 1); | 219 | lua_pushvalue(L, 1); |
241 | } | 220 | } |
242 | lua_rawset(L, lua_upvalueindex(1)); | 221 | lua_rawseti(L, lua_upvalueindex(1), f); |
243 | } | 222 | } |
244 | /* return current value */ | 223 | /* return current value */ |
245 | lua_pushstring(L, name); | 224 | lua_rawgeti(L, lua_upvalueindex(1), f); |
246 | lua_rawget(L, lua_upvalueindex(1)); | ||
247 | return 1; | 225 | return 1; |
248 | } | 226 | } |
249 | 227 | ||
@@ -279,8 +257,8 @@ static int f_lines (lua_State *L) { | |||
279 | 257 | ||
280 | static int io_lines (lua_State *L) { | 258 | static int io_lines (lua_State *L) { |
281 | if (lua_isnoneornil(L, 1)) { /* no arguments? */ | 259 | if (lua_isnoneornil(L, 1)) { /* no arguments? */ |
282 | lua_pushstring(L, IO_INPUT); | 260 | /* will iterate over default input */ |
283 | lua_rawget(L, lua_upvalueindex(1)); /* will iterate over default input */ | 261 | lua_rawgeti(L, lua_upvalueindex(1), IO_INPUT); |
284 | return f_lines(L); | 262 | return f_lines(L); |
285 | } | 263 | } |
286 | else { | 264 | else { |
@@ -520,6 +498,11 @@ static const luaL_reg flib[] = { | |||
520 | 498 | ||
521 | static void createmeta (lua_State *L) { | 499 | static void createmeta (lua_State *L) { |
522 | luaL_newmetatable(L, FILEHANDLE); /* create new metatable for file handles */ | 500 | luaL_newmetatable(L, FILEHANDLE); /* create new metatable for file handles */ |
501 | /* create (and set) default files */ | ||
502 | *newfile(L) = stdin; | ||
503 | lua_rawseti(L, -2, IO_INPUT); | ||
504 | *newfile(L) = stdout; | ||
505 | lua_rawseti(L, -2, IO_OUTPUT); | ||
523 | /* file methods */ | 506 | /* file methods */ |
524 | lua_pushliteral(L, "__index"); | 507 | lua_pushliteral(L, "__index"); |
525 | lua_pushvalue(L, -2); /* push metatable */ | 508 | lua_pushvalue(L, -2); /* push metatable */ |
@@ -742,9 +725,15 @@ LUALIB_API int luaopen_io (lua_State *L) { | |||
742 | lua_pushvalue(L, -1); | 725 | lua_pushvalue(L, -1); |
743 | luaL_openlib(L, LUA_IOLIBNAME, iolib, 1); | 726 | luaL_openlib(L, LUA_IOLIBNAME, iolib, 1); |
744 | /* put predefined file handles into `io' table */ | 727 | /* put predefined file handles into `io' table */ |
745 | registerfile(L, stdin, "stdin", IO_INPUT); | 728 | lua_pushstring(L, "stdin"); |
746 | registerfile(L, stdout, "stdout", IO_OUTPUT); | 729 | lua_rawgeti(L, 2, IO_INPUT); |
747 | registerfile(L, stderr, "stderr", NULL); | 730 | lua_rawset(L, 3); |
731 | lua_pushstring(L, "stdout"); | ||
732 | lua_rawgeti(L, 2, IO_OUTPUT); | ||
733 | lua_rawset(L, 3); | ||
734 | lua_pushstring(L, "stderr"); | ||
735 | *newfile(L) = stderr; | ||
736 | lua_rawset(L, 3); | ||
748 | return 1; | 737 | return 1; |
749 | } | 738 | } |
750 | 739 | ||