26 #define RTTLUA_BOILER "OROCOS RTTLua (TLSF)" 28 #define RTTLUA_BOILER "OROCOS RTTLua" 30 #define RTTLUA_VERSION "1.0-beta5" 31 #define XSTR(x) STR(x) 34 static lua_State *globalL = NULL;
36 static const char *progname = LUA_PROGNAME;
40 static void lstop (lua_State *L,
lua_Debug *ar) {
42 lua_sethook(L, NULL, 0, 0);
43 luaL_error(L,
"interrupted!");
47 static void laction (
int i) {
50 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
53 static void print_usage (
void) {
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" 68 void l_message (
const char *pname,
const char *msg) {
69 if (pname) fprintf(stderr,
"%s: ", pname);
70 fprintf(stderr,
"%s\n", msg);
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);
86 static int traceback (lua_State *L) {
87 if (!lua_isstring(L, 1))
89 lua_getfield(L, LUA_GLOBALSINDEX,
"debug");
90 if (!lua_istable(L, -1)) {
94 lua_getfield(L, -1,
"traceback");
95 if (!lua_isfunction(L, -1)) {
100 lua_pushinteger(L, 2);
106 static int docall (lua_State *L,
int narg,
int clear) {
108 int base = lua_gettop(L) - narg;
109 lua_pushcfunction(L, traceback);
111 signal(SIGINT, laction);
112 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
113 signal(SIGINT, SIG_DFL);
116 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
120 static void print_version (
void) {
121 l_message(NULL, RTTLUA_BOILER
" " RTTLUA_VERSION
" / " LUA_RELEASE
" (" XSTR(OROCOS_TARGET)
")" );
124 static void print_quit_info (
void) {
125 l_message(NULL,
" Use Ctrl-d to quit." );
128 static int getargs (lua_State *L,
char **argv,
int n) {
132 while (argv[argc]) argc++;
133 narg = argc - (n + 1);
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);
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);
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);
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));
163 static const char *get_prompt (lua_State *L,
int firstline) {
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);
173 static int incomplete (lua_State *L,
int status) {
174 if (status == LUA_ERRSYNTAX) {
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) {
187 static int pushline (lua_State *L,
int firstline) {
188 char buffer[LUA_MAXINPUT];
191 const char *prmt = get_prompt(L, firstline);
192 if (lua_readline(L, b, prmt) == 0)
195 if (l > 0 && b[l-1] ==
'\n')
197 if (firstline && b[0] ==
'=')
198 lua_pushfstring(L,
"return %s", b+1);
200 lua_pushstring(L, b);
206 static int loadline (lua_State *L) {
212 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1),
"=stdin");
213 if (!incomplete(L, status))
break;
216 lua_pushliteral(L,
"\n");
226 void dotty (lua_State *L) {
228 const char *oldprogname = progname;
230 while ((status = loadline(L)) != -1) {
231 if (status == 0) status = docall(L, 0, 0);
233 if (status == 0 && lua_gettop(L) > 0) {
234 lua_getglobal(L,
"print");
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)));
245 progname = oldprogname;
249 static int handle_script (lua_State *L,
char **argv,
int n) {
252 int narg = getargs(L, argv, n);
253 lua_setglobal(L,
"arg");
255 if (strcmp(fname,
"-") == 0 && strcmp(argv[n-1],
"--") != 0)
257 status = luaL_loadfile(L, fname);
258 lua_insert(L, -(narg+1));
260 status = docall(L, narg, 0);
263 return report(L, status);
268 #define notail(x) {if ((x)[2] != '\0') return -1;} 271 static int collectargs (
char **argv,
int *pi,
int *pv,
int *pe) {
273 for (i = 1; argv[i] != NULL; i++) {
274 if (argv[i][0] !=
'-')
276 switch (argv[i][1]) {
279 return (argv[i+1] != NULL ? i+1 : 0);
292 if (argv[i][2] ==
'\0') {
294 if (argv[i] == NULL)
return -1;
304 static int runargs (lua_State *L,
char **argv,
int n) {
306 for (i = 1; i < n; i++) {
307 if (argv[i] == NULL)
continue;
308 lua_assert(argv[i][0] ==
'-');
309 switch (argv[i][1]) {
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)
319 const char *filename = argv[i] + 2;
320 if (*filename ==
'\0') filename = argv[++i];
321 lua_assert(filename != NULL);
322 if (dolibrary(L, filename))
333 static int handle_luainit (lua_State *L) {
334 const char *init = getenv(LUA_INIT);
335 if (init == NULL)
return 0;
336 else if (init[0] ==
'@')
337 return dofile(L, init+1);
339 return dostring(L, init,
"=" LUA_INIT);
350 static int pmain (lua_State *L) {
351 struct Smain *s = (
struct Smain *)lua_touserdata(L, 1);
352 char **argv = s->argv;
354 int has_i = 0, has_v = 0, has_e = 0;
356 if (argv[0] && argv[0][0]) progname = argv[0];
357 lua_gc(L, LUA_GCSTOP, 0);
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);
368 if (has_v) print_version();
369 s->status = runargs(L, argv, (script > 0) ? script : s->argc);
370 if (s->status != 0)
return 0;
372 s->status = handle_script(L, argv, script);
373 if (s->status != 0)
return 0;
376 else if (script == 0 && !has_e && !has_v) {
377 if (lua_stdin_is_tty()) {
382 else dofile(L, NULL);
388 int main (
int argc,
char **argv) {
391 lua_State *L = lua_open();
393 l_message(argv[0],
"cannot create state: not enough memory");
398 status = lua_cpcall(L, &pmain, &s);
401 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
405 int main_args (lua_State *L,
int argc,
char **argv) {
411 status = lua_cpcall(L, &pmain, &s);
413 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;