git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/4] terminal: work around macos poll() bug

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Mar 9, 2022, 11:03 UTC
Message-ID
<20220309110325.36917-4-phillip.wood123@gmail.com>
In-Reply-To
<20220309110325.36917-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 cb653419c3..4189cbb12c 100644
--- a/compat/terminal.c
+++ b/compat/terminal.c
@@ -82,6 +82,32 @@ static int enable_non_canonical(enum save_term_flags 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
Previous: Phillip WoodNext: Ævar Arnfjörð Bjarmason
Message 27 of 53 in “builtin add -p: hopefully final readkey fixes”
  1. 0/4 builtin add -p: hopefully final readkey fixesPhillip Wood, Mar 4, 2022
  2. 1/4 terminal: use flags for save_term()Phillip Wood, Mar 4, 2022
  3. Ramsay JonesMar 4, 2022
  4. Phillip WoodMar 7, 2022
  5. Ramsay JonesMar 7, 2022
  6. Phillip WoodMar 8, 2022
  7. Ævar Arnfjörð BjarmasonMar 5, 2022
  8. Phillip WoodMar 7, 2022
  9. Ævar Arnfjörð BjarmasonMar 7, 2022
  10. 3/4 terminal: work around macos poll() bugPhillip Wood, Mar 4, 2022
  11. 2/4 terminal: don't assume stdin is /dev/ttyPhillip Wood, Mar 4, 2022
  12. Ramsay JonesMar 4, 2022
  13. 4/4 terminal: restore settings on SIGTSTPPhillip Wood, Mar 4, 2022
  14. Ævar Arnfjörð BjarmasonMar 5, 2022
  15. Phillip WoodMar 7, 2022
  16. Ævar Arnfjörð BjarmasonMar 7, 2022
  17. Phillip WoodMar 7, 2022
  18. Ævar Arnfjörð BjarmasonMar 7, 2022
  19. Phillip WoodMar 8, 2022
  20. Johannes SchindelinMar 9, 2022
  21. Phillip WoodMar 10, 2022
  22. 0/4 builtin add -p: hopefully final readkey fixesPhillip Wood, Mar 9, 2022
  23. 1/4 terminal: use flags for save_term()Phillip Wood, Mar 9, 2022
  24. Carlo ArenasMar 11, 2022
  25. Phillip WoodMar 14, 2022
  26. 2/4 terminal: don't assume stdin is /dev/ttyPhillip Wood, Mar 9, 2022
  27. 3/4 terminal: work around macos poll() bugPhillip Wood, Mar 9, 2022
  28. Ævar Arnfjörð BjarmasonMar 10, 2022
  29. Phillip WoodMar 10, 2022
  30. Junio C HamanoMar 10, 2022
  31. 4/4 terminal: restore settings on SIGTSTPPhillip Wood, Mar 9, 2022
  32. Junio C HamanoMar 9, 2022
  33. Junio C HamanoMar 9, 2022
  34. Phillip WoodMar 10, 2022
  35. Phillip WoodMar 10, 2022
  36. Junio C HamanoMar 10, 2022
  37. Johannes SchindelinMar 10, 2022
  38. Phillip WoodMar 10, 2022
  39. 0/4 builtin add -p: hopefully final readkey fixesPhillip Wood, Mar 15, 2022
  40. 2/4 terminal: don't assume stdin is /dev/ttyPhillip Wood, Mar 15, 2022
  41. Junio C HamanoMar 15, 2022
  42. rsbecker@nexbridge.comMar 15, 2022
  43. Junio C HamanoMar 15, 2022
  44. rsbecker@nexbridge.comMar 15, 2022
  45. 1/4 terminal: use flags for save_term()Phillip Wood, Mar 15, 2022
  46. 4/4 terminal: restore settings on SIGTSTPPhillip Wood, Mar 15, 2022
  47. Junio C HamanoMar 15, 2022
  48. 3/4 terminal: work around macos poll() bugPhillip Wood, Mar 15, 2022
  49. 0/4 builtin add -p: hopefully final readkey fixesPhillip Wood, Mar 16, 2022
  50. 1/4 terminal: use flags for save_term()Phillip Wood, Mar 16, 2022
  51. 2/4 terminal: don't assume stdin is /dev/ttyPhillip Wood, Mar 16, 2022
  52. 3/4 terminal: work around macos poll() bugPhillip Wood, Mar 16, 2022
  53. 4/4 terminal: restore settings on SIGTSTPPhillip Wood, Mar 16, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.