liolib.c 17 KB

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