| 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 |
| 11 // Abstract factory class capable of producing interactive and | 11 class _Process extends Process { |
| 12 // non-interactive processes. | 12 static Process start(String path, |
| 13 class _Process { | 13 List<String> arguments, |
| 14 | 14 [ProcessOptions options]) { |
| 15 factory Process.start(String path, | 15 return new _Process.start(path, arguments, options); |
| 16 List<String> arguments, | |
| 17 [ProcessOptions options]) { | |
| 18 return new _InteractiveProcess.start(path, arguments, options); | |
| 19 } | 16 } |
| 20 | 17 |
| 21 factory Process.run(String path, | 18 static Future<ProcessResult> run(String path, |
| 22 List<String> arguments, | 19 List<String> arguments, |
| 23 ProcessOptions options, | 20 [ProcessOptions options]) { |
| 24 void callback(int exitCode, | 21 return new _NonInteractiveProcess._start(path, arguments, options)._result; |
| 25 String stdout, | |
| 26 String stderr)) { | |
| 27 return new _NonInteractiveProcess.start(path, | |
| 28 arguments, | |
| 29 options, | |
| 30 callback); | |
| 31 } | 22 } |
| 32 } | |
| 33 | 23 |
| 34 | 24 _Process.start(String path, |
| 35 // _InteractiveProcess is the actual implementation of all processes | 25 List<String> arguments, |
| 36 // started from Dart code. | 26 ProcessOptions options) { |
| 37 class _InteractiveProcess implements Process { | |
| 38 | |
| 39 _InteractiveProcess.start(String path, | |
| 40 List<String> arguments, | |
| 41 ProcessOptions options) { | |
| 42 if (path is !String) { | 27 if (path is !String) { |
| 43 throw new IllegalArgumentException("Path is not a String: $path"); | 28 throw new IllegalArgumentException("Path is not a String: $path"); |
| 44 } | 29 } |
| 45 _path = path; | 30 _path = path; |
| 46 | 31 |
| 47 if (arguments is !List) { | 32 if (arguments is !List) { |
| 48 throw new IllegalArgumentException("Arguments is not a List: $arguments"); | 33 throw new IllegalArgumentException("Arguments is not a List: $arguments"); |
| 49 } | 34 } |
| 50 int len = arguments.length; | 35 int len = arguments.length; |
| 51 _arguments = new ObjectArray<String>(len); | 36 _arguments = new ObjectArray<String>(len); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 _in = new _Socket._internalReadOnly(); // stdout coming from process. | 75 _in = new _Socket._internalReadOnly(); // stdout coming from process. |
| 91 _out = new _Socket._internalWriteOnly(); // stdin going to process. | 76 _out = new _Socket._internalWriteOnly(); // stdin going to process. |
| 92 _err = new _Socket._internalReadOnly(); // stderr coming from process. | 77 _err = new _Socket._internalReadOnly(); // stderr coming from process. |
| 93 _exitHandler = new _Socket._internalReadOnly(); | 78 _exitHandler = new _Socket._internalReadOnly(); |
| 94 _closed = false; | 79 _closed = false; |
| 95 _killed = false; | 80 _killed = false; |
| 96 _started = false; | 81 _started = false; |
| 97 _onExit = null; | 82 _onExit = null; |
| 98 // TODO(ager): Make the actual process starting really async instead of | 83 // TODO(ager): Make the actual process starting really async instead of |
| 99 // simulating it with a timer. | 84 // simulating it with a timer. |
| 100 new Timer(0, (Timer ignore) => start()); | 85 new Timer(0, (Timer ignore) => _start()); |
| 101 } | 86 } |
| 102 | 87 |
| 103 String _windowsArgumentEscape(String argument) { | 88 String _windowsArgumentEscape(String argument) { |
| 104 var result = argument; | 89 var result = argument; |
| 105 if (argument.contains('\t') || argument.contains(' ')) { | 90 if (argument.contains('\t') || argument.contains(' ')) { |
| 106 // Produce something that the C runtime on Windows will parse | 91 // Produce something that the C runtime on Windows will parse |
| 107 // back as this string. | 92 // back as this string. |
| 108 | 93 |
| 109 // Replace any number of '\' followed by '"' with | 94 // Replace any number of '\' followed by '"' with |
| 110 // twice as many '\' followed by '\"'. | 95 // twice as many '\' followed by '\"'. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 return result; | 131 return result; |
| 147 } | 132 } |
| 148 | 133 |
| 149 int _intFromBytes(List<int> bytes, int offset) { | 134 int _intFromBytes(List<int> bytes, int offset) { |
| 150 return (bytes[offset] + | 135 return (bytes[offset] + |
| 151 (bytes[offset + 1] << 8) + | 136 (bytes[offset + 1] << 8) + |
| 152 (bytes[offset + 2] << 16) + | 137 (bytes[offset + 2] << 16) + |
| 153 (bytes[offset + 3] << 24)); | 138 (bytes[offset + 3] << 24)); |
| 154 } | 139 } |
| 155 | 140 |
| 156 void start() { | 141 void _start() { |
| 157 var status = new _ProcessStartStatus(); | 142 var status = new _ProcessStartStatus(); |
| 158 bool success = _start(_path, | 143 bool success = _startNative(_path, |
| 159 _arguments, | 144 _arguments, |
| 160 _workingDirectory, | 145 _workingDirectory, |
| 161 _environment, | 146 _environment, |
| 162 _in, | 147 _in, |
| 163 _out, | 148 _out, |
| 164 _err, | 149 _err, |
| 165 _exitHandler, | 150 _exitHandler, |
| 166 status); | 151 status); |
| 167 if (!success) { | 152 if (!success) { |
| 168 close(); | 153 close(); |
| 169 _reportError(new ProcessException(status._errorMessage, status._errorCode)
); | 154 _reportError(new ProcessException(status._errorMessage, |
| 155 status._errorCode)); |
| 170 return; | 156 return; |
| 171 } | 157 } |
| 172 _started = true; | 158 _started = true; |
| 173 | 159 |
| 174 // Make sure to activate socket handlers now that the file | 160 // Make sure to activate socket handlers now that the file |
| 175 // descriptors have been set. | 161 // descriptors have been set. |
| 176 _in._activateHandlers(); | 162 _in._activateHandlers(); |
| 177 _out._activateHandlers(); | 163 _out._activateHandlers(); |
| 178 _err._activateHandlers(); | 164 _err._activateHandlers(); |
| 179 | 165 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 200 exitDataRead += _exitHandler.inputStream.readInto( | 186 exitDataRead += _exitHandler.inputStream.readInto( |
| 201 exitDataBuffer, exitDataRead, EXIT_DATA_SIZE - exitDataRead); | 187 exitDataBuffer, exitDataRead, EXIT_DATA_SIZE - exitDataRead); |
| 202 if (exitDataRead == EXIT_DATA_SIZE) handleExit(); | 188 if (exitDataRead == EXIT_DATA_SIZE) handleExit(); |
| 203 }; | 189 }; |
| 204 | 190 |
| 205 if (_onStart !== null) { | 191 if (_onStart !== null) { |
| 206 _onStart(); | 192 _onStart(); |
| 207 } | 193 } |
| 208 } | 194 } |
| 209 | 195 |
| 210 bool _start(String path, | 196 bool _startNative(String path, |
| 211 List<String> arguments, | 197 List<String> arguments, |
| 212 String workingDirectory, | 198 String workingDirectory, |
| 213 List<String> environment, | 199 List<String> environment, |
| 214 Socket input, | 200 Socket input, |
| 215 Socket output, | 201 Socket output, |
| 216 Socket error, | 202 Socket error, |
| 217 Socket exitHandler, | 203 Socket exitHandler, |
| 218 _ProcessStartStatus status) native "Process_Start"; | 204 _ProcessStartStatus status) native "Process_Start"; |
| 219 | 205 |
| 220 InputStream get stdout() { | 206 InputStream get stdout() { |
| 221 if (_closed) { | 207 if (_closed) { |
| 222 throw new ProcessException("Process closed"); | 208 throw new ProcessException("Process closed"); |
| 223 } | 209 } |
| 224 return _in.inputStream; | 210 return _in.inputStream; |
| 225 } | 211 } |
| 226 | 212 |
| 227 InputStream get stderr() { | 213 InputStream get stderr() { |
| 228 if (_closed) { | 214 if (_closed) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 bool _closed; | 293 bool _closed; |
| 308 bool _killed; | 294 bool _killed; |
| 309 bool _started; | 295 bool _started; |
| 310 Function _onExit; | 296 Function _onExit; |
| 311 Function _onError; | 297 Function _onError; |
| 312 Function _onStart; | 298 Function _onStart; |
| 313 } | 299 } |
| 314 | 300 |
| 315 | 301 |
| 316 // _NonInteractiveProcess is a wrapper around an interactive process | 302 // _NonInteractiveProcess is a wrapper around an interactive process |
| 317 // that restricts the interface to disallow access to the streams and | 303 // that buffers output so it can be delivered when the process exits. |
| 318 // buffers output so it can be delivered to the callback when the | 304 // _NonInteractiveProcess is used to implement the Process.run |
| 319 // process exits. | 305 // method. |
| 320 class _NonInteractiveProcess implements Process { | 306 class _NonInteractiveProcess { |
| 321 _NonInteractiveProcess.start(String path, | 307 _NonInteractiveProcess._start(String path, |
| 322 List<String> arguments, | 308 List<String> arguments, |
| 323 ProcessOptions options, | 309 ProcessOptions options) { |
| 324 Function this._callback) { | 310 _completer = new Completer<ProcessResult>(); |
| 325 // Extract output encoding options and verify arguments. | 311 // Extract output encoding options and verify arguments. |
| 326 var stdoutEncoding = Encoding.UTF_8; | 312 var stdoutEncoding = Encoding.UTF_8; |
| 327 var stderrEncoding = Encoding.UTF_8; | 313 var stderrEncoding = Encoding.UTF_8; |
| 328 if (options !== null) { | 314 if (options !== null) { |
| 329 if (options.stdoutEncoding !== null) { | 315 if (options.stdoutEncoding !== null) { |
| 330 stdoutEncoding = options.stdoutEncoding; | 316 stdoutEncoding = options.stdoutEncoding; |
| 331 if (stdoutEncoding is !Encoding) { | 317 if (stdoutEncoding is !Encoding) { |
| 332 throw new IllegalArgumentException( | 318 throw new IllegalArgumentException( |
| 333 'stdoutEncoding option is not an encoding: $stdoutEncoding'); | 319 'stdoutEncoding option is not an encoding: $stdoutEncoding'); |
| 334 } | 320 } |
| 335 } | 321 } |
| 336 if (options.stderrEncoding !== null) { | 322 if (options.stderrEncoding !== null) { |
| 337 stderrEncoding = options.stderrEncoding; | 323 stderrEncoding = options.stderrEncoding; |
| 338 if (stderrEncoding is !Encoding) { | 324 if (stderrEncoding is !Encoding) { |
| 339 throw new IllegalArgumentException( | 325 throw new IllegalArgumentException( |
| 340 'stderrEncoding option is not an encoding: $stderrEncoding'); | 326 'stderrEncoding option is not an encoding: $stderrEncoding'); |
| 341 } | 327 } |
| 342 } | 328 } |
| 343 } | 329 } |
| 344 | 330 |
| 345 // Start the underlying process. | 331 // Start the underlying process. |
| 346 _process = new _InteractiveProcess.start(path, arguments, options); | 332 _process = new _Process.start(path, arguments, options); |
| 333 |
| 334 // Setup process error handling. |
| 335 _process.onError = (e) => _completer.completeException(e); |
| 347 | 336 |
| 348 // Setup process exit handling. | 337 // Setup process exit handling. |
| 349 _process.onExit = (exitCode) { | 338 _process.onExit = (exitCode) { |
| 350 _exitCode = exitCode; | 339 _exitCode = exitCode; |
| 351 _checkDone(); | 340 _checkDone(); |
| 352 }; | 341 }; |
| 353 | 342 |
| 354 // Setup stdout handling. | 343 // Setup stdout handling. |
| 355 _stdoutBuffer = new StringBuffer(); | 344 _stdoutBuffer = new StringBuffer(); |
| 356 var stdoutStream = new StringInputStream(_process.stdout, stdoutEncoding); | 345 var stdoutStream = new StringInputStream(_process.stdout, stdoutEncoding); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 371 if (data != null) _stderrBuffer.add(data); | 360 if (data != null) _stderrBuffer.add(data); |
| 372 }; | 361 }; |
| 373 stderrStream.onClosed = () { | 362 stderrStream.onClosed = () { |
| 374 _stderrClosed = true; | 363 _stderrClosed = true; |
| 375 _checkDone(); | 364 _checkDone(); |
| 376 }; | 365 }; |
| 377 } | 366 } |
| 378 | 367 |
| 379 void _checkDone() { | 368 void _checkDone() { |
| 380 if (_exitCode != null && _stderrClosed && _stdoutClosed) { | 369 if (_exitCode != null && _stderrClosed && _stdoutClosed) { |
| 381 _callback(_exitCode, _stdoutBuffer.toString(), _stderrBuffer.toString()); | 370 _completer.complete(new _ProcessResult(_exitCode, |
| 371 _stdoutBuffer.toString(), |
| 372 _stderrBuffer.toString())); |
| 382 } | 373 } |
| 383 } | 374 } |
| 384 | 375 |
| 385 InputStream get stdout() { | 376 Future<ProcessResult> get _result() => _completer.future; |
| 386 throw new UnsupportedOperationException( | |
| 387 'Cannot get stdout stream for process started with ' | |
| 388 'the run constructor. The entire stdout ' | |
| 389 'will be supplied in the callback on completion.'); | |
| 390 } | |
| 391 | 377 |
| 392 InputStream get stderr() { | 378 Completer<ProcessResult> _completer; |
| 393 throw new UnsupportedOperationException( | |
| 394 'Cannot get stderr stream for process started with ' | |
| 395 'the run constructor. The entire stderr ' | |
| 396 'will be supplied in the callback on completion.'); | |
| 397 } | |
| 398 | |
| 399 OutputStream get stdin() { | |
| 400 throw new UnsupportedOperationException( | |
| 401 'Cannot communicate via stdin with process started with ' | |
| 402 'the run constructor'); | |
| 403 } | |
| 404 | |
| 405 void set onStart(void callback()) => _process.onStart = callback; | |
| 406 | |
| 407 void set onExit(void callback(int exitCode)) { | |
| 408 throw new UnsupportedOperationException( | |
| 409 'Cannot set exit handler on process started with ' | |
| 410 'the run constructor. The exit code will ' | |
| 411 'be supplied in the callback on completion.'); | |
| 412 } | |
| 413 | |
| 414 void set onError(void callback(e)) { | |
| 415 _process.onError = callback; | |
| 416 } | |
| 417 | |
| 418 void kill() => _process.kill(); | |
| 419 | |
| 420 void close() => _process.close(); | |
| 421 | |
| 422 Process _process; | 379 Process _process; |
| 423 Function _callback; | |
| 424 StringBuffer _stdoutBuffer; | 380 StringBuffer _stdoutBuffer; |
| 425 StringBuffer _stderrBuffer; | 381 StringBuffer _stderrBuffer; |
| 426 int _exitCode; | 382 int _exitCode; |
| 427 bool _stdoutClosed = false; | 383 bool _stdoutClosed = false; |
| 428 bool _stderrClosed = false; | 384 bool _stderrClosed = false; |
| 429 } | 385 } |
| 386 |
| 387 |
| 388 class _ProcessResult implements ProcessResult { |
| 389 const _ProcessResult(int this.exitCode, |
| 390 String this.stdout, |
| 391 String this.stderr); |
| 392 |
| 393 final int exitCode; |
| 394 final String stdout; |
| 395 final String stderr; |
| 396 } |
| OLD | NEW |