aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c245
1 files changed, 122 insertions, 123 deletions
diff --git a/liolib.c b/liolib.c
index 4114eae3..16adf983 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.123 2001/10/02 16:41:36 roberto Exp $ 2** $Id: liolib.c,v 1.124 2001/10/26 17:33:30 roberto Exp $
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*/
@@ -10,7 +10,6 @@
10#include <string.h> 10#include <string.h>
11#include <time.h> 11#include <time.h>
12 12
13#define LUA_PRIVATE
14#include "lua.h" 13#include "lua.h"
15 14
16#include "lauxlib.h" 15#include "lauxlib.h"
@@ -40,12 +39,12 @@ int pclose(); */
40#define OUTFILE 1 39#define OUTFILE 1
41#define NOFILE 2 40#define NOFILE 2
42 41
43#define FILEHANDLE l_s("FileHandle") 42#define FILEHANDLE "FileHandle"
44#define CLOSEDFILEHANDLE l_s("ClosedFileHandle") 43#define CLOSEDFILEHANDLE "ClosedFileHandle"
45 44
46 45
47static const l_char *const filenames[] = {l_s("_INPUT"), l_s("_OUTPUT")}; 46static const char *const filenames[] = {"_INPUT", "_OUTPUT"};
48static const l_char *const basicfiles[] = {l_s("_STDIN"), l_s("_STDOUT")}; 47static const char *const basicfiles[] = {"_STDIN", "_STDOUT"};
49 48
50 49
51static int pushresult (lua_State *L, int i) { 50static int pushresult (lua_State *L, int i) {
@@ -77,16 +76,16 @@ static FILE *getopthandle (lua_State *L, int inout) {
77 if (p != NULL) { /* is it a userdata ? */ 76 if (p != NULL) { /* is it a userdata ? */
78 if (!checkfile(L, 1)) { /* not a valid file handle? */ 77 if (!checkfile(L, 1)) { /* not a valid file handle? */
79 if (strcmp(lua_type(L, 1), CLOSEDFILEHANDLE) == 0) 78 if (strcmp(lua_type(L, 1), CLOSEDFILEHANDLE) == 0)
80 luaL_argerror(L, 1, l_s("file is closed")); 79 luaL_argerror(L, 1, "file is closed");
81 else 80 else
82 luaL_argerror(L, 1, l_s("(invalid value)")); 81 luaL_argerror(L, 1, "(invalid value)");
83 } 82 }
84 lua_pushvalue(L, 1); lua_remove(L, 1); /* move it to stack top */ 83 lua_pushvalue(L, 1); lua_remove(L, 1); /* move it to stack top */
85 } 84 }
86 else { /* try global value */ 85 else { /* try global value */
87 lua_getglobal(L, filenames[inout]); 86 lua_getglobal(L, filenames[inout]);
88 if (!checkfile(L,-1)) 87 if (!checkfile(L,-1))
89 luaL_verror(L, l_s("global variable `%.10s' is not a valid file handle"), 88 luaL_verror(L, "global variable `%.10s' is not a valid file handle",
90 filenames[inout]); 89 filenames[inout]);
91 p = (FILE *)(lua_touserdata(L, -1)); 90 p = (FILE *)(lua_touserdata(L, -1));
92 } 91 }
@@ -100,7 +99,7 @@ static void newfile (lua_State *L, FILE *f) {
100} 99}
101 100
102 101
103static void newfilewithname (lua_State *L, FILE *f, const l_char *name) { 102static void newfilewithname (lua_State *L, FILE *f, const char *name) {
104 newfile(L, f); 103 newfile(L, f);
105 lua_setglobal(L, name); 104 lua_setglobal(L, name);
106} 105}
@@ -158,7 +157,7 @@ static int io_tmpfile (lua_State *L) {
158 157
159 158
160 159
161static int io_fromto (lua_State *L, int inout, const l_char *mode) { 160static int io_fromto (lua_State *L, int inout, const char *mode) {
162 FILE *current; 161 FILE *current;
163 if (lua_isnull(L, 1)) { 162 if (lua_isnull(L, 1)) {
164 getopthandle(L, inout); 163 getopthandle(L, inout);
@@ -166,25 +165,25 @@ static int io_fromto (lua_State *L, int inout, const l_char *mode) {
166 return io_close(L); 165 return io_close(L);
167 } 166 }
168 else { 167 else {
169 const l_char *s = luaL_check_string(L, 1); 168 const char *s = luaL_check_string(L, 1);
170 current = (*s == l_c('|')) ? popen(s+1, mode) : fopen(s, mode); 169 current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode);
171 return setnewfile(L, current, inout); 170 return setnewfile(L, current, inout);
172 } 171 }
173} 172}
174 173
175 174
176static int io_readfrom (lua_State *L) { 175static int io_readfrom (lua_State *L) {
177 return io_fromto(L, INFILE, l_s("r")); 176 return io_fromto(L, INFILE, "r");
178} 177}
179 178
180 179
181static int io_writeto (lua_State *L) { 180static int io_writeto (lua_State *L) {
182 return io_fromto(L, OUTFILE, l_s("w")); 181 return io_fromto(L, OUTFILE, "w");
183} 182}
184 183
185 184
186static int io_appendto (lua_State *L) { 185static int io_appendto (lua_State *L) {
187 FILE *current = fopen(luaL_check_string(L, 1), l_s("a")); 186 FILE *current = fopen(luaL_check_string(L, 1), "a");
188 return setnewfile(L, current, OUTFILE); 187 return setnewfile(L, current, OUTFILE);
189} 188}
190 189
@@ -208,7 +207,7 @@ static int io_appendto (lua_State *L) {
208** Addison-Wesley, 1993.) 207** Addison-Wesley, 1993.)
209*/ 208*/
210 209
211static void prep_read_until (int next[], const l_char *p, int pl) { 210static void prep_read_until (int next[], const char *p, int pl) {
212 int i = 0; 211 int i = 0;
213 int j = -1; 212 int j = -1;
214 next[0] = -1; 213 next[0] = -1;
@@ -221,8 +220,8 @@ static void prep_read_until (int next[], const l_char *p, int pl) {
221} 220}
222 221
223 222
224static int read_until (lua_State *L, FILE *f, const l_char *p, int pl) { 223static int read_until (lua_State *L, FILE *f, const char *p, int pl) {
225 l_charint c; 224 int c;
226 int j; 225 int j;
227 int next[LUA_MAXUNTIL+1]; 226 int next[LUA_MAXUNTIL+1];
228 luaL_Buffer b; 227 luaL_Buffer b;
@@ -255,7 +254,7 @@ static int read_until (lua_State *L, FILE *f, const l_char *p, int pl) {
255 254
256static int read_number (lua_State *L, FILE *f) { 255static int read_number (lua_State *L, FILE *f) {
257 lua_Number d; 256 lua_Number d;
258 if (fscanf(f, l_s(LUA_NUMBER_SCAN), &d) == 1) { 257 if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
259 lua_pushnumber(L, d); 258 lua_pushnumber(L, d);
260 return 1; 259 return 1;
261 } 260 }
@@ -264,7 +263,7 @@ static int read_number (lua_State *L, FILE *f) {
264 263
265 264
266static int test_eof (lua_State *L, FILE *f) { 265static int test_eof (lua_State *L, FILE *f) {
267 l_charint c = getc(f); 266 int c = getc(f);
268 ungetc(c, f); 267 ungetc(c, f);
269 lua_pushlstring(L, NULL, 0); 268 lua_pushlstring(L, NULL, 0);
270 return (c != EOF); 269 return (c != EOF);
@@ -278,9 +277,9 @@ static int read_chars (lua_State *L, FILE *f, size_t n) {
278 luaL_buffinit(L, &b); 277 luaL_buffinit(L, &b);
279 rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ 278 rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
280 do { 279 do {
281 l_char *p = luaL_prepbuffer(&b); 280 char *p = luaL_prepbuffer(&b);
282 if (rlen > n) rlen = n; /* cannot read more than asked */ 281 if (rlen > n) rlen = n; /* cannot read more than asked */
283 nr = fread(p, sizeof(l_char), rlen, f); 282 nr = fread(p, sizeof(char), rlen, f);
284 luaL_addsize(&b, nr); 283 luaL_addsize(&b, nr);
285 n -= nr; /* still have to read `n' chars */ 284 n -= nr; /* still have to read `n' chars */
286 } while (n > 0 && nr == rlen); /* until end of count or eof */ 285 } while (n > 0 && nr == rlen); /* until end of count or eof */
@@ -295,11 +294,11 @@ static int io_read (lua_State *L) {
295 int success; 294 int success;
296 int n; 295 int n;
297 if (nargs == 0) { /* no arguments? */ 296 if (nargs == 0) { /* no arguments? */
298 success = read_until(L, f, l_s("\n"), 1); /* read until \n (a line) */ 297 success = read_until(L, f, "\n", 1); /* read until \n (a line) */
299 n = 2; /* will return n-1 results */ 298 n = 2; /* will return n-1 results */
300 } 299 }
301 else { /* ensure stack space for all results and for auxlib's buffer */ 300 else { /* ensure stack space for all results and for auxlib's buffer */
302 luaL_check_stack(L, nargs+LUA_MINSTACK, l_s("too many arguments")); 301 luaL_check_stack(L, nargs+LUA_MINSTACK, "too many arguments");
303 success = 1; 302 success = 1;
304 for (n = 1; n<=nargs && success; n++) { 303 for (n = 1; n<=nargs && success; n++) {
305 if (lua_rawtag(L, n) == LUA_TNUMBER) { 304 if (lua_rawtag(L, n) == LUA_TNUMBER) {
@@ -307,32 +306,32 @@ static int io_read (lua_State *L) {
307 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); 306 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
308 } 307 }
309 else { 308 else {
310 const l_char *p = lua_tostring(L, n); 309 const char *p = lua_tostring(L, n);
311 if (!p || p[0] != l_c('*')) 310 if (!p || p[0] != '*')
312 lua_error(L, l_s("invalid `read' option")); 311 lua_error(L, "invalid `read' option");
313 switch (p[1]) { 312 switch (p[1]) {
314 case l_c('n'): /* number */ 313 case 'n': /* number */
315 success = read_number(L, f); 314 success = read_number(L, f);
316 break; 315 break;
317 case l_c('l'): /* line */ 316 case 'l': /* line */
318 success = read_until(L, f, l_s("\n"), 1); /* read until \n */ 317 success = read_until(L, f, "\n", 1); /* read until \n */
319 break; 318 break;
320 case l_c('a'): /* file */ 319 case 'a': /* file */
321 read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ 320 read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
322 success = 1; /* always success */ 321 success = 1; /* always success */
323 break; 322 break;
324 case l_c('w'): /* word */ 323 case 'w': /* word */
325 lua_error(L, l_s("obsolete option `*w'")); 324 lua_error(L, "obsolete option `*w'");
326 break; 325 break;
327 case l_c('u'): { /* read until */ 326 case 'u': { /* read until */
328 size_t pl = lua_strlen(L, n) - 2; 327 size_t pl = lua_strlen(L, n) - 2;
329 luaL_arg_check(L, 0 < pl && pl <= LUA_MAXUNTIL, n, 328 luaL_arg_check(L, 0 < pl && pl <= LUA_MAXUNTIL, n,
330 l_s("invalid read-until length")); 329 "invalid read-until length");
331 success = read_until(L, f, p+2, (int)(pl)); 330 success = read_until(L, f, p+2, (int)(pl));
332 break; 331 break;
333 } 332 }
334 default: 333 default:
335 luaL_argerror(L, n, l_s("invalid format")); 334 luaL_argerror(L, n, "invalid format");
336 success = 0; /* to avoid warnings */ 335 success = 0; /* to avoid warnings */
337 } 336 }
338 } 337 }
@@ -357,12 +356,12 @@ static int io_write (lua_State *L) {
357 if (lua_rawtag(L, arg) == LUA_TNUMBER) { 356 if (lua_rawtag(L, arg) == LUA_TNUMBER) {
358 /* optimization: could be done exactly as for strings */ 357 /* optimization: could be done exactly as for strings */
359 status = status && 358 status = status &&
360 fprintf(f, l_s(LUA_NUMBER_FMT), lua_tonumber(L, arg)) > 0; 359 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
361 } 360 }
362 else { 361 else {
363 size_t l; 362 size_t l;
364 const l_char *s = luaL_check_lstr(L, arg, &l); 363 const char *s = luaL_check_lstr(L, arg, &l);
365 status = status && (fwrite(s, sizeof(l_char), l, f) == l); 364 status = status && (fwrite(s, sizeof(char), l, f) == l);
366 } 365 }
367 } 366 }
368 pushresult(L, status); 367 pushresult(L, status);
@@ -372,11 +371,11 @@ static int io_write (lua_State *L) {
372 371
373static int io_seek (lua_State *L) { 372static int io_seek (lua_State *L) {
374 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; 373 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
375 static const l_char *const modenames[] = {l_s("set"), l_s("cur"), l_s("end"), NULL}; 374 static const char *const modenames[] = {"set", "cur", "end", NULL};
376 FILE *f = (FILE *)(luaL_check_userdata(L, 1, FILEHANDLE)); 375 FILE *f = (FILE *)(luaL_check_userdata(L, 1, FILEHANDLE));
377 int op = luaL_findstring(luaL_opt_string(L, 2, l_s("cur")), modenames); 376 int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
378 long offset = luaL_opt_long(L, 3, 0); 377 long offset = luaL_opt_long(L, 3, 0);
379 luaL_arg_check(L, op != -1, 2, l_s("invalid mode")); 378 luaL_arg_check(L, op != -1, 2, "invalid mode");
380 op = fseek(f, offset, mode[op]); 379 op = fseek(f, offset, mode[op]);
381 if (op) 380 if (op)
382 return pushresult(L, 0); /* error */ 381 return pushresult(L, 0); /* error */
@@ -420,9 +419,9 @@ static int io_rename (lua_State *L) {
420 419
421 420
422static int io_tmpname (lua_State *L) { 421static int io_tmpname (lua_State *L) {
423 l_char buff[L_tmpnam]; 422 char buff[L_tmpnam];
424 if (tmpnam(buff) != buff) 423 if (tmpnam(buff) != buff)
425 lua_error(L, l_s("unable to generate a unique filename")); 424 lua_error(L, "unable to generate a unique filename");
426 lua_pushstring(L, buff); 425 lua_pushstring(L, buff);
427 return 1; 426 return 1;
428} 427}
@@ -449,14 +448,14 @@ static int io_clock (lua_State *L) {
449** ======================================================= 448** =======================================================
450*/ 449*/
451 450
452static void setfield (lua_State *L, const l_char *key, int value) { 451static void setfield (lua_State *L, const char *key, int value) {
453 lua_pushstring(L, key); 452 lua_pushstring(L, key);
454 lua_pushnumber(L, value); 453 lua_pushnumber(L, value);
455 lua_rawset(L, -3); 454 lua_rawset(L, -3);
456} 455}
457 456
458 457
459static int getfield (lua_State *L, const l_char *key, int d) { 458static int getfield (lua_State *L, const char *key, int d) {
460 int res; 459 int res;
461 lua_pushstring(L, key); 460 lua_pushstring(L, key);
462 lua_rawget(L, -2); 461 lua_rawget(L, -2);
@@ -464,7 +463,7 @@ static int getfield (lua_State *L, const l_char *key, int d) {
464 res = (int)(lua_tonumber(L, -1)); 463 res = (int)(lua_tonumber(L, -1));
465 else { 464 else {
466 if (d == -2) 465 if (d == -2)
467 luaL_verror(L, l_s("field `%.20s' missing in date table"), key); 466 luaL_verror(L, "field `%.20s' missing in date table", key);
468 res = d; 467 res = d;
469 } 468 }
470 lua_pop(L, 1); 469 lua_pop(L, 1);
@@ -473,12 +472,12 @@ static int getfield (lua_State *L, const l_char *key, int d) {
473 472
474 473
475static int io_date (lua_State *L) { 474static int io_date (lua_State *L) {
476 const l_char *s = luaL_opt_string(L, 1, l_s("%c")); 475 const char *s = luaL_opt_string(L, 1, "%c");
477 time_t t = (time_t)(luaL_opt_number(L, 2, -1)); 476 time_t t = (time_t)(luaL_opt_number(L, 2, -1));
478 struct tm *stm; 477 struct tm *stm;
479 if (t == (time_t)(-1)) /* no time given? */ 478 if (t == (time_t)(-1)) /* no time given? */
480 t = time(NULL); /* use current time */ 479 t = time(NULL); /* use current time */
481 if (*s == l_c('!')) { /* UTC? */ 480 if (*s == '!') { /* UTC? */
482 stm = gmtime(&t); 481 stm = gmtime(&t);
483 s++; /* skip `!' */ 482 s++; /* skip `!' */
484 } 483 }
@@ -486,24 +485,24 @@ static int io_date (lua_State *L) {
486 stm = localtime(&t); 485 stm = localtime(&t);
487 if (stm == NULL) /* invalid date? */ 486 if (stm == NULL) /* invalid date? */
488 lua_pushnil(L); 487 lua_pushnil(L);
489 else if (strcmp(s, l_s("*t")) == 0) { 488 else if (strcmp(s, "*t") == 0) {
490 lua_newtable(L); 489 lua_newtable(L);
491 setfield(L, l_s("sec"), stm->tm_sec); 490 setfield(L, "sec", stm->tm_sec);
492 setfield(L, l_s("min"), stm->tm_min); 491 setfield(L, "min", stm->tm_min);
493 setfield(L, l_s("hour"), stm->tm_hour); 492 setfield(L, "hour", stm->tm_hour);
494 setfield(L, l_s("day"), stm->tm_mday); 493 setfield(L, "day", stm->tm_mday);
495 setfield(L, l_s("month"), stm->tm_mon+1); 494 setfield(L, "month", stm->tm_mon+1);
496 setfield(L, l_s("year"), stm->tm_year+1900); 495 setfield(L, "year", stm->tm_year+1900);
497 setfield(L, l_s("wday"), stm->tm_wday+1); 496 setfield(L, "wday", stm->tm_wday+1);
498 setfield(L, l_s("yday"), stm->tm_yday+1); 497 setfield(L, "yday", stm->tm_yday+1);
499 setfield(L, l_s("isdst"), stm->tm_isdst); 498 setfield(L, "isdst", stm->tm_isdst);
500 } 499 }
501 else { 500 else {
502 l_char b[256]; 501 char b[256];
503 if (strftime(b, sizeof(b), s, stm)) 502 if (strftime(b, sizeof(b), s, stm))
504 lua_pushstring(L, b); 503 lua_pushstring(L, b);
505 else 504 else
506 lua_error(L, l_s("invalid `date' format")); 505 lua_error(L, "invalid `date' format");
507 } 506 }
508 return 1; 507 return 1;
509} 508}
@@ -517,13 +516,13 @@ static int io_time (lua_State *L) {
517 struct tm ts; 516 struct tm ts;
518 luaL_check_rawtype(L, 1, LUA_TTABLE); 517 luaL_check_rawtype(L, 1, LUA_TTABLE);
519 lua_settop(L, 1); /* make sure table is at the top */ 518 lua_settop(L, 1); /* make sure table is at the top */
520 ts.tm_sec = getfield(L, l_s("sec"), 0); 519 ts.tm_sec = getfield(L, "sec", 0);
521 ts.tm_min = getfield(L, l_s("min"), 0); 520 ts.tm_min = getfield(L, "min", 0);
522 ts.tm_hour = getfield(L, l_s("hour"), 12); 521 ts.tm_hour = getfield(L, "hour", 12);
523 ts.tm_mday = getfield(L, l_s("day"), -2); 522 ts.tm_mday = getfield(L, "day", -2);
524 ts.tm_mon = getfield(L, l_s("month"), -2)-1; 523 ts.tm_mon = getfield(L, "month", -2)-1;
525 ts.tm_year = getfield(L, l_s("year"), -2)-1900; 524 ts.tm_year = getfield(L, "year", -2)-1900;
526 ts.tm_isdst = getfield(L, l_s("isdst"), -1); 525 ts.tm_isdst = getfield(L, "isdst", -1);
527 t = mktime(&ts); 526 t = mktime(&ts);
528 if (t == (time_t)(-1)) 527 if (t == (time_t)(-1))
529 lua_pushnil(L); 528 lua_pushnil(L);
@@ -546,10 +545,10 @@ static int io_difftime (lua_State *L) {
546static int io_setloc (lua_State *L) { 545static int io_setloc (lua_State *L) {
547 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, 546 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
548 LC_NUMERIC, LC_TIME}; 547 LC_NUMERIC, LC_TIME};
549 static const l_char *const catnames[] = {l_s("all"), l_s("collate"), l_s("ctype"), l_s("monetary"), 548 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
550 l_s("numeric"), l_s("time"), NULL}; 549 "numeric", "time", NULL};
551 int op = luaL_findstring(luaL_opt_string(L, 2, l_s("all")), catnames); 550 int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
552 luaL_arg_check(L, op != -1, 2, l_s("invalid option")); 551 luaL_arg_check(L, op != -1, 2, "invalid option");
553 lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1))); 552 lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));
554 return 1; 553 return 1;
555} 554}
@@ -566,10 +565,10 @@ static int io_exit (lua_State *L) {
566 565
567static int io_debug (lua_State *L) { 566static int io_debug (lua_State *L) {
568 for (;;) { 567 for (;;) {
569 l_char buffer[250]; 568 char buffer[250];
570 fprintf(stderr, l_s("lua_debug> ")); 569 fprintf(stderr, "lua_debug> ");
571 if (fgets(buffer, sizeof(buffer), stdin) == 0 || 570 if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
572 strcmp(buffer, l_s("cont\n")) == 0) 571 strcmp(buffer, "cont\n") == 0)
573 return 0; 572 return 0;
574 lua_dostring(L, buffer); 573 lua_dostring(L, buffer);
575 lua_settop(L, 0); /* remove eventual returns */ 574 lua_settop(L, 0); /* remove eventual returns */
@@ -586,61 +585,61 @@ static int errorfb (lua_State *L) {
586 lua_Debug ar; 585 lua_Debug ar;
587 luaL_Buffer b; 586 luaL_Buffer b;
588 luaL_buffinit(L, &b); 587 luaL_buffinit(L, &b);
589 luaL_addstring(&b, l_s("error: ")); 588 luaL_addstring(&b, "error: ");
590 luaL_addstring(&b, luaL_check_string(L, 1)); 589 luaL_addstring(&b, luaL_check_string(L, 1));
591 luaL_addstring(&b, l_s("\n")); 590 luaL_addstring(&b, "\n");
592 while (lua_getstack(L, level++, &ar)) { 591 while (lua_getstack(L, level++, &ar)) {
593 l_char buff[120]; /* enough to fit following `sprintf's */ 592 char buff[120]; /* enough to fit following `sprintf's */
594 if (level == 2) 593 if (level == 2)
595 luaL_addstring(&b, l_s("stack traceback:\n")); 594 luaL_addstring(&b, "stack traceback:\n");
596 else if (level > LEVELS1 && firstpart) { 595 else if (level > LEVELS1 && firstpart) {
597 /* no more than `LEVELS2' more levels? */ 596 /* no more than `LEVELS2' more levels? */
598 if (!lua_getstack(L, level+LEVELS2, &ar)) 597 if (!lua_getstack(L, level+LEVELS2, &ar))
599 level--; /* keep going */ 598 level--; /* keep going */
600 else { 599 else {
601 luaL_addstring(&b, l_s(" ...\n")); /* too many levels */ 600 luaL_addstring(&b, " ...\n"); /* too many levels */
602 while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ 601 while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
603 level++; 602 level++;
604 } 603 }
605 firstpart = 0; 604 firstpart = 0;
606 continue; 605 continue;
607 } 606 }
608 sprintf(buff, l_s("%4d: "), level-1); 607 sprintf(buff, "%4d: ", level-1);
609 luaL_addstring(&b, buff); 608 luaL_addstring(&b, buff);
610 lua_getinfo(L, l_s("Snl"), &ar); 609 lua_getinfo(L, "Snl", &ar);
611 switch (*ar.namewhat) { 610 switch (*ar.namewhat) {
612 case l_c('g'): case l_c('l'): /* global, local */ 611 case 'g': case 'l': /* global, local */
613 sprintf(buff, l_s("function `%.50s'"), ar.name); 612 sprintf(buff, "function `%.50s'", ar.name);
614 break; 613 break;
615 case l_c('f'): /* field */ 614 case 'f': /* field */
616 sprintf(buff, l_s("method `%.50s'"), ar.name); 615 sprintf(buff, "method `%.50s'", ar.name);
617 break; 616 break;
618 case l_c('t'): /* tag method */ 617 case 't': /* tag method */
619 sprintf(buff, l_s("`%.50s' tag method"), ar.name); 618 sprintf(buff, "`%.50s' tag method", ar.name);
620 break; 619 break;
621 default: { 620 default: {
622 if (*ar.what == l_c('m')) /* main? */ 621 if (*ar.what == 'm') /* main? */
623 sprintf(buff, l_s("main of %.70s"), ar.short_src); 622 sprintf(buff, "main of %.70s", ar.short_src);
624 else if (*ar.what == l_c('C')) /* C function? */ 623 else if (*ar.what == 'C') /* C function? */
625 sprintf(buff, l_s("%.70s"), ar.short_src); 624 sprintf(buff, "%.70s", ar.short_src);
626 else 625 else
627 sprintf(buff, l_s("function <%d:%.70s>"), ar.linedefined, ar.short_src); 626 sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src);
628 ar.source = NULL; /* do not print source again */ 627 ar.source = NULL; /* do not print source again */
629 } 628 }
630 } 629 }
631 luaL_addstring(&b, buff); 630 luaL_addstring(&b, buff);
632 if (ar.currentline > 0) { 631 if (ar.currentline > 0) {
633 sprintf(buff, l_s(" at line %d"), ar.currentline); 632 sprintf(buff, " at line %d", ar.currentline);
634 luaL_addstring(&b, buff); 633 luaL_addstring(&b, buff);
635 } 634 }
636 if (ar.source) { 635 if (ar.source) {
637 sprintf(buff, l_s(" [%.70s]"), ar.short_src); 636 sprintf(buff, " [%.70s]", ar.short_src);
638 luaL_addstring(&b, buff); 637 luaL_addstring(&b, buff);
639 } 638 }
640 luaL_addstring(&b, l_s("\n")); 639 luaL_addstring(&b, "\n");
641 } 640 }
642 luaL_pushresult(&b); 641 luaL_pushresult(&b);
643 lua_getglobal(L, l_s(LUA_ALERT)); 642 lua_getglobal(L, LUA_ALERT);
644 if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */ 643 if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */
645 lua_pushvalue(L, -2); /* error message */ 644 lua_pushvalue(L, -2); /* error message */
646 lua_rawcall(L, 1, 0); 645 lua_rawcall(L, 1, 0);
@@ -651,29 +650,29 @@ static int errorfb (lua_State *L) {
651 650
652 651
653static const luaL_reg iolib[] = { 652static const luaL_reg iolib[] = {
654 {l_s("appendto"), io_appendto}, 653 {"appendto", io_appendto},
655 {l_s("clock"), io_clock}, 654 {"clock", io_clock},
656 {l_s("closefile"), io_close}, 655 {"closefile", io_close},
657 {l_s("date"), io_date}, 656 {"date", io_date},
658 {l_s("debug"), io_debug}, 657 {"debug", io_debug},
659 {l_s("difftime"), io_difftime}, 658 {"difftime", io_difftime},
660 {l_s("execute"), io_execute}, 659 {"execute", io_execute},
661 {l_s("exit"), io_exit}, 660 {"exit", io_exit},
662 {l_s("flush"), io_flush}, 661 {"flush", io_flush},
663 {l_s("getenv"), io_getenv}, 662 {"getenv", io_getenv},
664 {l_s("openfile"), io_open}, 663 {"openfile", io_open},
665 {l_s("read"), io_read}, 664 {"read", io_read},
666 {l_s("readfrom"), io_readfrom}, 665 {"readfrom", io_readfrom},
667 {l_s("remove"), io_remove}, 666 {"remove", io_remove},
668 {l_s("rename"), io_rename}, 667 {"rename", io_rename},
669 {l_s("seek"), io_seek}, 668 {"seek", io_seek},
670 {l_s("setlocale"), io_setloc}, 669 {"setlocale", io_setloc},
671 {l_s("time"), io_time}, 670 {"time", io_time},
672 {l_s("tmpfile"), io_tmpfile}, 671 {"tmpfile", io_tmpfile},
673 {l_s("tmpname"), io_tmpname}, 672 {"tmpname", io_tmpname},
674 {l_s("write"), io_write}, 673 {"write", io_write},
675 {l_s("writeto"), io_writeto}, 674 {"writeto", io_writeto},
676 {l_s(LUA_ERRORMESSAGE), errorfb} 675 {LUA_ERRORMESSAGE, errorfb}
677}; 676};
678 677
679 678
@@ -684,12 +683,12 @@ LUALIB_API int lua_iolibopen (lua_State *L) {
684 /* predefined file handles */ 683 /* predefined file handles */
685 newfilewithname(L, stdin, basicfiles[INFILE]); 684 newfilewithname(L, stdin, basicfiles[INFILE]);
686 newfilewithname(L, stdout, basicfiles[OUTFILE]); 685 newfilewithname(L, stdout, basicfiles[OUTFILE]);
687 newfilewithname(L, stderr, l_s("_STDERR")); 686 newfilewithname(L, stderr, "_STDERR");
688 resetfile(L, INFILE); 687 resetfile(L, INFILE);
689 resetfile(L, OUTFILE); 688 resetfile(L, OUTFILE);
690 /* close files when collected */ 689 /* close files when collected */
691 lua_pushcfunction(L, file_collect); 690 lua_pushcfunction(L, file_collect);
692 lua_settagmethod(L, iotag, l_s("gc")); 691 lua_settagmethod(L, iotag, "gc");
693 return 0; 692 return 0;
694} 693}
695 694