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