From fd89a7598820f5b5a7a4b7029e31dd100cefbb96 Mon Sep 17 00:00:00 2001 From: Martin Herkt Date: Sat, 28 Dec 2013 04:44:12 +0100 Subject: osdep/io, mp_vfprintf: split out console detection --- osdep/io.c | 85 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/osdep/io.c b/osdep/io.c index 19f0be1108..2e2603eb7f 100644 --- a/osdep/io.c +++ b/osdep/io.c @@ -113,56 +113,57 @@ int mp_stat(const char *path, struct stat *buf) return res; } -static int mp_vfprintf(FILE *stream, const char *format, va_list args) +static int mp_check_console(HANDLE *wstream) { - int done = 0; + if (wstream != INVALID_HANDLE_VALUE) { + unsigned int filetype = GetFileType(wstream); - if (stream == stdout || stream == stderr) - { - HANDLE *wstream = GetStdHandle(stream == stdout ? - STD_OUTPUT_HANDLE : STD_ERROR_HANDLE); - if (wstream != INVALID_HANDLE_VALUE) + if (!((filetype == FILE_TYPE_UNKNOWN) && + (GetLastError() != ERROR_SUCCESS))) { - // figure out whether we're writing to a console - unsigned int filetype = GetFileType(wstream); - if (!((filetype == FILE_TYPE_UNKNOWN) && - (GetLastError() != ERROR_SUCCESS))) - { - int isConsole; - filetype &= ~(FILE_TYPE_REMOTE); - if (filetype == FILE_TYPE_CHAR) - { - DWORD ConsoleMode; - int ret = GetConsoleMode(wstream, &ConsoleMode); - if (!ret && (GetLastError() == ERROR_INVALID_HANDLE)) - isConsole = 0; - else - isConsole = 1; - } - else - isConsole = 0; - - if (isConsole) - { - int nchars = vsnprintf(NULL, 0, format, args) + 1; - char *buf = talloc_array(NULL, char, nchars); - if (buf) - { - vsnprintf(buf, nchars, format, args); - wchar_t *out = mp_from_utf8(NULL, buf); - size_t nchars = wcslen(out); - talloc_free(buf); - done = WriteConsoleW(wstream, out, nchars, NULL, NULL); - talloc_free(out); - } + filetype &= ~(FILE_TYPE_REMOTE); + + if (filetype == FILE_TYPE_CHAR) { + DWORD ConsoleMode; + int ret = GetConsoleMode(wstream, &ConsoleMode); + + if (!(!ret && (GetLastError() == ERROR_INVALID_HANDLE))) { + // This seems to be a console + return 1; } - else - done = vfprintf(stream, format, args); } } } - else + + return 0; +} + +static int mp_vfprintf(FILE *stream, const char *format, va_list args) +{ + int done = 0; + + HANDLE *wstream = INVALID_HANDLE_VALUE; + + if (stream == stdout || stream == stderr) { + wstream = GetStdHandle(stream == stdout ? + STD_OUTPUT_HANDLE : STD_ERROR_HANDLE); + } + + if (mp_check_console(wstream)) { + size_t len = vsnprintf(NULL, 0, format, args) + 1; + char *buf = talloc_array(NULL, char, len); + + if (buf) { + vsnprintf(buf, len, format, args); + wchar_t *out = mp_from_utf8(NULL, buf); + size_t len = wcslen(out); + talloc_free(buf); + done = WriteConsoleW(wstream, out, len, NULL, NULL); + talloc_free(out); + } + } else { done = vfprintf(stream, format, args); + } return done; } -- cgit v1.2.3