iolib.c 6.6 KB

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