aboutsummaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-09 16:16:57 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-09 16:16:57 -0300
commite238efc5367a8d88757dd84df0683e1d8d9f9211 (patch)
treec460226a60d7598193fa4fe87d1736d80bd5bfab /lua.c
parentfdd04e7a7f624dad1a1443e08193241dea935287 (diff)
downloadlua-e238efc5367a8d88757dd84df0683e1d8d9f9211.tar.gz
lua-e238efc5367a8d88757dd84df0683e1d8d9f9211.tar.bz2
lua-e238efc5367a8d88757dd84df0683e1d8d9f9211.zip
default now is multi-state
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c205
1 files changed, 128 insertions, 77 deletions
diff --git a/lua.c b/lua.c
index a97530d6..2d363585 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.42 2000/06/30 19:17:08 roberto Exp roberto $ 2** $Id: lua.c,v 1.43 2000/08/04 19:38:35 roberto Exp roberto $
3** Lua stand-alone interpreter 3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -10,11 +10,15 @@
10#include <stdlib.h> 10#include <stdlib.h>
11#include <string.h> 11#include <string.h>
12 12
13#define LUA_SINGLESTATE
14
13#include "lua.h" 15#include "lua.h"
14 16
15#include "luadebug.h" 17#include "luadebug.h"
16#include "lualib.h" 18#include "lualib.h"
17 19
20lua_State *lua_state = NULL;
21
18 22
19#ifndef PROMPT 23#ifndef PROMPT
20#define PROMPT "> " 24#define PROMPT "> "
@@ -26,6 +30,16 @@
26static int isatty (int x) { return x==0; } /* assume stdin is a tty */ 30static int isatty (int x) { return x==0; } /* assume stdin is a tty */
27#endif 31#endif
28 32
33
34/*
35** global options
36*/
37struct Options {
38 int toclose;
39 int stacksize;
40};
41
42
29typedef void (*handler)(int); /* type for signal actions */ 43typedef void (*handler)(int); /* type for signal actions */
30 44
31static void laction (int i); 45static void laction (int i);
@@ -35,6 +49,19 @@ static lua_Hook old_linehook = NULL;
35static lua_Hook old_callhook = NULL; 49static lua_Hook old_callhook = NULL;
36 50
37 51
52#ifdef USERINIT
53extern void USERINIT (void);
54#else
55#define USERINIT userinit
56void userinit (void) {
57 lua_iolibopen();
58 lua_strlibopen();
59 lua_mathlibopen();
60 lua_dblibopen();
61}
62#endif
63
64
38static handler lreset (void) { 65static handler lreset (void) {
39 return signal(SIGINT, laction); 66 return signal(SIGINT, laction);
40} 67}
@@ -75,7 +102,6 @@ static void print_message (void) {
75 "usage: lua [options]. Available options are:\n" 102 "usage: lua [options]. Available options are:\n"
76 " - execute stdin as a file\n" 103 " - execute stdin as a file\n"
77 " -c close lua when exiting\n" 104 " -c close lua when exiting\n"
78 " -d turn debug on\n"
79 " -e stat execute string `stat'\n" 105 " -e stat execute string `stat'\n"
80 " -f name execute file `name' with remaining arguments in table `arg'\n" 106 " -f name execute file `name' with remaining arguments in table `arg'\n"
81 " -i enter interactive mode with prompt\n" 107 " -i enter interactive mode with prompt\n"
@@ -122,17 +148,20 @@ static void l_getargs (void) {
122} 148}
123 149
124 150
125static void file_input (const char *argv) { 151static int file_input (const char *argv) {
126 int result = ldo(lua_dofile, argv); 152 int result = ldo(lua_dofile, argv);
127 if (result) { 153 if (result) {
128 if (result == LUA_ERRFILE) { 154 if (result == LUA_ERRFILE) {
129 fprintf(stderr, "lua: cannot execute file "); 155 fprintf(stderr, "lua: cannot execute file ");
130 perror(argv); 156 perror(argv);
131 } 157 }
132 exit(1); 158 return EXIT_FAILURE;
133 } 159 }
160 else
161 return EXIT_SUCCESS;
134} 162}
135 163
164
136/* maximum length of an input string */ 165/* maximum length of an input string */
137#ifndef MAXINPUT 166#ifndef MAXINPUT
138#define MAXINPUT BUFSIZ 167#define MAXINPUT BUFSIZ
@@ -175,92 +204,114 @@ static void manual_input (int version, int prompt) {
175} 204}
176 205
177 206
178int main (int argc, char *argv[]) { 207static int handle_argv (char *argv[], struct Options *opt) {
179 int toclose = 0; 208 if (opt->stacksize > 0) argv++; /* skip option `-s' (if present) */
180 int status = EXIT_SUCCESS; 209 if (*argv == NULL) { /* no more arguments? */
181 int i = 1;
182 int stacksize = 0;
183 if (i < argc && argv[1][0] == '-' && argv[1][1] == 's') {
184 stacksize = atoi(&argv[1][2]);
185 if (stacksize == 0) {
186 fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]);
187 exit(EXIT_FAILURE);
188 }
189 i++;
190 }
191 lua_state = lua_newstate(stacksize, 1);
192 lua_userinit();
193 lua_pushuserdata(argv);
194 lua_pushcclosure(l_getargs, 1);
195 lua_setglobal("getargs");
196 if (i >= argc) { /* no other arguments? */
197 if (isatty(0)) { 210 if (isatty(0)) {
198 manual_input(1, 1); 211 manual_input(1, 1);
199 } 212 }
200 else 213 else
201 ldo(lua_dofile, NULL); /* executes stdin as a file */ 214 ldo(lua_dofile, NULL); /* executes stdin as a file */
202 } 215 }
203 else for (; i<argc; i++) { 216 else { /* other arguments; loop over them */
204 if (argv[i][0] == '-') { /* option? */ 217 int i;
205 switch (argv[i][1]) { 218 for (i = 0; argv[i] != NULL; i++) {
206 case 0: 219 if (argv[i][0] != '-') { /* not an option? */
207 ldo(lua_dofile, NULL); /* executes stdin as a file */ 220 if (strchr(argv[i], '='))
208 break; 221 assign(argv[i]);
209 case 'i': 222 else
210 manual_input(0, 1); 223 if (file_input(argv[i]) != EXIT_SUCCESS)
211 break; 224 return EXIT_FAILURE; /* stop if file fails */
212 case 'q': 225 }
213 manual_input(0, 0); 226 else switch (argv[i][1]) { /* option */
214 break; 227 case 0: {
215 case 'd': 228 ldo(lua_dofile, NULL); /* executes stdin as a file */
216 lua_setdebug(lua_state, 1); 229 break;
217 if (i+1 >= argc) { /* last argument? */
218 manual_input(1, 1);
219 } 230 }
220 break; 231 case 'i': {
221 case 'c': 232 manual_input(0, 1);
222 toclose = 1; 233 break;
223 break; 234 }
224 case 'v': 235 case 'q': {
225 print_version(); 236 manual_input(0, 0);
226 break; 237 break;
227 case 'e':
228 i++;
229 if (i >= argc) {
230 print_message();
231 status = EXIT_FAILURE; goto endloop;
232 } 238 }
233 if (ldo(lua_dostring, argv[i]) != 0) { 239 case 'c': {
234 fprintf(stderr, "lua: error running argument `%s'\n", argv[i]); 240 opt->toclose = 1;
235 status = EXIT_FAILURE; goto endloop; 241 break;
236 } 242 }
237 break; 243 case 'v': {
238 case 'f': 244 print_version();
239 i++; 245 break;
240 if (i >= argc) { 246 }
247 case 'e': {
248 i++;
249 if (argv[i] == NULL) {
250 print_message();
251 return EXIT_FAILURE;
252 }
253 if (ldo(lua_dostring, argv[i]) != 0) {
254 fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
255 return EXIT_FAILURE;
256 }
257 break;
258 }
259 case 'f': {
260 i++;
261 if (argv[i] == NULL) {
262 print_message();
263 return EXIT_FAILURE;
264 }
265 lua_pushobject(getargs(argv+i)); /* collect remaining arguments */
266 lua_setglobal("arg");
267 return file_input(argv[i]); /* stop scanning arguments */
268 }
269 case 's': {
270 fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
271 return EXIT_FAILURE;
272 }
273 default: {
241 print_message(); 274 print_message();
242 status = EXIT_FAILURE; goto endloop; 275 return EXIT_FAILURE;
243 } 276 }
244 lua_pushobject(getargs(argv+i)); /* collect remaining arguments */ 277 }
245 lua_setglobal("arg");
246 file_input(argv[i]);
247 goto endloop; /* stop scanning arguments */
248 break;
249 case 's':
250 fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
251 status = EXIT_FAILURE; goto endloop;
252 default:
253 print_message();
254 status = EXIT_FAILURE; goto endloop;
255 }
256 } 278 }
257 else if (strchr(argv[i], '='))
258 assign(argv[i]);
259 else
260 file_input(argv[i]);
261 } 279 }
262 endloop: 280 return EXIT_SUCCESS;
263 if (toclose) 281}
282
283
284static void getstacksize (int argc, char *argv[], struct Options *opt) {
285 if (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 's') {
286 int stacksize = atoi(&argv[1][2]);
287 if (stacksize == 0) {
288 fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]);
289 exit(EXIT_FAILURE);
290 }
291 opt->stacksize = stacksize;
292 }
293 else
294 opt->stacksize = 0; /* no stack size */
295}
296
297
298static void register_getargs (char *argv[]) {
299 lua_pushuserdata(argv);
300 lua_pushcclosure(l_getargs, 1);
301 lua_setglobal("getargs");
302}
303
304
305int main (int argc, char *argv[]) {
306 struct Options opt;
307 int status;
308 opt.toclose = 0;
309 getstacksize(argc, argv, &opt); /* handle option `-s' */
310 lua_state = lua_newstate(opt.stacksize, 1); /* create state */
311 USERINIT(); /* open libraries */
312 register_getargs(argv); /* create `getargs' function */
313 status = handle_argv(argv+1, &opt);
314 if (opt.toclose)
264 lua_close(); 315 lua_close();
265 return status; 316 return status;
266} 317}