diff options
| -rw-r--r-- | liolib.c | 83 |
1 files changed, 48 insertions, 35 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 1.6 1997/11/19 18:16:33 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.7 1997/11/27 15:59: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 | */ |
| @@ -32,13 +32,16 @@ | |||
| 32 | #endif | 32 | #endif |
| 33 | 33 | ||
| 34 | 34 | ||
| 35 | #define CLOSEDTAG ".CLOSEDTAG" | 35 | #define CLOSEDTAG 2 |
| 36 | #define IOTAG ".IOTAG" | 36 | #define IOTAG 1 |
| 37 | 37 | ||
| 38 | #define FINPUT "_INPUT" | 38 | #define FINPUT "_INPUT" |
| 39 | #define FOUTPUT "_OUTPUT" | 39 | #define FOUTPUT "_OUTPUT" |
| 40 | 40 | ||
| 41 | 41 | ||
| 42 | #define FIRSTARG 3 /* 1st and 2nd are upvalues */ | ||
| 43 | |||
| 44 | |||
| 42 | #ifdef POPEN | 45 | #ifdef POPEN |
| 43 | FILE *popen(); | 46 | FILE *popen(); |
| 44 | int pclose(); | 47 | int pclose(); |
| @@ -48,16 +51,9 @@ int pclose(); | |||
| 48 | #endif | 51 | #endif |
| 49 | 52 | ||
| 50 | 53 | ||
| 51 | static void createtag (char *t) | 54 | static int gettag (int i) |
| 52 | { | ||
| 53 | lua_pushnumber(lua_newtag()); | ||
| 54 | lua_rawsetglobal(t); | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | static int gettag (char *t) | ||
| 59 | { | 55 | { |
| 60 | return lua_getnumber(lua_rawgetglobal(t)); | 56 | return lua_getnumber(lua_getparam(i)); |
| 61 | } | 57 | } |
| 62 | 58 | ||
| 63 | 59 | ||
| @@ -114,24 +110,25 @@ static void closefile (char *name) | |||
| 114 | } | 110 | } |
| 115 | 111 | ||
| 116 | 112 | ||
| 117 | static void setfile (FILE *f, char *name) | 113 | static void setfile (FILE *f, char *name, int tag) |
| 118 | { | 114 | { |
| 119 | lua_pushusertag(f, gettag(IOTAG)); | 115 | lua_pushusertag(f, tag); |
| 120 | lua_setglobal(name); | 116 | lua_setglobal(name); |
| 121 | } | 117 | } |
| 122 | 118 | ||
| 123 | 119 | ||
| 124 | static void setreturn (FILE *f, char *name) | 120 | static void setreturn (FILE *f, char *name) |
| 125 | { | 121 | { |
| 126 | setfile(f, name); | 122 | int tag = gettag(IOTAG); |
| 127 | lua_pushusertag(f, gettag(IOTAG)); | 123 | setfile(f, name, tag); |
| 124 | lua_pushusertag(f, tag); | ||
| 128 | } | 125 | } |
| 129 | 126 | ||
| 130 | 127 | ||
| 131 | static void io_readfrom (void) | 128 | static void io_readfrom (void) |
| 132 | { | 129 | { |
| 133 | FILE *current; | 130 | FILE *current; |
| 134 | lua_Object f = lua_getparam(1); | 131 | lua_Object f = lua_getparam(FIRSTARG); |
| 135 | if (f == LUA_NOOBJECT) { | 132 | if (f == LUA_NOOBJECT) { |
| 136 | closefile(FINPUT); | 133 | closefile(FINPUT); |
| 137 | current = stdin; | 134 | current = stdin; |
| @@ -139,7 +136,7 @@ static void io_readfrom (void) | |||
| 139 | else if (lua_tag(f) == gettag(IOTAG)) | 136 | else if (lua_tag(f) == gettag(IOTAG)) |
| 140 | current = lua_getuserdata(f); | 137 | current = lua_getuserdata(f); |
| 141 | else { | 138 | else { |
| 142 | char *s = luaL_check_string(1); | 139 | char *s = luaL_check_string(FIRSTARG); |
| 143 | current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); | 140 | current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); |
| 144 | if (current == NULL) { | 141 | if (current == NULL) { |
| 145 | pushresult(0); | 142 | pushresult(0); |
| @@ -153,7 +150,7 @@ static void io_readfrom (void) | |||
| 153 | static void io_writeto (void) | 150 | static void io_writeto (void) |
| 154 | { | 151 | { |
| 155 | FILE *current; | 152 | FILE *current; |
| 156 | lua_Object f = lua_getparam(1); | 153 | lua_Object f = lua_getparam(FIRSTARG); |
| 157 | if (f == LUA_NOOBJECT) { | 154 | if (f == LUA_NOOBJECT) { |
| 158 | closefile(FOUTPUT); | 155 | closefile(FOUTPUT); |
| 159 | current = stdout; | 156 | current = stdout; |
| @@ -161,7 +158,7 @@ static void io_writeto (void) | |||
| 161 | else if (lua_tag(f) == gettag(IOTAG)) | 158 | else if (lua_tag(f) == gettag(IOTAG)) |
| 162 | current = lua_getuserdata(f); | 159 | current = lua_getuserdata(f); |
| 163 | else { | 160 | else { |
| 164 | char *s = luaL_check_string(1); | 161 | char *s = luaL_check_string(FIRSTARG); |
| 165 | current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); | 162 | current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); |
| 166 | if (current == NULL) { | 163 | if (current == NULL) { |
| 167 | pushresult(0); | 164 | pushresult(0); |
| @@ -174,7 +171,7 @@ static void io_writeto (void) | |||
| 174 | 171 | ||
| 175 | static void io_appendto (void) | 172 | static void io_appendto (void) |
| 176 | { | 173 | { |
| 177 | char *s = luaL_check_string(1); | 174 | char *s = luaL_check_string(FIRSTARG); |
| 178 | FILE *fp = fopen (s, "a"); | 175 | FILE *fp = fopen (s, "a"); |
| 179 | if (fp != NULL) | 176 | if (fp != NULL) |
| 180 | setreturn(fp, FOUTPUT); | 177 | setreturn(fp, FOUTPUT); |
| @@ -187,7 +184,7 @@ static void io_appendto (void) | |||
| 187 | 184 | ||
| 188 | static void io_read (void) | 185 | static void io_read (void) |
| 189 | { | 186 | { |
| 190 | int arg = 1; | 187 | int arg = FIRSTARG; |
| 191 | FILE *f = getfileparam(FINPUT, &arg); | 188 | FILE *f = getfileparam(FINPUT, &arg); |
| 192 | char *buff; | 189 | char *buff; |
| 193 | char *p = luaL_opt_string(arg, "[^\n]*{\n}"); | 190 | char *p = luaL_opt_string(arg, "[^\n]*{\n}"); |
| @@ -238,7 +235,7 @@ static void io_read (void) | |||
| 238 | 235 | ||
| 239 | static void io_write (void) | 236 | static void io_write (void) |
| 240 | { | 237 | { |
| 241 | int arg = 1; | 238 | int arg = FIRSTARG; |
| 242 | FILE *f = getfileparam(FOUTPUT, &arg); | 239 | FILE *f = getfileparam(FOUTPUT, &arg); |
| 243 | int status = 1; | 240 | int status = 1; |
| 244 | char *s; | 241 | char *s; |
| @@ -370,11 +367,6 @@ static void errorfb (void) | |||
| 370 | 367 | ||
| 371 | static struct luaL_reg iolib[] = { | 368 | static struct luaL_reg iolib[] = { |
| 372 | {"setlocale", setloc}, | 369 | {"setlocale", setloc}, |
| 373 | {"readfrom", io_readfrom}, | ||
| 374 | {"writeto", io_writeto}, | ||
| 375 | {"appendto", io_appendto}, | ||
| 376 | {"read", io_read}, | ||
| 377 | {"write", io_write}, | ||
| 378 | {"execute", io_execute}, | 370 | {"execute", io_execute}, |
| 379 | {"remove", io_remove}, | 371 | {"remove", io_remove}, |
| 380 | {"rename", io_rename}, | 372 | {"rename", io_rename}, |
| @@ -386,16 +378,37 @@ static struct luaL_reg iolib[] = { | |||
| 386 | {"print_stack", errorfb} | 378 | {"print_stack", errorfb} |
| 387 | }; | 379 | }; |
| 388 | 380 | ||
| 381 | static struct luaL_reg iolibtag[] = { | ||
| 382 | {"readfrom", io_readfrom}, | ||
| 383 | {"writeto", io_writeto}, | ||
| 384 | {"appendto", io_appendto}, | ||
| 385 | {"read", io_read}, | ||
| 386 | {"write", io_write} | ||
| 387 | }; | ||
| 388 | |||
| 389 | static void openwithtags (void) | ||
| 390 | { | ||
| 391 | int iotag = lua_newtag(); | ||
| 392 | int closedtag = lua_newtag(); | ||
| 393 | int i; | ||
| 394 | for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) { | ||
| 395 | /* put both tags as upvalues for these functions */ | ||
| 396 | lua_pushnumber(iotag); | ||
| 397 | lua_pushnumber(closedtag); | ||
| 398 | lua_pushCclosure(iolibtag[i].func, 2); | ||
| 399 | lua_setglobal(iolibtag[i].name); | ||
| 400 | } | ||
| 401 | setfile(stdin, FINPUT, iotag); | ||
| 402 | setfile(stdout, FOUTPUT, iotag); | ||
| 403 | setfile(stdin, "_STDIN", iotag); | ||
| 404 | setfile(stdout, "_STDOUT", iotag); | ||
| 405 | setfile(stderr, "_STDERR", iotag); | ||
| 406 | } | ||
| 407 | |||
| 389 | void lua_iolibopen (void) | 408 | void lua_iolibopen (void) |
| 390 | { | 409 | { |
| 391 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); | 410 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); |
| 392 | createtag(IOTAG); | 411 | openwithtags(); |
| 393 | createtag(CLOSEDTAG); | ||
| 394 | setfile(stdin, FINPUT); | ||
| 395 | setfile(stdout, FOUTPUT); | ||
| 396 | setfile(stdin, "_STDIN"); | ||
| 397 | setfile(stdout, "_STDOUT"); | ||
| 398 | setfile(stderr, "_STDERR"); | ||
| 399 | lua_pushcfunction(errorfb); | 412 | lua_pushcfunction(errorfb); |
| 400 | lua_seterrormethod(); | 413 | lua_seterrormethod(); |
| 401 | } | 414 | } |
