liolib.c 7.5 KB

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