aboutsummaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c117
1 files changed, 58 insertions, 59 deletions
diff --git a/lua.c b/lua.c
index f90da9a5..933fb8ca 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.71 2001/10/17 21:12:57 roberto Exp $ 2** $Id: lua.c,v 1.72 2001/11/27 20:56:47 roberto Exp $
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,7 +10,6 @@
10#include <stdlib.h> 10#include <stdlib.h>
11#include <string.h> 11#include <string.h>
12 12
13#define LUA_PRIVATE
14#include "lua.h" 13#include "lua.h"
15 14
16#include "luadebug.h" 15#include "luadebug.h"
@@ -25,12 +24,12 @@ static int isatty (int x) { return x==0; } /* assume stdin is a tty */
25 24
26 25
27#ifndef LUA_PROGNAME 26#ifndef LUA_PROGNAME
28#define LUA_PROGNAME l_s("lua: ") 27#define LUA_PROGNAME "lua: "
29#endif 28#endif
30 29
31 30
32#ifndef PROMPT 31#ifndef PROMPT
33#define PROMPT l_s("> ") 32#define PROMPT "> "
34#endif 33#endif
35 34
36 35
@@ -62,7 +61,7 @@ static void lstop (void) {
62 lua_setlinehook(L, old_linehook); 61 lua_setlinehook(L, old_linehook);
63 lua_setcallhook(L, old_callhook); 62 lua_setcallhook(L, old_callhook);
64 lreset(); 63 lreset();
65 lua_error(L, l_s("interrupted!")); 64 lua_error(L, "interrupted!");
66} 65}
67 66
68 67
@@ -75,7 +74,7 @@ static void laction (int i) {
75} 74}
76 75
77 76
78static int ldo (int (*f)(lua_State *l, const l_char *), const l_char *name, 77static int ldo (int (*f)(lua_State *l, const char *), const char *name,
79 int clear) { 78 int clear) {
80 int res; 79 int res;
81 handler h = lreset(); 80 handler h = lreset();
@@ -86,45 +85,45 @@ static int ldo (int (*f)(lua_State *l, const l_char *), const l_char *name,
86 lua_settop(L, top); /* remove eventual results */ 85 lua_settop(L, top); /* remove eventual results */
87 /* Lua gives no message in such cases, so lua.c provides one */ 86 /* Lua gives no message in such cases, so lua.c provides one */
88 if (res == LUA_ERRMEM) { 87 if (res == LUA_ERRMEM) {
89 fprintf(stderr, LUA_PROGNAME l_s("memory allocation error\n")); 88 fprintf(stderr, LUA_PROGNAME "memory allocation error\n");
90 } 89 }
91 else if (res == LUA_ERRERR) 90 else if (res == LUA_ERRERR)
92 fprintf(stderr, LUA_PROGNAME l_s("error in error message\n")); 91 fprintf(stderr, LUA_PROGNAME "error in error message\n");
93 return res; 92 return res;
94} 93}
95 94
96 95
97static void print_message (void) { 96static void print_message (void) {
98 fprintf(stderr, 97 fprintf(stderr,
99 l_s("usage: lua [options]. Available options are:\n") 98 "usage: lua [options]. Available options are:\n"
100 l_s(" - execute stdin as a file\n") 99 " - execute stdin as a file\n"
101 l_s(" -c close Lua when exiting\n") 100 " -c close Lua when exiting\n"
102 l_s(" -e stat execute string `stat'\n") 101 " -e stat execute string `stat'\n"
103 l_s(" -f name execute file `name' with remaining arguments in table `arg'\n") 102 " -f name execute file `name' with remaining arguments in table `arg'\n"
104 l_s(" -i enter interactive mode with prompt\n") 103 " -i enter interactive mode with prompt\n"
105 l_s(" -q enter interactive mode without prompt\n") 104 " -q enter interactive mode without prompt\n"
106 l_s(" -sNUM set stack size to NUM (must be the first option)\n") 105 " -sNUM set stack size to NUM (must be the first option)\n"
107 l_s(" -v print version information\n") 106 " -v print version information\n"
108 l_s(" a=b set global `a' to string `b'\n") 107 " a=b set global `a' to string `b'\n"
109 l_s(" name execute file `name'\n") 108 " name execute file `name'\n"
110); 109);
111} 110}
112 111
113 112
114static void print_version (void) { 113static void print_version (void) {
115 printf(l_s("%.80s %.80s\n"), l_s(LUA_VERSION), l_s(LUA_COPYRIGHT)); 114 printf("%.80s %.80s\n", LUA_VERSION, LUA_COPYRIGHT);
116} 115}
117 116
118 117
119static void assign (l_char *arg) { 118static void assign (char *arg) {
120 l_char *eq = strchr(arg, l_c('=')); 119 char *eq = strchr(arg, '=');
121 *eq = l_c('\0'); /* spilt `arg' in two strings (name & value) */ 120 *eq = '\0'; /* spilt `arg' in two strings (name & value) */
122 lua_pushstring(L, eq+1); 121 lua_pushstring(L, eq+1);
123 lua_setglobal(L, arg); 122 lua_setglobal(L, arg);
124} 123}
125 124
126 125
127static void getargs (l_char *argv[]) { 126static void getargs (char *argv[]) {
128 int i; 127 int i;
129 lua_newtable(L); 128 lua_newtable(L);
130 for (i=0; argv[i]; i++) { 129 for (i=0; argv[i]; i++) {
@@ -134,24 +133,24 @@ static void getargs (l_char *argv[]) {
134 lua_settable(L, -3); 133 lua_settable(L, -3);
135 } 134 }
136 /* arg.n = maximum index in table `arg' */ 135 /* arg.n = maximum index in table `arg' */
137 lua_pushliteral(L, l_s("n")); 136 lua_pushliteral(L, "n");
138 lua_pushnumber(L, i-1); 137 lua_pushnumber(L, i-1);
139 lua_settable(L, -3); 138 lua_settable(L, -3);
140} 139}
141 140
142 141
143static int l_getargs (lua_State *l) { 142static int l_getargs (lua_State *l) {
144 l_char **argv = (l_char **)lua_touserdata(l, lua_upvalueindex(1)); 143 char **argv = (char **)lua_touserdata(l, lua_upvalueindex(1));
145 getargs(argv); 144 getargs(argv);
146 return 1; 145 return 1;
147} 146}
148 147
149 148
150static int file_input (const l_char *argv) { 149static int file_input (const char *argv) {
151 int result = ldo(lua_dofile, argv, 1); 150 int result = ldo(lua_dofile, argv, 1);
152 if (result) { 151 if (result) {
153 if (result == LUA_ERRFILE) { 152 if (result == LUA_ERRFILE) {
154 fprintf(stderr, LUA_PROGNAME l_s("cannot execute file ")); 153 fprintf(stderr, LUA_PROGNAME "cannot execute file ");
155 perror(argv); 154 perror(argv);
156 } 155 }
157 return EXIT_FAILURE; 156 return EXIT_FAILURE;
@@ -167,12 +166,12 @@ static int file_input (const l_char *argv) {
167#endif 166#endif
168 167
169 168
170static const l_char *get_prompt (int prompt) { 169static const char *get_prompt (int prompt) {
171 if (!prompt) 170 if (!prompt)
172 return l_s(""); 171 return "";
173 else { 172 else {
174 const l_char *s; 173 const char *s;
175 lua_getglobal(L, l_s("_PROMPT")); 174 lua_getglobal(L, "_PROMPT");
176 s = lua_tostring(L, -1); 175 s = lua_tostring(L, -1);
177 if (!s) s = PROMPT; 176 if (!s) s = PROMPT;
178 lua_pop(L, 1); /* remove global */ 177 lua_pop(L, 1); /* remove global */
@@ -188,20 +187,20 @@ static void manual_input (int version, int prompt) {
188 int toprint = 0; 187 int toprint = 0;
189 fputs(get_prompt(prompt), stdout); /* show prompt */ 188 fputs(get_prompt(prompt), stdout); /* show prompt */
190 for(;;) { 189 for(;;) {
191 l_char buffer[MAXINPUT]; 190 char buffer[MAXINPUT];
192 size_t l; 191 size_t l;
193 if (fgets(buffer, sizeof(buffer), stdin) == NULL) { 192 if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
194 printf(l_s("\n")); 193 printf("\n");
195 return; 194 return;
196 } 195 }
197 if (firstline && buffer[0] == l_c('=')) { 196 if (firstline && buffer[0] == '=') {
198 buffer[0] = l_c(' '); 197 buffer[0] = ' ';
199 lua_pushstring(L, l_s("return")); 198 lua_pushstring(L, "return");
200 toprint = 1; 199 toprint = 1;
201 } 200 }
202 l = strlen(buffer); 201 l = strlen(buffer);
203 if (buffer[l-1] == l_c('\n') && buffer[l-2] == l_c('\\')) { 202 if (buffer[l-1] == '\n' && buffer[l-2] == '\\') {
204 buffer[l-2] = l_c('\n'); 203 buffer[l-2] = '\n';
205 lua_pushlstring(L, buffer, l-1); 204 lua_pushlstring(L, buffer, l-1);
206 } 205 }
207 else { 206 else {
@@ -214,7 +213,7 @@ static void manual_input (int version, int prompt) {
214 ldo(lua_dostring, lua_tostring(L, 1), 0); 213 ldo(lua_dostring, lua_tostring(L, 1), 0);
215 lua_remove(L, 1); /* remove ran string */ 214 lua_remove(L, 1); /* remove ran string */
216 if (toprint && lua_gettop(L) > 0) { /* any result to print? */ 215 if (toprint && lua_gettop(L) > 0) { /* any result to print? */
217 lua_getglobal(L, l_s("print")); 216 lua_getglobal(L, "print");
218 lua_insert(L, 1); 217 lua_insert(L, 1);
219 lua_call(L, lua_gettop(L)-1, 0); 218 lua_call(L, lua_gettop(L)-1, 0);
220 } 219 }
@@ -224,7 +223,7 @@ static void manual_input (int version, int prompt) {
224} 223}
225 224
226 225
227static int handle_argv (l_char *argv[], int *toclose) { 226static int handle_argv (char *argv[], int *toclose) {
228 if (*argv == NULL) { /* no more arguments? */ 227 if (*argv == NULL) { /* no more arguments? */
229 if (isatty(0)) { 228 if (isatty(0)) {
230 manual_input(1, 1); 229 manual_input(1, 1);
@@ -235,8 +234,8 @@ static int handle_argv (l_char *argv[], int *toclose) {
235 else { /* other arguments; loop over them */ 234 else { /* other arguments; loop over them */
236 int i; 235 int i;
237 for (i = 0; argv[i] != NULL; i++) { 236 for (i = 0; argv[i] != NULL; i++) {
238 if (argv[i][0] != l_c('-')) { /* not an option? */ 237 if (argv[i][0] != '-') { /* not an option? */
239 if (strchr(argv[i], l_c('='))) 238 if (strchr(argv[i], '='))
240 assign(argv[i]); 239 assign(argv[i]);
241 else 240 else
242 if (file_input(argv[i]) != EXIT_SUCCESS) 241 if (file_input(argv[i]) != EXIT_SUCCESS)
@@ -247,49 +246,49 @@ static int handle_argv (l_char *argv[], int *toclose) {
247 ldo(lua_dofile, NULL, 1); /* executes stdin as a file */ 246 ldo(lua_dofile, NULL, 1); /* executes stdin as a file */
248 break; 247 break;
249 } 248 }
250 case l_c('i'): { 249 case 'i': {
251 manual_input(0, 1); 250 manual_input(0, 1);
252 break; 251 break;
253 } 252 }
254 case l_c('q'): { 253 case 'q': {
255 manual_input(0, 0); 254 manual_input(0, 0);
256 break; 255 break;
257 } 256 }
258 case l_c('c'): { 257 case 'c': {
259 *toclose = 1; 258 *toclose = 1;
260 break; 259 break;
261 } 260 }
262 case l_c('v'): { 261 case 'v': {
263 print_version(); 262 print_version();
264 break; 263 break;
265 } 264 }
266 case l_c('e'): { 265 case 'e': {
267 i++; 266 i++;
268 if (argv[i] == NULL) { 267 if (argv[i] == NULL) {
269 print_message(); 268 print_message();
270 return EXIT_FAILURE; 269 return EXIT_FAILURE;
271 } 270 }
272 if (ldo(lua_dostring, argv[i], 1) != 0) { 271 if (ldo(lua_dostring, argv[i], 1) != 0) {
273 fprintf(stderr, LUA_PROGNAME l_s("error running argument `%.99s'\n"), 272 fprintf(stderr, LUA_PROGNAME "error running argument `%.99s'\n",
274 argv[i]); 273 argv[i]);
275 return EXIT_FAILURE; 274 return EXIT_FAILURE;
276 } 275 }
277 break; 276 break;
278 } 277 }
279 case l_c('f'): { 278 case 'f': {
280 i++; 279 i++;
281 if (argv[i] == NULL) { 280 if (argv[i] == NULL) {
282 print_message(); 281 print_message();
283 return EXIT_FAILURE; 282 return EXIT_FAILURE;
284 } 283 }
285 getargs(argv+i); /* collect remaining arguments */ 284 getargs(argv+i); /* collect remaining arguments */
286 lua_setglobal(L, l_s("arg")); 285 lua_setglobal(L, "arg");
287 return file_input(argv[i]); /* stop scanning arguments */ 286 return file_input(argv[i]); /* stop scanning arguments */
288 } 287 }
289 case l_c('s'): { 288 case 's': {
290 if (i == 0) break; /* option already handled */ 289 if (i == 0) break; /* option already handled */
291 fprintf(stderr, 290 fprintf(stderr,
292 LUA_PROGNAME l_s("stack size (`-s') must be the first option\n")); 291 LUA_PROGNAME "stack size (`-s') must be the first option\n");
293 return EXIT_FAILURE; 292 return EXIT_FAILURE;
294 } 293 }
295 default: { 294 default: {
@@ -303,12 +302,12 @@ static int handle_argv (l_char *argv[], int *toclose) {
303} 302}
304 303
305 304
306static int getstacksize (int argc, l_char *argv[]) { 305static int getstacksize (int argc, char *argv[]) {
307 int stacksize = 0; 306 int stacksize = 0;
308 if (argc >= 2 && argv[1][0] == l_c('-') && argv[1][1] == l_c('s')) { 307 if (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 's') {
309 stacksize = strtol(&argv[1][2], NULL, 10); 308 stacksize = strtol(&argv[1][2], NULL, 10);
310 if (stacksize <= 0) { 309 if (stacksize <= 0) {
311 fprintf(stderr, LUA_PROGNAME l_s("invalid stack size ('%.20s')\n"), 310 fprintf(stderr, LUA_PROGNAME "invalid stack size ('%.20s')\n",
312 &argv[1][2]); 311 &argv[1][2]);
313 exit(EXIT_FAILURE); 312 exit(EXIT_FAILURE);
314 } 313 }
@@ -317,10 +316,10 @@ static int getstacksize (int argc, l_char *argv[]) {
317} 316}
318 317
319 318
320static void register_getargs (l_char *argv[]) { 319static void register_getargs (char *argv[]) {
321 lua_newuserdatabox(L, argv); 320 lua_newuserdatabox(L, argv);
322 lua_pushcclosure(L, l_getargs, 1); 321 lua_pushcclosure(L, l_getargs, 1);
323 lua_setglobal(L, l_s("getargs")); 322 lua_setglobal(L, "getargs");
324} 323}
325 324
326 325
@@ -333,7 +332,7 @@ static void openstdlibs (lua_State *l) {
333} 332}
334 333
335 334
336int main (int argc, l_char *argv[]) { 335int main (int argc, char *argv[]) {
337 int status; 336 int status;
338 int toclose = 0; 337 int toclose = 0;
339 L = lua_open(getstacksize(argc, argv)); /* create state */ 338 L = lua_open(getstacksize(argc, argv)); /* create state */