From 805da5c405847547f19a8e7967c4e32abc05b790 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 5 Sep 2024 23:09:30 +0200 Subject: Perform line continuation in non-interactive mode only --- src/lexer.l | 18 +++++++++++++++--- src/main.c | 11 +++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/lexer.l b/src/lexer.l index 8715932..3b4a2e2 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -1,10 +1,17 @@ %{ #include +#include #include #include "parser.h" #include "pinocchio.h" +#define YY_USER_INIT \ + do { \ + if (!interactive) \ + BEGIN(linecont); \ + } while (false); + #define YY_USER_ACTION \ do { \ yylloc.first_column = yylloc.last_column; \ @@ -16,8 +23,9 @@ } else \ yylloc.last_column++; \ } \ - } while (0); + } while (false); +extern bool interactive; extern const char *current_file; %} @@ -28,6 +36,7 @@ extern const char *current_file; ws [ \t] %x error +%s linecont %% @@ -43,8 +52,11 @@ ws [ \t] \\ { return '\\'; } \n { return EOL; } - /* Allow line-continuation when the newline is suffixed by a backslash */ -\n{ws}*\\ ; + /* Allow line-continuation when the newline is suffixed by a + backslash, but not in interactive mode! Interactive usage should + have this functionality disabled so that you get instant feedback + after hitting the enter key. */ +\n{ws}*\\ ; [a-zA-Z] { yylval.ch = *yytext; diff --git a/src/main.c b/src/main.c index 3e48469..81fe189 100644 --- a/src/main.c +++ b/src/main.c @@ -40,10 +40,10 @@ typedef enum { } bool_style_t; static int rv; -static bool interactive; static bool_style_t bflag = BS_BINARY; static tbl_style_t tflag = TS_UNSET; +bool interactive; const char *current_file; static void astprocess_cli(asts_t); @@ -70,7 +70,6 @@ main(int argc, char **argv) { argv[0] = basename(argv[0]); setlocale(LC_ALL, ""); - interactive = isatty(STDIN_FILENO); int opt; const char *sflag = NULL; @@ -131,6 +130,14 @@ usage: if (sflag != NULL && argc != 0) goto usage; + /* We’re considered to be interactive if stdin is a TTY (meaning it’s + not a pipe or something) and no files have been specified on the + command-line. We should also take care to check for -s, because + that implies non-interactive usage. We intentionally consider + ‘interactive’ usage with the command-line argument ‘-’ to not be + interactive. */ + interactive = sflag == NULL && argc == 0 && isatty(STDIN_FILENO); + if (argc == 0) { if (sflag != NULL && (yyin = fmemopen((char *)sflag, strlen(sflag), "r")) == NULL) -- cgit v1.2.3