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

[PATCH v3 1/2] add interface for /dev/tty interaction

From
Tay Ray Chuan <rctay89@gmail.com>
Date
Aug 5, 2012, 18:45 UTC
Message-ID
<1344192340-19415-2-git-send-email-rctay89@gmail.com>
In-Reply-To
<1344192340-19415-1-git-send-email-rctay89@gmail.com>

Factor out the opening and closing of /dev/tty from git_terminal_prompt(), so that callers may first test if a controlling terminal is available before proceeding with prompting proper.

When HAVE_DEV_TTY is not defined, terminal_open() falls back to checking tty-ness of stdin and stderr, as getpass() uses them both.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
 compat/terminal.c | 52 ++++++++++++++++++++++++++++++++++++++++++++--------
 compat/terminal.h | 10 ++++++++++
 2 files changed, 54 insertions(+), 8 deletions(-)
diff --git a/compat/terminal.c b/compat/terminal.c
index 6d16c8f..c85d5c7 100644
--- a/compat/terminal.c
+++ b/compat/terminal.c
@@ -24,15 +24,21 @@ static void restore_term_on_signal(int sig)
 	raise(sig);
 }
 
-char *git_terminal_prompt(const char *prompt, int echo)
+term_t terminal_open(void)
+{
+	return fopen("/dev/tty", "w+");
+}
+
+int terminal_close(term_t term)
+{
+	return fclose(term);
+}
+
+char *terminal_prompt(term_t term, const char *prompt, int echo)
 {
 	static struct strbuf buf = STRBUF_INIT;
 	int r;
-	FILE *fh;
-
-	fh = fopen("/dev/tty", "w+");
-	if (!fh)
-		return NULL;
+	FILE *fh = term;
 
 	if (!echo) {
 		struct termios t;
@@ -64,18 +70,48 @@ char *git_terminal_prompt(const char *prompt, int echo)
 	}
 
 	restore_term();
-	fclose(fh);
 
 	if (r == EOF)
 		return NULL;
 	return buf.buf;
 }
 
+char *git_terminal_prompt(const char *prompt, int echo)
+{
+	char *ret;
+	term_t term;
+
+	term = terminal_open();
+	if (!term)
+		return NULL;
+
+	ret = terminal_prompt(term, prompt, echo);
+
+	terminal_close(term);
+
+	return ret;
+}
+
 #else
 
-char *git_terminal_prompt(const char *prompt, int echo)
+term_t terminal_open()
+{
+	return isatty(0) && isatty(2);
+}
+
+int terminal_close(term_t term)
+{
+	return 0;
+}
+
+char *terminal_prompt(term_t term, const char *prompt, int echo)
 {
 	return getpass(prompt);
 }
 
+char *git_terminal_prompt(const char *prompt, int echo)
+{
+	return terminal_prompt(prompt, echo);
+}
+
 #endif
diff --git a/compat/terminal.h b/compat/terminal.h
index 97db7cd..cf2aa10 100644
--- a/compat/terminal.h
+++ b/compat/terminal.h
@@ -1,6 +1,16 @@
 #ifndef COMPAT_TERMINAL_H
 #define COMPAT_TERMINAL_H
 
+#ifdef HAVE_DEV_TTY
+typedef FILE *term_t;
+#else
+typedef int term_t;
+#endif
+
+term_t terminal_open();
+int terminal_close(term_t term);
+char *terminal_prompt(term_t term, const char *prompt, int echo);
+
 char *git_terminal_prompt(const char *prompt, int echo);
 
 #endif /* COMPAT_TERMINAL_H */
-- 
1.7.12.rc1.187.g6dd9156
Previous: Tay Ray ChuanNext: Tay Ray Chuan
Message 30 of 37 in “allow recovery from command name typos”
  1. 0/4 allow recovery from command name typosTay Ray Chuan, May 6, 2012
  2. 1/4 help.c::uniq: plug a leakTay Ray Chuan, May 6, 2012
  3. 2/4 help.c::exclude_cmds: plug a leakTay Ray Chuan, May 6, 2012
  4. 3/4 help.c: plug a leak when help.autocorrect is setTay Ray Chuan, May 6, 2012
  5. 4/4 allow recovery from command name typosTay Ray Chuan, May 6, 2012
  6. Jeff KingMay 6, 2012
  7. Tay Ray ChuanMay 6, 2012
  8. Thomas RastMay 7, 2012
  9. Tay Ray ChuanMay 7, 2012
  10. Junio C HamanoMay 7, 2012
  11. Tay Ray ChuanMay 9, 2012
  12. Junio C HamanoMay 9, 2012
  13. Jeff KingMay 6, 2012
  14. Tay Ray ChuanMay 6, 2012
  15. Jeff KingMay 7, 2012
  16. 0/4 allow recovery from command name typosTay Ray Chuan, Jul 25, 2012
  17. 1/4 help.c::uniq: plug a leakTay Ray Chuan, Jul 25, 2012
  18. 2/4 help.c::exclude_cmds: realloc() before copy, plug a leakTay Ray Chuan, Jul 25, 2012
  19. 3/4 help.c: plug leaks with(out) help.autocorrectTay Ray Chuan, Jul 25, 2012
  20. 4/4 allow recovery from command name typosTay Ray Chuan, Jul 25, 2012
  21. Junio C HamanoJul 25, 2012
  22. Tay Ray ChuanJul 26, 2012
  23. Jeff KingJul 26, 2012
  24. Junio C HamanoJul 26, 2012
  25. Jeff KingJul 26, 2012
  26. Junio C HamanoJul 26, 2012
  27. Junio C HamanoJul 25, 2012
  28. Junio C HamanoJul 25, 2012
  29. 0/2 allow recovery from command name typosTay Ray Chuan, Aug 5, 2012
  30. 1/2 add interface for /dev/tty interactionTay Ray Chuan, Aug 5, 2012
  31. 2/2 allow recovery from command name typosTay Ray Chuan, Aug 5, 2012
  32. Junio C HamanoAug 6, 2012
  33. Junio C HamanoAug 5, 2012
  34. Jeff KingAug 6, 2012
  35. Jeff KingAug 6, 2012
  36. Junio C HamanoAug 6, 2012
  37. Tay Ray ChuanMay 6, 2012

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.