lua.c 9.6 KB

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