diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-03-16 17:07:54 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-03-16 17:07:54 -0300 |
| commit | d6ff06751af3e52ae1e0e1697021fe884f104fc8 (patch) | |
| tree | 17c2b0837d6ee3e78c3bd982f98d00cbf58f4681 /liolib.c | |
| parent | 7a11c7f8e4a9913aa74fce8d1b395cac4b45de92 (diff) | |
| download | lua-d6ff06751af3e52ae1e0e1697021fe884f104fc8.tar.gz lua-d6ff06751af3e52ae1e0e1697021fe884f104fc8.tar.bz2 lua-d6ff06751af3e52ae1e0e1697021fe884f104fc8.zip | |
new functions openfile and closefile;
new way to keep file handle tags.
Diffstat (limited to 'liolib.c')
| -rw-r--r-- | liolib.c | 161 |
1 files changed, 79 insertions, 82 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 1.33 1999/03/05 20:45:01 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.34 1999/03/11 18:59:19 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,10 +32,8 @@ | |||
| 32 | #endif | 32 | #endif |
| 33 | 33 | ||
| 34 | 34 | ||
| 35 | #define CLOSEDTAG 2 | 35 | #define CLOSEDTAG(tag) ((tag)-1) |
| 36 | #define IOTAG 1 | ||
| 37 | 36 | ||
| 38 | #define FIRSTARG 3 /* 1st and 2nd are upvalues */ | ||
| 39 | 37 | ||
| 40 | #define FINPUT "_INPUT" | 38 | #define FINPUT "_INPUT" |
| 41 | #define FOUTPUT "_OUTPUT" | 39 | #define FOUTPUT "_OUTPUT" |
| @@ -70,20 +68,23 @@ static void pushresult (int i) { | |||
| 70 | ** ======================================================= | 68 | ** ======================================================= |
| 71 | */ | 69 | */ |
| 72 | 70 | ||
| 73 | static int gettag (int i) { | 71 | static int gettag (void) { |
| 74 | return (int)lua_getnumber(lua_getparam(i)); | 72 | lua_pushusertag(stdin, LUA_ANYTAG); |
| 73 | return lua_tag(lua_pop()); /* get the tag of stdin */ | ||
| 75 | } | 74 | } |
| 76 | 75 | ||
| 77 | 76 | ||
| 78 | static int ishandler (lua_Object f) { | 77 | static int ishandler (lua_Object f) { |
| 79 | if (lua_isuserdata(f)) { | 78 | if (lua_isuserdata(f)) { |
| 80 | if (lua_tag(f) == gettag(CLOSEDTAG)) | 79 | int tag = gettag(); |
| 80 | if (lua_tag(f) == CLOSEDTAG(tag)) | ||
| 81 | lua_error("cannot access a closed file"); | 81 | lua_error("cannot access a closed file"); |
| 82 | return lua_tag(f) == gettag(IOTAG); | 82 | return lua_tag(f) == tag; |
| 83 | } | 83 | } |
| 84 | else return 0; | 84 | else return 0; |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | |||
| 87 | static FILE *getfilebyname (char *name) { | 88 | static FILE *getfilebyname (char *name) { |
| 88 | lua_Object f = lua_getglobal(name); | 89 | lua_Object f = lua_getglobal(name); |
| 89 | if (!ishandler(f)) | 90 | if (!ishandler(f)) |
| @@ -98,6 +99,13 @@ static FILE *getfile (int arg) { | |||
| 98 | } | 99 | } |
| 99 | 100 | ||
| 100 | 101 | ||
| 102 | static FILE *getnonullfile (int arg) { | ||
| 103 | FILE *f = getfile(arg); | ||
| 104 | luaL_arg_check(f, arg, "invalid file handler"); | ||
| 105 | return f; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 101 | static FILE *getfileparam (char *name, int *arg) { | 109 | static FILE *getfileparam (char *name, int *arg) { |
| 102 | FILE *f = getfile(*arg); | 110 | FILE *f = getfile(*arg); |
| 103 | if (f) { | 111 | if (f) { |
| @@ -109,23 +117,26 @@ static FILE *getfileparam (char *name, int *arg) { | |||
| 109 | } | 117 | } |
| 110 | 118 | ||
| 111 | 119 | ||
| 112 | static void getmode (char mode, char *m) { | 120 | static void closefile (FILE *f) { |
| 113 | m[0] = mode; | 121 | if (f != stdin && f != stdout) { |
| 114 | if (*luaL_opt_string(FIRSTARG+1, "text") == 'b') { | 122 | int tag = gettag(); |
| 115 | m[1] = 'b'; | 123 | if (pclose(f) == -1) |
| 116 | m[2] = '\0'; | 124 | fclose(f); |
| 125 | lua_pushusertag(f, tag); | ||
| 126 | lua_settag(CLOSEDTAG(tag)); | ||
| 117 | } | 127 | } |
| 118 | else m[1] = '\0'; | ||
| 119 | } | 128 | } |
| 120 | 129 | ||
| 121 | 130 | ||
| 122 | static void closefile (char *name) { | 131 | static void io_close (void) { |
| 123 | FILE *f = getfilebyname(name); | 132 | closefile(getnonullfile(1)); |
| 124 | if (f == stdin || f == stdout) return; | 133 | } |
| 125 | if (pclose(f) == -1) | 134 | |
| 126 | fclose(f); | 135 | |
| 127 | lua_pushobject(lua_getglobal(name)); | 136 | static void io_open (void) { |
| 128 | lua_settag(gettag(CLOSEDTAG)); | 137 | FILE *f = fopen(luaL_check_string(1), luaL_check_string(2)); |
| 138 | if (f) lua_pushusertag(f, gettag()); | ||
| 139 | else pushresult(0); | ||
| 129 | } | 140 | } |
| 130 | 141 | ||
| 131 | 142 | ||
| @@ -136,7 +147,7 @@ static void setfile (FILE *f, char *name, int tag) { | |||
| 136 | 147 | ||
| 137 | 148 | ||
| 138 | static void setreturn (FILE *f, char *name) { | 149 | static void setreturn (FILE *f, char *name) { |
| 139 | int tag = gettag(IOTAG); | 150 | int tag = gettag(); |
| 140 | setfile(f, name, tag); | 151 | setfile(f, name, tag); |
| 141 | lua_pushusertag(f, tag); | 152 | lua_pushusertag(f, tag); |
| 142 | } | 153 | } |
| @@ -144,18 +155,16 @@ static void setreturn (FILE *f, char *name) { | |||
| 144 | 155 | ||
| 145 | static void io_readfrom (void) { | 156 | static void io_readfrom (void) { |
| 146 | FILE *current; | 157 | FILE *current; |
| 147 | lua_Object f = lua_getparam(FIRSTARG); | 158 | lua_Object f = lua_getparam(1); |
| 148 | if (f == LUA_NOOBJECT) { | 159 | if (f == LUA_NOOBJECT) { |
| 149 | closefile(FINPUT); | 160 | closefile(getfilebyname(FINPUT)); |
| 150 | current = stdin; | 161 | current = stdin; |
| 151 | } | 162 | } |
| 152 | else if (lua_tag(f) == gettag(IOTAG)) | 163 | else if (lua_tag(f) == gettag()) /* deprecated option */ |
| 153 | current = lua_getuserdata(f); | 164 | current = lua_getuserdata(f); |
| 154 | else { | 165 | else { |
| 155 | char *s = luaL_check_string(FIRSTARG); | 166 | char *s = luaL_check_string(1); |
| 156 | char m[MODESIZE]; | 167 | current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); |
| 157 | getmode('r', m); | ||
| 158 | current = (*s == '|') ? popen(s+1, "r") : fopen(s, m); | ||
| 159 | if (current == NULL) { | 168 | if (current == NULL) { |
| 160 | pushresult(0); | 169 | pushresult(0); |
| 161 | return; | 170 | return; |
| @@ -167,18 +176,16 @@ static void io_readfrom (void) { | |||
| 167 | 176 | ||
| 168 | static void io_writeto (void) { | 177 | static void io_writeto (void) { |
| 169 | FILE *current; | 178 | FILE *current; |
| 170 | lua_Object f = lua_getparam(FIRSTARG); | 179 | lua_Object f = lua_getparam(1); |
| 171 | if (f == LUA_NOOBJECT) { | 180 | if (f == LUA_NOOBJECT) { |
| 172 | closefile(FOUTPUT); | 181 | closefile(getfilebyname(FOUTPUT)); |
| 173 | current = stdout; | 182 | current = stdout; |
| 174 | } | 183 | } |
| 175 | else if (lua_tag(f) == gettag(IOTAG)) | 184 | else if (lua_tag(f) == gettag()) /* deprecated option */ |
| 176 | current = lua_getuserdata(f); | 185 | current = lua_getuserdata(f); |
| 177 | else { | 186 | else { |
| 178 | char *s = luaL_check_string(FIRSTARG); | 187 | char *s = luaL_check_string(1); |
| 179 | char m[MODESIZE]; | 188 | current = (*s == '|') ? popen(s+1,"w") : fopen(s, "w"); |
| 180 | getmode('w', m); | ||
| 181 | current = (*s == '|') ? popen(s+1,"w") : fopen(s, m); | ||
| 182 | if (current == NULL) { | 189 | if (current == NULL) { |
| 183 | pushresult(0); | 190 | pushresult(0); |
| 184 | return; | 191 | return; |
| @@ -189,11 +196,7 @@ static void io_writeto (void) { | |||
| 189 | 196 | ||
| 190 | 197 | ||
| 191 | static void io_appendto (void) { | 198 | static void io_appendto (void) { |
| 192 | char *s = luaL_check_string(FIRSTARG); | 199 | FILE *fp = fopen(luaL_check_string(1), "a"); |
| 193 | char m[MODESIZE]; | ||
| 194 | FILE *fp; | ||
| 195 | getmode('a', m); | ||
| 196 | fp = fopen (s, m); | ||
| 197 | if (fp != NULL) | 200 | if (fp != NULL) |
| 198 | setreturn(fp, FOUTPUT); | 201 | setreturn(fp, FOUTPUT); |
| 199 | else | 202 | else |
| @@ -298,7 +301,7 @@ static void read_file (FILE *f) { | |||
| 298 | 301 | ||
| 299 | static void io_read (void) { | 302 | static void io_read (void) { |
| 300 | static char *options[] = {"*n", "*l", "*a", ".*", "*w", NULL}; | 303 | static char *options[] = {"*n", "*l", "*a", ".*", "*w", NULL}; |
| 301 | int arg = FIRSTARG; | 304 | int arg = 1; |
| 302 | FILE *f = getfileparam(FINPUT, &arg); | 305 | FILE *f = getfileparam(FINPUT, &arg); |
| 303 | char *p = luaL_opt_string(arg++, "*l"); | 306 | char *p = luaL_opt_string(arg++, "*l"); |
| 304 | do { /* repeat for each part */ | 307 | do { /* repeat for each part */ |
| @@ -332,7 +335,7 @@ static void io_read (void) { | |||
| 332 | 335 | ||
| 333 | 336 | ||
| 334 | static void io_write (void) { | 337 | static void io_write (void) { |
| 335 | int arg = FIRSTARG; | 338 | int arg = 1; |
| 336 | FILE *f = getfileparam(FOUTPUT, &arg); | 339 | FILE *f = getfileparam(FOUTPUT, &arg); |
| 337 | int status = 1; | 340 | int status = 1; |
| 338 | char *s; | 341 | char *s; |
| @@ -346,11 +349,10 @@ static void io_write (void) { | |||
| 346 | static void io_seek (void) { | 349 | static void io_seek (void) { |
| 347 | static int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; | 350 | static int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; |
| 348 | static char *modenames[] = {"set", "cur", "end", NULL}; | 351 | static char *modenames[] = {"set", "cur", "end", NULL}; |
| 349 | FILE *f = getfile(FIRSTARG-1+1); | 352 | FILE *f = getnonullfile(1); |
| 350 | int op = luaL_findstring(luaL_opt_string(FIRSTARG-1+2, "cur"), modenames); | 353 | int op = luaL_findstring(luaL_opt_string(2, "cur"), modenames); |
| 351 | long offset = luaL_opt_long(FIRSTARG-1+3, 0); | 354 | long offset = luaL_opt_long(3, 0); |
| 352 | luaL_arg_check(f, FIRSTARG-1+1, "invalid file handler"); | 355 | luaL_arg_check(op != -1, 2, "invalid mode"); |
| 353 | luaL_arg_check(op != -1, FIRSTARG-1+2, "invalid mode"); | ||
| 354 | op = fseek(f, offset, mode[op]); | 356 | op = fseek(f, offset, mode[op]); |
| 355 | if (op) | 357 | if (op) |
| 356 | pushresult(0); /* error */ | 358 | pushresult(0); /* error */ |
| @@ -360,8 +362,8 @@ static void io_seek (void) { | |||
| 360 | 362 | ||
| 361 | 363 | ||
| 362 | static void io_flush (void) { | 364 | static void io_flush (void) { |
| 363 | FILE *f = getfile(FIRSTARG); | 365 | FILE *f = getfile(1); |
| 364 | luaL_arg_check(f || lua_getparam(FIRSTARG) == LUA_NOOBJECT, FIRSTARG, | 366 | luaL_arg_check(f || lua_getparam(1) == LUA_NOOBJECT, 1, |
| 365 | "invalid file handler"); | 367 | "invalid file handler"); |
| 366 | pushresult(fflush(f) == 0); | 368 | pushresult(fflush(f) == 0); |
| 367 | } | 369 | } |
| @@ -444,8 +446,9 @@ static void io_debug (void) { | |||
| 444 | for (;;) { | 446 | for (;;) { |
| 445 | char buffer[250]; | 447 | char buffer[250]; |
| 446 | fprintf(stderr, "lua_debug> "); | 448 | fprintf(stderr, "lua_debug> "); |
| 447 | if (fgets(buffer, sizeof(buffer), stdin) == 0) return; | 449 | if (fgets(buffer, sizeof(buffer), stdin) == 0 || |
| 448 | if (strcmp(buffer, "cont\n") == 0) return; | 450 | strcmp(buffer, "cont\n") == 0) |
| 451 | return; | ||
| 449 | lua_dostring(buffer); | 452 | lua_dostring(buffer); |
| 450 | } | 453 | } |
| 451 | } | 454 | } |
| @@ -512,49 +515,43 @@ static void errorfb (void) { | |||
| 512 | 515 | ||
| 513 | 516 | ||
| 514 | static struct luaL_reg iolib[] = { | 517 | static struct luaL_reg iolib[] = { |
| 515 | {"setlocale", setloc}, | 518 | {"_ERRORMESSAGE", errorfb}, |
| 516 | {"execute", io_execute}, | 519 | {"appendto", io_appendto}, |
| 517 | {"remove", io_remove}, | ||
| 518 | {"rename", io_rename}, | ||
| 519 | {"tmpname", io_tmpname}, | ||
| 520 | {"getenv", io_getenv}, | ||
| 521 | {"date", io_date}, | ||
| 522 | {"clock", io_clock}, | 520 | {"clock", io_clock}, |
| 523 | {"exit", io_exit}, | 521 | {"closefile", io_close}, |
| 522 | {"date", io_date}, | ||
| 524 | {"debug", io_debug}, | 523 | {"debug", io_debug}, |
| 525 | {"_ERRORMESSAGE", errorfb} | 524 | {"execute", io_execute}, |
| 526 | }; | 525 | {"exit", io_exit}, |
| 527 | |||
| 528 | static struct luaL_reg iolibtag[] = { | ||
| 529 | {"readfrom", io_readfrom}, | ||
| 530 | {"writeto", io_writeto}, | ||
| 531 | {"appendto", io_appendto}, | ||
| 532 | {"flush", io_flush}, | 526 | {"flush", io_flush}, |
| 527 | {"getenv", io_getenv}, | ||
| 528 | {"openfile", io_open}, | ||
| 533 | {"read", io_read}, | 529 | {"read", io_read}, |
| 530 | {"readfrom", io_readfrom}, | ||
| 531 | {"remove", io_remove}, | ||
| 532 | {"rename", io_rename}, | ||
| 534 | {"seek", io_seek}, | 533 | {"seek", io_seek}, |
| 535 | {"write", io_write} | 534 | {"setlocale", setloc}, |
| 535 | {"tmpname", io_tmpname}, | ||
| 536 | {"write", io_write}, | ||
| 537 | {"writeto", io_writeto} | ||
| 536 | }; | 538 | }; |
| 537 | 539 | ||
| 538 | static void openwithtags (void) { | 540 | |
| 541 | void lua_iolibopen (void) { | ||
| 539 | int iotag = lua_newtag(); | 542 | int iotag = lua_newtag(); |
| 540 | int closedtag = lua_newtag(); | 543 | lua_newtag(); /* alloc CLOSEDTAG: assume that CLOSEDTAG = iotag-1 */ |
| 541 | int i; | 544 | /* predefined file handles */ |
| 542 | for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) { | ||
| 543 | /* put both tags as upvalues for these functions */ | ||
| 544 | lua_pushnumber(iotag); | ||
| 545 | lua_pushnumber(closedtag); | ||
| 546 | lua_pushcclosure(iolibtag[i].func, 2); | ||
| 547 | lua_setglobal(iolibtag[i].name); | ||
| 548 | } | ||
| 549 | setfile(stdin, FINPUT, iotag); | 545 | setfile(stdin, FINPUT, iotag); |
| 550 | setfile(stdout, FOUTPUT, iotag); | 546 | setfile(stdout, FOUTPUT, iotag); |
| 551 | setfile(stdin, "_STDIN", iotag); | 547 | setfile(stdin, "_STDIN", iotag); |
| 552 | setfile(stdout, "_STDOUT", iotag); | 548 | setfile(stdout, "_STDOUT", iotag); |
| 553 | setfile(stderr, "_STDERR", iotag); | 549 | setfile(stderr, "_STDERR", iotag); |
| 554 | } | 550 | /* make sure stdin (with its tag) won't be collected */ |
| 555 | 551 | lua_pushusertag(stdin, iotag); lua_ref(1); | |
| 556 | void lua_iolibopen (void) { | 552 | /* close file when collected */ |
| 553 | lua_pushcfunction(io_close); lua_settagmethod(iotag, "gc"); | ||
| 554 | /* register lib functions */ | ||
| 557 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); | 555 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); |
| 558 | openwithtags(); | ||
| 559 | } | 556 | } |
| 560 | 557 | ||
