lua.c 9.5 KB

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