| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 const int _STDIO_HANDLE_TYPE_TERMINAL = 0; | 5 const int _STDIO_HANDLE_TYPE_TERMINAL = 0; |
| 6 const int _STDIO_HANDLE_TYPE_PIPE = 1; | 6 const int _STDIO_HANDLE_TYPE_PIPE = 1; |
| 7 const int _STDIO_HANDLE_TYPE_FILE = 2; | 7 const int _STDIO_HANDLE_TYPE_FILE = 2; |
| 8 const int _STDIO_HANDLE_TYPE_SOCKET = 3; | 8 const int _STDIO_HANDLE_TYPE_SOCKET = 3; |
| 9 const int _STDIO_HANDLE_TYPE_OTHER = -1; | 9 const int _STDIO_HANDLE_TYPE_OTHER = -1; |
| 10 | 10 |
| 11 | 11 |
| 12 InputStream _stdin; | 12 InputStream _stdin; |
| 13 OutputStream _stdout; | 13 OutputStream _stdout; |
| 14 OutputStream _stderr; | 14 OutputStream _stderr; |
| 15 | 15 |
| 16 | 16 |
| 17 InputStream _getStdioInputStream() { | |
| 18 switch (_StdIOUtils._getStdioHandleType(0)) { | |
| 19 case _STDIO_HANDLE_TYPE_TERMINAL: | |
| 20 case _STDIO_HANDLE_TYPE_PIPE: | |
| 21 case _STDIO_HANDLE_TYPE_SOCKET: | |
| 22 Socket s = new _Socket._internalReadOnly(); | |
| 23 _StdIOUtils._getStdioHandle(s, 0); | |
| 24 s._closed = false; | |
| 25 return s.inputStream; | |
| 26 case _STDIO_HANDLE_TYPE_FILE: | |
| 27 return new _FileInputStream.fromStdio(0); | |
| 28 default: | |
| 29 throw new FileIOException("Unsupported stdin type"); | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 | |
| 34 OutputStream _getStdioOutputStream(int fd) { | |
| 35 assert(fd == 1 || fd == 2); | |
| 36 switch (_StdIOUtils._getStdioHandleType(fd)) { | |
| 37 case _STDIO_HANDLE_TYPE_TERMINAL: | |
| 38 case _STDIO_HANDLE_TYPE_PIPE: | |
| 39 case _STDIO_HANDLE_TYPE_SOCKET: | |
| 40 Socket s = new _Socket._internalWriteOnly(); | |
| 41 _StdIOUtils._getStdioHandle(s, fd); | |
| 42 s._closed = false; | |
| 43 return s.outputStream; | |
| 44 case _STDIO_HANDLE_TYPE_FILE: | |
| 45 return new _FileOutputStream.fromStdio(fd); | |
| 46 default: | |
| 47 throw new FileIOException("Unsupported stdin type"); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 | |
| 52 InputStream get stdin { | 17 InputStream get stdin { |
| 53 if (_stdin == null) { | 18 if (_stdin == null) { |
| 54 _stdin = _getStdioInputStream(); | 19 _stdin = _StdIOUtils._getStdioInputStream(); |
| 55 } | 20 } |
| 56 return _stdin; | 21 return _stdin; |
| 57 } | 22 } |
| 58 | 23 |
| 59 | 24 |
| 60 OutputStream get stdout { | 25 OutputStream get stdout { |
| 61 if (_stdout == null) { | 26 if (_stdout == null) { |
| 62 _stdout = _getStdioOutputStream(1); | 27 _stdout = _StdIOUtils._getStdioOutputStream(1); |
| 63 } | 28 } |
| 64 return _stdout; | 29 return _stdout; |
| 65 } | 30 } |
| 66 | 31 |
| 67 | 32 |
| 68 OutputStream get stderr { | 33 OutputStream get stderr { |
| 69 if (_stderr == null) { | 34 if (_stderr == null) { |
| 70 _stderr = _getStdioOutputStream(2); | 35 _stderr = _StdIOUtils._getStdioOutputStream(2); |
| 71 } | 36 } |
| 72 return _stderr; | 37 return _stderr; |
| 73 } | 38 } |
| 74 | 39 |
| 75 | 40 |
| 76 class _StdIOUtils { | 41 class _StdIOUtils { |
| 77 external static _getStdioHandle(Socket socket, int num); | 42 external static OutputStream _getStdioOutputStream(int fd); |
| 78 external static _getStdioHandleType(int num); | 43 external static InputStream _getStdioInputStream(); |
| 79 } | 44 } |
| OLD | NEW |