From 4bc2092e2f15a6422774c34c7f8fa4079be67d08 Mon Sep 17 00:00:00 2001 From: Vitaly Magerya Date: Wed, 21 Jun 2023 20:48:36 +0200 Subject: [PATCH] [PATCH] Make ginsh evaluate line-by-line in non-interactive mode. When used interactively ginsh consumes the input line-by-line, so if a complete expression is entered it is enough to end the line to get an evaluated result. In the non-interative mode (i.e. if the input is not a tty) the input is consumed in blocks, making it impossible to use ginsh as a subprocess. This patch makes the non-interactive mode reuse the same line parsing logic as the interactive mode (without readline), but skips printing the "> " prompt, preserving the backward compatibility. --- ginsh/ginsh_lexer.lpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ginsh/ginsh_lexer.lpp b/ginsh/ginsh_lexer.lpp index 8692b1ac..5169ecef 100644 --- a/ginsh/ginsh_lexer.lpp +++ b/ginsh/ginsh_lexer.lpp @@ -203,8 +203,16 @@ static int ginsh_input(char *buf, int max_size) YY_FATAL_ERROR("input in flex scanner failed"); result = n; #endif - } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin)) - YY_FATAL_ERROR("input in flex scanner failed"); + } else { + int c = '*', n; + for (n = 0; n < max_size && (c = getc(yyin)) != EOF && c != '\n'; ++n) + buf[n] = (char)c; + if (c == '\n') + buf[n++] = (char)c; + if (c == EOF && ferror(yyin)) + YY_FATAL_ERROR("input in flex scanner failed"); + result = n; + } return result; } -- 2.45.0