lua.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. ** $Id: lua.c,v 1.122 2003/04/03 13:34:42 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. }
  118. static int docall (int status) {
  119. if (status == 0) status = lcall(0, 1);
  120. return report(status);
  121. }
  122. static int file_input (const char *name) {
  123. return docall(luaL_loadfile(L, name));
  124. }
  125. static int dostring (const char *s, const char *name) {
  126. return docall(luaL_loadbuffer(L, s, strlen(s), name));
  127. }
  128. static int load_file (const char *name) {
  129. lua_pushliteral(L, "require");
  130. lua_rawget(L, LUA_GLOBALSINDEX);
  131. if (!lua_isfunction(L, -1)) { /* no `require' defined? */
  132. lua_pop(L, 1);
  133. return file_input(name);
  134. }
  135. else {
  136. lua_pushstring(L, name);
  137. return report(lcall(1, 1));
  138. }
  139. }
  140. /*
  141. ** this macro can be used by some `history' system to save lines
  142. ** read in manual input
  143. */
  144. #ifndef lua_saveline
  145. #define lua_saveline(L,line) /* empty */
  146. #endif
  147. /*
  148. ** this macro defines a function to show the prompt and reads the
  149. ** next line for manual input
  150. */
  151. #ifndef lua_readline
  152. #define lua_readline(L,prompt) readline(L,prompt)
  153. /* maximum length of an input line */
  154. #ifndef MAXINPUT
  155. #define MAXINPUT 512
  156. #endif
  157. static int readline (lua_State *l, const char *prompt) {
  158. static char buffer[MAXINPUT];
  159. if (prompt) {
  160. fputs(prompt, stdout);
  161. fflush(stdout);
  162. }
  163. if (fgets(buffer, sizeof(buffer), stdin) == NULL)
  164. return 0; /* read fails */
  165. else {
  166. lua_pushstring(l, buffer);
  167. return 1;
  168. }
  169. }
  170. #endif
  171. static const char *get_prompt (int firstline) {
  172. const char *p = NULL;
  173. lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
  174. lua_rawget(L, LUA_GLOBALSINDEX);
  175. p = lua_tostring(L, -1);
  176. if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
  177. lua_pop(L, 1); /* remove global */
  178. return p;
  179. }
  180. static int incomplete (int status) {
  181. if (status == LUA_ERRSYNTAX &&
  182. strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
  183. lua_pop(L, 1);
  184. return 1;
  185. }
  186. else
  187. return 0;
  188. }
  189. static int load_string (void) {
  190. int status;
  191. lua_settop(L, 0);
  192. if (lua_readline(L, get_prompt(1)) == 0) /* no input? */
  193. return -1;
  194. if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */
  195. lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */
  196. lua_remove(L, -2); /* remove original line */
  197. }
  198. for (;;) { /* repeat until gets a complete line */
  199. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  200. if (!incomplete(status)) break; /* cannot try to add lines? */
  201. if (lua_readline(L, get_prompt(0)) == 0) /* no more input? */
  202. return -1;
  203. lua_concat(L, lua_gettop(L)); /* join lines */
  204. }
  205. lua_saveline(L, lua_tostring(L, 1));
  206. lua_remove(L, 1); /* remove line */
  207. return status;
  208. }
  209. static void manual_input (void) {
  210. int status;
  211. const char *oldprogname = progname;
  212. progname = NULL;
  213. while ((status = load_string()) != -1) {
  214. if (status == 0) status = lcall(0, 0);
  215. report(status);
  216. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  217. lua_getglobal(L, "print");
  218. lua_insert(L, 1);
  219. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  220. l_message(progname, lua_pushfstring(L, "error calling `print' (%s)",
  221. lua_tostring(L, -1)));
  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 (stdin_is_tty()) {
  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. if (argv[i][2] != '\0') {
  244. print_usage();
  245. return 1;
  246. }
  247. i++; /* skip this argument */
  248. goto endloop; /* stop handling arguments */
  249. }
  250. case '\0': {
  251. file_input(NULL); /* executes stdin as a file */
  252. break;
  253. }
  254. case 'i': {
  255. *interactive = 1;
  256. break;
  257. }
  258. case 'v': {
  259. print_version();
  260. break;
  261. }
  262. case 'e': {
  263. const char *chunk = argv[i] + 2;
  264. if (*chunk == '\0') chunk = argv[++i];
  265. if (chunk == NULL) {
  266. print_usage();
  267. return 1;
  268. }
  269. if (dostring(chunk, "=<command line>") != 0)
  270. return 1;
  271. break;
  272. }
  273. case 'l': {
  274. const char *filename = argv[i] + 2;
  275. if (*filename == '\0') filename = argv[++i];
  276. if (filename == NULL) {
  277. print_usage();
  278. return 1;
  279. }
  280. if (load_file(filename))
  281. return 1; /* stop if file fails */
  282. break;
  283. }
  284. case 'c': {
  285. l_message(progname, "option `-c' is deprecated");
  286. break;
  287. }
  288. case 's': {
  289. l_message(progname, "option `-s' is deprecated");
  290. break;
  291. }
  292. default: {
  293. print_usage();
  294. return 1;
  295. }
  296. }
  297. } endloop:
  298. if (argv[i] != NULL) {
  299. const char *filename = argv[i];
  300. getargs(argv, i); /* collect arguments */
  301. lua_setglobal(L, "arg");
  302. return file_input(filename); /* stop scanning arguments */
  303. }
  304. }
  305. return 0;
  306. }
  307. static void openstdlibs (lua_State *l) {
  308. const luaL_reg *lib = lualibs;
  309. for (; lib->func; lib++) {
  310. lib->func(l); /* open library */
  311. lua_settop(l, 0); /* discard any results */
  312. }
  313. }
  314. static int handle_luainit (void) {
  315. const char *init = getenv("LUA_INIT");
  316. if (init == NULL) return 0; /* status OK */
  317. else if (init[0] == '@')
  318. return file_input(init+1);
  319. else
  320. return dostring(init, "=LUA_INIT");
  321. }
  322. struct Smain {
  323. int argc;
  324. char **argv;
  325. int status;
  326. };
  327. static int pmain (lua_State *l) {
  328. struct Smain *s = (struct Smain *)lua_touserdata(l, 1);
  329. int status;
  330. int interactive = 0;
  331. if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
  332. L = l;
  333. lua_userinit(l); /* open libraries */
  334. status = handle_luainit();
  335. if (status == 0) {
  336. status = handle_argv(s->argv, &interactive);
  337. if (status == 0 && interactive) manual_input();
  338. }
  339. s->status = status;
  340. return 0;
  341. }
  342. int main (int argc, char *argv[]) {
  343. int status;
  344. struct Smain s;
  345. lua_State *l = lua_open(); /* create state */
  346. if (l == NULL) {
  347. l_message(argv[0], "cannot create state: not enough memory");
  348. return EXIT_FAILURE;
  349. }
  350. s.argc = argc;
  351. s.argv = argv;
  352. status = lua_cpcall(l, &pmain, &s);
  353. report(status);
  354. lua_close(l);
  355. return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
  356. }