liolib.c 8.0 KB

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