diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-01-25 14:45:36 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-01-25 14:45:36 -0200 |
commit | a53d9b66ca6247818acaf41e28cdf123082a272b (patch) | |
tree | 8e909200d4d925fc7394e6adf83cc5941456c588 /liolib.c | |
parent | c8559e3c8d12e052783e2952db1372c68cc16059 (diff) | |
download | lua-a53d9b66ca6247818acaf41e28cdf123082a272b.tar.gz lua-a53d9b66ca6247818acaf41e28cdf123082a272b.tar.bz2 lua-a53d9b66ca6247818acaf41e28cdf123082a272b.zip |
first implementation for type names
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 241 |
1 files changed, 90 insertions, 151 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.98 2001/01/11 18:59:03 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.99 2001/01/18 15:59:09 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 | */ |
@@ -32,7 +32,7 @@ | |||
32 | #define LC_MONETARY 0 | 32 | #define LC_MONETARY 0 |
33 | #define LC_NUMERIC 0 | 33 | #define LC_NUMERIC 0 |
34 | #define LC_TIME 0 | 34 | #define LC_TIME 0 |
35 | #define strerror(e) "generic I/O error" | 35 | #define strerror(e) "I/O error" |
36 | #define errno (-1) | 36 | #define errno (-1) |
37 | #endif | 37 | #endif |
38 | 38 | ||
@@ -53,13 +53,7 @@ int pclose(); */ | |||
53 | #define OUTFILE 1 | 53 | #define OUTFILE 1 |
54 | #define NOFILE 2 | 54 | #define NOFILE 2 |
55 | 55 | ||
56 | 56 | #define FILEHANDLE "FileHandle" | |
57 | typedef struct IOCtrl { | ||
58 | int ref[2]; /* ref for strings _INPUT/_OUTPUT */ | ||
59 | int iotag; /* tag for file handles */ | ||
60 | int closedtag; /* tag for closed handles */ | ||
61 | } IOCtrl; | ||
62 | |||
63 | 57 | ||
64 | 58 | ||
65 | static const char *const filenames[] = {"_INPUT", "_OUTPUT"}; | 59 | static const char *const filenames[] = {"_INPUT", "_OUTPUT"}; |
@@ -74,7 +68,7 @@ static int pushresult (lua_State *L, int i) { | |||
74 | lua_pushnil(L); | 68 | lua_pushnil(L); |
75 | lua_pushstring(L, strerror(errno)); | 69 | lua_pushstring(L, strerror(errno)); |
76 | lua_pushnumber(L, errno); | 70 | lua_pushnumber(L, errno); |
77 | return 3;; | 71 | return 3; |
78 | } | 72 | } |
79 | } | 73 | } |
80 | 74 | ||
@@ -86,83 +80,77 @@ static int pushresult (lua_State *L, int i) { | |||
86 | */ | 80 | */ |
87 | 81 | ||
88 | 82 | ||
89 | static FILE *gethandle (lua_State *L, IOCtrl *ctrl, int f) { | 83 | #define checkfile(L,f) (strcmp(lua_xtype(L,(f)), FILEHANDLE) == 0) |
90 | FILE *p = (FILE *)lua_touserdata(L, f); | ||
91 | if (p != NULL) { /* is `f' a userdata ? */ | ||
92 | int ftag = lua_tag(L, f); | ||
93 | if (ftag == ctrl->iotag) /* does it have the correct tag? */ | ||
94 | return p; | ||
95 | else if (ftag == ctrl->closedtag) | ||
96 | lua_error(L, "cannot access a closed file"); | ||
97 | /* else go through */ | ||
98 | } | ||
99 | return NULL; | ||
100 | } | ||
101 | 84 | ||
102 | 85 | ||
103 | static FILE *getnonullfile (lua_State *L, IOCtrl *ctrl, int arg) { | 86 | static FILE *getopthandle (lua_State *L, int inout) { |
104 | FILE *f = gethandle(L, ctrl, arg); | 87 | FILE *p = (FILE *)lua_touserdata(L, 1); |
105 | luaL_arg_check(L, f, arg, "invalid file handle"); | 88 | if (p != NULL) { /* is it a userdata ? */ |
106 | return f; | 89 | if (!checkfile(L,1)) { |
90 | if (strcmp(lua_xtype(L, 1), "ClosedFileHandle") == 0) | ||
91 | luaL_argerror(L, 1, "file is closed"); | ||
92 | else | ||
93 | luaL_argerror(L, 1, "(invalid value)"); | ||
94 | } | ||
95 | lua_remove(L, 1); /* remove it from stack */ | ||
96 | } | ||
97 | else if (inout != NOFILE) { /* try global value */ | ||
98 | lua_getglobal(L, filenames[inout]); | ||
99 | if (!checkfile(L,-1)) | ||
100 | luaL_verror(L, "global variable `%.10s' is not a valid file handle", | ||
101 | filenames[inout]); | ||
102 | p = (FILE *)lua_touserdata(L, -1); | ||
103 | lua_pop(L, 1); /* remove global value from stack */ | ||
104 | } | ||
105 | return p; | ||
107 | } | 106 | } |
108 | 107 | ||
109 | 108 | ||
110 | static FILE *getfilebyref (lua_State *L, IOCtrl *ctrl, int inout) { | 109 | static void pushfile (lua_State *L, FILE *f) { |
111 | FILE *f; | 110 | lua_pushusertag(L, f, lua_type2tag(L, FILEHANDLE)); |
112 | lua_getglobals(L); | ||
113 | lua_getref(L, ctrl->ref[inout]); | ||
114 | lua_rawget(L, -2); | ||
115 | f = gethandle(L, ctrl, -1); | ||
116 | if (f == NULL) | ||
117 | luaL_verror(L, "global variable `%.10s' is not a file handle", | ||
118 | filenames[inout]); | ||
119 | return f; | ||
120 | } | 111 | } |
121 | 112 | ||
122 | 113 | ||
123 | static void setfilebyname (lua_State *L, IOCtrl *ctrl, FILE *f, | 114 | static void setfilebyname (lua_State *L, FILE *f, const char *name) { |
124 | const char *name) { | 115 | pushfile(L, f); |
125 | lua_pushusertag(L, f, ctrl->iotag); | ||
126 | lua_setglobal(L, name); | 116 | lua_setglobal(L, name); |
127 | } | 117 | } |
128 | 118 | ||
129 | 119 | ||
130 | #define setfile(L,ctrl,f,inout) (setfilebyname(L,ctrl,f,filenames[inout])) | 120 | #define setfile(L,f,inout) (setfilebyname(L,f,filenames[inout])) |
131 | 121 | ||
132 | 122 | ||
133 | static int setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) { | 123 | static int setreturn (lua_State *L, FILE *f, int inout) { |
134 | if (f == NULL) | 124 | if (f == NULL) |
135 | return pushresult(L, 0); | 125 | return pushresult(L, 0); |
136 | else { | 126 | else { |
137 | if (inout != NOFILE) | 127 | if (inout != NOFILE) |
138 | setfile(L, ctrl, f, inout); | 128 | setfile(L, f, inout); |
139 | lua_pushusertag(L, f, ctrl->iotag); | 129 | pushfile(L, f); |
140 | return 1; | 130 | return 1; |
141 | } | 131 | } |
142 | } | 132 | } |
143 | 133 | ||
144 | 134 | ||
145 | static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) { | 135 | static int closefile (lua_State *L, FILE *f) { |
146 | if (f == stdin || f == stdout || f == stderr) | 136 | if (f == stdin || f == stdout || f == stderr) |
147 | return 1; | 137 | return 1; |
148 | else { | 138 | else { |
149 | lua_pushusertag(L, f, ctrl->iotag); | 139 | pushfile(L, f); |
150 | lua_settag(L, ctrl->closedtag); | 140 | lua_settag(L, lua_type2tag(L, "ClosedFileHandle")); |
151 | return (CLOSEFILE(L, f) == 0); | 141 | return (CLOSEFILE(L, f) == 0); |
152 | } | 142 | } |
153 | } | 143 | } |
154 | 144 | ||
155 | 145 | ||
156 | static int io_close (lua_State *L) { | 146 | static int io_close (lua_State *L) { |
157 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); | 147 | FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE); |
158 | lua_pop(L, 1); /* remove upvalue */ | 148 | return pushresult(L, closefile(L, f)); |
159 | return pushresult(L, closefile(L, ctrl, getnonullfile(L, ctrl, 1))); | ||
160 | } | 149 | } |
161 | 150 | ||
162 | 151 | ||
163 | static int file_collect (lua_State *L) { | 152 | static int file_collect (lua_State *L) { |
164 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); | 153 | FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE); |
165 | FILE *f = getnonullfile(L, ctrl, 1); | ||
166 | if (f != stdin && f != stdout && f != stderr) | 154 | if (f != stdin && f != stdout && f != stderr) |
167 | CLOSEFILE(L, f); | 155 | CLOSEFILE(L, f); |
168 | return 0; | 156 | return 0; |
@@ -170,36 +158,30 @@ static int file_collect (lua_State *L) { | |||
170 | 158 | ||
171 | 159 | ||
172 | static int io_open (lua_State *L) { | 160 | static int io_open (lua_State *L) { |
173 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); | 161 | FILE *f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2)); |
174 | FILE *f; | 162 | return setreturn(L, f, NOFILE); |
175 | lua_pop(L, 1); /* remove upvalue */ | ||
176 | f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2)); | ||
177 | return setreturn(L, ctrl, f, NOFILE); | ||
178 | } | 163 | } |
179 | 164 | ||
180 | 165 | ||
181 | static int io_tmpfile (lua_State *L) { | 166 | static int io_tmpfile (lua_State *L) { |
182 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); | 167 | return setreturn(L, tmpfile(), NOFILE); |
183 | return setreturn(L, ctrl, tmpfile(), NOFILE); | ||
184 | } | 168 | } |
185 | 169 | ||
186 | 170 | ||
187 | 171 | ||
188 | static int io_fromto (lua_State *L, int inout, const char *mode) { | 172 | static int io_fromto (lua_State *L, int inout, const char *mode) { |
189 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); | ||
190 | FILE *current; | 173 | FILE *current; |
191 | lua_pop(L, 1); /* remove upvalue */ | ||
192 | if (lua_isnull(L, 1)) { | 174 | if (lua_isnull(L, 1)) { |
193 | closefile(L, ctrl, getfilebyref(L, ctrl, inout)); | 175 | closefile(L, getopthandle(L, inout)); |
194 | current = (inout == 0) ? stdin : stdout; | 176 | current = (inout == 0) ? stdin : stdout; |
195 | } | 177 | } |
196 | else if (lua_tag(L, 1) == ctrl->iotag) /* deprecated option */ | 178 | else if (checkfile(L, 1)) /* deprecated option */ |
197 | current = (FILE *)lua_touserdata(L, 1); | 179 | current = (FILE *)lua_touserdata(L, 1); |
198 | else { | 180 | else { |
199 | const char *s = luaL_check_string(L, 1); | 181 | const char *s = luaL_check_string(L, 1); |
200 | current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode); | 182 | current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode); |
201 | } | 183 | } |
202 | return setreturn(L, ctrl, current, inout); | 184 | return setreturn(L, current, inout); |
203 | } | 185 | } |
204 | 186 | ||
205 | 187 | ||
@@ -214,11 +196,8 @@ static int io_writeto (lua_State *L) { | |||
214 | 196 | ||
215 | 197 | ||
216 | static int io_appendto (lua_State *L) { | 198 | static int io_appendto (lua_State *L) { |
217 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); | 199 | FILE *current = fopen(luaL_check_string(L, 1), "a"); |
218 | FILE *current; | 200 | return setreturn(L, current, OUTFILE); |
219 | lua_pop(L, 1); /* remove upvalue */ | ||
220 | current = fopen(luaL_check_string(L, 1), "a"); | ||
221 | return setreturn(L, ctrl, current, OUTFILE); | ||
222 | } | 201 | } |
223 | 202 | ||
224 | 203 | ||
@@ -315,31 +294,24 @@ static int read_chars (lua_State *L, FILE *f, size_t n) { | |||
315 | 294 | ||
316 | 295 | ||
317 | static int io_read (lua_State *L) { | 296 | static int io_read (lua_State *L) { |
318 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); | 297 | FILE *f = getopthandle(L, INFILE); |
319 | int lastarg = lua_gettop(L) - 1; | 298 | int nargs = lua_gettop(L); |
320 | int firstarg = 1; | ||
321 | int success; | 299 | int success; |
322 | FILE *f = gethandle(L, ctrl, firstarg); | ||
323 | int n; | 300 | int n; |
324 | if (f) firstarg++; | 301 | if (nargs == 0) { /* no arguments? */ |
325 | else f = getfilebyref(L, ctrl, INFILE); /* get _INPUT */ | 302 | success = read_line(L, f); |
326 | lua_pop(L, 1); | 303 | n = 2; /* will return n-1 results */ |
327 | if (firstarg > lastarg) { /* no arguments? */ | ||
328 | lua_settop(L, 0); /* erase upvalue and other eventual garbage */ | ||
329 | firstarg = lastarg = 1; /* correct indices */ | ||
330 | lua_pushliteral(L, "*l"); /* push default argument */ | ||
331 | } | 304 | } |
332 | else /* ensure stack space for all results and for auxlib's buffer */ | 305 | else { /* ensure stack space for all results and for auxlib's buffer */ |
333 | luaL_checkstack(L, lastarg-firstarg+1+LUA_MINSTACK, "too many arguments"); | 306 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); |
334 | success = 1; | 307 | success = 1; |
335 | for (n = firstarg; n<=lastarg && success; n++) { | 308 | for (n = 1; n<=nargs && success; n++) { |
336 | if (lua_isnumber(L, n)) | ||
337 | success = read_chars(L, f, (size_t)lua_tonumber(L, n)); | ||
338 | else { | ||
339 | const char *p = luaL_check_string(L, n); | 309 | const char *p = luaL_check_string(L, n); |
340 | if (p[0] != '*') { | 310 | if (p[0] != '*') { |
341 | lua_error(L, "read patterns are deprecated"); | 311 | if (lua_isnumber(L, n)) |
342 | success = 0; /* to avoid warnings */ | 312 | success = read_chars(L, f, (size_t)lua_tonumber(L, n)); |
313 | else | ||
314 | lua_error(L, "read patterns are deprecated"); | ||
343 | } | 315 | } |
344 | else { | 316 | else { |
345 | switch (p[1]) { | 317 | switch (p[1]) { |
@@ -367,21 +339,18 @@ static int io_read (lua_State *L) { | |||
367 | lua_pop(L, 1); /* remove last result */ | 339 | lua_pop(L, 1); /* remove last result */ |
368 | lua_pushnil(L); /* push nil instead */ | 340 | lua_pushnil(L); /* push nil instead */ |
369 | } | 341 | } |
370 | return n - firstarg; | 342 | return n - 1; |
371 | } | 343 | } |
372 | 344 | ||
373 | /* }====================================================== */ | 345 | /* }====================================================== */ |
374 | 346 | ||
375 | 347 | ||
376 | static int io_write (lua_State *L) { | 348 | static int io_write (lua_State *L) { |
377 | int lastarg = lua_gettop(L) - 1; | 349 | FILE *f = getopthandle(L, OUTFILE); |
378 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); | 350 | int nargs = lua_gettop(L); |
379 | int arg = 1; | 351 | int arg; |
380 | int status = 1; | 352 | int status = 1; |
381 | FILE *f = gethandle(L, ctrl, arg); | 353 | for (arg=1; arg<=nargs; arg++) { |
382 | if (f) arg++; | ||
383 | else f = getfilebyref(L, ctrl, OUTFILE); /* get _OUTPUT */ | ||
384 | for (; arg <= lastarg; arg++) { | ||
385 | if (lua_type(L, arg) == LUA_TNUMBER) { /* LUA_NUMBER */ | 354 | if (lua_type(L, arg) == LUA_TNUMBER) { /* LUA_NUMBER */ |
386 | /* optimization: could be done exactly as for strings */ | 355 | /* optimization: could be done exactly as for strings */ |
387 | status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0; | 356 | status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0; |
@@ -400,14 +369,9 @@ static int io_write (lua_State *L) { | |||
400 | static int io_seek (lua_State *L) { | 369 | static int io_seek (lua_State *L) { |
401 | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; | 370 | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; |
402 | static const char *const modenames[] = {"set", "cur", "end", NULL}; | 371 | static const char *const modenames[] = {"set", "cur", "end", NULL}; |
403 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); | 372 | FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE); |
404 | FILE *f; | 373 | int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames); |
405 | int op; | 374 | long offset = luaL_opt_long(L, 3, 0); |
406 | long offset; | ||
407 | lua_pop(L, 1); /* remove upvalue */ | ||
408 | f = getnonullfile(L, ctrl, 1); | ||
409 | op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames); | ||
410 | offset = luaL_opt_long(L, 3, 0); | ||
411 | luaL_arg_check(L, op != -1, 2, "invalid mode"); | 375 | luaL_arg_check(L, op != -1, 2, "invalid mode"); |
412 | op = fseek(f, offset, mode[op]); | 376 | op = fseek(f, offset, mode[op]); |
413 | if (op) | 377 | if (op) |
@@ -420,10 +384,7 @@ static int io_seek (lua_State *L) { | |||
420 | 384 | ||
421 | 385 | ||
422 | static int io_flush (lua_State *L) { | 386 | static int io_flush (lua_State *L) { |
423 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); | 387 | FILE *f = getopthandle(L, NOFILE); |
424 | FILE *f; | ||
425 | lua_pop(L, 1); /* remove upvalue */ | ||
426 | f = gethandle(L, ctrl, 1); | ||
427 | luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle"); | 388 | luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle"); |
428 | return pushresult(L, fflush(f) == 0); | 389 | return pushresult(L, fflush(f) == 0); |
429 | } | 390 | } |
@@ -686,66 +647,44 @@ static int errorfb (lua_State *L) { | |||
686 | 647 | ||
687 | 648 | ||
688 | static const struct luaL_reg iolib[] = { | 649 | static const struct luaL_reg iolib[] = { |
689 | {LUA_ERRORMESSAGE, errorfb}, | 650 | {"appendto", io_appendto}, |
690 | {"clock", io_clock}, | 651 | {"clock", io_clock}, |
652 | {"closefile", io_close}, | ||
691 | {"date", io_date}, | 653 | {"date", io_date}, |
692 | {"debug", io_debug}, | 654 | {"debug", io_debug}, |
693 | {"difftime", io_difftime}, | 655 | {"difftime", io_difftime}, |
694 | {"execute", io_execute}, | 656 | {"execute", io_execute}, |
695 | {"exit", io_exit}, | 657 | {"exit", io_exit}, |
696 | {"getenv", io_getenv}, | ||
697 | {"remove", io_remove}, | ||
698 | {"rename", io_rename}, | ||
699 | {"setlocale", io_setloc}, | ||
700 | {"time", io_time}, | ||
701 | {"tmpname", io_tmpname} | ||
702 | }; | ||
703 | |||
704 | |||
705 | static const struct luaL_reg iolibtag[] = { | ||
706 | {"appendto", io_appendto}, | ||
707 | {"closefile", io_close}, | ||
708 | {"flush", io_flush}, | 658 | {"flush", io_flush}, |
659 | {"getenv", io_getenv}, | ||
709 | {"openfile", io_open}, | 660 | {"openfile", io_open}, |
710 | {"read", io_read}, | 661 | {"read", io_read}, |
711 | {"readfrom", io_readfrom}, | 662 | {"readfrom", io_readfrom}, |
663 | {"remove", io_remove}, | ||
664 | {"rename", io_rename}, | ||
712 | {"seek", io_seek}, | 665 | {"seek", io_seek}, |
666 | {"setlocale", io_setloc}, | ||
667 | {"time", io_time}, | ||
713 | {"tmpfile", io_tmpfile}, | 668 | {"tmpfile", io_tmpfile}, |
669 | {"tmpname", io_tmpname}, | ||
714 | {"write", io_write}, | 670 | {"write", io_write}, |
715 | {"writeto", io_writeto} | 671 | {"writeto", io_writeto}, |
672 | {LUA_ERRORMESSAGE, errorfb} | ||
716 | }; | 673 | }; |
717 | 674 | ||
718 | 675 | ||
719 | static void openwithcontrol (lua_State *L) { | ||
720 | IOCtrl *ctrl = (IOCtrl *)lua_newuserdata(L, sizeof(IOCtrl)); | ||
721 | unsigned int i; | ||
722 | ctrl->iotag = lua_newtag(L); | ||
723 | ctrl->closedtag = lua_newtag(L); | ||
724 | for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) { | ||
725 | /* put `ctrl' as upvalue for these functions */ | ||
726 | lua_pushvalue(L, -1); | ||
727 | lua_pushcclosure(L, iolibtag[i].func, 1); | ||
728 | lua_setglobal(L, iolibtag[i].name); | ||
729 | } | ||
730 | /* create references to variable names */ | ||
731 | lua_pushstring(L, filenames[INFILE]); | ||
732 | ctrl->ref[INFILE] = lua_ref(L, 1); | ||
733 | lua_pushstring(L, filenames[OUTFILE]); | ||
734 | ctrl->ref[OUTFILE] = lua_ref(L, 1); | ||
735 | /* predefined file handles */ | ||
736 | setfile(L, ctrl, stdin, INFILE); | ||
737 | setfile(L, ctrl, stdout, OUTFILE); | ||
738 | setfilebyname(L, ctrl, stdin, "_STDIN"); | ||
739 | setfilebyname(L, ctrl, stdout, "_STDOUT"); | ||
740 | setfilebyname(L, ctrl, stderr, "_STDERR"); | ||
741 | /* close files when collected */ | ||
742 | lua_pushcclosure(L, file_collect, 1); /* pops `ctrl' from stack */ | ||
743 | lua_settagmethod(L, ctrl->iotag, "gc"); | ||
744 | } | ||
745 | |||
746 | |||
747 | LUALIB_API void lua_iolibopen (lua_State *L) { | 676 | LUALIB_API void lua_iolibopen (lua_State *L) { |
677 | int iotag = lua_newtype(L, FILEHANDLE, LUA_TUSERDATA); | ||
678 | lua_newtype(L, "ClosedFileHandle", LUA_TUSERDATA); | ||
748 | luaL_openl(L, iolib); | 679 | luaL_openl(L, iolib); |
749 | openwithcontrol(L); | 680 | /* predefined file handles */ |
681 | setfile(L, stdin, INFILE); | ||
682 | setfile(L, stdout, OUTFILE); | ||
683 | setfilebyname(L, stdin, "_STDIN"); | ||
684 | setfilebyname(L, stdout, "_STDOUT"); | ||
685 | setfilebyname(L, stderr, "_STDERR"); | ||
686 | /* close files when collected */ | ||
687 | lua_pushcfunction(L, file_collect); | ||
688 | lua_settagmethod(L, iotag, "gc"); | ||
750 | } | 689 | } |
751 | 690 | ||