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
29 static lua_State
*globalL
= NULL
;
31 static const char *progname
= LUA_PROGNAME
;
35 static void lstop (lua_State
*L
, lua_Debug
*ar
) {
36 (void)ar
; /* unused arg. */
37 lua_sethook(L
, NULL
, 0, 0);
38 luaL_error(L
, "interrupted!");
41 static void laction (int i
) {
42 signal(i
, SIG_DFL
); /* if another SIGINT happens before lstop,
43 terminate process (default action) */
44 lua_sethook(globalL
, lstop
, LUA_MASKCALL
| LUA_MASKRET
| LUA_MASKCOUNT
, 1);
48 static void print_usage (void) {
50 "usage: %s [options] [script [args]].\n"
51 "Available options are:\n"
52 " -e stat execute string " LUA_QL("stat") "\n"
53 " -l name require library " LUA_QL("name") "\n"
54 " -i enter interactive mode after executing " LUA_QL("script") "\n"
55 " -v show version information\n"
56 " -- stop handling options\n"
57 " - execute stdin and stop handling options\n"
64 static void l_message (const char *pname
, const char *msg
) {
65 if (pname
) fprintf(stderr
, "%s: ", pname
);
66 fprintf(stderr
, "%s\n", msg
);
71 static int report (lua_State
*L
, int status
) {
72 if (status
&& !lua_isnil(L
, -1)) {
73 const char *msg
= lua_tostring(L
, -1);
74 if (msg
== NULL
) msg
= "(error object is not a string)";
75 l_message(progname
, msg
);
82 static int traceback (lua_State
*L
) {
83 if (!lua_isstring(L
, 1)) /* 'message' not a string? */
84 return 1; /* keep it intact */
85 lua_getfield(L
, LUA_GLOBALSINDEX
, "debug");
86 if (!lua_istable(L
, -1)) {
90 lua_getfield(L
, -1, "traceback");
91 if (!lua_isfunction(L
, -1)) {
95 lua_pushvalue(L
, 1); /* pass error message */
96 lua_pushinteger(L
, 2); /* skip this function and traceback */
97 lua_call(L
, 2, 1); /* call debug.traceback */
102 static int docall (lua_State
*L
, int narg
, int clear
) {
104 int base
= lua_gettop(L
) - narg
; /* function index */
105 lua_pushcfunction(L
, traceback
); /* push traceback function */
106 lua_insert(L
, base
); /* put it under chunk and args */
107 signal(SIGINT
, laction
);
108 status
= lua_pcall(L
, narg
, (clear
? 0 : LUA_MULTRET
), base
);
109 signal(SIGINT
, SIG_DFL
);
110 lua_remove(L
, base
); /* remove traceback function */
111 /* force a complete garbage collection in case of errors */
112 if (status
!= 0) lua_gc(L
, LUA_GCCOLLECT
, 0);
116 static void print_version (void) {
117 l_message(NULL
, LUA_RELEASE
" " LUA_COPYRIGHT
);
121 static int getargs (lua_State
*L
, char **argv
, int n
) {
125 while (argv
[argc
]) argc
++; /* count total number of arguments */
126 narg
= argc
- (n
+ 1); /* number of arguments to the script */
127 luaL_checkstack(L
, narg
+ 3, "too many arguments to script");
128 for (i
=n
+1; i
< argc
; i
++)
129 lua_pushstring(L
, argv
[i
]);
130 lua_createtable(L
, narg
, n
+ 1);
131 for (i
=0; i
< argc
; i
++) {
132 lua_pushstring(L
, argv
[i
]);
133 lua_rawseti(L
, -2, i
- n
);
139 static int dofile (lua_State
*L
, const char *name
) {
140 int status
= luaL_loadfile(L
, name
) || docall(L
, 0, 1);
141 return report(L
, status
);
145 static int dostring (lua_State
*L
, const char *s
, const char *name
) {
146 int status
= luaL_loadbuffer(L
, s
, strlen(s
), name
) || docall(L
, 0, 1);
147 return report(L
, status
);
151 static int dolibrary (lua_State
*L
, const char *name
) {
152 lua_getglobal(L
, "require");
153 lua_pushstring(L
, name
);
154 return report(L
, docall(L
, 1, 1));
158 static const char *get_prompt (lua_State
*L
, int firstline
) {
160 lua_getfield(L
, LUA_GLOBALSINDEX
, firstline
? "_PROMPT" : "_PROMPT2");
161 p
= lua_tostring(L
, -1);
162 if (p
== NULL
) p
= (firstline
? LUA_PROMPT
: LUA_PROMPT2
);
163 lua_pop(L
, 1); /* remove global */
168 static int incomplete (lua_State
*L
, int status
) {
169 if (status
== LUA_ERRSYNTAX
) {
171 const char *msg
= lua_tolstring(L
, -1, &lmsg
);
172 const char *tp
= msg
+ lmsg
- (sizeof(LUA_QL("<eof>")) - 1);
173 if (strstr(msg
, LUA_QL("<eof>")) == tp
) {
178 return 0; /* else... */
182 static int pushline (lua_State
*L
, int firstline
) {
183 char buffer
[LUA_MAXINPUT
];
186 const char *prmt
= get_prompt(L
, firstline
);
187 if (lua_readline(L
, b
, prmt
) == 0)
188 return 0; /* no input */
190 if (l
> 0 && b
[l
-1] == '\n') /* line ends with newline? */
191 b
[l
-1] = '\0'; /* remove it */
192 if (firstline
&& b
[0] == '=') /* first line starts with `=' ? */
193 lua_pushfstring(L
, "return %s", b
+1); /* change it to `return' */
195 lua_pushstring(L
, b
);
201 static int loadline (lua_State
*L
) {
205 return -1; /* no input */
206 for (;;) { /* repeat until gets a complete line */
207 status
= luaL_loadbuffer(L
, lua_tostring(L
, 1), lua_strlen(L
, 1), "=stdin");
208 if (!incomplete(L
, status
)) break; /* cannot try to add lines? */
209 if (!pushline(L
, 0)) /* no more input? */
211 lua_pushliteral(L
, "\n"); /* add a new line... */
212 lua_insert(L
, -2); /* ...between the two lines */
213 lua_concat(L
, 3); /* join them */
216 lua_remove(L
, 1); /* remove line */
221 static void dotty (lua_State
*L
) {
223 const char *oldprogname
= progname
;
225 while ((status
= loadline(L
)) != -1) {
226 if (status
== 0) status
= docall(L
, 0, 0);
228 if (status
== 0 && lua_gettop(L
) > 0) { /* any result to print? */
229 lua_getglobal(L
, "print");
231 if (lua_pcall(L
, lua_gettop(L
)-1, 0, 0) != 0)
232 l_message(progname
, lua_pushfstring(L
,
233 "error calling " LUA_QL("print") " (%s)",
234 lua_tostring(L
, -1)));
237 lua_settop(L
, 0); /* clear stack */
240 progname
= oldprogname
;
244 static int handle_script (lua_State
*L
, char **argv
, int n
) {
247 int narg
= getargs(L
, argv
, n
); /* collect arguments */
248 lua_setglobal(L
, "arg");
250 if (strcmp(fname
, "-") == 0 && strcmp(argv
[n
-1], "--") != 0)
251 fname
= NULL
; /* stdin */
252 status
= luaL_loadfile(L
, fname
);
253 lua_insert(L
, -(narg
+1));
255 status
= docall(L
, narg
, 0);
258 return report(L
, status
);
262 /* check that argument has no extra characters at the end */
263 #define notail(x) {if ((x)[2] != '\0') return -1;}
266 static int collectargs (char **argv
, int *pi
, int *pv
, int *pe
) {
268 for (i
= 1; argv
[i
] != NULL
; i
++) {
269 if (argv
[i
][0] != '-') /* not an option? */
271 switch (argv
[i
][1]) { /* option */
274 return (argv
[i
+1] != NULL
? i
+1 : 0);
279 *pi
= 1; /* go through */
285 *pe
= 1; /* go through */
287 if (argv
[i
][2] == '\0') {
289 if (argv
[i
] == NULL
) return -1;
292 default: return -1; /* invalid option */
299 static int runargs (lua_State
*L
, char **argv
, int n
) {
301 for (i
= 1; i
< n
; i
++) {
302 if (argv
[i
] == NULL
) continue;
303 lua_assert(argv
[i
][0] == '-');
304 switch (argv
[i
][1]) { /* option */
306 const char *chunk
= argv
[i
] + 2;
307 if (*chunk
== '\0') chunk
= argv
[++i
];
308 lua_assert(chunk
!= NULL
);
309 if (dostring(L
, chunk
, "=(command line)") != 0)
314 const char *filename
= argv
[i
] + 2;
315 if (*filename
== '\0') filename
= argv
[++i
];
316 lua_assert(filename
!= NULL
);
317 if (dolibrary(L
, filename
))
318 return 1; /* stop if file fails */
328 static int handle_luainit (lua_State
*L
) {
330 const char *init
= getenv(LUA_INIT
);
332 const char *init
= NULL
;
334 if (init
== NULL
) return 0; /* status OK */
335 else if (init
[0] == '@')
336 return dofile(L
, init
+1);
338 return dostring(L
, init
, "=" LUA_INIT
);
349 static int pmain (lua_State
*L
) {
350 struct Smain
*s
= (struct Smain
*)lua_touserdata(L
, 1);
351 char **argv
= s
->argv
;
353 int has_i
= 0, has_v
= 0, has_e
= 0;
355 if (argv
[0] && argv
[0][0]) progname
= argv
[0];
356 lua_gc(L
, LUA_GCSTOP
, 0); /* stop collector during initialization */
357 luaL_openlibs(L
); /* open libraries */
358 lua_gc(L
, LUA_GCRESTART
, 0);
359 s
->status
= handle_luainit(L
);
360 if (s
->status
!= 0) return 0;
361 script
= collectargs(argv
, &has_i
, &has_v
, &has_e
);
362 if (script
< 0) { /* invalid args? */
367 if (has_v
) print_version();
368 s
->status
= runargs(L
, argv
, (script
> 0) ? script
: s
->argc
);
369 if (s
->status
!= 0) return 0;
371 s
->status
= handle_script(L
, argv
, script
);
372 if (s
->status
!= 0) return 0;
375 else if (script
== 0 && !has_e
&& !has_v
) {
376 if (lua_stdin_is_tty()) {
380 else dofile(L
, NULL
); /* executes stdin as a file */
386 int main (int argc
, char **argv
) {
390 openconsole(&dev_stdcon_r
, &dev_stdcon_w
);
392 lua_State
*L
= lua_open(); /* create state */
394 l_message(argv
[0], "cannot create state: not enough memory");
399 status
= lua_cpcall(L
, &pmain
, &s
);
402 return (status
|| s
.status
) ? EXIT_FAILURE
: EXIT_SUCCESS
;