git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tay Ray Chuan <rctay89@gmail.com>
To: "Git Mailing List" <git@vger.kernel.org>
Cc: "Junio C Hamano" <gitster@pobox.com>, "Jeff King" <peff@peff.net>
Subject: [PATCH v3 1/2] add interface for /dev/tty interaction
Date: Mon,  6 Aug 2012 02:45:39 +0800	[thread overview]
Message-ID: <1344192340-19415-2-git-send-email-rctay89@gmail.com> (raw)
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

  reply	other threads:[~2012-08-05 18:46 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-06  6:55 [PATCH 0/4] allow recovery from command name typos Tay Ray Chuan
2012-05-06  6:55 ` [PATCH 1/4] help.c::uniq: plug a leak Tay Ray Chuan
2012-05-06  6:55   ` [PATCH 2/4] help.c::exclude_cmds: " Tay Ray Chuan
2012-05-06  6:55     ` [PATCH 3/4] help.c: plug a leak when help.autocorrect is set Tay Ray Chuan
2012-05-06  6:55       ` [PATCH 4/4] allow recovery from command name typos Tay Ray Chuan
2012-05-06  8:21         ` Jeff King
2012-05-06 16:07           ` Tay Ray Chuan
2012-05-07  9:43             ` Thomas Rast
2012-05-07 15:49               ` Tay Ray Chuan
2012-05-07 17:41               ` Junio C Hamano
2012-05-09 15:06                 ` Tay Ray Chuan
2012-05-09 17:03                   ` Junio C Hamano
     [not found]         ` <CAOBOgRaDEgAqXWmdC6hrudkL5OwzeMffbj2RtKMxf2TsYWzotA@mail.gmail.com>
2012-05-06 16:04           ` Tay Ray Chuan
2012-05-06  8:12   ` [PATCH 1/4] help.c::uniq: plug a leak Jeff King
2012-05-06 15:54     ` Tay Ray Chuan
2012-05-07  7:30       ` Jeff King
2012-07-25 16:16 ` [PATCH v2 0/4] allow recovery from command name typos Tay Ray Chuan
2012-07-25 16:16   ` [PATCH v2 1/4] help.c::uniq: plug a leak Tay Ray Chuan
2012-07-25 16:16     ` [PATCH v2 2/4] help.c::exclude_cmds: realloc() before copy, " Tay Ray Chuan
2012-07-25 16:16       ` [PATCH v2 3/4] help.c: plug leaks with(out) help.autocorrect Tay Ray Chuan
2012-07-25 16:16         ` [PATCH v2 4/4] allow recovery from command name typos Tay Ray Chuan
2012-07-25 17:57           ` Junio C Hamano
2012-07-26 17:08             ` Tay Ray Chuan
2012-07-26 17:26               ` Jeff King
2012-07-26 17:59                 ` Junio C Hamano
2012-07-26 18:37                   ` Jeff King
2012-07-26 17:53               ` Junio C Hamano
2012-07-25 17:47         ` [PATCH v2 3/4] help.c: plug leaks with(out) help.autocorrect Junio C Hamano
2012-07-25 17:39       ` [PATCH v2 2/4] help.c::exclude_cmds: realloc() before copy, plug a leak Junio C Hamano
2012-08-05 18:45 ` [PATCH v3 0/2] allow recovery from command name typos Tay Ray Chuan
2012-08-05 18:45   ` Tay Ray Chuan [this message]
2012-08-05 18:45     ` [PATCH v3 2/2] " Tay Ray Chuan
2012-08-06  0:50       ` Junio C Hamano
2012-08-05 20:11     ` [PATCH v3 1/2] add interface for /dev/tty interaction Junio C Hamano
2012-08-06 19:45       ` Jeff King
2012-08-06 19:56         ` Jeff King
2012-08-06 20:01           ` Junio C Hamano

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=1344192340-19415-2-git-send-email-rctay89@gmail.com \
    --to=rctay89@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /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 NNTP newsgroup(s).