iolib.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include "lua.h"
  8. #include "luadebug.h"
  9. #include "lualib.h"
  10. FILE *lua_infile, *lua_outfile;
  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("system unable to define the error");
  28. #endif
  29. }
  30. }
  31. static void closefile (FILE *f)
  32. {
  33. if (f == stdin || f == stdout)
  34. return;
  35. if (f == lua_infile)
  36. lua_infile = stdin;
  37. if (f == lua_outfile)
  38. lua_outfile = stdout;
  39. if (pclose(f) == -1)
  40. fclose(f);
  41. }
  42. static void io_readfrom (void)
  43. {
  44. lua_Object f = lua_getparam(1);
  45. if (f == LUA_NOOBJECT)
  46. closefile(lua_infile); /* restore standart input */
  47. else if (lua_isuserdata(f))
  48. lua_infile = lua_getuserdata(f);
  49. else {
  50. char *s = lua_check_string(1, "readfrom");
  51. FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
  52. if (fp)
  53. lua_infile = fp;
  54. else {
  55. pushresult(0);
  56. return;
  57. }
  58. }
  59. lua_pushuserdata(lua_infile);
  60. }
  61. static void io_writeto (void)
  62. {
  63. lua_Object f = lua_getparam(1);
  64. if (f == LUA_NOOBJECT)
  65. closefile(lua_outfile); /* restore standart output */
  66. else if (lua_isuserdata(f))
  67. lua_outfile = lua_getuserdata(f);
  68. else {
  69. char *s = lua_check_string(1, "writeto");
  70. FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
  71. if (fp)
  72. lua_outfile = fp;
  73. else {
  74. pushresult(0);
  75. return;
  76. }
  77. }
  78. lua_pushuserdata(lua_outfile);
  79. }
  80. static void io_appendto (void)
  81. {
  82. char *s = lua_check_string(1, "appendto");
  83. FILE *fp = fopen (s, "a");
  84. if (fp != NULL) {
  85. lua_outfile = fp;
  86. lua_pushuserdata(lua_outfile);
  87. }
  88. else
  89. pushresult(0);
  90. }
  91. #define NEED_OTHER (EOF-1) /* just some flag different from EOF */
  92. static void io_read (void)
  93. {
  94. char *buff;
  95. char *p = lua_opt_string(1, "[^\n]*{\n}", "read");
  96. int inskip = 0; /* to control {skips} */
  97. int c = NEED_OTHER;
  98. luaI_addchar(0);
  99. while (*p) {
  100. if (*p == '{' || *p == '}') {
  101. inskip = (*p == '{');
  102. p++;
  103. }
  104. else {
  105. char *ep = item_end(p); /* get what is next */
  106. int m;
  107. if (c == NEED_OTHER) c = getc(lua_infile);
  108. if ((m = singlematch(c, p)) != 0) {
  109. if (!inskip) luaI_addchar(c);
  110. c = NEED_OTHER;
  111. }
  112. switch (*ep) {
  113. case '*': /* repetition */
  114. if (!m) p = ep+1; /* else stay in (repeat) the same item */
  115. break;
  116. case '?': /* optional */
  117. p = ep+1; /* continues reading the pattern */
  118. break;
  119. default:
  120. if (m) p = ep; /* continues reading the pattern */
  121. else
  122. goto break_while; /* pattern fails */
  123. }
  124. }
  125. } break_while:
  126. if (c >= 0) /* not EOF nor NEED_OTHER? */
  127. ungetc(c, lua_infile);
  128. buff = luaI_addchar(0);
  129. if (*buff != 0 || *p == 0) /* read something or did not fail? */
  130. lua_pushstring(buff);
  131. }
  132. static void io_write (void)
  133. {
  134. int arg = 1;
  135. int status = 1;
  136. char *s;
  137. while ((s = lua_opt_string(arg++, NULL, "write")) != NULL)
  138. status = status && (fputs(s, lua_outfile) != EOF);
  139. pushresult(status);
  140. }
  141. static void io_execute (void)
  142. {
  143. lua_pushnumber(system(lua_check_string(1, "execute")));
  144. }
  145. static void io_remove (void)
  146. {
  147. pushresult(remove(lua_check_string(1, "remove")) == 0);
  148. }
  149. static void io_rename (void)
  150. {
  151. pushresult(rename(lua_check_string(1, "rename"),
  152. lua_check_string(2, "rename")) == 0);
  153. }
  154. static void io_tmpname (void)
  155. {
  156. lua_pushstring(tmpnam(NULL));
  157. }
  158. static void io_getenv (void)
  159. {
  160. lua_pushstring(getenv(lua_check_string(1, "getenv"))); /* if NULL push nil */
  161. }
  162. static void io_date (void)
  163. {
  164. time_t t;
  165. struct tm *tm;
  166. char *s = lua_opt_string(1, "%c", "date");
  167. char b[BUFSIZ];
  168. time(&t); tm = localtime(&t);
  169. if (strftime(b,sizeof(b),s,tm))
  170. lua_pushstring(b);
  171. else
  172. lua_error("invalid `date' format");
  173. }
  174. static void io_exit (void)
  175. {
  176. lua_Object o = lua_getparam(1);
  177. exit(lua_isnumber(o) ? (int)lua_getnumber(o) : 1);
  178. }
  179. static void io_debug (void)
  180. {
  181. while (1) {
  182. char buffer[250];
  183. fprintf(stderr, "lua_debug> ");
  184. if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
  185. if (strcmp(buffer, "cont\n") == 0) return;
  186. lua_dostring(buffer);
  187. }
  188. }
  189. static void lua_printstack (FILE *f)
  190. {
  191. int level = 0;
  192. lua_Object func;
  193. fprintf(f, "Active Stack:\n");
  194. while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
  195. char *name;
  196. int currentline;
  197. fprintf(f, "\t");
  198. switch (*lua_getobjname(func, &name)) {
  199. case 'g':
  200. fprintf(f, "function %s", name);
  201. break;
  202. case 'f':
  203. fprintf(f, "`%s' fallback", name);
  204. break;
  205. default: {
  206. char *filename;
  207. int linedefined;
  208. lua_funcinfo(func, &filename, &linedefined);
  209. if (linedefined == 0)
  210. fprintf(f, "main of %s", filename);
  211. else if (linedefined < 0)
  212. fprintf(f, "%s", filename);
  213. else
  214. fprintf(f, "function (%s:%d)", filename, linedefined);
  215. }
  216. }
  217. if ((currentline = lua_currentline(func)) > 0)
  218. fprintf(f, " at line %d", currentline);
  219. fprintf(f, "\n");
  220. }
  221. }
  222. static void errorfb (void)
  223. {
  224. char *s = lua_opt_string(1, "(no messsage)", NULL);
  225. fprintf(stderr, "lua: %s\n", s);
  226. lua_printstack(stderr);
  227. }
  228. static struct lua_reg iolib[] = {
  229. {"readfrom", io_readfrom},
  230. {"writeto", io_writeto},
  231. {"appendto", io_appendto},
  232. {"read", io_read},
  233. {"write", io_write},
  234. {"execute", io_execute},
  235. {"remove", io_remove},
  236. {"rename", io_rename},
  237. {"tmpname", io_tmpname},
  238. {"getenv", io_getenv},
  239. {"date", io_date},
  240. {"exit", io_exit},
  241. {"debug", io_debug},
  242. {"print_stack", errorfb}
  243. };
  244. void iolib_open (void)
  245. {
  246. lua_infile=stdin; lua_outfile=stdout;
  247. luaI_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
  248. lua_setfallback("error", errorfb);
  249. }