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

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

Issue 2753233002: [dart:io] Move Platform.ansiSupported to {Stdin,Stdout}.supportsAnsiEscapes (Closed)
Patch Set: . 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
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(HOST_OS_WINDOWS) 6 #if defined(HOST_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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 static void SaveAndConfigureConsole() { 76 static void SaveAndConfigureConsole() {
77 MutexLocker ml(platform_win_mutex_); 77 MutexLocker ml(platform_win_mutex_);
78 // Set both the input and output code pages to UTF8. 78 // Set both the input and output code pages to UTF8.
79 ASSERT(saved_output_cp_ == -1); 79 ASSERT(saved_output_cp_ == -1);
80 ASSERT(saved_input_cp_ == -1); 80 ASSERT(saved_input_cp_ == -1);
81 saved_output_cp_ = GetConsoleOutputCP(); 81 saved_output_cp_ = GetConsoleOutputCP();
82 saved_input_cp_ = GetConsoleCP(); 82 saved_input_cp_ = GetConsoleCP();
83 SetConsoleOutputCP(CP_UTF8); 83 SetConsoleOutputCP(CP_UTF8);
84 SetConsoleCP(CP_UTF8); 84 SetConsoleCP(CP_UTF8);
85 85
86 ansi_supported_ = true; 86 // Try to set the bits for ANSI support, but swallow any failures.
87 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); 87 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
88 DWORD out_mode; 88 DWORD out_mode;
89 if ((out != INVALID_HANDLE_VALUE) && GetConsoleMode(out, &out_mode)) { 89 if ((out != INVALID_HANDLE_VALUE) && GetConsoleMode(out, &out_mode)) {
90 const DWORD request = out_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING; 90 const DWORD request = out_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
91 ansi_supported_ = ansi_supported_ && SetConsoleMode(out, request); 91 SetConsoleMode(out, request);
92 } else {
93 ansi_supported_ = false;
94 } 92 }
95
96 HANDLE in = GetStdHandle(STD_INPUT_HANDLE); 93 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
97 DWORD in_mode; 94 DWORD in_mode;
98 if ((in != INVALID_HANDLE_VALUE) && GetConsoleMode(in, &in_mode)) { 95 if ((in != INVALID_HANDLE_VALUE) && GetConsoleMode(in, &in_mode)) {
99 const DWORD request = in_mode | ENABLE_VIRTUAL_TERMINAL_INPUT; 96 const DWORD request = in_mode | ENABLE_VIRTUAL_TERMINAL_INPUT;
100 ansi_supported_ = ansi_supported_ && SetConsoleMode(in, request); 97 SetConsoleMode(in, request);
101 } else {
102 ansi_supported_ = false;
103 } 98 }
104 } 99 }
105 100
106 static void RestoreConsole() { 101 static void RestoreConsole() {
107 MutexLocker ml(platform_win_mutex_); 102 MutexLocker ml(platform_win_mutex_);
108 RestoreConsoleLocked(); 103 RestoreConsoleLocked();
109 } 104 }
110 105
111 static bool ansi_supported() { return ansi_supported_; }
112
113 private: 106 private:
114 static Mutex* platform_win_mutex_; 107 static Mutex* platform_win_mutex_;
115 static int saved_output_cp_; 108 static int saved_output_cp_;
116 static int saved_input_cp_; 109 static int saved_input_cp_;
117 static bool ansi_supported_;
118 110
119 static void RestoreConsoleLocked() { 111 static void RestoreConsoleLocked() {
120 // STD_OUTPUT_HANDLE and STD_INPUT_HANDLE may have been closed or 112 // STD_OUTPUT_HANDLE and STD_INPUT_HANDLE may have been closed or
121 // redirected. Therefore, we explicitly open the CONOUT$ and CONIN$ 113 // redirected. Therefore, we explicitly open the CONOUT$ and CONIN$
122 // devices, so that we can be sure that we are really unsetting 114 // devices, so that we can be sure that we are really unsetting
123 // ENABLE_VIRTUAL_TERMINAL_PROCESSING and ENABLE_VIRTUAL_TERMINAL_INPUT 115 // ENABLE_VIRTUAL_TERMINAL_PROCESSING and ENABLE_VIRTUAL_TERMINAL_INPUT
124 // respectively. 116 // respectively.
125 const intptr_t kWideBufLen = 64; 117 const intptr_t kWideBufLen = 64;
126 const char* conout = "CONOUT$"; 118 const char* conout = "CONOUT$";
127 wchar_t widebuf[kWideBufLen]; 119 wchar_t widebuf[kWideBufLen];
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 // return an error code and/or set errno. 164 // return an error code and/or set errno.
173 } 165 }
174 166
175 DISALLOW_ALLOCATION(); 167 DISALLOW_ALLOCATION();
176 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformWin); 168 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformWin);
177 }; 169 };
178 170
179 int PlatformWin::saved_output_cp_ = -1; 171 int PlatformWin::saved_output_cp_ = -1;
180 int PlatformWin::saved_input_cp_ = -1; 172 int PlatformWin::saved_input_cp_ = -1;
181 Mutex* PlatformWin::platform_win_mutex_ = NULL; 173 Mutex* PlatformWin::platform_win_mutex_ = NULL;
182 bool PlatformWin::ansi_supported_ = false;
183 174
184 bool Platform::Initialize() { 175 bool Platform::Initialize() {
185 PlatformWin::InitOnce(); 176 PlatformWin::InitOnce();
186 PlatformWin::SaveAndConfigureConsole(); 177 PlatformWin::SaveAndConfigureConsole();
187 return true; 178 return true;
188 } 179 }
189 180
190 181
191 int Platform::NumberOfProcessors() { 182 int Platform::NumberOfProcessors() {
192 SYSTEM_INFO info; 183 SYSTEM_INFO info;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 if (GetLastError() != ERROR_SUCCESS) { 260 if (GetLastError() != ERROR_SUCCESS) {
270 return NULL; 261 return NULL;
271 } 262 }
272 char* path = StringUtilsWin::WideToUtf8(tmp_buffer); 263 char* path = StringUtilsWin::WideToUtf8(tmp_buffer);
273 // Return the canonical path as the returned path might contain symlinks. 264 // Return the canonical path as the returned path might contain symlinks.
274 const char* canon_path = File::GetCanonicalPath(path); 265 const char* canon_path = File::GetCanonicalPath(path);
275 return canon_path; 266 return canon_path;
276 } 267 }
277 268
278 269
279 bool Platform::AnsiSupported() {
280 return PlatformWin::ansi_supported();
281 }
282
283
284 void Platform::Exit(int exit_code) { 270 void Platform::Exit(int exit_code) {
285 // TODO(zra): Remove once VM shuts down cleanly. 271 // TODO(zra): Remove once VM shuts down cleanly.
286 ::dart::private_flag_windows_run_tls_destructors = false; 272 ::dart::private_flag_windows_run_tls_destructors = false;
287 // Restore the console's output code page 273 // Restore the console's output code page
288 PlatformWin::RestoreConsole(); 274 PlatformWin::RestoreConsole();
289 // On Windows we use ExitProcess so that threads can't clobber the exit_code. 275 // On Windows we use ExitProcess so that threads can't clobber the exit_code.
290 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870 276 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870
291 ::ExitProcess(exit_code); 277 ::ExitProcess(exit_code);
292 } 278 }
293 279
294 } // namespace bin 280 } // namespace bin
295 } // namespace dart 281 } // namespace dart
296 282
297 #endif // defined(HOST_OS_WINDOWS) 283 #endif // defined(HOST_OS_WINDOWS)
OLDNEW
« no previous file with comments | « runtime/bin/platform_unsupported.cc ('k') | runtime/bin/stdio.h » ('j') | sdk/lib/io/stdio.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698