All of lore.kernel.org
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Git Mailing List <git@vger.kernel.org>
Cc: carenas@gmail.com,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: [PATCH 3/4] terminal: work around macos poll() bug
Date: Fri,  4 Mar 2022 13:11:25 +0000	[thread overview]
Message-ID: <20220304131126.8293-4-phillip.wood123@gmail.com> (raw)
In-Reply-To: <20220304131126.8293-1-phillip.wood123@gmail.com>

From: Phillip Wood <phillip.wood@dunelm.org.uk>

On macos the builtin "add -p" does not handle keys that generate
escape sequences because poll() does not work with terminals
there. Switch to using select() on non-windows platforms to work
around this.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 compat/terminal.c | 43 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/compat/terminal.c b/compat/terminal.c
index 249836e78f..5d516ff546 100644
--- a/compat/terminal.c
+++ b/compat/terminal.c
@@ -82,6 +82,32 @@ static int enable_non_canonical(unsigned flags)
 	return disable_bits(flags, ICANON | ECHO);
 }
 
+/*
+ * On macos it is not possible to use poll() with a terminal so use select
+ * instead.
+ */
+#include <sys/select.h>
+static int getchar_with_timeout(int timeout)
+{
+	struct timeval tv, *tvp = NULL;
+	fd_set readfds;
+	int res;
+
+	if (timeout >= 0) {
+		tv.tv_sec = timeout / 1000;
+		tv.tv_usec = (timeout % 1000) * 1000;
+		tvp = &tv;
+	}
+
+	FD_ZERO(&readfds);
+	FD_SET(0, &readfds);
+	res = select(1, &readfds, NULL, NULL, tvp);
+	if (res < 0)
+		return EOF;
+
+	return getchar();
+}
+
 #elif defined(GIT_WINDOWS_NATIVE)
 
 #define INPUT_PATH "CONIN$"
@@ -247,6 +273,16 @@ static int mingw_getchar(void)
 }
 #define getchar mingw_getchar
 
+static int getchar_with_timeout(int timeout)
+{
+	struct pollfd pfd = { .fd = 0, .events = POLLIN };
+
+	if (poll(&pfd, 1, timeout) < 1)
+		return EOF;
+
+	return getchar();
+}
+
 #endif
 
 #ifndef FORCE_TEXT
@@ -397,12 +433,7 @@ int read_key_without_echo(struct strbuf *buf)
 		 * half a second when we know that the sequence is complete.
 		 */
 		while (!is_known_escape_sequence(buf->buf)) {
-			struct pollfd pfd = { .fd = 0, .events = POLLIN };
-
-			if (poll(&pfd, 1, 500) < 1)
-				break;
-
-			ch = getchar();
+			ch = getchar_with_timeout(500);
 			if (ch == EOF)
 				return 0;
 			strbuf_addch(buf, ch);
-- 
2.35.1


  parent reply	other threads:[~2022-03-04 13:11 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04 13:11 [PATCH 0/4] builtin add -p: hopefully final readkey fixes Phillip Wood
2022-03-04 13:11 ` [PATCH 1/4] terminal: use flags for save_term() Phillip Wood
2022-03-04 20:40   ` Ramsay Jones
2022-03-07 11:11     ` Phillip Wood
2022-03-07 20:21       ` Ramsay Jones
2022-03-08 10:41         ` Phillip Wood
2022-03-05 14:02   ` Ævar Arnfjörð Bjarmason
2022-03-07 10:45     ` Phillip Wood
2022-03-07 12:06       ` Ævar Arnfjörð Bjarmason
2022-03-04 13:11 ` [PATCH 2/4] terminal: don't assume stdin is /dev/tty Phillip Wood
2022-03-04 20:42   ` Ramsay Jones
2022-03-04 13:11 ` Phillip Wood [this message]
2022-03-04 13:11 ` [PATCH 4/4] terminal: restore settings on SIGTSTP Phillip Wood
2022-03-05 13:59   ` Ævar Arnfjörð Bjarmason
2022-03-07 10:53     ` Phillip Wood
2022-03-07 11:49       ` Ævar Arnfjörð Bjarmason
2022-03-07 13:49         ` Phillip Wood
2022-03-07 14:45           ` Ævar Arnfjörð Bjarmason
2022-03-08 10:54             ` Phillip Wood
2022-03-09 12:19   ` Johannes Schindelin
2022-03-10 16:06     ` Phillip Wood
2022-03-09 11:03 ` [PATCH v2 0/4] builtin add -p: hopefully final readkey fixes Phillip Wood
2022-03-09 11:03   ` [PATCH v2 1/4] terminal: use flags for save_term() Phillip Wood
2022-03-11 16:52     ` Carlo Arenas
2022-03-14 10:49       ` Phillip Wood
2022-03-09 11:03   ` [PATCH v2 2/4] terminal: don't assume stdin is /dev/tty Phillip Wood
2022-03-09 11:03   ` [PATCH v2 3/4] terminal: work around macos poll() bug Phillip Wood
2022-03-10 13:35     ` Ævar Arnfjörð Bjarmason
2022-03-10 16:02       ` Phillip Wood
2022-03-10 18:02         ` Junio C Hamano
2022-03-09 11:03   ` [PATCH v2 4/4] terminal: restore settings on SIGTSTP Phillip Wood
2022-03-09 23:10   ` [PATCH v2 0/4] builtin add -p: hopefully final readkey fixes Junio C Hamano
2022-03-09 23:37     ` Junio C Hamano
2022-03-10 13:28       ` Phillip Wood
2022-03-10 18:18         ` Phillip Wood
2022-03-10 18:53           ` Junio C Hamano
2022-03-10 13:25   ` Johannes Schindelin
2022-03-10 16:08     ` Phillip Wood
2022-03-15 10:57 ` [PATCH v3 " Phillip Wood
2022-03-15 10:57   ` [PATCH v3 1/4] terminal: use flags for save_term() Phillip Wood
2022-03-15 10:57   ` [PATCH v3 2/4] terminal: don't assume stdin is /dev/tty Phillip Wood
2022-03-15 17:42     ` Junio C Hamano
2022-03-15 18:01       ` rsbecker
2022-03-15 19:05         ` Junio C Hamano
2022-03-15 19:38           ` rsbecker
2022-03-15 10:57   ` [PATCH v3 3/4] terminal: work around macos poll() bug Phillip Wood
2022-03-15 10:57   ` [PATCH v3 4/4] terminal: restore settings on SIGTSTP Phillip Wood
2022-03-15 17:51     ` Junio C Hamano
2022-03-16 18:54 ` [PATCH v4 0/4] builtin add -p: hopefully final readkey fixes Phillip Wood
2022-03-16 18:54   ` [PATCH v4 1/4] terminal: use flags for save_term() Phillip Wood
2022-03-16 18:54   ` [PATCH v4 2/4] terminal: don't assume stdin is /dev/tty Phillip Wood
2022-03-16 18:54   ` [PATCH v4 3/4] terminal: work around macos poll() bug Phillip Wood
2022-03-16 18:54   ` [PATCH v4 4/4] terminal: restore settings on SIGTSTP Phillip Wood

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=20220304131126.8293-4-phillip.wood123@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=carenas@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=phillip.wood@dunelm.org.uk \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.