diff options
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 77 |
1 files changed, 39 insertions, 38 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 2.21 2002/10/16 20:41:35 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.22 2002/10/21 20:41: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 | */ |
@@ -83,6 +83,10 @@ static FILE **newfile (lua_State *L) { | |||
83 | } | 83 | } |
84 | 84 | ||
85 | 85 | ||
86 | /* | ||
87 | ** assumes that top of the stack is the `io' library, and next is | ||
88 | ** the `io' metatable | ||
89 | */ | ||
86 | static void registerfile (lua_State *L, FILE *f, const char *name, | 90 | static void registerfile (lua_State *L, FILE *f, const char *name, |
87 | const char *impname) { | 91 | const char *impname) { |
88 | lua_pushstring(L, name); | 92 | lua_pushstring(L, name); |
@@ -90,9 +94,9 @@ static void registerfile (lua_State *L, FILE *f, const char *name, | |||
90 | if (impname) { | 94 | if (impname) { |
91 | lua_pushstring(L, impname); | 95 | lua_pushstring(L, impname); |
92 | lua_pushvalue(L, -2); | 96 | lua_pushvalue(L, -2); |
93 | lua_settable(L, -6); | 97 | lua_settable(L, -6); /* metatable[impname] = file */ |
94 | } | 98 | } |
95 | lua_settable(L, -3); | 99 | lua_settable(L, -3); /* io[name] = file */ |
96 | } | 100 | } |
97 | 101 | ||
98 | 102 | ||
@@ -127,8 +131,8 @@ static int io_gc (lua_State *L) { | |||
127 | 131 | ||
128 | 132 | ||
129 | static int io_open (lua_State *L) { | 133 | static int io_open (lua_State *L) { |
130 | const char *filename = luaL_check_string(L, 1); | 134 | const char *filename = luaL_checkstring(L, 1); |
131 | const char *mode = luaL_opt_string(L, 2, "r"); | 135 | const char *mode = luaL_optstring(L, 2, "r"); |
132 | FILE **pf = newfile(L); | 136 | FILE **pf = newfile(L); |
133 | *pf = fopen(filename, mode); | 137 | *pf = fopen(filename, mode); |
134 | return (*pf == NULL) ? pushresult(L, 0) : 1; | 138 | return (*pf == NULL) ? pushresult(L, 0) : 1; |
@@ -140,8 +144,8 @@ static int io_popen (lua_State *L) { | |||
140 | luaL_error(L, "`popen' not supported"); | 144 | luaL_error(L, "`popen' not supported"); |
141 | return 0; | 145 | return 0; |
142 | #else | 146 | #else |
143 | const char *filename = luaL_check_string(L, 1); | 147 | const char *filename = luaL_checkstring(L, 1); |
144 | const char *mode = luaL_opt_string(L, 2, "r"); | 148 | const char *mode = luaL_optstring(L, 2, "r"); |
145 | FILE **pf = newfile(L); | 149 | FILE **pf = newfile(L); |
146 | *pf = popen(filename, mode); | 150 | *pf = popen(filename, mode); |
147 | return (*pf == NULL) ? pushresult(L, 0) : 1; | 151 | return (*pf == NULL) ? pushresult(L, 0) : 1; |
@@ -179,7 +183,7 @@ static int g_iofile (lua_State *L, const char *name, const char *mode) { | |||
179 | if (filename) { | 183 | if (filename) { |
180 | FILE **pf = newfile(L); | 184 | FILE **pf = newfile(L); |
181 | *pf = fopen(filename, mode); | 185 | *pf = fopen(filename, mode); |
182 | luaL_arg_check(L, *pf, 1, strerror(errno)); | 186 | luaL_argcheck(L, *pf, 1, strerror(errno)); |
183 | } | 187 | } |
184 | else { | 188 | else { |
185 | tofile(L, 1); /* check that it's a valid file handle */ | 189 | tofile(L, 1); /* check that it's a valid file handle */ |
@@ -227,10 +231,10 @@ static int io_lines (lua_State *L) { | |||
227 | return f_lines(L); | 231 | return f_lines(L); |
228 | } | 232 | } |
229 | else { | 233 | else { |
230 | const char *filename = luaL_check_string(L, 1); | 234 | const char *filename = luaL_checkstring(L, 1); |
231 | FILE **pf = newfile(L); | 235 | FILE **pf = newfile(L); |
232 | *pf = fopen(filename, "r"); | 236 | *pf = fopen(filename, "r"); |
233 | luaL_arg_check(L, *pf, 1, strerror(errno)); | 237 | luaL_argcheck(L, *pf, 1, strerror(errno)); |
234 | aux_lines(L, lua_gettop(L), 1); | 238 | aux_lines(L, lua_gettop(L), 1); |
235 | return 1; | 239 | return 1; |
236 | } | 240 | } |
@@ -311,7 +315,7 @@ static int g_read (lua_State *L, FILE *f, int first) { | |||
311 | n = first+1; /* to return 1 result */ | 315 | n = first+1; /* to return 1 result */ |
312 | } | 316 | } |
313 | else { /* ensure stack space for all results and for auxlib's buffer */ | 317 | else { /* ensure stack space for all results and for auxlib's buffer */ |
314 | luaL_check_stack(L, nargs+LUA_MINSTACK, "too many arguments"); | 318 | luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); |
315 | success = 1; | 319 | success = 1; |
316 | for (n = first; nargs-- && success; n++) { | 320 | for (n = first; nargs-- && success; n++) { |
317 | if (lua_type(L, n) == LUA_TNUMBER) { | 321 | if (lua_type(L, n) == LUA_TNUMBER) { |
@@ -388,7 +392,7 @@ static int g_write (lua_State *L, FILE *f, int arg) { | |||
388 | } | 392 | } |
389 | else { | 393 | else { |
390 | size_t l; | 394 | size_t l; |
391 | const char *s = luaL_check_lstr(L, arg, &l); | 395 | const char *s = luaL_checklstring(L, arg, &l); |
392 | status = status && (fwrite(s, sizeof(char), l, f) == l); | 396 | status = status && (fwrite(s, sizeof(char), l, f) == l); |
393 | } | 397 | } |
394 | } | 398 | } |
@@ -411,9 +415,9 @@ static int f_seek (lua_State *L) { | |||
411 | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; | 415 | static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; |
412 | static const char *const modenames[] = {"set", "cur", "end", NULL}; | 416 | static const char *const modenames[] = {"set", "cur", "end", NULL}; |
413 | FILE *f = tofile(L, 1); | 417 | FILE *f = tofile(L, 1); |
414 | int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames); | 418 | int op = luaL_findstring(luaL_optstring(L, 2, "cur"), modenames); |
415 | long offset = luaL_opt_long(L, 3, 0); | 419 | long offset = luaL_optlong(L, 3, 0); |
416 | luaL_arg_check(L, op != -1, 2, "invalid mode"); | 420 | luaL_argcheck(L, op != -1, 2, "invalid mode"); |
417 | op = fseek(f, offset, mode[op]); | 421 | op = fseek(f, offset, mode[op]); |
418 | if (op) | 422 | if (op) |
419 | return pushresult(L, 0); /* error */ | 423 | return pushresult(L, 0); /* error */ |
@@ -473,7 +477,7 @@ static void createmeta (lua_State *L) { | |||
473 | lua_pushvalue(L, -2); /* push metatable */ | 477 | lua_pushvalue(L, -2); /* push metatable */ |
474 | lua_rawset(L, -3); /* metatable.__index = metatable */ | 478 | lua_rawset(L, -3); /* metatable.__index = metatable */ |
475 | lua_pushvalue(L, -1); /* push metatable (will be upvalue for library) */ | 479 | lua_pushvalue(L, -1); /* push metatable (will be upvalue for library) */ |
476 | luaL_openlib(L, flib, 1); | 480 | luaL_openlib(L, NULL, flib, 1); |
477 | lua_rawset(L, LUA_REGISTRYINDEX); /* registry.FILEHANDLE = metatable */ | 481 | lua_rawset(L, LUA_REGISTRYINDEX); /* registry.FILEHANDLE = metatable */ |
478 | } | 482 | } |
479 | 483 | ||
@@ -487,19 +491,19 @@ static void createmeta (lua_State *L) { | |||
487 | */ | 491 | */ |
488 | 492 | ||
489 | static int io_execute (lua_State *L) { | 493 | static int io_execute (lua_State *L) { |
490 | lua_pushnumber(L, system(luaL_check_string(L, 1))); | 494 | lua_pushnumber(L, system(luaL_checkstring(L, 1))); |
491 | return 1; | 495 | return 1; |
492 | } | 496 | } |
493 | 497 | ||
494 | 498 | ||
495 | static int io_remove (lua_State *L) { | 499 | static int io_remove (lua_State *L) { |
496 | return pushresult(L, remove(luaL_check_string(L, 1)) == 0); | 500 | return pushresult(L, remove(luaL_checkstring(L, 1)) == 0); |
497 | } | 501 | } |
498 | 502 | ||
499 | 503 | ||
500 | static int io_rename (lua_State *L) { | 504 | static int io_rename (lua_State *L) { |
501 | return pushresult(L, rename(luaL_check_string(L, 1), | 505 | return pushresult(L, rename(luaL_checkstring(L, 1), |
502 | luaL_check_string(L, 2)) == 0); | 506 | luaL_checkstring(L, 2)) == 0); |
503 | } | 507 | } |
504 | 508 | ||
505 | 509 | ||
@@ -513,7 +517,7 @@ static int io_tmpname (lua_State *L) { | |||
513 | 517 | ||
514 | 518 | ||
515 | static int io_getenv (lua_State *L) { | 519 | static int io_getenv (lua_State *L) { |
516 | lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */ | 520 | lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ |
517 | return 1; | 521 | return 1; |
518 | } | 522 | } |
519 | 523 | ||
@@ -571,8 +575,8 @@ static int getfield (lua_State *L, const char *key, int d) { | |||
571 | 575 | ||
572 | 576 | ||
573 | static int io_date (lua_State *L) { | 577 | static int io_date (lua_State *L) { |
574 | const char *s = luaL_opt_string(L, 1, "%c"); | 578 | const char *s = luaL_optstring(L, 1, "%c"); |
575 | time_t t = (time_t)(luaL_opt_number(L, 2, -1)); | 579 | time_t t = (time_t)(luaL_optnumber(L, 2, -1)); |
576 | struct tm *stm; | 580 | struct tm *stm; |
577 | if (t == (time_t)(-1)) /* no time given? */ | 581 | if (t == (time_t)(-1)) /* no time given? */ |
578 | t = time(NULL); /* use current time */ | 582 | t = time(NULL); /* use current time */ |
@@ -613,7 +617,7 @@ static int io_time (lua_State *L) { | |||
613 | else { | 617 | else { |
614 | time_t t; | 618 | time_t t; |
615 | struct tm ts; | 619 | struct tm ts; |
616 | luaL_check_type(L, 1, LUA_TTABLE); | 620 | luaL_checktype(L, 1, LUA_TTABLE); |
617 | lua_settop(L, 1); /* make sure table is at the top */ | 621 | lua_settop(L, 1); /* make sure table is at the top */ |
618 | ts.tm_sec = getfield(L, "sec", 0); | 622 | ts.tm_sec = getfield(L, "sec", 0); |
619 | ts.tm_min = getfield(L, "min", 0); | 623 | ts.tm_min = getfield(L, "min", 0); |
@@ -633,8 +637,8 @@ static int io_time (lua_State *L) { | |||
633 | 637 | ||
634 | 638 | ||
635 | static int io_difftime (lua_State *L) { | 639 | static int io_difftime (lua_State *L) { |
636 | lua_pushnumber(L, difftime((time_t)(luaL_check_number(L, 1)), | 640 | lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), |
637 | (time_t)(luaL_opt_number(L, 2, 0)))); | 641 | (time_t)(luaL_optnumber(L, 2, 0)))); |
638 | return 1; | 642 | return 1; |
639 | } | 643 | } |
640 | 644 | ||
@@ -647,16 +651,16 @@ static int io_setloc (lua_State *L) { | |||
647 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", | 651 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", |
648 | "numeric", "time", NULL}; | 652 | "numeric", "time", NULL}; |
649 | const char *l = lua_tostring(L, 1); | 653 | const char *l = lua_tostring(L, 1); |
650 | int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames); | 654 | int op = luaL_findstring(luaL_optstring(L, 2, "all"), catnames); |
651 | luaL_arg_check(L, l || lua_isnoneornil(L, 1), 1, "string expected"); | 655 | luaL_argcheck(L, l || lua_isnoneornil(L, 1), 1, "string expected"); |
652 | luaL_arg_check(L, op != -1, 2, "invalid option"); | 656 | luaL_argcheck(L, op != -1, 2, "invalid option"); |
653 | lua_pushstring(L, setlocale(cat[op], l)); | 657 | lua_pushstring(L, setlocale(cat[op], l)); |
654 | return 1; | 658 | return 1; |
655 | } | 659 | } |
656 | 660 | ||
657 | 661 | ||
658 | static int io_exit (lua_State *L) { | 662 | static int io_exit (lua_State *L) { |
659 | exit(luaL_opt_int(L, 1, EXIT_SUCCESS)); | 663 | exit(luaL_optint(L, 1, EXIT_SUCCESS)); |
660 | return 0; /* to avoid warnings */ | 664 | return 0; /* to avoid warnings */ |
661 | } | 665 | } |
662 | 666 | ||
@@ -681,18 +685,15 @@ static const luaL_reg syslib[] = { | |||
681 | 685 | ||
682 | LUALIB_API int lua_iolibopen (lua_State *L) { | 686 | LUALIB_API int lua_iolibopen (lua_State *L) { |
683 | createmeta(L); | 687 | createmeta(L); |
684 | luaL_opennamedlib(L, LUA_OSLIBNAME, syslib, 0); | 688 | luaL_openlib(L, LUA_OSLIBNAME, syslib, 0); |
685 | lua_pushliteral(L, FILEHANDLE); /* S: FH */ | 689 | lua_pushliteral(L, FILEHANDLE); |
686 | lua_rawget(L, LUA_REGISTRYINDEX); /* S: mt */ | 690 | lua_rawget(L, LUA_REGISTRYINDEX); |
687 | lua_pushvalue(L, -1); /* S: mt mt */ | 691 | lua_pushvalue(L, -1); |
688 | luaL_opennamedlib(L, LUA_IOLIBNAME, iolib, 1); /* S: mt */ | 692 | luaL_openlib(L, LUA_IOLIBNAME, iolib, 1); |
689 | lua_pushliteral(L, LUA_IOLIBNAME); /* S: `io' mt */ | ||
690 | lua_gettable(L, LUA_GLOBALSINDEX); /* S: io mt */ | ||
691 | /* put predefined file handles into `io' table */ | 693 | /* put predefined file handles into `io' table */ |
692 | registerfile(L, stdin, "stdin", IO_INPUT); | 694 | registerfile(L, stdin, "stdin", IO_INPUT); |
693 | registerfile(L, stdout, "stdout", IO_OUTPUT); | 695 | registerfile(L, stdout, "stdout", IO_OUTPUT); |
694 | registerfile(L, stderr, "stderr", NULL); | 696 | registerfile(L, stderr, "stderr", NULL); |
695 | lua_pop(L, 2); /* S: empty */ | ||
696 | return 0; | 697 | return 0; |
697 | } | 698 | } |
698 | 699 | ||