OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / man / mann / bgexec.n
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 '\" The definitions below are for supplemental macros used in Tcl/Tk
23 '\" manual entries.
24 '\"
25 '\" .AP type name in/out ?indent?
26 '\"     Start paragraph describing an argument to a library procedure.
27 '\"     type is type of argument (int, etc.), in/out is either "in", "out",
28 '\"     or "in/out" to describe whether procedure reads or modifies arg,
29 '\"     and indent is equivalent to second arg of .IP (shouldn't ever be
30 '\"     needed;  use .AS below instead)
31 '\"
32 '\" .AS ?type? ?name?
33 '\"     Give maximum sizes of arguments for setting tab stops.  Type and
34 '\"     name are examples of largest possible arguments that will be passed
35 '\"     to .AP later.  If args are omitted, default tab stops are used.
36 '\"
37 '\" .BS
38 '\"     Start box enclosure.  From here until next .BE, everything will be
39 '\"     enclosed in one large box.
40 '\"
41 '\" .BE
42 '\"     End of box enclosure.
43 '\"
44 '\" .CS
45 '\"     Begin code excerpt.
46 '\"
47 '\" .CE
48 '\"     End code excerpt.
49 '\"
50 '\" .VS ?version? ?br?
51 '\"     Begin vertical sidebar, for use in marking newly-changed parts
52 '\"     of man pages.  The first argument is ignored and used for recording
53 '\"     the version when the .VS was added, so that the sidebars can be
54 '\"     found and removed when they reach a certain age.  If another argument
55 '\"     is present, then a line break is forced before starting the sidebar.
56 '\"
57 '\" .VE
58 '\"     End of vertical sidebar.
59 '\"
60 '\" .DS
61 '\"     Begin an indented unfilled display.
62 '\"
63 '\" .DE
64 '\"     End of indented unfilled display.
65 '\"
66 '\" .SO
67 '\"     Start of list of standard options for a Tk widget.  The
68 '\"     options follow on successive lines, in four columns separated
69 '\"     by tabs.
70 '\"
71 '\" .SE
72 '\"     End of list of standard options for a Tk widget.
73 '\"
74 '\" .OP cmdName dbName dbClass
75 '\"     Start of description of a specific option.  cmdName gives the
76 '\"     option's name as specified in the class command, dbName gives
77 '\"     the option's name in the option database, and dbClass gives
78 '\"     the option's class in the option database.
79 '\"
80 '\" .UL arg1 arg2
81 '\"     Print arg1 underlined, then print arg2 normally.
82 '\"
83 '\" RCS: @(#) $Id: man.macros,v 1.1.1.1 2009/05/09 16:27:42 pcmacdon Exp $
84 '\"
85 '\"     # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
86 .if t .wh -1.3i ^B
87 .nr ^l \n(.l
88 .ad b
89 '\"     # Start an argument description
90 .de AP
91 .ie !"\\$4"" .TP \\$4
92 .el \{\
93 .   ie !"\\$2"" .TP \\n()Cu
94 .   el          .TP 15
95 .\}
96 .ta \\n()Au \\n()Bu
97 .ie !"\\$3"" \{\
98 \&\\$1  \\fI\\$2\\fP    (\\$3)
99 .\".b
100 .\}
101 .el \{\
102 .br
103 .ie !"\\$2"" \{\
104 \&\\$1  \\fI\\$2\\fP
105 .\}
106 .el \{\
107 \&\\fI\\$1\\fP
108 .\}
109 .\}
110 ..
111 '\"     # define tabbing values for .AP
112 .de AS
113 .nr )A 10n
114 .if !"\\$1"" .nr )A \\w'\\$1'u+3n
115 .nr )B \\n()Au+15n
116 .\"
117 .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
118 .nr )C \\n()Bu+\\w'(in/out)'u+2n
119 ..
120 .AS Tcl_Interp Tcl_CreateInterp in/out
121 '\"     # BS - start boxed text
122 '\"     # ^y = starting y location
123 '\"     # ^b = 1
124 .de BS
125 .br
126 .mk ^y
127 .nr ^b 1u
128 .if n .nf
129 .if n .ti 0
130 .if n \l'\\n(.lu\(ul'
131 .if n .fi
132 ..
133 '\"     # BE - end boxed text (draw box now)
134 .de BE
135 .nf
136 .ti 0
137 .mk ^t
138 .ie n \l'\\n(^lu\(ul'
139 .el \{\
140 .\"     Draw four-sided box normally, but don't draw top of
141 .\"     box if the box started on an earlier page.
142 .ie !\\n(^b-1 \{\
143 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
144 .\}
145 .el \}\
146 \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
147 .\}
148 .\}
149 .fi
150 .br
151 .nr ^b 0
152 ..
153 '\"     # VS - start vertical sidebar
154 '\"     # ^Y = starting y location
155 '\"     # ^v = 1 (for troff;  for nroff this doesn't matter)
156 .de VS
157 .if !"\\$2"" .br
158 .mk ^Y
159 .ie n 'mc \s12\(br\s0
160 .el .nr ^v 1u
161 ..
162 '\"     # VE - end of vertical sidebar
163 .de VE
164 .ie n 'mc
165 .el \{\
166 .ev 2
167 .nf
168 .ti 0
169 .mk ^t
170 \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
171 .sp -1
172 .fi
173 .ev
174 .\}
175 .nr ^v 0
176 ..
177 '\"     # Special macro to handle page bottom:  finish off current
178 '\"     # box/sidebar if in box/sidebar mode, then invoked standard
179 '\"     # page bottom macro.
180 .de ^B
181 .ev 2
182 'ti 0
183 'nf
184 .mk ^t
185 .if \\n(^b \{\
186 .\"     Draw three-sided box if this is the box's first page,
187 .\"     draw two sides but no top otherwise.
188 .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
189 .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
190 .\}
191 .if \\n(^v \{\
192 .nr ^x \\n(^tu+1v-\\n(^Yu
193 \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
194 .\}
195 .bp
196 'fi
197 .ev
198 .if \\n(^b \{\
199 .mk ^y
200 .nr ^b 2
201 .\}
202 .if \\n(^v \{\
203 .mk ^Y
204 .\}
205 ..
206 '\"     # DS - begin display
207 .de DS
208 .RS
209 .nf
210 .sp
211 ..
212 '\"     # DE - end display
213 .de DE
214 .fi
215 .RE
216 .sp
217 ..
218 '\"     # SO - start of list of standard options
219 .de SO
220 .SH "STANDARD OPTIONS"
221 .LP
222 .nf
223 .ta 4c 8c 12c
224 .ft B
225 ..
226 '\"     # SE - end of list of standard options
227 .de SE
228 .fi
229 .ft R
230 .LP
231 See the \\fBoptions\\fR manual entry for details on the standard options.
232 ..
233 '\"     # OP - start of full description for a single option
234 .de OP
235 .LP
236 .nf
237 .ta 4c
238 Command-Line Name:      \\fB\\$1\\fR
239 Database Name:  \\fB\\$2\\fR
240 Database Class: \\fB\\$3\\fR
241 .fi
242 .IP
243 ..
244 '\"     # CS - begin code excerpt
245 .de CS
246 .RS
247 .nf
248 .ta .25i .5i .75i 1i
249 .ft CW
250 .sp
251 ..
252 '\"     # CE - end code excerpt
253 .de CE
254 .fi
255 .RE
256 .ft R
257 .sp
258 ..
259 .de UL
260 \\$1\l'|0\(ul'\\$2
261 ..
262 .TH bgexec n 2.5 BLT "BLT Built-In Commands"
263 .BS
264 '\" Note:  do not modify the .SH NAME line immediately below!
265 .SH NAME
266 bgexec \- Run programs in the background while handling Tk events.
267 kill \- Terminate program or send signal.
268 .SH SYNOPSIS
269 \fBblt::bgexec \fIvarName\fR ?\fIswitches\fR? \fIprogram\fR ?\fIarg\fR?...
270 .br
271 \fBblt::kill \fIprocessid\fR ?\fIsignal\fR?
272 .BE
273 .SH DESCRIPTION
274 .PP
275 The \fBkill\fR command terminates a \fIprocessid\fR or under unix
276 sends a signal.
277 .PP
278 The \fBbgexec\fR command executes a \fIprogram\fR pipleline using
279 the \fBTcl\fR event-loop allowing other events to continue to be serviced.
280 Upon completion it sets the global variable \fIvarName\fR with
281 a list of 4 status values: a text token, the process-id, the exit code,
282 and a text message.
283 \fBBgexec\fR provides capabilities similar to the \fBexec\fR command,
284 but with added support for callbacks, output to variables and termination.
285 .PP
286 When used with no options,
287 the returned value from \fBbgexec\fR is the output from the \fIprogram\fR.
288 But when the last \fIarg\fR is an ampersand (&)
289 the \fIprogram\fR runs detached, and
290 \fBbgexec\fR immediately returns with a list of the process ids
291 created in the command pipeline.
292 Detached processes can be interrupted and terminated simply by setting
293 \fIvarName\fR.
294 .PP
295 The valid \fIswitches\fR are as follows:
296 .TP 
297 \fB\-check \fInum\fR 
298 Interval in ms to poll for the exiting processes.
299 The default is 1000.
300 .TP 
301 \fB\-closeonkill \fImillisecs\fR 
302 Force close of stdin/stdout on kill after the given interval.
303 This lets kill finalize processes, even
304 uninterruptably sleeping ones unable to receive signals.
305 The default is \fB0\fR for do not force close.
306 .TP 
307 \fB\-command \fIscript\fR 
308 Specifies a command to call upon command completion/termination.
309 Two extra arguments are appended before the call.
310 The data output from the command, and the status info as set
311 into \fIvarName\fR.
312 .TP 
313 \fB\-decodeerror \fIencodingName\fR 
314 Specifies the encoding of the stderr channel.
315 This affects only data returned to the Tcl interpreter.  No translation 
316 is done on file redirection.
317 For example if data is to be converted from Unicode for use in Tcl,
318 you would use the "unicode" encoding. The default is that no 
319 tranlation is performed.
320 .TP 
321 \fB\-decodeoutput \fIencodingName\fR 
322 Specifies the encoding of the stdout channels.
323 This affects only data returned to the Tcl interpreter.  No translation 
324 is done on file redirection.
325 For example if data is to be converted from Unicode for use in Tcl,
326 you would use the "unicode" encoding. The default is that no 
327 tranlation is performed.
328 .TP 
329 \fB\-echo \fIboolean\fR 
330 Indicates if the pipeline's stderr stream should be echoed.
331 \fINote: this option is deprecated.\fR
332 .TP 
333 \fB\-error \fIvarName\fR 
334 Specifies that a global variable \fIvarName\fR is to be set with the
335 contents of stderr after the program has completed. 
336 .TP 
337 \fB\-keepnewline \fIboolean\fR
338 Specifies that a trailing newline should be retained in the 
339 output. If \fIboolean\fR is true, the trailing newline is truncated
340 from the output of the \fB\-onoutput\fR and \fB\-output\fR variables.  
341 The default value is \fBtrue\fR.
342 .TP 
343 \fB\-killsignal \fIsignal\fR
344 Specifies the signal to be sent to the program when 
345 terminating. This option is available only on Unix. 
346 \fISignal\fR can either be a number (typically 1-32) or
347 a mnemonic (such as SIGINT). If \fIsignal\fR is the empty string, 
348 then no signal is sent.  The default signal is \fB9\fR (SIGKILL).
349 .TP 
350 \fB\-lasterror \fIvarName\fR
351 Specifies a variable \fIvarName\fR that is updated whenever data
352 becomes available from standard error of the program.
353 \fIVarName\fR is a global variable. Unlike the \fB\-error\fR option,
354 data is available as soon as it arrives.
355 .TP 
356 \fB\-lastoutput \fIvarName\fR 
357 Specifies a variable \fIvarName\fR that is updated whenever data
358 becomes available from standard output of the program.
359 \fIVarName\fR is a global variable. Unlike the \fB\-output\fR option,
360 data is available as soon as it arrives.
361 .TP 
362 \fB\-limit \fInumBytes\fR
363 Limit the size of the returned data to \fInumBytes\fR,
364 terminating the program if exceeded.
365 The limit applies to both stdout and stderr.
366 .TP 
367 \fB\-linebuffered \fIboolean\fR
368 Specifies that updates should be made on a line-by-line basis.
369 Normally when new data is available \fBbgexec\fR will set the variable
370 (\fB\-lastoutput\fR and \fB\-lasterror\fR options) or invoke the
371 command (\fB\-onoutput\fR and \fB\-onerror\fR options) delivering all
372 the new data currently available.  If \fIboolean\fR is true, only one
373 line at a time will be delivered.  This can be useful when you want to
374 process the output on a line-by-line basis.  
375 The default value is
376 \fBfalse\fR.
377 .TP 
378 \fB\-local \fIboolean\fR
379 When \fIboolean\fR is true, any unqualified variables or command options
380 are treated as local to the current namespace.
381 This is mostly useful for non-detaching (no ampersand) commands.
382 Note that using this flag with a detached command will
383 use variables from the current namespace, not from the
384 current proc stack-frame.
385 .TP 
386 \fB\-onerror \fIcommand\fR
387 Specifies the start of a Tcl command that will be executed
388 whenever new data is available from standard error. The data
389 is appended to the command as an extra argument before it is
390 executed.
391 .TP 
392 \fB\-onoutput \fIcommand\fR 
393 Specifies the start of a Tcl command that will be executed
394 whenever new data is available from standard output. The data
395 is appended to the command as an extra argument before it is
396 executed.
397 .TP 
398 \fB\-output \fIvarName\fR
399 Specifies a global variable \fIvarName\fR to be set with the
400 output of the program, upon completion.
401 .TP 
402 \fB\-raise \fIboolean\fR
403 When \fIboolean\fR is \fBtrue\fR, a non-zero return code from a
404 non-detached command will raise an error (.ie emulates \fBexec\fR).
405 The default is \fBfalse\fR an error is generated only if 
406 one of the following occurs: invalid
407 options are given, a redirection error,
408 or process creation failure (eg. executable program not found).
409 Detached commands, of course, never raise an error
410 on a non-zero return code.
411 .TP 
412 \fB\-\|\-\fR
413 This marks the end of the options.  The following argument will
414 be considered the name of a program even if it starts with 
415 a dash (\fB\-\fR).
416 .SH USAGE
417 Invoking \fBbgexec\fR without a trailing ampersand
418 will block and wait for result.  However, other Tcl
419 events continue to be serviced.  This prevents Tcl from hanging, eg:
420 .PP
421 .CS
422 pack [text .t]
423 set val [blt::bgexec myStatus du -s]
424 .CE
425 .PP
426 Note that text widget .t continues to respond to events.
427 .SH CALLBACKS
428 Here is an example that invokes the Unix \fBdu\fR program
429 with a \fB-command\fR callback.
430 .PP
431 .CS
432 proc Done {data status} {  puts "Done($status)\\n$data" }
433
434 blt::bgexec myStatus  -command Done   du -s $dir &
435 .CE
436 .PP
437 When \fBdu\fR has completed,
438 the handler \fBDone\fR is called with data and status.
439 Also, the global variable \fImyStatus\fR is set
440 to contain the program's exit status, eg:
441 .PP
442 .CS
443 EXITED 26811 0 {child completed normally}
444 .CE
445 .PP
446 If \fImyStatus\fR is set before \fBdu\fR has
447 completed, the process will be killed. Under Unix, this sends
448 a signal (SIGKILL by default).  Under Win32,
449 \fBTerminateProcess\fR is called.
450 .PP
451 .SH VARIABLE
452 Here is another example, this time using the \fB-output\fR option
453 to direct output to a variable.
454 .PP
455 .CS
456 global myStatus myOutput
457 blt::bgexec myStatus -output myOutput du -s $dir
458 puts "Disk usage for $dir is $myOutput"
459 .CE
460 .PP
461 Upon completion, \fBMyOutput\fR will contain the output of the program.
462 .SH STDERR
463 Various \fBbgexec\fR options can be used
464 to capture \fBstderr\fR separately from \fBstdout\fR.
465 .PP
466 .CS
467 global myStatus myOutput myErrs
468 blt::bgexec myStatus -output myOutput -error myErrs du -s $dir
469 .CE
470 .PP
471 The \fB\-error\fR option is similar to \fB\-output\fR in that it sets a
472 variable when the program completes with data written to stderr.
473 .PP
474 .SH LOCAL
475 By default, \fBbgexec\fR treats variable or command options
476 as being in the global namespace.
477 The \fB-local\fR option can change this to use the current namespace.
478 Thus data can be collected to namespace-local variables even those
479 inside of procs,  eg.
480 .CS
481 proc Work {} {
482   blt::bgexec myStatus -local 1 -output val -error err du -s
483   puts "VAL=$val"
484   puts "ERR=$err"
485 }
486 .CE
487 which collects data to local variables.
488 .PP
489 For detached processes, \fB-local\fR will cause
490 data to aggregate to namespace variables, ie. outside the proc, eg.
491 .CS
492 namespace eval ::Ns {
493   set pval {}
494   set perr {}
495   proc Work {} {
496     blt::bgexec myStatus -local 1 -output pval -error perr du -s &
497   }
498 }
499 .CE
500 This collects data to \fB::Ns::pval\fR and stderr to  \fB::Ns::perr\fR.
501 Similarly, proc names (eg \fB-onoutput\fR) will be relative to the current namespace.
502 .PP
503 .SH PROGRESS
504 The \fB\-output\fR and \fB\-error\fR variables are set only
505 after the program completes.  But if a program runs for a long time,
506 you can gather data
507 as it becomes available using the \fB\-onoutput\fR option. 
508 As new data becomes available, this
509 command is executed, with data appended as an argument.
510 .PP
511 .CS
512 proc GetInfo { data } { puts $data }
513
514 blt::bgexec myStatus -onoutput GetInfo du -s $dir
515 .CE
516 .PP
517 The \fB\-onerror\fR option performs a similar function for the stderr
518 data stream.
519 .PP
520 .SH ERROR HANDLING
521 Like \fBexec\fR, \fBbgexec\fR returns an error if the exit code of the
522 program is non-zero.  To handle this
523 invoke \fBbgexec\fR from within a \fBcatch\fR.
524 .PP
525 .CS
526 catch { blt::bgexec myStatus -output myOutput du -s $dir }
527 .CE
528 .PP
529 Detached jobs will generate an error only if the program startup
530 failed.  Otherwise the only indication is
531 the status code set in \fImyStatus\fR.
532 .SH TKWAIT
533 By default, \fBbgexec\fR waits for a program to finish and
534 returns the resulting output.
535 To detach a program simply append an ampersand (&) as the last
536 argument on the command line, eg.
537 .PP
538 .CS
539 global myStatus myOutput
540 blt::bgexec myStatus -output myOutput du -s $dir &
541 .CE
542 .PP
543 \fBBgexec\fR will then return immediately with
544 the spawned process ids as the result.  If needed
545 \fBtkwait\fR can be used to wait for the program to finish:
546 .PP
547 .CS
548 global myStatus myOutput
549 blt::bgexec myStatus -output myOutput du -s $dir &
550       ...
551 tkwait variable myStatus
552 .CE
553 .PP
554 Note however that using \fBtkwait\fR can be dangerous.
555 Multiple \fBtkwait\fR/\fBvwait\fR calls must complete
556 in the reverse order called.
557 The BLT \fBbusy\fR command can be used to try and enforce this,
558 but a better alternative is to just use \fB-command\fR instead.
559 .SH DIFFERENCES WITH EXEC
560 Using \fBbgexec\fR without an ampersand will not hang Tcl: events
561 continue to be serviced by the event handler while the call blocks.
562 Also unlike \fBexec\fR, an error will not be generated if output is
563 appears on \fBstderr\fR.  And output from \fBstderr\fR can be separately
564 managed and collected (without having to redirect to files).
565 Finally, \fBbgexec\fR ensures that invoked processes get properly
566 cleaned up at termination.
567 .SH DIFFERENCES WITH FILEEVENT
568 Since Tk 4.0, a subset of \fBbgexec\fR can be achieved using the
569 \fBfileevent\fR command.  The steps for running a program in the
570 background are:
571 .PP
572 Execute the program with the \fBopen\fR command (using the "|"
573 syntax) and save the file handle.
574 .CS
575   global fileId 
576   set fileId [open "|du -s $dir" r]
577 .CE
578 Next register a Tcl code snippet with \fBfileevent\fR to be run
579 whenever output is available on the file handle.  The code snippet
580 will read from the file handle and save the output in a variable.
581 .CS
582 fileevent fileId readable { 
583   if { [gets $fileId line] < 0 } {
584       close $fileId
585       set output $temp
586       unset fileId temp
587   } else {
588       append temp $line
589   }
590 }
591 .CE
592 .PP
593 However,
594 \fBBgexec\fR is simpler and less error prone than using
595 \fBopen\fR + \fBfileevent\fR.
596 You don't have to worry about non-blocking I/O.
597 Everything is handled for you automatically.
598 .PP
599 Moreover, \fBbgexec\fR can run programs that \fBfileevent\fR can not.
600 \fBFileevent\fR assumes that the when stdout is closed the program has
601 completed.  But some programs, like the Unix \fBcompress\fR program,
602 reopen stdout, fooling \fBfileevent\fR into thinking the program has
603 terminated.  In the example above, we assume that the program will
604 write and flush its output line-by-line.  However when running another
605 program, your application can block in the \fBgets\fR command reading
606 a partial line.
607 .PP
608 \fBBgexec\fR gives you get back the exit status of the program.
609 It also lets you reliably kill detached processes and
610 allows you to collect data from both stdout and stderr individually.
611 Finally, since data collection is handled in C code, \fBbgexec\fR is
612 faster and more efficient.
613 .SH SEE ALSO
614 busy, exec, tkwait, vwait
615 .SH KEYWORDS
616 exec, background, busy