liolib.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. ** $Id: liolib.c,v 1.13 1997/12/26 18:38:16 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 io_read (void)
  149. {
  150. int arg = FIRSTARG;
  151. FILE *f = getfileparam(FINPUT, &arg);
  152. char *buff;
  153. char *p = luaL_opt_string(arg, "[^\n]*{\n}");
  154. int inskip = 0; /* to control {skips} */
  155. int c = NEED_OTHER;
  156. luaL_resetbuffer();
  157. while (*p) {
  158. if (*p == '{') {
  159. inskip++;
  160. p++;
  161. }
  162. else if (*p == '}') {
  163. if (inskip == 0)
  164. lua_error("unbalanced braces in read pattern");
  165. inskip--;
  166. p++;
  167. }
  168. else {
  169. char *ep; /* get what is next */
  170. int m; /* match result */
  171. if (c == NEED_OTHER) c = getc(f);
  172. m = luaI_singlematch((c == EOF) ? 0 : (char)c, p, &ep);
  173. if (m) {
  174. if (inskip == 0) luaL_addchar(c);
  175. c = NEED_OTHER;
  176. }
  177. switch (*ep) {
  178. case '*': /* repetition */
  179. if (!m) p = ep+1; /* else stay in (repeat) the same item */
  180. break;
  181. case '?': /* optional */
  182. p = ep+1; /* continues reading the pattern */
  183. break;
  184. default:
  185. if (m) p = ep; /* continues reading the pattern */
  186. else
  187. goto break_while; /* pattern fails */
  188. }
  189. }
  190. } break_while:
  191. if (c >= 0) /* not EOF nor NEED_OTHER? */
  192. ungetc(c, f);
  193. luaL_addchar(0);
  194. buff = luaL_buffer();
  195. if (*buff != 0 || *p == 0) /* read something or did not fail? */
  196. lua_pushstring(buff);
  197. }
  198. static void io_write (void)
  199. {
  200. int arg = FIRSTARG;
  201. FILE *f = getfileparam(FOUTPUT, &arg);
  202. int status = 1;
  203. char *s;
  204. while ((s = luaL_opt_string(arg++, NULL)) != NULL)
  205. status = status && (fputs(s, f) != EOF);
  206. pushresult(status);
  207. }
  208. static void io_execute (void)
  209. {
  210. lua_pushnumber(system(luaL_check_string(1)));
  211. }
  212. static void io_remove (void)
  213. {
  214. pushresult(remove(luaL_check_string(1)) == 0);
  215. }
  216. static void io_rename (void)
  217. {
  218. pushresult(rename(luaL_check_string(1),
  219. luaL_check_string(2)) == 0);
  220. }
  221. static void io_tmpname (void)
  222. {
  223. lua_pushstring(tmpnam(NULL));
  224. }
  225. static void io_getenv (void)
  226. {
  227. lua_pushstring(getenv(luaL_check_string(1))); /* if NULL push nil */
  228. }
  229. static void io_date (void)
  230. {
  231. time_t t;
  232. struct tm *tm;
  233. char *s = luaL_opt_string(1, "%c");
  234. char b[BUFSIZ];
  235. time(&t); tm = localtime(&t);
  236. if (strftime(b,sizeof(b),s,tm))
  237. lua_pushstring(b);
  238. else
  239. lua_error("invalid `date' format");
  240. }
  241. static void setloc (void)
  242. {
  243. static int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC,
  244. LC_TIME};
  245. int op = (int)luaL_opt_number(2, 0);
  246. luaL_arg_check(0 <= op && op <= 5, 2, "invalid option");
  247. lua_pushstring(setlocale(cat[op], luaL_check_string(1)));
  248. }
  249. static void io_exit (void)
  250. {
  251. lua_Object o = lua_getparam(1);
  252. exit(lua_isnumber(o) ? (int)lua_getnumber(o) : 1);
  253. }
  254. static void io_debug (void)
  255. {
  256. while (1) {
  257. char buffer[250];
  258. fprintf(stderr, "lua_debug> ");
  259. if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
  260. if (strcmp(buffer, "cont\n") == 0) return;
  261. lua_dostring(buffer);
  262. }
  263. }
  264. static void lua_printstack (FILE *f)
  265. {
  266. int level = 1; /* skip level 0 (it's this function) */
  267. lua_Object func;
  268. while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
  269. char *name;
  270. int currentline;
  271. char *filename;
  272. int linedefined;
  273. lua_funcinfo(func, &filename, &linedefined);
  274. fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t");
  275. switch (*lua_getobjname(func, &name)) {
  276. case 'g':
  277. fprintf(f, "function %s", name);
  278. break;
  279. case 't':
  280. fprintf(f, "`%s' tag method", name);
  281. break;
  282. default: {
  283. if (linedefined == 0)
  284. fprintf(f, "main of %s", filename);
  285. else if (linedefined < 0)
  286. fprintf(f, "%s", filename);
  287. else
  288. fprintf(f, "function (%s:%d)", filename, linedefined);
  289. filename = NULL;
  290. }
  291. }
  292. if ((currentline = lua_currentline(func)) > 0)
  293. fprintf(f, " at line %d", currentline);
  294. if (filename)
  295. fprintf(f, " [in file %s]", filename);
  296. fprintf(f, "\n");
  297. }
  298. }
  299. static void errorfb (void)
  300. {
  301. fprintf(stderr, "lua: %s\n", lua_getstring(lua_getparam(1)));
  302. lua_printstack(stderr);
  303. }
  304. static struct luaL_reg iolib[] = {
  305. {"setlocale", setloc},
  306. {"execute", io_execute},
  307. {"remove", io_remove},
  308. {"rename", io_rename},
  309. {"tmpname", io_tmpname},
  310. {"getenv", io_getenv},
  311. {"date", io_date},
  312. {"exit", io_exit},
  313. {"debug", io_debug},
  314. {"print_stack", errorfb}
  315. };
  316. static struct luaL_reg iolibtag[] = {
  317. {"readfrom", io_readfrom},
  318. {"writeto", io_writeto},
  319. {"appendto", io_appendto},
  320. {"read", io_read},
  321. {"write", io_write}
  322. };
  323. static void openwithtags (void)
  324. {
  325. int iotag = lua_newtag();
  326. int closedtag = lua_newtag();
  327. int i;
  328. for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
  329. /* put both tags as upvalues for these functions */
  330. lua_pushnumber(iotag);
  331. lua_pushnumber(closedtag);
  332. lua_pushCclosure(iolibtag[i].func, 2);
  333. lua_setglobal(iolibtag[i].name);
  334. }
  335. setfile(stdin, FINPUT, iotag);
  336. setfile(stdout, FOUTPUT, iotag);
  337. setfile(stdin, "_STDIN", iotag);
  338. setfile(stdout, "_STDOUT", iotag);
  339. setfile(stderr, "_STDERR", iotag);
  340. }
  341. void lua_iolibopen (void)
  342. {
  343. luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
  344. openwithtags();
  345. lua_pushcfunction(errorfb);
  346. lua_seterrormethod();
  347. }