Subject: Re: opening two pipes for exec? - DN [1]
"George A. Howlett" <gah@cadence.com> - 27 May 1999 - comp.lang.tcl
Mark G. Saye <markgsaye@earthlink.net> wrote:
: I want to exec a command and put the output into a text widget. I'm
: using the command:
: set pipe [open "|$command |& cat" r+]
: fconfigure $pipe -buffering line
: which returns both stdout and stderr from $command. Can I open a second
: pipe (to stderr) so that I can add stderr messages into my text widget
: with a red tag, for instance ? Or is there some other way I can
: differentiate the two channels when I read from the pipe ? I'd like to
: be able to read from both pipes simultaneously (i.e. not wait until
: $command has finished to get stderr)
: I found a message on Deja saying this could possibly be done with tclX
: 'pipe' command, but I'm using tcl/tk 8.0.5 (on Linux RH 5.2). Any
: suggestions would be very helpful.
The "bgexec" command in BLT lets you do this in a simple,
straight-forward way.
text .text
.text tag configure stdoutTag -foreground green
.text tag configure stderrTag -foreground red
proc DisplayOutput { string } {
.text insert end $string stdoutTag
}
proc DisplayErrors { string } {
.text insert end $string stderrTag
}
bgexec myVar \
-onoutput DisplayOutput -onerror DisplayErrors \
$command &
The -onoutput and -onerror switches specify procedures that get called
whenever data is available on that channel. Both procedures take one
argument that is the new data.
The varible "myVar" gets set when the command is done executing. It
will contain the return code, message, etal. of the command. So you
can get the command run in the background (using "&" as the last
argument of the command) and still know when it finishes by putting a
trace on that variable.
If you need to stop the command while its running, just set the
"myVar" and the program will be killed. "bgexec" accepts all the same
syntax as the "exec" command. And it works the same way under both
Unix and Windows.
--gah
Last modified
1999-09-27
1999-09-27
(195.108.246.50)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
