liolib.c 18 KB

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