From 4fb7bc4b147fd30b781ea2dad533956d0362295a Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Mon, 7 Mar 2022 13:49:21 +0000 Subject: [PATCH] readline: back-port changes needed to properly detect EOF This commit is a partial back-port of this upstream readline commit: commit 002d31aa5f5929eb32d0e0e2e8b8d35d99e59961 Author: Chet Ramey Date: Thu Mar 3 11:11:47 2022 -0500 add rl_eof_found to public API; fix pointer aliasing problems \ with history-search-backward; fix a display problem with \ runs of invisible characters at the end of a physical \ screen line I have only pulled in the parts of this commit that relate to the new rl_eof_found global, and the RL_STATE_EOF state flag. These changes are needed in order to fix PR cli/28833, and are discussed in this thread to the bug-readline mailing list: https://lists.gnu.org/archive/html/bug-readline/2022-02/msg00021.html The above commit is not yet in any official readline release, but my hope is that now it has been merged into the readline tree it should be safe enough to back port this fix to GDB's tree. At some point in the future we will inevitably want to roll forward the version of readline that we maintain in the binutils-gdb repository. When that day comes the changes in this commit can be replaced with the latest upstream readline code, as I have not changed the meaning of this code at all from what is in upstream readline. This commit alone does not fix the PR cli/28833 issue, for that see the next commit, which changes GDB itself. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28833 --- readline/readline/callback.c | 8 +++++++- readline/readline/doc/rltech.texi | 11 +++++++++++ readline/readline/readline.c | 19 ++++++++++++------- readline/readline/readline.h | 8 +++++++- readline/readline/rlprivate.h | 1 - readline/readline/rltty.c | 4 ++-- 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/readline/readline/callback.c b/readline/readline/callback.c index a466cf9b6ef..58b84d2e2ad 100644 --- a/readline/readline/callback.c +++ b/readline/readline/callback.c @@ -1,6 +1,6 @@ /* callback.c -- functions to use readline as an X `callback' mechanism. */ -/* Copyright (C) 1987-2017 Free Software Foundation, Inc. +/* Copyright (C) 1987-2022 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -136,6 +136,8 @@ rl_callback_read_char (void) abort (); } + eof = 0; + memcpy ((void *)olevel, (void *)_rl_top_level, sizeof (procenv_t)); #if defined (HAVE_POSIX_SIGSETJMP) jcode = sigsetjmp (_rl_top_level, 0); @@ -268,6 +270,10 @@ rl_callback_read_char (void) _rl_want_redisplay = 0; } + /* Make sure application hooks can see whether we saw EOF. */ + if (rl_eof_found = eof) + RL_SETSTATE(RL_STATE_EOF); + if (rl_done) { line = readline_internal_teardown (eof); diff --git a/readline/readline/doc/rltech.texi b/readline/readline/doc/rltech.texi index bbf57c239c9..797e34d95e0 100644 --- a/readline/readline/doc/rltech.texi +++ b/readline/readline/doc/rltech.texi @@ -323,6 +323,14 @@ and point define a @emph{region}. @deftypevar int rl_done Setting this to a non-zero value causes Readline to return the current line immediately. +Readline will set this variable when it has read a key sequence bound +to @code{accept-line} and is about to return the line to the caller. +@end deftypevar + +@deftypevar int rl_eof_found +Readline will set this variable when it has read an EOF character (e.g., the +stty @samp{EOF} character) on an empty line or encountered a read error and +is about to return a NULL line to the caller. @end deftypevar @deftypevar int rl_num_chars_to_read @@ -588,6 +596,9 @@ the current call to @code{readline()}. @item RL_STATE_DONE Readline has read a key sequence bound to @code{accept-line} and is about to return the line to the caller. +@item RL_STATE_EOF +Readline has read an EOF character (e.g., the stty @samp{EOF} character) +or encountered a read error and is about to return a NULL line to the caller. @end table @end deftypevar diff --git a/readline/readline/readline.c b/readline/readline/readline.c index e61d188bbe9..0e33587f234 100644 --- a/readline/readline/readline.c +++ b/readline/readline/readline.c @@ -1,7 +1,7 @@ /* readline.c -- a general facility for reading lines of input with emacs style editing and completion. */ -/* Copyright (C) 1987-2020 Free Software Foundation, Inc. +/* Copyright (C) 1987-2022 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -165,6 +165,9 @@ int rl_end; /* Make this non-zero to return the current input_line. */ int rl_done; +/* If non-zero when readline_internal returns, it means we found EOF */ +int rl_eof_found = 0; + /* The last function executed by readline. */ rl_command_func_t *rl_last_func = (rl_command_func_t *)NULL; @@ -218,9 +221,6 @@ int _rl_eof_char = CTRL ('D'); /* Non-zero makes this the next keystroke to read. */ int rl_pending_input = 0; -/* If non-zero when readline_internal returns, it means we found EOF */ -int _rl_eof_found = 0; - /* Pointer to a useful terminal name. */ const char *rl_terminal_name = (const char *)NULL; @@ -474,6 +474,9 @@ readline_internal_teardown (int eof) RL_CHECK_SIGNALS (); + if (eof) + RL_SETSTATE (RL_STATE_EOF); /* XXX */ + /* Restore the original of this history line, iff the line that we are editing was originally in the history, AND the line has changed. */ entry = current_history (); @@ -596,6 +599,7 @@ readline_internal_charloop (void) RL_SETSTATE(RL_STATE_DONE); return (rl_done = 1); #else + RL_SETSTATE(RL_STATE_EOF); eof_found = 1; break; #endif @@ -636,6 +640,7 @@ readline_internal_charloop (void) RL_SETSTATE(RL_STATE_DONE); return (rl_done = 1); #else + RL_SETSTATE(RL_STATE_EOF); eof_found = 1; break; #endif @@ -703,8 +708,8 @@ static char * readline_internal (void) { readline_internal_setup (); - _rl_eof_found = readline_internal_charloop (); - return (readline_internal_teardown (_rl_eof_found)); + rl_eof_found = readline_internal_charloop (); + return (readline_internal_teardown (rl_eof_found)); } void @@ -1161,7 +1166,7 @@ rl_initialize (void) /* We aren't done yet. We haven't even gotten started yet! */ rl_done = 0; - RL_UNSETSTATE(RL_STATE_DONE); + RL_UNSETSTATE(RL_STATE_DONE|RL_STATE_EOF); /* Tell the history routines what is going on. */ _rl_start_using_history (); diff --git a/readline/readline/readline.h b/readline/readline/readline.h index 78fa39d02a1..9f79810f290 100644 --- a/readline/readline/readline.h +++ b/readline/readline/readline.h @@ -1,6 +1,6 @@ /* Readline.h -- the names of functions callable from within readline. */ -/* Copyright (C) 1987-2020 Free Software Foundation, Inc. +/* Copyright (C) 1987-2022 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -553,6 +553,10 @@ extern int rl_mark; line and should return it. */ extern int rl_done; +/* Flag to indicate that readline has read an EOF character or read has + returned 0 or error, and is returning a NULL line as a result. */ +extern int rl_eof_found; + /* If set to a character value, that will be the next keystroke read. */ extern int rl_pending_input; @@ -906,6 +910,8 @@ extern int rl_persistent_signal_handlers; #define RL_STATE_REDISPLAYING 0x1000000 /* updating terminal display */ #define RL_STATE_DONE 0x2000000 /* done; accepted line */ +#define RL_STATE_EOF 0x8000000 /* done; got eof on read */ + #define RL_SETSTATE(x) (rl_readline_state |= (x)) #define RL_UNSETSTATE(x) (rl_readline_state &= ~(x)) diff --git a/readline/readline/rlprivate.h b/readline/readline/rlprivate.h index 23ab2d8cec0..02838ae21ae 100644 --- a/readline/readline/rlprivate.h +++ b/readline/readline/rlprivate.h @@ -544,7 +544,6 @@ extern FILE *_rl_in_stream; extern FILE *_rl_out_stream; extern int _rl_last_command_was_kill; extern int _rl_eof_char; -extern int _rl_eof_found; extern procenv_t _rl_top_level; extern _rl_keyseq_cxt *_rl_kscxt; extern int _rl_keyseq_timeout; diff --git a/readline/readline/rltty.c b/readline/readline/rltty.c index d0cd572713a..11997b7c9d8 100644 --- a/readline/readline/rltty.c +++ b/readline/readline/rltty.c @@ -1,7 +1,7 @@ /* rltty.c -- functions to prepare and restore the terminal for readline's use. */ -/* Copyright (C) 1992-2017 Free Software Foundation, Inc. +/* Copyright (C) 1992-2022 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -692,7 +692,7 @@ rl_deprep_terminal (void) if (terminal_prepped & TPX_BRACKPASTE) { fprintf (rl_outstream, BRACK_PASTE_FINI); - if (_rl_eof_found) + if (rl_eof_found) fprintf (rl_outstream, "\n"); } -- 2.30.2