lua/lua.c

376 lines
8.4 KiB
C
Raw Normal View History

1993-07-28 21:18:00 +08:00
/*
2002-05-24 03:43:04 +08:00
** $Id: lua.c,v 1.87 2002/05/16 19:09:19 roberto Exp roberto $
1997-09-17 03:25:59 +08:00
** Lua stand-alone interpreter
** See Copyright Notice in lua.h
1993-07-28 21:18:00 +08:00
*/
1993-12-18 02:41:19 +08:00
#include <signal.h>
1993-07-28 21:18:00 +08:00
#include <stdio.h>
1998-01-20 03:49:49 +08:00
#include <stdlib.h>
1995-02-08 00:04:15 +08:00
#include <string.h>
1993-07-28 21:18:00 +08:00
#include "lua.h"
2002-02-15 06:23:43 +08:00
#include "lauxlib.h"
1997-09-17 03:25:59 +08:00
#include "luadebug.h"
1993-07-28 21:18:00 +08:00
#include "lualib.h"
2000-08-29 01:57:04 +08:00
#ifdef _POSIX_SOURCE
#include <unistd.h>
#else
static int isatty (int x) { return x==0; } /* assume stdin is a tty */
#endif
2000-08-10 03:16:57 +08:00
#ifndef LUA_PROGNAME
2002-04-23 22:59:22 +08:00
#define LUA_PROGNAME "lua"
#endif
1999-12-20 21:03:20 +08:00
#ifndef PROMPT
#define PROMPT "> "
1999-12-20 21:03:20 +08:00
#endif
2002-02-08 01:27:12 +08:00
#ifndef PROMPT2
#define PROMPT2 ">> "
#endif
#ifndef LUA_USERINIT
#define LUA_USERINIT(L) openstdlibs(L)
#endif
static lua_State *L = NULL;
2000-03-31 01:19:48 +08:00
static lua_Hook old_linehook = NULL;
static lua_Hook old_callhook = NULL;
static void lstop (void) {
2000-08-29 01:57:04 +08:00
lua_setlinehook(L, old_linehook);
lua_setcallhook(L, old_callhook);
lua_error(L, "interrupted!");
}
static void laction (int i) {
2002-04-01 22:42:33 +08:00
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
terminate process (default action) */
2000-08-29 01:57:04 +08:00
old_linehook = lua_setlinehook(L, (lua_Hook)lstop);
old_callhook = lua_setcallhook(L, (lua_Hook)lstop);
}
2002-05-02 04:48:12 +08:00
static void report (int status) {
2002-05-24 03:43:04 +08:00
if (status) {
2002-05-17 03:09:19 +08:00
lua_getglobal(L, "_ALERT");
lua_pushvalue(L, -2);
lua_pcall(L, 1, 0, 0);
2002-05-02 04:48:12 +08:00
lua_pop(L, 1);
}
2002-02-08 01:27:12 +08:00
}
2002-05-02 04:48:12 +08:00
static int lcall (int clear) {
int status;
int top = lua_gettop(L);
2002-05-02 04:48:12 +08:00
lua_getglobal(L, "_ERRORMESSAGE");
lua_insert(L, top);
2002-02-08 01:27:12 +08:00
signal(SIGINT, laction);
2002-05-02 04:48:12 +08:00
status = lua_pcall(L, 0, LUA_MULTRET, top);
2002-02-08 01:27:12 +08:00
signal(SIGINT, SIG_DFL);
2002-05-02 04:48:12 +08:00
if (status == 0) {
if (clear) lua_settop(L, top); /* remove eventual results */
else lua_remove(L, top); /* else remove only error function */
}
return status;
}
2002-02-08 01:27:12 +08:00
static void print_usage (void) {
1997-12-23 02:05:23 +08:00
fprintf(stderr,
2002-04-23 22:59:22 +08:00
"usage: %s [options]. Available options are:\n"
" - execute stdin as a file\n"
" -c close Lua when exiting\n"
" -e stat execute string `stat'\n"
" -f name execute file `name' with remaining arguments in table `arg'\n"
2002-02-08 01:27:12 +08:00
" -i enter interactive mode\n"
" -v print version information\n"
" a=b set global `a' to string `b'\n"
2002-04-23 22:59:22 +08:00
" name execute file `name'\n",
LUA_PROGNAME);
1999-12-31 02:29:46 +08:00
}
static void print_version (void) {
printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT);
1997-12-23 02:05:23 +08:00
}
1997-12-20 02:34:23 +08:00
static void assign (char *arg) {
char *eq = strchr(arg, '=');
*eq = '\0'; /* spilt `arg' in two strings (name & value) */
2000-08-29 01:57:04 +08:00
lua_pushstring(L, eq+1);
lua_setglobal(L, arg);
}
static void getargs (char *argv[]) {
2000-04-15 01:46:29 +08:00
int i;
2000-08-29 01:57:04 +08:00
lua_newtable(L);
for (i=0; argv[i]; i++) {
/* arg[i] = argv[i] */
2000-09-06 03:33:32 +08:00
lua_pushnumber(L, i);
lua_pushstring(L, argv[i]);
2002-02-08 01:27:12 +08:00
lua_rawset(L, -3);
}
/* arg.n = maximum index in table `arg' */
2002-02-08 01:27:12 +08:00
lua_pushliteral(L, "n");
2000-09-06 03:33:32 +08:00
lua_pushnumber(L, i-1);
2002-02-08 01:27:12 +08:00
lua_rawset(L, -3);
2000-04-15 01:46:29 +08:00
}
2002-05-17 03:09:19 +08:00
static int l_alert (lua_State *l) {
fputs(luaL_check_string(l, 1), stderr);
putc('\n', stderr);
return 0;
}
2000-08-29 01:57:04 +08:00
static int l_getargs (lua_State *l) {
char **argv = (char **)lua_touserdata(l, lua_upvalueindex(1));
2000-08-29 01:57:04 +08:00
getargs(argv);
return 1;
}
2002-05-24 03:43:04 +08:00
static int docall (int status) {
2002-05-02 04:48:12 +08:00
if (status == 0) status = lcall(1);
report(status);
return status;
}
2002-05-24 03:43:04 +08:00
static int file_input (const char *name) {
return docall(lua_loadfile(L, name));
}
2002-05-16 02:57:44 +08:00
static int dostring (const char *s, const char *name) {
2002-05-24 03:43:04 +08:00
return docall(lua_loadbuffer(L, s, strlen(s), name));
}
2000-08-10 03:16:57 +08:00
2002-02-08 01:27:12 +08:00
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#define save_line(b) if (strcspn(b, " \t\n") != 0) add_history(b)
#define push_line(b) if (incomplete) lua_pushstring(L, "\n"); lua_pushstring(L, b); free(b)
#else
#define save_line(b)
#define push_line(b) lua_pushstring(L, b)
/* maximum length of an input line */
#ifndef MAXINPUT
#define MAXINPUT 512
#endif
2002-02-08 01:27:12 +08:00
static char *readline (const char *prompt) {
static char buffer[MAXINPUT];
if (prompt) {
fputs(prompt, stdout);
fflush(stdout);
2001-02-21 02:15:33 +08:00
}
2002-02-08 01:27:12 +08:00
return fgets(buffer, sizeof(buffer), stdin);
}
2002-02-08 01:27:12 +08:00
#endif
2002-05-02 04:48:12 +08:00
static const char *get_prompt (int firstline) {
2002-02-08 01:27:12 +08:00
const char *p = NULL;
2002-05-02 04:48:12 +08:00
lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
2002-02-08 01:27:12 +08:00
p = lua_tostring(L, -1);
2002-05-02 04:48:12 +08:00
if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
2002-02-08 01:27:12 +08:00
lua_pop(L, 1); /* remove global */
return p;
}
2002-05-02 04:48:12 +08:00
static int incomplete (int status) {
if (status == LUA_ERRSYNTAX &&
2002-05-16 02:57:44 +08:00
strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
2002-05-02 04:48:12 +08:00
lua_pop(L, 1);
return 1;
}
else
return 0;
2002-02-08 01:27:12 +08:00
}
2002-02-15 05:47:50 +08:00
static int load_string (void) {
2002-05-02 04:48:12 +08:00
int firstline = 1;
int status;
lua_settop(L, 0);
do { /* repeat until gets a complete line */
char *buffer = readline(get_prompt(firstline));
2002-02-08 01:27:12 +08:00
if (buffer == NULL) { /* input end? */
2002-05-02 04:48:12 +08:00
lua_settop(L, 0);
return -1; /* input end */
2002-02-08 01:27:12 +08:00
}
2002-05-02 04:48:12 +08:00
if (firstline && buffer[0] == '=') {
2002-02-08 01:27:12 +08:00
buffer[0] = ' ';
lua_pushstring(L, "return");
}
2002-05-02 04:48:12 +08:00
firstline = 0;
2002-02-08 01:27:12 +08:00
push_line(buffer);
2002-05-02 04:48:12 +08:00
lua_concat(L, lua_gettop(L));
status = lua_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
} while (incomplete(status)); /* repeat loop to get rest of `line' */
save_line(lua_tostring(L, 1));
lua_remove(L, 1);
return status;
2002-02-08 01:27:12 +08:00
}
static void manual_input (int version) {
2002-05-02 04:48:12 +08:00
int status;
2002-02-08 01:27:12 +08:00
if (version) print_version();
2002-05-02 04:48:12 +08:00
while ((status = load_string()) != -1) {
if (status == 0) status = lcall(0);
report(status);
if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
lua_getglobal(L, "print");
2002-05-02 04:48:12 +08:00
lua_insert(L, 1);
lua_call(L, lua_gettop(L)-1, 0);
}
}
2002-02-08 01:27:12 +08:00
printf("\n");
}
static int handle_argv (char *argv[], int *toclose) {
2000-08-10 03:16:57 +08:00
if (*argv == NULL) { /* no more arguments? */
1997-12-20 02:34:23 +08:00
if (isatty(0)) {
2002-02-08 01:27:12 +08:00
manual_input(1);
1997-12-20 02:34:23 +08:00
}
else
2002-05-02 04:48:12 +08:00
file_input(NULL); /* executes stdin as a file */
1997-10-06 22:51:32 +08:00
}
2000-08-10 03:16:57 +08:00
else { /* other arguments; loop over them */
int i;
for (i = 0; argv[i] != NULL; i++) {
if (argv[i][0] != '-') { /* not an option? */
if (strchr(argv[i], '='))
2000-08-10 03:16:57 +08:00
assign(argv[i]);
else
2002-05-02 04:48:12 +08:00
if (file_input(argv[i]))
2000-08-10 03:16:57 +08:00
return EXIT_FAILURE; /* stop if file fails */
}
else switch (argv[i][1]) { /* option */
2002-02-08 01:27:12 +08:00
case '\0': {
2002-05-02 04:48:12 +08:00
file_input(NULL); /* executes stdin as a file */
2000-08-10 03:16:57 +08:00
break;
1999-12-31 02:29:46 +08:00
}
case 'i': {
2002-02-08 01:27:12 +08:00
manual_input(0);
2000-08-10 03:16:57 +08:00
break;
}
case 'c': {
*toclose = 1;
2000-08-10 03:16:57 +08:00
break;
1997-12-23 02:05:23 +08:00
}
case 'v': {
2000-08-10 03:16:57 +08:00
print_version();
break;
}
case 'e': {
2000-08-10 03:16:57 +08:00
i++;
if (argv[i] == NULL) {
2002-02-08 01:27:12 +08:00
print_usage();
2000-08-10 03:16:57 +08:00
return EXIT_FAILURE;
}
2002-05-16 02:57:44 +08:00
if (dostring(argv[i], "=prog. argument") != 0) {
2002-04-23 22:59:22 +08:00
fprintf(stderr, "%s: error running argument `%.99s'\n",
LUA_PROGNAME, argv[i]);
2000-08-10 03:16:57 +08:00
return EXIT_FAILURE;
}
break;
}
case 'f': {
2000-08-10 03:16:57 +08:00
i++;
if (argv[i] == NULL) {
2002-02-08 01:27:12 +08:00
print_usage();
2000-08-10 03:16:57 +08:00
return EXIT_FAILURE;
}
2000-08-29 01:57:04 +08:00
getargs(argv+i); /* collect remaining arguments */
lua_setglobal(L, "arg");
2000-08-10 03:16:57 +08:00
return file_input(argv[i]); /* stop scanning arguments */
}
case 's': {
2002-04-23 22:59:22 +08:00
fprintf(stderr,
"%s: option `-s' is deprecated (dynamic stack now)\n",
LUA_PROGNAME);
2002-02-08 01:27:12 +08:00
break;
2000-08-10 03:16:57 +08:00
}
default: {
2002-02-08 01:27:12 +08:00
print_usage();
2000-08-10 03:16:57 +08:00
return EXIT_FAILURE;
}
2000-08-10 03:16:57 +08:00
}
}
}
2000-08-10 03:16:57 +08:00
return EXIT_SUCCESS;
}
2002-05-17 03:09:19 +08:00
static void register_own (char *argv[]) {
lua_pushudataval(L, argv);
2000-08-29 01:57:04 +08:00
lua_pushcclosure(L, l_getargs, 1);
lua_setglobal(L, "getargs");
2002-05-17 03:09:19 +08:00
lua_register(L, "_ALERT", l_alert);
2000-08-10 03:16:57 +08:00
}
static void openstdlibs (lua_State *l) {
lua_baselibopen(l);
lua_tablibopen(l);
lua_iolibopen(l);
lua_strlibopen(l);
lua_mathlibopen(l);
lua_dblibopen(l);
}
2002-05-16 02:57:44 +08:00
static int handle_luainit (void) {
const char *init = getenv("LUA_INIT");
if (init == NULL) return 0; /* status OK */
else if (init[0] == '@')
return file_input(init+1);
else
return dostring(init, "=LUA_INIT");
}
int main (int argc, char *argv[]) {
2000-08-10 03:16:57 +08:00
int status;
int toclose = 0;
2002-02-15 05:47:50 +08:00
(void)argc; /* to avoid warnings */
L = lua_open(); /* create state */
LUA_USERINIT(L); /* open libraries */
2002-05-17 03:09:19 +08:00
register_own(argv); /* create own function */
2002-05-16 02:57:44 +08:00
status = handle_luainit();
if (status != 0) return status;
status = handle_argv(argv+1, &toclose);
if (toclose)
2000-08-29 01:57:04 +08:00
lua_close(L);
2000-05-11 01:00:21 +08:00
return status;
1993-07-28 21:18:00 +08:00
}