Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(872)

Side by Side Diff: runtime/bin/platform_win.cc

Issue 2739683005: [dart:io] Adds Platform.ansiSupported (Closed)
Patch Set: Cleanup Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/platform_unsupported.cc ('k') | sdk/lib/_internal/js_runtime/lib/io_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_WINDOWS) 6 #if defined(TARGET_OS_WINDOWS)
7 7
8 #include "bin/platform.h" 8 #include "bin/platform.h"
9 9
10 #include <crtdbg.h> 10 #include <crtdbg.h>
(...skipping 28 matching lines...) Expand all
39 const char* Platform::executable_name_ = NULL; 39 const char* Platform::executable_name_ = NULL;
40 char* Platform::resolved_executable_name_ = NULL; 40 char* Platform::resolved_executable_name_ = NULL;
41 int Platform::script_index_ = 1; 41 int Platform::script_index_ = 1;
42 char** Platform::argv_ = NULL; 42 char** Platform::argv_ = NULL;
43 43
44 class PlatformWin { 44 class PlatformWin {
45 public: 45 public:
46 static void InitOnce() { 46 static void InitOnce() {
47 platform_win_mutex_ = new Mutex(); 47 platform_win_mutex_ = new Mutex();
48 saved_output_cp_ = -1; 48 saved_output_cp_ = -1;
49 saved_input_cp_ = -1;
49 // Set up a no-op handler so that CRT functions return an error instead of 50 // Set up a no-op handler so that CRT functions return an error instead of
50 // hitting an assertion failure. 51 // hitting an assertion failure.
51 // See: https://msdn.microsoft.com/en-us/library/a9yf33zb.aspx 52 // See: https://msdn.microsoft.com/en-us/library/a9yf33zb.aspx
52 _set_invalid_parameter_handler(InvalidParameterHandler); 53 _set_invalid_parameter_handler(InvalidParameterHandler);
53 // Disable the message box for assertions in the CRT in Debug builds. 54 // Disable the message box for assertions in the CRT in Debug builds.
54 // See: https://msdn.microsoft.com/en-us/library/1y71x448.aspx 55 // See: https://msdn.microsoft.com/en-us/library/1y71x448.aspx
55 _CrtSetReportMode(_CRT_ASSERT, 0); 56 _CrtSetReportMode(_CRT_ASSERT, 0);
56 // Disable dialog boxes for "critical" errors or when OpenFile cannot find 57 // Disable dialog boxes for "critical" errors or when OpenFile cannot find
57 // the requested file. See: 58 // the requested file. See:
58 // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v= vs.85).aspx 59 // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v= vs.85).aspx
59 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); 60 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
60 } 61 }
61 62
62 static void SaveAndConfigureConsole() { 63 static void SaveAndConfigureConsole() {
63 MutexLocker ml(platform_win_mutex_); 64 MutexLocker ml(platform_win_mutex_);
65 // Set both the input and output code pages to UTF8.
64 ASSERT(saved_output_cp_ == -1); 66 ASSERT(saved_output_cp_ == -1);
67 ASSERT(saved_input_cp_ == -1);
65 saved_output_cp_ = GetConsoleOutputCP(); 68 saved_output_cp_ = GetConsoleOutputCP();
69 saved_input_cp_ = GetConsoleCP();
66 SetConsoleOutputCP(CP_UTF8); 70 SetConsoleOutputCP(CP_UTF8);
71 SetConsoleCP(CP_UTF8);
67 72
73 ansi_supported_ = true;
68 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); 74 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
69 if ((out != INVALID_HANDLE_VALUE) && 75 DWORD out_mode;
70 GetConsoleMode(out, &saved_console_out_mode_)) { 76 if ((out != INVALID_HANDLE_VALUE) && GetConsoleMode(out, &out_mode)) {
71 const DWORD request = 77 const DWORD request = out_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
72 saved_console_out_mode_ | ENABLE_VIRTUAL_TERMINAL_PROCESSING; 78 ansi_supported_ = ansi_supported_ && SetConsoleMode(out, request);
73 SetConsoleMode(out, request); 79 } else {
80 ansi_supported_ = false;
74 } 81 }
75 82
76 HANDLE in = GetStdHandle(STD_INPUT_HANDLE); 83 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
77 if ((in != INVALID_HANDLE_VALUE) && 84 DWORD in_mode;
78 GetConsoleMode(in, &saved_console_in_mode_)) { 85 if ((in != INVALID_HANDLE_VALUE) && GetConsoleMode(in, &in_mode)) {
79 const DWORD request = 86 const DWORD request = in_mode | ENABLE_VIRTUAL_TERMINAL_INPUT;
80 saved_console_in_mode_ | ENABLE_VIRTUAL_TERMINAL_INPUT; 87 ansi_supported_ = ansi_supported_ && SetConsoleMode(in, request);
81 SetConsoleMode(in, request); 88 } else {
89 ansi_supported_ = false;
sortie 2017/03/16 11:39:08 This looks like escape codes are considered unsupp
82 } 90 }
83 } 91 }
84 92
85 static void RestoreConsole() { 93 static void RestoreConsole() {
86 MutexLocker ml(platform_win_mutex_); 94 MutexLocker ml(platform_win_mutex_);
95
96 // STD_OUTPUT_HANDLE and STD_INPUT_HANDLE may have been closed or
97 // redirected. Therefore, we explicitly open the CONOUT$ and CONIN$
98 // devices, so that we can be sure that we are really unsetting
99 // ENABLE_VIRTUAL_TERMINAL_PROCESSING and ENABLE_VIRTUAL_TERMINAL_INPUT
100 // respectively.
101 HANDLE out;
102 {
103 Utf8ToWideScope conin("CONOUT$");
104 out = CreateFileW(conin.wide(), GENERIC_READ | GENERIC_WRITE,
105 FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
106 if (out != INVALID_HANDLE_VALUE) {
107 SetStdHandle(STD_OUTPUT_HANDLE, out);
108 }
109 }
110 DWORD out_mode;
111 if ((out != INVALID_HANDLE_VALUE) && GetConsoleMode(out, &out_mode)) {
112 DWORD request = out_mode & ~ENABLE_VIRTUAL_TERMINAL_INPUT;
113 SetConsoleMode(out, request);
114 }
115
116 HANDLE in;
117 {
118 Utf8ToWideScope conin("CONIN$");
119 in = CreateFileW(conin.wide(), GENERIC_READ | GENERIC_WRITE,
120 FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
121 if (in != INVALID_HANDLE_VALUE) {
122 SetStdHandle(STD_INPUT_HANDLE, in);
123 }
124 }
125 DWORD in_mode;
126 if ((in != INVALID_HANDLE_VALUE) && GetConsoleMode(in, &in_mode)) {
127 DWORD request = in_mode & ~ENABLE_VIRTUAL_TERMINAL_INPUT;
128 SetConsoleMode(in, request);
129 }
130
87 if (saved_output_cp_ != -1) { 131 if (saved_output_cp_ != -1) {
88 SetConsoleOutputCP(saved_output_cp_); 132 SetConsoleOutputCP(saved_output_cp_);
89 saved_output_cp_ = -1; 133 saved_output_cp_ = -1;
90 } 134 }
91 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); 135 if (saved_input_cp_ != -1) {
92 if (out != INVALID_HANDLE_VALUE) { 136 SetConsoleCP(saved_input_cp_);
93 SetConsoleMode(out, saved_console_out_mode_); 137 saved_input_cp_ = -1;
94 saved_console_out_mode_ = 0;
95 }
96 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
97 if (in != INVALID_HANDLE_VALUE) {
98 SetConsoleMode(in, saved_console_in_mode_);
99 saved_console_in_mode_ = 0;
100 } 138 }
101 } 139 }
102 140
141 static bool ansi_supported() { return ansi_supported_; }
142
103 private: 143 private:
104 static Mutex* platform_win_mutex_; 144 static Mutex* platform_win_mutex_;
105 static int saved_output_cp_; 145 static int saved_output_cp_;
106 static DWORD saved_console_out_mode_; 146 static int saved_input_cp_;
107 static DWORD saved_console_in_mode_; 147 static bool ansi_supported_;
108 148
109 static void InvalidParameterHandler(const wchar_t* expression, 149 static void InvalidParameterHandler(const wchar_t* expression,
110 const wchar_t* function, 150 const wchar_t* function,
111 const wchar_t* file, 151 const wchar_t* file,
112 unsigned int line, 152 unsigned int line,
113 uintptr_t reserved) { 153 uintptr_t reserved) {
114 // Doing nothing here means that the CRT call that invoked it will 154 // Doing nothing here means that the CRT call that invoked it will
115 // return an error code and/or set errno. 155 // return an error code and/or set errno.
116 } 156 }
117 157
118 DISALLOW_ALLOCATION(); 158 DISALLOW_ALLOCATION();
119 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformWin); 159 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformWin);
120 }; 160 };
121 161
122 int PlatformWin::saved_output_cp_ = -1; 162 int PlatformWin::saved_output_cp_ = -1;
123 DWORD PlatformWin::saved_console_out_mode_ = 0; 163 int PlatformWin::saved_input_cp_ = -1;
124 DWORD PlatformWin::saved_console_in_mode_ = 0;
125 Mutex* PlatformWin::platform_win_mutex_ = NULL; 164 Mutex* PlatformWin::platform_win_mutex_ = NULL;
165 bool PlatformWin::ansi_supported_ = false;
126 166
127 bool Platform::Initialize() { 167 bool Platform::Initialize() {
128 PlatformWin::InitOnce(); 168 PlatformWin::InitOnce();
129 PlatformWin::SaveAndConfigureConsole(); 169 PlatformWin::SaveAndConfigureConsole();
130 return true; 170 return true;
131 } 171 }
132 172
133 173
134 int Platform::NumberOfProcessors() { 174 int Platform::NumberOfProcessors() {
135 SYSTEM_INFO info; 175 SYSTEM_INFO info;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 if (GetLastError() != ERROR_SUCCESS) { 252 if (GetLastError() != ERROR_SUCCESS) {
213 return NULL; 253 return NULL;
214 } 254 }
215 char* path = StringUtilsWin::WideToUtf8(tmp_buffer); 255 char* path = StringUtilsWin::WideToUtf8(tmp_buffer);
216 // Return the canonical path as the returned path might contain symlinks. 256 // Return the canonical path as the returned path might contain symlinks.
217 const char* canon_path = File::GetCanonicalPath(path); 257 const char* canon_path = File::GetCanonicalPath(path);
218 return canon_path; 258 return canon_path;
219 } 259 }
220 260
221 261
262 bool Platform::AnsiSupported() {
263 return PlatformWin::ansi_supported();
264 }
265
266
222 void Platform::Exit(int exit_code) { 267 void Platform::Exit(int exit_code) {
223 // TODO(zra): Remove once VM shuts down cleanly. 268 // TODO(zra): Remove once VM shuts down cleanly.
224 ::dart::private_flag_windows_run_tls_destructors = false; 269 ::dart::private_flag_windows_run_tls_destructors = false;
225 // Restore the console's output code page 270 // Restore the console's output code page
226 PlatformWin::RestoreConsole(); 271 PlatformWin::RestoreConsole();
227 // On Windows we use ExitProcess so that threads can't clobber the exit_code. 272 // On Windows we use ExitProcess so that threads can't clobber the exit_code.
228 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870 273 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870
229 ::ExitProcess(exit_code); 274 ::ExitProcess(exit_code);
230 } 275 }
231 276
232 } // namespace bin 277 } // namespace bin
233 } // namespace dart 278 } // namespace dart
234 279
235 #endif // defined(TARGET_OS_WINDOWS) 280 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW
« no previous file with comments | « runtime/bin/platform_unsupported.cc ('k') | sdk/lib/_internal/js_runtime/lib/io_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698