]> gitweb.factorcode.org Git - factor.git/blob - basis/io/launcher/launcher-docs.factor
f20e65dc275a23b94e1ebc8f06dd6a0058c755d9
[factor.git] / basis / io / launcher / launcher-docs.factor
1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: help.markup help.syntax quotations kernel io io.files
4 math calendar ;
5 IN: io.launcher
6
7 ARTICLE: "io.launcher.command" "Specifying a command"
8 "The " { $snippet "command" } " slot of a " { $link process } " can contain either a string or a sequence of strings. In the first case, the string is processed in an operating system-specific manner. In the second case, the first element is a program name and the remaining elements are passed to the program as command-line arguments." ;
9
10 ARTICLE: "io.launcher.detached" "Running processes in the background"
11 "By default, " { $link run-process } " waits for the process to complete. To run a process without waiting for it to finish, set the " { $snippet "detached" } " slot of a " { $link process } ", or use the following word:"
12 { $subsection run-detached } ;
13
14 ARTICLE: "io.launcher.environment" "Setting environment variables"
15 "The " { $snippet "environment" } " slot of a " { $link process } " contains an association mapping environment variable names to values. The interpretation of environment variables is operating system-specific."
16 $nl
17 "The " { $snippet "environment-mode" } " slot controls how the environment of the current Factor instance is composed with the value of the " { $snippet "environment" } " slot:"
18 { $subsection +prepend-environment+ }
19 { $subsection +replace-environment+ }
20 { $subsection +append-environment+ }
21 "The default value is " { $link +append-environment+ } "." ;
22
23 ARTICLE: "io.launcher.redirection" "Input/output redirection"
24 "On all operating systems except for Windows CE, the default input/output/error streams can be redirected."
25 $nl
26 "To specify redirection, set the " { $snippet "stdin" } ", " { $snippet "stdout" } " and " { $snippet "stderr" } " slots of a " { $link process } " to one of the following values:"
27 { $list
28     { { $link f } " - default value; the stream is either inherited from the current process, or is a " { $link <process-stream> } " pipe" }
29     { { $link +closed+ } " - the stream is closed; reads will return end of file and writes will fails" }
30     { { $link +stdout+ } " - a special value for the " { $snippet "stderr" } " slot only, indicating that the standard output and standard error streams should be merged" }
31     { "a path name - the stream is sent to the given file, which must exist for input and is created automatically on output" }
32     { "an " { $link appender } " wrapping a path name - output is sent to the end given file, as with " { $link <file-appender> } }
33     { "a file stream or a socket - the stream is connected to the given Factor stream, which cannot be used again from within Factor and must be closed after the process has been started" }
34 } ;
35
36 ARTICLE: "io.launcher.priority" "Setting process priority"
37 "The priority of the child process can be set by storing one of the below symbols in the " { $snippet "priority" } " slot of a " { $link process } " tuple:"
38 { $list
39     { $link +lowest-priority+ }
40     { $link +low-priority+ }
41     { $link +normal-priority+ }
42     { $link +high-priority+ }
43     { $link +highest-priority+ }
44 }
45 "The default value is " { $link f } ", which denotes that the child process should inherit the current process priority." ;
46
47 HELP: +closed+
48 { $description "Possible value for the " { $snippet "stdin" } ", " { $snippet "stdout" } ", and " { $snippet "stderr" } " slots of a " { $link process } "." } ;
49
50 HELP: +stdout+
51 { $description "Possible value for the " { $snippet "stderr" } " slot of a " { $link process } "." } ;
52
53 HELP: appender
54 { $class-description "An object representing a file to append to. Instances are created with " { $link <appender> } "." } ;
55
56 HELP: <appender>
57 { $values { "path" "a pathname string" } { "appender" appender } }
58 { $description "Creates an object which may be stored in the " { $snippet "stdout" } " or " { $snippet "stderr" } " slot of a " { $link process } " instance." } ;
59
60 HELP: +prepend-environment+
61 { $description "Possible value of " { $snippet "environment-mode" } " slot of a " { $link process } "."
62 $nl
63 "If this value is set, the child process environment consists of the value of the " { $snippet "environment" } " slot together with the current environment, with entries from the current environment taking precedence."
64 $nl
65 "This is used in situations where you want to spawn a child process with some default environment variables set, but allowing the user to override these defaults by changing the environment before launching Factor." } ;
66
67 HELP: +replace-environment+
68 { $description "Possible value of " { $snippet "environment-mode" } " slot of a " { $link process } "."
69 $nl
70 "The child process environment consists of the value of the " { $snippet "environment" } " slot."
71 $nl
72 "This is used in situations where you want full control over a child process environment, perhaps for security or testing." } ;
73
74 HELP: +append-environment+
75 { $description "Possible value of " { $snippet "environment-mode" } " slot of a " { $link process } "."
76 $nl
77 "The child process environment consists of the current environment together with the value of the " { $snippet "environment" } " key, with entries from the " { $snippet "environment" } " key taking precedence."
78 $nl
79 "This is used in situations where you want a spawn child process with some overridden environment variables." } ;
80
81 ARTICLE: "io.launcher.timeouts" "Process run-time timeouts"
82 "The " { $snippet "timeout" } " slot of a " { $link process } " can be set to a " { $link duration } " specifying a maximum running time for the process. If " { $link wait-for-process } " is called and the process does not exit before the duration expires, it will be killed." ;
83
84 HELP: get-environment
85 { $values { "process" process } { "env" "an association" } }
86 { $description "Combines the current environment with the value of the " { $snippet "environment" } " slot of the " { $link process } " using the " { $snippet "environment-mode" } " slot." } ;
87
88 HELP: current-process-handle
89 { $values { "handle" "a process handle" } }
90 { $description "Returns the handle of the current process." } ;
91
92 HELP: run-process*
93 { $values { "process" process } { "handle" "a process handle" } }
94 { $contract "Launches a process." }
95 { $notes "User code should call " { $link run-process } " instead." } ;
96
97 HELP: run-process
98 { $values { "desc" "a launch descriptor" } { "process" process } }
99 { $description "Launches a process. The object can either be a string, a sequence of strings or a " { $link process } ". See " { $link "io.launcher.descriptors" } " for details." }
100 { $notes "The output value can be passed to " { $link wait-for-process } " to get an exit code." } ;
101
102 HELP: run-detached
103 { $values { "desc" "a launch descriptor" } { "process" process } }
104 { $contract "Launches a process without waiting for it to complete. The object can either be a string, a sequence of strings or a " { $link process } ". See " { $link "io.launcher.descriptors" } " for details." }
105 { $notes
106     "This word is functionally identical to passing a " { $link process } " to " { $link run-process } " having the " { $snippet "detached" } " slot set."
107     $nl
108     "The output value can be passed to " { $link wait-for-process } " to get an exit code."
109 } ;
110
111 HELP: process-failed
112 { $values { "code" "an exit status" } }
113 { $description "Throws a " { $link process-failed } " error." }
114 { $error-description "Thrown by " { $link try-process } " if the process exited with a non-zero status code." } ;
115
116 HELP: try-process
117 { $values { "desc" "a launch descriptor" } }
118 { $description "Launches a process and waits for it to complete. If it exits with a non-zero status code, throws a " { $link process-failed } " error." } ;
119
120 { run-process try-process run-detached } related-words
121
122 HELP: kill-process
123 { $values { "process" process } }
124 { $description "Kills a running process. Does nothing if the process has already exited." } ;
125
126 HELP: kill-process*
127 { $values { "handle" "a process handle" } }
128 { $contract "Kills a running process." }
129 { $notes "User code should call " { $link kill-process } " intead." } ;
130
131 HELP: process
132 { $class-description "A class representing a process. Instances are created by calling " { $link <process> } "." } ;
133
134 HELP: <process>
135 { $values { "process" process } }
136 { $description "Creates a new, empty process. It must be filled in before being passed to " { $link run-process } "." } ;
137
138 HELP: <process-stream>
139 { $values
140   { "desc" "a launch descriptor" }
141   { "encoding" "an encoding descriptor" }
142   { "stream" "a bidirectional stream" } }
143 { $description "Launches a process and redirects its input and output via a pair of pipes which may be read and written as a stream with the given encoding." } ;
144
145 HELP: <process-reader>
146 { $values
147   { "desc" "a launch descriptor" }
148   { "encoding" "an encoding descriptor" }
149   { "stream" "an input stream" } }
150 { $description "Launches a process and redirects its output via a pipe which may be read as a stream with the given encoding." } ;
151
152 HELP: <process-writer>
153 { $values
154   { "desc" "a launch descriptor" }
155   { "encoding" "an encoding descriptor" }
156   { "stream" "an output stream" }
157 }
158 { $description "Launches a process and redirects its input via a pipe which may be written to as a stream with the given encoding." } ;
159
160 HELP: with-process-stream
161 { $values
162   { "desc" "a launch descriptor" }
163   { "encoding" "an encoding descriptor" }
164   { "quot" quotation }
165 }
166 { $description "Launches a process and redirects its input and output via a pair of pipes. The quotation is called with " { $link input-stream } " and " { $link output-stream } " rebound to these pipes." } ;
167
168 HELP: with-process-reader
169 { $values
170   { "desc" "a launch descriptor" }
171   { "encoding" "an encoding descriptor" }
172   { "quot" quotation }
173 }
174 { $description "Launches a process and redirects its output via a pipe. The quotation is called with " { $link input-stream } " and " { $link output-stream } " rebound to this pipe." } ;
175
176 HELP: with-process-writer
177 { $values
178   { "desc" "a launch descriptor" }
179   { "encoding" "an encoding descriptor" }
180   { "quot" quotation }
181 }
182 { $description "Launches a process and redirects its input via a pipe. The quotation is called with " { $link input-stream } " and " { $link output-stream } " rebound to this pipe." } ;
183
184 HELP: wait-for-process
185 { $values { "process" process } { "status" object } }
186 { $description "If the process is still running, waits for it to exit, otherwise outputs the status code immediately. Can be called multiple times on the same process." }
187 { $notes "The status code is operating system specific; it may be an integer, or another object (the latter is the case on Unix if the process was killed by a signal). However, one cross-platform behavior code can rely on is that a status code of 0 indicates success." } ;
188
189 ARTICLE: "io.launcher.descriptors" "Launch descriptors"
190 "Words which launch processes can take either a command line string, a sequence of command line arguments, or a " { $link process } "."
191 $nl
192 "Strings and string arrays are wrapped in a new empty " { $link process } " with the " { $snippet "command" } " slot set. This covers basic use-cases where no launch parameters need to be set."
193 $nl
194 "A " { $link process } " instance can be created directly and passed to launching words for more control. It must be a fresh instance which has never been spawned before. To spawn a process several times from the same descriptor, " { $link clone } " the descriptor first." ;
195
196 ARTICLE: "io.launcher.lifecycle" "The process lifecycle"
197 "A freshly instantiated " { $link process } " represents a set of launch parameters."
198 { $subsection process }
199 { $subsection <process> }
200 "Words for launching processes take a fresh process which has never been started before as input, and output a copy as output."
201 { $subsection process-started? }
202 "The " { $link process } " instance output by launching words contains all original slot values in addition to the " { $snippet "handle" } " slot, which indicates the process is currently running."
203 { $subsection process-running? }
204 "It is possible to wait for a process to exit:"
205 { $subsection wait-for-process }
206 "A running process can also be killed:"
207 { $subsection kill-process } ;
208
209 ARTICLE: "io.launcher.launch" "Launching processes"
210 "Launching processes:"
211 { $subsection run-process }
212 { $subsection try-process }
213 { $subsection run-detached }
214 "Redirecting standard input and output to a pipe:"
215 { $subsection <process-reader> }
216 { $subsection <process-writer> }
217 { $subsection <process-stream> }
218 "Combinators built on top of the above:"
219 { $subsection with-process-reader }
220 { $subsection with-process-writer }
221 { $subsection with-process-stream } ;
222
223 ARTICLE: "io.launcher.examples" "Launcher examples"
224 "Starting a command and waiting for it to finish:"
225 { $code
226     "\"ls /etc\" run-process"
227 }
228 "Starting a program in the background:"
229 { $code
230     "{ \"emacs\" \"foo.txt\" } run-detached"
231 }
232 "Running a command, throwing an exception if it exits unsuccessfully:"
233 { $code
234     "\"make clean all\" try-process"
235 }
236 "Running a command, throwing an exception if it exits unsuccessfully or if it takes too long to run:"
237 { $code
238     "<process>"
239     "    \"make test\" >>command"
240     "    5 minutes >>timeout"
241     "try-process"
242 }
243 "Running a command, throwing an exception if it exits unsuccessfully, and redirecting output and error messages to a log file:"
244 { $code
245     "<process>"
246     "    \"make clean all\" >>command"
247     "    \"log.txt\" >>stdout"
248     "    +stdout+ >>stderr"
249     "try-process"
250 }
251 "Running a command, appending error messages to a log file, and reading the output for further processing:"
252 { $code
253     "\"log.txt\" <file-appender> ["
254     "    <process>"
255     "        swap >>stderr"
256     "        \"report\" >>command"
257     "    ascii <process-reader> lines sort reverse [ print ] each"
258     "] with-disposal"
259 } ;
260
261 ARTICLE: "io.launcher" "Operating system processes"
262 "The " { $vocab-link "io.launcher" } " vocabulary implements cross-platform process launching."
263 { $subsection "io.launcher.examples" }
264 { $subsection "io.launcher.descriptors" }
265 { $subsection "io.launcher.launch" }
266 "Advanced topics:"
267 { $subsection "io.launcher.lifecycle" }
268 { $subsection "io.launcher.command" }
269 { $subsection "io.launcher.detached" }
270 { $subsection "io.launcher.environment" }
271 { $subsection "io.launcher.redirection" }
272 { $subsection "io.launcher.priority" }
273 { $subsection "io.launcher.timeouts" } ;
274
275 ABOUT: "io.launcher"