lua.c 9.7 KB

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