| 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 class _ProcessStartStatus { | 5 class _ProcessStartStatus { |
| 6 int _errorCode; // Set to OS error code if process start failed. | 6 int _errorCode; // Set to OS error code if process start failed. |
| 7 String _errorMessage; // Set to OS error message if process start failed. | 7 String _errorMessage; // Set to OS error message if process start failed. |
| 8 } | 8 } |
| 9 | 9 |
| 10 | 10 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 } | 61 } |
| 62 | 62 |
| 63 if (options !== null && options.workingDirectory !== null) { | 63 if (options !== null && options.workingDirectory !== null) { |
| 64 _workingDirectory = options.workingDirectory; | 64 _workingDirectory = options.workingDirectory; |
| 65 if (_workingDirectory is !String) { | 65 if (_workingDirectory is !String) { |
| 66 throw new IllegalArgumentException( | 66 throw new IllegalArgumentException( |
| 67 "WorkingDirectory is not a String: $_workingDirectory"); | 67 "WorkingDirectory is not a String: $_workingDirectory"); |
| 68 } | 68 } |
| 69 } | 69 } |
| 70 | 70 |
| 71 if (options !== null && options.environment !== null) { | |
| 72 var env = options.environment; | |
| 73 if (env is !Map) { | |
| 74 throw new IllegalArgumentException("Environment is not a map: $env"); | |
| 75 } | |
| 76 _environment = []; | |
| 77 env.forEach((key, value) { | |
| 78 if (key is !String || value is !String) { | |
| 79 throw new IllegalArgumentException( | |
| 80 "Environment key or value is not a string: ($key, $value)"); | |
| 81 } | |
| 82 if (key.contains('=')) { | |
| 83 throw new IllegalArgumentException( | |
| 84 "Environment keys may not contain '=': $key"); | |
| 85 } | |
| 86 _environment.add('$key=$value'); | |
| 87 }); | |
| 88 } | |
| 89 | |
| 90 _in = new _Socket._internalReadOnly(); // stdout coming from process. | 71 _in = new _Socket._internalReadOnly(); // stdout coming from process. |
| 91 _out = new _Socket._internalWriteOnly(); // stdin going to process. | 72 _out = new _Socket._internalWriteOnly(); // stdin going to process. |
| 92 _err = new _Socket._internalReadOnly(); // stderr coming from process. | 73 _err = new _Socket._internalReadOnly(); // stderr coming from process. |
| 93 _exitHandler = new _Socket._internalReadOnly(); | 74 _exitHandler = new _Socket._internalReadOnly(); |
| 94 _closed = false; | 75 _closed = false; |
| 95 _killed = false; | 76 _killed = false; |
| 96 _started = false; | 77 _started = false; |
| 97 _onExit = null; | 78 _onExit = null; |
| 98 // TODO(ager): Make the actual process starting really async instead of | 79 // TODO(ager): Make the actual process starting really async instead of |
| 99 // simulating it with a timer. | 80 // simulating it with a timer. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 (bytes[offset + 1] << 8) + | 132 (bytes[offset + 1] << 8) + |
| 152 (bytes[offset + 2] << 16) + | 133 (bytes[offset + 2] << 16) + |
| 153 (bytes[offset + 3] << 24)); | 134 (bytes[offset + 3] << 24)); |
| 154 } | 135 } |
| 155 | 136 |
| 156 void start() { | 137 void start() { |
| 157 var status = new _ProcessStartStatus(); | 138 var status = new _ProcessStartStatus(); |
| 158 bool success = _start(_path, | 139 bool success = _start(_path, |
| 159 _arguments, | 140 _arguments, |
| 160 _workingDirectory, | 141 _workingDirectory, |
| 161 _environment, | |
| 162 _in, | 142 _in, |
| 163 _out, | 143 _out, |
| 164 _err, | 144 _err, |
| 165 _exitHandler, | 145 _exitHandler, |
| 166 status); | 146 status); |
| 167 if (!success) { | 147 if (!success) { |
| 168 close(); | 148 close(); |
| 169 if (_onError !== null) { | 149 if (_onError !== null) { |
| 170 _onError(new ProcessException(status._errorMessage, status._errorCode)); | 150 _onError(new ProcessException(status._errorMessage, status._errorCode)); |
| 171 return; | 151 return; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 }; | 185 }; |
| 206 | 186 |
| 207 if (_onStart !== null) { | 187 if (_onStart !== null) { |
| 208 _onStart(); | 188 _onStart(); |
| 209 } | 189 } |
| 210 } | 190 } |
| 211 | 191 |
| 212 bool _start(String path, | 192 bool _start(String path, |
| 213 List<String> arguments, | 193 List<String> arguments, |
| 214 String workingDirectory, | 194 String workingDirectory, |
| 215 List<String> environment, | |
| 216 Socket input, | 195 Socket input, |
| 217 Socket output, | 196 Socket output, |
| 218 Socket error, | 197 Socket error, |
| 219 Socket exitHandler, | 198 Socket exitHandler, |
| 220 _ProcessStartStatus status) native "Process_Start"; | 199 _ProcessStartStatus status) native "Process_Start"; |
| 221 | 200 |
| 222 InputStream get stdout() { | 201 InputStream get stdout() { |
| 223 if (_closed) { | 202 if (_closed) { |
| 224 throw new ProcessException("Process closed"); | 203 throw new ProcessException("Process closed"); |
| 225 } | 204 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 _onError = callback; | 267 _onError = callback; |
| 289 } | 268 } |
| 290 | 269 |
| 291 void set onStart(void callback()) { | 270 void set onStart(void callback()) { |
| 292 _onStart = callback; | 271 _onStart = callback; |
| 293 } | 272 } |
| 294 | 273 |
| 295 String _path; | 274 String _path; |
| 296 ObjectArray<String> _arguments; | 275 ObjectArray<String> _arguments; |
| 297 String _workingDirectory; | 276 String _workingDirectory; |
| 298 List<String> _environment; | |
| 299 // Private methods of _Socket are used by _in, _out, and _err. | 277 // Private methods of _Socket are used by _in, _out, and _err. |
| 300 _Socket _in; | 278 _Socket _in; |
| 301 _Socket _out; | 279 _Socket _out; |
| 302 _Socket _err; | 280 _Socket _err; |
| 303 Socket _exitHandler; | 281 Socket _exitHandler; |
| 304 int _pid; | 282 int _pid; |
| 305 bool _closed; | 283 bool _closed; |
| 306 bool _killed; | 284 bool _killed; |
| 307 bool _started; | 285 bool _started; |
| 308 Function _onExit; | 286 Function _onExit; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 void close() => _process.close(); | 396 void close() => _process.close(); |
| 419 | 397 |
| 420 Process _process; | 398 Process _process; |
| 421 Function _callback; | 399 Function _callback; |
| 422 StringBuffer _stdoutBuffer; | 400 StringBuffer _stdoutBuffer; |
| 423 StringBuffer _stderrBuffer; | 401 StringBuffer _stderrBuffer; |
| 424 int _exitCode; | 402 int _exitCode; |
| 425 bool _stdoutClosed = false; | 403 bool _stdoutClosed = false; |
| 426 bool _stderrClosed = false; | 404 bool _stderrClosed = false; |
| 427 } | 405 } |
| OLD | NEW |