diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-06-05 17:42:06 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-06-05 17:42:06 -0300 |
commit | 23f0ff95177eda2e0a80e3a48562cc6837705735 (patch) | |
tree | 66a9d1b5600433d7e42f32ba0567da3b88899a2e | |
parent | 355037528c8c0896c5e643bb724f8d686c6322ad (diff) | |
download | lua-23f0ff95177eda2e0a80e3a48562cc6837705735.tar.gz lua-23f0ff95177eda2e0a80e3a48562cc6837705735.tar.bz2 lua-23f0ff95177eda2e0a80e3a48562cc6837705735.zip |
lots of janitor work (including comments) + 'arg' table created
before running any script + changes in the parameters and return
of 'collectargs'
-rw-r--r-- | lua.c | 376 |
1 files changed, 223 insertions, 153 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.209 2014/02/05 14:22:55 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.210 2014/02/26 15:27:56 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 | */ |
@@ -103,17 +103,24 @@ static lua_State *globalL = NULL; | |||
103 | static const char *progname = LUA_PROGNAME; | 103 | static const char *progname = LUA_PROGNAME; |
104 | 104 | ||
105 | 105 | ||
106 | 106 | /* | |
107 | ** Hook set by signal function to stop the interpreter. | ||
108 | */ | ||
107 | static void lstop (lua_State *L, lua_Debug *ar) { | 109 | static void lstop (lua_State *L, lua_Debug *ar) { |
108 | (void)ar; /* unused arg. */ | 110 | (void)ar; /* unused arg. */ |
109 | lua_sethook(L, NULL, 0, 0); | 111 | lua_sethook(L, NULL, 0, 0); /* reset hook */ |
110 | luaL_error(L, "interrupted!"); | 112 | luaL_error(L, "interrupted!"); |
111 | } | 113 | } |
112 | 114 | ||
113 | 115 | ||
116 | /* | ||
117 | ** Function to be called at a C signal. Because a C signal cannot | ||
118 | ** just change a Lua state (as there is no proper syncronization), | ||
119 | ** this function only sets a hook that, when called, will stop the | ||
120 | ** interpreter. | ||
121 | */ | ||
114 | static void laction (int i) { | 122 | static void laction (int i) { |
115 | signal(i, SIG_DFL); /* if another SIGINT happens before lstop, | 123 | signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ |
116 | terminate process (default action) */ | ||
117 | lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); | 124 | lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); |
118 | } | 125 | } |
119 | 126 | ||
@@ -139,27 +146,23 @@ static void print_usage (const char *badoption) { | |||
139 | } | 146 | } |
140 | 147 | ||
141 | 148 | ||
149 | /* | ||
150 | ** Prints an error message, adding the program name in front of it | ||
151 | ** (if present) | ||
152 | */ | ||
142 | static void l_message (const char *pname, const char *msg) { | 153 | static void l_message (const char *pname, const char *msg) { |
143 | if (pname) luai_writestringerror("%s: ", pname); | 154 | if (pname) luai_writestringerror("%s: ", pname); |
144 | luai_writestringerror("%s\n", msg); | 155 | luai_writestringerror("%s\n", msg); |
145 | } | 156 | } |
146 | 157 | ||
147 | 158 | ||
159 | /* | ||
160 | ** Check whether 'status' is not OK and, if so, prints the error | ||
161 | ** message on the top of the stack. Because this function can be called | ||
162 | ** unprotected, it only accepts actual strings as error messages. (A | ||
163 | ** coercion could raise a memory error.) | ||
164 | */ | ||
148 | static int report (lua_State *L, int status) { | 165 | static int report (lua_State *L, int status) { |
149 | if (status != LUA_OK && !lua_isnil(L, -1)) { | ||
150 | const char *msg = lua_tostring(L, -1); | ||
151 | if (msg == NULL) msg = "(error object is not a string)"; | ||
152 | l_message(progname, msg); | ||
153 | lua_pop(L, 1); | ||
154 | /* force a complete garbage collection in case of errors */ | ||
155 | lua_gc(L, LUA_GCCOLLECT, 0); | ||
156 | } | ||
157 | return status; | ||
158 | } | ||
159 | |||
160 | |||
161 | /* the next function is called unprotected, so it must avoid errors */ | ||
162 | static void finalreport (lua_State *L, int status) { | ||
163 | if (status != LUA_OK) { | 166 | if (status != LUA_OK) { |
164 | const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1) | 167 | const char *msg = (lua_type(L, -1) == LUA_TSTRING) ? lua_tostring(L, -1) |
165 | : NULL; | 168 | : NULL; |
@@ -167,31 +170,39 @@ static void finalreport (lua_State *L, int status) { | |||
167 | l_message(progname, msg); | 170 | l_message(progname, msg); |
168 | lua_pop(L, 1); | 171 | lua_pop(L, 1); |
169 | } | 172 | } |
173 | return status; | ||
170 | } | 174 | } |
171 | 175 | ||
172 | 176 | ||
173 | static int traceback (lua_State *L) { | 177 | /* |
178 | ** Message handler to be used to run all chunks | ||
179 | */ | ||
180 | static int msghandler (lua_State *L) { | ||
174 | const char *msg = lua_tostring(L, 1); | 181 | const char *msg = lua_tostring(L, 1); |
175 | if (msg) | 182 | if (msg) /* is error object a string? */ |
176 | luaL_traceback(L, L, msg, 1); | 183 | luaL_traceback(L, L, msg, 1); /* use standard traceback */ |
177 | else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */ | 184 | else if (!lua_isnoneornil(L, 1)) { /* non-string error object? */ |
178 | if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */ | 185 | if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */ |
179 | lua_pushliteral(L, "(no error message)"); | 186 | lua_pushliteral(L, "(no error message)"); |
180 | } | 187 | } /* else no error object, does nothing */ |
181 | return 1; | 188 | return 1; |
182 | } | 189 | } |
183 | 190 | ||
184 | 191 | ||
192 | /* | ||
193 | ** Interface to 'lua_pcall', which sets appropriate message function | ||
194 | ** and C-signal handler. Used to run all chunks. | ||
195 | */ | ||
185 | static int docall (lua_State *L, int narg, int nres) { | 196 | static int docall (lua_State *L, int narg, int nres) { |
186 | int status; | 197 | int status; |
187 | int base = lua_gettop(L) - narg; /* function index */ | 198 | int base = lua_gettop(L) - narg; /* function index */ |
188 | lua_pushcfunction(L, traceback); /* push traceback function */ | 199 | lua_pushcfunction(L, msghandler); /* push message handler */ |
189 | lua_insert(L, base); /* put it under chunk and args */ | 200 | lua_insert(L, base); /* put it under function and args */ |
190 | globalL = L; /* to be available to 'laction' */ | 201 | globalL = L; /* to be available to 'laction' */ |
191 | signal(SIGINT, laction); | 202 | signal(SIGINT, laction); /* set C-signal handler */ |
192 | status = lua_pcall(L, narg, nres, base); | 203 | status = lua_pcall(L, narg, nres, base); |
193 | signal(SIGINT, SIG_DFL); | 204 | signal(SIGINT, SIG_DFL); /* reset C-signal handler */ |
194 | lua_remove(L, base); /* remove traceback function */ | 205 | lua_remove(L, base); /* remove message handler from the stack */ |
195 | return status; | 206 | return status; |
196 | } | 207 | } |
197 | 208 | ||
@@ -202,38 +213,47 @@ static void print_version (void) { | |||
202 | } | 213 | } |
203 | 214 | ||
204 | 215 | ||
205 | static int getargs (lua_State *L, char **argv, int n) { | 216 | /* |
206 | int narg; | 217 | ** Create the 'arg' table, which stores all arguments from the |
207 | int i; | 218 | ** command line ('argv'). It should be aligned so that, at index 0, |
208 | int argc = 0; | 219 | ** it has 'argv[script]', which is the script name. The arguments |
209 | while (argv[argc]) argc++; /* count total number of arguments */ | 220 | ** to the script (everything after 'script') go to positive indices; |
210 | narg = argc - (n + 1); /* number of arguments to the script */ | 221 | ** other arguments (before the script name) go to negative indices. |
211 | luaL_checkstack(L, narg + 3, "too many arguments to script"); | 222 | ** If there is no script name, assume interpreter's name as base. |
212 | for (i=n+1; i < argc; i++) | 223 | */ |
213 | lua_pushstring(L, argv[i]); | 224 | static void createargtable (lua_State *L, char **argv, int argc, int script) { |
214 | lua_createtable(L, narg, n + 1); | 225 | int i, narg; |
215 | for (i=0; i < argc; i++) { | 226 | if (script == argc) script = 0; /* no script name? */ |
227 | narg = argc - (script + 1); /* number of positive indices */ | ||
228 | lua_createtable(L, narg, script + 1); | ||
229 | for (i = 0; i < argc; i++) { | ||
216 | lua_pushstring(L, argv[i]); | 230 | lua_pushstring(L, argv[i]); |
217 | lua_rawseti(L, -2, i - n); | 231 | lua_rawseti(L, -2, i - script); |
218 | } | 232 | } |
219 | return narg; | 233 | lua_setglobal(L, "arg"); |
220 | } | 234 | } |
221 | 235 | ||
222 | 236 | ||
223 | static int dofile (lua_State *L, const char *name) { | 237 | static int dochunk (lua_State *L, int status) { |
224 | int status = luaL_loadfile(L, name); | ||
225 | if (status == LUA_OK) status = docall(L, 0, 0); | 238 | if (status == LUA_OK) status = docall(L, 0, 0); |
226 | return report(L, status); | 239 | return report(L, status); |
227 | } | 240 | } |
228 | 241 | ||
229 | 242 | ||
243 | static int dofile (lua_State *L, const char *name) { | ||
244 | return dochunk(L, luaL_loadfile(L, name)); | ||
245 | } | ||
246 | |||
247 | |||
230 | static int dostring (lua_State *L, const char *s, const char *name) { | 248 | static int dostring (lua_State *L, const char *s, const char *name) { |
231 | int status = luaL_loadbuffer(L, s, strlen(s), name); | 249 | return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name)); |
232 | if (status == LUA_OK) status = docall(L, 0, 0); | ||
233 | return report(L, status); | ||
234 | } | 250 | } |
235 | 251 | ||
236 | 252 | ||
253 | /* | ||
254 | ** Calls 'require(name)' and stores the result in a global variable | ||
255 | ** with the given name. | ||
256 | */ | ||
237 | static int dolibrary (lua_State *L, const char *name) { | 257 | static int dolibrary (lua_State *L, const char *name) { |
238 | int status; | 258 | int status; |
239 | lua_getglobal(L, "require"); | 259 | lua_getglobal(L, "require"); |
@@ -245,6 +265,9 @@ static int dolibrary (lua_State *L, const char *name) { | |||
245 | } | 265 | } |
246 | 266 | ||
247 | 267 | ||
268 | /* | ||
269 | ** Returns the string to be used as a prompt by the interpreter. | ||
270 | */ | ||
248 | static const char *get_prompt (lua_State *L, int firstline) { | 271 | static const char *get_prompt (lua_State *L, int firstline) { |
249 | const char *p; | 272 | const char *p; |
250 | lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); | 273 | lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); |
@@ -257,6 +280,12 @@ static const char *get_prompt (lua_State *L, int firstline) { | |||
257 | #define EOFMARK "<eof>" | 280 | #define EOFMARK "<eof>" |
258 | #define marklen (sizeof(EOFMARK)/sizeof(char) - 1) | 281 | #define marklen (sizeof(EOFMARK)/sizeof(char) - 1) |
259 | 282 | ||
283 | |||
284 | /* | ||
285 | ** Check whether 'status' signals a syntax error and the error | ||
286 | ** message at the top of the stack ends with the above mark for | ||
287 | ** incoplete statements. | ||
288 | */ | ||
260 | static int incomplete (lua_State *L, int status) { | 289 | static int incomplete (lua_State *L, int status) { |
261 | if (status == LUA_ERRSYNTAX) { | 290 | if (status == LUA_ERRSYNTAX) { |
262 | size_t lmsg; | 291 | size_t lmsg; |
@@ -270,7 +299,9 @@ static int incomplete (lua_State *L, int status) { | |||
270 | } | 299 | } |
271 | 300 | ||
272 | 301 | ||
273 | /* prompt the user, read a line, and push it into the Lua stack */ | 302 | /* |
303 | ** Prompt the user, read a line, and push it into the Lua stack. | ||
304 | */ | ||
274 | static int pushline (lua_State *L, int firstline) { | 305 | static int pushline (lua_State *L, int firstline) { |
275 | char buffer[LUA_MAXINPUT]; | 306 | char buffer[LUA_MAXINPUT]; |
276 | char *b = buffer; | 307 | char *b = buffer; |
@@ -292,8 +323,10 @@ static int pushline (lua_State *L, int firstline) { | |||
292 | } | 323 | } |
293 | 324 | ||
294 | 325 | ||
295 | /* try to compile line on the stack as 'return <line>'; on return, stack | 326 | /* |
296 | has either compiled chunk or original line (if compilation failed) */ | 327 | ** Try to compile line on the stack as 'return <line>'; on return, stack |
328 | ** has either compiled chunk or original line (if compilation failed). | ||
329 | */ | ||
297 | static int addreturn (lua_State *L) { | 330 | static int addreturn (lua_State *L) { |
298 | int status; | 331 | int status; |
299 | size_t len; const char *line; | 332 | size_t len; const char *line; |
@@ -309,14 +342,16 @@ static int addreturn (lua_State *L) { | |||
309 | } | 342 | } |
310 | 343 | ||
311 | 344 | ||
312 | /* read multiple lines until a complete line */ | 345 | /* |
346 | ** Read multiple lines until a complete Lua statement | ||
347 | */ | ||
313 | static int multiline (lua_State *L) { | 348 | static int multiline (lua_State *L) { |
314 | for (;;) { /* repeat until gets a complete line */ | 349 | for (;;) { /* repeat until gets a complete statement */ |
315 | size_t len; | 350 | size_t len; |
316 | const char *line = lua_tolstring(L, 1, &len); /* get what it has */ | 351 | const char *line = lua_tolstring(L, 1, &len); /* get what it has */ |
317 | int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ | 352 | int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ |
318 | if (!incomplete(L, status) || !pushline(L, 0)) | 353 | if (!incomplete(L, status) || !pushline(L, 0)) |
319 | return status; /* cannot/should not try to add continuation line */ | 354 | return status; /* cannot or should not try to add continuation line */ |
320 | lua_pushliteral(L, "\n"); /* add newline... */ | 355 | lua_pushliteral(L, "\n"); /* add newline... */ |
321 | lua_insert(L, -2); /* ...between the two lines */ | 356 | lua_insert(L, -2); /* ...between the two lines */ |
322 | lua_concat(L, 3); /* join them */ | 357 | lua_concat(L, 3); /* join them */ |
@@ -324,6 +359,12 @@ static int multiline (lua_State *L) { | |||
324 | } | 359 | } |
325 | 360 | ||
326 | 361 | ||
362 | /* | ||
363 | ** Read a line and try to load (compile) it first as an expression (by | ||
364 | ** adding "return " in front of it) and second as a statement. Return | ||
365 | ** the final status of load/call with the resulting function (if any) | ||
366 | ** in the top of the stack. | ||
367 | */ | ||
327 | static int loadline (lua_State *L) { | 368 | static int loadline (lua_State *L) { |
328 | int status; | 369 | int status; |
329 | lua_settop(L, 0); | 370 | lua_settop(L, 0); |
@@ -331,29 +372,43 @@ static int loadline (lua_State *L) { | |||
331 | return -1; /* no input */ | 372 | return -1; /* no input */ |
332 | if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ | 373 | if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ |
333 | status = multiline(L); /* try as command, maybe with continuation lines */ | 374 | status = multiline(L); /* try as command, maybe with continuation lines */ |
334 | lua_saveline(L, 1); | 375 | lua_saveline(L, 1); /* keep history */ |
335 | lua_remove(L, 1); /* remove line */ | 376 | lua_remove(L, 1); /* remove line from the stack */ |
336 | lua_assert(lua_gettop(L) == 1); | 377 | lua_assert(lua_gettop(L) == 1); |
337 | return status; | 378 | return status; |
338 | } | 379 | } |
339 | 380 | ||
340 | 381 | ||
341 | static void dotty (lua_State *L) { | 382 | /* |
383 | ** Prints (calling the Lua 'print' function) any values on the stack | ||
384 | */ | ||
385 | static void l_print (lua_State *L) { | ||
386 | int n = lua_gettop(L); | ||
387 | if (n > 0) { /* any result to be printed? */ | ||
388 | luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); | ||
389 | lua_getglobal(L, "print"); | ||
390 | lua_insert(L, 1); | ||
391 | if (lua_pcall(L, n, 0, 0) != LUA_OK) | ||
392 | l_message(progname, lua_pushfstring(L, | ||
393 | "error calling " LUA_QL("print") " (%s)", | ||
394 | lua_tostring(L, -1))); | ||
395 | } | ||
396 | } | ||
397 | |||
398 | |||
399 | /* | ||
400 | ** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and | ||
401 | ** print any results. | ||
402 | */ | ||
403 | static void doREPL (lua_State *L) { | ||
342 | int status; | 404 | int status; |
343 | const char *oldprogname = progname; | 405 | const char *oldprogname = progname; |
344 | progname = NULL; | 406 | progname = NULL; /* no 'progname' on errors in interactive mode */ |
345 | while ((status = loadline(L)) != -1) { | 407 | while ((status = loadline(L)) != -1) { |
346 | if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET); | 408 | if (status == LUA_OK) |
347 | report(L, status); | 409 | status = docall(L, 0, LUA_MULTRET); |
348 | if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ | 410 | if (status == LUA_OK) l_print(L); |
349 | luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); | 411 | else report(L, status); |
350 | lua_getglobal(L, "print"); | ||
351 | lua_insert(L, 1); | ||
352 | if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) | ||
353 | l_message(progname, lua_pushfstring(L, | ||
354 | "error calling " LUA_QL("print") " (%s)", | ||
355 | lua_tostring(L, -1))); | ||
356 | } | ||
357 | } | 412 | } |
358 | lua_settop(L, 0); /* clear stack */ | 413 | lua_settop(L, 0); /* clear stack */ |
359 | luai_writeline(); | 414 | luai_writeline(); |
@@ -361,97 +416,109 @@ static void dotty (lua_State *L) { | |||
361 | } | 416 | } |
362 | 417 | ||
363 | 418 | ||
419 | /* | ||
420 | ** Push on the stack 'n' strings from 'argv' | ||
421 | */ | ||
422 | static void pushargs (lua_State *L, char **argv, int n) { | ||
423 | int i; | ||
424 | luaL_checkstack(L, n + 3, "too many arguments to script"); | ||
425 | for (i = 1; i < n; i++) /* skip 0 (the script name) */ | ||
426 | lua_pushstring(L, argv[i]); | ||
427 | } | ||
428 | |||
429 | |||
364 | static int handle_script (lua_State *L, char **argv, int n) { | 430 | static int handle_script (lua_State *L, char **argv, int n) { |
365 | int status; | 431 | int status; |
366 | const char *fname; | 432 | const char *fname = argv[0]; |
367 | int narg = getargs(L, argv, n); /* collect arguments */ | 433 | if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) |
368 | lua_setglobal(L, "arg"); | ||
369 | fname = argv[n]; | ||
370 | if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) | ||
371 | fname = NULL; /* stdin */ | 434 | fname = NULL; /* stdin */ |
372 | status = luaL_loadfile(L, fname); | 435 | status = luaL_loadfile(L, fname); |
373 | lua_insert(L, -(narg+1)); | 436 | if (status == LUA_OK) { |
374 | if (status == LUA_OK) | 437 | pushargs(L, argv, n); /* push arguments to script */ |
375 | status = docall(L, narg, LUA_MULTRET); | 438 | status = docall(L, n - 1, LUA_MULTRET); |
376 | else | 439 | } |
377 | lua_pop(L, narg); | ||
378 | return report(L, status); | 440 | return report(L, status); |
379 | } | 441 | } |
380 | 442 | ||
381 | 443 | ||
382 | /* check that argument has no extra characters at the end */ | ||
383 | #define noextrachars(x) {if ((x)[2] != '\0') return -1;} | ||
384 | |||
385 | |||
386 | /* indices of various argument indicators in array args */ | ||
387 | #define has_i 0 /* -i */ | ||
388 | #define has_v 1 /* -v */ | ||
389 | #define has_e 2 /* -e */ | ||
390 | #define has_E 3 /* -E */ | ||
391 | |||
392 | #define num_has 4 /* number of 'has_*' */ | ||
393 | 444 | ||
445 | /* bits of various argument indicators in 'args' */ | ||
446 | #define has_error 1 /* bad option */ | ||
447 | #define has_i 2 /* -i */ | ||
448 | #define has_v 4 /* -v */ | ||
449 | #define has_e 8 /* -e */ | ||
450 | #define has_E 16 /* -E */ | ||
394 | 451 | ||
395 | static int collectargs (char **argv, int *args) { | 452 | /* |
453 | ** Traverses all arguments from 'argv', returning a mask with those | ||
454 | ** needed before running any Lua code (or an error code if it finds | ||
455 | ** any invalid argument). 'first' returns the first not-handled argument | ||
456 | ** (either the script name or a bad argument in case of error). | ||
457 | */ | ||
458 | static int collectargs (char **argv, int *first) { | ||
459 | int args = 0; | ||
396 | int i; | 460 | int i; |
397 | for (i = 1; argv[i] != NULL; i++) { | 461 | for (i = 1; argv[i] != NULL; i++) { |
462 | *first = i; | ||
398 | if (argv[i][0] != '-') /* not an option? */ | 463 | if (argv[i][0] != '-') /* not an option? */ |
399 | return i; | 464 | return args; /* stop handling options */ |
400 | switch (argv[i][1]) { /* option */ | 465 | switch (argv[i][1]) { /* else check option */ |
401 | case '-': | 466 | case '-': /* '--' */ |
402 | noextrachars(argv[i]); | 467 | if (argv[i][2] != '\0') /* extra characters after '--'? */ |
403 | return (argv[i+1] != NULL ? i+1 : 0); | 468 | return has_error; /* invalid option */ |
404 | case '\0': | 469 | *first = i + 1; |
405 | return i; | 470 | return args; |
471 | case '\0': /* '-' */ | ||
472 | return args; /* script "name" is '-' */ | ||
406 | case 'E': | 473 | case 'E': |
407 | args[has_E] = 1; | 474 | if (argv[i][2] != '\0') /* extra characters after 1st? */ |
475 | return has_error; /* invalid option */ | ||
476 | args |= has_E; | ||
408 | break; | 477 | break; |
409 | case 'i': | 478 | case 'i': |
410 | noextrachars(argv[i]); | 479 | args |= has_i; /* goes through (-i implies -v) */ |
411 | args[has_i] = 1; /* go through */ | ||
412 | case 'v': | 480 | case 'v': |
413 | noextrachars(argv[i]); | 481 | if (argv[i][2] != '\0') /* extra characters after 1st? */ |
414 | args[has_v] = 1; | 482 | return has_error; /* invalid option */ |
483 | args |= has_v; | ||
415 | break; | 484 | break; |
416 | case 'e': | 485 | case 'e': |
417 | args[has_e] = 1; /* go through */ | 486 | args |= has_e; /* go through */ |
418 | case 'l': /* both options need an argument */ | 487 | case 'l': /* both options need an argument */ |
419 | if (argv[i][2] == '\0') { /* no concatenated argument? */ | 488 | if (argv[i][2] == '\0') { /* no concatenated argument? */ |
420 | i++; /* try next 'argv' */ | 489 | i++; /* try next 'argv' */ |
421 | if (argv[i] == NULL || argv[i][0] == '-') | 490 | if (argv[i] == NULL || argv[i][0] == '-') |
422 | return -(i - 1); /* no next argument or it is another option */ | 491 | return has_error; /* no next argument or it is another option */ |
423 | } | 492 | } |
424 | break; | 493 | break; |
425 | default: /* invalid option; return its index... */ | 494 | default: /* invalid option */ |
426 | return -i; /* ...as a negative value */ | 495 | return has_error; |
427 | } | 496 | } |
428 | } | 497 | } |
429 | return 0; | 498 | *first = i; /* no script name */ |
499 | return args; | ||
430 | } | 500 | } |
431 | 501 | ||
432 | 502 | ||
503 | /* | ||
504 | ** Processes options 'e' and 'l', which involve running Lua code. | ||
505 | ** Returns 0 if some code raises an error. | ||
506 | */ | ||
433 | static int runargs (lua_State *L, char **argv, int n) { | 507 | static int runargs (lua_State *L, char **argv, int n) { |
434 | int i; | 508 | int i; |
435 | for (i = 1; i < n; i++) { | 509 | for (i = 1; i < n; i++) { |
436 | lua_assert(argv[i][0] == '-'); | 510 | int status; |
437 | switch (argv[i][1]) { /* option */ | 511 | int option = argv[i][1]; |
438 | case 'e': { | 512 | lua_assert(argv[i][0] == '-'); /* already checked */ |
439 | const char *chunk = argv[i] + 2; | 513 | if (option == 'e' || option == 'l') { |
440 | if (*chunk == '\0') chunk = argv[++i]; | 514 | const char *extra = argv[i] + 2; /* both options need an argument */ |
441 | lua_assert(chunk != NULL); | 515 | if (*extra == '\0') extra = argv[++i]; |
442 | if (dostring(L, chunk, "=(command line)") != LUA_OK) | 516 | lua_assert(extra != NULL); |
443 | return 0; | 517 | if (option == 'e') |
444 | break; | 518 | status = dostring(L, extra, "=(command line)"); |
445 | } | 519 | else |
446 | case 'l': { | 520 | status = dolibrary(L, extra); |
447 | const char *filename = argv[i] + 2; | 521 | if (status != LUA_OK) return 0; |
448 | if (*filename == '\0') filename = argv[++i]; | ||
449 | lua_assert(filename != NULL); | ||
450 | if (dolibrary(L, filename) != LUA_OK) | ||
451 | return 0; /* stop if file fails */ | ||
452 | break; | ||
453 | } | ||
454 | default: break; | ||
455 | } | 522 | } |
456 | } | 523 | } |
457 | return 1; | 524 | return 1; |
@@ -473,40 +540,44 @@ static int handle_luainit (lua_State *L) { | |||
473 | } | 540 | } |
474 | 541 | ||
475 | 542 | ||
543 | /* | ||
544 | ** Main body of stand-alone interpreter (to be called in protected mode). | ||
545 | ** Reads the options and handles them all. | ||
546 | */ | ||
476 | static int pmain (lua_State *L) { | 547 | static int pmain (lua_State *L) { |
477 | int argc = (int)lua_tointeger(L, 1); | 548 | int argc = (int)lua_tointeger(L, 1); |
478 | char **argv = (char **)lua_touserdata(L, 2); | 549 | char **argv = (char **)lua_touserdata(L, 2); |
479 | int script; | 550 | int script; |
480 | int args[num_has]; | 551 | int args = collectargs(argv, &script); |
481 | args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0; | 552 | luaL_checkversion(L); /* check that interpreter has correct version */ |
482 | if (argv[0] && argv[0][0]) progname = argv[0]; | 553 | if (argv[0] && argv[0][0]) progname = argv[0]; |
483 | script = collectargs(argv, args); | 554 | if (args == has_error) { /* bad arg? */ |
484 | if (script < 0) { /* invalid arg? */ | 555 | print_usage(argv[script]); /* 'script' has index of bad arg. */ |
485 | print_usage(argv[-script]); | ||
486 | return 0; | 556 | return 0; |
487 | } | 557 | } |
488 | if (args[has_v]) print_version(); | 558 | if (args & has_v) /* option '-v'? */ |
489 | if (args[has_E]) { /* option '-E'? */ | 559 | print_version(); |
560 | if (args & has_E) { /* option '-E'? */ | ||
490 | lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ | 561 | lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ |
491 | lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); | 562 | lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); |
492 | } | 563 | } |
493 | /* open standard libraries */ | 564 | luaL_openlibs(L); /* open standard libraries */ |
494 | luaL_checkversion(L); | 565 | createargtable(L, argv, argc, script); /* create table 'arg' */ |
495 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ | 566 | if (!(args & has_E)) { /* no option '-E'? */ |
496 | luaL_openlibs(L); /* open libraries */ | 567 | if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ |
497 | lua_gc(L, LUA_GCRESTART, 0); | 568 | return 0; /* error running LUA_INIT */ |
498 | if (!args[has_E] && handle_luainit(L) != LUA_OK) | 569 | } |
499 | return 0; /* error running LUA_INIT */ | 570 | if (!runargs(L, argv, script)) /* execute arguments -e and -l */ |
500 | /* execute arguments -e and -l */ | 571 | return 0; /* something failed */ |
501 | if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; | 572 | if (script < argc && /* execute main script (if there is one) */ |
502 | /* execute main script (if there is one) */ | 573 | handle_script(L, argv + script, argc - script) != LUA_OK) |
503 | if (script && handle_script(L, argv, script) != LUA_OK) return 0; | 574 | return 0; |
504 | if (args[has_i]) /* -i option? */ | 575 | if (args & has_i) /* -i option? */ |
505 | dotty(L); | 576 | doREPL(L); /* do read-eval-print loop */ |
506 | else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */ | 577 | else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ |
507 | if (lua_stdin_is_tty()) { | 578 | if (lua_stdin_is_tty()) { /* running in interactive mode? */ |
508 | print_version(); | 579 | print_version(); |
509 | dotty(L); | 580 | doREPL(L); /* do read-eval-print loop */ |
510 | } | 581 | } |
511 | else dofile(L, NULL); /* executes stdin as a file */ | 582 | else dofile(L, NULL); /* executes stdin as a file */ |
512 | } | 583 | } |
@@ -522,13 +593,12 @@ int main (int argc, char **argv) { | |||
522 | l_message(argv[0], "cannot create state: not enough memory"); | 593 | l_message(argv[0], "cannot create state: not enough memory"); |
523 | return EXIT_FAILURE; | 594 | return EXIT_FAILURE; |
524 | } | 595 | } |
525 | /* call 'pmain' in protected mode */ | 596 | lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ |
526 | lua_pushcfunction(L, &pmain); | ||
527 | lua_pushinteger(L, argc); /* 1st argument */ | 597 | lua_pushinteger(L, argc); /* 1st argument */ |
528 | lua_pushlightuserdata(L, argv); /* 2nd argument */ | 598 | lua_pushlightuserdata(L, argv); /* 2nd argument */ |
529 | status = lua_pcall(L, 2, 1, 0); | 599 | status = lua_pcall(L, 2, 1, 0); /* do the call */ |
530 | result = lua_toboolean(L, -1); /* get result */ | 600 | result = lua_toboolean(L, -1); /* get result */ |
531 | finalreport(L, status); | 601 | report(L, status); |
532 | lua_close(L); | 602 | lua_close(L); |
533 | return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; | 603 | return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; |
534 | } | 604 | } |