| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
| 6 | 6 |
| 7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
| 8 #if defined(TARGET_OS_WINDOWS) | 8 #if defined(TARGET_OS_WINDOWS) |
| 9 | 9 |
| 10 #include "bin/stdio.h" | 10 #include "bin/stdio.h" |
| 11 | 11 |
| 12 namespace dart { | 12 namespace dart { |
| 13 namespace bin { | 13 namespace bin { |
| 14 | 14 |
| 15 int Stdin::ReadByte() { | 15 bool Stdin::ReadByte(int* byte) { |
| 16 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); | 16 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); |
| 17 uint8_t buffer[1]; | 17 uint8_t buffer[1]; |
| 18 DWORD read = 0; | 18 DWORD read = 0; |
| 19 int c = -1; | 19 BOOL success = ReadFile(h, buffer, 1, &read, NULL); |
| 20 if (ReadFile(h, buffer, 1, &read, NULL) && (read == 1)) { | 20 if (!success && (GetLastError() != ERROR_BROKEN_PIPE)) { |
| 21 c = buffer[0]; | 21 return false; |
| 22 } | 22 } |
| 23 return c; | 23 *byte = (read == 1) ? buffer[0] : -1; |
| 24 return true; |
| 24 } | 25 } |
| 25 | 26 |
| 26 | 27 |
| 27 bool Stdin::GetEchoMode() { | 28 bool Stdin::GetEchoMode(bool* enabled) { |
| 28 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); | 29 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); |
| 29 DWORD mode; | 30 DWORD mode; |
| 30 if (!GetConsoleMode(h, &mode)) { | 31 if (!GetConsoleMode(h, &mode)) { |
| 31 return false; | 32 return false; |
| 32 } | 33 } |
| 33 return ((mode & ENABLE_ECHO_INPUT) != 0); | 34 *enabled = ((mode & ENABLE_ECHO_INPUT) != 0); |
| 35 return true; |
| 34 } | 36 } |
| 35 | 37 |
| 36 | 38 |
| 37 void Stdin::SetEchoMode(bool enabled) { | 39 bool Stdin::SetEchoMode(bool enabled) { |
| 38 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); | 40 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); |
| 39 DWORD mode; | 41 DWORD mode; |
| 40 if (!GetConsoleMode(h, &mode)) { | 42 if (!GetConsoleMode(h, &mode)) { |
| 41 return; | 43 return false; |
| 42 } | 44 } |
| 43 if (enabled) { | 45 if (enabled) { |
| 44 mode |= ENABLE_ECHO_INPUT; | 46 mode |= ENABLE_ECHO_INPUT; |
| 45 } else { | 47 } else { |
| 46 mode &= ~ENABLE_ECHO_INPUT; | 48 mode &= ~ENABLE_ECHO_INPUT; |
| 47 } | 49 } |
| 48 SetConsoleMode(h, mode); | 50 return SetConsoleMode(h, mode); |
| 49 } | 51 } |
| 50 | 52 |
| 51 | 53 |
| 52 bool Stdin::GetLineMode() { | 54 bool Stdin::GetLineMode(bool* enabled) { |
| 53 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); | 55 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); |
| 54 DWORD mode; | 56 DWORD mode; |
| 55 if (!GetConsoleMode(h, &mode)) { | 57 if (!GetConsoleMode(h, &mode)) { |
| 56 return false; | 58 return false; |
| 57 } | 59 } |
| 58 return (mode & ENABLE_LINE_INPUT) != 0; | 60 *enabled = (mode & ENABLE_LINE_INPUT) != 0; |
| 61 return true; |
| 59 } | 62 } |
| 60 | 63 |
| 61 | 64 |
| 62 void Stdin::SetLineMode(bool enabled) { | 65 bool Stdin::SetLineMode(bool enabled) { |
| 63 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); | 66 HANDLE h = GetStdHandle(STD_INPUT_HANDLE); |
| 64 DWORD mode; | 67 DWORD mode; |
| 65 if (!GetConsoleMode(h, &mode)) { | 68 if (!GetConsoleMode(h, &mode)) { |
| 66 return; | 69 return false; |
| 67 } | 70 } |
| 68 if (enabled) { | 71 if (enabled) { |
| 69 mode |= ENABLE_LINE_INPUT; | 72 mode |= ENABLE_LINE_INPUT; |
| 70 } else { | 73 } else { |
| 71 mode &= ~ENABLE_LINE_INPUT; | 74 mode &= ~ENABLE_LINE_INPUT; |
| 72 } | 75 } |
| 73 SetConsoleMode(h, mode); | 76 return SetConsoleMode(h, mode); |
| 74 } | 77 } |
| 75 | 78 |
| 76 | 79 |
| 77 bool Stdout::GetTerminalSize(intptr_t fd, int size[2]) { | 80 bool Stdout::GetTerminalSize(intptr_t fd, int size[2]) { |
| 78 HANDLE h; | 81 HANDLE h; |
| 79 if (fd == 1) { | 82 if (fd == 1) { |
| 80 h = GetStdHandle(STD_OUTPUT_HANDLE); | 83 h = GetStdHandle(STD_OUTPUT_HANDLE); |
| 81 } else { | 84 } else { |
| 82 h = GetStdHandle(STD_ERROR_HANDLE); | 85 h = GetStdHandle(STD_ERROR_HANDLE); |
| 83 } | 86 } |
| 84 CONSOLE_SCREEN_BUFFER_INFO info; | 87 CONSOLE_SCREEN_BUFFER_INFO info; |
| 85 if (!GetConsoleScreenBufferInfo(h, &info)) { | 88 if (!GetConsoleScreenBufferInfo(h, &info)) { |
| 86 return false; | 89 return false; |
| 87 } | 90 } |
| 88 size[0] = info.srWindow.Right - info.srWindow.Left + 1; | 91 size[0] = info.srWindow.Right - info.srWindow.Left + 1; |
| 89 size[1] = info.srWindow.Bottom - info.srWindow.Top + 1; | 92 size[1] = info.srWindow.Bottom - info.srWindow.Top + 1; |
| 90 return true; | 93 return true; |
| 91 } | 94 } |
| 92 | 95 |
| 93 } // namespace bin | 96 } // namespace bin |
| 94 } // namespace dart | 97 } // namespace dart |
| 95 | 98 |
| 96 #endif // defined(TARGET_OS_WINDOWS) | 99 #endif // defined(TARGET_OS_WINDOWS) |
| 97 | 100 |
| 98 #endif // !defined(DART_IO_DISABLED) | 101 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |