aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c37
1 files changed, 25 insertions, 12 deletions
diff --git a/liolib.c b/liolib.c
index a0464671..ffa5b16c 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.3 1997/10/30 20:29:09 roberto Exp roberto $ 2** $Id: liolib.c,v 1.4 1997/11/04 15:27:53 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*/
@@ -41,8 +41,21 @@ int pclose();
41#endif 41#endif
42 42
43 43
44int lua_tagio; 44static void createtag (char *t)
45static int closedtag; 45{
46 lua_pushobject(lua_globalbag());
47 lua_pushstring(t);
48 lua_pushnumber(lua_newtag());
49 lua_settable();
50}
51
52
53static int gettag (char *t)
54{
55 lua_pushobject(lua_globalbag());
56 lua_pushstring(t);
57 return lua_getnumber(lua_gettable());
58}
46 59
47 60
48static void pushresult (int i) 61static void pushresult (int i)
@@ -59,9 +72,9 @@ static void pushresult (int i)
59static int ishandler (lua_Object f) 72static int ishandler (lua_Object f)
60{ 73{
61 if (lua_isuserdata(f)) { 74 if (lua_isuserdata(f)) {
62 if (lua_tag(f) == closedtag) 75 if (lua_tag(f) == gettag("closedtag"))
63 lua_error("trying to access a closed file"); 76 lua_error("trying to access a closed file");
64 return lua_tag(f) == lua_tagio; 77 return lua_tag(f) == gettag("tagio");
65 } 78 }
66 else return 0; 79 else return 0;
67} 80}
@@ -94,13 +107,13 @@ static void closefile (char *name)
94 if (pclose(f) == -1) 107 if (pclose(f) == -1)
95 fclose(f); 108 fclose(f);
96 lua_pushobject(lua_getglobal(name)); 109 lua_pushobject(lua_getglobal(name));
97 lua_settag(closedtag); 110 lua_settag(gettag("closedtag"));
98} 111}
99 112
100 113
101static void setfile (FILE *f, char *name) 114static void setfile (FILE *f, char *name)
102{ 115{
103 lua_pushusertag(f, lua_tagio); 116 lua_pushusertag(f, gettag("tagio"));
104 lua_setglobal(name); 117 lua_setglobal(name);
105} 118}
106 119
@@ -108,7 +121,7 @@ static void setfile (FILE *f, char *name)
108static void setreturn (FILE *f, char *name) 121static void setreturn (FILE *f, char *name)
109{ 122{
110 setfile(f, name); 123 setfile(f, name);
111 lua_pushusertag(f, lua_tagio); 124 lua_pushusertag(f, gettag("tagio"));
112} 125}
113 126
114 127
@@ -120,7 +133,7 @@ static void io_readfrom (void)
120 closefile("_INPUT"); 133 closefile("_INPUT");
121 current = stdin; 134 current = stdin;
122 } 135 }
123 else if (lua_tag(f) == lua_tagio) 136 else if (lua_tag(f) == gettag("tagio"))
124 current = lua_getuserdata(f); 137 current = lua_getuserdata(f);
125 else { 138 else {
126 char *s = luaL_check_string(1); 139 char *s = luaL_check_string(1);
@@ -142,7 +155,7 @@ static void io_writeto (void)
142 closefile("_OUTPUT"); 155 closefile("_OUTPUT");
143 current = stdout; 156 current = stdout;
144 } 157 }
145 else if (lua_tag(f) == lua_tagio) 158 else if (lua_tag(f) == gettag("tagio"))
146 current = lua_getuserdata(f); 159 current = lua_getuserdata(f);
147 else { 160 else {
148 char *s = luaL_check_string(1); 161 char *s = luaL_check_string(1);
@@ -373,8 +386,8 @@ static struct luaL_reg iolib[] = {
373void lua_iolibopen (void) 386void lua_iolibopen (void)
374{ 387{
375 luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); 388 luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
376 lua_tagio = lua_newtag(); 389 createtag("iotag");
377 closedtag = lua_newtag(); 390 createtag("closedtag");
378 setfile(stdin, "_INPUT"); 391 setfile(stdin, "_INPUT");
379 setfile(stdout, "_OUTPUT"); 392 setfile(stdout, "_OUTPUT");
380 setfile(stdin, "_STDIN"); 393 setfile(stdin, "_STDIN");