iolib.c 6.9 KB

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