liolib.c 7.8 KB

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