liolib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. ** $Id: liolib.c,v 1.76 2000/08/31 20:23:40 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, -2);
  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_pop(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_pop(L, 1); /* remove upvalue */
  124. if (ctrl == (IOCtrl *)lua_touserdata(L, 1)) {
  125. /* collecting `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_pop(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_pop(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_pop(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_pop(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. else
  303. luaL_checkstack(L, lastarg-firstarg+1, "too many arguments");
  304. for (n = firstarg; n<=lastarg; n++) {
  305. size_t l;
  306. int success;
  307. luaL_resetbuffer(L);
  308. if (lua_isnumber(L, n))
  309. success = read_chars(L, f, (size_t)lua_tonumber(L, n));
  310. else {
  311. const char *p = luaL_check_string(L, n);
  312. if (p[0] != '*')
  313. success = read_pattern(L, f, p); /* deprecated! */
  314. else {
  315. switch (p[1]) {
  316. case 'n': /* number */
  317. if (!read_number(L, f)) goto endloop; /* read fails */
  318. continue; /* number is already pushed; avoid the "pushstring" */
  319. case 'l': /* line */
  320. success = read_line(L, f);
  321. break;
  322. case 'a': /* file */
  323. read_file(L, f);
  324. success = 1; /* always success */
  325. break;
  326. case 'w': /* word */
  327. read_word(L, f);
  328. success = 0; /* must read something to succeed */
  329. break;
  330. default:
  331. luaL_argerror(L, n, "invalid format");
  332. success = 0; /* to avoid warnings */
  333. }
  334. }
  335. }
  336. l = luaL_getsize(L);
  337. if (!success && l==0) break; /* read fails */
  338. lua_pushlstring(L, luaL_buffer(L), l);
  339. } endloop:
  340. return n - firstarg;
  341. }
  342. /* }====================================================== */
  343. static int io_write (lua_State *L) {
  344. int lastarg = lua_gettop(L) - 1;
  345. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  346. int arg = 1;
  347. int status = 1;
  348. FILE *f = gethandle(L, ctrl, arg);
  349. if (f) arg++;
  350. else f = getfilebyref(L, ctrl, OUTFILE); /* get _OUTPUT */
  351. for (; arg <= lastarg; arg++) {
  352. if (lua_type(L, arg)[2] == 'm') { /* nuMber? */ /* LUA_NUMBER */
  353. /* optimization: could be done exactly as for strings */
  354. status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0;
  355. }
  356. else {
  357. size_t l;
  358. const char *s = luaL_check_lstr(L, arg, &l);
  359. status = status && (fwrite(s, sizeof(char), l, f) == l);
  360. }
  361. }
  362. pushresult(L, status);
  363. return 1;
  364. }
  365. static int io_seek (lua_State *L) {
  366. static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  367. static const char *const modenames[] = {"set", "cur", "end", NULL};
  368. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  369. FILE *f;
  370. int op;
  371. long offset;
  372. lua_pop(L, 1); /* remove upvalue */
  373. f = getnonullfile(L, ctrl, 1);
  374. op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
  375. offset = luaL_opt_long(L, 3, 0);
  376. luaL_arg_check(L, op != -1, 2, "invalid mode");
  377. op = fseek(f, offset, mode[op]);
  378. if (op)
  379. return pushresult(L, 0); /* error */
  380. else {
  381. lua_pushnumber(L, ftell(f));
  382. return 1;
  383. }
  384. }
  385. static int io_flush (lua_State *L) {
  386. IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  387. FILE *f;
  388. lua_pop(L, 1); /* remove upvalue */
  389. f = gethandle(L, ctrl, 1);
  390. luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle");
  391. return pushresult(L, fflush(f) == 0);
  392. }
  393. /* }====================================================== */
  394. /*
  395. ** {======================================================
  396. ** Other O.S. Operations
  397. ** =======================================================
  398. */
  399. static int io_execute (lua_State *L) {
  400. lua_pushnumber(L, system(luaL_check_string(L, 1)));
  401. return 1;
  402. }
  403. static int io_remove (lua_State *L) {
  404. return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
  405. }
  406. static int io_rename (lua_State *L) {
  407. return pushresult(L, rename(luaL_check_string(L, 1),
  408. luaL_check_string(L, 2)) == 0);
  409. }
  410. static int io_tmpname (lua_State *L) {
  411. lua_pushstring(L, tmpnam(NULL));
  412. return 1;
  413. }
  414. static int io_getenv (lua_State *L) {
  415. lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
  416. return 1;
  417. }
  418. static int io_clock (lua_State *L) {
  419. lua_pushnumber(L, ((double)clock())/CLOCKS_PER_SEC);
  420. return 1;
  421. }
  422. static int io_date (lua_State *L) {
  423. char b[256];
  424. const char *s = luaL_opt_string(L, 1, "%c");
  425. struct tm *stm;
  426. time_t t;
  427. time(&t); stm = localtime(&t);
  428. if (strftime(b, sizeof(b), s, stm))
  429. lua_pushstring(L, b);
  430. else
  431. lua_error(L, "invalid `date' format");
  432. return 1;
  433. }
  434. static int setloc (lua_State *L) {
  435. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  436. LC_NUMERIC, LC_TIME};
  437. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  438. "numeric", "time", NULL};
  439. int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
  440. luaL_arg_check(L, op != -1, 2, "invalid option");
  441. lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));
  442. return 1;
  443. }
  444. static int io_exit (lua_State *L) {
  445. exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
  446. return 0; /* to avoid warnings */
  447. }
  448. /* }====================================================== */
  449. static int io_debug (lua_State *L) {
  450. for (;;) {
  451. char buffer[250];
  452. fprintf(stderr, "lua_debug> ");
  453. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  454. strcmp(buffer, "cont\n") == 0)
  455. return 0;
  456. lua_dostring(L, buffer);
  457. lua_settop(L, 0); /* remove eventual returns */
  458. }
  459. }
  460. #define LEVELS1 12 /* size of the first part of the stack */
  461. #define LEVELS2 10 /* size of the second part of the stack */
  462. static int errorfb (lua_State *L) {
  463. int level = 1; /* skip level 0 (it's this function) */
  464. int firstpart = 1; /* still before eventual `...' */
  465. lua_Debug ar;
  466. lua_settop(L, 1);
  467. luaL_checktype(L, 1, "string");
  468. lua_pushstring(L, "error: ");
  469. lua_insert(L, 1);
  470. lua_pushstring(L, "\nstack traceback:\n");
  471. lua_concat(L, 3);
  472. while (lua_getstack(L, level++, &ar)) {
  473. char buff[120]; /* enough to fit following `sprintf's */
  474. char buffchunk[60];
  475. int toconcat = 1; /* number of strings in the stack to concat */
  476. if (level > LEVELS1 && firstpart) {
  477. /* no more than `LEVELS2' more levels? */
  478. if (!lua_getstack(L, level+LEVELS2, &ar))
  479. level--; /* keep going */
  480. else {
  481. lua_pushstring(L, " ...\n"); /* too many levels */
  482. lua_concat(L, 2);
  483. while (lua_getstack(L, level+LEVELS2, &ar)) /* get last levels */
  484. level++;
  485. }
  486. firstpart = 0;
  487. continue;
  488. }
  489. sprintf(buff, "%4d: ", level-1);
  490. lua_pushstring(L, buff); toconcat++;
  491. lua_getinfo(L, "Snl", &ar);
  492. luaL_chunkid(buffchunk, ar.source, sizeof(buffchunk));
  493. switch (*ar.namewhat) {
  494. case 'g': case 'l': /* global, local */
  495. sprintf(buff, "function `%.50s'", ar.name);
  496. break;
  497. case 'f': /* field */
  498. sprintf(buff, "method `%.50s'", ar.name);
  499. break;
  500. case 't': /* tag method */
  501. sprintf(buff, "`%.50s' tag method", ar.name);
  502. break;
  503. default: {
  504. if (*ar.what == 'm') /* main? */
  505. sprintf(buff, "main of %.70s", buffchunk);
  506. else if (*ar.what == 'C') /* C function? */
  507. sprintf(buff, "%.70s", buffchunk);
  508. else
  509. sprintf(buff, "function <%d:%.70s>", ar.linedefined, buffchunk);
  510. ar.source = NULL;
  511. }
  512. }
  513. lua_pushstring(L, buff); toconcat++;
  514. if (ar.currentline > 0) {
  515. sprintf(buff, " at line %d", ar.currentline);
  516. lua_pushstring(L, buff); toconcat++;
  517. }
  518. if (ar.source) {
  519. sprintf(buff, " [%.70s]", buffchunk);
  520. lua_pushstring(L, buff); toconcat++;
  521. }
  522. lua_pushstring(L, "\n"); toconcat++;
  523. lua_concat(L, toconcat);
  524. }
  525. lua_getglobals(L);
  526. lua_pushstring(L, LUA_ALERT);
  527. lua_rawget(L, -2);
  528. if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */
  529. lua_pushvalue(L, -3); /* error message */
  530. lua_call(L, 1, 0);
  531. }
  532. return 0;
  533. }
  534. static const struct luaL_reg iolib[] = {
  535. {LUA_ERRORMESSAGE, errorfb},
  536. {"clock", io_clock},
  537. {"date", io_date},
  538. {"debug", io_debug},
  539. {"execute", io_execute},
  540. {"exit", io_exit},
  541. {"getenv", io_getenv},
  542. {"remove", io_remove},
  543. {"rename", io_rename},
  544. {"setlocale", setloc},
  545. {"tmpname", io_tmpname}
  546. };
  547. static const struct luaL_reg iolibtag[] = {
  548. {"appendto", io_appendto},
  549. {"closefile", io_close},
  550. {"flush", io_flush},
  551. {"openfile", io_open},
  552. {"read", io_read},
  553. {"readfrom", io_readfrom},
  554. {"seek", io_seek},
  555. {"write", io_write},
  556. {"writeto", io_writeto}
  557. };
  558. static void openwithcontrol (lua_State *L) {
  559. IOCtrl *ctrl = (IOCtrl *)malloc(sizeof(IOCtrl));
  560. unsigned int i;
  561. int ctrltag = lua_newtag(L);
  562. ctrl->iotag = lua_newtag(L);
  563. ctrl->closedtag = lua_newtag(L);
  564. for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
  565. /* put `ctrl' as upvalue for these functions */
  566. lua_pushusertag(L, ctrl, ctrltag);
  567. lua_pushcclosure(L, iolibtag[i].func, 1);
  568. lua_setglobal(L, iolibtag[i].name);
  569. }
  570. /* create references to variable names */
  571. lua_pushstring(L, filenames[INFILE]);
  572. ctrl->ref[INFILE] = lua_ref(L, 1);
  573. lua_pushstring(L, filenames[OUTFILE]);
  574. ctrl->ref[OUTFILE] = lua_ref(L, 1);
  575. /* predefined file handles */
  576. setfile(L, ctrl, stdin, INFILE);
  577. setfile(L, ctrl, stdout, OUTFILE);
  578. setfilebyname(L, ctrl, stdin, "_STDIN");
  579. setfilebyname(L, ctrl, stdout, "_STDOUT");
  580. setfilebyname(L, ctrl, stderr, "_STDERR");
  581. /* delete `ctrl' when collected */
  582. lua_pushusertag(L, ctrl, ctrltag);
  583. lua_pushcclosure(L, file_collect, 1);
  584. lua_settagmethod(L, ctrltag, "gc");
  585. /* close files when collected */
  586. lua_copytagmethods(L, ctrl->iotag, ctrltag);
  587. }
  588. void lua_iolibopen (lua_State *L) {
  589. luaL_openl(L, iolib);
  590. openwithcontrol(L);
  591. }