liolib.c 19 KB

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