liolib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. ** $Id: liolib.c,v 2.52 2004/05/28 18:32:51 roberto Exp roberto $
  3. ** Standard I/O (and system) library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <errno.h>
  7. #include <locale.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #define liolib_c
  13. #define LUA_LIB
  14. #include "lua.h"
  15. #include "lauxlib.h"
  16. #include "lualib.h"
  17. /*
  18. ** {======================================================
  19. ** FILE Operations
  20. ** =======================================================
  21. */
  22. #define IO_INPUT 1
  23. #define IO_OUTPUT 2
  24. static int pushresult (lua_State *L, int i, const char *filename) {
  25. if (i) {
  26. lua_pushboolean(L, 1);
  27. return 1;
  28. }
  29. else {
  30. lua_pushnil(L);
  31. if (filename)
  32. lua_pushfstring(L, "%s: %s", filename, strerror(errno));
  33. else
  34. lua_pushfstring(L, "%s", strerror(errno));
  35. lua_pushinteger(L, errno);
  36. return 3;
  37. }
  38. }
  39. static FILE **topfile (lua_State *L, int findex) {
  40. FILE **f = (FILE **)luaL_checkudata(L, findex, LUA_FILEHANDLE);
  41. if (f == NULL) luaL_argerror(L, findex, "bad file");
  42. return f;
  43. }
  44. static int io_type (lua_State *L) {
  45. FILE **f = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE);
  46. if (f == NULL) lua_pushnil(L);
  47. else if (*f == NULL)
  48. lua_pushliteral(L, "closed file");
  49. else
  50. lua_pushliteral(L, "file");
  51. return 1;
  52. }
  53. static FILE *tofile (lua_State *L, int findex) {
  54. FILE **f = topfile(L, findex);
  55. if (*f == NULL)
  56. luaL_error(L, "attempt to use a closed file");
  57. return *f;
  58. }
  59. /*
  60. ** When creating file handles, always creates a `closed' file handle
  61. ** before opening the actual file; so, if there is a memory error, the
  62. ** file is not left opened.
  63. */
  64. static FILE **newfile (lua_State *L) {
  65. FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
  66. *pf = NULL; /* file handle is currently `closed' */
  67. luaL_getmetatable(L, LUA_FILEHANDLE);
  68. lua_setmetatable(L, -2);
  69. return pf;
  70. }
  71. static int aux_close (lua_State *L) {
  72. FILE *f = tofile(L, 1);
  73. if (f == stdin || f == stdout || f == stderr)
  74. return 0; /* file cannot be closed */
  75. else {
  76. int ok = (pclose(f) != -1) || (fclose(f) == 0);
  77. if (ok)
  78. *(FILE **)lua_touserdata(L, 1) = NULL; /* mark file as closed */
  79. return ok;
  80. }
  81. }
  82. static int io_close (lua_State *L) {
  83. if (lua_isnone(L, 1) && lua_type(L, lua_upvalueindex(1)) == LUA_TTABLE)
  84. lua_rawgeti(L, lua_upvalueindex(1), IO_OUTPUT);
  85. return pushresult(L, aux_close(L), NULL);
  86. }
  87. static int io_gc (lua_State *L) {
  88. FILE **f = topfile(L, 1);
  89. if (*f != NULL) /* ignore closed files */
  90. aux_close(L);
  91. return 0;
  92. }
  93. static int io_tostring (lua_State *L) {
  94. char buff[4*sizeof(void *) + 2]; /* enough space for a `%p' */
  95. FILE **f = topfile(L, 1);
  96. if (*f == NULL)
  97. strcpy(buff, "closed");
  98. else
  99. sprintf(buff, "%p", lua_touserdata(L, 1));
  100. lua_pushfstring(L, "file (%s)", buff);
  101. return 1;
  102. }
  103. static int io_open (lua_State *L) {
  104. const char *filename = luaL_checkstring(L, 1);
  105. const char *mode = luaL_optstring(L, 2, "r");
  106. FILE **pf = newfile(L);
  107. *pf = fopen(filename, mode);
  108. return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
  109. }
  110. static int io_tmpfile (lua_State *L) {
  111. FILE **pf = newfile(L);
  112. *pf = tmpfile();
  113. return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
  114. }
  115. static FILE *getiofile (lua_State *L, int f) {
  116. lua_rawgeti(L, lua_upvalueindex(1), f);
  117. return tofile(L, -1);
  118. }
  119. static int g_iofile (lua_State *L, int f, const char *mode) {
  120. if (!lua_isnoneornil(L, 1)) {
  121. const char *filename = lua_tostring(L, 1);
  122. if (filename) {
  123. FILE **pf = newfile(L);
  124. *pf = fopen(filename, mode);
  125. if (*pf == NULL) {
  126. lua_pushfstring(L, "%s: %s", filename, strerror(errno));
  127. luaL_argerror(L, 1, lua_tostring(L, -1));
  128. }
  129. }
  130. else {
  131. tofile(L, 1); /* check that it's a valid file handle */
  132. lua_pushvalue(L, 1);
  133. }
  134. lua_rawseti(L, lua_upvalueindex(1), f);
  135. }
  136. /* return current value */
  137. lua_rawgeti(L, lua_upvalueindex(1), f);
  138. return 1;
  139. }
  140. static int io_input (lua_State *L) {
  141. return g_iofile(L, IO_INPUT, "r");
  142. }
  143. static int io_output (lua_State *L) {
  144. return g_iofile(L, IO_OUTPUT, "w");
  145. }
  146. static int io_readline (lua_State *L);
  147. static void aux_lines (lua_State *L, int idx, int close) {
  148. lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
  149. lua_pushvalue(L, idx);
  150. lua_pushboolean(L, close); /* close/not close file when finished */
  151. lua_pushcclosure(L, io_readline, 3);
  152. }
  153. static int f_lines (lua_State *L) {
  154. tofile(L, 1); /* check that it's a valid file handle */
  155. aux_lines(L, 1, 0);
  156. return 1;
  157. }
  158. static int io_lines (lua_State *L) {
  159. if (lua_isnoneornil(L, 1)) { /* no arguments? */
  160. /* will iterate over default input */
  161. lua_rawgeti(L, lua_upvalueindex(1), IO_INPUT);
  162. return f_lines(L);
  163. }
  164. else {
  165. const char *filename = luaL_checkstring(L, 1);
  166. FILE **pf = newfile(L);
  167. *pf = fopen(filename, "r");
  168. luaL_argcheck(L, *pf, 1, strerror(errno));
  169. aux_lines(L, lua_gettop(L), 1);
  170. return 1;
  171. }
  172. }
  173. /*
  174. ** {======================================================
  175. ** READ
  176. ** =======================================================
  177. */
  178. static int read_number (lua_State *L, FILE *f) {
  179. lua_Number d;
  180. if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
  181. lua_pushnumber(L, d);
  182. return 1;
  183. }
  184. else return 0; /* read fails */
  185. }
  186. static int test_eof (lua_State *L, FILE *f) {
  187. int c = getc(f);
  188. ungetc(c, f);
  189. lua_pushlstring(L, NULL, 0);
  190. return (c != EOF);
  191. }
  192. static int read_line (lua_State *L, FILE *f) {
  193. luaL_Buffer b;
  194. luaL_buffinit(L, &b);
  195. for (;;) {
  196. size_t l;
  197. char *p = luaL_prepbuffer(&b);
  198. if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
  199. luaL_pushresult(&b); /* close buffer */
  200. return (lua_strlen(L, -1) > 0); /* check whether read something */
  201. }
  202. l = strlen(p);
  203. if (p[l-1] != '\n')
  204. luaL_addsize(&b, l);
  205. else {
  206. luaL_addsize(&b, l - 1); /* do not include `eol' */
  207. luaL_pushresult(&b); /* close buffer */
  208. return 1; /* read at least an `eol' */
  209. }
  210. }
  211. }
  212. static int read_chars (lua_State *L, FILE *f, size_t n) {
  213. size_t rlen; /* how much to read */
  214. size_t nr; /* number of chars actually read */
  215. luaL_Buffer b;
  216. luaL_buffinit(L, &b);
  217. rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
  218. do {
  219. char *p = luaL_prepbuffer(&b);
  220. if (rlen > n) rlen = n; /* cannot read more than asked */
  221. nr = fread(p, sizeof(char), rlen, f);
  222. luaL_addsize(&b, nr);
  223. n -= nr; /* still have to read `n' chars */
  224. } while (n > 0 && nr == rlen); /* until end of count or eof */
  225. luaL_pushresult(&b); /* close buffer */
  226. return (n == 0 || lua_strlen(L, -1) > 0);
  227. }
  228. static int g_read (lua_State *L, FILE *f, int first) {
  229. int nargs = lua_gettop(L) - 1;
  230. int success;
  231. int n;
  232. if (nargs == 0) { /* no arguments? */
  233. success = read_line(L, f);
  234. n = first+1; /* to return 1 result */
  235. }
  236. else { /* ensure stack space for all results and for auxlib's buffer */
  237. luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
  238. success = 1;
  239. for (n = first; nargs-- && success; n++) {
  240. if (lua_type(L, n) == LUA_TNUMBER) {
  241. size_t l = (size_t)lua_tointeger(L, n);
  242. success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
  243. }
  244. else {
  245. const char *p = lua_tostring(L, n);
  246. luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
  247. switch (p[1]) {
  248. case 'n': /* number */
  249. success = read_number(L, f);
  250. break;
  251. case 'l': /* line */
  252. success = read_line(L, f);
  253. break;
  254. case 'a': /* file */
  255. read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
  256. success = 1; /* always success */
  257. break;
  258. case 'w': /* word */
  259. return luaL_error(L, "obsolete option `*w' to `read'");
  260. default:
  261. return luaL_argerror(L, n, "invalid format");
  262. }
  263. }
  264. }
  265. }
  266. if (!success) {
  267. lua_pop(L, 1); /* remove last result */
  268. lua_pushnil(L); /* push nil instead */
  269. }
  270. return n - first;
  271. }
  272. static int io_read (lua_State *L) {
  273. return g_read(L, getiofile(L, IO_INPUT), 1);
  274. }
  275. static int f_read (lua_State *L) {
  276. return g_read(L, tofile(L, 1), 2);
  277. }
  278. static int io_readline (lua_State *L) {
  279. FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(2));
  280. if (f == NULL) /* file is already closed? */
  281. luaL_error(L, "file is already closed");
  282. if (read_line(L, f)) return 1;
  283. else { /* EOF */
  284. if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
  285. lua_settop(L, 0);
  286. lua_pushvalue(L, lua_upvalueindex(2));
  287. aux_close(L); /* close it */
  288. }
  289. return 0;
  290. }
  291. }
  292. /* }====================================================== */
  293. static int g_write (lua_State *L, FILE *f, int arg) {
  294. int nargs = lua_gettop(L) - 1;
  295. int status = 1;
  296. for (; nargs--; arg++) {
  297. if (lua_type(L, arg) == LUA_TNUMBER) {
  298. /* optimization: could be done exactly as for strings */
  299. status = status &&
  300. fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
  301. }
  302. else {
  303. size_t l;
  304. const char *s = luaL_checklstring(L, arg, &l);
  305. status = status && (fwrite(s, sizeof(char), l, f) == l);
  306. }
  307. }
  308. return pushresult(L, status, NULL);
  309. }
  310. static int io_write (lua_State *L) {
  311. return g_write(L, getiofile(L, IO_OUTPUT), 1);
  312. }
  313. static int f_write (lua_State *L) {
  314. return g_write(L, tofile(L, 1), 2);
  315. }
  316. static int f_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 = tofile(L, 1);
  320. int op = luaL_findstring(luaL_optstring(L, 2, "cur"), modenames);
  321. lua_Integer offset = luaL_optinteger(L, 3, 0);
  322. luaL_argcheck(L, op != -1, 2, "invalid mode");
  323. op = fseek(f, offset, mode[op]);
  324. if (op)
  325. return pushresult(L, 0, NULL); /* error */
  326. else {
  327. lua_pushinteger(L, ftell(f));
  328. return 1;
  329. }
  330. }
  331. static int f_setvbuf (lua_State *L) {
  332. static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
  333. static const char *const modenames[] = {"no", "full", "line", NULL};
  334. FILE *f = tofile(L, 1);
  335. int op = luaL_findstring(luaL_checkstring(L, 2), modenames);
  336. luaL_argcheck(L, op != -1, 2, "invalid mode");
  337. return pushresult(L, setvbuf(f, NULL, mode[op], 0) == 0, NULL);
  338. }
  339. static int io_flush (lua_State *L) {
  340. return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
  341. }
  342. static int f_flush (lua_State *L) {
  343. return pushresult(L, fflush(tofile(L, 1)) == 0, NULL);
  344. }
  345. static const luaL_reg iolib[] = {
  346. {"input", io_input},
  347. {"output", io_output},
  348. {"lines", io_lines},
  349. {"close", io_close},
  350. {"flush", io_flush},
  351. {"open", io_open},
  352. {"read", io_read},
  353. {"tmpfile", io_tmpfile},
  354. {"type", io_type},
  355. {"write", io_write},
  356. {NULL, NULL}
  357. };
  358. static const luaL_reg flib[] = {
  359. {"flush", f_flush},
  360. {"read", f_read},
  361. {"lines", f_lines},
  362. {"seek", f_seek},
  363. {"setvbuf", f_setvbuf},
  364. {"write", f_write},
  365. {"close", io_close},
  366. {"__gc", io_gc},
  367. {"__tostring", io_tostring},
  368. {NULL, NULL}
  369. };
  370. static void createmeta (lua_State *L) {
  371. luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
  372. /* create (and set) default files */
  373. *newfile(L) = stdin;
  374. lua_rawseti(L, -2, IO_INPUT);
  375. *newfile(L) = stdout;
  376. lua_rawseti(L, -2, IO_OUTPUT);
  377. /* file methods */
  378. lua_pushvalue(L, -1); /* push metatable */
  379. lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
  380. luaL_openlib(L, NULL, flib, 0);
  381. }
  382. /* }====================================================== */
  383. /*
  384. ** {======================================================
  385. ** Other O.S. Operations
  386. ** =======================================================
  387. */
  388. static int io_execute (lua_State *L) {
  389. lua_pushinteger(L, system(luaL_checkstring(L, 1)));
  390. return 1;
  391. }
  392. static int io_remove (lua_State *L) {
  393. const char *filename = luaL_checkstring(L, 1);
  394. return pushresult(L, remove(filename) == 0, filename);
  395. }
  396. static int io_rename (lua_State *L) {
  397. const char *fromname = luaL_checkstring(L, 1);
  398. const char *toname = luaL_checkstring(L, 2);
  399. return pushresult(L, rename(fromname, toname) == 0, fromname);
  400. }
  401. static int io_tmpname (lua_State *L) {
  402. #if !USE_TMPNAME
  403. luaL_error(L, "`tmpname' not supported");
  404. return 0;
  405. #else
  406. char buff[L_tmpnam];
  407. if (tmpnam(buff) != buff)
  408. return luaL_error(L, "unable to generate a unique filename in `tmpname'");
  409. lua_pushstring(L, buff);
  410. return 1;
  411. #endif
  412. }
  413. static int io_getenv (lua_State *L) {
  414. lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
  415. return 1;
  416. }
  417. static int io_clock (lua_State *L) {
  418. lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
  419. return 1;
  420. }
  421. /*
  422. ** {======================================================
  423. ** Time/Date operations
  424. ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
  425. ** wday=%w+1, yday=%j, isdst=? }
  426. ** =======================================================
  427. */
  428. static void setfield (lua_State *L, const char *key, int value) {
  429. lua_pushstring(L, key);
  430. lua_pushinteger(L, value);
  431. lua_rawset(L, -3);
  432. }
  433. static void setboolfield (lua_State *L, const char *key, int value) {
  434. lua_pushstring(L, key);
  435. lua_pushboolean(L, value);
  436. lua_rawset(L, -3);
  437. }
  438. static int getboolfield (lua_State *L, const char *key) {
  439. int res;
  440. lua_getfield(L, -1, key);
  441. res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
  442. lua_pop(L, 1);
  443. return res;
  444. }
  445. static int getfield (lua_State *L, const char *key, int d) {
  446. int res;
  447. lua_getfield(L, -1, key);
  448. if (lua_isnumber(L, -1))
  449. res = (int)lua_tointeger(L, -1);
  450. else {
  451. if (d < 0)
  452. return luaL_error(L, "field `%s' missing in date table", key);
  453. res = d;
  454. }
  455. lua_pop(L, 1);
  456. return res;
  457. }
  458. static int io_date (lua_State *L) {
  459. const char *s = luaL_optstring(L, 1, "%c");
  460. lua_Number n = luaL_optnumber(L, 2, -1);
  461. time_t t = (n == -1) ? time(NULL) : (time_t)n;
  462. struct tm *stm;
  463. if (*s == '!') { /* UTC? */
  464. stm = gmtime(&t);
  465. s++; /* skip `!' */
  466. }
  467. else
  468. stm = localtime(&t);
  469. if (stm == NULL) /* invalid date? */
  470. lua_pushnil(L);
  471. else if (strcmp(s, "*t") == 0) {
  472. lua_createtable(L, 0, 9); /* 9 = number of fields */
  473. setfield(L, "sec", stm->tm_sec);
  474. setfield(L, "min", stm->tm_min);
  475. setfield(L, "hour", stm->tm_hour);
  476. setfield(L, "day", stm->tm_mday);
  477. setfield(L, "month", stm->tm_mon+1);
  478. setfield(L, "year", stm->tm_year+1900);
  479. setfield(L, "wday", stm->tm_wday+1);
  480. setfield(L, "yday", stm->tm_yday+1);
  481. setboolfield(L, "isdst", stm->tm_isdst);
  482. }
  483. else {
  484. char b[256];
  485. if (strftime(b, sizeof(b), s, stm))
  486. lua_pushstring(L, b);
  487. else
  488. return luaL_error(L, "`date' format too long");
  489. }
  490. return 1;
  491. }
  492. static int io_time (lua_State *L) {
  493. if (lua_isnoneornil(L, 1)) /* called without args? */
  494. lua_pushnumber(L, time(NULL)); /* return current time */
  495. else {
  496. time_t t;
  497. struct tm ts;
  498. luaL_checktype(L, 1, LUA_TTABLE);
  499. lua_settop(L, 1); /* make sure table is at the top */
  500. ts.tm_sec = getfield(L, "sec", 0);
  501. ts.tm_min = getfield(L, "min", 0);
  502. ts.tm_hour = getfield(L, "hour", 12);
  503. ts.tm_mday = getfield(L, "day", -1);
  504. ts.tm_mon = getfield(L, "month", -1) - 1;
  505. ts.tm_year = getfield(L, "year", -1) - 1900;
  506. ts.tm_isdst = getboolfield(L, "isdst");
  507. t = mktime(&ts);
  508. if (t == (time_t)(-1))
  509. lua_pushnil(L);
  510. else
  511. lua_pushnumber(L, t);
  512. }
  513. return 1;
  514. }
  515. static int io_difftime (lua_State *L) {
  516. lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
  517. (time_t)(luaL_optnumber(L, 2, 0))));
  518. return 1;
  519. }
  520. /* }====================================================== */
  521. static int io_setloc (lua_State *L) {
  522. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  523. LC_NUMERIC, LC_TIME};
  524. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  525. "numeric", "time", NULL};
  526. const char *l = lua_tostring(L, 1);
  527. int op = luaL_findstring(luaL_optstring(L, 2, "all"), catnames);
  528. luaL_argcheck(L, l || lua_isnoneornil(L, 1), 1, "string expected");
  529. luaL_argcheck(L, op != -1, 2, "invalid option");
  530. lua_pushstring(L, setlocale(cat[op], l));
  531. return 1;
  532. }
  533. static int io_exit (lua_State *L) {
  534. exit(luaL_optint(L, 1, EXIT_SUCCESS));
  535. return 0; /* to avoid warnings */
  536. }
  537. static const luaL_reg syslib[] = {
  538. {"clock", io_clock},
  539. {"date", io_date},
  540. {"difftime", io_difftime},
  541. {"execute", io_execute},
  542. {"exit", io_exit},
  543. {"getenv", io_getenv},
  544. {"remove", io_remove},
  545. {"rename", io_rename},
  546. {"setlocale", io_setloc},
  547. {"time", io_time},
  548. {"tmpname", io_tmpname},
  549. {NULL, NULL}
  550. };
  551. /* }====================================================== */
  552. LUALIB_API int luaopen_io (lua_State *L) {
  553. luaL_openlib(L, LUA_OSLIBNAME, syslib, 0);
  554. createmeta(L);
  555. lua_pushvalue(L, -1);
  556. luaL_openlib(L, LUA_IOLIBNAME, iolib, 1);
  557. /* put predefined file handles into `io' table */
  558. lua_pushliteral(L, "stdin");
  559. lua_rawgeti(L, 2, IO_INPUT);
  560. lua_rawset(L, 3);
  561. lua_pushliteral(L, "stdout");
  562. lua_rawgeti(L, 2, IO_OUTPUT);
  563. lua_rawset(L, 3);
  564. lua_pushliteral(L, "stderr");
  565. *newfile(L) = stderr;
  566. lua_rawset(L, 3);
  567. return 1;
  568. }