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 | ** 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 | */ |
| @@ -31,28 +31,38 @@ · 25 unmodified lines | |||
| 31 | #define LUA_MAXINPUT 512 | 31 | #define LUA_MAXINPUT 512 |
| 32 | #endif | 32 | #endif |
| 33 | 33 | ||
| 34 | #if !defined(LUA_INIT) | 34 | #if !defined(LUA_INIT_VAR) |
| 35 | #define LUA_INIT "LUA_INIT" | 35 | #define LUA_INIT_VAR "LUA_INIT" |
| 36 | #endif | 36 | #endif |
| 37 | 37 | ||
| 38 | #define LUA_INITVERSION \ | 38 | #define LUA_INITVARVERSION \ |
| 39 | LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR | 39 | LUA_INIT_VAR "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR |
| 40 | 40 | ||
| 41 | 41 | ||
| 42 | /* | 42 | /* |
| 43 | ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that | 43 | ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that |
| 44 | ** is, whether we're running lua interactively). | 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 | #include <unistd.h> | 50 | #include <unistd.h> |
| 48 | #define lua_stdin_is_tty() isatty(0) | 51 | #define lua_stdin_is_tty() isatty(0) |
| 49 | #elif defined(LUA_WIN) | 52 | |
| 53 | #elif defined(LUA_WIN) /* }{ */ | ||
| 54 | |||
| 50 | #include <io.h> | 55 | #include <io.h> |
| 51 | #include <stdio.h> | ||
| 52 | #define lua_stdin_is_tty() _isatty(_fileno(stdin)) | 56 | #define lua_stdin_is_tty() _isatty(_fileno(stdin)) |
| 53 | #else | 57 | |
| 58 | #else /* }{ */ | ||
| 59 | |||
| 60 | /* ANSI definition */ | ||
| 54 | #define lua_stdin_is_tty() 1 /* assume stdin is a tty */ | 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 | ** lua_saveline defines how to "save" a read line in a "history". | 71 | ** lua_saveline defines how to "save" a read line in a "history". |
| 62 | ** lua_freeline defines how to free a line read by lua_readline. | 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 | #include <readline/readline.h> | 78 | #include <readline/readline.h> |
| 68 | #include <readline/history.h> | 79 | #include <readline/history.h> |
| 69 | #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) | 80 | #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) |
| @@ -72,7 +83,7 @@ · 2 unmodified lines | |||
| 72 | add_history(lua_tostring(L, idx)); /* add it to history */ | 83 | add_history(lua_tostring(L, idx)); /* add it to history */ |
| 73 | #define lua_freeline(L,b) ((void)L, free(b)) | 84 | #define lua_freeline(L,b) ((void)L, free(b)) |
| 74 | 85 | ||
| 75 | #elif !defined(lua_readline) | 86 | #else /* }{ */ |
| 76 | 87 | ||
| 77 | #define lua_readline(L,b,p) \ | 88 | #define lua_readline(L,b,p) \ |
| 78 | ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ | 89 | ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ |
| @@ -80,7 +91,9 @@ · 1 unmodified line | |||
| 80 | #define lua_saveline(L,idx) { (void)L; (void)idx; } | 91 | #define lua_saveline(L,idx) { (void)L; (void)idx; } |
| 81 | #define lua_freeline(L,b) { (void)L; (void)b; } | 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 | static int pushline (lua_State *L, int firstline) { | 274 | static int pushline (lua_State *L, int firstline) { |
| 261 | char buffer[LUA_MAXINPUT]; | 275 | char buffer[LUA_MAXINPUT]; |
| 262 | char *b = buffer; | 276 | char *b = buffer; |
| 263 | size_t l; | 277 | size_t l; |
| 264 | const char *prmt = get_prompt(L, firstline); | 278 | const char *prmt = get_prompt(L, firstline); |
| 265 | int readstatus = lua_readline(L, b, prmt); | 279 | int readstatus = lua_readline(L, b, prmt); |
| 266 | lua_pop(L, 1); /* remove result from 'get_prompt' */ | ||
| 267 | if (readstatus == 0) | 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 | l = strlen(b); | 283 | l = strlen(b); |
| 270 | if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ | 284 | if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ |
| 271 | b[l-1] = '\0'; /* remove it */ | 285 | b[l-1] = '\0'; /* remove it */ |
| 272 | if (firstline && b[0] == '=') /* first line starts with `=' ? */ | 286 | if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ |
| 273 | lua_pushfstring(L, "return %s", b+1); /* change it to `return' */ | 287 | lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ |
| 274 | else | 288 | else |
| 275 | lua_pushstring(L, b); | 289 | lua_pushstring(L, b); |
| 276 | lua_freeline(L, b); | 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 | int status; | 298 | int status; |
| 283 | lua_settop(L, 0); | 299 | size_t len; const char *line; |
| 284 | if (!pushline(L, 1)) | 300 | lua_pushliteral(L, "return "); |
| 285 | return -1; /* no input */ | 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 | for (;;) { /* repeat until gets a complete line */ | 314 | for (;;) { /* repeat until gets a complete line */ |
| 287 | size_t l; | 315 | size_t len; |
| 288 | const char *line = lua_tolstring(L, 1, &l); | 316 | const char *line = lua_tolstring(L, 1, &len); /* get what it has */ |
| 289 | status = luaL_loadbuffer(L, line, l, "=stdin"); | 317 | int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ |
| 290 | if (!incomplete(L, status)) break; /* cannot try to add lines? */ | 318 | if (!incomplete(L, status) || !pushline(L, 0)) |
| 291 | if (!pushline(L, 0)) /* no more input? */ | 319 | return status; /* cannot/should not try to add continuation line */ |
| 292 | return -1; | 320 | lua_pushliteral(L, "\n"); /* add newline... */ |
| 293 | lua_pushliteral(L, "\n"); /* add a new line... */ | ||
| 294 | lua_insert(L, -2); /* ...between the two lines */ | 321 | lua_insert(L, -2); /* ...between the two lines */ |
| 295 | lua_concat(L, 3); /* join them */ | 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 | lua_saveline(L, 1); | 334 | lua_saveline(L, 1); |
| 298 | lua_remove(L, 1); /* remove line */ | 335 | lua_remove(L, 1); /* remove line */ |
| 336 | lua_assert(lua_gettop(L) == 1); | ||
| 299 | return status; | 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 | static int handle_luainit (lua_State *L) { | 461 | static int handle_luainit (lua_State *L) { |
| 424 | const char *name = "=" LUA_INITVERSION; | 462 | const char *name = "=" LUA_INITVARVERSION; |
| 425 | const char *init = getenv(name + 1); | 463 | const char *init = getenv(name + 1); |
| 426 | if (init == NULL) { | 464 | if (init == NULL) { |
| 427 | name = "=" LUA_INIT; | 465 | name = "=" LUA_INIT_VAR; |
| 428 | init = getenv(name + 1); /* try alternative name */ | 466 | init = getenv(name + 1); /* try alternative name */ |
| 429 | } | 467 | } |
| 430 | if (init == NULL) return LUA_OK; | 468 | if (init == NULL) return LUA_OK; |