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