diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-11-23 11:49:35 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-11-23 11:49:35 -0200 |
commit | 35d6b1505702b0f4a2eee0e6d2f8dfc50943a1a7 (patch) | |
tree | a2c7dab0d015beda8ac85ff7458a997536f6ed31 /liolib.c | |
parent | 22914afab31d98b2a2e8f03f8141f186879e80d0 (diff) | |
download | lua-35d6b1505702b0f4a2eee0e6d2f8dfc50943a1a7.tar.gz lua-35d6b1505702b0f4a2eee0e6d2f8dfc50943a1a7.tar.bz2 lua-35d6b1505702b0f4a2eee0e6d2f8dfc50943a1a7.zip |
some cleaning
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 128 |
1 files changed, 38 insertions, 90 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.90 2000/10/27 16:15:53 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.91 2000/10/31 13:10:24 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 | */ |
@@ -51,6 +51,8 @@ int pclose(); */ | |||
51 | 51 | ||
52 | #define INFILE 0 | 52 | #define INFILE 0 |
53 | #define OUTFILE 1 | 53 | #define OUTFILE 1 |
54 | #define NOFILE 2 | ||
55 | |||
54 | 56 | ||
55 | typedef struct IOCtrl { | 57 | typedef struct IOCtrl { |
56 | int ref[2]; /* ref for strings _INPUT/_OUTPUT */ | 58 | int ref[2]; /* ref for strings _INPUT/_OUTPUT */ |
@@ -132,7 +134,8 @@ static int setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) { | |||
132 | if (f == NULL) | 134 | if (f == NULL) |
133 | return pushresult(L, 0); | 135 | return pushresult(L, 0); |
134 | else { | 136 | else { |
135 | setfile(L, ctrl, f, inout); | 137 | if (inout != NOFILE) |
138 | setfile(L, ctrl, f, inout); | ||
136 | lua_pushusertag(L, f, ctrl->iotag); | 139 | lua_pushusertag(L, f, ctrl->iotag); |
137 | return 1; | 140 | return 1; |
138 | } | 141 | } |
@@ -171,12 +174,13 @@ static int io_open (lua_State *L) { | |||
171 | FILE *f; | 174 | FILE *f; |
172 | lua_pop(L, 1); /* remove upvalue */ | 175 | lua_pop(L, 1); /* remove upvalue */ |
173 | f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2)); | 176 | f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2)); |
174 | if (f) { | 177 | return setreturn(L, ctrl, f, NOFILE); |
175 | lua_pushusertag(L, f, ctrl->iotag); | 178 | } |
176 | return 1; | 179 | |
177 | } | 180 | |
178 | else | 181 | static int io_tmpfile (lua_State *L) { |
179 | return pushresult(L, 0); | 182 | IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); |
183 | return setreturn(L, ctrl, tmpfile(), NOFILE); | ||
180 | } | 184 | } |
181 | 185 | ||
182 | 186 | ||
@@ -227,71 +231,9 @@ static int io_appendto (lua_State *L) { | |||
227 | 231 | ||
228 | 232 | ||
229 | 233 | ||
230 | #ifdef LUA_COMPAT_READPATTERN | ||
231 | |||
232 | /* | ||
233 | ** We cannot lookahead without need, because this can lock stdin. | ||
234 | ** This flag signals when we need to read a next char. | ||
235 | */ | ||
236 | #define NEED_OTHER (EOF-1) /* just some flag different from EOF */ | ||
237 | |||
238 | |||
239 | static int read_pattern (lua_State *L, FILE *f, const char *p) { | ||
240 | int inskip = 0; /* {skip} level */ | ||
241 | int c = NEED_OTHER; | ||
242 | luaL_Buffer b; | ||
243 | luaL_buffinit(L, &b); | ||
244 | while (*p != '\0') { | ||
245 | switch (*p) { | ||
246 | case '{': | ||
247 | inskip++; | ||
248 | p++; | ||
249 | continue; | ||
250 | case '}': | ||
251 | if (!inskip) lua_error(L, "unbalanced braces in read pattern"); | ||
252 | inskip--; | ||
253 | p++; | ||
254 | continue; | ||
255 | default: { | ||
256 | const char *ep = luaI_classend(L, p); /* get what is next */ | ||
257 | int m; /* match result */ | ||
258 | if (c == NEED_OTHER) c = getc(f); | ||
259 | m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep); | ||
260 | if (m) { | ||
261 | if (!inskip) luaL_putchar(&b, c); | ||
262 | c = NEED_OTHER; | ||
263 | } | ||
264 | switch (*ep) { | ||
265 | case '+': /* repetition (1 or more) */ | ||
266 | if (!m) goto break_while; /* pattern fails? */ | ||
267 | /* else go through */ | ||
268 | case '*': /* repetition (0 or more) */ | ||
269 | while (m) { /* reads the same item until it fails */ | ||
270 | c = getc(f); | ||
271 | m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep); | ||
272 | if (m && !inskip) luaL_putchar(&b, c); | ||
273 | } | ||
274 | /* go through to continue reading the pattern */ | ||
275 | case '?': /* optional */ | ||
276 | p = ep+1; /* continues reading the pattern */ | ||
277 | continue; | ||
278 | default: | ||
279 | if (!m) goto break_while; /* pattern fails? */ | ||
280 | p = ep; /* else continues reading the pattern */ | ||
281 | } | ||
282 | } | ||
283 | } | ||
284 | } break_while: | ||
285 | if (c != NEED_OTHER) ungetc(c, f); | ||
286 | luaL_pushresult(&b); /* close buffer */ | ||
287 | return (*p == '\0'); | ||
288 | } | ||
289 | |||
290 | #else | ||
291 | 234 | ||
292 | #define read_pattern(L, f, p) (lua_error(L, "read patterns are deprecated"), 0) | 235 | #define read_pattern(L, f, p) (lua_error(L, "read patterns are deprecated"), 0) |
293 | 236 | ||
294 | #endif | ||
295 | 237 | ||
296 | 238 | ||
297 | static int read_number (lua_State *L, FILE *f) { | 239 | static int read_number (lua_State *L, FILE *f) { |
@@ -400,8 +342,10 @@ static int io_read (lua_State *L) { | |||
400 | success = read_chars(L, f, (size_t)lua_tonumber(L, n)); | 342 | success = read_chars(L, f, (size_t)lua_tonumber(L, n)); |
401 | else { | 343 | else { |
402 | const char *p = luaL_check_string(L, n); | 344 | const char *p = luaL_check_string(L, n); |
403 | if (p[0] != '*') | 345 | if (p[0] != '*') { |
404 | success = read_pattern(L, f, p); /* deprecated! */ | 346 | lua_error(L, "read patterns are deprecated"); |
347 | success = 0; /* to avoid warnings */ | ||
348 | } | ||
405 | else { | 349 | else { |
406 | switch (p[1]) { | 350 | switch (p[1]) { |
407 | case 'n': /* number */ | 351 | case 'n': /* number */ |
@@ -516,7 +460,10 @@ static int io_rename (lua_State *L) { | |||
516 | 460 | ||
517 | 461 | ||
518 | static int io_tmpname (lua_State *L) { | 462 | static int io_tmpname (lua_State *L) { |
519 | lua_pushstring(L, tmpnam(NULL)); | 463 | char buff[L_tmpnam]; |
464 | if (tmpnam(buff) != buff) | ||
465 | lua_error(L, "unable to generate a unique filename"); | ||
466 | lua_pushstring(L, buff); | ||
520 | return 1; | 467 | return 1; |
521 | } | 468 | } |
522 | 469 | ||
@@ -548,7 +495,7 @@ static int io_date (lua_State *L) { | |||
548 | } | 495 | } |
549 | 496 | ||
550 | 497 | ||
551 | static int setloc (lua_State *L) { | 498 | static int io_setloc (lua_State *L) { |
552 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, | 499 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, |
553 | LC_NUMERIC, LC_TIME}; | 500 | LC_NUMERIC, LC_TIME}; |
554 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", | 501 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", |
@@ -658,28 +605,29 @@ static int errorfb (lua_State *L) { | |||
658 | static const struct luaL_reg iolib[] = { | 605 | static const struct luaL_reg iolib[] = { |
659 | {LUA_ERRORMESSAGE, errorfb}, | 606 | {LUA_ERRORMESSAGE, errorfb}, |
660 | {"clock", io_clock}, | 607 | {"clock", io_clock}, |
661 | {"date", io_date}, | 608 | {"date", io_date}, |
662 | {"debug", io_debug}, | 609 | {"debug", io_debug}, |
663 | {"execute", io_execute}, | 610 | {"execute", io_execute}, |
664 | {"exit", io_exit}, | 611 | {"exit", io_exit}, |
665 | {"getenv", io_getenv}, | 612 | {"getenv", io_getenv}, |
666 | {"remove", io_remove}, | 613 | {"remove", io_remove}, |
667 | {"rename", io_rename}, | 614 | {"rename", io_rename}, |
668 | {"setlocale", setloc}, | 615 | {"setlocale", io_setloc}, |
669 | {"tmpname", io_tmpname} | 616 | {"tmpname", io_tmpname} |
670 | }; | 617 | }; |
671 | 618 | ||
672 | 619 | ||
673 | static const struct luaL_reg iolibtag[] = { | 620 | static const struct luaL_reg iolibtag[] = { |
674 | {"appendto", io_appendto}, | 621 | {"appendto", io_appendto}, |
675 | {"closefile", io_close}, | 622 | {"closefile", io_close}, |
676 | {"flush", io_flush}, | 623 | {"flush", io_flush}, |
677 | {"openfile", io_open}, | 624 | {"openfile", io_open}, |
678 | {"read", io_read}, | 625 | {"read", io_read}, |
679 | {"readfrom", io_readfrom}, | 626 | {"readfrom", io_readfrom}, |
680 | {"seek", io_seek}, | 627 | {"seek", io_seek}, |
681 | {"write", io_write}, | 628 | {"tmpfile", io_tmpfile}, |
682 | {"writeto", io_writeto} | 629 | {"write", io_write}, |
630 | {"writeto", io_writeto} | ||
683 | }; | 631 | }; |
684 | 632 | ||
685 | 633 | ||