diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-23 14:17:25 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-23 14:17:25 -0300 |
commit | 39b79783297bee79db9853b63d199e120a009a8f (patch) | |
tree | c738c621c4c28d8822c2f785400786301985273b /liolib.c | |
parent | d164e2294f73d8e69f00d95a66014514b2dd0ec0 (diff) | |
download | lua-39b79783297bee79db9853b63d199e120a009a8f.tar.gz lua-39b79783297bee79db9853b63d199e120a009a8f.tar.bz2 lua-39b79783297bee79db9853b63d199e120a009a8f.zip |
first (big) step to support wide chars
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 252 |
1 files changed, 126 insertions, 126 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.106 2001/02/09 19:52:54 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.107 2001/02/22 17:15:18 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 | */ |
@@ -47,10 +47,10 @@ int pclose(); */ | |||
47 | #define OUTFILE 1 | 47 | #define OUTFILE 1 |
48 | #define NOFILE 2 | 48 | #define NOFILE 2 |
49 | 49 | ||
50 | #define FILEHANDLE "FileHandle" | 50 | #define FILEHANDLE l_s("FileHandle") |
51 | 51 | ||
52 | 52 | ||
53 | static const char *const filenames[] = {"_INPUT", "_OUTPUT"}; | 53 | static const l_char *const filenames[] = {l_s("_INPUT"), l_s("_OUTPUT")}; |
54 | 54 | ||
55 | 55 | ||
56 | static int pushresult (lua_State *L, int i) { | 56 | static int pushresult (lua_State *L, int i) { |
@@ -81,10 +81,10 @@ static FILE *getopthandle (lua_State *L, int inout) { | |||
81 | FILE *p = (FILE *)lua_touserdata(L, 1); | 81 | FILE *p = (FILE *)lua_touserdata(L, 1); |
82 | if (p != NULL) { /* is it a userdata ? */ | 82 | if (p != NULL) { /* is it a userdata ? */ |
83 | if (!checkfile(L, 1)) { | 83 | if (!checkfile(L, 1)) { |
84 | if (strcmp(lua_xtype(L, 1), "ClosedFileHandle") == 0) | 84 | if (strcmp(lua_xtype(L, 1), l_s("ClosedFileHandle")) == 0) |
85 | luaL_argerror(L, 1, "file is closed"); | 85 | luaL_argerror(L, 1, l_s("file is closed")); |
86 | else | 86 | else |
87 | luaL_argerror(L, 1, "(invalid value)"); | 87 | luaL_argerror(L, 1, l_s("(invalid value)")); |
88 | } | 88 | } |
89 | /* move it to stack top */ | 89 | /* move it to stack top */ |
90 | lua_pushvalue(L, 1); lua_remove(L, 1); | 90 | lua_pushvalue(L, 1); lua_remove(L, 1); |
@@ -92,7 +92,7 @@ static FILE *getopthandle (lua_State *L, int inout) { | |||
92 | else if (inout != NOFILE) { /* try global value */ | 92 | else if (inout != NOFILE) { /* try global value */ |
93 | lua_getglobal(L, filenames[inout]); | 93 | lua_getglobal(L, filenames[inout]); |
94 | if (!checkfile(L,-1)) | 94 | if (!checkfile(L,-1)) |
95 | luaL_verror(L, "global variable `%.10s' is not a valid file handle", | 95 | luaL_verror(L, l_s("global variable `%.10s' is not a valid file handle"), |
96 | filenames[inout]); | 96 | filenames[inout]); |
97 | p = (FILE *)lua_touserdata(L, -1); | 97 | p = (FILE *)lua_touserdata(L, -1); |
98 | } | 98 | } |
@@ -105,7 +105,7 @@ static void pushfile (lua_State *L, FILE *f) { | |||
105 | } | 105 | } |
106 | 106 | ||
107 | 107 | ||
108 | static void setfilebyname (lua_State *L, FILE *f, const char *name) { | 108 | static void setfilebyname (lua_State *L, FILE *f, const l_char *name) { |
109 | pushfile(L, f); | 109 | pushfile(L, f); |
110 | lua_setglobal(L, name); | 110 | lua_setglobal(L, name); |
111 | } | 111 | } |
@@ -131,7 +131,7 @@ static int closefile (lua_State *L, FILE *f) { | |||
131 | return 1; | 131 | return 1; |
132 | else { | 132 | else { |
133 | lua_pushuserdata(L, f); | 133 | lua_pushuserdata(L, f); |
134 | lua_settag(L, lua_type2tag(L, "ClosedFileHandle")); | 134 | lua_settag(L, lua_type2tag(L, l_s("ClosedFileHandle"))); |
135 | return (CLOSEFILE(L, f) == 0); | 135 | return (CLOSEFILE(L, f) == 0); |
136 | } | 136 | } |
137 | } | 137 | } |
@@ -163,7 +163,7 @@ static int io_tmpfile (lua_State *L) { | |||
163 | 163 | ||
164 | 164 | ||
165 | 165 | ||
166 | static int io_fromto (lua_State *L, int inout, const char *mode) { | 166 | static int io_fromto (lua_State *L, int inout, const l_char *mode) { |
167 | FILE *current; | 167 | FILE *current; |
168 | if (lua_isnull(L, 1)) { | 168 | if (lua_isnull(L, 1)) { |
169 | closefile(L, getopthandle(L, inout)); | 169 | closefile(L, getopthandle(L, inout)); |
@@ -172,25 +172,25 @@ static int io_fromto (lua_State *L, int inout, const char *mode) { | |||
172 | else if (checkfile(L, 1)) /* deprecated option */ | 172 | else if (checkfile(L, 1)) /* deprecated option */ |
173 | current = (FILE *)lua_touserdata(L, 1); | 173 | current = (FILE *)lua_touserdata(L, 1); |
174 | else { | 174 | else { |
175 | const char *s = luaL_check_string(L, 1); | 175 | const l_char *s = luaL_check_string(L, 1); |
176 | current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode); | 176 | current = (*s == l_c('|')) ? popen(s+1, mode) : fopen(s, mode); |
177 | } | 177 | } |
178 | return setreturn(L, current, inout); | 178 | return setreturn(L, current, inout); |
179 | } | 179 | } |
180 | 180 | ||
181 | 181 | ||
182 | static int io_readfrom (lua_State *L) { | 182 | static int io_readfrom (lua_State *L) { |
183 | return io_fromto(L, INFILE, "r"); | 183 | return io_fromto(L, INFILE, l_s("r")); |
184 | } | 184 | } |
185 | 185 | ||
186 | 186 | ||
187 | static int io_writeto (lua_State *L) { | 187 | static int io_writeto (lua_State *L) { |
188 | return io_fromto(L, OUTFILE, "w"); | 188 | return io_fromto(L, OUTFILE, l_s("w")); |
189 | } | 189 | } |
190 | 190 | ||
191 | 191 | ||
192 | static int io_appendto (lua_State *L) { | 192 | static int io_appendto (lua_State *L) { |
193 | FILE *current = fopen(luaL_check_string(L, 1), "a"); | 193 | FILE *current = fopen(luaL_check_string(L, 1), l_s("a")); |
194 | return setreturn(L, current, OUTFILE); | 194 | return setreturn(L, current, OUTFILE); |
195 | } | 195 | } |
196 | 196 | ||
@@ -205,7 +205,7 @@ static int io_appendto (lua_State *L) { | |||
205 | 205 | ||
206 | static int read_number (lua_State *L, FILE *f) { | 206 | static int read_number (lua_State *L, FILE *f) { |
207 | double d; | 207 | double d; |
208 | if (fscanf(f, "%lf", &d) == 1) { | 208 | if (fscanf(f, l_s("%lf"), &d) == 1) { |
209 | lua_pushnumber(L, d); | 209 | lua_pushnumber(L, d); |
210 | return 1; | 210 | return 1; |
211 | } | 211 | } |
@@ -233,11 +233,11 @@ static int read_line (lua_State *L, FILE *f) { | |||
233 | luaL_Buffer b; | 233 | luaL_Buffer b; |
234 | luaL_buffinit(L, &b); | 234 | luaL_buffinit(L, &b); |
235 | for (;;) { | 235 | for (;;) { |
236 | char *p = luaL_prepbuffer(&b); | 236 | l_char *p = luaL_prepbuffer(&b); |
237 | if (!fgets(p, LUAL_BUFFERSIZE, f)) /* read fails? */ | 237 | if (!fgets(p, LUAL_BUFFERSIZE, f)) /* read fails? */ |
238 | break; | 238 | break; |
239 | n = strlen(p); | 239 | n = strlen(p); |
240 | if (p[n-1] != '\n') | 240 | if (p[n-1] != l_c('\n')) |
241 | luaL_addsize(&b, n); | 241 | luaL_addsize(&b, n); |
242 | else { | 242 | else { |
243 | luaL_addsize(&b, n-1); /* do not add the `\n' */ | 243 | luaL_addsize(&b, n-1); /* do not add the `\n' */ |
@@ -253,15 +253,15 @@ static void read_file (lua_State *L, FILE *f) { | |||
253 | size_t len = 0; | 253 | size_t len = 0; |
254 | size_t size = LUAL_BUFFERSIZE; | 254 | size_t size = LUAL_BUFFERSIZE; |
255 | size_t oldsize = 0; | 255 | size_t oldsize = 0; |
256 | char *buffer = NULL; | 256 | l_char *buffer = NULL; |
257 | for (;;) { | 257 | for (;;) { |
258 | char *newbuffer = (char *)l_realloc(buffer, oldsize, size); | 258 | l_char *newbuffer = (l_char *)l_realloc(buffer, oldsize, size); |
259 | if (newbuffer == NULL) { | 259 | if (newbuffer == NULL) { |
260 | l_free(buffer, oldsize); | 260 | l_free(buffer, oldsize); |
261 | lua_error(L, "not enough memory to read a file"); | 261 | lua_error(L, l_s("not enough memory to read a file")); |
262 | } | 262 | } |
263 | buffer = newbuffer; | 263 | buffer = newbuffer; |
264 | len += fread(buffer+len, sizeof(char), size-len, f); | 264 | len += fread(buffer+len, sizeof(l_char), size-len, f); |
265 | if (len < size) break; /* did not read all it could */ | 265 | if (len < size) break; /* did not read all it could */ |
266 | oldsize = size; | 266 | oldsize = size; |
267 | size *= 2; | 267 | size *= 2; |
@@ -279,17 +279,17 @@ static int read_chars (lua_State *L, FILE *f, size_t n) { | |||
279 | return (c != EOF); | 279 | return (c != EOF); |
280 | } | 280 | } |
281 | else { | 281 | else { |
282 | char *buffer; | 282 | l_char *buffer; |
283 | size_t n1; | 283 | size_t n1; |
284 | char statbuff[LUAL_BUFFERSIZE]; | 284 | l_char statbuff[LUAL_BUFFERSIZE]; |
285 | if (n <= LUAL_BUFFERSIZE) | 285 | if (n <= LUAL_BUFFERSIZE) |
286 | buffer = statbuff; | 286 | buffer = statbuff; |
287 | else { | 287 | else { |
288 | buffer = (char *)l_malloc(n); | 288 | buffer = (l_char *)l_malloc(n); |
289 | if (buffer == NULL) | 289 | if (buffer == NULL) |
290 | lua_error(L, "not enough memory to read a file"); | 290 | lua_error(L, l_s("not enough memory to read a file")); |
291 | } | 291 | } |
292 | n1 = fread(buffer, sizeof(char), n, f); | 292 | n1 = fread(buffer, sizeof(l_char), n, f); |
293 | lua_pushlstring(L, buffer, n1); | 293 | lua_pushlstring(L, buffer, n1); |
294 | if (buffer != statbuff) l_free(buffer, n); | 294 | if (buffer != statbuff) l_free(buffer, n); |
295 | return (n1 > 0 || n == 0); | 295 | return (n1 > 0 || n == 0); |
@@ -307,31 +307,31 @@ static int io_read (lua_State *L) { | |||
307 | n = 2; /* will return n-1 results */ | 307 | n = 2; /* will return n-1 results */ |
308 | } | 308 | } |
309 | else { /* ensure stack space for all results and for auxlib's buffer */ | 309 | else { /* ensure stack space for all results and for auxlib's buffer */ |
310 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); | 310 | luaL_checkstack(L, nargs+LUA_MINSTACK, l_s("too many arguments")); |
311 | success = 1; | 311 | success = 1; |
312 | for (n = 1; n<=nargs && success; n++) { | 312 | for (n = 1; n<=nargs && success; n++) { |
313 | if (lua_type(L, n) == LUA_TNUMBER) | 313 | if (lua_type(L, n) == LUA_TNUMBER) |
314 | success = read_chars(L, f, (size_t)lua_tonumber(L, n)); | 314 | success = read_chars(L, f, (size_t)lua_tonumber(L, n)); |
315 | else { | 315 | else { |
316 | const char *p = lua_tostring(L, n); | 316 | const l_char *p = lua_tostring(L, n); |
317 | if (!p || p[0] != '*') | 317 | if (!p || p[0] != l_c('*')) |
318 | lua_error(L, "invalid `read' option"); | 318 | lua_error(L, l_s("invalid `read' option")); |
319 | switch (p[1]) { | 319 | switch (p[1]) { |
320 | case 'n': /* number */ | 320 | case l_c('n'): /* number */ |
321 | success = read_number(L, f); | 321 | success = read_number(L, f); |
322 | break; | 322 | break; |
323 | case 'l': /* line */ | 323 | case l_c('l'): /* line */ |
324 | success = read_line(L, f); | 324 | success = read_line(L, f); |
325 | break; | 325 | break; |
326 | case 'a': /* file */ | 326 | case l_c('a'): /* file */ |
327 | read_file(L, f); | 327 | read_file(L, f); |
328 | success = 1; /* always success */ | 328 | success = 1; /* always success */ |
329 | break; | 329 | break; |
330 | case 'w': /* word */ | 330 | case l_c('w'): /* word */ |
331 | success = read_word(L, f); | 331 | success = read_word(L, f); |
332 | break; | 332 | break; |
333 | default: | 333 | default: |
334 | luaL_argerror(L, n, "invalid format"); | 334 | luaL_argerror(L, n, l_s("invalid format")); |
335 | success = 0; /* to avoid warnings */ | 335 | success = 0; /* to avoid warnings */ |
336 | } | 336 | } |
337 | } | 337 | } |
@@ -355,12 +355,12 @@ static int io_write (lua_State *L) { | |||
355 | for (arg=1; arg<=nargs; arg++) { | 355 | for (arg=1; arg<=nargs; arg++) { |
356 | if (lua_type(L, arg) == LUA_TNUMBER) { /* LUA_NUMBER */ | 356 | if (lua_type(L, arg) == LUA_TNUMBER) { /* LUA_NUMBER */ |
357 | /* optimization: could be done exactly as for strings */ | 357 | /* optimization: could be done exactly as for strings */ |
358 | status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0; | 358 | status = status && fprintf(f, l_s("%.16g"), lua_tonumber(L, arg)) > 0; |
359 | } | 359 | } |
360 | else { | 360 | else { |
361 | size_t l; | 361 | size_t l; |
362 | const char *s = luaL_check_lstr(L, arg, &l); | 362 | const l_char *s = luaL_check_lstr(L, arg, &l); |
363 | status = status && (fwrite(s, sizeof(char), l, f) == l); | 363 | status = status && (fwrite(s, sizeof(l_char), l, f) == l); |
364 | } | 364 | } |
365 | } | 365 | } |
366 | pushresult(L, status); | 366 | pushresult(L, status); |
@@ -370,11 +370,11 @@ static int io_write (lua_State *L) { | |||
370 | 370 | ||
371 | static int io_seek (lua_State *L) { | 371 | static int io_seek (lua_State *L) { |
372 | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; | 372 | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; |
373 | static const char *const modenames[] = {"set", "cur", "end", NULL}; | 373 | static const l_char *const modenames[] = {l_s("set"), l_s("cur"), l_s("end"), NULL}; |
374 | FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE); | 374 | FILE *f = (FILE *)luaL_check_userdata(L, 1, FILEHANDLE); |
375 | int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames); | 375 | int op = luaL_findstring(luaL_opt_string(L, 2, l_s("cur")), modenames); |
376 | long offset = luaL_opt_long(L, 3, 0); | 376 | long offset = luaL_opt_long(L, 3, 0); |
377 | luaL_arg_check(L, op != -1, 2, "invalid mode"); | 377 | luaL_arg_check(L, op != -1, 2, l_s("invalid mode")); |
378 | op = fseek(f, offset, mode[op]); | 378 | op = fseek(f, offset, mode[op]); |
379 | if (op) | 379 | if (op) |
380 | return pushresult(L, 0); /* error */ | 380 | return pushresult(L, 0); /* error */ |
@@ -387,7 +387,7 @@ static int io_seek (lua_State *L) { | |||
387 | 387 | ||
388 | static int io_flush (lua_State *L) { | 388 | static int io_flush (lua_State *L) { |
389 | FILE *f = getopthandle(L, NOFILE); | 389 | FILE *f = getopthandle(L, NOFILE); |
390 | luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle"); | 390 | luaL_arg_check(L, f || lua_isnull(L, 1), 1, l_s("invalid file handle")); |
391 | return pushresult(L, fflush(f) == 0); | 391 | return pushresult(L, fflush(f) == 0); |
392 | } | 392 | } |
393 | 393 | ||
@@ -418,9 +418,9 @@ static int io_rename (lua_State *L) { | |||
418 | 418 | ||
419 | 419 | ||
420 | static int io_tmpname (lua_State *L) { | 420 | static int io_tmpname (lua_State *L) { |
421 | char buff[L_tmpnam]; | 421 | l_char buff[L_tmpnam]; |
422 | if (tmpnam(buff) != buff) | 422 | if (tmpnam(buff) != buff) |
423 | lua_error(L, "unable to generate a unique filename"); | 423 | lua_error(L, l_s("unable to generate a unique filename")); |
424 | lua_pushstring(L, buff); | 424 | lua_pushstring(L, buff); |
425 | return 1; | 425 | return 1; |
426 | } | 426 | } |
@@ -447,14 +447,14 @@ static int io_clock (lua_State *L) { | |||
447 | ** ======================================================= | 447 | ** ======================================================= |
448 | */ | 448 | */ |
449 | 449 | ||
450 | static void setfield (lua_State *L, const char *key, int value) { | 450 | static void setfield (lua_State *L, const l_char *key, int value) { |
451 | lua_pushstring(L, key); | 451 | lua_pushstring(L, key); |
452 | lua_pushnumber(L, value); | 452 | lua_pushnumber(L, value); |
453 | lua_rawset(L, -3); | 453 | lua_rawset(L, -3); |
454 | } | 454 | } |
455 | 455 | ||
456 | 456 | ||
457 | static int getfield (lua_State *L, const char *key, int d) { | 457 | static int getfield (lua_State *L, const l_char *key, int d) { |
458 | int res; | 458 | int res; |
459 | lua_pushstring(L, key); | 459 | lua_pushstring(L, key); |
460 | lua_rawget(L, -2); | 460 | lua_rawget(L, -2); |
@@ -462,7 +462,7 @@ static int getfield (lua_State *L, const char *key, int d) { | |||
462 | res = (int)lua_tonumber(L, -1); | 462 | res = (int)lua_tonumber(L, -1); |
463 | else { | 463 | else { |
464 | if (d == -2) | 464 | if (d == -2) |
465 | luaL_verror(L, "field `%.20s' missing in date table", key); | 465 | luaL_verror(L, l_s("field `%.20s' missing in date table"), key); |
466 | res = d; | 466 | res = d; |
467 | } | 467 | } |
468 | lua_pop(L, 1); | 468 | lua_pop(L, 1); |
@@ -471,12 +471,12 @@ static int getfield (lua_State *L, const char *key, int d) { | |||
471 | 471 | ||
472 | 472 | ||
473 | static int io_date (lua_State *L) { | 473 | static int io_date (lua_State *L) { |
474 | const char *s = luaL_opt_string(L, 1, "%c"); | 474 | const l_char *s = luaL_opt_string(L, 1, l_s("%c")); |
475 | time_t t = (time_t)luaL_opt_number(L, 2, -1); | 475 | time_t t = (time_t)luaL_opt_number(L, 2, -1); |
476 | struct tm *stm; | 476 | struct tm *stm; |
477 | if (t == (time_t)-1) /* no time given? */ | 477 | if (t == (time_t)-1) /* no time given? */ |
478 | t = time(NULL); /* use current time */ | 478 | t = time(NULL); /* use current time */ |
479 | if (*s == '!') { /* UTC? */ | 479 | if (*s == l_c('!')) { /* UTC? */ |
480 | stm = gmtime(&t); | 480 | stm = gmtime(&t); |
481 | s++; /* skip `!' */ | 481 | s++; /* skip `!' */ |
482 | } | 482 | } |
@@ -484,24 +484,24 @@ static int io_date (lua_State *L) { | |||
484 | stm = localtime(&t); | 484 | stm = localtime(&t); |
485 | if (stm == NULL) /* invalid date? */ | 485 | if (stm == NULL) /* invalid date? */ |
486 | lua_pushnil(L); | 486 | lua_pushnil(L); |
487 | else if (strcmp(s, "*t") == 0) { | 487 | else if (strcmp(s, l_s("*t")) == 0) { |
488 | lua_newtable(L); | 488 | lua_newtable(L); |
489 | setfield(L, "sec", stm->tm_sec); | 489 | setfield(L, l_s("sec"), stm->tm_sec); |
490 | setfield(L, "min", stm->tm_min); | 490 | setfield(L, l_s("min"), stm->tm_min); |
491 | setfield(L, "hour", stm->tm_hour); | 491 | setfield(L, l_s("hour"), stm->tm_hour); |
492 | setfield(L, "day", stm->tm_mday); | 492 | setfield(L, l_s("day"), stm->tm_mday); |
493 | setfield(L, "month", stm->tm_mon+1); | 493 | setfield(L, l_s("month"), stm->tm_mon+1); |
494 | setfield(L, "year", stm->tm_year+1900); | 494 | setfield(L, l_s("year"), stm->tm_year+1900); |
495 | setfield(L, "wday", stm->tm_wday+1); | 495 | setfield(L, l_s("wday"), stm->tm_wday+1); |
496 | setfield(L, "yday", stm->tm_yday+1); | 496 | setfield(L, l_s("yday"), stm->tm_yday+1); |
497 | setfield(L, "isdst", stm->tm_isdst); | 497 | setfield(L, l_s("isdst"), stm->tm_isdst); |
498 | } | 498 | } |
499 | else { | 499 | else { |
500 | char b[256]; | 500 | l_char b[256]; |
501 | if (strftime(b, sizeof(b), s, stm)) | 501 | if (strftime(b, sizeof(b), s, stm)) |
502 | lua_pushstring(L, b); | 502 | lua_pushstring(L, b); |
503 | else | 503 | else |
504 | lua_error(L, "invalid `date' format"); | 504 | lua_error(L, l_s("invalid `date' format")); |
505 | } | 505 | } |
506 | return 1; | 506 | return 1; |
507 | } | 507 | } |
@@ -515,13 +515,13 @@ static int io_time (lua_State *L) { | |||
515 | struct tm ts; | 515 | struct tm ts; |
516 | luaL_checktype(L, 1, LUA_TTABLE); | 516 | luaL_checktype(L, 1, LUA_TTABLE); |
517 | lua_settop(L, 1); /* make sure table is at the top */ | 517 | lua_settop(L, 1); /* make sure table is at the top */ |
518 | ts.tm_sec = getfield(L, "sec", 0); | 518 | ts.tm_sec = getfield(L, l_s("sec"), 0); |
519 | ts.tm_min = getfield(L, "min", 0); | 519 | ts.tm_min = getfield(L, l_s("min"), 0); |
520 | ts.tm_hour = getfield(L, "hour", 12); | 520 | ts.tm_hour = getfield(L, l_s("hour"), 12); |
521 | ts.tm_mday = getfield(L, "day", -2); | 521 | ts.tm_mday = getfield(L, l_s("day"), -2); |
522 | ts.tm_mon = getfield(L, "month", -2)-1; | 522 | ts.tm_mon = getfield(L, l_s("month"), -2)-1; |
523 | ts.tm_year = getfield(L, "year", -2)-1900; | 523 | ts.tm_year = getfield(L, l_s("year"), -2)-1900; |
524 | ts.tm_isdst = getfield(L, "isdst", -1); | 524 | ts.tm_isdst = getfield(L, l_s("isdst"), -1); |
525 | t = mktime(&ts); | 525 | t = mktime(&ts); |
526 | if (t == (time_t)-1) | 526 | if (t == (time_t)-1) |
527 | lua_pushnil(L); | 527 | lua_pushnil(L); |
@@ -544,10 +544,10 @@ static int io_difftime (lua_State *L) { | |||
544 | static int io_setloc (lua_State *L) { | 544 | static int io_setloc (lua_State *L) { |
545 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, | 545 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, |
546 | LC_NUMERIC, LC_TIME}; | 546 | LC_NUMERIC, LC_TIME}; |
547 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", | 547 | static const l_char *const catnames[] = {l_s("all"), l_s("collate"), l_s("ctype"), l_s("monetary"), |
548 | "numeric", "time", NULL}; | 548 | l_s("numeric"), l_s("time"), NULL}; |
549 | int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames); | 549 | int op = luaL_findstring(luaL_opt_string(L, 2, l_s("all")), catnames); |
550 | luaL_arg_check(L, op != -1, 2, "invalid option"); | 550 | luaL_arg_check(L, op != -1, 2, l_s("invalid option")); |
551 | lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1))); | 551 | lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1))); |
552 | return 1; | 552 | return 1; |
553 | } | 553 | } |
@@ -564,10 +564,10 @@ static int io_exit (lua_State *L) { | |||
564 | 564 | ||
565 | static int io_debug (lua_State *L) { | 565 | static int io_debug (lua_State *L) { |
566 | for (;;) { | 566 | for (;;) { |
567 | char buffer[250]; | 567 | l_char buffer[250]; |
568 | fprintf(stderr, "lua_debug> "); | 568 | fprintf(stderr, l_s("lua_debug> ")); |
569 | if (fgets(buffer, sizeof(buffer), stdin) == 0 || | 569 | if (fgets(buffer, sizeof(buffer), stdin) == 0 || |
570 | strcmp(buffer, "cont\n") == 0) | 570 | strcmp(buffer, l_s("cont\n")) == 0) |
571 | return 0; | 571 | return 0; |
572 | lua_dostring(L, buffer); | 572 | lua_dostring(L, buffer); |
573 | lua_settop(L, 0); /* remove eventual returns */ | 573 | lua_settop(L, 0); /* remove eventual returns */ |
@@ -584,61 +584,61 @@ static int errorfb (lua_State *L) { | |||
584 | lua_Debug ar; | 584 | lua_Debug ar; |
585 | luaL_Buffer b; | 585 | luaL_Buffer b; |
586 | luaL_buffinit(L, &b); | 586 | luaL_buffinit(L, &b); |
587 | luaL_addstring(&b, "error: "); | 587 | luaL_addstring(&b, l_s("error: ")); |
588 | luaL_addstring(&b, luaL_check_string(L, 1)); | 588 | luaL_addstring(&b, luaL_check_string(L, 1)); |
589 | luaL_addstring(&b, "\n"); | 589 | luaL_addstring(&b, l_s("\n")); |
590 | while (lua_getstack(L, level++, &ar)) { | 590 | while (lua_getstack(L, level++, &ar)) { |
591 | char buff[120]; /* enough to fit following `sprintf's */ | 591 | l_char buff[120]; /* enough to fit following `sprintf's */ |
592 | if (level == 2) | 592 | if (level == 2) |
593 | luaL_addstring(&b, "stack traceback:\n"); | 593 | luaL_addstring(&b, l_s("stack traceback:\n")); |
594 | else if (level > LEVELS1 && firstpart) { | 594 | else if (level > LEVELS1 && firstpart) { |
595 | /* no more than `LEVELS2' more levels? */ | 595 | /* no more than `LEVELS2' more levels? */ |
596 | if (!lua_getstack(L, level+LEVELS2, &ar)) | 596 | if (!lua_getstack(L, level+LEVELS2, &ar)) |
597 | level--; /* keep going */ | 597 | level--; /* keep going */ |
598 | else { | 598 | else { |
599 | luaL_addstring(&b, " ...\n"); /* too many levels */ | 599 | luaL_addstring(&b, l_s(" ...\n")); /* too many levels */ |
600 | while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ | 600 | while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ |
601 | level++; | 601 | level++; |
602 | } | 602 | } |
603 | firstpart = 0; | 603 | firstpart = 0; |
604 | continue; | 604 | continue; |
605 | } | 605 | } |
606 | sprintf(buff, "%4d: ", level-1); | 606 | sprintf(buff, l_s("%4d: "), level-1); |
607 | luaL_addstring(&b, buff); | 607 | luaL_addstring(&b, buff); |
608 | lua_getinfo(L, "Snl", &ar); | 608 | lua_getinfo(L, l_s("Snl"), &ar); |
609 | switch (*ar.namewhat) { | 609 | switch (*ar.namewhat) { |
610 | case 'g': case 'l': /* global, local */ | 610 | case l_c('g'): case l_c('l'): /* global, local */ |
611 | sprintf(buff, "function `%.50s'", ar.name); | 611 | sprintf(buff, l_s("function `%.50s'"), ar.name); |
612 | break; | 612 | break; |
613 | case 'f': /* field */ | 613 | case l_c('f'): /* field */ |
614 | sprintf(buff, "method `%.50s'", ar.name); | 614 | sprintf(buff, l_s("method `%.50s'"), ar.name); |
615 | break; | 615 | break; |
616 | case 't': /* tag method */ | 616 | case l_c('t'): /* tag method */ |
617 | sprintf(buff, "`%.50s' tag method", ar.name); | 617 | sprintf(buff, l_s("`%.50s' tag method"), ar.name); |
618 | break; | 618 | break; |
619 | default: { | 619 | default: { |
620 | if (*ar.what == 'm') /* main? */ | 620 | if (*ar.what == l_c('m')) /* main? */ |
621 | sprintf(buff, "main of %.70s", ar.short_src); | 621 | sprintf(buff, l_s("main of %.70s"), ar.short_src); |
622 | else if (*ar.what == 'C') /* C function? */ | 622 | else if (*ar.what == l_c('C')) /* C function? */ |
623 | sprintf(buff, "%.70s", ar.short_src); | 623 | sprintf(buff, l_s("%.70s"), ar.short_src); |
624 | else | 624 | else |
625 | sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src); | 625 | sprintf(buff, l_s("function <%d:%.70s>"), ar.linedefined, ar.short_src); |
626 | ar.source = NULL; /* do not print source again */ | 626 | ar.source = NULL; /* do not print source again */ |
627 | } | 627 | } |
628 | } | 628 | } |
629 | luaL_addstring(&b, buff); | 629 | luaL_addstring(&b, buff); |
630 | if (ar.currentline > 0) { | 630 | if (ar.currentline > 0) { |
631 | sprintf(buff, " at line %d", ar.currentline); | 631 | sprintf(buff, l_s(" at line %d"), ar.currentline); |
632 | luaL_addstring(&b, buff); | 632 | luaL_addstring(&b, buff); |
633 | } | 633 | } |
634 | if (ar.source) { | 634 | if (ar.source) { |
635 | sprintf(buff, " [%.70s]", ar.short_src); | 635 | sprintf(buff, l_s(" [%.70s]"), ar.short_src); |
636 | luaL_addstring(&b, buff); | 636 | luaL_addstring(&b, buff); |
637 | } | 637 | } |
638 | luaL_addstring(&b, "\n"); | 638 | luaL_addstring(&b, l_s("\n")); |
639 | } | 639 | } |
640 | luaL_pushresult(&b); | 640 | luaL_pushresult(&b); |
641 | lua_getglobal(L, LUA_ALERT); | 641 | lua_getglobal(L, l_s(LUA_ALERT)); |
642 | if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */ | 642 | if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */ |
643 | lua_pushvalue(L, -2); /* error message */ | 643 | lua_pushvalue(L, -2); /* error message */ |
644 | lua_rawcall(L, 1, 0); | 644 | lua_rawcall(L, 1, 0); |
@@ -649,44 +649,44 @@ static int errorfb (lua_State *L) { | |||
649 | 649 | ||
650 | 650 | ||
651 | static const luaL_reg iolib[] = { | 651 | static const luaL_reg iolib[] = { |
652 | {"appendto", io_appendto}, | 652 | {l_s("appendto"), io_appendto}, |
653 | {"clock", io_clock}, | 653 | {l_s("clock"), io_clock}, |
654 | {"closefile", io_close}, | 654 | {l_s("closefile"), io_close}, |
655 | {"date", io_date}, | 655 | {l_s("date"), io_date}, |
656 | {"debug", io_debug}, | 656 | {l_s("debug"), io_debug}, |
657 | {"difftime", io_difftime}, | 657 | {l_s("difftime"), io_difftime}, |
658 | {"execute", io_execute}, | 658 | {l_s("execute"), io_execute}, |
659 | {"exit", io_exit}, | 659 | {l_s("exit"), io_exit}, |
660 | {"flush", io_flush}, | 660 | {l_s("flush"), io_flush}, |
661 | {"getenv", io_getenv}, | 661 | {l_s("getenv"), io_getenv}, |
662 | {"openfile", io_open}, | 662 | {l_s("openfile"), io_open}, |
663 | {"read", io_read}, | 663 | {l_s("read"), io_read}, |
664 | {"readfrom", io_readfrom}, | 664 | {l_s("readfrom"), io_readfrom}, |
665 | {"remove", io_remove}, | 665 | {l_s("remove"), io_remove}, |
666 | {"rename", io_rename}, | 666 | {l_s("rename"), io_rename}, |
667 | {"seek", io_seek}, | 667 | {l_s("seek"), io_seek}, |
668 | {"setlocale", io_setloc}, | 668 | {l_s("setlocale"), io_setloc}, |
669 | {"time", io_time}, | 669 | {l_s("time"), io_time}, |
670 | {"tmpfile", io_tmpfile}, | 670 | {l_s("tmpfile"), io_tmpfile}, |
671 | {"tmpname", io_tmpname}, | 671 | {l_s("tmpname"), io_tmpname}, |
672 | {"write", io_write}, | 672 | {l_s("write"), io_write}, |
673 | {"writeto", io_writeto}, | 673 | {l_s("writeto"), io_writeto}, |
674 | {LUA_ERRORMESSAGE, errorfb} | 674 | {l_s(LUA_ERRORMESSAGE), errorfb} |
675 | }; | 675 | }; |
676 | 676 | ||
677 | 677 | ||
678 | LUALIB_API void lua_iolibopen (lua_State *L) { | 678 | LUALIB_API void lua_iolibopen (lua_State *L) { |
679 | int iotag = lua_newtype(L, FILEHANDLE, LUA_TUSERDATA); | 679 | int iotag = lua_newtype(L, FILEHANDLE, LUA_TUSERDATA); |
680 | lua_newtype(L, "ClosedFileHandle", LUA_TUSERDATA); | 680 | lua_newtype(L, l_s("ClosedFileHandle"), LUA_TUSERDATA); |
681 | luaL_openl(L, iolib); | 681 | luaL_openl(L, iolib); |
682 | /* predefined file handles */ | 682 | /* predefined file handles */ |
683 | setfile(L, stdin, INFILE); | 683 | setfile(L, stdin, INFILE); |
684 | setfile(L, stdout, OUTFILE); | 684 | setfile(L, stdout, OUTFILE); |
685 | setfilebyname(L, stdin, "_STDIN"); | 685 | setfilebyname(L, stdin, l_s("_STDIN")); |
686 | setfilebyname(L, stdout, "_STDOUT"); | 686 | setfilebyname(L, stdout, l_s("_STDOUT")); |
687 | setfilebyname(L, stderr, "_STDERR"); | 687 | setfilebyname(L, stderr, l_s("_STDERR")); |
688 | /* close files when collected */ | 688 | /* close files when collected */ |
689 | lua_pushcfunction(L, file_collect); | 689 | lua_pushcfunction(L, file_collect); |
690 | lua_settagmethod(L, iotag, "gc"); | 690 | lua_settagmethod(L, iotag, l_s("gc")); |
691 | } | 691 | } |
692 | 692 | ||