| 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 // TODO(ager): The only reason for this class is that we | 7 // TODO(ager): The only reason for this class is that we |
| 8 // cannot patch a top-level at this point. | 8 // cannot patch a top-level at this point. |
| 9 class _ProcessUtils { | 9 class _ProcessUtils { |
| 10 external static void _exit(int status); | 10 external static void _exit(int status); |
| 11 external static void _setExitCode(int status); | 11 external static void _setExitCode(int status); |
| 12 external static void _sleep(int millis); | 12 external static void _sleep(int millis); |
| 13 external static int _pid(Process process); | 13 external static int _pid(Process process); |
| 14 } | 14 } |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Exit the Dart VM process immediately with the given [status] code. | 17 * Exit the Dart VM process immediately with the given exit code. |
| 18 * | 18 * |
| 19 * This does not wait for any asynchronous operations to terminate. Using | 19 * This does not wait for any asynchronous operations to terminate. Using |
| 20 * [exit] is therefore very likely to lose data. | 20 * [exit] is therefore very likely to lose data. |
| 21 * |
| 22 * The handling of exit codes is platform specific. |
| 23 * |
| 24 * On Linux and Mac OS an exit code for normal termination will always |
| 25 * be in the range [0..255]. If an exit code outside this range is |
| 26 * set the actual exit code will be the lower 8 bits masked off and |
| 27 * treated as an unsigned value. E.g. using an exit code of -1 will |
| 28 * result in an actual exit code of 255 being reported. |
| 29 * |
| 30 * On Windows the exit code can be set to any 32-bit value. However |
| 31 * some of these values are reserved for reporting system errors like |
| 32 * crashes. |
| 33 * |
| 34 * Besides this the Dart executable itself uses an exit code of `254` |
| 35 * for reporting compile time errors and an exit code of `255` for |
| 36 * reporting runtime error (unhandled exception). |
| 37 * |
| 38 * Due to these facts it is recommended to only use exit codes in the |
| 39 * range [0..127] for communicating the result of running a Dart |
| 40 * program to the surrounding environment. This will avoid any |
| 41 * cross-platform issues. |
| 21 */ | 42 */ |
| 22 void exit(int status) { | 43 void exit(int code) { |
| 23 if (status is !int) { | 44 if (status is !int) { |
| 24 throw new ArgumentError("exit: int status expected"); | 45 throw new ArgumentError("exit: int status expected"); |
| 25 } | 46 } |
| 26 _ProcessUtils._exit(status); | 47 _ProcessUtils._exit(status); |
| 27 } | 48 } |
| 28 | 49 |
| 29 /** | 50 /** |
| 30 * Global exit code for the Dart VM. | 51 * Global exit code for the Dart VM. |
| 31 * | 52 * |
| 32 * The exit code is global for the Dart VM and the last assignment to | 53 * The exit code is global for the Dart VM and the last assignment to |
| 33 * exitCode from any isolate determines the exit code of the Dart VM | 54 * exitCode from any isolate determines the exit code of the Dart VM |
| 34 * on normal termination. | 55 * on normal termination. |
| 56 * |
| 57 * See [exit] for more information on how to chose a value for the |
| 58 * exit code. |
| 35 */ | 59 */ |
| 36 set exitCode(int status) { | 60 set exitCode(int code) { |
| 37 if (status is !int) { | 61 if (status is !int) { |
| 38 throw new ArgumentError("setExitCode: int status expected"); | 62 throw new ArgumentError("setExitCode: int status expected"); |
| 39 } | 63 } |
| 40 _ProcessUtils._setExitCode(status); | 64 _ProcessUtils._setExitCode(status); |
| 41 } | 65 } |
| 42 | 66 |
| 43 /** | 67 /** |
| 44 * Sleep for the duration specified in [duration]. | 68 * Sleep for the duration specified in [duration]. |
| 45 * | 69 * |
| 46 * Use this with care, as no asynchronous operations can be processed | 70 * Use this with care, as no asynchronous operations can be processed |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 List<String> arguments, | 202 List<String> arguments, |
| 179 {String workingDirectory, | 203 {String workingDirectory, |
| 180 Map<String, String> environment, | 204 Map<String, String> environment, |
| 181 bool includeParentEnvironment: true, | 205 bool includeParentEnvironment: true, |
| 182 bool runInShell: false, | 206 bool runInShell: false, |
| 183 Encoding stdoutEncoding: SYSTEM_ENCODING, | 207 Encoding stdoutEncoding: SYSTEM_ENCODING, |
| 184 Encoding stderrEncoding: SYSTEM_ENCODING}); | 208 Encoding stderrEncoding: SYSTEM_ENCODING}); |
| 185 | 209 |
| 186 /** | 210 /** |
| 187 * Returns the standard output stream of the process as a [:Stream:]. | 211 * Returns the standard output stream of the process as a [:Stream:]. |
| 188 * | |
| 189 * Throws an [UnsupportedError] if the process is | |
| 190 * non-interactive. | |
| 191 */ | 212 */ |
| 192 Stream<List<int>> get stdout; | 213 Stream<List<int>> get stdout; |
| 193 | 214 |
| 194 /** | 215 /** |
| 195 * Returns the standard error stream of the process as a [:Stream:]. | 216 * Returns the standard error stream of the process as a [:Stream:]. |
| 196 * | |
| 197 * Throws an [UnsupportedError] if the process is | |
| 198 * non-interactive. | |
| 199 */ | 217 */ |
| 200 Stream<List<int>> get stderr; | 218 Stream<List<int>> get stderr; |
| 201 | 219 |
| 202 /** | 220 /** |
| 203 * Returns the standard input stream of the process as an [IOSink]. | 221 * Returns the standard input stream of the process as an [IOSink]. |
| 204 * | |
| 205 * Throws an [UnsupportedError] if the process is | |
| 206 * non-interactive. | |
| 207 */ | 222 */ |
| 208 IOSink get stdin; | 223 IOSink get stdin; |
| 209 | 224 |
| 210 /** | 225 /** |
| 211 * Returns the process id of the process. | 226 * Returns the process id of the process. |
| 212 */ | 227 */ |
| 213 int get pid; | 228 int get pid; |
| 214 | 229 |
| 215 /** | 230 /** |
| 216 * Returns a [:Future:] which completes with the exit code of the process | 231 * Returns a [:Future:] which completes with the exit code of the process |
| 217 * when the process completes. | 232 * when the process completes. |
| 218 * | 233 * |
| 219 * Throws an [UnsupportedError] if the process is | 234 * The handling of exit codes is platform specific. |
| 220 * non-interactive. | 235 * |
| 236 * On Linux and Mac a normal exit code will be a positive value in |
| 237 * the range [0..255]. If the process was terminated due to a signal |
| 238 * the exit code will be a negative value in the range [-255..0[, |
| 239 * where the absolute value of the exit code is the signal |
| 240 * number. For example, if a process crashes due to a segmentation |
| 241 * violation the exit code will be -11, as the signal SIGSEGV has the |
| 242 * number 11. |
| 243 * |
| 244 * On Windows a process can report any 32-bit value as an exit |
| 245 * code. When returning the exit code this exit code is turned into |
| 246 * a signed value. Some special values are used to report |
| 247 * termination due to some system event. E.g. if a process crashes |
| 248 * due to an access violation the 32-bit exit code is `0xc0000005`, |
| 249 * which will be returned as the negative number `-1073741819`. To |
| 250 * get the original 32-bit value use `(0x100000000 + exitCode) & |
| 251 * 0xffffffff`. |
| 221 */ | 252 */ |
| 222 Future<int> exitCode; | 253 Future<int> exitCode; |
| 223 | 254 |
| 224 /** | 255 /** |
| 225 * On Windows, [kill] kills the process, ignoring the [signal] | 256 * On Windows, [kill] kills the process, ignoring the [signal] |
| 226 * flag. On Posix systems, [kill] sends [signal] to the | 257 * flag. On Posix systems, [kill] sends [signal] to the |
| 227 * process. Depending on the signal giving, it'll have different | 258 * process. Depending on the signal giving, it'll have different |
| 228 * meanings. When the process terminates as a result of calling | 259 * meanings. When the process terminates as a result of calling |
| 229 * [kill] [onExit] is called. | 260 * [kill] [onExit] is called. |
| 230 * | 261 * |
| 231 * Returns [:true:] if the process is successfully killed (the | 262 * Returns [:true:] if the process is successfully killed (the |
| 232 * signal is successfully sent). Returns [:false:] if the process | 263 * signal is successfully sent). Returns [:false:] if the process |
| 233 * could not be killed (the signal could not be sent). Usually, | 264 * could not be killed (the signal could not be sent). Usually, |
| 234 * a [:false:] return value from kill means that the process is | 265 * a [:false:] return value from kill means that the process is |
| 235 * already dead. | 266 * already dead. |
| 236 */ | 267 */ |
| 237 bool kill([ProcessSignal signal = ProcessSignal.SIGTERM]); | 268 bool kill([ProcessSignal signal = ProcessSignal.SIGTERM]); |
| 238 } | 269 } |
| 239 | 270 |
| 240 | 271 |
| 241 /** | 272 /** |
| 242 * [ProcessResult] represents the result of running a non-interactive | 273 * [ProcessResult] represents the result of running a non-interactive |
| 243 * process started with [:Process.run:]. | 274 * process started with [:Process.run:]. |
| 244 */ | 275 */ |
| 245 abstract class ProcessResult { | 276 abstract class ProcessResult { |
| 246 /** | 277 /** |
| 247 * Exit code for the process. | 278 * Exit code for the process. |
| 279 * |
| 280 * See [Process.exitCode] for more information in the exit code |
| 281 * value. |
| 248 */ | 282 */ |
| 249 int get exitCode; | 283 int get exitCode; |
| 250 | 284 |
| 251 /** | 285 /** |
| 252 * Standard output from the process. The value used for the | 286 * Standard output from the process. The value used for the |
| 253 * `stdoutEncoding` argument to `Process.run` determins the type. If | 287 * `stdoutEncoding` argument to `Process.run` determins the type. If |
| 254 * `null` was used this value is of type `List<int> otherwise it is | 288 * `null` was used this value is of type `List<int> otherwise it is |
| 255 * of type `String`. | 289 * of type `String`. |
| 256 */ | 290 */ |
| 257 get stdout; | 291 get stdout; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 /** | 368 /** |
| 335 * Contains the system message for the process exception if any. | 369 * Contains the system message for the process exception if any. |
| 336 */ | 370 */ |
| 337 final String message; | 371 final String message; |
| 338 | 372 |
| 339 /** | 373 /** |
| 340 * Contains the OS error code for the process exception if any. | 374 * Contains the OS error code for the process exception if any. |
| 341 */ | 375 */ |
| 342 final int errorCode; | 376 final int errorCode; |
| 343 } | 377 } |
| OLD | NEW |