public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 1/2] readline: back-port changes needed to properly detect EOF
Date: Mon,  7 Mar 2022 15:13:14 +0000	[thread overview]
Message-ID: <452f0981e2ae041ec22b46da254e742b9d62730d.1646665813.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1646665813.git.aburgess@redhat.com>

This commit is a partial back-port of this upstream readline commit:

  commit 002d31aa5f5929eb32d0e0e2e8b8d35d99e59961
  Author: Chet Ramey <chet.ramey@case.edu>
  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 the
next commit, which changes GDB itself, is needed.

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 @@
 @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 @@
 @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.25.4


  reply	other threads:[~2022-03-07 15:13 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-07 15:13 [PATCH 0/2] Fix GDB prompt corruption issue Andrew Burgess
2022-03-07 15:13 ` Andrew Burgess [this message]
2022-03-09 17:45   ` [PATCH 1/2] readline: back-port changes needed to properly detect EOF Tom Tromey
2022-03-07 15:13 ` [PATCH 2/2] gdb: handle bracketed-paste-mode and ctrl-d correctly Andrew Burgess
2022-03-07 17:10   ` Eli Zaretskii
2022-03-08  9:45     ` Andrew Burgess
2022-03-08 12:10       ` Eli Zaretskii
2022-03-14 10:23 ` [PATCHv2 0/3] Fix GDB prompt corruption issue Andrew Burgess
2022-03-14 10:23   ` [PATCHv2 1/3] gdb: work around prompt corruption caused by bracketed-paste-mode Andrew Burgess
2022-03-16 17:31     ` Tom Tromey
2022-03-16 20:45       ` Andrew Burgess
2022-03-17  8:22         ` Aktemur, Tankut Baris
2022-03-25 18:32     ` Simon Marchi
2022-03-26 14:02       ` Andrew Burgess
2022-03-27  0:59         ` Simon Marchi
2022-03-14 10:23   ` [PATCHv2 2/3] readline: back-port changes needed to properly detect EOF Andrew Burgess
2022-03-14 10:23   ` [PATCHv2 3/3] gdb: handle bracketed-paste-mode and EOF correctly Andrew Burgess
2022-03-21 15:58   ` [PATCHv3 0/2] Fix GDB prompt corruption issue Andrew Burgess
2022-03-21 15:58     ` [PATCHv3 1/2] readline: back-port changes needed to properly detect EOF Andrew Burgess
2022-03-21 15:58     ` [PATCHv3 2/2] gdb: handle bracketed-paste-mode and EOF correctly Andrew Burgess
2022-03-29 14:26     ` [PATCHv4 0/3] Fix GDB prompt corruption issue Andrew Burgess
2022-03-29 14:26       ` [PATCHv4 1/3] gdb: improved EOF handling when using readline 7 Andrew Burgess
2022-03-29 14:26       ` [PATCHv4 2/3] readline: back-port changes needed to properly detect EOF Andrew Burgess
2022-03-29 14:26       ` [PATCHv4 3/3] gdb: handle bracketed-paste-mode and EOF correctly Andrew Burgess
2022-04-21 16:49       ` [PATCHv4 0/3] Fix GDB prompt corruption issue Andrew Burgess
2022-04-22 17:52         ` Andrew Burgess
2022-04-26 14:27           ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=452f0981e2ae041ec22b46da254e742b9d62730d.1646665813.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).