iolib.c 6.9 KB

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