diff options
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 36 |
1 files changed, 27 insertions, 9 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.2 1997/09/23 14:12:44 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 | */ |
@@ -56,20 +56,37 @@ static void pushresult (int i) | |||
56 | } | 56 | } |
57 | 57 | ||
58 | 58 | ||
59 | static int ishandler (lua_Object f) | ||
60 | { | ||
61 | if (lua_isuserdata(f)) { | ||
62 | if (lua_tag(f) == closedtag) | ||
63 | lua_error("trying to access a closed file"); | ||
64 | return lua_tag(f) == lua_tagio; | ||
65 | } | ||
66 | else return 0; | ||
67 | } | ||
59 | 68 | ||
60 | static FILE *getfile (char *name) | 69 | static FILE *getfile (char *name) |
61 | { | 70 | { |
62 | lua_Object f = lua_getglobal(name); | 71 | lua_Object f = lua_getglobal(name); |
63 | if (!lua_isuserdata(f) || lua_tag(f) != lua_tagio) { | 72 | if (!ishandler(f)) |
64 | if (lua_tag(f) == closedtag) | ||
65 | luaL_verror("file %s has been closed", name); | ||
66 | else | ||
67 | luaL_verror("global variable %s is not a file handle", name); | 73 | luaL_verror("global variable %s is not a file handle", name); |
68 | } | ||
69 | return lua_getuserdata(f); | 74 | return lua_getuserdata(f); |
70 | } | 75 | } |
71 | 76 | ||
72 | 77 | ||
78 | static FILE *getfileparam (char *name, int *arg) | ||
79 | { | ||
80 | lua_Object f = lua_getparam(*arg); | ||
81 | if (ishandler(f)) { | ||
82 | (*arg)++; | ||
83 | return lua_getuserdata(f); | ||
84 | } | ||
85 | else | ||
86 | return getfile(name); | ||
87 | } | ||
88 | |||
89 | |||
73 | static void closefile (char *name) | 90 | static void closefile (char *name) |
74 | { | 91 | { |
75 | FILE *f = getfile(name); | 92 | FILE *f = getfile(name); |
@@ -154,9 +171,10 @@ static void io_appendto (void) | |||
154 | 171 | ||
155 | static void io_read (void) | 172 | static void io_read (void) |
156 | { | 173 | { |
157 | FILE *f = getfile("_INPUT"); | 174 | int arg = 1; |
175 | FILE *f = getfileparam("_INPUT", &arg); | ||
158 | char *buff; | 176 | char *buff; |
159 | char *p = luaL_opt_string(1, "[^\n]*{\n}"); | 177 | char *p = luaL_opt_string(arg, "[^\n]*{\n}"); |
160 | int inskip = 0; /* to control {skips} */ | 178 | int inskip = 0; /* to control {skips} */ |
161 | int c = NEED_OTHER; | 179 | int c = NEED_OTHER; |
162 | luaI_emptybuff(); | 180 | luaI_emptybuff(); |
@@ -204,8 +222,8 @@ static void io_read (void) | |||
204 | 222 | ||
205 | static void io_write (void) | 223 | static void io_write (void) |
206 | { | 224 | { |
207 | FILE *f = getfile("_OUTPUT"); | ||
208 | int arg = 1; | 225 | int arg = 1; |
226 | FILE *f = getfileparam("_OUTPUT", &arg); | ||
209 | int status = 1; | 227 | int status = 1; |
210 | char *s; | 228 | char *s; |
211 | while ((s = luaL_opt_string(arg++, NULL)) != NULL) | 229 | while ((s = luaL_opt_string(arg++, NULL)) != NULL) |