Chromium Code Reviews| 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; |
| 11 const int _STDIO_HANDLE_TYPE_OTHER = 4; | 11 const int _STDIO_HANDLE_TYPE_OTHER = 4; |
| 12 | 12 |
| 13 | |
| 13 class _StdStream extends Stream<List<int>> { | 14 class _StdStream extends Stream<List<int>> { |
| 14 final Stream<List<int>> _stream; | 15 final Stream<List<int>> _stream; |
| 15 | 16 |
| 16 _StdStream(Stream<List<int>> this._stream); | 17 _StdStream(Stream<List<int>> this._stream); |
| 17 | 18 |
| 18 StreamSubscription<List<int>> listen(void onData(List<int> event), | 19 StreamSubscription<List<int>> listen(void onData(List<int> event), |
| 19 {void onError(error), | 20 {void onError(error), |
| 20 void onDone(), | 21 void onDone(), |
| 21 bool cancelOnError}) { | 22 bool cancelOnError}) { |
| 22 return _stream.listen( | 23 return _stream.listen( |
| 23 onData, | 24 onData, |
| 24 onError: onError, | 25 onError: onError, |
| 25 onDone: onDone, | 26 onDone: onDone, |
| 26 cancelOnError: cancelOnError); | 27 cancelOnError: cancelOnError); |
| 27 } | 28 } |
| 28 } | 29 } |
| 29 | 30 |
| 31 | |
| 32 class _StdinEventSink { | |
| 33 Function add; | |
| 34 Function addError; | |
| 35 Function close; | |
| 36 _StdinEventSink(this.add, this.addError, this.close); | |
| 37 } | |
| 38 | |
| 39 /** | |
| 40 * [Stdin] allows both synchronous and asynchronous reads from the standard | |
| 41 * input stream. | |
| 42 * | |
| 43 * Mixing synchronous and asynchronous reads is undefined. | |
| 44 */ | |
| 45 class Stdin extends _StdStream { | |
| 46 Stdin._(Stream<List<int>> stream) : super(stream); | |
| 47 | |
| 48 /** | |
| 49 * Synchronously read a line from stdin. This call will block until a full | |
| 50 * line is available. The line will contain the newline character(s). | |
| 51 * | |
| 52 * If end-of-file is reached, `null` is returned. | |
| 53 * | |
| 54 * If end-of-file is reached after some data has already been read, that data | |
| 55 * is returned. | |
| 56 */ | |
| 57 String readLineSync({Encoding encoding: Encoding.SYSTEM, | |
| 58 bool retainNewlines: false}) { | |
| 59 var decoder = new StringDecoder(encoding)._decoder; | |
| 60 var line = new StringBuffer(); | |
| 61 bool end = false; | |
| 62 bool lastCharWasCR = false; | |
| 63 var sink = new _StdinEventSink( | |
| 64 (char) { | |
| 65 if (!retainNewlines && char == '\r') { | |
|
Bill Hesse
2013/07/02 13:08:27
You need to output 'foo\r' for 'foo\r\r\n'. Move
Anders Johnsen
2013/07/02 13:17:15
Done.
| |
| 66 lastCharWasCR = true; | |
| 67 return; | |
| 68 } else if (char == '\n') { | |
| 69 end = true; | |
| 70 if (!retainNewlines) return; | |
| 71 } else if (lastCharWasCR) { | |
| 72 lastCharWasCR = false; | |
| 73 line.write('\r'); | |
| 74 } | |
| 75 line.write(char); | |
| 76 }, | |
| 77 (error) { | |
| 78 throw error; | |
| 79 }, () { | |
| 80 if (lastCharWasCR) line.write('\r'); | |
| 81 }); | |
| 82 | |
| 83 bool empty = true; | |
| 84 while (!end) { | |
| 85 int b = readByteSync(); | |
| 86 if (b >= 0) { | |
| 87 empty = false; | |
| 88 decoder.handleData([b], sink); | |
| 89 } else { | |
| 90 decoder.handleDone(sink); | |
| 91 break; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 if (empty) return null; | |
| 96 return line.toString(); | |
| 97 } | |
| 98 | |
| 99 /** | |
| 100 * Synchronously read a byte from stdin. This call will block until a byte is | |
| 101 * available. | |
| 102 * | |
| 103 * If at end of file, -1 is returned. | |
| 104 */ | |
| 105 int readByteSync(); | |
| 106 } | |
| 107 | |
| 108 | |
| 30 class _StdSink implements IOSink { | 109 class _StdSink implements IOSink { |
| 31 final IOSink _sink; | 110 final IOSink _sink; |
| 32 | 111 |
| 33 _StdSink(IOSink this._sink); | 112 _StdSink(IOSink this._sink); |
| 34 | 113 |
| 35 Encoding get encoding => _sink.encoding; | 114 Encoding get encoding => _sink.encoding; |
| 36 void set encoding(Encoding encoding) { | 115 void set encoding(Encoding encoding) { |
| 37 _sink.encoding = encoding; | 116 _sink.encoding = encoding; |
| 38 } | 117 } |
| 39 void write(object) => _sink.write(object); | 118 void write(object) => _sink.write(object); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 51 static const StdioType TERMINAL = const StdioType._("terminal"); | 130 static const StdioType TERMINAL = const StdioType._("terminal"); |
| 52 static const StdioType PIPE = const StdioType._("pipe"); | 131 static const StdioType PIPE = const StdioType._("pipe"); |
| 53 static const StdioType FILE = const StdioType._("file"); | 132 static const StdioType FILE = const StdioType._("file"); |
| 54 static const StdioType OTHER = const StdioType._("other"); | 133 static const StdioType OTHER = const StdioType._("other"); |
| 55 final String name; | 134 final String name; |
| 56 const StdioType._(String this.name); | 135 const StdioType._(String this.name); |
| 57 String toString() => "StdioType: $name"; | 136 String toString() => "StdioType: $name"; |
| 58 } | 137 } |
| 59 | 138 |
| 60 | 139 |
| 61 Stream<List<int>> _stdin; | 140 Stdin _stdin; |
| 62 IOSink _stdout; | 141 IOSink _stdout; |
| 63 IOSink _stderr; | 142 IOSink _stderr; |
| 64 | 143 |
| 65 | 144 |
| 66 Stream<List<int>> get stdin { | 145 Stream<List<int>> get stdin { |
| 67 if (_stdin == null) { | 146 if (_stdin == null) { |
| 68 _stdin = _StdIOUtils._getStdioInputStream(); | 147 _stdin = _StdIOUtils._getStdioInputStream(); |
| 69 } | 148 } |
| 70 return _stdin; | 149 return _stdin; |
| 71 } | 150 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 } catch (e) { | 190 } catch (e) { |
| 112 // Only the interface implemented, _sink not available. | 191 // Only the interface implemented, _sink not available. |
| 113 } | 192 } |
| 114 } | 193 } |
| 115 return StdioType.OTHER; | 194 return StdioType.OTHER; |
| 116 } | 195 } |
| 117 | 196 |
| 118 | 197 |
| 119 class _StdIOUtils { | 198 class _StdIOUtils { |
| 120 external static IOSink _getStdioOutputStream(int fd); | 199 external static IOSink _getStdioOutputStream(int fd); |
| 121 external static Stream<List<int>> _getStdioInputStream(); | 200 external static Stdio _getStdioInputStream(); |
| 122 external static int _socketType(nativeSocket); | 201 external static int _socketType(nativeSocket); |
| 123 } | 202 } |
| OLD | NEW |