2
0

liolib.c 18 KB

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