aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-07-05 15:13:46 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-07-05 15:13:46 -0300
commit93fd6892f85ecd8a4e82d2339016a9f71a42d0e8 (patch)
tree622b835f00c3d0e2bd63d5e092686c6d6312126b
parent193bf7919ea97a2d1a98734e1a215ee6d3fc021b (diff)
downloadlua-93fd6892f85ecd8a4e82d2339016a9f71a42d0e8.tar.gz
lua-93fd6892f85ecd8a4e82d2339016a9f71a42d0e8.tar.bz2
lua-93fd6892f85ecd8a4e82d2339016a9f71a42d0e8.zip
Fixed bug in 'multiline'
'incomplete' was popping error message that should be used in case there is no more lines to complete the input, that is, 'pushline' returns NULL, due to end of file.
-rw-r--r--lua.c28
-rw-r--r--testes/main.lua5
2 files changed, 19 insertions, 14 deletions
diff --git a/lua.c b/lua.c
index 51979a8b..bff6a8f5 100644
--- a/lua.c
+++ b/lua.c
@@ -544,10 +544,8 @@ static int incomplete (lua_State *L, int status) {
544 if (status == LUA_ERRSYNTAX) { 544 if (status == LUA_ERRSYNTAX) {
545 size_t lmsg; 545 size_t lmsg;
546 const char *msg = lua_tolstring(L, -1, &lmsg); 546 const char *msg = lua_tolstring(L, -1, &lmsg);
547 if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) { 547 if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0)
548 lua_pop(L, 1);
549 return 1; 548 return 1;
550 }
551 } 549 }
552 return 0; /* else... */ 550 return 0; /* else... */
553} 551}
@@ -561,9 +559,9 @@ static int pushline (lua_State *L, int firstline) {
561 size_t l; 559 size_t l;
562 const char *prmt = get_prompt(L, firstline); 560 const char *prmt = get_prompt(L, firstline);
563 char *b = lua_readline(buffer, prmt); 561 char *b = lua_readline(buffer, prmt);
564 if (b == NULL)
565 return 0; /* no input (prompt will be popped by caller) */
566 lua_pop(L, 1); /* remove prompt */ 562 lua_pop(L, 1); /* remove prompt */
563 if (b == NULL)
564 return 0; /* no input */
567 l = strlen(b); 565 l = strlen(b);
568 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ 566 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
569 b[--l] = '\0'; /* remove it */ 567 b[--l] = '\0'; /* remove it */
@@ -584,11 +582,8 @@ static int addreturn (lua_State *L) {
584 const char *line = lua_tostring(L, -1); /* original line */ 582 const char *line = lua_tostring(L, -1); /* original line */
585 const char *retline = lua_pushfstring(L, "return %s;", line); 583 const char *retline = lua_pushfstring(L, "return %s;", line);
586 int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); 584 int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
587 if (status == LUA_OK) { 585 if (status == LUA_OK)
588 lua_remove(L, -2); /* remove modified line */ 586 lua_remove(L, -2); /* remove modified line */
589 if (line[0] != '\0') /* non empty? */
590 lua_saveline(line); /* keep history */
591 }
592 else 587 else
593 lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ 588 lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
594 return status; 589 return status;
@@ -596,17 +591,18 @@ static int addreturn (lua_State *L) {
596 591
597 592
598/* 593/*
599** Read multiple lines until a complete Lua statement 594** Read multiple lines until a complete Lua statement or an error not
595** for an incomplete statement. Start with first line already read in
596** the stack.
600*/ 597*/
601static int multiline (lua_State *L) { 598static int multiline (lua_State *L) {
602 for (;;) { /* repeat until gets a complete statement */ 599 for (;;) { /* repeat until gets a complete statement */
603 size_t len; 600 size_t len;
604 const char *line = lua_tolstring(L, 1, &len); /* get what it has */ 601 const char *line = lua_tolstring(L, 1, &len); /* get what it has */
605 int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ 602 int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
606 if (!incomplete(L, status) || !pushline(L, 0)) { 603 if (!incomplete(L, status) || !pushline(L, 0))
607 lua_saveline(line); /* keep history */ 604 return status; /* should not or cannot try to add continuation line */
608 return status; /* cannot or should not try to add continuation line */ 605 lua_remove(L, -2); /* remove error message (from incomplete line) */
609 }
610 lua_pushliteral(L, "\n"); /* add newline... */ 606 lua_pushliteral(L, "\n"); /* add newline... */
611 lua_insert(L, -2); /* ...between the two lines */ 607 lua_insert(L, -2); /* ...between the two lines */
612 lua_concat(L, 3); /* join them */ 608 lua_concat(L, 3); /* join them */
@@ -621,12 +617,16 @@ static int multiline (lua_State *L) {
621** in the top of the stack. 617** in the top of the stack.
622*/ 618*/
623static int loadline (lua_State *L) { 619static int loadline (lua_State *L) {
620 const char *line;
624 int status; 621 int status;
625 lua_settop(L, 0); 622 lua_settop(L, 0);
626 if (!pushline(L, 1)) 623 if (!pushline(L, 1))
627 return -1; /* no input */ 624 return -1; /* no input */
628 if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ 625 if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
629 status = multiline(L); /* try as command, maybe with continuation lines */ 626 status = multiline(L); /* try as command, maybe with continuation lines */
627 line = lua_tostring(L, 1);
628 if (line[0] != '\0') /* non empty? */
629 lua_saveline(line); /* keep history */
630 lua_remove(L, 1); /* remove line from the stack */ 630 lua_remove(L, 1); /* remove line from the stack */
631 lua_assert(lua_gettop(L) == 1); 631 lua_assert(lua_gettop(L) == 1);
632 return status; 632 return status;
diff --git a/testes/main.lua b/testes/main.lua
index 9a86fb5a..17fbcb61 100644
--- a/testes/main.lua
+++ b/testes/main.lua
@@ -349,6 +349,11 @@ prepfile("a = [[b\nc\nd\ne]]\n=a")
349RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) 349RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
350checkprogout("b\nc\nd\ne\n\n") 350checkprogout("b\nc\nd\ne\n\n")
351 351
352-- input interrupted in continuation line
353prepfile("a.\n")
354RUN([[lua -i < %s > /dev/null 2> %s]], prog, out)
355checkprogout("near <eof>\n")
356
352local prompt = "alo" 357local prompt = "alo"
353prepfile[[ -- 358prepfile[[ --
354a = 2 359a = 2