liolib.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. ** $Id: liolib.c,v 1.20 1998/06/05 22:17:44 roberto Exp roberto $
  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. #include <time.h>
  11. #include "lauxlib.h"
  12. #include "lua.h"
  13. #include "luadebug.h"
  14. #include "lualib.h"
  15. #ifndef OLD_ANSI
  16. #include <locale.h>
  17. #else
  18. #define setlocale(a,b) 0
  19. #define LC_ALL 0
  20. #define LC_COLLATE 0
  21. #define LC_CTYPE 0
  22. #define LC_MONETARY 0
  23. #define LC_NUMERIC 0
  24. #define LC_TIME 0
  25. #define strerror(e) "(no error message provided by operating system)"
  26. #endif
  27. #define CLOSEDTAG 2
  28. #define IOTAG 1
  29. #define FIRSTARG 3 /* 1st and 2nd are upvalues */
  30. #define FINPUT "_INPUT"
  31. #define FOUTPUT "_OUTPUT"
  32. #ifdef POPEN
  33. FILE *popen();
  34. int pclose();
  35. #else
  36. #define popen(x,y) NULL /* that is, popen always fails */
  37. #define pclose(x) (-1)
  38. #endif
  39. static int gettag (int i)
  40. {
  41. return lua_getnumber(lua_getparam(i));
  42. }
  43. static void pushresult (int i)
  44. {
  45. if (i)
  46. lua_pushuserdata(NULL);
  47. else {
  48. lua_pushnil();
  49. lua_pushstring(strerror(errno));
  50. }
  51. }
  52. static int ishandler (lua_Object f)
  53. {
  54. if (lua_isuserdata(f)) {
  55. if (lua_tag(f) == gettag(CLOSEDTAG))
  56. lua_error("cannot access a closed file");
  57. return lua_tag(f) == gettag(IOTAG);
  58. }
  59. else return 0;
  60. }
  61. static FILE *getfile (char *name)
  62. {
  63. lua_Object f = lua_getglobal(name);
  64. if (!ishandler(f))
  65. luaL_verror("global variable `%.50s' is not a file handle", name);
  66. return lua_getuserdata(f);
  67. }
  68. static FILE *getfileparam (char *name, int *arg)
  69. {
  70. lua_Object f = lua_getparam(*arg);
  71. if (ishandler(f)) {
  72. (*arg)++;
  73. return lua_getuserdata(f);
  74. }
  75. else
  76. return getfile(name);
  77. }
  78. static void closefile (char *name)
  79. {
  80. FILE *f = getfile(name);
  81. if (f == stdin || f == stdout) return;
  82. if (pclose(f) == -1)
  83. fclose(f);
  84. lua_pushobject(lua_getglobal(name));
  85. lua_settag(gettag(CLOSEDTAG));
  86. }
  87. static void setfile (FILE *f, char *name, int tag)
  88. {
  89. lua_pushusertag(f, tag);
  90. lua_setglobal(name);
  91. }
  92. static void setreturn (FILE *f, char *name)
  93. {
  94. int tag = gettag(IOTAG);
  95. setfile(f, name, tag);
  96. lua_pushusertag(f, tag);
  97. }
  98. static void io_readfrom (void)
  99. {
  100. FILE *current;
  101. lua_Object f = lua_getparam(FIRSTARG);
  102. if (f == LUA_NOOBJECT) {
  103. closefile(FINPUT);
  104. current = stdin;
  105. }
  106. else if (lua_tag(f) == gettag(IOTAG))
  107. current = lua_getuserdata(f);
  108. else {
  109. char *s = luaL_check_string(FIRSTARG);
  110. current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
  111. if (current == NULL) {
  112. pushresult(0);
  113. return;
  114. }
  115. }
  116. setreturn(current, FINPUT);
  117. }
  118. static void io_writeto (void)
  119. {
  120. FILE *current;
  121. lua_Object f = lua_getparam(FIRSTARG);
  122. if (f == LUA_NOOBJECT) {
  123. closefile(FOUTPUT);
  124. current = stdout;
  125. }
  126. else if (lua_tag(f) == gettag(IOTAG))
  127. current = lua_getuserdata(f);
  128. else {
  129. char *s = luaL_check_string(FIRSTARG);
  130. current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
  131. if (current == NULL) {
  132. pushresult(0);
  133. return;
  134. }
  135. }
  136. setreturn(current, FOUTPUT);
  137. }
  138. static void io_appendto (void)
  139. {
  140. char *s = luaL_check_string(FIRSTARG);
  141. FILE *fp = fopen (s, "a");
  142. if (fp != NULL)
  143. setreturn(fp, FOUTPUT);
  144. else
  145. pushresult(0);
  146. }
  147. #define NEED_OTHER (EOF-1) /* just some flag different from EOF */
  148. static void read_until (FILE *f, int lim) {
  149. int l = 0;
  150. int c;
  151. for (c = getc(f); c != EOF && c != lim; c = getc(f)) {
  152. luaL_addchar(c);
  153. l++;
  154. }
  155. if (l > 0 || c == lim) /* read anything? */
  156. lua_pushlstring(luaL_buffer(), l);
  157. }
  158. static void io_read (void) {
  159. int arg = FIRSTARG;
  160. FILE *f = getfileparam(FINPUT, &arg);
  161. char *p = luaL_opt_string(arg, NULL);
  162. luaL_resetbuffer();
  163. if (p == NULL) /* default: read a line */
  164. read_until(f, '\n');
  165. else if (p[0] == '.' && p[1] == '*' && p[2] == 0) /* p = ".*" */
  166. read_until(f, EOF);
  167. else {
  168. int l = 0; /* number of chars read in buffer */
  169. int inskip = 0; /* to control {skips} */
  170. int c = NEED_OTHER;
  171. while (*p) {
  172. switch (*p) {
  173. case '{':
  174. inskip++;
  175. p++;
  176. continue;
  177. case '}':
  178. if (inskip == 0)
  179. lua_error("unbalanced braces in read pattern");
  180. inskip--;
  181. p++;
  182. continue;
  183. default: {
  184. char *ep; /* get what is next */
  185. int m; /* match result */
  186. if (c == NEED_OTHER) c = getc(f);
  187. if (c == EOF) {
  188. luaI_singlematch(0, p, &ep); /* to set "ep" */
  189. m = 0;
  190. }
  191. else {
  192. m = luaI_singlematch(c, p, &ep);
  193. if (m) {
  194. if (inskip == 0) {
  195. luaL_addchar(c);
  196. l++;
  197. }
  198. c = NEED_OTHER;
  199. }
  200. }
  201. switch (*ep) {
  202. case '*': /* repetition */
  203. if (!m) p = ep+1; /* else stay in (repeat) the same item */
  204. continue;
  205. case '?': /* optional */
  206. p = ep+1; /* continues reading the pattern */
  207. continue;
  208. default:
  209. if (m) p = ep; /* continues reading the pattern */
  210. else
  211. goto break_while; /* pattern fails */
  212. }
  213. }
  214. }
  215. } break_while:
  216. if (c >= 0) /* not EOF nor NEED_OTHER? */
  217. ungetc(c, f);
  218. if (l > 0 || *p == 0) /* read something or did not fail? */
  219. lua_pushlstring(luaL_buffer(), l);
  220. }
  221. }
  222. static void io_write (void)
  223. {
  224. int arg = FIRSTARG;
  225. FILE *f = getfileparam(FOUTPUT, &arg);
  226. int status = 1;
  227. char *s;
  228. long l;
  229. while ((s = luaL_opt_lstr(arg++, NULL, &l)) != NULL)
  230. status = status && (fwrite(s, 1, l, f) == l);
  231. pushresult(status);
  232. }
  233. static void io_execute (void)
  234. {
  235. lua_pushnumber(system(luaL_check_string(1)));
  236. }
  237. static void io_remove (void)
  238. {
  239. pushresult(remove(luaL_check_string(1)) == 0);
  240. }
  241. static void io_rename (void)
  242. {
  243. pushresult(rename(luaL_check_string(1),
  244. luaL_check_string(2)) == 0);
  245. }
  246. static void io_tmpname (void)
  247. {
  248. lua_pushstring(tmpnam(NULL));
  249. }
  250. static void io_getenv (void)
  251. {
  252. lua_pushstring(getenv(luaL_check_string(1))); /* if NULL push nil */
  253. }
  254. static void io_clock (void) {
  255. lua_pushnumber(((double)clock())/CLOCKS_PER_SEC);
  256. }
  257. static void io_date (void)
  258. {
  259. time_t t;
  260. struct tm *tm;
  261. char *s = luaL_opt_string(1, "%c");
  262. char b[BUFSIZ];
  263. time(&t); tm = localtime(&t);
  264. if (strftime(b,sizeof(b),s,tm))
  265. lua_pushstring(b);
  266. else
  267. lua_error("invalid `date' format");
  268. }
  269. static void setloc (void)
  270. {
  271. static int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC,
  272. LC_TIME};
  273. static char *catnames[] = {"all", "collate", "ctype", "monetary",
  274. "numeric", "time", NULL};
  275. int op = luaL_findstring(luaL_opt_string(2, "all"), catnames);
  276. luaL_arg_check(op != -1, 2, "invalid option");
  277. lua_pushstring(setlocale(cat[op], luaL_check_string(1)));
  278. }
  279. static void io_exit (void)
  280. {
  281. lua_Object o = lua_getparam(1);
  282. exit(lua_isnumber(o) ? (int)lua_getnumber(o) : 1);
  283. }
  284. static void io_debug (void)
  285. {
  286. while (1) {
  287. char buffer[250];
  288. fprintf(stderr, "lua_debug> ");
  289. if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
  290. if (strcmp(buffer, "cont\n") == 0) return;
  291. lua_dostring(buffer);
  292. }
  293. }
  294. static void lua_printstack (FILE *f)
  295. {
  296. int level = 1; /* skip level 0 (it's this function) */
  297. lua_Object func;
  298. while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
  299. char *name;
  300. int currentline;
  301. char *filename;
  302. int linedefined;
  303. lua_funcinfo(func, &filename, &linedefined);
  304. fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t");
  305. switch (*lua_getobjname(func, &name)) {
  306. case 'g':
  307. fprintf(f, "function %s", name);
  308. break;
  309. case 't':
  310. fprintf(f, "`%s' tag method", name);
  311. break;
  312. default: {
  313. if (linedefined == 0)
  314. fprintf(f, "main of %s", filename);
  315. else if (linedefined < 0)
  316. fprintf(f, "%s", filename);
  317. else
  318. fprintf(f, "function (%s:%d)", filename, linedefined);
  319. filename = NULL;
  320. }
  321. }
  322. if ((currentline = lua_currentline(func)) > 0)
  323. fprintf(f, " at line %d", currentline);
  324. if (filename)
  325. fprintf(f, " [in file %s]", filename);
  326. fprintf(f, "\n");
  327. }
  328. }
  329. static void errorfb (void)
  330. {
  331. fprintf(stderr, "lua: %s\n", lua_getstring(lua_getparam(1)));
  332. lua_printstack(stderr);
  333. }
  334. static struct luaL_reg iolib[] = {
  335. {"setlocale", setloc},
  336. {"execute", io_execute},
  337. {"remove", io_remove},
  338. {"rename", io_rename},
  339. {"tmpname", io_tmpname},
  340. {"getenv", io_getenv},
  341. {"date", io_date},
  342. {"clock", io_clock},
  343. {"exit", io_exit},
  344. {"debug", io_debug},
  345. {"print_stack", errorfb}
  346. };
  347. static struct luaL_reg iolibtag[] = {
  348. {"readfrom", io_readfrom},
  349. {"writeto", io_writeto},
  350. {"appendto", io_appendto},
  351. {"read", io_read},
  352. {"write", io_write}
  353. };
  354. static void openwithtags (void)
  355. {
  356. int iotag = lua_newtag();
  357. int closedtag = lua_newtag();
  358. int i;
  359. for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
  360. /* put both tags as upvalues for these functions */
  361. lua_pushnumber(iotag);
  362. lua_pushnumber(closedtag);
  363. lua_pushcclosure(iolibtag[i].func, 2);
  364. lua_setglobal(iolibtag[i].name);
  365. }
  366. setfile(stdin, FINPUT, iotag);
  367. setfile(stdout, FOUTPUT, iotag);
  368. setfile(stdin, "_STDIN", iotag);
  369. setfile(stdout, "_STDOUT", iotag);
  370. setfile(stderr, "_STDERR", iotag);
  371. }
  372. void lua_iolibopen (void)
  373. {
  374. luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
  375. openwithtags();
  376. lua_pushcfunction(errorfb);
  377. lua_seterrormethod();
  378. }