liolib.c 18 KB

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