lua.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. ** $Id: lua.c,v 1.136 2005/03/21 18:12:07 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 = LUA_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 traceback (lua_State *L) {
  58. luaL_getfield(L, LUA_GLOBALSINDEX, "debug.traceback");
  59. if (!lua_isfunction(L, -1))
  60. lua_pop(L, 1);
  61. else {
  62. lua_pushvalue(L, 1); /* pass error message */
  63. lua_pushinteger(L, 2); /* skip this function and traceback */
  64. lua_call(L, 2, 1); /* call debug.traceback */
  65. }
  66. return 1;
  67. }
  68. static int docall (lua_State *L, int narg, int clear) {
  69. int status;
  70. int base = lua_gettop(L) - narg; /* function index */
  71. lua_pushcfunction(L, traceback); /* push traceback function */
  72. lua_insert(L, base); /* put it under chunk and args */
  73. signal(SIGINT, laction);
  74. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  75. signal(SIGINT, SIG_DFL);
  76. lua_remove(L, base); /* remove traceback function */
  77. return status;
  78. }
  79. static void print_version (void) {
  80. l_message(NULL, LUA_VERSION " " LUA_COPYRIGHT);
  81. }
  82. static int getargs (lua_State *L, char *argv[], int n) {
  83. int i, narg;
  84. for (i=n+1; argv[i]; i++) {
  85. luaL_checkstack(L, 1, "too many arguments to script");
  86. lua_pushstring(L, argv[i]);
  87. }
  88. narg = i-(n+1); /* number of arguments to the script (not to `lua.c') */
  89. lua_newtable(L);
  90. for (i=0; argv[i]; i++) {
  91. lua_pushstring(L, argv[i]);
  92. lua_rawseti(L, -2, i - n);
  93. }
  94. return narg;
  95. }
  96. static int dofile (lua_State *L, const char *name) {
  97. int status = luaL_loadfile(L, name) || docall(L, 0, 1);
  98. return report(L, status);
  99. }
  100. static int dostring (lua_State *L, const char *s, const char *name) {
  101. int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
  102. return report(L, status);
  103. }
  104. static int dolibrary (lua_State *L, const char *name) {
  105. luaL_getfield(L, LUA_GLOBALSINDEX, "package.path");
  106. if (!lua_isstring(L, -1)) {
  107. l_message(progname, "`package.path' must be a string");
  108. return 1;
  109. }
  110. name = luaL_searchpath(L, name, lua_tostring(L, -1));
  111. if (name == NULL) return report(L, 1);
  112. else return dofile(L, name);
  113. }
  114. static const char *get_prompt (lua_State *L, int firstline) {
  115. const char *p;
  116. lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
  117. lua_rawget(L, LUA_GLOBALSINDEX);
  118. p = lua_tostring(L, -1);
  119. if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
  120. lua_pop(L, 1); /* remove global */
  121. return p;
  122. }
  123. static int incomplete (lua_State *L, int status) {
  124. if (status == LUA_ERRSYNTAX &&
  125. strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
  126. lua_pop(L, 1);
  127. return 1;
  128. }
  129. else
  130. return 0;
  131. }
  132. static int pushline (lua_State *L, int firstline) {
  133. char buffer[LUA_MAXINPUT];
  134. char *b = buffer;
  135. size_t l;
  136. const char *prmt = get_prompt(L, firstline);
  137. if (lua_readline(L, b, prmt) == 0)
  138. return 0; /* no input */
  139. l = strlen(b);
  140. if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
  141. b[l-1] = '\0'; /* remove it */
  142. if (firstline && b[0] == '=') /* first line starts with `=' ? */
  143. lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
  144. else
  145. lua_pushstring(L, b);
  146. lua_freeline(L, b);
  147. return 1;
  148. }
  149. static int loadline (lua_State *L) {
  150. int status;
  151. lua_settop(L, 0);
  152. if (!pushline(L, 1))
  153. return -1; /* no input */
  154. for (;;) { /* repeat until gets a complete line */
  155. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  156. if (!incomplete(L, status)) break; /* cannot try to add lines? */
  157. if (!pushline(L, 0)) /* no more input? */
  158. return -1;
  159. lua_pushliteral(L, "\n"); /* add a new line... */
  160. lua_insert(L, -2); /* ...between the two lines */
  161. lua_concat(L, 3); /* join them */
  162. }
  163. lua_saveline(L, 1);
  164. lua_remove(L, 1); /* remove line */
  165. return status;
  166. }
  167. static void dotty (lua_State *L) {
  168. int status;
  169. const char *oldprogname = progname;
  170. progname = NULL;
  171. print_version();
  172. while ((status = loadline(L)) != -1) {
  173. if (status == 0) status = docall(L, 0, 0);
  174. report(L, status);
  175. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  176. lua_getglobal(L, "print");
  177. lua_insert(L, 1);
  178. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  179. l_message(progname, lua_pushfstring(L, "error calling `print' (%s)",
  180. lua_tostring(L, -1)));
  181. }
  182. }
  183. lua_settop(L, 0); /* clear stack */
  184. fputs("\n", stdout);
  185. progname = oldprogname;
  186. }
  187. static int checkvar (lua_State *L) {
  188. const char *name = lua_tostring(L, 2);
  189. if (name)
  190. luaL_error(L, "attempt to access undefined variable `%s'", name);
  191. return 0;
  192. }
  193. #define clearinteractive(i) (*i &= 2)
  194. static int handle_argv (lua_State *L, char *argv[], int *interactive) {
  195. if (argv[1] == NULL) { /* no arguments? */
  196. *interactive = 0;
  197. if (lua_stdin_is_tty())
  198. dotty(L);
  199. else
  200. dofile(L, NULL); /* executes stdin as a file */
  201. }
  202. else { /* other arguments; loop over them */
  203. int i;
  204. for (i = 1; argv[i] != NULL; i++) {
  205. if (argv[i][0] != '-') break; /* not an option? */
  206. switch (argv[i][1]) { /* option */
  207. case '-': { /* `--' */
  208. if (argv[i][2] != '\0') {
  209. print_usage();
  210. return 1;
  211. }
  212. i++; /* skip this argument */
  213. goto endloop; /* stop handling arguments */
  214. }
  215. case '\0': {
  216. clearinteractive(interactive);
  217. dofile(L, NULL); /* executes stdin as a file */
  218. break;
  219. }
  220. case 'i': {
  221. *interactive = 2; /* force interactive mode after arguments */
  222. break;
  223. }
  224. case 'v': {
  225. clearinteractive(interactive);
  226. print_version();
  227. break;
  228. }
  229. case 'w': {
  230. if (lua_getmetatable(L, LUA_GLOBALSINDEX)) {
  231. lua_pushcfunction(L, checkvar);
  232. lua_setfield(L, -2, "__index");
  233. }
  234. break;
  235. }
  236. case 'e': {
  237. const char *chunk = argv[i] + 2;
  238. clearinteractive(interactive);
  239. if (*chunk == '\0') chunk = argv[++i];
  240. if (chunk == NULL) {
  241. print_usage();
  242. return 1;
  243. }
  244. if (dostring(L, chunk, "=(command line)") != 0)
  245. return 1;
  246. break;
  247. }
  248. case 'l': {
  249. const char *filename = argv[i] + 2;
  250. if (*filename == '\0') filename = argv[++i];
  251. if (filename == NULL) {
  252. print_usage();
  253. return 1;
  254. }
  255. if (dolibrary(L, filename))
  256. return 1; /* stop if file fails */
  257. break;
  258. }
  259. default: {
  260. clearinteractive(interactive);
  261. print_usage();
  262. return 1;
  263. }
  264. }
  265. } endloop:
  266. if (argv[i] != NULL) {
  267. const char *filename = argv[i];
  268. int narg = getargs(L, argv, i); /* collect arguments */
  269. int status;
  270. lua_setglobal(L, "arg");
  271. clearinteractive(interactive);
  272. status = luaL_loadfile(L, filename);
  273. lua_insert(L, -(narg+1));
  274. if (status == 0)
  275. status = docall(L, narg, 0);
  276. else
  277. lua_pop(L, narg);
  278. return report(L, status);
  279. }
  280. }
  281. return 0;
  282. }
  283. static int handle_luainit (lua_State *L) {
  284. const char *init = getenv("LUA_INIT");
  285. if (init == NULL) return 0; /* status OK */
  286. else if (init[0] == '@')
  287. return dofile(L, init+1);
  288. else
  289. return dostring(L, init, "=LUA_INIT");
  290. }
  291. struct Smain {
  292. int argc;
  293. char **argv;
  294. int status;
  295. };
  296. static int pmain (lua_State *L) {
  297. struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
  298. int status;
  299. int interactive = 1;
  300. if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
  301. globalL = L;
  302. luaopen_stdlibs(L); /* open libraries */
  303. status = handle_luainit(L);
  304. if (status == 0) {
  305. status = handle_argv(L, s->argv, &interactive);
  306. if (status == 0 && interactive) dotty(L);
  307. }
  308. s->status = status;
  309. return 0;
  310. }
  311. int main (int argc, char *argv[]) {
  312. int status;
  313. struct Smain s;
  314. lua_State *L = lua_open(); /* create state */
  315. if (L == NULL) {
  316. l_message(argv[0], "cannot create state: not enough memory");
  317. return EXIT_FAILURE;
  318. }
  319. s.argc = argc;
  320. s.argv = argv;
  321. status = lua_cpcall(L, &pmain, &s);
  322. report(L, status);
  323. lua_close(L);
  324. return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
  325. }