aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-01-07 14:26:48 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-01-07 14:26:48 -0200
commit26679b1a48de4f7cfcde985764cb31c78ece4fc3 (patch)
treedf30c6972204791dd6a792dd36c1dd132bc5dbf4 /liolib.c
parente04c2b9aa817ed59b4e05025e0d33bfb3bba8f59 (diff)
downloadlua-26679b1a48de4f7cfcde985764cb31c78ece4fc3.tar.gz
lua-26679b1a48de4f7cfcde985764cb31c78ece4fc3.tar.bz2
lua-26679b1a48de4f7cfcde985764cb31c78ece4fc3.zip
back to upavalues as extra arguments for C closures; this way it's
trivial to make currying.
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/liolib.c b/liolib.c
index c487d1d2..9ed3e8d8 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.12 1997/12/18 19:11:43 roberto Exp roberto $ 2** $Id: liolib.c,v 1.13 1997/12/26 18:38:16 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*/
@@ -34,6 +34,8 @@
34#define CLOSEDTAG 2 34#define CLOSEDTAG 2
35#define IOTAG 1 35#define IOTAG 1
36 36
37#define FIRSTARG 3 /* 1st and 2nd are upvalues */
38
37#define FINPUT "_INPUT" 39#define FINPUT "_INPUT"
38#define FOUTPUT "_OUTPUT" 40#define FOUTPUT "_OUTPUT"
39 41
@@ -49,7 +51,7 @@ int pclose();
49 51
50static int gettag (int i) 52static int gettag (int i)
51{ 53{
52 return lua_getnumber(lua_upvalue(i)); 54 return lua_getnumber(lua_getparam(i));
53} 55}
54 56
55 57
@@ -124,7 +126,7 @@ static void setreturn (FILE *f, char *name)
124static void io_readfrom (void) 126static void io_readfrom (void)
125{ 127{
126 FILE *current; 128 FILE *current;
127 lua_Object f = lua_getparam(1); 129 lua_Object f = lua_getparam(FIRSTARG);
128 if (f == LUA_NOOBJECT) { 130 if (f == LUA_NOOBJECT) {
129 closefile(FINPUT); 131 closefile(FINPUT);
130 current = stdin; 132 current = stdin;
@@ -132,7 +134,7 @@ static void io_readfrom (void)
132 else if (lua_tag(f) == gettag(IOTAG)) 134 else if (lua_tag(f) == gettag(IOTAG))
133 current = lua_getuserdata(f); 135 current = lua_getuserdata(f);
134 else { 136 else {
135 char *s = luaL_check_string(1); 137 char *s = luaL_check_string(FIRSTARG);
136 current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); 138 current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
137 if (current == NULL) { 139 if (current == NULL) {
138 pushresult(0); 140 pushresult(0);
@@ -146,7 +148,7 @@ static void io_readfrom (void)
146static void io_writeto (void) 148static void io_writeto (void)
147{ 149{
148 FILE *current; 150 FILE *current;
149 lua_Object f = lua_getparam(1); 151 lua_Object f = lua_getparam(FIRSTARG);
150 if (f == LUA_NOOBJECT) { 152 if (f == LUA_NOOBJECT) {
151 closefile(FOUTPUT); 153 closefile(FOUTPUT);
152 current = stdout; 154 current = stdout;
@@ -154,7 +156,7 @@ static void io_writeto (void)
154 else if (lua_tag(f) == gettag(IOTAG)) 156 else if (lua_tag(f) == gettag(IOTAG))
155 current = lua_getuserdata(f); 157 current = lua_getuserdata(f);
156 else { 158 else {
157 char *s = luaL_check_string(1); 159 char *s = luaL_check_string(FIRSTARG);
158 current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); 160 current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
159 if (current == NULL) { 161 if (current == NULL) {
160 pushresult(0); 162 pushresult(0);
@@ -167,7 +169,7 @@ static void io_writeto (void)
167 169
168static void io_appendto (void) 170static void io_appendto (void)
169{ 171{
170 char *s = luaL_check_string(1); 172 char *s = luaL_check_string(FIRSTARG);
171 FILE *fp = fopen (s, "a"); 173 FILE *fp = fopen (s, "a");
172 if (fp != NULL) 174 if (fp != NULL)
173 setreturn(fp, FOUTPUT); 175 setreturn(fp, FOUTPUT);
@@ -180,7 +182,7 @@ static void io_appendto (void)
180 182
181static void io_read (void) 183static void io_read (void)
182{ 184{
183 int arg = 1; 185 int arg = FIRSTARG;
184 FILE *f = getfileparam(FINPUT, &arg); 186 FILE *f = getfileparam(FINPUT, &arg);
185 char *buff; 187 char *buff;
186 char *p = luaL_opt_string(arg, "[^\n]*{\n}"); 188 char *p = luaL_opt_string(arg, "[^\n]*{\n}");
@@ -232,7 +234,7 @@ static void io_read (void)
232 234
233static void io_write (void) 235static void io_write (void)
234{ 236{
235 int arg = 1; 237 int arg = FIRSTARG;
236 FILE *f = getfileparam(FOUTPUT, &arg); 238 FILE *f = getfileparam(FOUTPUT, &arg);
237 int status = 1; 239 int status = 1;
238 char *s; 240 char *s;