lua.c 9.5 KB

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