2
0

lua.c 9.6 KB

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