This source file includes following definitions.
- _sir_write_stdio
- _sir_config_console
- __sir_config_consoles_once
- _sir_initialize_stdio
- _sir_write_stdio
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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) {
37 return (EOF != fputs(message, stream)) ? true : _sir_handleerr(errno);
38 }
39 #else
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) {
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) {
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) {
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) {
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