liolib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. ** $Id: liolib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  3. ** Standard I/O (and system) library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include "lua.h"
  11. #include "lauxlib.h"
  12. #include "luadebug.h"
  13. #include "lualib.h"
  14. #ifndef OLD_ANSI
  15. #include <errno.h>
  16. #include <locale.h>
  17. #endif
  18. #ifdef POPEN
  19. /* FILE *popen();
  20. int pclose(); */
  21. #define CLOSEFILE(L, f) ((pclose(f) == -1) ? fclose(f) : 0)
  22. #else
  23. /* no support for popen */
  24. #define popen(x,y) NULL /* that is, popen always fails */
  25. #define CLOSEFILE(L, f) (fclose(f))
  26. #endif
  27. #define INFILE 0
  28. #define OUTFILE 1
  29. #define NOFILE 2
  30. #define FILEHANDLE "FileHandle"
  31. #define CLOSEDFILEHANDLE "ClosedFileHandle"
  32. static const char *const filenames[] = {"_INPUT", "_OUTPUT"};
  33. static const char *const basicfiles[] = {"_STDIN", "_STDOUT"};
  34. static int pushresult (lua_State *L, int i) {
  35. if (i) {
  36. lua_pushnumber(L, 1);
  37. return 1;
  38. }
  39. else {
  40. lua_pushnil(L);
  41. lua_pushstring(L, strerror(errno));
  42. lua_pushnumber(L, errno);
  43. return 3;
  44. }
  45. }
  46. /*
  47. ** {======================================================
  48. ** FILE Operations
  49. ** =======================================================
  50. */
  51. static int checkfile (lua_State *L, int findex, const char *tname) {
  52. int res;
  53. lua_getmetatable(L, findex);
  54. lua_getstr(L, LUA_REGISTRYINDEX, tname);
  55. res = lua_equal(L, -1, -2);
  56. lua_pop(L, 2);
  57. return res;
  58. }
  59. /* temporary?? should be in auxlib... */
  60. static void *luaL_check_userdata (lua_State *L, int findex, const char *tn) {
  61. luaL_arg_check(L, checkfile(L, findex, tn), findex, "bad file");
  62. return lua_touserdata(L, findex);
  63. }
  64. static FILE *getopthandle (lua_State *L, int inout) {
  65. FILE *p = (FILE *)(lua_touserdata(L, 1));
  66. if (p != NULL) { /* is it a userdata ? */
  67. if (!checkfile(L, 1, FILEHANDLE)) { /* not a valid file handle? */
  68. if (checkfile(L, 1, CLOSEDFILEHANDLE))
  69. luaL_argerror(L, 1, "file is closed");
  70. else
  71. luaL_argerror(L, 1, "(invalid value)");
  72. }
  73. lua_pushvalue(L, 1); lua_remove(L, 1); /* move it to stack top */
  74. }
  75. else { /* try global value */
  76. lua_getglobal(L, filenames[inout]);
  77. if (!checkfile(L, -1, FILEHANDLE))
  78. luaL_verror(L, "global variable `%.10s' is not a valid file handle",
  79. filenames[inout]);
  80. p = (FILE *)(lua_touserdata(L, -1));
  81. }
  82. return p; /* leave handle at stack top to avoid GC */
  83. }
  84. static void newfile (lua_State *L, FILE *f) {
  85. lua_newuserdatabox(L, f);
  86. lua_getstr(L, LUA_REGISTRYINDEX, FILEHANDLE);
  87. lua_setmetatable(L, -2);
  88. }
  89. static void newfilewithname (lua_State *L, FILE *f, const char *name) {
  90. newfile(L, f);
  91. lua_setglobal(L, name);
  92. }
  93. static int setnewfile (lua_State *L, FILE *f, int inout) {
  94. if (f == NULL)
  95. return pushresult(L, 0);
  96. else {
  97. newfile(L, f);
  98. if (inout != NOFILE) {
  99. lua_pushvalue(L, -1);
  100. lua_setglobal(L, filenames[inout]);
  101. }
  102. return 1;
  103. }
  104. }
  105. static void resetfile (lua_State *L, int inout) {
  106. lua_getglobal(L, basicfiles[inout]);
  107. lua_setglobal(L, filenames[inout]);
  108. }
  109. static int io_close (lua_State *L) {
  110. FILE *f = (FILE *)(luaL_check_userdata(L, 1, FILEHANDLE));
  111. int status = 1;
  112. if (f != stdin && f != stdout && f != stderr) {
  113. lua_settop(L, 1); /* make sure file is on top */
  114. lua_getstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE);
  115. lua_setmetatable(L, 1);
  116. status = (CLOSEFILE(L, f) == 0);
  117. }
  118. return pushresult(L, status);
  119. }
  120. static int file_collect (lua_State *L) {
  121. FILE *f = (FILE *)(luaL_check_userdata(L, 1, FILEHANDLE));
  122. if (f != stdin && f != stdout && f != stderr)
  123. CLOSEFILE(L, f);
  124. return 0;
  125. }
  126. static int io_open (lua_State *L) {
  127. FILE *f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2));
  128. return setnewfile(L, f, NOFILE);
  129. }
  130. static int io_tmpfile (lua_State *L) {
  131. return setnewfile(L, tmpfile(), NOFILE);
  132. }
  133. static int io_fromto (lua_State *L, int inout, const char *mode) {
  134. FILE *current;
  135. if (lua_isnone(L, 1)) {
  136. getopthandle(L, inout);
  137. resetfile(L, inout);
  138. return io_close(L);
  139. }
  140. else {
  141. const char *s = luaL_check_string(L, 1);
  142. current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode);
  143. return setnewfile(L, current, inout);
  144. }
  145. }
  146. static int io_readfrom (lua_State *L) {
  147. return io_fromto(L, INFILE, "r");
  148. }
  149. static int io_writeto (lua_State *L) {
  150. return io_fromto(L, OUTFILE, "w");
  151. }
  152. static int io_appendto (lua_State *L) {
  153. FILE *current = fopen(luaL_check_string(L, 1), "a");
  154. return setnewfile(L, current, OUTFILE);
  155. }
  156. /*
  157. ** {======================================================
  158. ** READ
  159. ** =======================================================
  160. */
  161. #ifndef LUA_MAXUNTIL
  162. #define LUA_MAXUNTIL 100
  163. #endif
  164. /*
  165. ** Knuth-Morris-Pratt algorithm for string searching
  166. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  167. ** Addison-Wesley, 1993.)
  168. */
  169. static void prep_read_until (int next[], const char *p, int pl) {
  170. int i = 0;
  171. int j = -1;
  172. next[0] = -1;
  173. while (i < pl) {
  174. if (j == -1 || p[i] == p[j]) {
  175. i++; j++; next[i] = j;
  176. }
  177. else j = next[j];
  178. }
  179. }
  180. static int read_until (lua_State *L, FILE *f, const char *p, int pl) {
  181. int c;
  182. int j;
  183. int next[LUA_MAXUNTIL+1];
  184. luaL_Buffer b;
  185. luaL_buffinit(L, &b);
  186. prep_read_until(next, p, pl);
  187. j = 0;
  188. while ((c = getc(f)) != EOF) {
  189. NoRead:
  190. if (c == p[j]) {
  191. j++; /* go to next char in pattern */
  192. if (j == pl) { /* complete match? */
  193. luaL_pushresult(&b); /* close buffer */
  194. return 1; /* always success */
  195. }
  196. }
  197. else if (j == 0)
  198. luaL_putchar(&b, c);
  199. else { /* match fail */
  200. luaL_addlstring(&b, p, j - next[j]); /* put failed part on result */
  201. j = next[j]; /* backtrack pattern index */
  202. goto NoRead; /* repeat without reading next char */
  203. }
  204. }
  205. /* end of file without a match */
  206. luaL_addlstring(&b, p, j); /* put failed part on result */
  207. luaL_pushresult(&b); /* close buffer */
  208. return (lua_strlen(L, -1) > 0);
  209. }
  210. static int read_number (lua_State *L, FILE *f) {
  211. lua_Number d;
  212. if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
  213. lua_pushnumber(L, d);
  214. return 1;
  215. }
  216. else return 0; /* read fails */
  217. }
  218. static int test_eof (lua_State *L, FILE *f) {
  219. int c = getc(f);
  220. ungetc(c, f);
  221. lua_pushlstring(L, NULL, 0);
  222. return (c != EOF);
  223. }
  224. static int read_chars (lua_State *L, FILE *f, size_t n) {
  225. size_t rlen; /* how much to read */
  226. size_t nr; /* number of chars actually read */
  227. luaL_Buffer b;
  228. luaL_buffinit(L, &b);
  229. rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
  230. do {
  231. char *p = luaL_prepbuffer(&b);
  232. if (rlen > n) rlen = n; /* cannot read more than asked */
  233. nr = fread(p, sizeof(char), rlen, f);
  234. luaL_addsize(&b, nr);
  235. n -= nr; /* still have to read `n' chars */
  236. } while (n > 0 && nr == rlen); /* until end of count or eof */
  237. luaL_pushresult(&b); /* close buffer */
  238. return (n == 0 || lua_strlen(L, -1) > 0);
  239. }
  240. static int io_read (lua_State *L) {
  241. FILE *f = getopthandle(L, INFILE);
  242. int nargs = lua_gettop(L) - 1;
  243. int success;
  244. int n;
  245. if (nargs == 0) { /* no arguments? */
  246. success = read_until(L, f, "\n", 1); /* read until \n (a line) */
  247. n = 2; /* will return n-1 results */
  248. }
  249. else { /* ensure stack space for all results and for auxlib's buffer */
  250. luaL_check_stack(L, nargs+LUA_MINSTACK, "too many arguments");
  251. success = 1;
  252. for (n = 1; n<=nargs && success; n++) {
  253. if (lua_type(L, n) == LUA_TNUMBER) {
  254. size_t l = (size_t)lua_tonumber(L, n);
  255. success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
  256. }
  257. else {
  258. const char *p = lua_tostring(L, n);
  259. if (!p || p[0] != '*')
  260. lua_error(L, "invalid `read' option");
  261. switch (p[1]) {
  262. case 'n': /* number */
  263. success = read_number(L, f);
  264. break;
  265. case 'l': /* line */
  266. success = read_until(L, f, "\n", 1); /* read until \n */
  267. break;
  268. case 'a': /* file */
  269. read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
  270. success = 1; /* always success */
  271. break;
  272. case 'w': /* word */
  273. lua_error(L, "obsolete option `*w'");
  274. break;
  275. case 'u': { /* read until */
  276. size_t pl = lua_strlen(L, n) - 2;
  277. luaL_arg_check(L, 0 < pl && pl <= LUA_MAXUNTIL, n,
  278. "invalid read-until length");
  279. success = read_until(L, f, p+2, (int)(pl));
  280. break;
  281. }
  282. default:
  283. luaL_argerror(L, n, "invalid format");
  284. success = 0; /* to avoid warnings */
  285. }
  286. }
  287. }
  288. }
  289. if (!success) {
  290. lua_pop(L, 1); /* remove last result */
  291. lua_pushnil(L); /* push nil instead */
  292. }
  293. return n - 1;
  294. }
  295. /* }====================================================== */
  296. static int io_write (lua_State *L) {
  297. FILE *f = getopthandle(L, OUTFILE);
  298. int nargs = lua_gettop(L)-1;
  299. int arg;
  300. int status = 1;
  301. for (arg=1; arg<=nargs; arg++) {
  302. if (lua_type(L, arg) == LUA_TNUMBER) {
  303. /* optimization: could be done exactly as for strings */
  304. status = status &&
  305. fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
  306. }
  307. else {
  308. size_t l;
  309. const char *s = luaL_check_lstr(L, arg, &l);
  310. status = status && (fwrite(s, sizeof(char), l, f) == l);
  311. }
  312. }
  313. pushresult(L, status);
  314. return 1;
  315. }
  316. static int io_seek (lua_State *L) {
  317. static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  318. static const char *const modenames[] = {"set", "cur", "end", NULL};
  319. FILE *f = (FILE *)(luaL_check_userdata(L, 1, FILEHANDLE));
  320. int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
  321. long offset = luaL_opt_long(L, 3, 0);
  322. luaL_arg_check(L, op != -1, 2, "invalid mode");
  323. op = fseek(f, offset, mode[op]);
  324. if (op)
  325. return pushresult(L, 0); /* error */
  326. else {
  327. lua_pushnumber(L, ftell(f));
  328. return 1;
  329. }
  330. }
  331. static int io_flush (lua_State *L) {
  332. FILE *f = (lua_isnone(L, 1)) ? (FILE *)(NULL) :
  333. (FILE *)(luaL_check_userdata(L, 1, FILEHANDLE));
  334. return pushresult(L, fflush(f) == 0);
  335. }
  336. /* }====================================================== */
  337. /*
  338. ** {======================================================
  339. ** Other O.S. Operations
  340. ** =======================================================
  341. */
  342. static int io_execute (lua_State *L) {
  343. lua_pushnumber(L, system(luaL_check_string(L, 1)));
  344. return 1;
  345. }
  346. static int io_remove (lua_State *L) {
  347. return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
  348. }
  349. static int io_rename (lua_State *L) {
  350. return pushresult(L, rename(luaL_check_string(L, 1),
  351. luaL_check_string(L, 2)) == 0);
  352. }
  353. static int io_tmpname (lua_State *L) {
  354. char buff[L_tmpnam];
  355. if (tmpnam(buff) != buff)
  356. lua_error(L, "unable to generate a unique filename");
  357. lua_pushstring(L, buff);
  358. return 1;
  359. }
  360. static int io_getenv (lua_State *L) {
  361. lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
  362. return 1;
  363. }
  364. static int io_clock (lua_State *L) {
  365. lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
  366. return 1;
  367. }
  368. /*
  369. ** {======================================================
  370. ** Time/Date operations
  371. ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
  372. ** wday=%w+1, yday=%j, isdst=? }
  373. ** =======================================================
  374. */
  375. static void setfield (lua_State *L, const char *key, int value) {
  376. lua_pushnumber(L, value);
  377. lua_setstr(L, -2, key);
  378. }
  379. static int getfield (lua_State *L, const char *key, int d) {
  380. int res;
  381. lua_getstr(L, -1, key);
  382. if (lua_isnumber(L, -1))
  383. res = (int)(lua_tonumber(L, -1));
  384. else {
  385. if (d == -2)
  386. luaL_verror(L, "field `%.20s' missing in date table", key);
  387. res = d;
  388. }
  389. lua_pop(L, 1);
  390. return res;
  391. }
  392. static int io_date (lua_State *L) {
  393. const char *s = luaL_opt_string(L, 1, "%c");
  394. time_t t = (time_t)(luaL_opt_number(L, 2, -1));
  395. struct tm *stm;
  396. if (t == (time_t)(-1)) /* no time given? */
  397. t = time(NULL); /* use current time */
  398. if (*s == '!') { /* UTC? */
  399. stm = gmtime(&t);
  400. s++; /* skip `!' */
  401. }
  402. else
  403. stm = localtime(&t);
  404. if (stm == NULL) /* invalid date? */
  405. lua_pushnil(L);
  406. else if (strcmp(s, "*t") == 0) {
  407. lua_newtable(L);
  408. setfield(L, "sec", stm->tm_sec);
  409. setfield(L, "min", stm->tm_min);
  410. setfield(L, "hour", stm->tm_hour);
  411. setfield(L, "day", stm->tm_mday);
  412. setfield(L, "month", stm->tm_mon+1);
  413. setfield(L, "year", stm->tm_year+1900);
  414. setfield(L, "wday", stm->tm_wday+1);
  415. setfield(L, "yday", stm->tm_yday+1);
  416. setfield(L, "isdst", stm->tm_isdst);
  417. }
  418. else {
  419. char b[256];
  420. if (strftime(b, sizeof(b), s, stm))
  421. lua_pushstring(L, b);
  422. else
  423. lua_error(L, "invalid `date' format");
  424. }
  425. return 1;
  426. }
  427. static int io_time (lua_State *L) {
  428. if (lua_isnone(L, 1)) /* called without args? */
  429. lua_pushnumber(L, time(NULL)); /* return current time */
  430. else {
  431. time_t t;
  432. struct tm ts;
  433. luaL_check_type(L, 1, LUA_TTABLE);
  434. lua_settop(L, 1); /* make sure table is at the top */
  435. ts.tm_sec = getfield(L, "sec", 0);
  436. ts.tm_min = getfield(L, "min", 0);
  437. ts.tm_hour = getfield(L, "hour", 12);
  438. ts.tm_mday = getfield(L, "day", -2);
  439. ts.tm_mon = getfield(L, "month", -2)-1;
  440. ts.tm_year = getfield(L, "year", -2)-1900;
  441. ts.tm_isdst = getfield(L, "isdst", -1);
  442. t = mktime(&ts);
  443. if (t == (time_t)(-1))
  444. lua_pushnil(L);
  445. else
  446. lua_pushnumber(L, t);
  447. }
  448. return 1;
  449. }
  450. static int io_difftime (lua_State *L) {
  451. lua_pushnumber(L, difftime((time_t)(luaL_check_number(L, 1)),
  452. (time_t)(luaL_opt_number(L, 2, 0))));
  453. return 1;
  454. }
  455. /* }====================================================== */
  456. static int io_setloc (lua_State *L) {
  457. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  458. LC_NUMERIC, LC_TIME};
  459. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  460. "numeric", "time", NULL};
  461. int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
  462. luaL_arg_check(L, op != -1, 2, "invalid option");
  463. lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));
  464. return 1;
  465. }
  466. static int io_exit (lua_State *L) {
  467. exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
  468. return 0; /* to avoid warnings */
  469. }
  470. /* }====================================================== */
  471. static int io_debug (lua_State *L) {
  472. for (;;) {
  473. char buffer[250];
  474. fprintf(stderr, "lua_debug> ");
  475. if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
  476. strcmp(buffer, "cont\n") == 0)
  477. return 0;
  478. lua_dostring(L, buffer);
  479. lua_settop(L, 0); /* remove eventual returns */
  480. }
  481. }
  482. #define LEVELS1 12 /* size of the first part of the stack */
  483. #define LEVELS2 10 /* size of the second part of the stack */
  484. static int errorfb (lua_State *L) {
  485. int level = 1; /* skip level 0 (it's this function) */
  486. int firstpart = 1; /* still before eventual `...' */
  487. lua_Debug ar;
  488. luaL_Buffer b;
  489. luaL_buffinit(L, &b);
  490. luaL_addstring(&b, "error: ");
  491. luaL_addstring(&b, luaL_check_string(L, 1));
  492. luaL_addstring(&b, "\n");
  493. while (lua_getstack(L, level++, &ar)) {
  494. char buff[120]; /* enough to fit following `sprintf's */
  495. if (level == 2)
  496. luaL_addstring(&b, "stack traceback:\n");
  497. else if (level > LEVELS1 && firstpart) {
  498. /* no more than `LEVELS2' more levels? */
  499. if (!lua_getstack(L, level+LEVELS2, &ar))
  500. level--; /* keep going */
  501. else {
  502. luaL_addstring(&b, " ...\n"); /* too many levels */
  503. while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
  504. level++;
  505. }
  506. firstpart = 0;
  507. continue;
  508. }
  509. sprintf(buff, "%4d: ", level-1);
  510. luaL_addstring(&b, buff);
  511. lua_getinfo(L, "Snl", &ar);
  512. switch (*ar.namewhat) {
  513. case 'g': case 'l': /* global, local */
  514. sprintf(buff, "function `%.50s'", ar.name);
  515. break;
  516. case 'f': /* field */
  517. sprintf(buff, "method `%.50s'", ar.name);
  518. break;
  519. case 't': /* tag method */
  520. sprintf(buff, "`%.50s' tag method", ar.name);
  521. break;
  522. default: {
  523. if (*ar.what == 'm') /* main? */
  524. sprintf(buff, "main of %.70s", ar.short_src);
  525. else if (*ar.what == 'C') /* C function? */
  526. sprintf(buff, "%.70s", ar.short_src);
  527. else
  528. sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src);
  529. ar.source = NULL; /* do not print source again */
  530. }
  531. }
  532. luaL_addstring(&b, buff);
  533. if (ar.currentline > 0) {
  534. sprintf(buff, " at line %d", ar.currentline);
  535. luaL_addstring(&b, buff);
  536. }
  537. if (ar.source) {
  538. sprintf(buff, " [%.70s]", ar.short_src);
  539. luaL_addstring(&b, buff);
  540. }
  541. luaL_addstring(&b, "\n");
  542. }
  543. luaL_pushresult(&b);
  544. lua_getglobal(L, LUA_ALERT);
  545. if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */
  546. lua_pushvalue(L, -2); /* error message */
  547. lua_rawcall(L, 1, 0);
  548. }
  549. return 0;
  550. }
  551. static const luaL_reg iolib[] = {
  552. {"appendto", io_appendto},
  553. {"clock", io_clock},
  554. {"closefile", io_close},
  555. {"date", io_date},
  556. {"debug", io_debug},
  557. {"difftime", io_difftime},
  558. {"execute", io_execute},
  559. {"exit", io_exit},
  560. {"flush", io_flush},
  561. {"getenv", io_getenv},
  562. {"openfile", io_open},
  563. {"read", io_read},
  564. {"readfrom", io_readfrom},
  565. {"remove", io_remove},
  566. {"rename", io_rename},
  567. {"seek", io_seek},
  568. {"setlocale", io_setloc},
  569. {"time", io_time},
  570. {"tmpfile", io_tmpfile},
  571. {"tmpname", io_tmpname},
  572. {"write", io_write},
  573. {"writeto", io_writeto},
  574. {LUA_ERRORMESSAGE, errorfb}
  575. };
  576. LUALIB_API int lua_iolibopen (lua_State *L) {
  577. lua_newtable(L); /* meta table for FILEHANDLE */
  578. /* close files when collected */
  579. lua_pushcfunction(L, file_collect);
  580. lua_setstr(L, -2, "gc");
  581. /* put new metatable into registry */
  582. lua_setstr(L, LUA_REGISTRYINDEX, FILEHANDLE);
  583. /* meta table for CLOSEDFILEHANDLE */
  584. lua_newtable(L);
  585. lua_setstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE);
  586. luaL_openl(L, iolib);
  587. /* predefined file handles */
  588. newfilewithname(L, stdin, basicfiles[INFILE]);
  589. newfilewithname(L, stdout, basicfiles[OUTFILE]);
  590. newfilewithname(L, stderr, "_STDERR");
  591. resetfile(L, INFILE);
  592. resetfile(L, OUTFILE);
  593. return 0;
  594. }