From: Stepan Kasal <kasal@ucw.cz>
To: GIT Mailing-list <git@vger.kernel.org>
Cc: msysGit <msysgit@googlegroups.com>, Karsten Blees <blees@dcon.de>,
Johannes Schindelin <johannes.schindelin@gmx.de>,
Stepan Kasal <kasal@ucw.cz>
Subject: [PATCH 2/5] Detect console streams more reliably on Windows
Date: Fri, 6 Jun 2014 15:42:50 +0200 [thread overview]
Message-ID: <1402062173-9602-3-git-send-email-kasal@ucw.cz> (raw)
In-Reply-To: <1402062173-9602-1-git-send-email-kasal@ucw.cz>
From: Karsten Blees <blees@dcon.de>
GetStdHandle(STD_OUTPUT_HANDLE) doesn't work for stderr if stdout is
redirected. Use _get_osfhandle of the FILE* instead.
_isatty() is true for all character devices (including parallel and serial
ports). Check return value of GetConsoleScreenBufferInfo instead to
reliably detect console handles (also don't initialize internal state from
an uninitialized CONSOLE_SCREEN_BUFFER_INFO structure if the function
fails).
Signed-off-by: Karsten Blees <blees@dcon.de>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Stepan Kasal <kasal@ucw.cz>
---
compat/winansi.c | 50 ++++++++++++++++++++++++++------------------------
1 file changed, 26 insertions(+), 24 deletions(-)
diff --git a/compat/winansi.c b/compat/winansi.c
index abe0fea..c4be401 100644
--- a/compat/winansi.c
+++ b/compat/winansi.c
@@ -25,27 +25,39 @@ static HANDLE console;
static WORD plain_attr;
static WORD attr;
static int negative;
+static FILE *last_stream = NULL;
-static void init(void)
+static int is_console(FILE *stream)
{
CONSOLE_SCREEN_BUFFER_INFO sbi;
+ HANDLE hcon;
static int initialized = 0;
- if (initialized)
- return;
- console = GetStdHandle(STD_OUTPUT_HANDLE);
- if (console == INVALID_HANDLE_VALUE)
- console = NULL;
+ /* use cached value if stream hasn't changed */
+ if (stream == last_stream)
+ return console != NULL;
- if (!console)
- return;
+ last_stream = stream;
+ console = NULL;
- GetConsoleScreenBufferInfo(console, &sbi);
- attr = plain_attr = sbi.wAttributes;
- negative = 0;
+ /* get OS handle of the stream */
+ hcon = (HANDLE) _get_osfhandle(_fileno(stream));
+ if (hcon == INVALID_HANDLE_VALUE)
+ return 0;
+
+ /* check if its a handle to a console output screen buffer */
+ if (!GetConsoleScreenBufferInfo(hcon, &sbi))
+ return 0;
+
+ if (!initialized) {
+ attr = plain_attr = sbi.wAttributes;
+ negative = 0;
+ initialized = 1;
+ }
- initialized = 1;
+ console = hcon;
+ return 1;
}
static int write_console(const char *str, size_t len)
@@ -292,12 +304,7 @@ int winansi_fputs(const char *str, FILE *stream)
{
int rv;
- if (!isatty(fileno(stream)))
- return fputs(str, stream);
-
- init();
-
- if (!console)
+ if (!is_console(stream))
return fputs(str, stream);
rv = ansi_emulate(str, stream);
@@ -315,12 +322,7 @@ int winansi_vfprintf(FILE *stream, const char *format, va_list list)
char *buf = small_buf;
va_list cp;
- if (!isatty(fileno(stream)))
- goto abort;
-
- init();
-
- if (!console)
+ if (!is_console(stream))
goto abort;
va_copy(cp, list);
--
2.0.0.9635.g0be03cb
--
--
*** Please reply-to-all at all times ***
*** (do not pretend to know who is subscribed and who is not) ***
*** Please avoid top-posting. ***
The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free.
You received this message because you are subscribed to the Google
Groups "msysGit" group.
To post to this group, send email to msysgit@googlegroups.com
To unsubscribe from this group, send email to
msysgit+unsubscribe@googlegroups.com
For more options, and view previous threads, visit this group at
http://groups.google.com/group/msysgit?hl=en_US?hl=en
---
You received this message because you are subscribed to the Google Groups "msysGit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to msysgit+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
next prev parent reply other threads:[~2014-06-06 13:43 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-06 13:42 [PATCH 0/5] First part of Unicode console support for msysgit Stepan Kasal
2014-06-06 13:42 ` [PATCH 1/5] Support Unicode console output on Windows Stepan Kasal
2014-06-06 13:42 ` Stepan Kasal [this message]
2014-06-06 13:42 ` [PATCH 3/5] Warn if the Windows console font doesn't support Unicode Stepan Kasal
2014-06-06 21:18 ` Peter Krefting
2014-06-07 7:02 ` Stepan Kasal
2014-06-06 13:42 ` [PATCH 4/5] Win32: move main macro to a function Stepan Kasal
2014-06-06 13:42 ` [PATCH 5/5] Win32: Thread-safe windows console output Stepan Kasal
2014-06-06 21:29 ` Peter Krefting
2014-06-06 22:03 ` Karsten Blees
2014-06-06 17:44 ` [PATCH 0/5] First part of Unicode console support for msysgit Karsten Blees
2014-06-06 18:39 ` Stepan Kasal
2014-06-07 7:57 ` [PATCH v2 0/6] " Stepan Kasal
2014-06-07 7:57 ` [PATCH v2 1/6] Support Unicode console output on Windows Stepan Kasal
2014-06-07 7:57 ` [PATCH v2 2/6] Detect console streams more reliably " Stepan Kasal
2014-06-07 7:57 ` [PATCH v2 3/6] Warn if the Windows console font doesn't support Unicode Stepan Kasal
2014-06-07 7:57 ` [PATCH v2 4/6] Win32: add Unicode conversion functions Stepan Kasal
2014-06-07 7:57 ` [PATCH v2 5/6] Win32: Thread-safe windows console output Stepan Kasal
2014-06-13 6:10 ` Johannes Sixt
2014-06-13 22:09 ` [PATCH 7/6] Win32: reliably detect console pipe handles Karsten Blees
2014-06-07 7:57 ` [PATCH v2 6/6] Win32: fix broken pipe detection Stepan Kasal
2014-06-06 20:48 ` Re: [PATCH 0/5] First part of Unicode console support for msysgit Stepan Kasal
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=1402062173-9602-3-git-send-email-kasal@ucw.cz \
--to=kasal@ucw.cz \
--cc=blees@dcon.de \
--cc=git@vger.kernel.org \
--cc=johannes.schindelin@gmx.de \
--cc=msysgit@googlegroups.com \
/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).