Showing the changes to src/lua.c (1 of 60 files). Show all
Unified · Split
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | /* |
| 2 | −** $Id: lua.c,v 1.206 2012/09/29 20:07:06 roberto Exp $ | |
| 2 | +** $Id: lua.c,v 1.210 2014/02/26 15:27:56 roberto Exp $ | |
| 3 | 3 | ** Lua stand-alone interpreter |
| 4 | 4 | ** See Copyright Notice in lua.h |
| 5 | 5 | */ |
| @@ -31,28 +31,38 @@ · 25 unmodified lines | ||
| 31 | 31 | #define LUA_MAXINPUT 512 |
| 32 | 32 | #endif |
| 33 | 33 | |
| 34 | −#if !defined(LUA_INIT) | |
| 35 | −#define LUA_INIT "LUA_INIT" | |
| 34 | +#if !defined(LUA_INIT_VAR) | |
| 35 | +#define LUA_INIT_VAR "LUA_INIT" | |
| 36 | 36 | #endif |
| 37 | 37 | |
| 38 | −#define LUA_INITVERSION \ | |
| 39 | − LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR | |
| 38 | +#define LUA_INITVARVERSION \ | |
| 39 | + LUA_INIT_VAR "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR | |
| 40 | 40 | |
| 41 | 41 | |
| 42 | 42 | /* |
| 43 | 43 | ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that |
| 44 | 44 | ** is, whether we're running lua interactively). |
| 45 | 45 | */ |
| 46 | −#if defined(LUA_USE_ISATTY) | |
| 46 | +#if !defined(lua_stdin_is_tty) /* { */ | |
| 47 | + | |
| 48 | +#if defined(LUA_USE_POSIX) /* { */ | |
| 49 | + | |
| 47 | 50 | #include <unistd.h> |
| 48 | 51 | #define lua_stdin_is_tty() isatty(0) |
| 49 | −#elif defined(LUA_WIN) | |
| 52 | + | |
| 53 | +#elif defined(LUA_WIN) /* }{ */ | |
| 54 | + | |
| 50 | 55 | #include <io.h> |
| 51 | −#include <stdio.h> | |
| 52 | 56 | #define lua_stdin_is_tty() _isatty(_fileno(stdin)) |
| 53 | −#else | |
| 57 | + | |
| 58 | +#else /* }{ */ | |
| 59 | + | |
| 60 | +/* ANSI definition */ | |
| 54 | 61 | #define lua_stdin_is_tty() 1 /* assume stdin is a tty */ |
| 55 | −#endif | |
| 62 | + | |
| 63 | +#endif /* } */ | |
| 64 | + | |
| 65 | +#endif /* } */ | |
| 56 | 66 | |
| 57 | 67 | |
| 58 | 68 | /* |
| @@ -61,9 +71,10 @@ · 2 unmodified lines | ||
| 61 | 71 | ** lua_saveline defines how to "save" a read line in a "history". |
| 62 | 72 | ** lua_freeline defines how to free a line read by lua_readline. |
| 63 | 73 | */ |
| 64 | −#if defined(LUA_USE_READLINE) | |
| 74 | +#if !defined(lua_readline) /* { */ | |
| 75 | + | |
| 76 | +#if defined(LUA_USE_READLINE) /* { */ | |
| 65 | 77 | |
| 66 | −#include <stdio.h> | |
| 67 | 78 | #include <readline/readline.h> |
| 68 | 79 | #include <readline/history.h> |
| 69 | 80 | #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) |
| @@ -72,7 +83,7 @@ · 2 unmodified lines | ||
| 72 | 83 | add_history(lua_tostring(L, idx)); /* add it to history */ |
| 73 | 84 | #define lua_freeline(L,b) ((void)L, free(b)) |
| 74 | 85 | |
| 75 | −#elif !defined(lua_readline) | |
| 86 | +#else /* }{ */ | |
| 76 | 87 | |
| 77 | 88 | #define lua_readline(L,b,p) \ |
| 78 | 89 | ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ |
| @@ -80,7 +91,9 @@ · 1 unmodified line | ||
| 80 | 91 | #define lua_saveline(L,idx) { (void)L; (void)idx; } |
| 81 | 92 | #define lua_freeline(L,b) { (void)L; (void)b; } |
| 82 | 93 | |
| 83 | −#endif | |
| 94 | +#endif /* } */ | |
| 95 | + | |
| 96 | +#endif /* } */ | |
| 84 | 97 | |
| 85 | 98 | |
| 86 | 99 | |
| @@ -257,20 +270,21 @@ static int incomplete (lua_State *L, int status) { · 170 unmodified lines | ||
| 257 | 270 | } |
| 258 | 271 | |
| 259 | 272 | |
| 273 | +/* prompt the user, read a line, and push it into the Lua stack */ | |
| 260 | 274 | static int pushline (lua_State *L, int firstline) { |
| 261 | 275 | char buffer[LUA_MAXINPUT]; |
| 262 | 276 | char *b = buffer; |
| 263 | 277 | size_t l; |
| 264 | 278 | const char *prmt = get_prompt(L, firstline); |
| 265 | 279 | int readstatus = lua_readline(L, b, prmt); |
| 266 | − lua_pop(L, 1); /* remove result from 'get_prompt' */ | |
| 267 | 280 | if (readstatus == 0) |
| 268 | − return 0; /* no input */ | |
| 281 | + return 0; /* no input (prompt will be popped by caller) */ | |
| 282 | + lua_pop(L, 1); /* remove prompt */ | |
| 269 | 283 | l = strlen(b); |
| 270 | 284 | if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ |
| 271 | 285 | b[l-1] = '\0'; /* remove it */ |
| 272 | − if (firstline && b[0] == '=') /* first line starts with `=' ? */ | |
| 273 | − lua_pushfstring(L, "return %s", b+1); /* change it to `return' */ | |
| 286 | + if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ | |
| 287 | + lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ | |
| 274 | 288 | else |
| 275 | 289 | lua_pushstring(L, b); |
| 276 | 290 | lua_freeline(L, b); |
| @@ -278,24 +292,48 @@ static int pushline (lua_State *L, int firstline) { · 1 unmodified line | ||
| 278 | 292 | } |
| 279 | 293 | |
| 280 | 294 | |
| 281 | −static int loadline (lua_State *L) { | |
| 295 | +/* try to compile line on the stack as 'return <line>'; on return, stack | |
| 296 | + has either compiled chunk or original line (if compilation failed) */ | |
| 297 | +static int addreturn (lua_State *L) { | |
| 282 | 298 | int status; |
| 283 | − lua_settop(L, 0); | |
| 284 | − if (!pushline(L, 1)) | |
| 285 | − return -1; /* no input */ | |
| 299 | + size_t len; const char *line; | |
| 300 | + lua_pushliteral(L, "return "); | |
| 301 | + lua_pushvalue(L, -2); /* duplicate line */ | |
| 302 | + lua_concat(L, 2); /* new line is "return ..." */ | |
| 303 | + line = lua_tolstring(L, -1, &len); | |
| 304 | + if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) | |
| 305 | + lua_remove(L, -3); /* remove original line */ | |
| 306 | + else | |
| 307 | + lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */ | |
| 308 | + return status; | |
| 309 | +} | |
| 310 | + | |
| 311 | + | |
| 312 | +/* read multiple lines until a complete line */ | |
| 313 | +static int multiline (lua_State *L) { | |
| 286 | 314 | for (;;) { /* repeat until gets a complete line */ |
| 287 | − size_t l; | |
| 288 | − const char *line = lua_tolstring(L, 1, &l); | |
| 289 | − status = luaL_loadbuffer(L, line, l, "=stdin"); | |
| 290 | − if (!incomplete(L, status)) break; /* cannot try to add lines? */ | |
| 291 | − if (!pushline(L, 0)) /* no more input? */ | |
| 292 | − return -1; | |
| 293 | − lua_pushliteral(L, "\n"); /* add a new line... */ | |
| 315 | + size_t len; | |
| 316 | + const char *line = lua_tolstring(L, 1, &len); /* get what it has */ | |
| 317 | + int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ | |
| 318 | + if (!incomplete(L, status) || !pushline(L, 0)) | |
| 319 | + return status; /* cannot/should not try to add continuation line */ | |
| 320 | + lua_pushliteral(L, "\n"); /* add newline... */ | |
| 294 | 321 | lua_insert(L, -2); /* ...between the two lines */ |
| 295 | 322 | lua_concat(L, 3); /* join them */ |
| 296 | 323 | } |
| 324 | +} | |
| 325 | + | |
| 326 | + | |
| 327 | +static int loadline (lua_State *L) { | |
| 328 | + int status; | |
| 329 | + lua_settop(L, 0); | |
| 330 | + if (!pushline(L, 1)) | |
| 331 | + return -1; /* no input */ | |
| 332 | + if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ | |
| 333 | + status = multiline(L); /* try as command, maybe with continuation lines */ | |
| 297 | 334 | lua_saveline(L, 1); |
| 298 | 335 | lua_remove(L, 1); /* remove line */ |
| 336 | + lua_assert(lua_gettop(L) == 1); | |
| 299 | 337 | return status; |
| 300 | 338 | } |
| 301 | 339 | |
| @@ -421,10 +459,10 @@ static int runargs (lua_State *L, char **argv, int n) { · 119 unmodified lines | ||
| 421 | 459 | |
| 422 | 460 | |
| 423 | 461 | static int handle_luainit (lua_State *L) { |
| 424 | − const char *name = "=" LUA_INITVERSION; | |
| 462 | + const char *name = "=" LUA_INITVARVERSION; | |
| 425 | 463 | const char *init = getenv(name + 1); |
| 426 | 464 | if (init == NULL) { |
| 427 | − name = "=" LUA_INIT; | |
| 465 | + name = "=" LUA_INIT_VAR; | |
| 428 | 466 | init = getenv(name + 1); /* try alternative name */ |
| 429 | 467 | } |
| 430 | 468 | if (init == NULL) return LUA_OK; |