lua.c 9.3 KB

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