/script_binding/lua/lua/lua.c

http://ftk.googlecode.com/ · C · 392 lines · 323 code · 61 blank · 8 comment · 100 complexity · 50502857aab32e8a1886b491b3fe1ddb MD5 · raw file

  1. /*
  2. ** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $
  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. static lua_State *globalL = NULL;
  15. static const char *progname = LUA_PROGNAME;
  16. static void lstop (lua_State *L, lua_Debug *ar) {
  17. (void)ar; /* unused arg. */
  18. lua_sethook(L, NULL, 0, 0);
  19. luaL_error(L, "interrupted!");
  20. }
  21. static void laction (int i) {
  22. signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
  23. terminate process (default action) */
  24. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  25. }
  26. static void print_usage (void) {
  27. fprintf(stderr,
  28. "usage: %s [options] [script [args]].\n"
  29. "Available options are:\n"
  30. " -e stat execute string " LUA_QL("stat") "\n"
  31. " -l name require library " LUA_QL("name") "\n"
  32. " -i enter interactive mode after executing " LUA_QL("script") "\n"
  33. " -v show version information\n"
  34. " -- stop handling options\n"
  35. " - execute stdin and stop handling options\n"
  36. ,
  37. progname);
  38. fflush(stderr);
  39. }
  40. static void l_message (const char *pname, const char *msg) {
  41. if (pname) fprintf(stderr, "%s: ", pname);
  42. fprintf(stderr, "%s\n", msg);
  43. fflush(stderr);
  44. }
  45. static int report (lua_State *L, int status) {
  46. if (status && !lua_isnil(L, -1)) {
  47. const char *msg = lua_tostring(L, -1);
  48. if (msg == NULL) msg = "(error object is not a string)";
  49. l_message(progname, msg);
  50. lua_pop(L, 1);
  51. }
  52. return status;
  53. }
  54. static int traceback (lua_State *L) {
  55. if (!lua_isstring(L, 1)) /* 'message' not a string? */
  56. return 1; /* keep it intact */
  57. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  58. if (!lua_istable(L, -1)) {
  59. lua_pop(L, 1);
  60. return 1;
  61. }
  62. lua_getfield(L, -1, "traceback");
  63. if (!lua_isfunction(L, -1)) {
  64. lua_pop(L, 2);
  65. return 1;
  66. }
  67. lua_pushvalue(L, 1); /* pass error message */
  68. lua_pushinteger(L, 2); /* skip this function and traceback */
  69. lua_call(L, 2, 1); /* call debug.traceback */
  70. return 1;
  71. }
  72. static int docall (lua_State *L, int narg, int clear) {
  73. int status;
  74. int base = lua_gettop(L) - narg; /* function index */
  75. lua_pushcfunction(L, traceback); /* push traceback function */
  76. lua_insert(L, base); /* put it under chunk and args */
  77. signal(SIGINT, laction);
  78. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  79. signal(SIGINT, SIG_DFL);
  80. lua_remove(L, base); /* remove traceback function */
  81. /* force a complete garbage collection in case of errors */
  82. if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
  83. return status;
  84. }
  85. static void print_version (void) {
  86. l_message(NULL, LUA_RELEASE " " LUA_COPYRIGHT);
  87. }
  88. static int getargs (lua_State *L, char **argv, int n) {
  89. int narg;
  90. int i;
  91. int argc = 0;
  92. while (argv[argc]) argc++; /* count total number of arguments */
  93. narg = argc - (n + 1); /* number of arguments to the script */
  94. luaL_checkstack(L, narg + 3, "too many arguments to script");
  95. for (i=n+1; i < argc; i++)
  96. lua_pushstring(L, argv[i]);
  97. lua_createtable(L, narg, n + 1);
  98. for (i=0; i < argc; i++) {
  99. lua_pushstring(L, argv[i]);
  100. lua_rawseti(L, -2, i - n);
  101. }
  102. return narg;
  103. }
  104. static int dofile (lua_State *L, const char *name) {
  105. int status = luaL_loadfile(L, name) || docall(L, 0, 1);
  106. return report(L, status);
  107. }
  108. static int dostring (lua_State *L, const char *s, const char *name) {
  109. int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
  110. return report(L, status);
  111. }
  112. static int dolibrary (lua_State *L, const char *name) {
  113. lua_getglobal(L, "require");
  114. lua_pushstring(L, name);
  115. return report(L, docall(L, 1, 1));
  116. }
  117. static const char *get_prompt (lua_State *L, int firstline) {
  118. const char *p;
  119. lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
  120. p = lua_tostring(L, -1);
  121. if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
  122. lua_pop(L, 1); /* remove global */
  123. return p;
  124. }
  125. static int incomplete (lua_State *L, int status) {
  126. if (status == LUA_ERRSYNTAX) {
  127. size_t lmsg;
  128. const char *msg = lua_tolstring(L, -1, &lmsg);
  129. const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
  130. if (strstr(msg, LUA_QL("<eof>")) == tp) {
  131. lua_pop(L, 1);
  132. return 1;
  133. }
  134. }
  135. return 0; /* else... */
  136. }
  137. static int pushline (lua_State *L, int firstline) {
  138. char buffer[LUA_MAXINPUT];
  139. char *b = buffer;
  140. size_t l;
  141. const char *prmt = get_prompt(L, firstline);
  142. if (lua_readline(L, b, prmt) == 0)
  143. return 0; /* no input */
  144. l = strlen(b);
  145. if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
  146. b[l-1] = '\0'; /* remove it */
  147. if (firstline && b[0] == '=') /* first line starts with `=' ? */
  148. lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
  149. else
  150. lua_pushstring(L, b);
  151. lua_freeline(L, b);
  152. return 1;
  153. }
  154. static int loadline (lua_State *L) {
  155. int status;
  156. lua_settop(L, 0);
  157. if (!pushline(L, 1))
  158. return -1; /* no input */
  159. for (;;) { /* repeat until gets a complete line */
  160. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  161. if (!incomplete(L, status)) break; /* cannot try to add lines? */
  162. if (!pushline(L, 0)) /* no more input? */
  163. return -1;
  164. lua_pushliteral(L, "\n"); /* add a new line... */
  165. lua_insert(L, -2); /* ...between the two lines */
  166. lua_concat(L, 3); /* join them */
  167. }
  168. lua_saveline(L, 1);
  169. lua_remove(L, 1); /* remove line */
  170. return status;
  171. }
  172. static void dotty (lua_State *L) {
  173. int status;
  174. const char *oldprogname = progname;
  175. progname = NULL;
  176. while ((status = loadline(L)) != -1) {
  177. if (status == 0) status = docall(L, 0, 0);
  178. report(L, status);
  179. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  180. lua_getglobal(L, "print");
  181. lua_insert(L, 1);
  182. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  183. l_message(progname, lua_pushfstring(L,
  184. "error calling " LUA_QL("print") " (%s)",
  185. lua_tostring(L, -1)));
  186. }
  187. }
  188. lua_settop(L, 0); /* clear stack */
  189. fputs("\n", stdout);
  190. fflush(stdout);
  191. progname = oldprogname;
  192. }
  193. static int handle_script (lua_State *L, char **argv, int n) {
  194. int status;
  195. const char *fname;
  196. int narg = getargs(L, argv, n); /* collect arguments */
  197. lua_setglobal(L, "arg");
  198. fname = argv[n];
  199. if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
  200. fname = NULL; /* stdin */
  201. status = luaL_loadfile(L, fname);
  202. lua_insert(L, -(narg+1));
  203. if (status == 0)
  204. status = docall(L, narg, 0);
  205. else
  206. lua_pop(L, narg);
  207. return report(L, status);
  208. }
  209. /* check that argument has no extra characters at the end */
  210. #define notail(x) {if ((x)[2] != '\0') return -1;}
  211. static int collectargs (char **argv, int *pi, int *pv, int *pe) {
  212. int i;
  213. for (i = 1; argv[i] != NULL; i++) {
  214. if (argv[i][0] != '-') /* not an option? */
  215. return i;
  216. switch (argv[i][1]) { /* option */
  217. case '-':
  218. notail(argv[i]);
  219. return (argv[i+1] != NULL ? i+1 : 0);
  220. case '\0':
  221. return i;
  222. case 'i':
  223. notail(argv[i]);
  224. *pi = 1; /* go through */
  225. case 'v':
  226. notail(argv[i]);
  227. *pv = 1;
  228. break;
  229. case 'e':
  230. *pe = 1; /* go through */
  231. case 'l':
  232. if (argv[i][2] == '\0') {
  233. i++;
  234. if (argv[i] == NULL) return -1;
  235. }
  236. break;
  237. default: return -1; /* invalid option */
  238. }
  239. }
  240. return 0;
  241. }
  242. static int runargs (lua_State *L, char **argv, int n) {
  243. int i;
  244. for (i = 1; i < n; i++) {
  245. if (argv[i] == NULL) continue;
  246. lua_assert(argv[i][0] == '-');
  247. switch (argv[i][1]) { /* option */
  248. case 'e': {
  249. const char *chunk = argv[i] + 2;
  250. if (*chunk == '\0') chunk = argv[++i];
  251. lua_assert(chunk != NULL);
  252. if (dostring(L, chunk, "=(command line)") != 0)
  253. return 1;
  254. break;
  255. }
  256. case 'l': {
  257. const char *filename = argv[i] + 2;
  258. if (*filename == '\0') filename = argv[++i];
  259. lua_assert(filename != NULL);
  260. if (dolibrary(L, filename))
  261. return 1; /* stop if file fails */
  262. break;
  263. }
  264. default: break;
  265. }
  266. }
  267. return 0;
  268. }
  269. static int handle_luainit (lua_State *L) {
  270. const char *init = getenv(LUA_INIT);
  271. if (init == NULL) return 0; /* status OK */
  272. else if (init[0] == '@')
  273. return dofile(L, init+1);
  274. else
  275. return dostring(L, init, "=" LUA_INIT);
  276. }
  277. struct Smain {
  278. int argc;
  279. char **argv;
  280. int status;
  281. };
  282. static int pmain (lua_State *L) {
  283. struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
  284. char **argv = s->argv;
  285. int script;
  286. int has_i = 0, has_v = 0, has_e = 0;
  287. globalL = L;
  288. if (argv[0] && argv[0][0]) progname = argv[0];
  289. lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
  290. luaL_openlibs(L); /* open libraries */
  291. lua_gc(L, LUA_GCRESTART, 0);
  292. s->status = handle_luainit(L);
  293. if (s->status != 0) return 0;
  294. script = collectargs(argv, &has_i, &has_v, &has_e);
  295. if (script < 0) { /* invalid args? */
  296. print_usage();
  297. s->status = 1;
  298. return 0;
  299. }
  300. if (has_v) print_version();
  301. s->status = runargs(L, argv, (script > 0) ? script : s->argc);
  302. if (s->status != 0) return 0;
  303. if (script)
  304. s->status = handle_script(L, argv, script);
  305. if (s->status != 0) return 0;
  306. if (has_i)
  307. dotty(L);
  308. else if (script == 0 && !has_e && !has_v) {
  309. if (lua_stdin_is_tty()) {
  310. print_version();
  311. dotty(L);
  312. }
  313. else dofile(L, NULL); /* executes stdin as a file */
  314. }
  315. return 0;
  316. }
  317. int main (int argc, char **argv) {
  318. int status;
  319. struct Smain s;
  320. lua_State *L = lua_open(); /* create state */
  321. if (L == NULL) {
  322. l_message(argv[0], "cannot create state: not enough memory");
  323. return EXIT_FAILURE;
  324. }
  325. s.argc = argc;
  326. s.argv = argv;
  327. status = lua_cpcall(L, &pmain, &s);
  328. report(L, status);
  329. lua_close(L);
  330. return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
  331. }