iolib.c 7.2 KB

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