liolib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. ** $Id: liolib.c,v 1.73 2000/08/29 14:33:31 roberto Exp roberto $
  3. ** Standard I/O (and system) library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #include "lua.h"
  12. #include "lauxlib.h"
  13. #include "luadebug.h"
  14. #include "lualib.h"
  15. #ifndef OLD_ANSI
  16. #include <errno.h>
  17. #include <locale.h>
  18. #else
  19. /* no support for locale and for strerror: fake them */
  20. #define setlocale(a,b) ((void)a, strcmp((b),"C")==0?"C":NULL)
  21. #define LC_ALL 0
  22. #define LC_COLLATE 0
  23. #define LC_CTYPE 0
  24. #define LC_MONETARY 0
  25. #define LC_NUMERIC 0
  26. #define LC_TIME 0
  27. #define strerror(e) "generic I/O error"
  28. #define errno (-1)
  29. #endif
  30. #ifdef POPEN
  31. /* FILE *popen();
  32. int pclose(); */
  33. #define CLOSEFILE(L, f) ((pclose(f) == -1) ? fclose(f) : 0)
  34. #else
  35. /* no support for popen */
  36. #define popen(x,y) NULL /* that is, popen always fails */
  37. #define CLOSEFILE(L, f) (fclose(f))
  38. #endif
  39. #define INFILE 0
  40. #define OUTFILE 1
  41. typedef struct IOCtrl {
  42. int ref[2]; /* ref for strings _INPUT/_OUTPUT */
  43. int iotag; /* tag for file handles */
  44. int closedtag; /* tag for closed handles */
  45. } IOCtrl;
  46. static const char *const filenames[] = {"_INPUT", "_OUTPUT"};
  47. static int pushresult (lua_State *L, int i) {
  48. if (i) {
  49. lua_pushuserdata(L, NULL);
  50. return 1;
  51. }
  52. else {
  53. lua_pushnil(L);
  54. lua_pushstring(L, strerror(errno));
  55. lua_pushnumber(L, errno);
  56. return 3;;
  57. }
  58. }
  59. /*
  60. ** {======================================================
  61. ** FILE Operations
  62. ** =======================================================
  63. */
  64. static FILE *gethandle (lua_State *L, IOCtrl *ctrl, int f) {
  65. void *p = lua_touserdata(L, f);
  66. if (p != NULL) { /* is `f' a userdata ? */
  67. int ftag = lua_tag(L, f);
  68. if (ftag == ctrl->iotag) /* does it have the correct tag? */
  69. return (FILE *)p;
  70. else if (ftag == ctrl->closedtag)
  71. lua_error(L, "cannot access a closed file");
  72. /* else go through */
  73. }
  74. return NULL;
  75. }
  76. static FILE *getnonullfile (lua_State *L, IOCtrl *ctrl, int arg) {
  77. FILE *f = gethandle(L, ctrl, arg);
  78. luaL_arg_check(L, f, arg, "invalid file handle");
  79. return f;
  80. }
  81. static FILE *getfilebyref (lua_State *L, IOCtrl *ctrl, int inout) {
  82. FILE *f;
  83. lua_getglobals(L);
  84. lua_getref(L, ctrl->ref[inout]);
  85. lua_rawget(L);
  86. f = gethandle(L, ctrl, -1);
  87. if (f == NULL)
  88. luaL_verror(L, "global variable `%.10s' is not a file handle",
  89. filenames[inout]);
  90. return f;
  91. }
  92. static void setfilebyname (lua_State *L, IOCtrl *ctrl, FILE *f,
  93. const char *name) {
  94. lua_pushusertag(L, f, ctrl->iotag);
  95. lua_setglobal(L, name);
  96. }
  97. #define setfile(L,ctrl,f,inout) (setfilebyname(L,ctrl,f,filenames[inout]))
  98. static int setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) {
  99. if (f == NULL)
  100. return pushresult(L, 0);
  101. else {
  102. setfile(L, ctrl, f, inout);
  103. lua_pushusertag(L, f, ctrl->iotag);
  104. return 1;
  105. }
  106. }
  107. static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) {
  108. if (f == stdin || f == stdout || f == stderr)
  109. return 1;
  110. else {
  111. lua_pushusertag(L, f, ctrl->iotag);
  112. lua_settag(L, ctrl->closedtag);
  113. return (CLOSEFILE(L, f) == 0);
  114. }
  115. }
  116. static int io_close (lua_State *L) {
  117. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  118. lua_settop(L, -1); /* remove upvalue */
  119. return pushresult(L, closefile(L, ctrl, getnonullfile(L, ctrl, 1)));
  120. }
  121. static int file_collect (lua_State *L) {
  122. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  123. lua_settop(L, -1); /* remove upvalue */
  124. if (ctrl == (IOCtrl *)lua_touserdata(L, 1)) {
  125. /* collectig `ctrl' itself */
  126. lua_unref(L, ctrl->ref[INFILE]);
  127. lua_unref(L, ctrl->ref[OUTFILE]);
  128. free(ctrl);
  129. }
  130. else { /* collecting a file: Close it */
  131. FILE *f = getnonullfile(L, ctrl, 1);
  132. if (f != stdin && f != stdout && f != stderr)
  133. CLOSEFILE(L, f);
  134. }
  135. return 0;
  136. }
  137. static int io_open (lua_State *L) {
  138. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  139. FILE *f;
  140. lua_settop(L, -1); /* remove upvalue */
  141. f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2));
  142. if (f) {
  143. lua_pushusertag(L, f, ctrl->iotag);
  144. return 1;
  145. }
  146. else
  147. return pushresult(L, 0);
  148. }
  149. static int io_fromto (lua_State *L, int inout, const char *mode) {
  150. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  151. FILE *current;
  152. lua_settop(L, -1); /* remove upvalue */
  153. if (lua_isnull(L, 1)) {
  154. closefile(L, ctrl, getfilebyref(L, ctrl, inout));
  155. current = (inout == 0) ? stdin : stdout;
  156. }
  157. else if (lua_tag(L, 1) == ctrl->iotag) /* deprecated option */
  158. current = (FILE *)lua_touserdata(L, 1);
  159. else {
  160. const char *s = luaL_check_string(L, 1);
  161. current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode);
  162. }
  163. return setreturn(L, ctrl, current, inout);
  164. }
  165. static int io_readfrom (lua_State *L) {
  166. return io_fromto(L, INFILE, "r");
  167. }
  168. static int io_writeto (lua_State *L) {
  169. return io_fromto(L, OUTFILE, "w");
  170. }
  171. static int io_appendto (lua_State *L) {
  172. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  173. FILE *current;
  174. lua_settop(L, -1); /* remove upvalue */
  175. current = fopen(luaL_check_string(L, 1), "a");
  176. return setreturn(L, ctrl, current, OUTFILE);
  177. }
  178. /*
  179. ** {======================================================
  180. ** READ
  181. ** =======================================================
  182. */
  183. #ifdef LUA_COMPAT_READPATTERN
  184. /*
  185. ** We cannot lookahead without need, because this can lock stdin.
  186. ** This flag signals when we need to read a next char.
  187. */
  188. #define NEED_OTHER (EOF-1) /* just some flag different from EOF */
  189. static int read_pattern (lua_State *L, FILE *f, const char *p) {
  190. int inskip = 0; /* {skip} level */
  191. int c = NEED_OTHER;
  192. while (*p != '\0') {
  193. switch (*p) {
  194. case '{':
  195. inskip++;
  196. p++;
  197. continue;
  198. case '}':
  199. if (!inskip) lua_error(L, "unbalanced braces in read pattern");
  200. inskip--;
  201. p++;
  202. continue;
  203. default: {
  204. const char *ep = luaI_classend(L, p); /* get what is next */
  205. int m; /* match result */
  206. if (c == NEED_OTHER) c = getc(f);
  207. m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep);
  208. if (m) {
  209. if (!inskip) luaL_addchar(L, c);
  210. c = NEED_OTHER;
  211. }
  212. switch (*ep) {
  213. case '+': /* repetition (1 or more) */
  214. if (!m) goto break_while; /* pattern fails? */
  215. /* else go through */
  216. case '*': /* repetition (0 or more) */
  217. while (m) { /* reads the same item until it fails */
  218. c = getc(f);
  219. m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep);
  220. if (m && !inskip) luaL_addchar(L, c);
  221. }
  222. /* go through to continue reading the pattern */
  223. case '?': /* optional */
  224. p = ep+1; /* continues reading the pattern */
  225. continue;
  226. default:
  227. if (!m) goto break_while; /* pattern fails? */
  228. p = ep; /* else continues reading the pattern */
  229. }
  230. }
  231. }
  232. } break_while:
  233. if (c != NEED_OTHER) ungetc(c, f);
  234. return (*p == '\0');
  235. }
  236. #else
  237. #define read_pattern(L, f, p) (lua_error(L, "read patterns are deprecated"), 0)
  238. #endif
  239. static int read_number (lua_State *L, FILE *f) {
  240. double d;
  241. if (fscanf(f, "%lf", &d) == 1) {
  242. lua_pushnumber(L, d);
  243. return 1;
  244. }
  245. else return 0; /* read fails */
  246. }
  247. static void read_word (lua_State *L, FILE *f) {
  248. int c;
  249. do { c = fgetc(f); } while (isspace(c)); /* skip spaces */
  250. while (c != EOF && !isspace(c)) {
  251. luaL_addchar(L, c);
  252. c = fgetc(f);
  253. }
  254. ungetc(c, f);
  255. }
  256. #define HUNK_LINE 256
  257. #define HUNK_FILE BUFSIZ
  258. static int read_line (lua_State *L, FILE *f) {
  259. int n;
  260. char *b;
  261. for (;;) {
  262. b = luaL_openspace(L, HUNK_LINE);
  263. if (!fgets(b, HUNK_LINE, f)) return 0; /* read fails */
  264. n = strlen(b);
  265. if (b[n-1] != '\n')
  266. luaL_addsize(L, n);
  267. else {
  268. luaL_addsize(L, n-1); /* do not add the `\n' */
  269. break;
  270. }
  271. }
  272. return 1;
  273. }
  274. static void read_file (lua_State *L, FILE *f) {
  275. size_t n;
  276. do {
  277. char *b = luaL_openspace(L, HUNK_FILE);
  278. n = fread(b, sizeof(char), HUNK_FILE, f);
  279. luaL_addsize(L, n);
  280. } while (n==HUNK_FILE);
  281. }
  282. static int read_chars (lua_State *L, FILE *f, size_t n) {
  283. char *b = luaL_openspace(L, n);
  284. size_t n1 = fread(b, sizeof(char), n, f);
  285. luaL_addsize(L, n1);
  286. return (n == n1);
  287. }
  288. static int io_read (lua_State *L) {
  289. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  290. int lastarg = lua_gettop(L) - 1;
  291. int firstarg = 1;
  292. FILE *f = gethandle(L, ctrl, firstarg);
  293. int n;
  294. if (f) firstarg++;
  295. else f = getfilebyref(L, ctrl, INFILE); /* get _INPUT */
  296. lua_settop(L, -1);
  297. if (firstarg > lastarg) { /* no arguments? */
  298. lua_settop(L, 0); /* erase upvalue and other eventual garbage */
  299. firstarg = lastarg = 1; /* correct indices */
  300. lua_pushstring(L, "*l"); /* push default argument */
  301. }
  302. luaL_checkstack(L, lastarg-firstarg+1, "too many results");
  303. for (n = firstarg; n<=lastarg; n++) {
  304. size_t l;
  305. int success;
  306. luaL_resetbuffer(L);
  307. if (lua_isnumber(L, n))
  308. success = read_chars(L, f, (size_t)lua_tonumber(L, n));
  309. else {
  310. const char *p = luaL_check_string(L, n);
  311. if (p[0] != '*')
  312. success = read_pattern(L, f, p); /* deprecated! */
  313. else {
  314. switch (p[1]) {
  315. case 'n': /* number */
  316. if (!read_number(L, f)) goto endloop; /* read fails */
  317. continue; /* number is already pushed; avoid the "pushstring" */
  318. case 'l': /* line */
  319. success = read_line(L, f);
  320. break;
  321. case 'a': /* file */
  322. read_file(L, f);
  323. success = 1; /* always success */
  324. break;
  325. case 'w': /* word */
  326. read_word(L, f);
  327. success = 0; /* must read something to succeed */
  328. break;
  329. default:
  330. luaL_argerror(L, n, "invalid format");
  331. success = 0; /* to avoid warnings */
  332. }
  333. }
  334. }
  335. l = luaL_getsize(L);
  336. if (!success && l==0) break; /* read fails */
  337. lua_pushlstring(L, luaL_buffer(L), l);
  338. } endloop:
  339. return n - firstarg;
  340. }
  341. /* }====================================================== */
  342. static int io_write (lua_State *L) {
  343. int lastarg = lua_gettop(L) - 1;
  344. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  345. int arg = 1;
  346. int status = 1;
  347. FILE *f = gethandle(L, ctrl, arg);
  348. if (f) arg++;
  349. else f = getfilebyref(L, ctrl, OUTFILE); /* get _OUTPUT */
  350. for (; arg <= lastarg; arg++) {
  351. if (lua_type(L, arg)[2] == 'm') { /* nuMber? */ /* LUA_NUMBER */
  352. /* optimization: could be done exactly as for strings */
  353. status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0;
  354. }
  355. else {
  356. size_t l;
  357. const char *s = luaL_check_lstr(L, arg, &l);
  358. status = status && (fwrite(s, sizeof(char), l, f) == l);
  359. }
  360. }
  361. pushresult(L, status);
  362. return 1;
  363. }
  364. static int io_seek (lua_State *L) {
  365. static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  366. static const char *const modenames[] = {"set", "cur", "end", NULL};
  367. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  368. FILE *f;
  369. int op;
  370. long offset;
  371. lua_settop(L, -1); /* remove upvalue */
  372. f = getnonullfile(L, ctrl, 1);
  373. op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
  374. offset = luaL_opt_long(L, 3, 0);
  375. luaL_arg_check(L, op != -1, 2, "invalid mode");
  376. op = fseek(f, offset, mode[op]);
  377. if (op)
  378. return pushresult(L, 0); /* error */
  379. else {
  380. lua_pushnumber(L, ftell(f));
  381. return 1;
  382. }
  383. }
  384. static int io_flush (lua_State *L) {
  385. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  386. FILE *f;
  387. lua_settop(L, -1); /* remove upvalue */
  388. f = gethandle(L, ctrl, 1);
  389. luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle");
  390. return pushresult(L, fflush(f) == 0);
  391. }
  392. /* }====================================================== */
  393. /*
  394. ** {======================================================
  395. ** Other O.S. Operations
  396. ** =======================================================
  397. */
  398. static int io_execute (lua_State *L) {
  399. lua_pushnumber(L, system(luaL_check_string(L, 1)));
  400. return 1;
  401. }
  402. static int io_remove (lua_State *L) {
  403. return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
  404. }
  405. static int io_rename (lua_State *L) {
  406. return pushresult(L, rename(luaL_check_string(L, 1),
  407. luaL_check_string(L, 2)) == 0);
  408. }
  409. static int io_tmpname (lua_State *L) {
  410. lua_pushstring(L, tmpnam(NULL));
  411. return 1;
  412. }
  413. static int io_getenv (lua_State *L) {
  414. lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
  415. return 1;
  416. }
  417. static int io_clock (lua_State *L) {
  418. lua_pushnumber(L, ((double)clock())/CLOCKS_PER_SEC);
  419. return 1;
  420. }
  421. static int io_date (lua_State *L) {
  422. char b[256];
  423. const char *s = luaL_opt_string(L, 1, "%c");
  424. struct tm *stm;
  425. time_t t;
  426. time(&t); stm = localtime(&t);
  427. if (strftime(b, sizeof(b), s, stm))
  428. lua_pushstring(L, b);
  429. else
  430. lua_error(L, "invalid `date' format");
  431. return 1;
  432. }
  433. static int setloc (lua_State *L) {
  434. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  435. LC_NUMERIC, LC_TIME};
  436. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  437. "numeric", "time", NULL};
  438. int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
  439. luaL_arg_check(L, op != -1, 2, "invalid option");
  440. lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));
  441. return 1;
  442. }
  443. static int io_exit (lua_State *L) {
  444. exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
  445. return 0; /* to avoid warnings */
  446. }
  447. /* }====================================================== */
  448. static int io_debug (lua_State *L) {
  449. for (;;) {
  450. char buffer[250];
  451. fprintf(stderr, "lua_debug> ");
  452. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  453. strcmp(buffer, "cont\n") == 0)
  454. return 0;
  455. lua_dostring(L, buffer);
  456. lua_settop(L, 0); /* remove eventual returns */
  457. }
  458. }
  459. #define MESSAGESIZE 150
  460. #define MAXMESSAGE (MESSAGESIZE*10)
  461. static int errorfb (lua_State *L) {
  462. char buff[MAXMESSAGE];
  463. int level = 1; /* skip level 0 (it's this function) */
  464. lua_Debug ar;
  465. sprintf(buff, "error: %.200s\n", lua_tostring(L, 1));
  466. while (lua_getstack(L, level++, &ar)) {
  467. char buffchunk[60];
  468. lua_getinfo(L, "Snl", &ar);
  469. luaL_chunkid(buffchunk, ar.source, sizeof(buffchunk));
  470. if (level == 2) strcat(buff, "stack traceback:\n");
  471. strcat(buff, " ");
  472. if (strlen(buff) > MAXMESSAGE-MESSAGESIZE) {
  473. strcat(buff, "...\n");
  474. break; /* buffer is full */
  475. }
  476. switch (*ar.namewhat) {
  477. case 'g': case 'l': /* global, local */
  478. sprintf(buff+strlen(buff), "function `%.50s'", ar.name);
  479. break;
  480. case 'f': /* field */
  481. sprintf(buff+strlen(buff), "method `%.50s'", ar.name);
  482. break;
  483. case 't': /* tag method */
  484. sprintf(buff+strlen(buff), "`%.50s' tag method", ar.name);
  485. break;
  486. default: {
  487. if (*ar.what == 'm') /* main? */
  488. sprintf(buff+strlen(buff), "main of %.70s", buffchunk);
  489. else if (*ar.what == 'C') /* C function? */
  490. sprintf(buff+strlen(buff), "%.70s", buffchunk);
  491. else
  492. sprintf(buff+strlen(buff), "function <%d:%.70s>",
  493. ar.linedefined, buffchunk);
  494. ar.source = NULL;
  495. }
  496. }
  497. if (ar.currentline > 0)
  498. sprintf(buff+strlen(buff), " at line %d", ar.currentline);
  499. if (ar.source)
  500. sprintf(buff+strlen(buff), " [%.70s]", buffchunk);
  501. strcat(buff, "\n");
  502. }
  503. lua_getglobals(L);
  504. lua_pushstring(L, LUA_ALERT);
  505. lua_rawget(L);
  506. if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */
  507. lua_pushstring(L, buff);
  508. lua_call(L, 1, 0);
  509. }
  510. return 0;
  511. }
  512. static const struct luaL_reg iolib[] = {
  513. {LUA_ERRORMESSAGE, errorfb},
  514. {"clock", io_clock},
  515. {"date", io_date},
  516. {"debug", io_debug},
  517. {"execute", io_execute},
  518. {"exit", io_exit},
  519. {"getenv", io_getenv},
  520. {"remove", io_remove},
  521. {"rename", io_rename},
  522. {"setlocale", setloc},
  523. {"tmpname", io_tmpname}
  524. };
  525. static const struct luaL_reg iolibtag[] = {
  526. {"appendto", io_appendto},
  527. {"closefile", io_close},
  528. {"flush", io_flush},
  529. {"openfile", io_open},
  530. {"read", io_read},
  531. {"readfrom", io_readfrom},
  532. {"seek", io_seek},
  533. {"write", io_write},
  534. {"writeto", io_writeto}
  535. };
  536. static void openwithcontrol (lua_State *L) {
  537. IOCtrl *ctrl = (IOCtrl *)malloc(sizeof(IOCtrl));
  538. unsigned int i;
  539. int ctrltag = lua_newtag(L);
  540. ctrl->iotag = lua_newtag(L);
  541. ctrl->closedtag = lua_newtag(L);
  542. for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
  543. /* put `ctrl' as upvalue for these functions */
  544. lua_pushusertag(L, ctrl, ctrltag);
  545. lua_pushcclosure(L, iolibtag[i].func, 1);
  546. lua_setglobal(L, iolibtag[i].name);
  547. }
  548. /* create references to variable names */
  549. lua_pushstring(L, filenames[INFILE]);
  550. ctrl->ref[INFILE] = lua_ref(L, 1);
  551. lua_pushstring(L, filenames[OUTFILE]);
  552. ctrl->ref[OUTFILE] = lua_ref(L, 1);
  553. /* predefined file handles */
  554. setfile(L, ctrl, stdin, INFILE);
  555. setfile(L, ctrl, stdout, OUTFILE);
  556. setfilebyname(L, ctrl, stdin, "_STDIN");
  557. setfilebyname(L, ctrl, stdout, "_STDOUT");
  558. setfilebyname(L, ctrl, stderr, "_STDERR");
  559. /* delete `ctrl' when collected */
  560. lua_pushusertag(L, ctrl, ctrltag);
  561. lua_pushcclosure(L, file_collect, 1);
  562. lua_settagmethod(L, ctrltag, "gc");
  563. /* close files when collected */
  564. lua_copytagmethods(L, ctrl->iotag, ctrltag);
  565. }
  566. void lua_iolibopen (lua_State *L) {
  567. luaL_openl(L, iolib);
  568. openwithcontrol(L);
  569. }