summaryrefslogtreecommitdiff
path: root/src/tool_main.c
diff options
context:
space:
mode:
authorRich Turner <richturn@microsoft.com>2018-06-28 15:25:00 -0700
committerDaniel Stenberg <daniel@haxx.se>2018-09-29 12:39:41 +0200
commitbecfe1233ff2b6b0c3e1b6a10048b55b68c2539f (patch)
tree57bd7b06e8af6e730570e9bb26d15f0682309416 /src/tool_main.c
parent454fa3fd7be9b4a8d51d19d3515a3a935c3bf400 (diff)
downloadcurl-becfe1233ff2b6b0c3e1b6a10048b55b68c2539f.tar.gz
curl: enabled Windows VT Support and UTF-8 output
Enabled Console VT support (if running OS supports VT) in tool_main.c. Fixes #3008 Closes #3011
Diffstat (limited to 'src/tool_main.c')
-rw-r--r--src/tool_main.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/tool_main.c b/src/tool_main.c
index b9f7eae2c..3a9f0cdff 100644
--- a/src/tool_main.c
+++ b/src/tool_main.c
@@ -236,6 +236,56 @@ static void main_free(struct GlobalConfig *config)
config->last = NULL;
}
+#ifdef _WIN32
+/* TerminalSettings for Windows */
+struct TerminalSettings {
+ HANDLE hStdOut;
+ DWORD dwOutputMode;
+ UINT nCodepage;
+}TerminalSettings;
+#endif
+
+static void configure_terminal(void)
+{
+#ifdef _WIN32
+ /*
+ * If we're running Windows, enable VT output & set codepage to UTF-8.
+ * Note: VT mode flag can be set on any version of Windows, but VT
+ * processing only performed on Win10 >= Creators Update)
+ */
+
+ /* Define the VT flags in case we're building with an older SDK */
+#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
+ #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
+#endif
+
+ /* Cache current codepage (will restore on exit) & set codepage to UTF-8 */
+ memset(&TerminalSettings, 0, sizeof(TerminalSettings));
+ TerminalSettings.nCodepage = GetConsoleOutputCP();
+ SetConsoleOutputCP(65001);
+
+ /* Enable VT output */
+ TerminalSettings.hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
+ if((TerminalSettings.hStdOut != INVALID_HANDLE_VALUE)
+ && (GetConsoleMode(TerminalSettings.hStdOut,
+ &TerminalSettings.dwOutputMode))) {
+ SetConsoleMode(TerminalSettings.hStdOut,
+ TerminalSettings.dwOutputMode
+ | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
+ }
+#endif
+}
+
+static void restore_terminal(void)
+{
+#ifdef _WIN32
+ /* Restore Console output mode and codepage to whatever they were
+ * when Curl started */
+ SetConsoleMode(TerminalSettings.hStdOut, TerminalSettings.dwOutputMode);
+ SetConsoleOutputCP(TerminalSettings.nCodepage);
+#endif
+}
+
/*
** curl tool main function.
*/
@@ -245,6 +295,9 @@ int main(int argc, char *argv[])
struct GlobalConfig global;
memset(&global, 0, sizeof(global));
+ /* Perform any platform-specific terminal configuration */
+ configure_terminal();
+
main_checkfds();
#if defined(HAVE_SIGNAL) && defined(SIGPIPE)
@@ -270,6 +323,9 @@ int main(int argc, char *argv[])
main_free(&global);
}
+ /* Return the terminal to its original state */
+ restore_terminal();
+
#ifdef __NOVELL_LIBC__
if(getenv("_IN_NETWARE_BASH_") == NULL)
tool_pressanykey();