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