OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /** Exit the Dart VM process with the given [status] code. */ | |
6 void exit(int status) { | |
7 if (status is !int) { | |
8 throw new ArgumentError("int status expected"); | |
9 } | |
10 _exit(status); | |
11 } | |
12 | |
13 /** | |
14 * [Process] is used to start new processes using the static | |
15 * [start] and [run] methods. | |
16 */ | |
17 class Process { | |
18 /** | |
19 * Starts a process running the [executable] with the specified | |
20 * [arguments]. Returns a [:Future<Process>:] that completes with a | |
21 * Process instance when the process has been successfully | |
22 * started. That [Process] object can be used to interact with the | |
23 * process. If the process cannot be started the returned [Future] | |
24 * completes with an exception. | |
25 * | |
26 * An optional [ProcessOptions] object can be passed to specify | |
27 * options other than the executable and the arguments. | |
28 * | |
29 * Users must read all data coming on the [stdout] and [stderr] | |
30 * streams of processes started with [:Process.start:]. If the user | |
31 * does not read all data on the streams the underlying system | |
32 * resources will not be freed since there is still pending data. | |
33 */ | |
34 static Future<Process> start(String executable, | |
35 List<String> arguments, | |
36 [ProcessOptions options]) { | |
37 return _Process.start(executable, arguments, options); | |
38 } | |
39 | |
40 /** | |
41 * Starts a process and runs it non-interactively to completion. The | |
42 * process run is [executable] with the specified [arguments]. | |
43 * | |
44 * An optional [ProcessOptions] object can be passed to specify | |
45 * options other than the executable and the arguments. | |
46 * | |
47 * Returns a [:Future<ProcessResult>:] that completes with the | |
48 * result of running the process, i.e., exit code, standard out and | |
49 * standard in. | |
50 */ | |
51 static Future<ProcessResult> run(String executable, | |
52 List<String> arguments, | |
53 [ProcessOptions options]) { | |
54 return _Process.run(executable, arguments, options); | |
55 } | |
56 | |
57 /** | |
58 * Returns an input stream of the process stdout. | |
59 * | |
60 * Throws an [UnsupportedError] if the process is | |
61 * non-interactive. | |
62 */ | |
63 abstract InputStream get stdout; | |
64 | |
65 /** | |
66 * Returns an input stream of the process stderr. | |
67 * | |
68 * Throws an [UnsupportedError] if the process is | |
69 * non-interactive. | |
70 */ | |
71 abstract InputStream get stderr; | |
72 | |
73 /** | |
74 * Returns an output stream to the process stdin. | |
75 * | |
76 * Throws an [UnsupportedError] if the process is | |
77 * non-interactive. | |
78 */ | |
79 abstract OutputStream get stdin; | |
80 | |
81 /** | |
82 * Sets an exit handler which gets invoked when the process | |
83 * terminates. | |
84 * | |
85 * Throws an [UnsupportedError] if the process is | |
86 * non-interactive. | |
87 */ | |
88 abstract void set onExit(void callback(int exitCode)); | |
89 | |
90 /** | |
91 * On Windows, [kill] kills the process, ignoring the [signal] | |
92 * flag. On Posix systems, [kill] sends [signal] to the | |
93 * process. Depending on the signal giving, it'll have different | |
94 * meanings. When the process terminates as a result of calling | |
95 * [kill] [onExit] is called. | |
96 * | |
97 * Returns [:true:] if the process is successfully killed (the | |
98 * signal is successfully sent). Returns [:false:] if the process | |
99 * could not be killed (the signal could not be sent). Usually, | |
100 * a [:false:] return value from kill means that the process is | |
101 * already dead. | |
102 */ | |
103 abstract bool kill([ProcessSignal signal = ProcessSignal.SIGTERM]); | |
104 } | |
105 | |
106 | |
107 /** | |
108 * [ProcessResult] represents the result of running a non-interactive | |
109 * process started with [:Process.run:]. | |
110 */ | |
111 abstract class ProcessResult { | |
112 /** | |
113 * Exit code for the process. | |
114 */ | |
115 int get exitCode; | |
116 | |
117 /** | |
118 * Standard output from the process as a string. | |
119 */ | |
120 String get stdout; | |
121 | |
122 /** | |
123 * Standard error from the process as a string. | |
124 */ | |
125 String get stderr; | |
126 } | |
127 | |
128 | |
129 /** | |
130 * [ProcessOptions] represents the options that can be supplied when | |
131 * starting a process. | |
132 */ | |
133 class ProcessOptions { | |
134 /** | |
135 * The working directory from which the process is started. Note | |
136 * that the change of directory occurs before executing the process | |
137 * on some platforms, which may have impact when using relative | |
138 * paths for the executable and the arguments. | |
139 */ | |
140 String workingDirectory; | |
141 | |
142 /** | |
143 * The encoding used for text on stdout when starting a | |
144 * non-interactive process with [:Process.run:]. | |
145 * | |
146 * This option is ignored for interactive processes started with | |
147 * [:Process.start:]. | |
148 * | |
149 * The default stdoutEncoding is UTF_8. | |
150 */ | |
151 Encoding stdoutEncoding; | |
152 | |
153 /** | |
154 * The encoding used for text on stderr when starting a | |
155 * non-interactive process with [:Process.run:]. | |
156 * | |
157 * This option is ignored for interactive processes started with | |
158 * [:Process.start:]. | |
159 * | |
160 * The default stderrEncoding is UTF_8. | |
161 */ | |
162 Encoding stderrEncoding; | |
163 | |
164 /** | |
165 * Provides the environment variables for the process. If not set | |
166 * the environment of the parent process is inherited. | |
167 * | |
168 * Currently, only ASCII environment variables are supported and | |
169 * errors are likely to occur if an environment variables with | |
170 * code-points outside the ASCII range is passed in. | |
171 */ | |
172 Map<String, String> environment; | |
173 } | |
174 | |
175 /** | |
176 * On Posix systems, [ProcessSignal] is used to send a specific signal | |
177 * to a child process, see [:Process.kill:]. | |
178 */ | |
179 class ProcessSignal { | |
180 static const ProcessSignal SIGHUP = const ProcessSignal._signal(1); | |
181 static const ProcessSignal SIGINT = const ProcessSignal._signal(2); | |
182 static const ProcessSignal SIGQUIT = const ProcessSignal._signal(3); | |
183 static const ProcessSignal SIGILL = const ProcessSignal._signal(4); | |
184 static const ProcessSignal SIGTRAP = const ProcessSignal._signal(5); | |
185 static const ProcessSignal SIGABRT = const ProcessSignal._signal(6); | |
186 static const ProcessSignal SIGBUS = const ProcessSignal._signal(7); | |
187 static const ProcessSignal SIGFPE = const ProcessSignal._signal(8); | |
188 static const ProcessSignal SIGKILL = const ProcessSignal._signal(9); | |
189 static const ProcessSignal SIGUSR1 = const ProcessSignal._signal(10); | |
190 static const ProcessSignal SIGSEGV = const ProcessSignal._signal(11); | |
191 static const ProcessSignal SIGUSR2 = const ProcessSignal._signal(12); | |
192 static const ProcessSignal SIGPIPE = const ProcessSignal._signal(13); | |
193 static const ProcessSignal SIGALRM = const ProcessSignal._signal(14); | |
194 static const ProcessSignal SIGTERM = const ProcessSignal._signal(15); | |
195 static const ProcessSignal SIGCHLD = const ProcessSignal._signal(17); | |
196 static const ProcessSignal SIGCONT = const ProcessSignal._signal(18); | |
197 static const ProcessSignal SIGSTOP = const ProcessSignal._signal(19); | |
198 static const ProcessSignal SIGTSTP = const ProcessSignal._signal(20); | |
199 static const ProcessSignal SIGTTIN = const ProcessSignal._signal(21); | |
200 static const ProcessSignal SIGTTOU = const ProcessSignal._signal(22); | |
201 static const ProcessSignal SIGURG = const ProcessSignal._signal(23); | |
202 static const ProcessSignal SIGXCPU = const ProcessSignal._signal(24); | |
203 static const ProcessSignal SIGXFSZ = const ProcessSignal._signal(25); | |
204 static const ProcessSignal SIGVTALRM = const ProcessSignal._signal(26); | |
205 static const ProcessSignal SIGPROF = const ProcessSignal._signal(27); | |
206 static const ProcessSignal SIGPOLL = const ProcessSignal._signal(29); | |
207 static const ProcessSignal SIGSYS = const ProcessSignal._signal(31); | |
208 | |
209 const ProcessSignal._signal(int this._signalNumber); | |
210 final int _signalNumber; | |
211 } | |
212 | |
213 | |
214 class ProcessException implements Exception { | |
215 const ProcessException([String this.message = "", int this.errorCode = 0]); | |
216 String toString() => "ProcessException: $message ($errorCode)"; | |
217 | |
218 /** | |
219 * Contains the system message for the process exception if any. | |
220 */ | |
221 final String message; | |
222 | |
223 /** | |
224 * Contains the OS error code for the process exception if any. | |
225 */ | |
226 final int errorCode; | |
227 } | |
OLD | NEW |