liolib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. ** $Id: liolib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  3. ** Standard I/O (and system) library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <errno.h>
  7. #include <locale.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include "lua.h"
  13. #include "lauxlib.h"
  14. #include "luadebug.h"
  15. #include "lualib.h"
  16. #ifdef POPEN
  17. /* FILE *popen();
  18. int pclose(); */
  19. #define CLOSEFILE(L, f) ((pclose(f) == -1) ? fclose(f) : 0)
  20. #else
  21. /* no support for popen */
  22. #define popen(x,y) NULL /* that is, popen always fails */
  23. #define CLOSEFILE(L, f) (fclose(f))
  24. #endif
  25. #define INFILE 0
  26. #define OUTFILE 1
  27. #define NOFILE 2
  28. #define FILEHANDLE "FileHandle"
  29. #define CLOSEDFILEHANDLE "ClosedFileHandle"
  30. static const char *const filenames[] = {"_INPUT", "_OUTPUT"};
  31. static const char *const basicfiles[] = {"_STDIN", "_STDOUT"};
  32. static int pushresult (lua_State *L, int i) {
  33. if (i) {
  34. lua_pushnumber(L, 1);
  35. return 1;
  36. }
  37. else {
  38. lua_pushnil(L);
  39. lua_pushstring(L, strerror(errno));
  40. lua_pushnumber(L, errno);
  41. return 3;
  42. }
  43. }
  44. /*
  45. ** {======================================================
  46. ** FILE Operations
  47. ** =======================================================
  48. */
  49. static int checkfile (lua_State *L, int findex, const char *tname) {
  50. int res;
  51. lua_getmetatable(L, findex);
  52. lua_pushstring(L, tname);
  53. lua_rawget(L, LUA_REGISTRYINDEX);
  54. res = lua_equal(L, -1, -2);
  55. lua_pop(L, 2);
  56. return res;
  57. }
  58. /* temporary?? should be in auxlib... */
  59. static void *luaL_check_userdata (lua_State *L, int findex, const char *tn) {
  60. luaL_arg_check(L, checkfile(L, findex, tn), findex, "bad file");
  61. return lua_touserdata(L, findex);
  62. }
  63. static FILE *getopthandle (lua_State *L, int inout) {
  64. FILE *p = (FILE *)(lua_touserdata(L, 1));
  65. if (p != NULL) { /* is it a userdata ? */
  66. if (!checkfile(L, 1, FILEHANDLE)) { /* not a valid file handle? */
  67. if (checkfile(L, 1, CLOSEDFILEHANDLE))
  68. luaL_argerror(L, 1, "file is closed");
  69. else
  70. luaL_argerror(L, 1, "(invalid value)");
  71. }
  72. lua_pushvalue(L, 1); lua_remove(L, 1); /* move it to stack top */
  73. }
  74. else { /* try global value */
  75. lua_getglobal(L, filenames[inout]);
  76. if (!checkfile(L, -1, FILEHANDLE))
  77. luaL_verror(L, "global variable `%.10s' is not a valid file handle",
  78. filenames[inout]);
  79. p = (FILE *)(lua_touserdata(L, -1));
  80. }
  81. return p; /* leave handle at stack top to avoid GC */
  82. }
  83. static void newfile (lua_State *L, FILE *f) {
  84. lua_newuserdatabox(L, f);
  85. lua_pushliteral(L, FILEHANDLE);
  86. lua_rawget(L, LUA_REGISTRYINDEX);
  87. lua_setmetatable(L, -2);
  88. }
  89. static void newfilewithname (lua_State *L, FILE *f, const char *name) {
  90. newfile(L, f);
  91. lua_setglobal(L, name);
  92. }
  93. static int setnewfile (lua_State *L, FILE *f, int inout) {
  94. if (f == NULL)
  95. return pushresult(L, 0);
  96. else {
  97. newfile(L, f);
  98. if (inout != NOFILE) {
  99. lua_pushvalue(L, -1);
  100. lua_setglobal(L, filenames[inout]);
  101. }
  102. return 1;
  103. }
  104. }
  105. static void resetfile (lua_State *L, int inout) {
  106. lua_getglobal(L, basicfiles[inout]);
  107. lua_setglobal(L, filenames[inout]);
  108. }
  109. static int io_close (lua_State *L) {
  110. FILE *f = (FILE *)(luaL_check_userdata(L, 1, FILEHANDLE));
  111. int status = 1;
  112. if (f != stdin && f != stdout && f != stderr) {
  113. lua_settop(L, 1); /* make sure file is on top */
  114. lua_pushliteral(L, CLOSEDFILEHANDLE);
  115. lua_rawget(L, LUA_REGISTRYINDEX);
  116. lua_setmetatable(L, 1);
  117. status = (CLOSEFILE(L, f) == 0);
  118. }
  119. return pushresult(L, status);
  120. }
  121. static int file_collect (lua_State *L) {
  122. FILE *f = (FILE *)(luaL_check_userdata(L, 1, FILEHANDLE));
  123. if (f != stdin && f != stdout && f != stderr)
  124. CLOSEFILE(L, f);
  125. return 0;
  126. }
  127. static int io_open (lua_State *L) {
  128. FILE *f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2));
  129. return setnewfile(L, f, NOFILE);
  130. }
  131. static int io_tmpfile (lua_State *L) {
  132. return setnewfile(L, tmpfile(), NOFILE);
  133. }
  134. static int io_fromto (lua_State *L, int inout, const char *mode) {
  135. FILE *current;
  136. if (lua_isnone(L, 1)) {
  137. getopthandle(L, inout);
  138. resetfile(L, inout);
  139. return io_close(L);
  140. }
  141. else {
  142. const char *s = luaL_check_string(L, 1);
  143. current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode);
  144. return setnewfile(L, current, inout);
  145. }
  146. }
  147. static int io_readfrom (lua_State *L) {
  148. return io_fromto(L, INFILE, "r");
  149. }
  150. static int io_writeto (lua_State *L) {
  151. return io_fromto(L, OUTFILE, "w");
  152. }
  153. static int io_appendto (lua_State *L) {
  154. FILE *current = fopen(luaL_check_string(L, 1), "a");
  155. return setnewfile(L, current, OUTFILE);
  156. }
  157. /*
  158. ** {======================================================
  159. ** READ
  160. ** =======================================================
  161. */
  162. #ifndef LUA_MAXUNTIL
  163. #define LUA_MAXUNTIL 100
  164. #endif
  165. /*
  166. ** Knuth-Morris-Pratt algorithm for string searching
  167. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  168. ** Addison-Wesley, 1993.)
  169. */
  170. static void prep_read_until (int next[], const char *p, int pl) {
  171. int i = 0;
  172. int j = -1;
  173. next[0] = -1;
  174. while (i < pl) {
  175. if (j == -1 || p[i] == p[j]) {
  176. i++; j++; next[i] = j;
  177. }
  178. else j = next[j];
  179. }
  180. }
  181. static int read_until (lua_State *L, FILE *f, const char *p, int pl) {
  182. int c;
  183. int j;
  184. int next[LUA_MAXUNTIL+1];
  185. luaL_Buffer b;
  186. luaL_buffinit(L, &b);
  187. prep_read_until(next, p, pl);
  188. j = 0;
  189. while ((c = getc(f)) != EOF) {
  190. NoRead:
  191. if (c == p[j]) {
  192. j++; /* go to next char in pattern */
  193. if (j == pl) { /* complete match? */
  194. luaL_pushresult(&b); /* close buffer */
  195. return 1; /* always success */
  196. }
  197. }
  198. else if (j == 0)
  199. luaL_putchar(&b, c);
  200. else { /* match fail */
  201. luaL_addlstring(&b, p, j - next[j]); /* put failed part on result */
  202. j = next[j]; /* backtrack pattern index */
  203. goto NoRead; /* repeat without reading next char */
  204. }
  205. }
  206. /* end of file without a match */
  207. luaL_addlstring(&b, p, j); /* put failed part on result */
  208. luaL_pushresult(&b); /* close buffer */
  209. return (lua_strlen(L, -1) > 0);
  210. }
  211. static int read_number (lua_State *L, FILE *f) {
  212. lua_Number d;
  213. if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
  214. lua_pushnumber(L, d);
  215. return 1;
  216. }
  217. else return 0; /* read fails */
  218. }
  219. static int test_eof (lua_State *L, FILE *f) {
  220. int c = getc(f);
  221. ungetc(c, f);
  222. lua_pushlstring(L, NULL, 0);
  223. return (c != EOF);
  224. }
  225. static int read_chars (lua_State *L, FILE *f, size_t n) {
  226. size_t rlen; /* how much to read */
  227. size_t nr; /* number of chars actually read */
  228. luaL_Buffer b;
  229. luaL_buffinit(L, &b);
  230. rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
  231. do {
  232. char *p = luaL_prepbuffer(&b);
  233. if (rlen > n) rlen = n; /* cannot read more than asked */
  234. nr = fread(p, sizeof(char), rlen, f);
  235. luaL_addsize(&b, nr);
  236. n -= nr; /* still have to read `n' chars */
  237. } while (n > 0 && nr == rlen); /* until end of count or eof */
  238. luaL_pushresult(&b); /* close buffer */
  239. return (n == 0 || lua_strlen(L, -1) > 0);
  240. }
  241. static int io_read (lua_State *L) {
  242. FILE *f = getopthandle(L, INFILE);
  243. int nargs = lua_gettop(L) - 1;
  244. int success;
  245. int n;
  246. if (nargs == 0) { /* no arguments? */
  247. success = read_until(L, f, "\n", 1); /* read until \n (a line) */
  248. n = 2; /* will return n-1 results */
  249. }
  250. else { /* ensure stack space for all results and for auxlib's buffer */
  251. luaL_check_stack(L, nargs+LUA_MINSTACK, "too many arguments");
  252. success = 1;
  253. for (n = 1; n<=nargs && success; n++) {
  254. if (lua_type(L, n) == LUA_TNUMBER) {
  255. size_t l = (size_t)lua_tonumber(L, n);
  256. success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
  257. }
  258. else {
  259. const char *p = lua_tostring(L, n);
  260. if (!p || p[0] != '*')
  261. lua_error(L, "invalid `read' option");
  262. switch (p[1]) {
  263. case 'n': /* number */
  264. success = read_number(L, f);
  265. break;
  266. case 'l': /* line */
  267. success = read_until(L, f, "\n", 1); /* read until \n */
  268. break;
  269. case 'a': /* file */
  270. read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
  271. success = 1; /* always success */
  272. break;
  273. case 'w': /* word */
  274. lua_error(L, "obsolete option `*w'");
  275. break;
  276. case 'u': { /* read until */
  277. size_t pl = lua_strlen(L, n) - 2;
  278. luaL_arg_check(L, 0 < pl && pl <= LUA_MAXUNTIL, n,
  279. "invalid read-until length");
  280. success = read_until(L, f, p+2, (int)(pl));
  281. break;
  282. }
  283. default:
  284. luaL_argerror(L, n, "invalid format");
  285. success = 0; /* to avoid warnings */
  286. }
  287. }
  288. }
  289. }
  290. if (!success) {
  291. lua_pop(L, 1); /* remove last result */
  292. lua_pushnil(L); /* push nil instead */
  293. }
  294. return n - 1;
  295. }
  296. /* }====================================================== */
  297. static int io_write (lua_State *L) {
  298. FILE *f = getopthandle(L, OUTFILE);
  299. int nargs = lua_gettop(L)-1;
  300. int arg;
  301. int status = 1;
  302. for (arg=1; arg<=nargs; arg++) {
  303. if (lua_type(L, arg) == LUA_TNUMBER) {
  304. /* optimization: could be done exactly as for strings */
  305. status = status &&
  306. fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
  307. }
  308. else {
  309. size_t l;
  310. const char *s = luaL_check_lstr(L, arg, &l);
  311. status = status && (fwrite(s, sizeof(char), l, f) == l);
  312. }
  313. }
  314. pushresult(L, status);
  315. return 1;
  316. }
  317. static int io_seek (lua_State *L) {
  318. static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  319. static const char *const modenames[] = {"set", "cur", "end", NULL};
  320. FILE *f = (FILE *)(luaL_check_userdata(L, 1, FILEHANDLE));
  321. int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
  322. long offset = luaL_opt_long(L, 3, 0);
  323. luaL_arg_check(L, op != -1, 2, "invalid mode");
  324. op = fseek(f, offset, mode[op]);
  325. if (op)
  326. return pushresult(L, 0); /* error */
  327. else {
  328. lua_pushnumber(L, ftell(f));
  329. return 1;
  330. }
  331. }
  332. static int io_flush (lua_State *L) {
  333. FILE *f = (lua_isnone(L, 1)) ? (FILE *)(NULL) :
  334. (FILE *)(luaL_check_userdata(L, 1, FILEHANDLE));
  335. return pushresult(L, fflush(f) == 0);
  336. }
  337. /* }====================================================== */
  338. /*
  339. ** {======================================================
  340. ** Other O.S. Operations
  341. ** =======================================================
  342. */
  343. static int io_execute (lua_State *L) {
  344. lua_pushnumber(L, system(luaL_check_string(L, 1)));
  345. return 1;
  346. }
  347. static int io_remove (lua_State *L) {
  348. return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
  349. }
  350. static int io_rename (lua_State *L) {
  351. return pushresult(L, rename(luaL_check_string(L, 1),
  352. luaL_check_string(L, 2)) == 0);
  353. }
  354. static int io_tmpname (lua_State *L) {
  355. char buff[L_tmpnam];
  356. if (tmpnam(buff) != buff)
  357. lua_error(L, "unable to generate a unique filename");
  358. lua_pushstring(L, buff);
  359. return 1;
  360. }
  361. static int io_getenv (lua_State *L) {
  362. lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
  363. return 1;
  364. }
  365. static int io_clock (lua_State *L) {
  366. lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
  367. return 1;
  368. }
  369. /*
  370. ** {======================================================
  371. ** Time/Date operations
  372. ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
  373. ** wday=%w+1, yday=%j, isdst=? }
  374. ** =======================================================
  375. */
  376. static void setfield (lua_State *L, const char *key, int value) {
  377. lua_pushstring(L, key);
  378. lua_pushnumber(L, value);
  379. lua_rawset(L, -3);
  380. }
  381. static int getfield (lua_State *L, const char *key, int d) {
  382. int res;
  383. lua_pushstring(L, key);
  384. lua_gettable(L, -2);
  385. if (lua_isnumber(L, -1))
  386. res = (int)(lua_tonumber(L, -1));
  387. else {
  388. if (d == -2)
  389. luaL_verror(L, "field `%.20s' missing in date table", key);
  390. res = d;
  391. }
  392. lua_pop(L, 1);
  393. return res;
  394. }
  395. static int io_date (lua_State *L) {
  396. const char *s = luaL_opt_string(L, 1, "%c");
  397. time_t t = (time_t)(luaL_opt_number(L, 2, -1));
  398. struct tm *stm;
  399. if (t == (time_t)(-1)) /* no time given? */
  400. t = time(NULL); /* use current time */
  401. if (*s == '!') { /* UTC? */
  402. stm = gmtime(&t);
  403. s++; /* skip `!' */
  404. }
  405. else
  406. stm = localtime(&t);
  407. if (stm == NULL) /* invalid date? */
  408. lua_pushnil(L);
  409. else if (strcmp(s, "*t") == 0) {
  410. lua_newtable(L);
  411. setfield(L, "sec", stm->tm_sec);
  412. setfield(L, "min", stm->tm_min);
  413. setfield(L, "hour", stm->tm_hour);
  414. setfield(L, "day", stm->tm_mday);
  415. setfield(L, "month", stm->tm_mon+1);
  416. setfield(L, "year", stm->tm_year+1900);
  417. setfield(L, "wday", stm->tm_wday+1);
  418. setfield(L, "yday", stm->tm_yday+1);
  419. setfield(L, "isdst", stm->tm_isdst);
  420. }
  421. else {
  422. char b[256];
  423. if (strftime(b, sizeof(b), s, stm))
  424. lua_pushstring(L, b);
  425. else
  426. lua_error(L, "invalid `date' format");
  427. }
  428. return 1;
  429. }
  430. static int io_time (lua_State *L) {
  431. if (lua_isnone(L, 1)) /* called without args? */
  432. lua_pushnumber(L, time(NULL)); /* return current time */
  433. else {
  434. time_t t;
  435. struct tm ts;
  436. luaL_check_type(L, 1, LUA_TTABLE);
  437. lua_settop(L, 1); /* make sure table is at the top */
  438. ts.tm_sec = getfield(L, "sec", 0);
  439. ts.tm_min = getfield(L, "min", 0);
  440. ts.tm_hour = getfield(L, "hour", 12);
  441. ts.tm_mday = getfield(L, "day", -2);
  442. ts.tm_mon = getfield(L, "month", -2)-1;
  443. ts.tm_year = getfield(L, "year", -2)-1900;
  444. ts.tm_isdst = getfield(L, "isdst", -1);
  445. t = mktime(&ts);
  446. if (t == (time_t)(-1))
  447. lua_pushnil(L);
  448. else
  449. lua_pushnumber(L, t);
  450. }
  451. return 1;
  452. }
  453. static int io_difftime (lua_State *L) {
  454. lua_pushnumber(L, difftime((time_t)(luaL_check_number(L, 1)),
  455. (time_t)(luaL_opt_number(L, 2, 0))));
  456. return 1;
  457. }
  458. /* }====================================================== */
  459. static int io_setloc (lua_State *L) {
  460. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  461. LC_NUMERIC, LC_TIME};
  462. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  463. "numeric", "time", NULL};
  464. const char *l = lua_tostring(L, 1);
  465. int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
  466. luaL_arg_check(L, l || lua_isnil(L, 1) || lua_isnone(L, 1), 1,
  467. "string expected");
  468. luaL_arg_check(L, op != -1, 2, "invalid option");
  469. lua_pushstring(L, setlocale(cat[op], l));
  470. return 1;
  471. }
  472. static int io_exit (lua_State *L) {
  473. exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
  474. return 0; /* to avoid warnings */
  475. }
  476. /* }====================================================== */
  477. static int io_debug (lua_State *L) {
  478. for (;;) {
  479. char buffer[250];
  480. fprintf(stderr, "lua_debug> ");
  481. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  482. strcmp(buffer, "cont\n") == 0)
  483. return 0;
  484. lua_dostring(L, buffer);
  485. lua_settop(L, 0); /* remove eventual returns */
  486. }
  487. }
  488. #define LEVELS1 12 /* size of the first part of the stack */
  489. #define LEVELS2 10 /* size of the second part of the stack */
  490. static int errorfb (lua_State *L) {
  491. int level = 1; /* skip level 0 (it's this function) */
  492. int firstpart = 1; /* still before eventual `...' */
  493. lua_Debug ar;
  494. luaL_Buffer b;
  495. luaL_buffinit(L, &b);
  496. luaL_addstring(&b, "error: ");
  497. luaL_addstring(&b, luaL_check_string(L, 1));
  498. luaL_addstring(&b, "\n");
  499. while (lua_getstack(L, level++, &ar)) {
  500. char buff[120]; /* enough to fit following `sprintf's */
  501. if (level == 2)
  502. luaL_addstring(&b, "stack traceback:\n");
  503. else if (level > LEVELS1 && firstpart) {
  504. /* no more than `LEVELS2' more levels? */
  505. if (!lua_getstack(L, level+LEVELS2, &ar))
  506. level--; /* keep going */
  507. else {
  508. luaL_addstring(&b, " ...\n"); /* too many levels */
  509. while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
  510. level++;
  511. }
  512. firstpart = 0;
  513. continue;
  514. }
  515. sprintf(buff, "%4d: ", level-1);
  516. luaL_addstring(&b, buff);
  517. lua_getinfo(L, "Snl", &ar);
  518. switch (*ar.namewhat) {
  519. case 'g': case 'l': /* global, local */
  520. sprintf(buff, "function `%.50s'", ar.name);
  521. break;
  522. case 'f': /* field */
  523. sprintf(buff, "method `%.50s'", ar.name);
  524. break;
  525. case 't': /* tag method */
  526. sprintf(buff, "`%.50s' tag method", ar.name);
  527. break;
  528. default: {
  529. if (*ar.what == 'm') /* main? */
  530. sprintf(buff, "main of %.70s", ar.short_src);
  531. else if (*ar.what == 'C') /* C function? */
  532. sprintf(buff, "%.70s", ar.short_src);
  533. else
  534. sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src);
  535. ar.source = NULL; /* do not print source again */
  536. }
  537. }
  538. luaL_addstring(&b, buff);
  539. if (ar.currentline > 0) {
  540. sprintf(buff, " at line %d", ar.currentline);
  541. luaL_addstring(&b, buff);
  542. }
  543. if (ar.source) {
  544. sprintf(buff, " [%.70s]", ar.short_src);
  545. luaL_addstring(&b, buff);
  546. }
  547. luaL_addstring(&b, "\n");
  548. }
  549. luaL_pushresult(&b);
  550. lua_getglobal(L, LUA_ALERT);
  551. if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */
  552. lua_pushvalue(L, -2); /* error message */
  553. lua_rawcall(L, 1, 0);
  554. }
  555. return 0;
  556. }
  557. static const luaL_reg iolib[] = {
  558. {"appendto", io_appendto},
  559. {"clock", io_clock},
  560. {"closefile", io_close},
  561. {"date", io_date},
  562. {"debug", io_debug},
  563. {"difftime", io_difftime},
  564. {"execute", io_execute},
  565. {"exit", io_exit},
  566. {"flush", io_flush},
  567. {"getenv", io_getenv},
  568. {"openfile", io_open},
  569. {"read", io_read},
  570. {"readfrom", io_readfrom},
  571. {"remove", io_remove},
  572. {"rename", io_rename},
  573. {"seek", io_seek},
  574. {"setlocale", io_setloc},
  575. {"time", io_time},
  576. {"tmpfile", io_tmpfile},
  577. {"tmpname", io_tmpname},
  578. {"write", io_write},
  579. {"writeto", io_writeto},
  580. {LUA_ERRORMESSAGE, errorfb}
  581. };
  582. LUALIB_API int lua_iolibopen (lua_State *L) {
  583. lua_pushliteral(L, FILEHANDLE);
  584. lua_newtable(L); /* meta table for FILEHANDLE */
  585. /* close files when collected */
  586. lua_pushliteral(L, "gc");
  587. lua_pushcfunction(L, file_collect);
  588. lua_rawset(L, -3);
  589. /* put new metatable into registry */
  590. lua_rawset(L, LUA_REGISTRYINDEX);
  591. /* meta table for CLOSEDFILEHANDLE */
  592. lua_pushliteral(L, CLOSEDFILEHANDLE);
  593. lua_newtable(L);
  594. lua_rawset(L, LUA_REGISTRYINDEX);
  595. luaL_openl(L, iolib);
  596. /* predefined file handles */
  597. newfilewithname(L, stdin, basicfiles[INFILE]);
  598. newfilewithname(L, stdout, basicfiles[OUTFILE]);
  599. newfilewithname(L, stderr, "_STDERR");
  600. resetfile(L, INFILE);
  601. resetfile(L, OUTFILE);
  602. return 0;
  603. }