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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 const int _STDIO_HANDLE_TYPE_TERMINAL = 0; | 7 const int _STDIO_HANDLE_TYPE_TERMINAL = 0; |
8 const int _STDIO_HANDLE_TYPE_PIPE = 1; | 8 const int _STDIO_HANDLE_TYPE_PIPE = 1; |
9 const int _STDIO_HANDLE_TYPE_FILE = 2; | 9 const int _STDIO_HANDLE_TYPE_FILE = 2; |
10 const int _STDIO_HANDLE_TYPE_SOCKET = 3; | 10 const int _STDIO_HANDLE_TYPE_SOCKET = 3; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 * Synchronously read a byte from stdin. This call will block until a byte is | 142 * Synchronously read a byte from stdin. This call will block until a byte is |
143 * available. | 143 * available. |
144 * | 144 * |
145 * If at end of file, -1 is returned. | 145 * If at end of file, -1 is returned. |
146 */ | 146 */ |
147 external int readByteSync(); | 147 external int readByteSync(); |
148 } | 148 } |
149 | 149 |
150 | 150 |
151 /** | 151 /** |
152 * [Stdout] exposes methods to query the terminal for properties. | 152 * [Stdout] represents the [IOSink] for either `stdout` or `stderr`. |
153 * | 153 * |
154 * Use [hasTerminal] to test if there is a terminal associated to stdout. | 154 * It provides a *blocking* `IOSink`, so using this to write will block until |
| 155 * the output is written. |
| 156 * |
| 157 * In some situations this blocking behavior is undesirable as it does not |
| 158 * provide the same non-blocking behavior as dart:io in general exposes. |
| 159 * Use the property [nonBlocking] to get an `IOSink` which has the non-blocking |
| 160 * behavior. |
| 161 * |
| 162 * This class can also be used to check whether `stdout` or `stderr` is |
| 163 * connected to a terminal and query some terminal properties. |
155 */ | 164 */ |
156 class Stdout extends _StdSink implements IOSink { | 165 class Stdout extends _StdSink implements IOSink { |
157 Stdout._(IOSink sink) : super(sink); | 166 final int _fd; |
| 167 IOSink _nonBlocking; |
| 168 |
| 169 Stdout._(IOSink sink, this._fd) : super(sink); |
158 | 170 |
159 /** | 171 /** |
160 * Returns true if there is a terminal attached to stdout. | 172 * Returns true if there is a terminal attached to stdout. |
161 */ | 173 */ |
162 external bool get hasTerminal; | 174 bool get hasTerminal => _hasTerminal(_fd); |
163 | 175 |
164 /** | 176 /** |
165 * Get the number of columns of the terminal. | 177 * Get the number of columns of the terminal. |
166 * | 178 * |
167 * If no terminal is attached to stdout, a [StdoutException] is thrown. See | 179 * If no terminal is attached to stdout, a [StdoutException] is thrown. See |
168 * [hasTerminal] for more info. | 180 * [hasTerminal] for more info. |
169 */ | 181 */ |
170 external int get terminalColumns; | 182 int get terminalColumns => _terminalColumns(_fd); |
171 | 183 |
172 /** | 184 /** |
173 * Get the number of lines of the terminal. | 185 * Get the number of lines of the terminal. |
174 * | 186 * |
175 * If no terminal is attached to stdout, a [StdoutException] is thrown. See | 187 * If no terminal is attached to stdout, a [StdoutException] is thrown. See |
176 * [hasTerminal] for more info. | 188 * [hasTerminal] for more info. |
177 */ | 189 */ |
178 external int get terminalLines; | 190 int get terminalLines => _terminalLines(_fd); |
| 191 |
| 192 external bool _hasTerminal(int fd); |
| 193 external int _terminalColumns(int fd); |
| 194 external int _terminalLines(int fd); |
| 195 |
| 196 /** |
| 197 * Get a non-blocking `IOSink`. |
| 198 */ |
| 199 IOSink get nonBlocking { |
| 200 if (_nonBlocking == null) { |
| 201 _nonBlocking = new IOSink(new _FileStreamConsumer.fromStdio(_fd)); |
| 202 } |
| 203 return _nonBlocking; |
| 204 } |
179 } | 205 } |
180 | 206 |
181 | 207 |
182 class StdoutException implements IOException { | 208 class StdoutException implements IOException { |
183 final String message; | 209 final String message; |
184 final OSError osError; | 210 final OSError osError; |
185 | 211 |
186 const StdoutException(this.message, [this.osError]); | 212 const StdoutException(this.message, [this.osError]); |
187 | 213 |
188 String toString() { | 214 String toString() { |
189 return "StdoutException: $message${osError == null ? "" : ", $osError"}"; | 215 return "StdoutException: $message${osError == null ? "" : ", $osError"}"; |
190 } | 216 } |
191 } | 217 } |
192 | 218 |
| 219 class _StdConsumer implements StreamConsumer<List<int>> { |
| 220 final _file; |
| 221 |
| 222 _StdConsumer(int fd) : _file = _File._openStdioSync(fd); |
| 223 |
| 224 Future addStream(Stream<List<int>> stream) { |
| 225 var completer = new Completer(); |
| 226 var sub; |
| 227 sub = stream.listen( |
| 228 (data) { |
| 229 try { |
| 230 _file.writeFromSync(data); |
| 231 } catch (e, s) { |
| 232 sub.cancel(); |
| 233 completer.completeError(e, s); |
| 234 } |
| 235 }, |
| 236 onError: completer.completeError, |
| 237 onDone: completer.complete, |
| 238 cancelOnError: true); |
| 239 return completer.future; |
| 240 } |
| 241 |
| 242 Future close() { |
| 243 _file.closeSync(); |
| 244 return new Future.value(); |
| 245 } |
| 246 } |
193 | 247 |
194 class _StdSink implements IOSink { | 248 class _StdSink implements IOSink { |
195 final IOSink _sink; | 249 final IOSink _sink; |
196 | 250 |
197 _StdSink(this._sink); | 251 _StdSink(this._sink); |
198 | 252 |
199 Encoding get encoding => _sink.encoding; | 253 Encoding get encoding => _sink.encoding; |
200 void set encoding(Encoding encoding) { | 254 void set encoding(Encoding encoding) { |
201 _sink.encoding = encoding; | 255 _sink.encoding = encoding; |
202 } | 256 } |
(...skipping 17 matching lines...) Expand all Loading... |
220 static const StdioType FILE = const StdioType._("file"); | 274 static const StdioType FILE = const StdioType._("file"); |
221 static const StdioType OTHER = const StdioType._("other"); | 275 static const StdioType OTHER = const StdioType._("other"); |
222 final String name; | 276 final String name; |
223 const StdioType._(this.name); | 277 const StdioType._(this.name); |
224 String toString() => "StdioType: $name"; | 278 String toString() => "StdioType: $name"; |
225 } | 279 } |
226 | 280 |
227 | 281 |
228 Stdin _stdin; | 282 Stdin _stdin; |
229 Stdout _stdout; | 283 Stdout _stdout; |
230 IOSink _stderr; | 284 Stdout _stderr; |
231 | 285 |
232 | 286 |
233 /// The standard input stream of data read by this program. | 287 /// The standard input stream of data read by this program. |
234 Stdin get stdin { | 288 Stdin get stdin { |
235 if (_stdin == null) { | 289 if (_stdin == null) { |
236 _stdin = _StdIOUtils._getStdioInputStream(); | 290 _stdin = _StdIOUtils._getStdioInputStream(); |
237 } | 291 } |
238 return _stdin; | 292 return _stdin; |
239 } | 293 } |
240 | 294 |
241 | 295 |
242 /// The standard output stream of data written by this program. | 296 /// The standard output stream of data written by this program. |
243 Stdout get stdout { | 297 Stdout get stdout { |
244 if (_stdout == null) { | 298 if (_stdout == null) { |
245 _stdout = _StdIOUtils._getStdioOutputStream(1); | 299 _stdout = _StdIOUtils._getStdioOutputStream(1); |
246 } | 300 } |
247 return _stdout; | 301 return _stdout; |
248 } | 302 } |
249 | 303 |
250 | 304 |
251 /// The standard output stream of errors written by this program. | 305 /// The standard output stream of errors written by this program. |
252 IOSink get stderr { | 306 Stdout get stderr { |
253 if (_stderr == null) { | 307 if (_stderr == null) { |
254 _stderr = _StdIOUtils._getStdioOutputStream(2); | 308 _stderr = _StdIOUtils._getStdioOutputStream(2); |
255 } | 309 } |
256 return _stderr; | 310 return _stderr; |
257 } | 311 } |
258 | 312 |
259 | 313 |
260 /// For a stream, returns whether it is attached to a file, pipe, terminal, or | 314 /// For a stream, returns whether it is attached to a file, pipe, terminal, or |
261 /// something else. | 315 /// something else. |
262 StdioType stdioType(object) { | 316 StdioType stdioType(object) { |
(...skipping 28 matching lines...) Expand all Loading... |
291 return StdioType.OTHER; | 345 return StdioType.OTHER; |
292 } | 346 } |
293 | 347 |
294 | 348 |
295 class _StdIOUtils { | 349 class _StdIOUtils { |
296 external static _getStdioOutputStream(int fd); | 350 external static _getStdioOutputStream(int fd); |
297 external static Stdin _getStdioInputStream(); | 351 external static Stdin _getStdioInputStream(); |
298 external static int _socketType(nativeSocket); | 352 external static int _socketType(nativeSocket); |
299 external static _getStdioHandleType(int fd); | 353 external static _getStdioHandleType(int fd); |
300 } | 354 } |
OLD | NEW |