liolib.c 8.3 KB

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