1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

better the old way, using upvalues to keep iotag.

This commit is contained in:
Roberto Ierusalimschy 1999-04-05 16:47:05 -03:00
parent 7c9aee64c2
commit de04533dc0

101
liolib.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.35 1999/03/16 20:07:54 roberto Exp roberto $ ** $Id: liolib.c,v 1.36 1999/03/26 13:48:26 roberto Exp roberto $
** Standard I/O (and system) library ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -32,21 +32,25 @@
#endif #endif
#define CLOSEDTAG(tag) ((tag)-1) #define IOTAG 1
#define FIRSTARG 2 /* 1st is upvalue */
#define CLOSEDTAG(tag) ((tag)-1) /* assume that CLOSEDTAG = iotag-1 */
#define FINPUT "_INPUT" #define FINPUT "_INPUT"
#define FOUTPUT "_OUTPUT" #define FOUTPUT "_OUTPUT"
#define MODESIZE 3 /* string for file mode */
#ifdef POPEN #ifdef POPEN
FILE *popen(); FILE *popen();
int pclose(); int pclose();
#define CLOSEFILE(f) {if (pclose(f) == -1) fclose(f);}
#else #else
/* no support for popen */ /* no support for popen */
#define popen(x,y) NULL /* that is, popen always fails */ #define popen(x,y) NULL /* that is, popen always fails */
#define pclose(x) (-1) #define CLOSEFILE(f) {fclose(f);}
#endif #endif
@ -69,8 +73,7 @@ static void pushresult (int i) {
*/ */
static int gettag (void) { static int gettag (void) {
lua_pushusertag(stdin, LUA_ANYTAG); return (int)lua_getnumber(lua_getparam(IOTAG));
return lua_tag(lua_pop()); /* get the tag of stdin */
} }
@ -117,17 +120,10 @@ static FILE *getfileparam (char *name, int *arg) {
} }
static void rawclose (FILE *f) {
if (f != stdin && f != stdout) {
if (pclose(f) == -1) fclose(f);
}
}
static void closefile (FILE *f) { static void closefile (FILE *f) {
if (f != stdin && f != stdout) { if (f != stdin && f != stdout) {
int tag = gettag(); int tag = gettag();
rawclose(f); CLOSEFILE(f);
lua_pushusertag(f, tag); lua_pushusertag(f, tag);
lua_settag(CLOSEDTAG(tag)); lua_settag(CLOSEDTAG(tag));
} }
@ -135,22 +131,20 @@ static void closefile (FILE *f) {
static void io_close (void) { static void io_close (void) {
closefile(getnonullfile(1)); closefile(getnonullfile(FIRSTARG));
} }
static void gc_close (void) { static void gc_close (void) {
int tag = luaL_check_int(1); FILE *f = getnonullfile(FIRSTARG);
lua_Object fh = lua_getparam(2); if (f != stdin && f != stdout && f != stderr) {
FILE *f = lua_getuserdata(fh); CLOSEFILE(f);
luaL_arg_check(lua_isuserdata(fh) && lua_tag(fh) == tag, 2, }
"invalid file handle for GC");
rawclose(f);
} }
static void io_open (void) { static void io_open (void) {
FILE *f = fopen(luaL_check_string(1), luaL_check_string(2)); FILE *f = fopen(luaL_check_string(FIRSTARG), luaL_check_string(FIRSTARG+1));
if (f) lua_pushusertag(f, gettag()); if (f) lua_pushusertag(f, gettag());
else pushresult(0); else pushresult(0);
} }
@ -171,7 +165,7 @@ static void setreturn (FILE *f, char *name) {
static void io_readfrom (void) { static void io_readfrom (void) {
FILE *current; FILE *current;
lua_Object f = lua_getparam(1); lua_Object f = lua_getparam(FIRSTARG);
if (f == LUA_NOOBJECT) { if (f == LUA_NOOBJECT) {
closefile(getfilebyname(FINPUT)); closefile(getfilebyname(FINPUT));
current = stdin; current = stdin;
@ -179,7 +173,7 @@ static void io_readfrom (void) {
else if (lua_tag(f) == gettag()) /* deprecated option */ else if (lua_tag(f) == gettag()) /* deprecated option */
current = lua_getuserdata(f); current = lua_getuserdata(f);
else { else {
char *s = luaL_check_string(1); char *s = luaL_check_string(FIRSTARG);
current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
if (current == NULL) { if (current == NULL) {
pushresult(0); pushresult(0);
@ -192,7 +186,7 @@ static void io_readfrom (void) {
static void io_writeto (void) { static void io_writeto (void) {
FILE *current; FILE *current;
lua_Object f = lua_getparam(1); lua_Object f = lua_getparam(FIRSTARG);
if (f == LUA_NOOBJECT) { if (f == LUA_NOOBJECT) {
closefile(getfilebyname(FOUTPUT)); closefile(getfilebyname(FOUTPUT));
current = stdout; current = stdout;
@ -200,7 +194,7 @@ static void io_writeto (void) {
else if (lua_tag(f) == gettag()) /* deprecated option */ else if (lua_tag(f) == gettag()) /* deprecated option */
current = lua_getuserdata(f); current = lua_getuserdata(f);
else { else {
char *s = luaL_check_string(1); char *s = luaL_check_string(FIRSTARG);
current = (*s == '|') ? popen(s+1,"w") : fopen(s, "w"); current = (*s == '|') ? popen(s+1,"w") : fopen(s, "w");
if (current == NULL) { if (current == NULL) {
pushresult(0); pushresult(0);
@ -212,7 +206,7 @@ static void io_writeto (void) {
static void io_appendto (void) { static void io_appendto (void) {
FILE *fp = fopen(luaL_check_string(1), "a"); FILE *fp = fopen(luaL_check_string(FIRSTARG), "a");
if (fp != NULL) if (fp != NULL)
setreturn(fp, FOUTPUT); setreturn(fp, FOUTPUT);
else else
@ -317,7 +311,7 @@ static void read_file (FILE *f) {
static void io_read (void) { static void io_read (void) {
static char *options[] = {"*n", "*l", "*a", ".*", "*w", NULL}; static char *options[] = {"*n", "*l", "*a", ".*", "*w", NULL};
int arg = 1; int arg = FIRSTARG;
FILE *f = getfileparam(FINPUT, &arg); FILE *f = getfileparam(FINPUT, &arg);
char *p = luaL_opt_string(arg++, "*l"); char *p = luaL_opt_string(arg++, "*l");
do { /* repeat for each part */ do { /* repeat for each part */
@ -351,7 +345,7 @@ static void io_read (void) {
static void io_write (void) { static void io_write (void) {
int arg = 1; int arg = FIRSTARG;
FILE *f = getfileparam(FOUTPUT, &arg); FILE *f = getfileparam(FOUTPUT, &arg);
int status = 1; int status = 1;
char *s; char *s;
@ -365,10 +359,10 @@ static void io_write (void) {
static void io_seek (void) { static void io_seek (void) {
static int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
static char *modenames[] = {"set", "cur", "end", NULL}; static char *modenames[] = {"set", "cur", "end", NULL};
FILE *f = getnonullfile(1); FILE *f = getnonullfile(FIRSTARG);
int op = luaL_findstring(luaL_opt_string(2, "cur"), modenames); int op = luaL_findstring(luaL_opt_string(FIRSTARG+1, "cur"), modenames);
long offset = luaL_opt_long(3, 0); long offset = luaL_opt_long(FIRSTARG+2, 0);
luaL_arg_check(op != -1, 2, "invalid mode"); luaL_arg_check(op != -1, FIRSTARG+1, "invalid mode");
op = fseek(f, offset, mode[op]); op = fseek(f, offset, mode[op]);
if (op) if (op)
pushresult(0); /* error */ pushresult(0); /* error */
@ -378,8 +372,8 @@ static void io_seek (void) {
static void io_flush (void) { static void io_flush (void) {
FILE *f = getfile(1); FILE *f = getfile(FIRSTARG);
luaL_arg_check(f || lua_getparam(1) == LUA_NOOBJECT, 1, luaL_arg_check(f || lua_getparam(FIRSTARG) == LUA_NOOBJECT, FIRSTARG,
"invalid file handle"); "invalid file handle");
pushresult(fflush(f) == 0); pushresult(fflush(f) == 0);
} }
@ -532,44 +526,57 @@ static void errorfb (void) {
static struct luaL_reg iolib[] = { static struct luaL_reg iolib[] = {
{"_ERRORMESSAGE", errorfb}, {"_ERRORMESSAGE", errorfb},
{"appendto", io_appendto},
{"clock", io_clock}, {"clock", io_clock},
{"closefile", io_close},
{"date", io_date}, {"date", io_date},
{"debug", io_debug}, {"debug", io_debug},
{"execute", io_execute}, {"execute", io_execute},
{"exit", io_exit}, {"exit", io_exit},
{"flush", io_flush},
{"getenv", io_getenv}, {"getenv", io_getenv},
{"remove", io_remove},
{"rename", io_rename},
{"setlocale", setloc},
{"tmpname", io_tmpname}
};
static struct luaL_reg iolibtag[] = {
{"appendto", io_appendto},
{"closefile", io_close},
{"flush", io_flush},
{"openfile", io_open}, {"openfile", io_open},
{"read", io_read}, {"read", io_read},
{"readfrom", io_readfrom}, {"readfrom", io_readfrom},
{"remove", io_remove},
{"rename", io_rename},
{"seek", io_seek}, {"seek", io_seek},
{"setlocale", setloc},
{"tmpname", io_tmpname},
{"write", io_write}, {"write", io_write},
{"writeto", io_writeto} {"writeto", io_writeto}
}; };
void lua_iolibopen (void) { static void openwithtags (void) {
int i;
int iotag = lua_newtag(); int iotag = lua_newtag();
lua_newtag(); /* alloc CLOSEDTAG: assume that CLOSEDTAG = iotag-1 */ lua_newtag(); /* alloc CLOSEDTAG: assume that CLOSEDTAG = iotag-1 */
for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
/* put iotag as upvalue for these functions */
lua_pushnumber(iotag);
lua_pushcclosure(iolibtag[i].func, 1);
lua_setglobal(iolibtag[i].name);
}
/* predefined file handles */ /* predefined file handles */
setfile(stdin, FINPUT, iotag); setfile(stdin, FINPUT, iotag);
setfile(stdout, FOUTPUT, iotag); setfile(stdout, FOUTPUT, iotag);
setfile(stdin, "_STDIN", iotag); setfile(stdin, "_STDIN", iotag);
setfile(stdout, "_STDOUT", iotag); setfile(stdout, "_STDOUT", iotag);
setfile(stderr, "_STDERR", iotag); setfile(stderr, "_STDERR", iotag);
/* make sure stdin (with its tag) won't be collected */
lua_pushusertag(stdin, iotag); lua_ref(1);
/* close file when collected */ /* close file when collected */
lua_pushnumber(iotag); lua_pushnumber(iotag);
lua_pushcclosure(gc_close, 1); lua_pushcclosure(gc_close, 1);
lua_settagmethod(iotag, "gc"); lua_settagmethod(iotag, "gc");
/* register lib functions */ }
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
void lua_iolibopen (void) {
/* register lib functions */
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
openwithtags();
} }