| 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_MACOS) | 8 #if defined(TARGET_OS_MACOS) |
| 9 | 9 |
| 10 #include "bin/stdio.h" | 10 #include "bin/stdio.h" |
| 11 | 11 |
| 12 #include <errno.h> // NOLINT | 12 #include <errno.h> // NOLINT |
| 13 #include <sys/ioctl.h> // NOLINT | 13 #include <sys/ioctl.h> // NOLINT |
| 14 #include <termios.h> // NOLINT | 14 #include <termios.h> // NOLINT |
| 15 | 15 |
| 16 #include "bin/fdutils.h" | 16 #include "bin/fdutils.h" |
| 17 #include "platform/signal_blocker.h" | 17 #include "platform/signal_blocker.h" |
| 18 | 18 |
| 19 namespace dart { | 19 namespace dart { |
| 20 namespace bin { | 20 namespace bin { |
| 21 | 21 |
| 22 int Stdin::ReadByte() { | 22 bool Stdin::ReadByte(int* byte) { |
| 23 int c = getchar(); | 23 int c = NO_RETRY_EXPECTED(getchar()); |
| 24 if (c == EOF) { | 24 if ((c == EOF) && (errno != 0)) { |
| 25 c = -1; | 25 return false; |
| 26 } | 26 } |
| 27 return c; | 27 *byte = (c == EOF) ? -1 : c; |
| 28 return true; |
| 28 } | 29 } |
| 29 | 30 |
| 30 | 31 |
| 31 bool Stdin::GetEchoMode() { | 32 bool Stdin::GetEchoMode(bool* enabled) { |
| 32 struct termios term; | 33 struct termios term; |
| 33 tcgetattr(STDIN_FILENO, &term); | 34 int status = NO_RETRY_EXPECTED(tcgetattr(STDIN_FILENO, &term)); |
| 34 return ((term.c_lflag & ECHO) != 0); | 35 if (status != 0) { |
| 36 return false; |
| 37 } |
| 38 *enabled = ((term.c_lflag & ECHO) != 0); |
| 39 return true; |
| 35 } | 40 } |
| 36 | 41 |
| 37 | 42 |
| 38 void Stdin::SetEchoMode(bool enabled) { | 43 bool Stdin::SetEchoMode(bool enabled) { |
| 39 struct termios term; | 44 struct termios term; |
| 40 tcgetattr(STDIN_FILENO, &term); | 45 int status = NO_RETRY_EXPECTED(tcgetattr(STDIN_FILENO, &term)); |
| 46 if (status != 0) { |
| 47 return false; |
| 48 } |
| 41 if (enabled) { | 49 if (enabled) { |
| 42 term.c_lflag |= (ECHO | ECHONL); | 50 term.c_lflag |= (ECHO | ECHONL); |
| 43 } else { | 51 } else { |
| 44 term.c_lflag &= ~(ECHO | ECHONL); | 52 term.c_lflag &= ~(ECHO | ECHONL); |
| 45 } | 53 } |
| 46 tcsetattr(STDIN_FILENO, TCSANOW, &term); | 54 status = NO_RETRY_EXPECTED(tcsetattr(STDIN_FILENO, TCSANOW, &term)); |
| 55 return (status == 0); |
| 47 } | 56 } |
| 48 | 57 |
| 49 | 58 |
| 50 bool Stdin::GetLineMode() { | 59 bool Stdin::GetLineMode(bool* enabled) { |
| 51 struct termios term; | 60 struct termios term; |
| 52 tcgetattr(STDIN_FILENO, &term); | 61 int status = NO_RETRY_EXPECTED(tcgetattr(STDIN_FILENO, &term)); |
| 53 return ((term.c_lflag & ICANON) != 0); | 62 if (status != 0) { |
| 63 return false; |
| 64 } |
| 65 *enabled = ((term.c_lflag & ICANON) != 0); |
| 66 return true; |
| 54 } | 67 } |
| 55 | 68 |
| 56 | 69 |
| 57 void Stdin::SetLineMode(bool enabled) { | 70 bool Stdin::SetLineMode(bool enabled) { |
| 58 struct termios term; | 71 struct termios term; |
| 59 tcgetattr(STDIN_FILENO, &term); | 72 int status = NO_RETRY_EXPECTED(tcgetattr(STDIN_FILENO, &term)); |
| 73 if (status != 0) { |
| 74 return false; |
| 75 } |
| 60 if (enabled) { | 76 if (enabled) { |
| 61 term.c_lflag |= ICANON; | 77 term.c_lflag |= ICANON; |
| 62 } else { | 78 } else { |
| 63 term.c_lflag &= ~(ICANON); | 79 term.c_lflag &= ~(ICANON); |
| 64 } | 80 } |
| 65 tcsetattr(STDIN_FILENO, TCSANOW, &term); | 81 status = NO_RETRY_EXPECTED(tcsetattr(STDIN_FILENO, TCSANOW, &term)); |
| 82 return (status == 0); |
| 66 } | 83 } |
| 67 | 84 |
| 68 | 85 |
| 69 bool Stdout::GetTerminalSize(intptr_t fd, int size[2]) { | 86 bool Stdout::GetTerminalSize(intptr_t fd, int size[2]) { |
| 70 struct winsize w; | 87 struct winsize w; |
| 71 int status = NO_RETRY_EXPECTED(ioctl(fd, TIOCGWINSZ, &w)); | 88 int status = NO_RETRY_EXPECTED(ioctl(fd, TIOCGWINSZ, &w)); |
| 72 if ((status == 0) && ((w.ws_col != 0) || (w.ws_row != 0))) { | 89 if ((status == 0) && ((w.ws_col != 0) || (w.ws_row != 0))) { |
| 73 size[0] = w.ws_col; | 90 size[0] = w.ws_col; |
| 74 size[1] = w.ws_row; | 91 size[1] = w.ws_row; |
| 75 return true; | 92 return true; |
| 76 } | 93 } |
| 77 return false; | 94 return false; |
| 78 } | 95 } |
| 79 | 96 |
| 80 } // namespace bin | 97 } // namespace bin |
| 81 } // namespace dart | 98 } // namespace dart |
| 82 | 99 |
| 83 #endif // defined(TARGET_OS_MACOS) | 100 #endif // defined(TARGET_OS_MACOS) |
| 84 | 101 |
| 85 #endif // !defined(DART_IO_DISABLED) | 102 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |