2 ** $Id: lua.c,v 1.14 1998/02/11 20:56:05 roberto Exp $
3 ** Lua stand-alone interpreter
4 ** See Copyright Notice in lua.h
21 #define setlocale(a,b) 0
27 #define isatty(x) (x==0) /* assume stdin is a tty */
31 typedef void (*handler
)(int); /* type for signal actions */
33 static void laction (int i
);
35 static handler
lreset (void)
39 return signal(SIGINT
, laction
);
42 static void lstop (void)
45 lua_error("interrupted!");
48 static void laction (int i
)
50 lua_linehook
= (lua_LHFunction
)lstop
;
51 lua_callhook
= (lua_CHFunction
)lstop
;
54 static int ldo (int (*f
)(char *), char *name
)
58 res
= f(name
); /* dostring | dofile */
59 signal(SIGINT
, h
); /* restore old action */
64 static void print_message (void)
67 "Lua: command line options:\n"
68 " -v print version information\n"
70 " -e stat dostring `stat'\n"
71 " -q interactive mode without prompt\n"
72 " -i interactive mode with prompt\n"
73 " - executes stdin as a file\n"
74 " a=b sets global `a' with string `b'\n"
75 " name dofile `name'\n\n");
79 static void assign (char *arg
)
81 if (strlen(arg
) >= 500)
82 fprintf(stderr
, "lua: shell argument too long");
85 char *eq
= strchr(arg
, '=');
87 strncpy(buffer
, arg
, eq
-arg
);
89 lua_setglobal(buffer
);
95 static void manual_input (int prompt
)
99 char buffer
[BUF_SIZE
];
103 printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
110 else if (c
== '\n') {
111 if (i
>0 && buffer
[i
-1] == '\\')
115 else if (i
>= BUF_SIZE
-1) {
116 fprintf(stderr
, "lua: argument line too long\n");
119 else buffer
[i
++] = c
;
122 ldo(lua_dostring
, buffer
);
129 int main (int argc
, char *argv
[])
132 setlocale(LC_ALL
, "");
136 lua_pushstring("> "); lua_setglobal("_PROMPT");
137 if (argc
< 2) { /* no arguments? */
139 printf("%s %s\n", LUA_VERSION
, LUA_COPYRIGHT
);
143 ldo(lua_dofile
, NULL
); /* executes stdin as a file */
145 else for (i
=1; i
<argc
; i
++) {
146 if (argv
[i
][0] == '-') { /* option? */
147 switch (argv
[i
][1]) {
149 ldo(lua_dofile
, NULL
); /* executes stdin as a file */
161 printf("%s %s\n(written by %s)\n\n",
162 LUA_VERSION
, LUA_COPYRIGHT
, LUA_AUTHORS
);
166 if (ldo(lua_dostring
, argv
[i
]) != 0) {
167 fprintf(stderr
, "lua: error running argument `%s'\n", argv
[i
]);
176 else if (strchr(argv
[i
], '='))
179 int result
= ldo(lua_dofile
, argv
[i
]);
182 fprintf(stderr
, "lua: cannot execute file ");