| 1 | /* $Id: maybe_con_write.c 3188 2018-03-24 15:32:26Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * maybe_con_write - Optimized console output on windows.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2016-2018 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a
|
|---|
| 10 | * copy of this software and associated documentation files (the "Software"),
|
|---|
| 11 | * to deal in the Software without restriction, including without limitation
|
|---|
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|---|
| 13 | * and/or sell copies of the Software, and to permit persons to whom the
|
|---|
| 14 | * Software is furnished to do so, subject to the following conditions:
|
|---|
| 15 | *
|
|---|
| 16 | * The above copyright notice and this permission notice shall be included
|
|---|
| 17 | * in all copies or substantial portions of the Software.
|
|---|
| 18 | *
|
|---|
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|---|
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|---|
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|---|
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|---|
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|---|
| 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|---|
| 25 | * IN THE SOFTWARE.
|
|---|
| 26 | *
|
|---|
| 27 | * Alternatively, the content of this file may be used under the terms of the
|
|---|
| 28 | * GPL version 2 or later, or LGPL version 2.1 or later.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | /*********************************************************************************************************************************
|
|---|
| 33 | * Header Files *
|
|---|
| 34 | *********************************************************************************************************************************/
|
|---|
| 35 | #include "console.h"
|
|---|
| 36 | #ifdef KBUILD_OS_WINDOWS
|
|---|
| 37 | # include <windows.h>
|
|---|
| 38 | #endif
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #ifdef _MSC_VER
|
|---|
| 41 | # include <conio.h>
|
|---|
| 42 | typedef unsigned int to_write_t;
|
|---|
| 43 | #else
|
|---|
| 44 | typedef size_t to_write_t;
|
|---|
| 45 | #endif
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Drop-in write replacement for optimizing console output on windows.
|
|---|
| 50 | *
|
|---|
| 51 | * @returns Number of bytes written, -1 + errno on failure.
|
|---|
| 52 | * @param fd The file descript to write to.
|
|---|
| 53 | * @param pvBuf What to write.
|
|---|
| 54 | * @param cbToWrite How much to write.
|
|---|
| 55 | */
|
|---|
| 56 | ssize_t maybe_con_write(int fd, void const *pvBuf, size_t cbToWrite)
|
|---|
| 57 | {
|
|---|
| 58 | ssize_t cbWritten;
|
|---|
| 59 |
|
|---|
| 60 | #ifdef KBUILD_OS_WINDOWS
|
|---|
| 61 | /*
|
|---|
| 62 | * If it's a TTY, do our own conversion to wide char and
|
|---|
| 63 | * call WriteConsoleW directly.
|
|---|
| 64 | */
|
|---|
| 65 | if (cbToWrite > 0)
|
|---|
| 66 | {
|
|---|
| 67 | HANDLE hCon = (HANDLE)_get_osfhandle(fd);
|
|---|
| 68 | if ( hCon != INVALID_HANDLE_VALUE
|
|---|
| 69 | && hCon != NULL)
|
|---|
| 70 | {
|
|---|
| 71 | if (is_console_handle((intptr_t)hCon))
|
|---|
| 72 | {
|
|---|
| 73 | size_t cwcTmp = cbToWrite * 2 + 16;
|
|---|
| 74 | wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t));
|
|---|
| 75 | if (pawcTmp)
|
|---|
| 76 | {
|
|---|
| 77 | int cwcToWrite;
|
|---|
| 78 | static UINT s_uConsoleCp = 0;
|
|---|
| 79 | if (s_uConsoleCp == 0)
|
|---|
| 80 | s_uConsoleCp = GetConsoleCP();
|
|---|
| 81 |
|
|---|
| 82 | cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite,
|
|---|
| 83 | pawcTmp, (int)(cwcTmp - 1));
|
|---|
| 84 | if (cwcToWrite > 0)
|
|---|
| 85 | {
|
|---|
| 86 | /* Let the CRT do the rest. At least the Visual C++ 2010 CRT
|
|---|
| 87 | sources indicates _cputws will do the right thing. */
|
|---|
| 88 | pawcTmp[cwcToWrite] = '\0';
|
|---|
| 89 | if (_cputws(pawcTmp) >= 0)
|
|---|
| 90 | return cbToWrite;
|
|---|
| 91 | return -1;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | #endif
|
|---|
| 98 |
|
|---|
| 99 | /*
|
|---|
| 100 | * Semi regular write handling.
|
|---|
| 101 | */
|
|---|
| 102 | cbWritten = write(fd, pvBuf, (to_write_t)cbToWrite);
|
|---|
| 103 | if (cbWritten == (ssize_t)cbToWrite)
|
|---|
| 104 | { /* likely */ }
|
|---|
| 105 | else if (cbWritten >= 0 || errno == EINTR)
|
|---|
| 106 | {
|
|---|
| 107 | if (cbWritten < 0)
|
|---|
| 108 | cbWritten = 0;
|
|---|
| 109 | while (cbWritten < (ssize_t)cbToWrite)
|
|---|
| 110 | {
|
|---|
| 111 | ssize_t cbThis = write(fd, (char *)pvBuf + cbWritten, (to_write_t)(cbToWrite - cbWritten));
|
|---|
| 112 | if (cbThis >= 0)
|
|---|
| 113 | cbWritten += cbThis;
|
|---|
| 114 | else if (errno != EINTR)
|
|---|
| 115 | return -1;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | return cbWritten;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|