summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--linit.c3
-rw-r--r--liolib.c290
-rw-r--r--lualib.h6
3 files changed, 49 insertions, 250 deletions
diff --git a/linit.c b/linit.c
index 0c483943..a50b894e 100644
--- a/linit.c
+++ b/linit.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: linit.c,v 1.6 2000/08/09 14:50:13 roberto Exp roberto $ 2** $Id: linit.c,v 1.7 2004/07/09 14:29:29 roberto Exp roberto $
3** Initialization of libraries for lua.c 3** Initialization of libraries for lua.c
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -18,6 +18,7 @@ static const luaL_reg lualibs[] = {
18 {"", luaopen_base}, 18 {"", luaopen_base},
19 {LUA_TABLIBNAME, luaopen_table}, 19 {LUA_TABLIBNAME, luaopen_table},
20 {LUA_IOLIBNAME, luaopen_io}, 20 {LUA_IOLIBNAME, luaopen_io},
21 {LUA_OSLIBNAME, luaopen_os},
21 {LUA_STRLIBNAME, luaopen_string}, 22 {LUA_STRLIBNAME, luaopen_string},
22 {LUA_MATHLIBNAME, luaopen_math}, 23 {LUA_MATHLIBNAME, luaopen_math},
23 {LUA_DBLIBNAME, luaopen_debug}, 24 {LUA_DBLIBNAME, luaopen_debug},
diff --git a/liolib.c b/liolib.c
index 138b5c83..04961b5e 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,16 +1,14 @@
1/* 1/*
2** $Id: liolib.c,v 2.52 2004/05/28 18:32:51 roberto Exp roberto $ 2** $Id: liolib.c,v 2.53 2004/05/28 18:35:05 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*/
6 6
7 7
8#include <errno.h> 8#include <errno.h>
9#include <locale.h>
10#include <stdio.h> 9#include <stdio.h>
11#include <stdlib.h> 10#include <stdlib.h>
12#include <string.h> 11#include <string.h>
13#include <time.h>
14 12
15#define liolib_c 13#define liolib_c
16#define LUA_LIB 14#define LUA_LIB
@@ -22,18 +20,13 @@
22 20
23 21
24 22
25
26/*
27** {======================================================
28** FILE Operations
29** =======================================================
30*/
31
32
33#define IO_INPUT 1 23#define IO_INPUT 1
34#define IO_OUTPUT 2 24#define IO_OUTPUT 2
35 25
36 26
27static const char *const fnames[] = {"input", "output"};
28
29
37static int pushresult (lua_State *L, int i, const char *filename) { 30static int pushresult (lua_State *L, int i, const char *filename) {
38 if (i) { 31 if (i) {
39 lua_pushboolean(L, 1); 32 lua_pushboolean(L, 1);
@@ -51,9 +44,9 @@ static int pushresult (lua_State *L, int i, const char *filename) {
51} 44}
52 45
53 46
54static FILE **topfile (lua_State *L, int findex) { 47static FILE **topfile (lua_State *L) {
55 FILE **f = (FILE **)luaL_checkudata(L, findex, LUA_FILEHANDLE); 48 FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE);
56 if (f == NULL) luaL_argerror(L, findex, "bad file"); 49 if (f == NULL) luaL_argerror(L, 1, "bad file");
57 return f; 50 return f;
58} 51}
59 52
@@ -69,8 +62,8 @@ static int io_type (lua_State *L) {
69} 62}
70 63
71 64
72static FILE *tofile (lua_State *L, int findex) { 65static FILE *tofile (lua_State *L) {
73 FILE **f = topfile(L, findex); 66 FILE **f = topfile(L);
74 if (*f == NULL) 67 if (*f == NULL)
75 luaL_error(L, "attempt to use a closed file"); 68 luaL_error(L, "attempt to use a closed file");
76 return *f; 69 return *f;
@@ -93,7 +86,7 @@ static FILE **newfile (lua_State *L) {
93 86
94 87
95static int aux_close (lua_State *L) { 88static int aux_close (lua_State *L) {
96 FILE *f = tofile(L, 1); 89 FILE *f = tofile(L);
97 if (f == stdin || f == stdout || f == stderr) 90 if (f == stdin || f == stdout || f == stderr)
98 return 0; /* file cannot be closed */ 91 return 0; /* file cannot be closed */
99 else { 92 else {
@@ -113,7 +106,7 @@ static int io_close (lua_State *L) {
113 106
114 107
115static int io_gc (lua_State *L) { 108static int io_gc (lua_State *L) {
116 FILE **f = topfile(L, 1); 109 FILE **f = topfile(L);
117 if (*f != NULL) /* ignore closed files */ 110 if (*f != NULL) /* ignore closed files */
118 aux_close(L); 111 aux_close(L);
119 return 0; 112 return 0;
@@ -121,8 +114,8 @@ static int io_gc (lua_State *L) {
121 114
122 115
123static int io_tostring (lua_State *L) { 116static int io_tostring (lua_State *L) {
124 char buff[4*sizeof(void *) + 2]; /* enough space for a `%p' */ 117 char buff[4*sizeof(void *) + 8]; /* enough space for a `%p' */
125 FILE **f = topfile(L, 1); 118 FILE **f = topfile(L);
126 if (*f == NULL) 119 if (*f == NULL)
127 strcpy(buff, "closed"); 120 strcpy(buff, "closed");
128 else 121 else
@@ -148,9 +141,14 @@ static int io_tmpfile (lua_State *L) {
148} 141}
149 142
150 143
151static FILE *getiofile (lua_State *L, int f) { 144static FILE *getiofile (lua_State *L, int findex) {
152 lua_rawgeti(L, lua_upvalueindex(1), f); 145 FILE *f;
153 return tofile(L, -1); 146 lua_rawgeti(L, lua_upvalueindex(1), findex);
147 lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE));
148 f = *(FILE **)lua_touserdata(L, -1);
149 if (f == NULL)
150 luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
151 return f;
154} 152}
155 153
156 154
@@ -166,9 +164,10 @@ static int g_iofile (lua_State *L, int f, const char *mode) {
166 } 164 }
167 } 165 }
168 else { 166 else {
169 tofile(L, 1); /* check that it's a valid file handle */ 167 tofile(L); /* check that it's a valid file handle */
170 lua_pushvalue(L, 1); 168 lua_pushvalue(L, 1);
171 } 169 }
170 lua_assert(luaL_checkudata(L, -1, LUA_FILEHANDLE));
172 lua_rawseti(L, lua_upvalueindex(1), f); 171 lua_rawseti(L, lua_upvalueindex(1), f);
173 } 172 }
174 /* return current value */ 173 /* return current value */
@@ -199,7 +198,7 @@ static void aux_lines (lua_State *L, int idx, int close) {
199 198
200 199
201static int f_lines (lua_State *L) { 200static int f_lines (lua_State *L) {
202 tofile(L, 1); /* check that it's a valid file handle */ 201 tofile(L); /* check that it's a valid file handle */
203 aux_lines(L, 1, 0); 202 aux_lines(L, 1, 0);
204 return 1; 203 return 1;
205} 204}
@@ -339,7 +338,7 @@ static int io_read (lua_State *L) {
339 338
340 339
341static int f_read (lua_State *L) { 340static int f_read (lua_State *L) {
342 return g_read(L, tofile(L, 1), 2); 341 return g_read(L, tofile(L), 2);
343} 342}
344 343
345 344
@@ -386,14 +385,14 @@ static int io_write (lua_State *L) {
386 385
387 386
388static int f_write (lua_State *L) { 387static int f_write (lua_State *L) {
389 return g_write(L, tofile(L, 1), 2); 388 return g_write(L, tofile(L), 2);
390} 389}
391 390
392 391
393static int f_seek (lua_State *L) { 392static int f_seek (lua_State *L) {
394 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; 393 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
395 static const char *const modenames[] = {"set", "cur", "end", NULL}; 394 static const char *const modenames[] = {"set", "cur", "end", NULL};
396 FILE *f = tofile(L, 1); 395 FILE *f = tofile(L);
397 int op = luaL_findstring(luaL_optstring(L, 2, "cur"), modenames); 396 int op = luaL_findstring(luaL_optstring(L, 2, "cur"), modenames);
398 lua_Integer offset = luaL_optinteger(L, 3, 0); 397 lua_Integer offset = luaL_optinteger(L, 3, 0);
399 luaL_argcheck(L, op != -1, 2, "invalid mode"); 398 luaL_argcheck(L, op != -1, 2, "invalid mode");
@@ -410,7 +409,7 @@ static int f_seek (lua_State *L) {
410static int f_setvbuf (lua_State *L) { 409static int f_setvbuf (lua_State *L) {
411 static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; 410 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
412 static const char *const modenames[] = {"no", "full", "line", NULL}; 411 static const char *const modenames[] = {"no", "full", "line", NULL};
413 FILE *f = tofile(L, 1); 412 FILE *f = tofile(L);
414 int op = luaL_findstring(luaL_checkstring(L, 2), modenames); 413 int op = luaL_findstring(luaL_checkstring(L, 2), modenames);
415 luaL_argcheck(L, op != -1, 2, "invalid mode"); 414 luaL_argcheck(L, op != -1, 2, "invalid mode");
416 return pushresult(L, setvbuf(f, NULL, mode[op], 0) == 0, NULL); 415 return pushresult(L, setvbuf(f, NULL, mode[op], 0) == 0, NULL);
@@ -424,7 +423,7 @@ static int io_flush (lua_State *L) {
424 423
425 424
426static int f_flush (lua_State *L) { 425static int f_flush (lua_State *L) {
427 return pushresult(L, fflush(tofile(L, 1)) == 0, NULL); 426 return pushresult(L, fflush(tofile(L)) == 0, NULL);
428} 427}
429 428
430 429
@@ -459,238 +458,35 @@ static const luaL_reg flib[] = {
459 458
460static void createmeta (lua_State *L) { 459static void createmeta (lua_State *L) {
461 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ 460 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
462 /* create (and set) default files */
463 *newfile(L) = stdin;
464 lua_rawseti(L, -2, IO_INPUT);
465 *newfile(L) = stdout;
466 lua_rawseti(L, -2, IO_OUTPUT);
467 /* file methods */
468 lua_pushvalue(L, -1); /* push metatable */ 461 lua_pushvalue(L, -1); /* push metatable */
469 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ 462 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
470 luaL_openlib(L, NULL, flib, 0); 463 luaL_openlib(L, NULL, flib, 0); /* file methods */
471}
472
473/* }====================================================== */
474
475
476/*
477** {======================================================
478** Other O.S. Operations
479** =======================================================
480*/
481
482static int io_execute (lua_State *L) {
483 lua_pushinteger(L, system(luaL_checkstring(L, 1)));
484 return 1;
485}
486
487
488static int io_remove (lua_State *L) {
489 const char *filename = luaL_checkstring(L, 1);
490 return pushresult(L, remove(filename) == 0, filename);
491}
492
493
494static int io_rename (lua_State *L) {
495 const char *fromname = luaL_checkstring(L, 1);
496 const char *toname = luaL_checkstring(L, 2);
497 return pushresult(L, rename(fromname, toname) == 0, fromname);
498}
499
500
501static int io_tmpname (lua_State *L) {
502#if !USE_TMPNAME
503 luaL_error(L, "`tmpname' not supported");
504 return 0;
505#else
506 char buff[L_tmpnam];
507 if (tmpnam(buff) != buff)
508 return luaL_error(L, "unable to generate a unique filename in `tmpname'");
509 lua_pushstring(L, buff);
510 return 1;
511#endif
512} 464}
513 465
514 466
515static int io_getenv (lua_State *L) { 467static void createupval (lua_State *L) {
516 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ 468 lua_newtable(L);
517 return 1; 469 /* create (and set) default files */
518} 470 *newfile(L) = stdin;
519 471 lua_rawseti(L, -2, IO_INPUT);
520 472 *newfile(L) = stdout;
521static int io_clock (lua_State *L) { 473 lua_rawseti(L, -2, IO_OUTPUT);
522 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
523 return 1;
524}
525
526
527/*
528** {======================================================
529** Time/Date operations
530** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
531** wday=%w+1, yday=%j, isdst=? }
532** =======================================================
533*/
534
535static void setfield (lua_State *L, const char *key, int value) {
536 lua_pushstring(L, key);
537 lua_pushinteger(L, value);
538 lua_rawset(L, -3);
539}
540
541static void setboolfield (lua_State *L, const char *key, int value) {
542 lua_pushstring(L, key);
543 lua_pushboolean(L, value);
544 lua_rawset(L, -3);
545}
546
547static int getboolfield (lua_State *L, const char *key) {
548 int res;
549 lua_getfield(L, -1, key);
550 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
551 lua_pop(L, 1);
552 return res;
553}
554
555
556static int getfield (lua_State *L, const char *key, int d) {
557 int res;
558 lua_getfield(L, -1, key);
559 if (lua_isnumber(L, -1))
560 res = (int)lua_tointeger(L, -1);
561 else {
562 if (d < 0)
563 return luaL_error(L, "field `%s' missing in date table", key);
564 res = d;
565 }
566 lua_pop(L, 1);
567 return res;
568}
569
570
571static int io_date (lua_State *L) {
572 const char *s = luaL_optstring(L, 1, "%c");
573 lua_Number n = luaL_optnumber(L, 2, -1);
574 time_t t = (n == -1) ? time(NULL) : (time_t)n;
575 struct tm *stm;
576 if (*s == '!') { /* UTC? */
577 stm = gmtime(&t);
578 s++; /* skip `!' */
579 }
580 else
581 stm = localtime(&t);
582 if (stm == NULL) /* invalid date? */
583 lua_pushnil(L);
584 else if (strcmp(s, "*t") == 0) {
585 lua_createtable(L, 0, 9); /* 9 = number of fields */
586 setfield(L, "sec", stm->tm_sec);
587 setfield(L, "min", stm->tm_min);
588 setfield(L, "hour", stm->tm_hour);
589 setfield(L, "day", stm->tm_mday);
590 setfield(L, "month", stm->tm_mon+1);
591 setfield(L, "year", stm->tm_year+1900);
592 setfield(L, "wday", stm->tm_wday+1);
593 setfield(L, "yday", stm->tm_yday+1);
594 setboolfield(L, "isdst", stm->tm_isdst);
595 }
596 else {
597 char b[256];
598 if (strftime(b, sizeof(b), s, stm))
599 lua_pushstring(L, b);
600 else
601 return luaL_error(L, "`date' format too long");
602 }
603 return 1;
604}
605
606
607static int io_time (lua_State *L) {
608 if (lua_isnoneornil(L, 1)) /* called without args? */
609 lua_pushnumber(L, time(NULL)); /* return current time */
610 else {
611 time_t t;
612 struct tm ts;
613 luaL_checktype(L, 1, LUA_TTABLE);
614 lua_settop(L, 1); /* make sure table is at the top */
615 ts.tm_sec = getfield(L, "sec", 0);
616 ts.tm_min = getfield(L, "min", 0);
617 ts.tm_hour = getfield(L, "hour", 12);
618 ts.tm_mday = getfield(L, "day", -1);
619 ts.tm_mon = getfield(L, "month", -1) - 1;
620 ts.tm_year = getfield(L, "year", -1) - 1900;
621 ts.tm_isdst = getboolfield(L, "isdst");
622 t = mktime(&ts);
623 if (t == (time_t)(-1))
624 lua_pushnil(L);
625 else
626 lua_pushnumber(L, t);
627 }
628 return 1;
629}
630
631
632static int io_difftime (lua_State *L) {
633 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
634 (time_t)(luaL_optnumber(L, 2, 0))));
635 return 1;
636}
637
638/* }====================================================== */
639
640
641static int io_setloc (lua_State *L) {
642 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
643 LC_NUMERIC, LC_TIME};
644 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
645 "numeric", "time", NULL};
646 const char *l = lua_tostring(L, 1);
647 int op = luaL_findstring(luaL_optstring(L, 2, "all"), catnames);
648 luaL_argcheck(L, l || lua_isnoneornil(L, 1), 1, "string expected");
649 luaL_argcheck(L, op != -1, 2, "invalid option");
650 lua_pushstring(L, setlocale(cat[op], l));
651 return 1;
652}
653
654
655static int io_exit (lua_State *L) {
656 exit(luaL_optint(L, 1, EXIT_SUCCESS));
657 return 0; /* to avoid warnings */
658} 474}
659 475
660static const luaL_reg syslib[] = {
661 {"clock", io_clock},
662 {"date", io_date},
663 {"difftime", io_difftime},
664 {"execute", io_execute},
665 {"exit", io_exit},
666 {"getenv", io_getenv},
667 {"remove", io_remove},
668 {"rename", io_rename},
669 {"setlocale", io_setloc},
670 {"time", io_time},
671 {"tmpname", io_tmpname},
672 {NULL, NULL}
673};
674
675/* }====================================================== */
676
677 476
678 477
679LUALIB_API int luaopen_io (lua_State *L) { 478LUALIB_API int luaopen_io (lua_State *L) {
680 luaL_openlib(L, LUA_OSLIBNAME, syslib, 0);
681 createmeta(L); 479 createmeta(L);
480 createupval(L);
682 lua_pushvalue(L, -1); 481 lua_pushvalue(L, -1);
683 luaL_openlib(L, LUA_IOLIBNAME, iolib, 1); 482 luaL_openlib(L, LUA_IOLIBNAME, iolib, 1);
684 /* put predefined file handles into `io' table */ 483 /* put predefined file handles into `io' table */
685 lua_pushliteral(L, "stdin"); 484 lua_rawgeti(L, -2, IO_INPUT); /* get current input from metatable */
686 lua_rawgeti(L, 2, IO_INPUT); 485 lua_setfield(L, -2, "stdin"); /* io.stdin = metatable[IO_INPUT] */
687 lua_rawset(L, 3); 486 lua_rawgeti(L, -2, IO_OUTPUT); /* get current output from metatable */
688 lua_pushliteral(L, "stdout"); 487 lua_setfield(L, -2, "stdout"); /* io.stdout = metatable[IO_OUTPUT] */
689 lua_rawgeti(L, 2, IO_OUTPUT);
690 lua_rawset(L, 3);
691 lua_pushliteral(L, "stderr");
692 *newfile(L) = stderr; 488 *newfile(L) = stderr;
693 lua_rawset(L, 3); 489 lua_setfield(L, -2, "stderr"); /* io.stderr = newfile(stderr) */
694 return 1; 490 return 1;
695} 491}
696 492
diff --git a/lualib.h b/lualib.h
index 6efe0e2d..24a3c7bf 100644
--- a/lualib.h
+++ b/lualib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lualib.h,v 1.30 2004/05/28 18:35:05 roberto Exp roberto $ 2** $Id: lualib.h,v 1.31 2004/07/09 14:29:29 roberto Exp roberto $
3** Lua standard libraries 3** Lua standard libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -22,9 +22,11 @@ LUALIB_API int luaopen_base (lua_State *L);
22LUALIB_API int luaopen_table (lua_State *L); 22LUALIB_API int luaopen_table (lua_State *L);
23 23
24#define LUA_IOLIBNAME "io" 24#define LUA_IOLIBNAME "io"
25#define LUA_OSLIBNAME "os"
26LUALIB_API int luaopen_io (lua_State *L); 25LUALIB_API int luaopen_io (lua_State *L);
27 26
27#define LUA_OSLIBNAME "os"
28LUALIB_API int luaopen_os (lua_State *L);
29
28#define LUA_STRLIBNAME "string" 30#define LUA_STRLIBNAME "string"
29LUALIB_API int luaopen_string (lua_State *L); 31LUALIB_API int luaopen_string (lua_State *L);
30 32