lua.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. ** $Id: lua.c,v 1.129 2004/07/01 14:26:28 roberto Exp roberto $
  3. ** Lua stand-alone interpreter
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define lua_c
  11. #include "lua.h"
  12. #include "lauxlib.h"
  13. #include "lualib.h"
  14. /*
  15. ** generic extra include file
  16. */
  17. #ifdef LUA_USERCONFIG
  18. #include LUA_USERCONFIG
  19. #endif
  20. static lua_State *globalL = NULL;
  21. static const char *progname = PROGNAME;
  22. static void lstop (lua_State *L, lua_Debug *ar) {
  23. (void)ar; /* unused arg. */
  24. lua_sethook(L, NULL, 0, 0);
  25. luaL_error(L, "interrupted!");
  26. }
  27. static void laction (int i) {
  28. signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
  29. terminate process (default action) */
  30. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  31. }
  32. static void print_usage (void) {
  33. fprintf(stderr,
  34. "usage: %s [options] [script [args]].\n"
  35. "Available options are:\n"
  36. " - execute stdin as a file\n"
  37. " -e stat execute string `stat'\n"
  38. " -i enter interactive mode after executing `script'\n"
  39. " -l name load and run library `name'\n"
  40. " -v show version information\n"
  41. " -- stop handling options\n" ,
  42. progname);
  43. }
  44. static void l_message (const char *pname, const char *msg) {
  45. if (pname) fprintf(stderr, "%s: ", pname);
  46. fprintf(stderr, "%s\n", msg);
  47. }
  48. static int report (lua_State *L, int status) {
  49. if (status && !lua_isnil(L, -1)) {
  50. const char *msg = lua_tostring(L, -1);
  51. if (msg == NULL) msg = "(error object is not a string)";
  52. l_message(progname, msg);
  53. lua_pop(L, 1);
  54. }
  55. return status;
  56. }
  57. static int docall (lua_State *L, int narg, int clear) {
  58. int status;
  59. int base = lua_gettop(L) - narg; /* function index */
  60. lua_pushliteral(L, "_TRACEBACK");
  61. lua_rawget(L, LUA_GLOBALSINDEX); /* get traceback function */
  62. lua_insert(L, base); /* put it under chunk and args */
  63. signal(SIGINT, laction);
  64. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  65. signal(SIGINT, SIG_DFL);
  66. lua_remove(L, base); /* remove traceback function */
  67. return status;
  68. }
  69. static void print_version (void) {
  70. l_message(NULL, LUA_VERSION " " LUA_COPYRIGHT);
  71. }
  72. static int getargs (lua_State *L, char *argv[], int n) {
  73. int i, narg;
  74. for (i=n+1; argv[i]; i++) {
  75. luaL_checkstack(L, 1, "too many arguments to script");
  76. lua_pushstring(L, argv[i]);
  77. }
  78. narg = i-(n+1); /* number of arguments to the script (not to `lua.c') */
  79. lua_newtable(L);
  80. for (i=0; argv[i]; i++) {
  81. lua_pushnumber(L, i - n);
  82. lua_pushstring(L, argv[i]);
  83. lua_rawset(L, -3);
  84. }
  85. return narg;
  86. }
  87. static int dofile (lua_State *L, const char *name) {
  88. int status = luaL_loadfile(L, name) || docall(L, 0, 1);
  89. return report(L, status);
  90. }
  91. static int dostring (lua_State *L, const char *s, const char *name) {
  92. int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
  93. return report(L, status);
  94. }
  95. static int dolibrary (lua_State *L, const char *name) {
  96. name = luaL_searchpath(L, name, NULL);
  97. if (name == NULL) return report(L, 1);
  98. else return dofile(L, name);
  99. }
  100. /*
  101. ** this macro defines a function to show the prompt and reads the
  102. ** next line for manual input
  103. */
  104. #ifndef lua_readline
  105. #define lua_readline(L,prompt) readline(L,prompt)
  106. /* maximum length of an input line */
  107. #ifndef MAXINPUT
  108. #define MAXINPUT 512
  109. #endif
  110. static int readline (lua_State *L, const char *prompt) {
  111. static char buffer[MAXINPUT];
  112. if (prompt) {
  113. fputs(prompt, stdout);
  114. fflush(stdout);
  115. }
  116. if (fgets(buffer, sizeof(buffer), stdin) == NULL)
  117. return 0; /* read fails */
  118. else {
  119. lua_pushstring(L, buffer);
  120. return 1;
  121. }
  122. }
  123. #endif
  124. static const char *get_prompt (lua_State *L, int firstline) {
  125. const char *p = NULL;
  126. lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
  127. lua_rawget(L, LUA_GLOBALSINDEX);
  128. p = lua_tostring(L, -1);
  129. if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
  130. lua_pop(L, 1); /* remove global */
  131. return p;
  132. }
  133. static int incomplete (lua_State *L, int status) {
  134. if (status == LUA_ERRSYNTAX &&
  135. strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
  136. lua_pop(L, 1);
  137. return 1;
  138. }
  139. else
  140. return 0;
  141. }
  142. static int loadline (lua_State *L) {
  143. int status;
  144. lua_settop(L, 0);
  145. if (lua_readline(L, get_prompt(L, 1)) == 0) /* no input? */
  146. return -1;
  147. if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */
  148. lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */
  149. lua_remove(L, -2); /* remove original line */
  150. }
  151. for (;;) { /* repeat until gets a complete line */
  152. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  153. if (!incomplete(L, status)) break; /* cannot try to add lines? */
  154. if (lua_readline(L, get_prompt(L, 0)) == 0) /* no more input? */
  155. return -1;
  156. lua_concat(L, lua_gettop(L)); /* join lines */
  157. }
  158. lua_saveline(L, lua_tostring(L, 1));
  159. lua_remove(L, 1); /* remove line */
  160. return status;
  161. }
  162. static void dotty (lua_State *L) {
  163. int status;
  164. const char *oldprogname = progname;
  165. progname = NULL;
  166. print_version();
  167. while ((status = loadline(L)) != -1) {
  168. if (status == 0) status = docall(L, 0, 0);
  169. report(L, status);
  170. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  171. lua_getglobal(L, "print");
  172. lua_insert(L, 1);
  173. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  174. l_message(progname, lua_pushfstring(L, "error calling `print' (%s)",
  175. lua_tostring(L, -1)));
  176. }
  177. }
  178. lua_settop(L, 0); /* clear stack */
  179. fputs("\n", stdout);
  180. progname = oldprogname;
  181. }
  182. static int checkvar (lua_State *L) {
  183. const char *name = lua_tostring(L, 2);
  184. if (name)
  185. luaL_error(L, "attempt to access undefined variable `%s'", name);
  186. return 0;
  187. }
  188. #define clearinteractive(i) (*i &= 2)
  189. static int handle_argv (lua_State *L, char *argv[], int *interactive) {
  190. if (argv[1] == NULL) { /* no arguments? */
  191. *interactive = 0;
  192. if (stdin_is_tty())
  193. dotty(L);
  194. else
  195. dofile(L, NULL); /* executes stdin as a file */
  196. }
  197. else { /* other arguments; loop over them */
  198. int i;
  199. for (i = 1; argv[i] != NULL; i++) {
  200. if (argv[i][0] != '-') break; /* not an option? */
  201. switch (argv[i][1]) { /* option */
  202. case '-': { /* `--' */
  203. if (argv[i][2] != '\0') {
  204. print_usage();
  205. return 1;
  206. }
  207. i++; /* skip this argument */
  208. goto endloop; /* stop handling arguments */
  209. }
  210. case '\0': {
  211. clearinteractive(interactive);
  212. dofile(L, NULL); /* executes stdin as a file */
  213. break;
  214. }
  215. case 'i': {
  216. *interactive = 2; /* force interactive mode after arguments */
  217. break;
  218. }
  219. case 'v': {
  220. clearinteractive(interactive);
  221. print_version();
  222. break;
  223. }
  224. case 'w': {
  225. if (lua_getmetatable(L, LUA_GLOBALSINDEX)) {
  226. lua_pushcfunction(L, checkvar);
  227. lua_setfield(L, -2, "__index");
  228. }
  229. break;
  230. }
  231. case 'e': {
  232. const char *chunk = argv[i] + 2;
  233. clearinteractive(interactive);
  234. if (*chunk == '\0') chunk = argv[++i];
  235. if (chunk == NULL) {
  236. print_usage();
  237. return 1;
  238. }
  239. if (dostring(L, chunk, "=<command line>") != 0)
  240. return 1;
  241. break;
  242. }
  243. case 'l': {
  244. const char *filename = argv[i] + 2;
  245. if (*filename == '\0') filename = argv[++i];
  246. if (filename == NULL) {
  247. print_usage();
  248. return 1;
  249. }
  250. if (dolibrary(L, filename))
  251. return 1; /* stop if file fails */
  252. break;
  253. }
  254. default: {
  255. clearinteractive(interactive);
  256. print_usage();
  257. return 1;
  258. }
  259. }
  260. } endloop:
  261. if (argv[i] != NULL) {
  262. const char *filename = argv[i];
  263. int narg = getargs(L, argv, i); /* collect arguments */
  264. int status;
  265. lua_setglobal(L, "arg");
  266. clearinteractive(interactive);
  267. status = luaL_loadfile(L, filename);
  268. lua_insert(L, -(narg+1));
  269. if (status == 0)
  270. status = docall(L, narg, 0);
  271. else
  272. lua_pop(L, narg);
  273. return report(L, status);
  274. }
  275. }
  276. return 0;
  277. }
  278. static int handle_luainit (lua_State *L) {
  279. const char *init = getenv("LUA_INIT");
  280. if (init == NULL) return 0; /* status OK */
  281. else if (init[0] == '@')
  282. return dofile(L, init+1);
  283. else
  284. return dostring(L, init, "=LUA_INIT");
  285. }
  286. struct Smain {
  287. int argc;
  288. char **argv;
  289. int status;
  290. };
  291. static int pmain (lua_State *L) {
  292. struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
  293. int status;
  294. int interactive = 1;
  295. if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
  296. globalL = L;
  297. lua_userinit(L); /* open libraries */
  298. status = handle_luainit(L);
  299. if (status == 0) {
  300. status = handle_argv(L, s->argv, &interactive);
  301. if (status == 0 && interactive) dotty(L);
  302. }
  303. s->status = status;
  304. return 0;
  305. }
  306. int main (int argc, char *argv[]) {
  307. int status;
  308. struct Smain s;
  309. lua_State *L = lua_open(); /* create state */
  310. if (L == NULL) {
  311. l_message(argv[0], "cannot create state: not enough memory");
  312. return EXIT_FAILURE;
  313. }
  314. s.argc = argc;
  315. s.argv = argv;
  316. status = lua_cpcall(L, &pmain, &s);
  317. report(L, status);
  318. lua_close(L);
  319. return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
  320. }