liolib.c 17 KB

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