From 91493254000520634be208201398d312a8635c83 Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Sun, 19 Mar 2006 19:25:29 +0000
Subject: [PATCH] in get_key_buffer(), if we fail to get a character over
 MAX_BUF_SIZE times in a row, hang up regardless of the value of errno; this
 fixes a problem where nano doesn't terminate properly under xterm if the user
 su's to root, runs nano, and then closes the terminal window

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3309 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog   |  6 ++++++
 src/nano.h  |  2 +-
 src/winio.c | 12 +++++++++---
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 374d9e10..aab8dc3e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -58,6 +58,12 @@ CVS code -
 	  set errno to EINVAL as well as return -1 if they fail.  This
 	  matches the manual page. (DLR)
 - winio.c:
+  get_key_buffer()
+	- If we fail to get a character over MAX_BUF_SIZE times in a
+	  row, hang up regardless of the value of errno.  This fixes a
+	  problem where nano doesn't terminate properly under xterm if
+	  the user su's to root, runs nano, and then closes the terminal
+	  window. (DLR, found by John <acocaracha@gmail.com>)
   parse_kbinput()
 	- Interpret Shift-Begin, Shift-Delete, Shift-End, Shift-Home,
 	  Shift-Insert, and Shift-Suspend as Begin, Delete, End, Home,
diff --git a/src/nano.h b/src/nano.h
index 358d317a..bfd6a512 100644
--- a/src/nano.h
+++ b/src/nano.h
@@ -590,7 +590,7 @@ typedef struct rcoption {
  * counting the blank lines at their ends. */
 #define MAX_SEARCH_HISTORY 100
 
-/* The maximum number of bytes we read into a buffer at one time. */
+/* The maximum number of bytes buffered at one time. */
 #define MAX_BUF_SIZE 128
 
 #endif /* !NANO_H */
diff --git a/src/winio.c b/src/winio.c
index 20914635..f2b6f4b9 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -122,7 +122,7 @@ void reset_kbinput(void)
  * default keystroke buffer is empty. */
 void get_key_buffer(WINDOW *win)
 {
-    int input;
+    int input, errcount;
 
     /* If the keystroke buffer isn't empty, get out. */
     if (key_buffer != NULL)
@@ -137,10 +137,16 @@ void get_key_buffer(WINDOW *win)
      * screen updates. */
     doupdate();
 
+    errcount = 0;
     while ((input = wgetch(win)) == ERR) {
+	errcount++;
+
 	/* If errno is EIO, it means that the input source that we were
-	 * using is gone, so die gracefully. */
-	if (errno == EIO)
+	 * using is gone, so die gracefully.  If we've failed to get a
+	 * character over MAX_BUF_SIZE times in a row, it can mean the
+	 * same thing regardless of the value of errno, so die
+	 * gracefully then too. */
+	if (errno == EIO || errcount > MAX_BUF_SIZE)
 	    handle_hupterm(0);
     }
 
-- 
GitLab