summaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-07-13 16:56:44 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-07-13 16:56:44 -0300
commita535ad6fee6b0a5fc795dc1dfe8737b63b3684d8 (patch)
tree017e569be364aaef8d7c446359c479cf99ba6f48 /lua.c
parent4206d7ed6033764c81d97ccdc343f4defda516e6 (diff)
downloadlua-a535ad6fee6b0a5fc795dc1dfe8737b63b3684d8.tar.gz
lua-a535ad6fee6b0a5fc795dc1dfe8737b63b3684d8.tar.bz2
lua-a535ad6fee6b0a5fc795dc1dfe8737b63b3684d8.zip
several small changes (mainly function names)
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c143
1 files changed, 59 insertions, 84 deletions
diff --git a/lua.c b/lua.c
index dac53e29..7cc48582 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.128 2004/06/17 14:06:52 roberto Exp roberto $ 2** $Id: lua.c,v 1.129 2004/07/01 14:26:28 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*/
@@ -28,38 +28,23 @@
28 28
29 29
30 30
31static lua_State *L = NULL; 31static lua_State *globalL = NULL;
32 32
33static const char *progname = PROGNAME; 33static const char *progname = PROGNAME;
34 34
35 35
36 36
37static const luaL_reg lualibs[] = { 37static void lstop (lua_State *L, lua_Debug *ar) {
38 {"base", luaopen_base},
39 {"table", luaopen_table},
40 {"io", luaopen_io},
41 {"string", luaopen_string},
42 {"math", luaopen_math},
43 {"debug", luaopen_debug},
44 {"loadlib", luaopen_loadlib},
45 /* add your libraries here */
46 LUA_EXTRALIBS
47 {NULL, NULL}
48};
49
50
51
52static void lstop (lua_State *l, lua_Debug *ar) {
53 (void)ar; /* unused arg. */ 38 (void)ar; /* unused arg. */
54 lua_sethook(l, NULL, 0, 0); 39 lua_sethook(L, NULL, 0, 0);
55 luaL_error(l, "interrupted!"); 40 luaL_error(L, "interrupted!");
56} 41}
57 42
58 43
59static void laction (int i) { 44static void laction (int i) {
60 signal(i, SIG_DFL); /* if another SIGINT happens before lstop, 45 signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
61 terminate process (default action) */ 46 terminate process (default action) */
62 lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); 47 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
63} 48}
64 49
65 50
@@ -83,7 +68,7 @@ static void l_message (const char *pname, const char *msg) {
83} 68}
84 69
85 70
86static int report (int status) { 71static int report (lua_State *L, int status) {
87 if (status && !lua_isnil(L, -1)) { 72 if (status && !lua_isnil(L, -1)) {
88 const char *msg = lua_tostring(L, -1); 73 const char *msg = lua_tostring(L, -1);
89 if (msg == NULL) msg = "(error object is not a string)"; 74 if (msg == NULL) msg = "(error object is not a string)";
@@ -94,7 +79,7 @@ static int report (int status) {
94} 79}
95 80
96 81
97static int lcall (int narg, int clear) { 82static int docall (lua_State *L, int narg, int clear) {
98 int status; 83 int status;
99 int base = lua_gettop(L) - narg; /* function index */ 84 int base = lua_gettop(L) - narg; /* function index */
100 lua_pushliteral(L, "_TRACEBACK"); 85 lua_pushliteral(L, "_TRACEBACK");
@@ -113,7 +98,7 @@ static void print_version (void) {
113} 98}
114 99
115 100
116static int getargs (char *argv[], int n) { 101static int getargs (lua_State *L, char *argv[], int n) {
117 int i, narg; 102 int i, narg;
118 for (i=n+1; argv[i]; i++) { 103 for (i=n+1; argv[i]; i++) {
119 luaL_checkstack(L, 1, "too many arguments to script"); 104 luaL_checkstack(L, 1, "too many arguments to script");
@@ -130,22 +115,22 @@ static int getargs (char *argv[], int n) {
130} 115}
131 116
132 117
133static int file_input (const char *name) { 118static int dofile (lua_State *L, const char *name) {
134 int status = luaL_loadfile(L, name) || lcall(0, 1); 119 int status = luaL_loadfile(L, name) || docall(L, 0, 1);
135 return report(status); 120 return report(L, status);
136} 121}
137 122
138 123
139static int dostring (const char *s, const char *name) { 124static int dostring (lua_State *L, const char *s, const char *name) {
140 int status = luaL_loadbuffer(L, s, strlen(s), name) || lcall(0, 1); 125 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
141 return report(status); 126 return report(L, status);
142} 127}
143 128
144 129
145static int load_file (const char *name) { 130static int dolibrary (lua_State *L, const char *name) {
146 name = luaL_searchpath(L, name, NULL); 131 name = luaL_searchpath(L, name, NULL);
147 if (name == NULL) return report(1); 132 if (name == NULL) return report(L, 1);
148 else return file_input(name); 133 else return dofile(L, name);
149} 134}
150 135
151 136
@@ -163,7 +148,7 @@ static int load_file (const char *name) {
163#endif 148#endif
164 149
165 150
166static int readline (lua_State *l, const char *prompt) { 151static int readline (lua_State *L, const char *prompt) {
167 static char buffer[MAXINPUT]; 152 static char buffer[MAXINPUT];
168 if (prompt) { 153 if (prompt) {
169 fputs(prompt, stdout); 154 fputs(prompt, stdout);
@@ -172,7 +157,7 @@ static int readline (lua_State *l, const char *prompt) {
172 if (fgets(buffer, sizeof(buffer), stdin) == NULL) 157 if (fgets(buffer, sizeof(buffer), stdin) == NULL)
173 return 0; /* read fails */ 158 return 0; /* read fails */
174 else { 159 else {
175 lua_pushstring(l, buffer); 160 lua_pushstring(L, buffer);
176 return 1; 161 return 1;
177 } 162 }
178} 163}
@@ -180,7 +165,7 @@ static int readline (lua_State *l, const char *prompt) {
180#endif 165#endif
181 166
182 167
183static const char *get_prompt (int firstline) { 168static const char *get_prompt (lua_State *L, int firstline) {
184 const char *p = NULL; 169 const char *p = NULL;
185 lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2"); 170 lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
186 lua_rawget(L, LUA_GLOBALSINDEX); 171 lua_rawget(L, LUA_GLOBALSINDEX);
@@ -191,7 +176,7 @@ static const char *get_prompt (int firstline) {
191} 176}
192 177
193 178
194static int incomplete (int status) { 179static int incomplete (lua_State *L, int status) {
195 if (status == LUA_ERRSYNTAX && 180 if (status == LUA_ERRSYNTAX &&
196 strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) { 181 strstr(lua_tostring(L, -1), "near `<eof>'") != NULL) {
197 lua_pop(L, 1); 182 lua_pop(L, 1);
@@ -202,10 +187,10 @@ static int incomplete (int status) {
202} 187}
203 188
204 189
205static int load_string (void) { 190static int loadline (lua_State *L) {
206 int status; 191 int status;
207 lua_settop(L, 0); 192 lua_settop(L, 0);
208 if (lua_readline(L, get_prompt(1)) == 0) /* no input? */ 193 if (lua_readline(L, get_prompt(L, 1)) == 0) /* no input? */
209 return -1; 194 return -1;
210 if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */ 195 if (lua_tostring(L, -1)[0] == '=') { /* line starts with `=' ? */
211 lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */ 196 lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */
@@ -213,8 +198,8 @@ static int load_string (void) {
213 } 198 }
214 for (;;) { /* repeat until gets a complete line */ 199 for (;;) { /* repeat until gets a complete line */
215 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); 200 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
216 if (!incomplete(status)) break; /* cannot try to add lines? */ 201 if (!incomplete(L, status)) break; /* cannot try to add lines? */
217 if (lua_readline(L, get_prompt(0)) == 0) /* no more input? */ 202 if (lua_readline(L, get_prompt(L, 0)) == 0) /* no more input? */
218 return -1; 203 return -1;
219 lua_concat(L, lua_gettop(L)); /* join lines */ 204 lua_concat(L, lua_gettop(L)); /* join lines */
220 } 205 }
@@ -224,13 +209,14 @@ static int load_string (void) {
224} 209}
225 210
226 211
227static void manual_input (void) { 212static void dotty (lua_State *L) {
228 int status; 213 int status;
229 const char *oldprogname = progname; 214 const char *oldprogname = progname;
230 progname = NULL; 215 progname = NULL;
231 while ((status = load_string()) != -1) { 216 print_version();
232 if (status == 0) status = lcall(0, 0); 217 while ((status = loadline(L)) != -1) {
233 report(status); 218 if (status == 0) status = docall(L, 0, 0);
219 report(L, status);
234 if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ 220 if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
235 lua_getglobal(L, "print"); 221 lua_getglobal(L, "print");
236 lua_insert(L, 1); 222 lua_insert(L, 1);
@@ -245,25 +231,23 @@ static void manual_input (void) {
245} 231}
246 232
247 233
248static int checkvar (lua_State *l) { 234static int checkvar (lua_State *L) {
249 const char *name = lua_tostring(l, 2); 235 const char *name = lua_tostring(L, 2);
250 if (name) 236 if (name)
251 luaL_error(l, "attempt to access undefined variable `%s'", name); 237 luaL_error(L, "attempt to access undefined variable `%s'", name);
252 return 0; 238 return 0;
253} 239}
254 240
255 241
256#define clearinteractive(i) (*i &= 2) 242#define clearinteractive(i) (*i &= 2)
257 243
258static int handle_argv (char *argv[], int *interactive) { 244static int handle_argv (lua_State *L, char *argv[], int *interactive) {
259 if (argv[1] == NULL) { /* no arguments? */ 245 if (argv[1] == NULL) { /* no arguments? */
260 *interactive = 0; 246 *interactive = 0;
261 if (stdin_is_tty()) { 247 if (stdin_is_tty())
262 print_version(); 248 dotty(L);
263 manual_input();
264 }
265 else 249 else
266 file_input(NULL); /* executes stdin as a file */ 250 dofile(L, NULL); /* executes stdin as a file */
267 } 251 }
268 else { /* other arguments; loop over them */ 252 else { /* other arguments; loop over them */
269 int i; 253 int i;
@@ -280,7 +264,7 @@ static int handle_argv (char *argv[], int *interactive) {
280 } 264 }
281 case '\0': { 265 case '\0': {
282 clearinteractive(interactive); 266 clearinteractive(interactive);
283 file_input(NULL); /* executes stdin as a file */ 267 dofile(L, NULL); /* executes stdin as a file */
284 break; 268 break;
285 } 269 }
286 case 'i': { 270 case 'i': {
@@ -307,7 +291,7 @@ static int handle_argv (char *argv[], int *interactive) {
307 print_usage(); 291 print_usage();
308 return 1; 292 return 1;
309 } 293 }
310 if (dostring(chunk, "=<command line>") != 0) 294 if (dostring(L, chunk, "=<command line>") != 0)
311 return 1; 295 return 1;
312 break; 296 break;
313 } 297 }
@@ -318,7 +302,7 @@ static int handle_argv (char *argv[], int *interactive) {
318 print_usage(); 302 print_usage();
319 return 1; 303 return 1;
320 } 304 }
321 if (load_file(filename)) 305 if (dolibrary(L, filename))
322 return 1; /* stop if file fails */ 306 return 1; /* stop if file fails */
323 break; 307 break;
324 } 308 }
@@ -331,39 +315,30 @@ static int handle_argv (char *argv[], int *interactive) {
331 } endloop: 315 } endloop:
332 if (argv[i] != NULL) { 316 if (argv[i] != NULL) {
333 const char *filename = argv[i]; 317 const char *filename = argv[i];
334 int narg = getargs(argv, i); /* collect arguments */ 318 int narg = getargs(L, argv, i); /* collect arguments */
335 int status; 319 int status;
336 lua_setglobal(L, "arg"); 320 lua_setglobal(L, "arg");
337 clearinteractive(interactive); 321 clearinteractive(interactive);
338 status = luaL_loadfile(L, filename); 322 status = luaL_loadfile(L, filename);
339 lua_insert(L, -(narg+1)); 323 lua_insert(L, -(narg+1));
340 if (status == 0) 324 if (status == 0)
341 status = lcall(narg, 0); 325 status = docall(L, narg, 0);
342 else 326 else
343 lua_pop(L, narg); 327 lua_pop(L, narg);
344 return report(status); 328 return report(L, status);
345 } 329 }
346 } 330 }
347 return 0; 331 return 0;
348} 332}
349 333
350 334
351static void openstdlibs (lua_State *l) { 335static int handle_luainit (lua_State *L) {
352 const luaL_reg *lib = lualibs;
353 for (; lib->func; lib++) {
354 lib->func(l); /* open library */
355 lua_settop(l, 0); /* discard any results */
356 }
357}
358
359
360static int handle_luainit (void) {
361 const char *init = getenv("LUA_INIT"); 336 const char *init = getenv("LUA_INIT");
362 if (init == NULL) return 0; /* status OK */ 337 if (init == NULL) return 0; /* status OK */
363 else if (init[0] == '@') 338 else if (init[0] == '@')
364 return file_input(init+1); 339 return dofile(L, init+1);
365 else 340 else
366 return dostring(init, "=LUA_INIT"); 341 return dostring(L, init, "=LUA_INIT");
367} 342}
368 343
369 344
@@ -374,17 +349,17 @@ struct Smain {
374}; 349};
375 350
376 351
377static int pmain (lua_State *l) { 352static int pmain (lua_State *L) {
378 struct Smain *s = (struct Smain *)lua_touserdata(l, 1); 353 struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
379 int status; 354 int status;
380 int interactive = 1; 355 int interactive = 1;
381 if (s->argv[0] && s->argv[0][0]) progname = s->argv[0]; 356 if (s->argv[0] && s->argv[0][0]) progname = s->argv[0];
382 L = l; 357 globalL = L;
383 lua_userinit(l); /* open libraries */ 358 lua_userinit(L); /* open libraries */
384 status = handle_luainit(); 359 status = handle_luainit(L);
385 if (status == 0) { 360 if (status == 0) {
386 status = handle_argv(s->argv, &interactive); 361 status = handle_argv(L, s->argv, &interactive);
387 if (status == 0 && interactive) manual_input(); 362 if (status == 0 && interactive) dotty(L);
388 } 363 }
389 s->status = status; 364 s->status = status;
390 return 0; 365 return 0;
@@ -394,16 +369,16 @@ static int pmain (lua_State *l) {
394int main (int argc, char *argv[]) { 369int main (int argc, char *argv[]) {
395 int status; 370 int status;
396 struct Smain s; 371 struct Smain s;
397 lua_State *l = lua_open(); /* create state */ 372 lua_State *L = lua_open(); /* create state */
398 if (l == NULL) { 373 if (L == NULL) {
399 l_message(argv[0], "cannot create state: not enough memory"); 374 l_message(argv[0], "cannot create state: not enough memory");
400 return EXIT_FAILURE; 375 return EXIT_FAILURE;
401 } 376 }
402 s.argc = argc; 377 s.argc = argc;
403 s.argv = argv; 378 s.argv = argv;
404 status = lua_cpcall(l, &pmain, &s); 379 status = lua_cpcall(L, &pmain, &s);
405 report(status); 380 report(L, status);
406 lua_close(l); 381 lua_close(L);
407 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS; 382 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
408} 383}
409 384