diff options
author | Lua Team <team@lua.org> | 2015-06-03 12:00:00 +0000 |
---|---|---|
committer | repogen <> | 2015-06-03 12:00:00 +0000 |
commit | 6ddf2b3ca86471f90439976967c9e608582f0d0b (patch) | |
tree | 7fa563fe4062fcea38b86adfe8a26ac413f0d97b /src/lua.c | |
parent | 34c362812ab38172d3da36404ec9a85f929579c5 (diff) | |
download | lua-github-5.3.1-rc1.tar.gz |
Lua 5.3.1-rc15.3.1-rc1
Diffstat (limited to 'src/lua.c')
-rw-r--r-- | src/lua.c | 36 |
1 files changed, 19 insertions, 17 deletions
@@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.222 2014/11/11 19:41:27 roberto Exp $ +** $Id: lua.c,v 1.225 2015/03/30 15:42:59 roberto Exp $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -80,9 +80,7 @@ #include <readline/readline.h> #include <readline/history.h> #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,idx) \ - if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \ - add_history(lua_tostring(L, idx)); /* add it to history */ +#define lua_saveline(L,line) ((void)L, add_history(line)) #define lua_freeline(L,b) ((void)L, free(b)) #else /* }{ */ @@ -90,7 +88,7 @@ #define lua_readline(L,b,p) \ ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ -#define lua_saveline(L,idx) { (void)L; (void)idx; } +#define lua_saveline(L,line) { (void)L; (void)line; } #define lua_freeline(L,b) { (void)L; (void)b; } #endif /* } */ @@ -315,11 +313,11 @@ static int pushline (lua_State *L, int firstline) { lua_pop(L, 1); /* remove prompt */ l = strlen(b); if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ - b[l-1] = '\0'; /* remove it */ + b[--l] = '\0'; /* remove it */ if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ else - lua_pushstring(L, b); + lua_pushlstring(L, b, l); lua_freeline(L, b); return 1; } @@ -336,8 +334,12 @@ static int addreturn (lua_State *L) { lua_pushvalue(L, -2); /* duplicate line */ lua_concat(L, 2); /* new line is "return ..." */ line = lua_tolstring(L, -1, &len); - if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) + if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) { lua_remove(L, -3); /* remove original line */ + line += sizeof("return")/sizeof(char); /* remove 'return' for history */ + if (line[0] != '\0') /* non empty? */ + lua_saveline(L, line); /* keep history */ + } else lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */ return status; @@ -352,8 +354,10 @@ static int multiline (lua_State *L) { size_t len; const char *line = lua_tolstring(L, 1, &len); /* get what it has */ int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ - if (!incomplete(L, status) || !pushline(L, 0)) + if (!incomplete(L, status) || !pushline(L, 0)) { + lua_saveline(L, line); /* keep history */ return status; /* cannot or should not try to add continuation line */ + } lua_pushliteral(L, "\n"); /* add newline... */ lua_insert(L, -2); /* ...between the two lines */ lua_concat(L, 3); /* join them */ @@ -374,7 +378,6 @@ static int loadline (lua_State *L) { return -1; /* no input */ if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ status = multiline(L); /* try as command, maybe with continuation lines */ - lua_saveline(L, 1); /* keep history */ lua_remove(L, 1); /* remove line from the stack */ lua_assert(lua_gettop(L) == 1); return status; @@ -482,14 +485,14 @@ static int collectargs (char **argv, int *first) { args |= has_E; break; case 'i': - args |= has_i; /* goes through (-i implies -v) */ + args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ case 'v': if (argv[i][2] != '\0') /* extra characters after 1st? */ return has_error; /* invalid option */ args |= has_v; break; case 'e': - args |= has_e; /* go through */ + args |= has_e; /* FALLTHROUGH */ case 'l': /* both options need an argument */ if (argv[i][2] == '\0') { /* no concatenated argument? */ i++; /* try next 'argv' */ @@ -513,17 +516,16 @@ static int collectargs (char **argv, int *first) { static int runargs (lua_State *L, char **argv, int n) { int i; for (i = 1; i < n; i++) { - int status; int option = argv[i][1]; lua_assert(argv[i][0] == '-'); /* already checked */ if (option == 'e' || option == 'l') { + int status; const char *extra = argv[i] + 2; /* both options need an argument */ if (*extra == '\0') extra = argv[++i]; lua_assert(extra != NULL); - if (option == 'e') - status = dostring(L, extra, "=(command line)"); - else - status = dolibrary(L, extra); + status = (option == 'e') + ? dostring(L, extra, "=(command line)") + : dolibrary(L, extra); if (status != LUA_OK) return 0; } } |