Imported from ../lua-3.2.tar.gz.
[lua.git] / src / lua / lua.c
blob5acd61733d2e25942fd0ef46ebe8f3a39ebd77f6
1 /*
2 ** $Id: lua.c,v 1.21 1999/07/02 18:22:38 roberto Exp $
3 ** Lua stand-alone interpreter
4 ** See Copyright Notice in lua.h
5 */
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
13 #include "lua.h"
14 #include "luadebug.h"
15 #include "lualib.h"
18 #ifdef _POSIX_SOURCE
19 #include <unistd.h>
20 #else
21 #define isatty(x) (x==0) /* assume stdin is a tty */
22 #endif
25 typedef void (*handler)(int); /* type for signal actions */
27 static void laction (int i);
30 static lua_LHFunction old_linehook = NULL;
31 static lua_CHFunction old_callhook = NULL;
34 static handler lreset (void) {
35 return signal(SIGINT, laction);
39 static void lstop (void) {
40 lua_setlinehook(old_linehook);
41 lua_setcallhook(old_callhook);
42 lreset();
43 lua_error("interrupted!");
47 static void laction (int i) {
48 signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
49 terminate process (default action) */
50 old_linehook = lua_setlinehook((lua_LHFunction)lstop);
51 old_callhook = lua_setcallhook((lua_CHFunction)lstop);
55 static int ldo (int (*f)(char *), char *name) {
56 int res;
57 handler h = lreset();
58 res = f(name); /* dostring | dofile */
59 signal(SIGINT, h); /* restore old action */
60 return res;
64 static void print_message (void) {
65 fprintf(stderr,
66 "Lua: command line options:\n"
67 " -v print version information\n"
68 " -d turn debug on\n"
69 " -e stat dostring `stat'\n"
70 " -q interactive mode without prompt\n"
71 " -i interactive mode with prompt\n"
72 " - executes stdin as a file\n"
73 " a=b sets global `a' with string `b'\n"
74 " name dofile `name'\n\n");
78 static void assign (char *arg) {
79 if (strlen(arg) >= 500)
80 fprintf(stderr, "lua: shell argument too long");
81 else {
82 char buffer[500];
83 char *eq = strchr(arg, '=');
84 lua_pushstring(eq+1);
85 strncpy(buffer, arg, eq-arg);
86 buffer[eq-arg] = 0;
87 lua_setglobal(buffer);
92 static void manual_input (int prompt) {
93 int cont = 1;
94 while (cont) {
95 char buffer[BUFSIZ];
96 int i = 0;
97 lua_beginblock();
98 if (prompt)
99 printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
100 for(;;) {
101 int c = getchar();
102 if (c == EOF) {
103 cont = 0;
104 break;
106 else if (c == '\n') {
107 if (i>0 && buffer[i-1] == '\\')
108 buffer[i-1] = '\n';
109 else break;
111 else if (i >= BUFSIZ-1) {
112 fprintf(stderr, "lua: argument line too long\n");
113 break;
115 else buffer[i++] = (char)c;
117 buffer[i] = '\0';
118 ldo(lua_dostring, buffer);
119 lua_endblock();
121 printf("\n");
125 int main (int argc, char *argv[])
127 int i;
128 lua_open();
129 lua_pushstring("> "); lua_setglobal("_PROMPT");
130 lua_userinit();
131 if (argc < 2) { /* no arguments? */
132 if (isatty(0)) {
133 printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
134 manual_input(1);
136 else
137 ldo(lua_dofile, NULL); /* executes stdin as a file */
139 else for (i=1; i<argc; i++) {
140 if (argv[i][0] == '-') { /* option? */
141 switch (argv[i][1]) {
142 case 0:
143 ldo(lua_dofile, NULL); /* executes stdin as a file */
144 break;
145 case 'i':
146 manual_input(1);
147 break;
148 case 'q':
149 manual_input(0);
150 break;
151 case 'd':
152 lua_setdebug(1);
153 break;
154 case 'v':
155 printf("%s %s\n(written by %s)\n\n",
156 LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
157 break;
158 case 'e':
159 i++;
160 if (ldo(lua_dostring, argv[i]) != 0) {
161 fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
162 return 1;
164 break;
165 default:
166 print_message();
167 exit(1);
170 else if (strchr(argv[i], '='))
171 assign(argv[i]);
172 else {
173 int result = ldo(lua_dofile, argv[i]);
174 if (result) {
175 if (result == 2) {
176 fprintf(stderr, "lua: cannot execute file ");
177 perror(argv[i]);
179 exit(1);
183 #ifdef DEBUG
184 lua_close();
185 #endif
186 return 0;