2 ** $Id: lua.c,v 1.55 2000/10/20 16:36:32 roberto Exp $
3 ** Lua stand-alone interpreter
4 ** See Copyright Notice in lua.h
19 static lua_State
*L
= NULL
;
29 static int isatty (int x
) { return x
==0; } /* assume stdin is a tty */
42 typedef void (*handler
)(int); /* type for signal actions */
44 static void laction (int i
);
47 static lua_Hook old_linehook
= NULL
;
48 static lua_Hook old_callhook
= NULL
;
51 static void userinit (void) {
57 /* add your libraries here */
61 static handler
lreset (void) {
62 return signal(SIGINT
, laction
);
66 static void lstop (void) {
67 lua_setlinehook(L
, old_linehook
);
68 lua_setcallhook(L
, old_callhook
);
70 lua_error(L
, "interrupted!");
74 static void laction (int i
) {
75 (void)i
; /* to avoid warnings */
76 signal(SIGINT
, SIG_DFL
); /* if another SIGINT happens before lstop,
77 terminate process (default action) */
78 old_linehook
= lua_setlinehook(L
, (lua_Hook
)lstop
);
79 old_callhook
= lua_setcallhook(L
, (lua_Hook
)lstop
);
83 static int ldo (int (*f
)(lua_State
*l
, const char *), const char *name
) {
86 int top
= lua_gettop(L
);
87 res
= f(L
, name
); /* dostring | dofile */
88 lua_settop(L
, top
); /* remove eventual results */
89 signal(SIGINT
, h
); /* restore old action */
90 /* Lua gives no message in such cases, so lua.c provides one */
91 if (res
== LUA_ERRMEM
) {
92 fprintf(stderr
, "lua: memory allocation error\n");
94 else if (res
== LUA_ERRERR
)
95 fprintf(stderr
, "lua: error in error message\n");
100 static void print_message (void) {
102 "usage: lua [options]. Available options are:\n"
103 " - execute stdin as a file\n"
104 " -c close Lua when exiting\n"
105 " -e stat execute string `stat'\n"
106 " -f name execute file `name' with remaining arguments in table `arg'\n"
107 " -i enter interactive mode with prompt\n"
108 " -q enter interactive mode without prompt\n"
109 " -sNUM set stack size to NUM (must be the first option)\n"
110 " -v print version information\n"
111 " a=b set global `a' to string `b'\n"
112 " name execute file `name'\n"
117 static void print_version (void) {
118 printf("%.80s %.80s\n", LUA_VERSION
, LUA_COPYRIGHT
);
122 static void assign (char *arg
) {
123 char *eq
= strchr(arg
, '=');
124 *eq
= '\0'; /* spilt `arg' in two strings (name & value) */
125 lua_pushstring(L
, eq
+1);
126 lua_setglobal(L
, arg
);
130 static void getargs (char *argv
[]) {
133 for (i
=0; argv
[i
]; i
++) {
134 /* arg[i] = argv[i] */
135 lua_pushnumber(L
, i
);
136 lua_pushstring(L
, argv
[i
]);
139 /* arg.n = maximum index in table `arg' */
140 lua_pushstring(L
, "n");
141 lua_pushnumber(L
, i
-1);
146 static int l_getargs (lua_State
*l
) {
147 char **argv
= (char **)lua_touserdata(l
, -1);
153 static int file_input (const char *argv
) {
154 int result
= ldo(lua_dofile
, argv
);
156 if (result
== LUA_ERRFILE
) {
157 fprintf(stderr
, "lua: cannot execute file ");
167 /* maximum length of an input string */
169 #define MAXINPUT BUFSIZ
172 static void manual_input (int version
, int prompt
) {
174 if (version
) print_version();
176 char buffer
[MAXINPUT
];
180 lua_getglobal(L
, "_PROMPT");
181 s
= lua_tostring(L
, -1);
184 lua_pop(L
, 1); /* remove global */
192 else if (c
== '\n') {
193 if (i
>0 && buffer
[i
-1] == '\\')
197 else if (i
>= MAXINPUT
-1) {
198 fprintf(stderr
, "lua: input line too long\n");
201 else buffer
[i
++] = (char)c
;
204 ldo(lua_dostring
, buffer
);
205 lua_settop(L
, 0); /* remove eventual results */
211 static int handle_argv (char *argv
[], struct Options
*opt
) {
212 if (opt
->stacksize
> 0) argv
++; /* skip option `-s' (if present) */
213 if (*argv
== NULL
) { /* no more arguments? */
218 ldo(lua_dofile
, NULL
); /* executes stdin as a file */
220 else { /* other arguments; loop over them */
222 for (i
= 0; argv
[i
] != NULL
; i
++) {
223 if (argv
[i
][0] != '-') { /* not an option? */
224 if (strchr(argv
[i
], '='))
227 if (file_input(argv
[i
]) != EXIT_SUCCESS
)
228 return EXIT_FAILURE
; /* stop if file fails */
230 else switch (argv
[i
][1]) { /* option */
232 ldo(lua_dofile
, NULL
); /* executes stdin as a file */
253 if (argv
[i
] == NULL
) {
257 if (ldo(lua_dostring
, argv
[i
]) != 0) {
258 fprintf(stderr
, "lua: error running argument `%.99s'\n", argv
[i
]);
265 if (argv
[i
] == NULL
) {
269 getargs(argv
+i
); /* collect remaining arguments */
270 lua_setglobal(L
, "arg");
271 return file_input(argv
[i
]); /* stop scanning arguments */
274 fprintf(stderr
, "lua: stack size (`-s') must be the first option\n");
288 static void getstacksize (int argc
, char *argv
[], struct Options
*opt
) {
289 if (argc
>= 2 && argv
[1][0] == '-' && argv
[1][1] == 's') {
290 int stacksize
= atoi(&argv
[1][2]);
291 if (stacksize
<= 0) {
292 fprintf(stderr
, "lua: invalid stack size ('%.20s')\n", &argv
[1][2]);
295 opt
->stacksize
= stacksize
;
298 opt
->stacksize
= 0; /* no stack size */
302 static void register_getargs (char *argv
[]) {
303 lua_pushuserdata(L
, argv
);
304 lua_pushcclosure(L
, l_getargs
, 1);
305 lua_setglobal(L
, "getargs");
309 int main (int argc
, char *argv
[]) {
313 getstacksize(argc
, argv
, &opt
); /* handle option `-s' */
314 L
= lua_open(opt
.stacksize
); /* create state */
315 userinit(); /* open libraries */
316 register_getargs(argv
); /* create `getargs' function */
317 status
= handle_argv(argv
+1, &opt
);