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] represents the [IOSink] for either `stdout` or `stderr`. | 152 * [Stdout] exposes methods to query the terminal for properties. |
153 * | 153 * |
154 * It provides a *blocking* `IOSink`, so using this to write will block until | 154 * Use [hasTerminal] to test if there is a terminal associated to stdout. |
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. | |
164 */ | 155 */ |
165 class Stdout extends _StdSink implements IOSink { | 156 class Stdout extends _StdSink implements IOSink { |
166 final int _fd; | 157 Stdout._(IOSink sink) : super(sink); |
167 IOSink _nonBlocking; | |
168 | |
169 Stdout._(IOSink sink, this._fd) : super(sink); | |
170 | 158 |
171 /** | 159 /** |
172 * Returns true if there is a terminal attached to stdout. | 160 * Returns true if there is a terminal attached to stdout. |
173 */ | 161 */ |
174 bool get hasTerminal => _hasTerminal(_fd); | 162 external bool get hasTerminal; |
175 | 163 |
176 /** | 164 /** |
177 * Get the number of columns of the terminal. | 165 * Get the number of columns of the terminal. |
178 * | 166 * |
179 * If no terminal is attached to stdout, a [StdoutException] is thrown. See | 167 * If no terminal is attached to stdout, a [StdoutException] is thrown. See |
180 * [hasTerminal] for more info. | 168 * [hasTerminal] for more info. |
181 */ | 169 */ |
182 int get terminalColumns => _terminalColumns(_fd); | 170 external int get terminalColumns; |
183 | 171 |
184 /** | 172 /** |
185 * Get the number of lines of the terminal. | 173 * Get the number of lines of the terminal. |
186 * | 174 * |
187 * If no terminal is attached to stdout, a [StdoutException] is thrown. See | 175 * If no terminal is attached to stdout, a [StdoutException] is thrown. See |
188 * [hasTerminal] for more info. | 176 * [hasTerminal] for more info. |
189 */ | 177 */ |
190 int get terminalLines => _terminalLines(_fd); | 178 external int get terminalLines; |
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 } | |
205 } | 179 } |
206 | 180 |
207 | 181 |
208 class StdoutException implements IOException { | 182 class StdoutException implements IOException { |
209 final String message; | 183 final String message; |
210 final OSError osError; | 184 final OSError osError; |
211 | 185 |
212 const StdoutException(this.message, [this.osError]); | 186 const StdoutException(this.message, [this.osError]); |
213 | 187 |
214 String toString() { | 188 String toString() { |
215 return "StdoutException: $message${osError == null ? "" : ", $osError"}"; | 189 return "StdoutException: $message${osError == null ? "" : ", $osError"}"; |
216 } | 190 } |
217 } | 191 } |
218 | 192 |
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 } | |
247 | 193 |
248 class _StdSink implements IOSink { | 194 class _StdSink implements IOSink { |
249 final IOSink _sink; | 195 final IOSink _sink; |
250 | 196 |
251 _StdSink(this._sink); | 197 _StdSink(this._sink); |
252 | 198 |
253 Encoding get encoding => _sink.encoding; | 199 Encoding get encoding => _sink.encoding; |
254 void set encoding(Encoding encoding) { | 200 void set encoding(Encoding encoding) { |
255 _sink.encoding = encoding; | 201 _sink.encoding = encoding; |
256 } | 202 } |
(...skipping 17 matching lines...) Expand all Loading... |
274 static const StdioType FILE = const StdioType._("file"); | 220 static const StdioType FILE = const StdioType._("file"); |
275 static const StdioType OTHER = const StdioType._("other"); | 221 static const StdioType OTHER = const StdioType._("other"); |
276 final String name; | 222 final String name; |
277 const StdioType._(this.name); | 223 const StdioType._(this.name); |
278 String toString() => "StdioType: $name"; | 224 String toString() => "StdioType: $name"; |
279 } | 225 } |
280 | 226 |
281 | 227 |
282 Stdin _stdin; | 228 Stdin _stdin; |
283 Stdout _stdout; | 229 Stdout _stdout; |
284 Stdout _stderr; | 230 IOSink _stderr; |
285 | 231 |
286 | 232 |
287 /// The standard input stream of data read by this program. | 233 /// The standard input stream of data read by this program. |
288 Stdin get stdin { | 234 Stdin get stdin { |
289 if (_stdin == null) { | 235 if (_stdin == null) { |
290 _stdin = _StdIOUtils._getStdioInputStream(); | 236 _stdin = _StdIOUtils._getStdioInputStream(); |
291 } | 237 } |
292 return _stdin; | 238 return _stdin; |
293 } | 239 } |
294 | 240 |
295 | 241 |
296 /// The standard output stream of data written by this program. | 242 /// The standard output stream of data written by this program. |
297 Stdout get stdout { | 243 Stdout get stdout { |
298 if (_stdout == null) { | 244 if (_stdout == null) { |
299 _stdout = _StdIOUtils._getStdioOutputStream(1); | 245 _stdout = _StdIOUtils._getStdioOutputStream(1); |
300 } | 246 } |
301 return _stdout; | 247 return _stdout; |
302 } | 248 } |
303 | 249 |
304 | 250 |
305 /// The standard output stream of errors written by this program. | 251 /// The standard output stream of errors written by this program. |
306 Stdout get stderr { | 252 IOSink get stderr { |
307 if (_stderr == null) { | 253 if (_stderr == null) { |
308 _stderr = _StdIOUtils._getStdioOutputStream(2); | 254 _stderr = _StdIOUtils._getStdioOutputStream(2); |
309 } | 255 } |
310 return _stderr; | 256 return _stderr; |
311 } | 257 } |
312 | 258 |
313 | 259 |
314 /// For a stream, returns whether it is attached to a file, pipe, terminal, or | 260 /// For a stream, returns whether it is attached to a file, pipe, terminal, or |
315 /// something else. | 261 /// something else. |
316 StdioType stdioType(object) { | 262 StdioType stdioType(object) { |
(...skipping 28 matching lines...) Expand all Loading... |
345 return StdioType.OTHER; | 291 return StdioType.OTHER; |
346 } | 292 } |
347 | 293 |
348 | 294 |
349 class _StdIOUtils { | 295 class _StdIOUtils { |
350 external static _getStdioOutputStream(int fd); | 296 external static _getStdioOutputStream(int fd); |
351 external static Stdin _getStdioInputStream(); | 297 external static Stdin _getStdioInputStream(); |
352 external static int _socketType(nativeSocket); | 298 external static int _socketType(nativeSocket); |
353 external static _getStdioHandleType(int fd); | 299 external static _getStdioHandleType(int fd); |
354 } | 300 } |
OLD | NEW |