liolib.c 17 KB

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