liolib.c 19 KB

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