diff options
Diffstat (limited to 'src/openvpn/console_builtin.c')
-rw-r--r-- | src/openvpn/console_builtin.c | 107 |
1 files changed, 53 insertions, 54 deletions
diff --git a/src/openvpn/console_builtin.c b/src/openvpn/console_builtin.c index 445928b..3a977ee 100644 --- a/src/openvpn/console_builtin.c +++ b/src/openvpn/console_builtin.c @@ -5,9 +5,9 @@ * packet encryption, packet authentication, and * packet compression. * - * Copyright (C) 2002-2018 OpenVPN Inc <sales@openvpn.net> + * Copyright (C) 2002-2021 OpenVPN Inc <sales@openvpn.net> * Copyright (C) 2014-2015 David Sommerseth <davids@redhat.com> - * Copyright (C) 2016-2018 David Sommerseth <davids@openvpn.net> + * Copyright (C) 2016-2021 David Sommerseth <davids@openvpn.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 @@ -58,78 +58,77 @@ static bool get_console_input_win32(const char *prompt, const bool echo, char *input, const int capacity) { - HANDLE in = INVALID_HANDLE_VALUE; - HANDLE err = INVALID_HANDLE_VALUE; - DWORD len = 0; - ASSERT(prompt); ASSERT(input); ASSERT(capacity > 0); input[0] = '\0'; - in = GetStdHandle(STD_INPUT_HANDLE); - err = get_orig_stderr(); - - if (in != INVALID_HANDLE_VALUE - && err != INVALID_HANDLE_VALUE - && !win32_service_interrupt(&win32_signal) - && WriteFile(err, prompt, strlen(prompt), &len, NULL)) + HANDLE in = GetStdHandle(STD_INPUT_HANDLE); + int orig_stderr = get_orig_stderr(); // guaranteed to be always valid + if ((in == INVALID_HANDLE_VALUE) + || win32_service_interrupt(&win32_signal) + || (_write(orig_stderr, prompt, strlen(prompt)) == -1)) { - bool is_console = (GetFileType(in) == FILE_TYPE_CHAR); - DWORD flags_save = 0; - int status = 0; - WCHAR *winput; + msg(M_WARN|M_ERRNO, "get_console_input_win32(): unexpected error"); + return false; + } - if (is_console) - { - if (GetConsoleMode(in, &flags_save)) - { - DWORD flags = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; - if (echo) - { - flags |= ENABLE_ECHO_INPUT; - } - SetConsoleMode(in, flags); - } - else - { - is_console = 0; - } - } + bool is_console = (GetFileType(in) == FILE_TYPE_CHAR); + DWORD flags_save = 0; + int status = 0; + WCHAR *winput; - if (is_console) + if (is_console) + { + if (GetConsoleMode(in, &flags_save)) { - winput = malloc(capacity * sizeof(WCHAR)); - if (winput == NULL) + DWORD flags = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; + if (echo) { - return false; + flags |= ENABLE_ECHO_INPUT; } - - status = ReadConsoleW(in, winput, capacity, &len, NULL); - WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL); - free(winput); + SetConsoleMode(in, flags); } else { - status = ReadFile(in, input, capacity, &len, NULL); + is_console = 0; } + } - string_null_terminate(input, (int)len, capacity); - chomp(input); + DWORD len = 0; - if (!echo) - { - WriteFile(err, "\r\n", 2, &len, NULL); - } - if (is_console) - { - SetConsoleMode(in, flags_save); - } - if (status && !win32_service_interrupt(&win32_signal)) + if (is_console) + { + winput = malloc(capacity * sizeof(WCHAR)); + if (winput == NULL) { - return true; + return false; } + + status = ReadConsoleW(in, winput, capacity, &len, NULL); + WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL); + free(winput); + } + else + { + status = ReadFile(in, input, capacity, &len, NULL); + } + + string_null_terminate(input, (int)len, capacity); + chomp(input); + + if (!echo) + { + _write(orig_stderr, "\r\n", 2); + } + if (is_console) + { + SetConsoleMode(in, flags_save); + } + if (status && !win32_service_interrupt(&win32_signal)) + { + return true; } return false; |