liolib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. ** $Id: liolib.c,v 2.73.1.4 2010/05/14 15:33:51 roberto Exp $
  3. ** Standard I/O (and system) library
  4. ** See Copyright Notice in lua.h
  5. */
  6. // #include "c_errno.h"
  7. #include "c_stdio.h"
  8. #include "c_stdlib.h"
  9. #include "c_string.h"
  10. #include "vfs.h"
  11. #define liolib_c
  12. #define LUA_LIB
  13. #include "lua.h"
  14. #include "lauxlib.h"
  15. #include "lualib.h"
  16. #include "lrotable.h"
  17. #define IO_INPUT 1
  18. #define IO_OUTPUT 2
  19. #define IO_STDERR 0
  20. #if LUA_OPTIMIZE_MEMORY != 2
  21. #define LUA_IO_GETFIELD(f) lua_rawgeti(L, LUA_ENVIRONINDEX, f)
  22. #define LUA_IO_SETFIELD(f) lua_rawseti(L, LUA_ENVIRONINDEX, f)
  23. #else
  24. #define LUA_IO_GETFIELD(f) lua_rawgeti(L, LUA_REGISTRYINDEX, liolib_keys[f])
  25. #define LUA_IO_SETFIELD(f) lua_rawseti(L, LUA_REGISTRYINDEX, liolib_keys[f])
  26. /* "Pseudo-random" keys for the registry */
  27. static const int liolib_keys[] = {(int)&luaL_callmeta, (int)&luaL_typerror, (int)&luaL_argerror};
  28. #endif
  29. static const char *const fnames[] = {"input", "output"};
  30. static int pushresult (lua_State *L, int i, const char *filename) {
  31. int en = vfs_ferrno(0); /* calls to Lua API may change this value */
  32. if (i) {
  33. lua_pushboolean(L, 1);
  34. return 1;
  35. }
  36. else {
  37. lua_pushnil(L);
  38. if (filename)
  39. lua_pushfstring(L, "%s: err(%d)", filename, en);
  40. else
  41. lua_pushfstring(L, "err(%d)", en);
  42. lua_pushinteger(L, en);
  43. return 3;
  44. }
  45. }
  46. static void fileerror (lua_State *L, int arg, const char *filename) {
  47. lua_pushfstring(L, "%s: err(%d)", filename, vfs_ferrno(0));
  48. luaL_argerror(L, arg, lua_tostring(L, -1));
  49. }
  50. #define tofilep(L) ((int *)luaL_checkudata(L, 1, LUA_FILEHANDLE))
  51. static int io_type (lua_State *L) {
  52. void *ud;
  53. luaL_checkany(L, 1);
  54. ud = lua_touserdata(L, 1);
  55. lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
  56. if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
  57. lua_pushnil(L); /* not a file */
  58. else if (*((int *)ud) < FS_OPEN_OK)
  59. lua_pushliteral(L, "closed file");
  60. else
  61. lua_pushliteral(L, "file");
  62. return 1;
  63. }
  64. static int tofile (lua_State *L) {
  65. int *f = tofilep(L);
  66. if (*f < FS_OPEN_OK)
  67. luaL_error(L, "attempt to use a closed file");
  68. return *f;
  69. }
  70. /*
  71. ** When creating file handles, always creates a `closed' file handle
  72. ** before opening the actual file; so, if there is a memory error, the
  73. ** file is not left opened.
  74. */
  75. static int *newfile (lua_State *L) {
  76. int *pf = (int *)lua_newuserdata(L, sizeof(int));
  77. *pf = FS_OPEN_OK - 1; /* file handle is currently `closed' */
  78. luaL_getmetatable(L, LUA_FILEHANDLE);
  79. lua_setmetatable(L, -2);
  80. return pf;
  81. }
  82. #if LUA_OPTIMIZE_MEMORY != 2
  83. /*
  84. ** function to (not) close the standard files stdin, stdout, and stderr
  85. */
  86. static int io_noclose (lua_State *L) {
  87. lua_pushnil(L);
  88. lua_pushliteral(L, "cannot close standard file");
  89. return 2;
  90. }
  91. #if 0
  92. /*
  93. ** function to close 'popen' files
  94. */
  95. static int io_pclose (lua_State *L) {
  96. int *p = tofilep(L);
  97. int ok = lua_pclose(L, *p);
  98. *p = FS_OPEN_OK - 1;
  99. return pushresult(L, ok, NULL);
  100. }
  101. #endif
  102. /*
  103. ** function to close regular files
  104. */
  105. static int io_fclose (lua_State *L) {
  106. int *p = tofilep(L);
  107. int ok = (vfs_close(*p) == 0);
  108. *p = FS_OPEN_OK - 1;
  109. return pushresult(L, ok, NULL);
  110. }
  111. #endif
  112. static int aux_close (lua_State *L) {
  113. #if LUA_OPTIMIZE_MEMORY != 2
  114. lua_getfenv(L, 1);
  115. lua_getfield(L, -1, "__close");
  116. return (lua_tocfunction(L, -1))(L);
  117. #else
  118. int *p = tofilep(L);
  119. if(*p == c_stdin || *p == c_stdout || *p == c_stderr)
  120. {
  121. lua_pushnil(L);
  122. lua_pushliteral(L, "cannot close standard file");
  123. return 2;
  124. }
  125. int ok = (vfs_close(*p) == 0);
  126. *p = FS_OPEN_OK - 1;
  127. return pushresult(L, ok, NULL);
  128. #endif
  129. }
  130. static int io_close (lua_State *L) {
  131. if (lua_isnone(L, 1))
  132. LUA_IO_GETFIELD(IO_OUTPUT);
  133. tofile(L); /* make sure argument is a file */
  134. return aux_close(L);
  135. }
  136. static int io_gc (lua_State *L) {
  137. int f = *tofilep(L);
  138. /* ignore closed files */
  139. if (f != FS_OPEN_OK - 1)
  140. aux_close(L);
  141. return 0;
  142. }
  143. static int io_tostring (lua_State *L) {
  144. int f = *tofilep(L);
  145. if (f == FS_OPEN_OK - 1)
  146. lua_pushliteral(L, "file (closed)");
  147. else
  148. lua_pushfstring(L, "file (%i)", f);
  149. return 1;
  150. }
  151. static int io_open (lua_State *L) {
  152. const char *filename = luaL_checkstring(L, 1);
  153. const char *mode = luaL_optstring(L, 2, "r");
  154. int *pf = newfile(L);
  155. *pf = vfs_open(filename, mode);
  156. return (*pf == FS_OPEN_OK - 1) ? pushresult(L, 0, filename) : 1;
  157. }
  158. /*
  159. ** this function has a separated environment, which defines the
  160. ** correct __close for 'popen' files
  161. */
  162. #if 0
  163. static int io_popen (lua_State *L) {
  164. const char *filename = luaL_checkstring(L, 1);
  165. const char *mode = luaL_optstring(L, 2, "r");
  166. int *pf = newfile(L);
  167. *pf = lua_popen(L, filename, fs_mode2flags(mode));
  168. return (*pf == FS_OPEN_OK - 1) ? pushresult(L, 0, filename) : 1;
  169. }
  170. static int io_tmpfile (lua_State *L) {
  171. int *pf = newfile(L);
  172. *pf = tmpfile();
  173. return (*pf == FS_OPEN_OK - 1) ? pushresult(L, 0, NULL) : 1;
  174. }
  175. #endif
  176. static int getiofile (lua_State *L, int findex) {
  177. int *pf;
  178. LUA_IO_GETFIELD(findex);
  179. pf = (int *)lua_touserdata(L, -1);
  180. if (pf == NULL || *pf == FS_OPEN_OK - 1){
  181. luaL_error(L, "default %s file is closed", fnames[findex - 1]);
  182. return FS_OPEN_OK - 1;
  183. }
  184. return *pf;
  185. }
  186. static int g_iofile (lua_State *L, int f, const char *mode) {
  187. if (!lua_isnoneornil(L, 1)) {
  188. const char *filename = lua_tostring(L, 1);
  189. if (filename) {
  190. int *pf = newfile(L);
  191. *pf = vfs_open(filename, mode);
  192. if (*pf == FS_OPEN_OK - 1)
  193. fileerror(L, 1, filename);
  194. }
  195. else {
  196. tofile(L); /* check that it's a valid file handle */
  197. lua_pushvalue(L, 1);
  198. }
  199. LUA_IO_SETFIELD(f);
  200. }
  201. /* return current value */
  202. LUA_IO_GETFIELD(f);
  203. return 1;
  204. }
  205. static int io_input (lua_State *L) {
  206. return g_iofile(L, IO_INPUT, "r");
  207. }
  208. static int io_output (lua_State *L) {
  209. return g_iofile(L, IO_OUTPUT, "w");
  210. }
  211. static int io_readline (lua_State *L);
  212. static void aux_lines (lua_State *L, int idx, int toclose) {
  213. lua_pushvalue(L, idx);
  214. lua_pushboolean(L, toclose); /* close/not close file when finished */
  215. lua_pushcclosure(L, io_readline, 2);
  216. }
  217. static int f_lines (lua_State *L) {
  218. tofile(L); /* check that it's a valid file handle */
  219. aux_lines(L, 1, 0);
  220. return 1;
  221. }
  222. static int io_lines (lua_State *L) {
  223. if (lua_isnoneornil(L, 1)) { /* no arguments? */
  224. /* will iterate over default input */
  225. LUA_IO_GETFIELD(IO_INPUT);
  226. return f_lines(L);
  227. }
  228. else {
  229. const char *filename = luaL_checkstring(L, 1);
  230. int *pf = newfile(L);
  231. *pf = vfs_open(filename, "r");
  232. if (*pf == FS_OPEN_OK - 1)
  233. fileerror(L, 1, filename);
  234. aux_lines(L, lua_gettop(L), 1);
  235. return 1;
  236. }
  237. }
  238. /*
  239. ** {======================================================
  240. ** READ
  241. ** =======================================================
  242. */
  243. #if 0
  244. static int read_number (lua_State *L, int f) {
  245. lua_Number d;
  246. if (fs_scanf(f, LUA_NUMBER_SCAN, &d) == 1) {
  247. lua_pushnumber(L, d);
  248. return 1;
  249. }
  250. else {
  251. lua_pushnil(L); /* "result" to be removed */
  252. return 0; /* read fails */
  253. }
  254. }
  255. #endif
  256. static int test_eof (lua_State *L, int f) {
  257. int c = vfs_getc(f);
  258. vfs_ungetc(c, f);
  259. lua_pushlstring(L, NULL, 0);
  260. return (c != EOF);
  261. }
  262. #if 0
  263. static int read_line (lua_State *L, int f) {
  264. luaL_Buffer b;
  265. luaL_buffinit(L, &b);
  266. for (;;) {
  267. size_t l;
  268. char *p = luaL_prepbuffer(&b);
  269. if (fs_gets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
  270. luaL_pushresult(&b); /* close buffer */
  271. return (lua_objlen(L, -1) > 0); /* check whether read something */
  272. }
  273. l = c_strlen(p);
  274. if (l == 0 || p[l-1] != '\n')
  275. luaL_addsize(&b, l);
  276. else {
  277. luaL_addsize(&b, l - 1); /* do not include `eol' */
  278. luaL_pushresult(&b); /* close buffer */
  279. return 1; /* read at least an `eol' */
  280. }
  281. }
  282. }
  283. #else
  284. static int read_line (lua_State *L, int f) {
  285. luaL_Buffer b;
  286. luaL_buffinit(L, &b);
  287. char *p = luaL_prepbuffer(&b);
  288. signed char c = EOF;
  289. int i = 0;
  290. do{
  291. c = (signed char)vfs_getc(f);
  292. if(c==EOF){
  293. break;
  294. }
  295. p[i++] = c;
  296. }while((c!=EOF) && (c!='\n') && (i<LUAL_BUFFERSIZE) );
  297. if(i>0 && p[i-1] == '\n')
  298. i--; /* do not include `eol' */
  299. if(i==0){
  300. luaL_pushresult(&b); /* close buffer */
  301. return (lua_objlen(L, -1) > 0); /* check whether read something */
  302. }
  303. luaL_addsize(&b, i);
  304. luaL_pushresult(&b); /* close buffer */
  305. return 1; /* read at least an `eol' */
  306. }
  307. #endif
  308. static int read_chars (lua_State *L, int f, size_t n) {
  309. size_t rlen; /* how much to read */
  310. size_t nr; /* number of chars actually read */
  311. luaL_Buffer b;
  312. luaL_buffinit(L, &b);
  313. rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
  314. do {
  315. char *p = luaL_prepbuffer(&b);
  316. if (rlen > n) rlen = n; /* cannot read more than asked */
  317. nr = vfs_read(f, p, rlen);
  318. luaL_addsize(&b, nr);
  319. n -= nr; /* still have to read `n' chars */
  320. } while (n > 0 && nr == rlen); /* until end of count or eof */
  321. luaL_pushresult(&b); /* close buffer */
  322. return (n == 0 || lua_objlen(L, -1) > 0);
  323. }
  324. static int g_read (lua_State *L, int f, int first) {
  325. int nargs = lua_gettop(L) - 1;
  326. int success;
  327. int n;
  328. //vfs_clearerr(f);
  329. if (nargs == 0) { /* no arguments? */
  330. success = read_line(L, f);
  331. n = first+1; /* to return 1 result */
  332. }
  333. else { /* ensure stack space for all results and for auxlib's buffer */
  334. luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
  335. success = 1;
  336. for (n = first; nargs-- && success; n++) {
  337. if (lua_type(L, n) == LUA_TNUMBER) {
  338. size_t l = (size_t)lua_tointeger(L, n);
  339. success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
  340. }
  341. else {
  342. const char *p = lua_tostring(L, n);
  343. luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
  344. switch (p[1]) {
  345. #if 0
  346. case 'n': /* number */
  347. success = read_number(L, f);
  348. break;
  349. #endif
  350. case 'l': /* line */
  351. success = read_line(L, f);
  352. break;
  353. case 'a': /* file */
  354. read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
  355. success = 1; /* always success */
  356. break;
  357. default:
  358. return luaL_argerror(L, n, "invalid format");
  359. }
  360. }
  361. }
  362. }
  363. if (vfs_ferrno(f))
  364. return pushresult(L, 0, NULL);
  365. if (!success) {
  366. lua_pop(L, 1); /* remove last result */
  367. lua_pushnil(L); /* push nil instead */
  368. }
  369. return n - first;
  370. }
  371. static int io_read (lua_State *L) {
  372. return g_read(L, getiofile(L, IO_INPUT), 1);
  373. }
  374. static int f_read (lua_State *L) {
  375. return g_read(L, tofile(L), 2);
  376. }
  377. static int io_readline (lua_State *L) {
  378. int *pf = (int *)lua_touserdata(L, lua_upvalueindex(1));
  379. int sucess;
  380. if (pf == NULL || *pf == FS_OPEN_OK - 1){ /* file is already closed? */
  381. luaL_error(L, "file is already closed");
  382. return 0;
  383. }
  384. sucess = read_line(L, *pf);
  385. if (vfs_ferrno(*pf))
  386. return luaL_error(L, "err(%d)", vfs_ferrno(*pf));
  387. if (sucess) return 1;
  388. else { /* EOF */
  389. if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
  390. lua_settop(L, 0);
  391. lua_pushvalue(L, lua_upvalueindex(1));
  392. aux_close(L); /* close it */
  393. }
  394. return 0;
  395. }
  396. }
  397. /* }====================================================== */
  398. static int g_write (lua_State *L, int f, int arg) {
  399. int nargs = lua_gettop(L) - 1;
  400. int status = 1;
  401. for (; nargs--; arg++) {
  402. #if 0
  403. if (lua_type(L, arg) == LUA_TNUMBER) {
  404. /* optimization: could be done exactly as for strings */
  405. status = status &&
  406. fs_printf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
  407. }
  408. else
  409. #endif
  410. {
  411. size_t l;
  412. const char *s = luaL_checklstring(L, arg, &l);
  413. status = status && (vfs_write(f, s, l) == l);
  414. }
  415. }
  416. return pushresult(L, status, NULL);
  417. }
  418. static int io_write (lua_State *L) {
  419. return g_write(L, getiofile(L, IO_OUTPUT), 1);
  420. }
  421. static int f_write (lua_State *L) {
  422. return g_write(L, tofile(L), 2);
  423. }
  424. static int f_seek (lua_State *L) {
  425. static const int mode[] = {VFS_SEEK_SET, VFS_SEEK_CUR, VFS_SEEK_END};
  426. static const char *const modenames[] = {"set", "cur", "end", NULL};
  427. int f = tofile(L);
  428. int op = luaL_checkoption(L, 2, "cur", modenames);
  429. long offset = luaL_optlong(L, 3, 0);
  430. op = vfs_lseek(f, offset, mode[op]);
  431. if (op)
  432. return pushresult(L, 0, NULL); /* error */
  433. else {
  434. lua_pushinteger(L, vfs_tell(f));
  435. return 1;
  436. }
  437. }
  438. #if 0
  439. static int f_setvbuf (lua_State *L) {
  440. static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
  441. static const char *const modenames[] = {"no", "full", "line", NULL};
  442. int f = tofile(L);
  443. int op = luaL_checkoption(L, 2, NULL, modenames);
  444. lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
  445. int res = setvbuf(f, NULL, mode[op], sz);
  446. return pushresult(L, res == 0, NULL);
  447. }
  448. #endif
  449. static int io_flush (lua_State *L) {
  450. return pushresult(L, vfs_flush(getiofile(L, IO_OUTPUT)) == 0, NULL);
  451. }
  452. static int f_flush (lua_State *L) {
  453. return pushresult(L, vfs_flush(tofile(L)) == 0, NULL);
  454. }
  455. #undef MIN_OPT_LEVEL
  456. #define MIN_OPT_LEVEL 2
  457. #include "lrodefs.h"
  458. #if LUA_OPTIMIZE_MEMORY == 2
  459. const LUA_REG_TYPE iolib_funcs[] = {
  460. #else
  461. const LUA_REG_TYPE iolib[] = {
  462. #endif
  463. {LSTRKEY("close"), LFUNCVAL(io_close)},
  464. {LSTRKEY("flush"), LFUNCVAL(io_flush)},
  465. {LSTRKEY("input"), LFUNCVAL(io_input)},
  466. {LSTRKEY("lines"), LFUNCVAL(io_lines)},
  467. {LSTRKEY("open"), LFUNCVAL(io_open)},
  468. {LSTRKEY("output"), LFUNCVAL(io_output)},
  469. // {LSTRKEY("popen"), LFUNCVAL(io_popen)},
  470. {LSTRKEY("read"), LFUNCVAL(io_read)},
  471. // {LSTRKEY("tmpfile"), LFUNCVAL(io_tmpfile)},
  472. {LSTRKEY("type"), LFUNCVAL(io_type)},
  473. {LSTRKEY("write"), LFUNCVAL(io_write)},
  474. {LNILKEY, LNILVAL}
  475. };
  476. #if LUA_OPTIMIZE_MEMORY == 2
  477. static int luaL_index(lua_State *L)
  478. {
  479. return luaR_findfunction(L, iolib_funcs);
  480. }
  481. const luaL_Reg iolib[] = {
  482. {"__index", luaL_index},
  483. {NULL, NULL}
  484. };
  485. #endif
  486. #undef MIN_OPT_LEVEL
  487. #define MIN_OPT_LEVEL 1
  488. #include "lrodefs.h"
  489. const LUA_REG_TYPE flib[] = {
  490. {LSTRKEY("close"), LFUNCVAL(io_close)},
  491. {LSTRKEY("flush"), LFUNCVAL(f_flush)},
  492. {LSTRKEY("lines"), LFUNCVAL(f_lines)},
  493. {LSTRKEY("read"), LFUNCVAL(f_read)},
  494. {LSTRKEY("seek"), LFUNCVAL(f_seek)},
  495. // {LSTRKEY("setvbuf"), LFUNCVAL(f_setvbuf)},
  496. {LSTRKEY("write"), LFUNCVAL(f_write)},
  497. {LSTRKEY("__gc"), LFUNCVAL(io_gc)},
  498. {LSTRKEY("__tostring"), LFUNCVAL(io_tostring)},
  499. #if LUA_OPTIMIZE_MEMORY > 0
  500. {LSTRKEY("__index"), LROVAL(flib)},
  501. #endif
  502. {LNILKEY, LNILVAL}
  503. };
  504. static void createmeta (lua_State *L) {
  505. #if LUA_OPTIMIZE_MEMORY == 0
  506. luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
  507. lua_pushvalue(L, -1); /* push metatable */
  508. lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
  509. luaL_register(L, NULL, flib); /* file methods */
  510. #else
  511. luaL_rometatable(L, LUA_FILEHANDLE, (void*)flib); /* create metatable for file handles */
  512. #endif
  513. }
  514. static void createstdfile (lua_State *L, int f, int k, const char *fname) {
  515. *newfile(L) = f;
  516. #if LUA_OPTIMIZE_MEMORY != 2
  517. if (k > 0) {
  518. lua_pushvalue(L, -1);
  519. lua_rawseti(L, LUA_ENVIRONINDEX, k);
  520. }
  521. lua_pushvalue(L, -2); /* copy environment */
  522. lua_setfenv(L, -2); /* set it */
  523. lua_setfield(L, -3, fname);
  524. #else
  525. lua_pushvalue(L, -1);
  526. lua_rawseti(L, LUA_REGISTRYINDEX, liolib_keys[k]);
  527. lua_setfield(L, -2, fname);
  528. #endif
  529. }
  530. #if LUA_OPTIMIZE_MEMORY != 2
  531. static void newfenv (lua_State *L, lua_CFunction cls) {
  532. lua_createtable(L, 0, 1);
  533. lua_pushcfunction(L, cls);
  534. lua_setfield(L, -2, "__close");
  535. }
  536. #endif
  537. LUALIB_API int luaopen_io (lua_State *L) {
  538. createmeta(L);
  539. #if LUA_OPTIMIZE_MEMORY != 2
  540. /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
  541. newfenv(L, io_fclose);
  542. lua_replace(L, LUA_ENVIRONINDEX);
  543. /* open library */
  544. luaL_register(L, LUA_IOLIBNAME, iolib);
  545. newfenv(L, io_noclose); /* close function for default files */
  546. #else
  547. luaL_register_light(L, LUA_IOLIBNAME, iolib);
  548. lua_pushvalue(L, -1);
  549. lua_setmetatable(L, -2);
  550. #endif
  551. #if 0
  552. /* create (and set) default files */
  553. createstdfile(L, c_stdin, IO_INPUT, "stdin");
  554. createstdfile(L, c_stdout, IO_OUTPUT, "stdout");
  555. createstdfile(L, c_stderr, IO_STDERR, "stderr");
  556. #if LUA_OPTIMIZE_MEMORY != 2
  557. lua_pop(L, 1); /* pop environment for default files */
  558. lua_getfield(L, -1, "popen");
  559. newfenv(L, io_pclose); /* create environment for 'popen' */
  560. lua_setfenv(L, -2); /* set fenv for 'popen' */
  561. lua_pop(L, 1); /* pop 'popen' */
  562. #endif
  563. #endif
  564. return 1;
  565. }