diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-11-27 13:59:44 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-11-27 13:59:44 -0200 |
commit | 024528e0c2ce75ac28ebbbc1220d4ae4045d3adf (patch) | |
tree | 5f147fede3540cfac8ef98c1bfeac5d99cf6157c /liolib.c | |
parent | ef37c87e9333d825838d421e4652076b70c83a72 (diff) | |
download | lua-024528e0c2ce75ac28ebbbc1220d4ae4045d3adf.tar.gz lua-024528e0c2ce75ac28ebbbc1220d4ae4045d3adf.tar.bz2 lua-024528e0c2ce75ac28ebbbc1220d4ae4045d3adf.zip |
global variables starting with '.' are protected in Lua (temporarily at
least...)
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 53 |
1 files changed, 28 insertions, 25 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.5 1997/11/19 17:29:23 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.6 1997/11/19 18:16:33 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,6 +32,13 @@ | |||
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | 34 | ||
35 | #define CLOSEDTAG ".CLOSEDTAG" | ||
36 | #define IOTAG ".IOTAG" | ||
37 | |||
38 | #define FINPUT "_INPUT" | ||
39 | #define FOUTPUT "_OUTPUT" | ||
40 | |||
41 | |||
35 | #ifdef POPEN | 42 | #ifdef POPEN |
36 | FILE *popen(); | 43 | FILE *popen(); |
37 | int pclose(); | 44 | int pclose(); |
@@ -43,18 +50,14 @@ int pclose(); | |||
43 | 50 | ||
44 | static void createtag (char *t) | 51 | static void createtag (char *t) |
45 | { | 52 | { |
46 | lua_pushobject(lua_globalbag()); | ||
47 | lua_pushstring(t); | ||
48 | lua_pushnumber(lua_newtag()); | 53 | lua_pushnumber(lua_newtag()); |
49 | lua_settable(); | 54 | lua_rawsetglobal(t); |
50 | } | 55 | } |
51 | 56 | ||
52 | 57 | ||
53 | static int gettag (char *t) | 58 | static int gettag (char *t) |
54 | { | 59 | { |
55 | lua_pushobject(lua_globalbag()); | 60 | return lua_getnumber(lua_rawgetglobal(t)); |
56 | lua_pushstring(t); | ||
57 | return lua_getnumber(lua_gettable()); | ||
58 | } | 61 | } |
59 | 62 | ||
60 | 63 | ||
@@ -72,9 +75,9 @@ static void pushresult (int i) | |||
72 | static int ishandler (lua_Object f) | 75 | static int ishandler (lua_Object f) |
73 | { | 76 | { |
74 | if (lua_isuserdata(f)) { | 77 | if (lua_isuserdata(f)) { |
75 | if (lua_tag(f) == gettag("closedtag")) | 78 | if (lua_tag(f) == gettag(CLOSEDTAG)) |
76 | lua_error("trying to access a closed file"); | 79 | lua_error("trying to access a closed file"); |
77 | return lua_tag(f) == gettag("tagio"); | 80 | return lua_tag(f) == gettag(IOTAG); |
78 | } | 81 | } |
79 | else return 0; | 82 | else return 0; |
80 | } | 83 | } |
@@ -107,13 +110,13 @@ static void closefile (char *name) | |||
107 | if (pclose(f) == -1) | 110 | if (pclose(f) == -1) |
108 | fclose(f); | 111 | fclose(f); |
109 | lua_pushobject(lua_getglobal(name)); | 112 | lua_pushobject(lua_getglobal(name)); |
110 | lua_settag(gettag("closedtag")); | 113 | lua_settag(gettag(CLOSEDTAG)); |
111 | } | 114 | } |
112 | 115 | ||
113 | 116 | ||
114 | static void setfile (FILE *f, char *name) | 117 | static void setfile (FILE *f, char *name) |
115 | { | 118 | { |
116 | lua_pushusertag(f, gettag("tagio")); | 119 | lua_pushusertag(f, gettag(IOTAG)); |
117 | lua_setglobal(name); | 120 | lua_setglobal(name); |
118 | } | 121 | } |
119 | 122 | ||
@@ -121,7 +124,7 @@ static void setfile (FILE *f, char *name) | |||
121 | static void setreturn (FILE *f, char *name) | 124 | static void setreturn (FILE *f, char *name) |
122 | { | 125 | { |
123 | setfile(f, name); | 126 | setfile(f, name); |
124 | lua_pushusertag(f, gettag("tagio")); | 127 | lua_pushusertag(f, gettag(IOTAG)); |
125 | } | 128 | } |
126 | 129 | ||
127 | 130 | ||
@@ -130,10 +133,10 @@ static void io_readfrom (void) | |||
130 | FILE *current; | 133 | FILE *current; |
131 | lua_Object f = lua_getparam(1); | 134 | lua_Object f = lua_getparam(1); |
132 | if (f == LUA_NOOBJECT) { | 135 | if (f == LUA_NOOBJECT) { |
133 | closefile("_INPUT"); | 136 | closefile(FINPUT); |
134 | current = stdin; | 137 | current = stdin; |
135 | } | 138 | } |
136 | else if (lua_tag(f) == gettag("tagio")) | 139 | else if (lua_tag(f) == gettag(IOTAG)) |
137 | current = lua_getuserdata(f); | 140 | current = lua_getuserdata(f); |
138 | else { | 141 | else { |
139 | char *s = luaL_check_string(1); | 142 | char *s = luaL_check_string(1); |
@@ -143,7 +146,7 @@ static void io_readfrom (void) | |||
143 | return; | 146 | return; |
144 | } | 147 | } |
145 | } | 148 | } |
146 | setreturn(current, "_INPUT"); | 149 | setreturn(current, FINPUT); |
147 | } | 150 | } |
148 | 151 | ||
149 | 152 | ||
@@ -152,10 +155,10 @@ static void io_writeto (void) | |||
152 | FILE *current; | 155 | FILE *current; |
153 | lua_Object f = lua_getparam(1); | 156 | lua_Object f = lua_getparam(1); |
154 | if (f == LUA_NOOBJECT) { | 157 | if (f == LUA_NOOBJECT) { |
155 | closefile("_OUTPUT"); | 158 | closefile(FOUTPUT); |
156 | current = stdout; | 159 | current = stdout; |
157 | } | 160 | } |
158 | else if (lua_tag(f) == gettag("tagio")) | 161 | else if (lua_tag(f) == gettag(IOTAG)) |
159 | current = lua_getuserdata(f); | 162 | current = lua_getuserdata(f); |
160 | else { | 163 | else { |
161 | char *s = luaL_check_string(1); | 164 | char *s = luaL_check_string(1); |
@@ -165,7 +168,7 @@ static void io_writeto (void) | |||
165 | return; | 168 | return; |
166 | } | 169 | } |
167 | } | 170 | } |
168 | setreturn(current, "_OUTPUT"); | 171 | setreturn(current, FOUTPUT); |
169 | } | 172 | } |
170 | 173 | ||
171 | 174 | ||
@@ -174,7 +177,7 @@ static void io_appendto (void) | |||
174 | char *s = luaL_check_string(1); | 177 | char *s = luaL_check_string(1); |
175 | FILE *fp = fopen (s, "a"); | 178 | FILE *fp = fopen (s, "a"); |
176 | if (fp != NULL) | 179 | if (fp != NULL) |
177 | setreturn(fp, "_OUTPUT"); | 180 | setreturn(fp, FOUTPUT); |
178 | else | 181 | else |
179 | pushresult(0); | 182 | pushresult(0); |
180 | } | 183 | } |
@@ -185,7 +188,7 @@ static void io_appendto (void) | |||
185 | static void io_read (void) | 188 | static void io_read (void) |
186 | { | 189 | { |
187 | int arg = 1; | 190 | int arg = 1; |
188 | FILE *f = getfileparam("_INPUT", &arg); | 191 | FILE *f = getfileparam(FINPUT, &arg); |
189 | char *buff; | 192 | char *buff; |
190 | char *p = luaL_opt_string(arg, "[^\n]*{\n}"); | 193 | char *p = luaL_opt_string(arg, "[^\n]*{\n}"); |
191 | int inskip = 0; /* to control {skips} */ | 194 | int inskip = 0; /* to control {skips} */ |
@@ -236,7 +239,7 @@ static void io_read (void) | |||
236 | static void io_write (void) | 239 | static void io_write (void) |
237 | { | 240 | { |
238 | int arg = 1; | 241 | int arg = 1; |
239 | FILE *f = getfileparam("_OUTPUT", &arg); | 242 | FILE *f = getfileparam(FOUTPUT, &arg); |
240 | int status = 1; | 243 | int status = 1; |
241 | char *s; | 244 | char *s; |
242 | while ((s = luaL_opt_string(arg++, NULL)) != NULL) | 245 | while ((s = luaL_opt_string(arg++, NULL)) != NULL) |
@@ -386,10 +389,10 @@ static struct luaL_reg iolib[] = { | |||
386 | void lua_iolibopen (void) | 389 | void lua_iolibopen (void) |
387 | { | 390 | { |
388 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); | 391 | luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0]))); |
389 | createtag("iotag"); | 392 | createtag(IOTAG); |
390 | createtag("closedtag"); | 393 | createtag(CLOSEDTAG); |
391 | setfile(stdin, "_INPUT"); | 394 | setfile(stdin, FINPUT); |
392 | setfile(stdout, "_OUTPUT"); | 395 | setfile(stdout, FOUTPUT); |
393 | setfile(stdin, "_STDIN"); | 396 | setfile(stdin, "_STDIN"); |
394 | setfile(stdout, "_STDOUT"); | 397 | setfile(stdout, "_STDOUT"); |
395 | setfile(stderr, "_STDERR"); | 398 | setfile(stderr, "_STDERR"); |