1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2020 Dmitry Kozlyuk
7 #include "cmdline_private.h"
9 /* Missing from some MinGW-w64 distributions. */
10 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
11 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
14 #ifndef ENABLE_VIRTUAL_TERMINAL_INPUT
15 #define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
19 terminal_adjust(struct cmdline *cl)
24 ZeroMemory(&cl->oldterm, sizeof(cl->oldterm));
26 /* Detect console input, set it up and make it emulate VT100. */
27 handle = GetStdHandle(STD_INPUT_HANDLE);
28 if (GetConsoleMode(handle, &mode)) {
29 cl->oldterm.is_console_input = 1;
30 cl->oldterm.input_mode = mode;
33 ENABLE_LINE_INPUT | /* no line buffering */
34 ENABLE_ECHO_INPUT | /* no echo */
35 ENABLE_PROCESSED_INPUT | /* pass Ctrl+C to program */
36 ENABLE_MOUSE_INPUT | /* no mouse events */
37 ENABLE_WINDOW_INPUT); /* no window resize events */
38 mode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
39 SetConsoleMode(handle, mode);
42 /* Detect console output and make it emulate VT100. */
43 handle = GetStdHandle(STD_OUTPUT_HANDLE);
44 if (GetConsoleMode(handle, &mode)) {
45 cl->oldterm.is_console_output = 1;
46 cl->oldterm.output_mode = mode;
48 mode &= ~ENABLE_WRAP_AT_EOL_OUTPUT;
49 mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
50 SetConsoleMode(handle, mode);
55 terminal_restore(const struct cmdline *cl)
57 if (cl->oldterm.is_console_input) {
58 HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
59 SetConsoleMode(handle, cl->oldterm.input_mode);
62 if (cl->oldterm.is_console_output) {
63 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
64 SetConsoleMode(handle, cl->oldterm.output_mode);
69 cmdline_is_key_down(const INPUT_RECORD *record)
71 return (record->EventType == KEY_EVENT) &&
72 record->Event.KeyEvent.bKeyDown;
76 cmdline_poll_char_console(HANDLE handle)
81 if (!PeekConsoleInput(handle, &record, 1, &events)) {
82 /* Simulate poll(3) behavior on EOF. */
83 return (GetLastError() == ERROR_HANDLE_EOF) ? 1 : -1;
86 if ((events == 0) || !cmdline_is_key_down(&record))
93 cmdline_poll_char_file(struct cmdline *cl, HANDLE handle)
95 DWORD type = GetFileType(handle);
97 /* Since console is handled by cmdline_poll_char_console(),
98 * this is either a serial port or input handle had been replaced.
100 if (type == FILE_TYPE_CHAR)
101 return cmdline_poll_char_console(handle);
103 /* PeekNamedPipe() can handle all pipes and also sockets. */
104 if (type == FILE_TYPE_PIPE) {
106 if (!PeekNamedPipe(handle, NULL, 0, NULL, &bytes_avail, NULL))
107 return (GetLastError() == ERROR_BROKEN_PIPE) ? 1 : -1;
108 return bytes_avail ? 1 : 0;
111 /* There is no straightforward way to peek a file in Windows
112 * I/O model. Read the byte, if it is not the end of file,
113 * buffer it for subsequent read. This will not work with
114 * a file being appended and probably some other edge cases.
116 if (type == FILE_TYPE_DISK) {
120 ret = _read(cl->s_in, &c, sizeof(c));
122 cl->repeat_count = 1;
123 cl->repeated_char = c;
128 /* GetFileType() failed or file of unknown type,
129 * which we do not know how to peek anyway.
135 cmdline_poll_char(struct cmdline *cl)
137 HANDLE handle = (HANDLE)_get_osfhandle(cl->s_in);
138 return cl->oldterm.is_console_input ?
139 cmdline_poll_char_console(handle) :
140 cmdline_poll_char_file(cl, handle);
144 cmdline_read_char(struct cmdline *cl, char *c)
148 KEY_EVENT_RECORD *key;
151 if (!cl->oldterm.is_console_input)
152 return _read(cl->s_in, c, 1);
154 /* Return repeated strokes from previous event. */
155 if (cl->repeat_count > 0) {
156 *c = cl->repeated_char;
161 handle = (HANDLE)_get_osfhandle(cl->s_in);
162 key = &record.Event.KeyEvent;
164 if (!ReadConsoleInput(handle, &record, 1, &events)) {
165 if (GetLastError() == ERROR_HANDLE_EOF) {
171 } while (!cmdline_is_key_down(&record));
173 *c = key->uChar.AsciiChar;
175 /* Save repeated strokes from a single event. */
176 if (key->wRepeatCount > 1) {
177 cl->repeated_char = *c;
178 cl->repeat_count = key->wRepeatCount - 1;
185 cmdline_vdprintf(int fd, const char *format, va_list op)
194 file = _fdopen(copy, "a");
200 ret = vfprintf(file, format, op);
202 fclose(file); /* also closes copy */