lua.c 9.5 KB

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