1 /* $NetBSD: lua.c,v 1.1.1.2 2012/03/15 00:08:11 alnsn Exp $ */
4 ** $Id: lua.c,v 1.1.1.2 2012/03/15 00:08:11 alnsn Exp $
5 ** Lua stand-alone interpreter
6 ** See Copyright Notice in lua.h
24 static lua_State
*globalL
= NULL
;
26 static const char *progname
= LUA_PROGNAME
;
30 static void lstop (lua_State
*L
, lua_Debug
*ar
) {
31 (void)ar
; /* unused arg. */
32 lua_sethook(L
, NULL
, 0, 0);
33 luaL_error(L
, "interrupted!");
37 static void laction (int i
) {
38 signal(i
, SIG_DFL
); /* if another SIGINT happens before lstop,
39 terminate process (default action) */
40 lua_sethook(globalL
, lstop
, LUA_MASKCALL
| LUA_MASKRET
| LUA_MASKCOUNT
, 1);
44 static void print_usage (void) {
46 "usage: %s [options] [script [args]].\n"
47 "Available options are:\n"
48 " -e stat execute string " LUA_QL("stat") "\n"
49 " -l name require library " LUA_QL("name") "\n"
50 " -i enter interactive mode after executing " LUA_QL("script") "\n"
51 " -v show version information\n"
52 " -- stop handling options\n"
53 " - execute stdin and stop handling options\n"
60 static void l_message (const char *pname
, const char *msg
) {
61 if (pname
) fprintf(stderr
, "%s: ", pname
);
62 fprintf(stderr
, "%s\n", msg
);
67 static int report (lua_State
*L
, int status
) {
68 if (status
&& !lua_isnil(L
, -1)) {
69 const char *msg
= lua_tostring(L
, -1);
70 if (msg
== NULL
) msg
= "(error object is not a string)";
71 l_message(progname
, msg
);
78 static int traceback (lua_State
*L
) {
79 if (!lua_isstring(L
, 1)) /* 'message' not a string? */
80 return 1; /* keep it intact */
81 lua_getfield(L
, LUA_GLOBALSINDEX
, "debug");
82 if (!lua_istable(L
, -1)) {
86 lua_getfield(L
, -1, "traceback");
87 if (!lua_isfunction(L
, -1)) {
91 lua_pushvalue(L
, 1); /* pass error message */
92 lua_pushinteger(L
, 2); /* skip this function and traceback */
93 lua_call(L
, 2, 1); /* call debug.traceback */
98 static int docall (lua_State
*L
, int narg
, int clear
) {
100 int base
= lua_gettop(L
) - narg
; /* function index */
101 lua_pushcfunction(L
, traceback
); /* push traceback function */
102 lua_insert(L
, base
); /* put it under chunk and args */
103 signal(SIGINT
, laction
);
104 status
= lua_pcall(L
, narg
, (clear
? 0 : LUA_MULTRET
), base
);
105 signal(SIGINT
, SIG_DFL
);
106 lua_remove(L
, base
); /* remove traceback function */
107 /* force a complete garbage collection in case of errors */
108 if (status
!= 0) lua_gc(L
, LUA_GCCOLLECT
, 0);
113 static void print_version (void) {
114 l_message(NULL
, LUA_RELEASE
" " LUA_COPYRIGHT
);
118 static int getargs (lua_State
*L
, char **argv
, int n
) {
122 while (argv
[argc
]) argc
++; /* count total number of arguments */
123 narg
= argc
- (n
+ 1); /* number of arguments to the script */
124 luaL_checkstack(L
, narg
+ 3, "too many arguments to script");
125 for (i
=n
+1; i
< argc
; i
++)
126 lua_pushstring(L
, argv
[i
]);
127 lua_createtable(L
, narg
, n
+ 1);
128 for (i
=0; i
< argc
; i
++) {
129 lua_pushstring(L
, argv
[i
]);
130 lua_rawseti(L
, -2, i
- n
);
136 static int dofile (lua_State
*L
, const char *name
) {
137 int status
= luaL_loadfile(L
, name
) || docall(L
, 0, 1);
138 return report(L
, status
);
142 static int dostring (lua_State
*L
, const char *s
, const char *name
) {
143 int status
= luaL_loadbuffer(L
, s
, strlen(s
), name
) || docall(L
, 0, 1);
144 return report(L
, status
);
148 static int dolibrary (lua_State
*L
, const char *name
) {
149 lua_getglobal(L
, "require");
150 lua_pushstring(L
, name
);
151 return report(L
, docall(L
, 1, 1));
155 static const char *get_prompt (lua_State
*L
, int firstline
) {
157 lua_getfield(L
, LUA_GLOBALSINDEX
, firstline
? "_PROMPT" : "_PROMPT2");
158 p
= lua_tostring(L
, -1);
159 if (p
== NULL
) p
= (firstline
? LUA_PROMPT
: LUA_PROMPT2
);
160 lua_pop(L
, 1); /* remove global */
165 static int incomplete (lua_State
*L
, int status
) {
166 if (status
== LUA_ERRSYNTAX
) {
168 const char *msg
= lua_tolstring(L
, -1, &lmsg
);
169 const char *tp
= msg
+ lmsg
- (sizeof(LUA_QL("<eof>")) - 1);
170 if (strstr(msg
, LUA_QL("<eof>")) == tp
) {
175 return 0; /* else... */
179 static int pushline (lua_State
*L
, int firstline
) {
180 char buffer
[LUA_MAXINPUT
];
183 const char *prmt
= get_prompt(L
, firstline
);
184 if (lua_readline(L
, b
, prmt
) == 0)
185 return 0; /* no input */
187 if (l
> 0 && b
[l
-1] == '\n') /* line ends with newline? */
188 b
[l
-1] = '\0'; /* remove it */
189 if (firstline
&& b
[0] == '=') /* first line starts with `=' ? */
190 lua_pushfstring(L
, "return %s", b
+1); /* change it to `return' */
192 lua_pushstring(L
, b
);
198 static int loadline (lua_State
*L
) {
202 return -1; /* no input */
203 for (;;) { /* repeat until gets a complete line */
204 status
= luaL_loadbuffer(L
, lua_tostring(L
, 1), lua_strlen(L
, 1), "=stdin");
205 if (!incomplete(L
, status
)) break; /* cannot try to add lines? */
206 if (!pushline(L
, 0)) /* no more input? */
208 lua_pushliteral(L
, "\n"); /* add a new line... */
209 lua_insert(L
, -2); /* ...between the two lines */
210 lua_concat(L
, 3); /* join them */
213 lua_remove(L
, 1); /* remove line */
218 static void dotty (lua_State
*L
) {
220 const char *oldprogname
= progname
;
222 while ((status
= loadline(L
)) != -1) {
223 if (status
== 0) status
= docall(L
, 0, 0);
225 if (status
== 0 && lua_gettop(L
) > 0) { /* any result to print? */
226 lua_getglobal(L
, "print");
228 if (lua_pcall(L
, lua_gettop(L
)-1, 0, 0) != 0)
229 l_message(progname
, lua_pushfstring(L
,
230 "error calling " LUA_QL("print") " (%s)",
231 lua_tostring(L
, -1)));
234 lua_settop(L
, 0); /* clear stack */
237 progname
= oldprogname
;
241 static int handle_script (lua_State
*L
, char **argv
, int n
) {
244 int narg
= getargs(L
, argv
, n
); /* collect arguments */
245 lua_setglobal(L
, "arg");
247 if (strcmp(fname
, "-") == 0 && strcmp(argv
[n
-1], "--") != 0)
248 fname
= NULL
; /* stdin */
249 status
= luaL_loadfile(L
, fname
);
250 lua_insert(L
, -(narg
+1));
252 status
= docall(L
, narg
, 0);
255 return report(L
, status
);
259 /* check that argument has no extra characters at the end */
260 #define notail(x) {if ((x)[2] != '\0') return -1;}
263 static int collectargs (char **argv
, int *pi
, int *pv
, int *pe
) {
265 for (i
= 1; argv
[i
] != NULL
; i
++) {
266 if (argv
[i
][0] != '-') /* not an option? */
268 switch (argv
[i
][1]) { /* option */
271 return (argv
[i
+1] != NULL
? i
+1 : 0);
276 *pi
= 1; /* go through */
282 *pe
= 1; /* go through */
284 if (argv
[i
][2] == '\0') {
286 if (argv
[i
] == NULL
) return -1;
289 default: return -1; /* invalid option */
296 static int runargs (lua_State
*L
, char **argv
, int n
) {
298 for (i
= 1; i
< n
; i
++) {
299 if (argv
[i
] == NULL
) continue;
300 lua_assert(argv
[i
][0] == '-');
301 switch (argv
[i
][1]) { /* option */
303 const char *chunk
= argv
[i
] + 2;
304 if (*chunk
== '\0') chunk
= argv
[++i
];
305 lua_assert(chunk
!= NULL
);
306 if (dostring(L
, chunk
, "=(command line)") != 0)
311 const char *filename
= argv
[i
] + 2;
312 if (*filename
== '\0') filename
= argv
[++i
];
313 lua_assert(filename
!= NULL
);
314 if (dolibrary(L
, filename
))
315 return 1; /* stop if file fails */
325 static int handle_luainit (lua_State
*L
) {
326 const char *init
= getenv(LUA_INIT
);
327 if (init
== NULL
) return 0; /* status OK */
328 else if (init
[0] == '@')
329 return dofile(L
, init
+1);
331 return dostring(L
, init
, "=" LUA_INIT
);
342 static int pmain (lua_State
*L
) {
343 struct Smain
*s
= (struct Smain
*)lua_touserdata(L
, 1);
344 char **argv
= s
->argv
;
346 int has_i
= 0, has_v
= 0, has_e
= 0;
348 if (argv
[0] && argv
[0][0]) progname
= argv
[0];
349 lua_gc(L
, LUA_GCSTOP
, 0); /* stop collector during initialization */
350 luaL_openlibs(L
); /* open libraries */
351 lua_gc(L
, LUA_GCRESTART
, 0);
352 s
->status
= handle_luainit(L
);
353 if (s
->status
!= 0) return 0;
354 script
= collectargs(argv
, &has_i
, &has_v
, &has_e
);
355 if (script
< 0) { /* invalid args? */
360 if (has_v
) print_version();
361 s
->status
= runargs(L
, argv
, (script
> 0) ? script
: s
->argc
);
362 if (s
->status
!= 0) return 0;
364 s
->status
= handle_script(L
, argv
, script
);
365 if (s
->status
!= 0) return 0;
368 else if (script
== 0 && !has_e
&& !has_v
) {
369 if (lua_stdin_is_tty()) {
373 else dofile(L
, NULL
); /* executes stdin as a file */
379 int main (int argc
, char **argv
) {
382 lua_State
*L
= lua_open(); /* create state */
384 l_message(argv
[0], "cannot create state: not enough memory");
389 status
= lua_cpcall(L
, &pmain
, &s
);
392 return (status
|| s
.status
) ? EXIT_FAILURE
: EXIT_SUCCESS
;