OSDN Git Service

87380cbc9586afa0183ca1e231456b6c98e38035
[eos/base.git] / util / src / TclTk / blt2.5 / man / bgexec.mann
1 '\"
2 '\" Copyright 1991-1997 by Bell Labs Innovations for Lucent Technologies.
3 '\"
4 '\" Permission to use, copy, modify, and distribute this software and its
5 '\" documentation for any purpose and without fee is hereby granted, provided
6 '\" that the above copyright notice appear in all copies and that both that the
7 '\" copyright notice and warranty disclaimer appear in supporting documentation,
8 '\" and that the names of Lucent Technologies any of their entities not be used
9 '\" in advertising or publicity pertaining to distribution of the software
10 '\" without specific, written prior permission.
11 '\"
12 '\" Lucent Technologies disclaims all warranties with regard to this software,
13 '\" including all implied warranties of merchantability and fitness.  In no event
14 '\" shall Lucent Technologies be liable for any special, indirect or
15 '\" consequential damages or any damages whatsoever resulting from loss of use,
16 '\" data or profits, whether in an action of contract, negligence or other
17 '\" tortuous action, arising out of or in connection with the use or performance
18 '\" of this software.  
19 '\"
20 '\" Bgexec command created by George Howlett.
21 '\"
22 .so man.macros
23 .TH bgexec n BLT_VERSION BLT "BLT Built-In Commands"
24 .BS
25 '\" Note:  do not modify the .SH NAME line immediately below!
26 .SH NAME
27 bgexec \- Run programs in the background while handling Tk events.
28 kill \- Terminate program or send signal.
29 .SH SYNOPSIS
30 \fBblt::bgexec \fIvarName\fR ?\fIswitches\fR? \fIprogram\fR ?\fIarg\fR?...
31 .br
32 \fBblt::kill \fIprocessid\fR ?\fIsignal\fR?
33 .BE
34 .SH DESCRIPTION
35 .PP
36 The \fBkill\fR command terminates a \fIprocessid\fR or under unix
37 sends a signal.
38 .PP
39 The \fBbgexec\fR command executes a \fIprogram\fR pipleline using
40 the \fBTcl\fR event-loop allowing other events to continue to be serviced.
41 Upon completion it sets the global variable \fIvarName\fR with
42 a list of 4 status values: a text token, the process-id, the exit code,
43 and a text message.
44 \fBBgexec\fR provides capabilities similar to the \fBexec\fR command,
45 but with added support for callbacks, output to variables and termination.
46 .PP
47 When used with no options,
48 the returned value from \fBbgexec\fR is the output from the \fIprogram\fR.
49 But when the last \fIarg\fR is an ampersand (&)
50 the \fIprogram\fR runs detached, and
51 \fBbgexec\fR immediately returns with a list of the process ids
52 created in the command pipeline.
53 Detached processes can be interrupted and terminated simply by setting
54 \fIvarName\fR.
55 .PP
56 The valid \fIswitches\fR are as follows:
57 .TP 
58 \fB\-check \fInum\fR 
59 Interval in ms to poll for the exiting processes.
60 The default is 1000.
61 .TP 
62 \fB\-closeonkill \fImillisecs\fR 
63 Force close of stdin/stdout on kill after the given interval.
64 This lets kill finalize processes, even
65 uninterruptably sleeping ones unable to receive signals.
66 The default is \fB0\fR for do not force close.
67 .TP 
68 \fB\-command \fIscript\fR 
69 Specifies a command to call upon command completion/termination.
70 Two extra arguments are appended before the call.
71 The data output from the command, and the status info as set
72 into \fIvarName\fR.
73 .TP 
74 \fB\-decodeerror \fIencodingName\fR 
75 Specifies the encoding of the stderr channel.
76 This affects only data returned to the Tcl interpreter.  No translation 
77 is done on file redirection.
78 For example if data is to be converted from Unicode for use in Tcl,
79 you would use the "unicode" encoding. The default is that no 
80 tranlation is performed.
81 .TP 
82 \fB\-decodeoutput \fIencodingName\fR 
83 Specifies the encoding of the stdout channels.
84 This affects only data returned to the Tcl interpreter.  No translation 
85 is done on file redirection.
86 For example if data is to be converted from Unicode for use in Tcl,
87 you would use the "unicode" encoding. The default is that no 
88 tranlation is performed.
89 .TP 
90 \fB\-echo \fIboolean\fR 
91 Indicates if the pipeline's stderr stream should be echoed.
92 \fINote: this option is deprecated.\fR
93 .TP 
94 \fB\-error \fIvarName\fR 
95 Specifies that a global variable \fIvarName\fR is to be set with the
96 contents of stderr after the program has completed. 
97 .TP 
98 \fB\-keepnewline \fIboolean\fR
99 Specifies that a trailing newline should be retained in the 
100 output. If \fIboolean\fR is true, the trailing newline is truncated
101 from the output of the \fB\-onoutput\fR and \fB\-output\fR variables.  
102 The default value is \fBtrue\fR.
103 .TP 
104 \fB\-killsignal \fIsignal\fR
105 Specifies the signal to be sent to the program when 
106 terminating. This option is available only on Unix. 
107 \fISignal\fR can either be a number (typically 1-32) or
108 a mnemonic (such as SIGINT). If \fIsignal\fR is the empty string, 
109 then no signal is sent.  The default signal is \fB9\fR (SIGKILL).
110 .TP 
111 \fB\-lasterror \fIvarName\fR
112 Specifies a variable \fIvarName\fR that is updated whenever data
113 becomes available from standard error of the program.
114 \fIVarName\fR is a global variable. Unlike the \fB\-error\fR option,
115 data is available as soon as it arrives.
116 .TP 
117 \fB\-lastoutput \fIvarName\fR 
118 Specifies a variable \fIvarName\fR that is updated whenever data
119 becomes available from standard output of the program.
120 \fIVarName\fR is a global variable. Unlike the \fB\-output\fR option,
121 data is available as soon as it arrives.
122 .TP 
123 \fB\-limit \fInumBytes\fR
124 Limit the size of the returned data to \fInumBytes\fR,
125 terminating the program if exceeded.
126 The limit applies to both stdout and stderr.
127 .TP 
128 \fB\-linebuffered \fIboolean\fR
129 Specifies that updates should be made on a line-by-line basis.
130 Normally when new data is available \fBbgexec\fR will set the variable
131 (\fB\-lastoutput\fR and \fB\-lasterror\fR options) or invoke the
132 command (\fB\-onoutput\fR and \fB\-onerror\fR options) delivering all
133 the new data currently available.  If \fIboolean\fR is true, only one
134 line at a time will be delivered.  This can be useful when you want to
135 process the output on a line-by-line basis.  
136 The default value is
137 \fBfalse\fR.
138 .TP 
139 \fB\-local \fIboolean\fR
140 When \fIboolean\fR is true, any unqualified variables or command options
141 are treated as local to the current namespace.
142 This is mostly useful for non-detaching (no ampersand) commands.
143 Note that using this flag with a detached command will
144 use variables from the current namespace, not from the
145 current proc stack-frame.
146 .TP 
147 \fB\-onerror \fIcommand\fR
148 Specifies the start of a Tcl command that will be executed
149 whenever new data is available from standard error. The data
150 is appended to the command as an extra argument before it is
151 executed.
152 .TP 
153 \fB\-onoutput \fIcommand\fR 
154 Specifies the start of a Tcl command that will be executed
155 whenever new data is available from standard output. The data
156 is appended to the command as an extra argument before it is
157 executed.
158 .TP 
159 \fB\-output \fIvarName\fR
160 Specifies a global variable \fIvarName\fR to be set with the
161 output of the program, upon completion.
162 .TP 
163 \fB\-raise \fIboolean\fR
164 When \fIboolean\fR is \fBtrue\fR, a non-zero return code from a
165 non-detached command will raise an error (.ie emulates \fBexec\fR).
166 The default is \fBfalse\fR an error is generated only if 
167 one of the following occurs: invalid
168 options are given, a redirection error,
169 or process creation failure (eg. executable program not found).
170 Detached commands, of course, never raise an error
171 on a non-zero return code.
172 .TP 
173 \fB\-\|\-\fR
174 This marks the end of the options.  The following argument will
175 be considered the name of a program even if it starts with 
176 a dash (\fB\-\fR).
177 .SH USAGE
178 Invoking \fBbgexec\fR without a trailing ampersand
179 will block and wait for result.  However, other Tcl
180 events continue to be serviced.  This prevents Tcl from hanging, eg:
181 .PP
182 .CS
183 pack [text .t]
184 set val [blt::bgexec myStatus du -s]
185 .CE
186 .PP
187 Note that text widget .t continues to respond to events.
188 .SH CALLBACKS
189 Here is an example that invokes the Unix \fBdu\fR program
190 with a \fB-command\fR callback.
191 .PP
192 .CS
193 proc Done {data status} {  puts "Done($status)\\n$data" }
194
195 blt::bgexec myStatus  -command Done   du -s $dir &
196 .CE
197 .PP
198 When \fBdu\fR has completed,
199 the handler \fBDone\fR is called with data and status.
200 Also, the global variable \fImyStatus\fR is set
201 to contain the program's exit status, eg:
202 .PP
203 .CS
204 EXITED 26811 0 {child completed normally}
205 .CE
206 .PP
207 If \fImyStatus\fR is set before \fBdu\fR has
208 completed, the process will be killed. Under Unix, this sends
209 a signal (SIGKILL by default).  Under Win32,
210 \fBTerminateProcess\fR is called.
211 .PP
212 .SH VARIABLE
213 Here is another example, this time using the \fB-output\fR option
214 to direct output to a variable.
215 .PP
216 .CS
217 global myStatus myOutput
218 blt::bgexec myStatus -output myOutput du -s $dir
219 puts "Disk usage for $dir is $myOutput"
220 .CE
221 .PP
222 Upon completion, \fBMyOutput\fR will contain the output of the program.
223 .SH STDERR
224 Various \fBbgexec\fR options can be used
225 to capture \fBstderr\fR separately from \fBstdout\fR.
226 .PP
227 .CS
228 global myStatus myOutput myErrs
229 blt::bgexec myStatus -output myOutput -error myErrs du -s $dir
230 .CE
231 .PP
232 The \fB\-error\fR option is similar to \fB\-output\fR in that it sets a
233 variable when the program completes with data written to stderr.
234 .PP
235 .SH LOCAL
236 By default, \fBbgexec\fR treats variable or command options
237 as being in the global namespace.
238 The \fB-local\fR option can change this to use the current namespace.
239 Thus data can be collected to namespace-local variables even those
240 inside of procs,  eg.
241 .CS
242 proc Work {} {
243   blt::bgexec myStatus -local 1 -output val -error err du -s
244   puts "VAL=$val"
245   puts "ERR=$err"
246 }
247 .CE
248 which collects data to local variables.
249 .PP
250 For detached processes, \fB-local\fR will cause
251 data to aggregate to namespace variables, ie. outside the proc, eg.
252 .CS
253 namespace eval ::Ns {
254   set pval {}
255   set perr {}
256   proc Work {} {
257     blt::bgexec myStatus -local 1 -output pval -error perr du -s &
258   }
259 }
260 .CE
261 This collects data to \fB::Ns::pval\fR and stderr to  \fB::Ns::perr\fR.
262 Similarly, proc names (eg \fB-onoutput\fR) will be relative to the current namespace.
263 .PP
264 .SH PROGRESS
265 The \fB\-output\fR and \fB\-error\fR variables are set only
266 after the program completes.  But if a program runs for a long time,
267 you can gather data
268 as it becomes available using the \fB\-onoutput\fR option. 
269 As new data becomes available, this
270 command is executed, with data appended as an argument.
271 .PP
272 .CS
273 proc GetInfo { data } { puts $data }
274
275 blt::bgexec myStatus -onoutput GetInfo du -s $dir
276 .CE
277 .PP
278 The \fB\-onerror\fR option performs a similar function for the stderr
279 data stream.
280 .PP
281 .SH ERROR HANDLING
282 Like \fBexec\fR, \fBbgexec\fR returns an error if the exit code of the
283 program is non-zero.  To handle this
284 invoke \fBbgexec\fR from within a \fBcatch\fR.
285 .PP
286 .CS
287 catch { blt::bgexec myStatus -output myOutput du -s $dir }
288 .CE
289 .PP
290 Detached jobs will generate an error only if the program startup
291 failed.  Otherwise the only indication is
292 the status code set in \fImyStatus\fR.
293 .SH TKWAIT
294 By default, \fBbgexec\fR waits for a program to finish and
295 returns the resulting output.
296 To detach a program simply append an ampersand (&) as the last
297 argument on the command line, eg.
298 .PP
299 .CS
300 global myStatus myOutput
301 blt::bgexec myStatus -output myOutput du -s $dir &
302 .CE
303 .PP
304 \fBBgexec\fR will then return immediately with
305 the spawned process ids as the result.  If needed
306 \fBtkwait\fR can be used to wait for the program to finish:
307 .PP
308 .CS
309 global myStatus myOutput
310 blt::bgexec myStatus -output myOutput du -s $dir &
311       ...
312 tkwait variable myStatus
313 .CE
314 .PP
315 Note however that using \fBtkwait\fR can be dangerous.
316 Multiple \fBtkwait\fR/\fBvwait\fR calls must complete
317 in the reverse order called.
318 The BLT \fBbusy\fR command can be used to try and enforce this,
319 but a better alternative is to just use \fB-command\fR instead.
320 .SH DIFFERENCES WITH EXEC
321 Using \fBbgexec\fR without an ampersand will not hang Tcl: events
322 continue to be serviced by the event handler while the call blocks.
323 Also unlike \fBexec\fR, an error will not be generated if output is
324 appears on \fBstderr\fR.  And output from \fBstderr\fR can be separately
325 managed and collected (without having to redirect to files).
326 Finally, \fBbgexec\fR ensures that invoked processes get properly
327 cleaned up at termination.
328 .SH DIFFERENCES WITH FILEEVENT
329 Since Tk 4.0, a subset of \fBbgexec\fR can be achieved using the
330 \fBfileevent\fR command.  The steps for running a program in the
331 background are:
332 .PP
333 Execute the program with the \fBopen\fR command (using the "|"
334 syntax) and save the file handle.
335 .CS
336   global fileId 
337   set fileId [open "|du -s $dir" r]
338 .CE
339 Next register a Tcl code snippet with \fBfileevent\fR to be run
340 whenever output is available on the file handle.  The code snippet
341 will read from the file handle and save the output in a variable.
342 .CS
343 fileevent fileId readable { 
344   if { [gets $fileId line] < 0 } {
345       close $fileId
346       set output $temp
347       unset fileId temp
348   } else {
349       append temp $line
350   }
351 }
352 .CE
353 .PP
354 However,
355 \fBBgexec\fR is simpler and less error prone than using
356 \fBopen\fR + \fBfileevent\fR.
357 You don't have to worry about non-blocking I/O.
358 Everything is handled for you automatically.
359 .PP
360 Moreover, \fBbgexec\fR can run programs that \fBfileevent\fR can not.
361 \fBFileevent\fR assumes that the when stdout is closed the program has
362 completed.  But some programs, like the Unix \fBcompress\fR program,
363 reopen stdout, fooling \fBfileevent\fR into thinking the program has
364 terminated.  In the example above, we assume that the program will
365 write and flush its output line-by-line.  However when running another
366 program, your application can block in the \fBgets\fR command reading
367 a partial line.
368 .PP
369 \fBBgexec\fR gives you get back the exit status of the program.
370 It also lets you reliably kill detached processes and
371 allows you to collect data from both stdout and stderr individually.
372 Finally, since data collection is handled in C code, \fBbgexec\fR is
373 faster and more efficient.
374 .SH SEE ALSO
375 busy, exec, tkwait, vwait
376 .SH KEYWORDS
377 exec, background, busy