root/src/libsir/src/sirconsole.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _sir_write_stdio
  2. _sir_config_console
  3. __sir_config_consoles_once
  4. _sir_initialize_stdio
  5. _sir_write_stdio

   1 /*
   2  * sirconsole.c
   3  *
   4  * Version: 2.2.5
   5  *
   6  * -----------------------------------------------------------------------------
   7  *
   8  * SPDX-License-Identifier: MIT
   9  *
  10  * Copyright (c) 2018-2024 Ryan M. Lederman <lederman@gmail.com>
  11  *
  12  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  13  * this software and associated documentation files (the "Software"), to deal in
  14  * the Software without restriction, including without limitation the rights to
  15  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  16  * the Software, and to permit persons to whom the Software is furnished to do so,
  17  * subject to the following conditions:
  18  *
  19  * The above copyright notice and this permission notice shall be included in all
  20  * copies or substantial portions of the Software.
  21  *
  22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  24  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  25  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  26  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28  *
  29  * -----------------------------------------------------------------------------
  30  */
  31 
  32 #include "sir/console.h"
  33 #include "sir/internal.h"
  34 
  35 #if !defined(__WIN__)
  36 bool _sir_write_stdio(FILE* stream, const char* message) {
     /* [previous][next][first][last][top][bottom][index][help] */
  37     return (EOF != fputs(message, stream)) ? true : _sir_handleerr(errno);
  38 }
  39 #else /* __WIN__ */
  40 HANDLE __sir_stdout  = INVALID_HANDLE_VALUE;
  41 HANDLE __sir_stderr  = INVALID_HANDLE_VALUE;
  42 static sir_once config_once = SIR_ONCE_INIT;
  43 
  44 static
  45 bool _sir_config_console(HANDLE console) {
     /* [previous][next][first][last][top][bottom][index][help] */
  46     if (INVALID_HANDLE_VALUE == console || NULL == console)
  47         return _sir_handlewin32err(GetLastError());
  48 
  49     DWORD mode = 0UL;
  50     if (!GetConsoleMode(console, &mode))
  51         return _sir_handlewin32err(GetLastError());
  52 
  53     mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT;
  54     if (!SetConsoleMode(console, mode))
  55         return _sir_handlewin32err(GetLastError());
  56 
  57     return true;
  58 }
  59 
  60 static
  61 BOOL CALLBACK __sir_config_consoles_once(PINIT_ONCE ponce, PVOID param, PVOID* ctx) {
     /* [previous][next][first][last][top][bottom][index][help] */
  62     SIR_UNUSED(ponce);
  63     SIR_UNUSED(param);
  64     SIR_UNUSED(ctx);
  65 
  66     __sir_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
  67     __sir_stderr = GetStdHandle(STD_ERROR_HANDLE);
  68 
  69     return (_sir_config_console(__sir_stdout) && _sir_config_console(__sir_stderr))
  70         ? TRUE : FALSE;
  71 }
  72 
  73 void _sir_initialize_stdio(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
  74     if (!_sir_once(&config_once, __sir_config_consoles_once))
  75         _sir_selflog("warning: unable to configure stdio consoles!");
  76 }
  77 
  78 bool _sir_write_stdio(HANDLE console, const char* message, size_t len) {
     /* [previous][next][first][last][top][bottom][index][help] */
  79     DWORD chars   = (DWORD)len;
  80     DWORD written = 0UL;
  81 
  82     do {
  83         DWORD pass = 0UL;
  84 
  85         if (!WriteConsole(console, message + written, chars - written, &pass, NULL))
  86             return _sir_handlewin32err(GetLastError());
  87 
  88         written += pass;
  89     } while (written < chars);
  90 
  91     return written == chars;
  92 }
  93 #endif /* !__WIN__ */

/* [previous][next][first][last][top][bottom][index][help] */