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 |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #include "bin/stdio.h" |
6 | |
7 | #include "bin/builtin.h" |
8 | #include "bin/dartutils.h" |
9 | #include "bin/utils.h" |
10 | |
11 | #include "include/dart_api.h" |
12 | |
13 | #include "platform/globals.h" |
14 | #include "platform/utils.h" |
15 | |
16 | namespace dart { |
17 | namespace bin { |
18 | |
19 | static bool GetIntptrArgument(Dart_NativeArguments args, |
20 | intptr_t idx, |
21 | intptr_t* value) { |
22 | ASSERT(value != NULL); |
23 | int64_t v; |
24 | Dart_Handle status = Dart_GetNativeIntegerArgument(args, 0, &v); |
25 | if (Dart_IsError(status)) { |
26 | // The caller is expecting an OSError if something goes wrong. |
27 | OSError os_error(-1, "Invalid argument" , OSError::kUnknown); |
28 | Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
29 | return false; |
30 | } |
31 | if ((v < kIntptrMin) || (kIntptrMax < v)) { |
32 | // The caller is expecting an OSError if something goes wrong. |
33 | OSError os_error(-1, "Invalid argument" , OSError::kUnknown); |
34 | Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
35 | return false; |
36 | } |
37 | *value = static_cast<intptr_t>(v); |
38 | return true; |
39 | } |
40 | |
41 | void FUNCTION_NAME(Stdin_ReadByte)(Dart_NativeArguments args) { |
42 | ScopedBlockingCall blocker; |
43 | intptr_t fd; |
44 | if (!GetIntptrArgument(args, 0, &fd)) { |
45 | return; |
46 | } |
47 | int byte = -1; |
48 | if (Stdin::ReadByte(fd, &byte)) { |
49 | Dart_SetIntegerReturnValue(args, byte); |
50 | } else { |
51 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
52 | } |
53 | } |
54 | |
55 | void FUNCTION_NAME(Stdin_GetEchoMode)(Dart_NativeArguments args) { |
56 | bool enabled = false; |
57 | intptr_t fd; |
58 | if (!GetIntptrArgument(args, 0, &fd)) { |
59 | return; |
60 | } |
61 | if (Stdin::GetEchoMode(fd, &enabled)) { |
62 | Dart_SetBooleanReturnValue(args, enabled); |
63 | } else { |
64 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
65 | } |
66 | } |
67 | |
68 | void FUNCTION_NAME(Stdin_SetEchoMode)(Dart_NativeArguments args) { |
69 | intptr_t fd; |
70 | if (!GetIntptrArgument(args, 0, &fd)) { |
71 | return; |
72 | } |
73 | bool enabled; |
74 | Dart_Handle status = Dart_GetNativeBooleanArgument(args, 1, &enabled); |
75 | if (Dart_IsError(status)) { |
76 | // The caller is expecting an OSError if something goes wrong. |
77 | OSError os_error(-1, "Invalid argument" , OSError::kUnknown); |
78 | Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
79 | return; |
80 | } |
81 | if (Stdin::SetEchoMode(fd, enabled)) { |
82 | Dart_SetReturnValue(args, Dart_True()); |
83 | } else { |
84 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
85 | } |
86 | } |
87 | |
88 | void FUNCTION_NAME(Stdin_GetLineMode)(Dart_NativeArguments args) { |
89 | bool enabled = false; |
90 | intptr_t fd; |
91 | if (!GetIntptrArgument(args, 0, &fd)) { |
92 | return; |
93 | } |
94 | if (Stdin::GetLineMode(fd, &enabled)) { |
95 | Dart_SetBooleanReturnValue(args, enabled); |
96 | } else { |
97 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
98 | } |
99 | } |
100 | |
101 | void FUNCTION_NAME(Stdin_SetLineMode)(Dart_NativeArguments args) { |
102 | intptr_t fd; |
103 | if (!GetIntptrArgument(args, 0, &fd)) { |
104 | return; |
105 | } |
106 | bool enabled; |
107 | Dart_Handle status = Dart_GetNativeBooleanArgument(args, 1, &enabled); |
108 | if (Dart_IsError(status)) { |
109 | // The caller is expecting an OSError if something goes wrong. |
110 | OSError os_error(-1, "Invalid argument" , OSError::kUnknown); |
111 | Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
112 | return; |
113 | } |
114 | if (Stdin::SetLineMode(fd, enabled)) { |
115 | Dart_SetBooleanReturnValue(args, true); |
116 | } else { |
117 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
118 | } |
119 | } |
120 | |
121 | void FUNCTION_NAME(Stdin_AnsiSupported)(Dart_NativeArguments args) { |
122 | bool supported = false; |
123 | intptr_t fd; |
124 | if (!GetIntptrArgument(args, 0, &fd)) { |
125 | return; |
126 | } |
127 | if (Stdin::AnsiSupported(fd, &supported)) { |
128 | Dart_SetBooleanReturnValue(args, supported); |
129 | } else { |
130 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
131 | } |
132 | } |
133 | |
134 | void FUNCTION_NAME(Stdout_GetTerminalSize)(Dart_NativeArguments args) { |
135 | intptr_t fd; |
136 | if (!GetIntptrArgument(args, 0, &fd)) { |
137 | return; |
138 | } |
139 | int size[2]; |
140 | if (Stdout::GetTerminalSize(fd, size)) { |
141 | Dart_Handle list = Dart_NewList(2); |
142 | Dart_ListSetAt(list, 0, Dart_NewInteger(size[0])); |
143 | Dart_ListSetAt(list, 1, Dart_NewInteger(size[1])); |
144 | Dart_SetReturnValue(args, list); |
145 | } else { |
146 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
147 | } |
148 | } |
149 | |
150 | void FUNCTION_NAME(Stdout_AnsiSupported)(Dart_NativeArguments args) { |
151 | intptr_t fd; |
152 | if (!GetIntptrArgument(args, 0, &fd)) { |
153 | return; |
154 | } |
155 | bool supported = false; |
156 | if (Stdout::AnsiSupported(fd, &supported)) { |
157 | Dart_SetBooleanReturnValue(args, supported); |
158 | } else { |
159 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
160 | } |
161 | } |
162 | |
163 | } // namespace bin |
164 | } // namespace dart |
165 | |