summaryrefslogtreecommitdiffstats
path: root/doc/exec.n
diff options
context:
space:
mode:
Diffstat (limited to 'doc/exec.n')
-rw-r--r--doc/exec.n268
1 files changed, 108 insertions, 160 deletions
diff --git a/doc/exec.n b/doc/exec.n
index becb130..3857a71 100644
--- a/doc/exec.n
+++ b/doc/exec.n
@@ -5,7 +5,7 @@
'\"
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
-'\"
+'\"
.TH exec n 8.5 Tcl "Tcl Built-In Commands"
.so man.macros
.BS
@@ -13,8 +13,9 @@
.SH NAME
exec \- Invoke subprocesses
.SH SYNOPSIS
-\fBexec \fR?\fIswitches\fR? \fIarg \fR?\fIarg ...\fR? ?\fB&\fR?
+\fBexec \fR?\fIswitches\fR? \fIarg \fR?\fIarg ...\fR?
.BE
+
.SH DESCRIPTION
.PP
This command treats its arguments as the specification
@@ -22,10 +23,6 @@ of one or more subprocesses to execute.
The arguments take the form of a standard shell pipeline
where each \fIarg\fR becomes one word of a command, and
each distinct command becomes a subprocess.
-The result of the command is the standard output of the final subprocess in
-the pipeline, interpreted using the system \fBencoding\fR; to use any other
-encoding (especially including binary data), the pipeline must be
-\fBopen\fRed, configured and read explicitly.
.PP
If the initial arguments to \fBexec\fR start with \fB\-\fR then
they are treated as command-line switches and are not part
@@ -33,17 +30,16 @@ of the pipeline specification. The following switches are
currently supported:
.TP 13
\fB\-ignorestderr\fR
-.
+.VS 8.5
Stops the \fBexec\fR command from treating the output of messages to the
pipeline's standard error channel as an error case.
+.VE 8.5
.TP 13
\fB\-keepnewline\fR
-.
Retains a trailing newline in the pipeline's output.
Normally a trailing newline will be deleted.
.TP 13
\fB\-\|\-\fR
-.
Marks the end of switches. The argument following this one will
be treated as the first \fIarg\fR even if it starts with a \fB\-\fR.
.PP
@@ -59,77 +55,64 @@ or in the same argument with no intervening space (i.e.
.QW \fB<\fIfileName\fR ).
.TP 15
\fB|\fR
-.
Separates distinct commands in the pipeline. The standard output
of the preceding command will be piped into the standard input
of the next command.
.TP 15
\fB|&\fR
-.
Separates distinct commands in the pipeline. Both standard output
and standard error of the preceding command will be piped into
the standard input of the next command.
This form of redirection overrides forms such as 2> and >&.
.TP 15
\fB<\0\fIfileName\fR
-.
The file named by \fIfileName\fR is opened and used as the standard
input for the first command in the pipeline.
.TP 15
\fB<@\0\fIfileId\fR
-.
\fIFileId\fR must be the identifier for an open file, such as the return
value from a previous call to \fBopen\fR.
It is used as the standard input for the first command in the pipeline.
\fIFileId\fR must have been opened for reading.
.TP 15
\fB<<\0\fIvalue\fR
-.
\fIValue\fR is passed to the first command as its standard input.
.TP 15
\fB>\0\fIfileName\fR
-.
Standard output from the last command is redirected to the file named
\fIfileName\fR, overwriting its previous contents.
.TP 15
\fB2>\0\fIfileName\fR
-.
Standard error from all commands in the pipeline is redirected to the
file named \fIfileName\fR, overwriting its previous contents.
.TP 15
\fB>&\0\fIfileName\fR
-.
Both standard output from the last command and standard error from all
commands are redirected to the file named \fIfileName\fR, overwriting
its previous contents.
.TP 15
\fB>>\0\fIfileName\fR
-.
Standard output from the last command is
redirected to the file named \fIfileName\fR, appending to it rather
than overwriting it.
.TP 15
\fB2>>\0\fIfileName\fR
-.
Standard error from all commands in the pipeline is
redirected to the file named \fIfileName\fR, appending to it rather
than overwriting it.
.TP 15
\fB>>&\0\fIfileName\fR
-.
Both standard output from the last command and standard error from
all commands are redirected to the file named \fIfileName\fR,
appending to it rather than overwriting it.
.TP 15
\fB>@\0\fIfileId\fR
-.
\fIFileId\fR must be the identifier for an open file, such as the return
value from a previous call to \fBopen\fR.
Standard output from the last command is redirected to \fIfileId\fR's
file, which must have been opened for writing.
.TP 15
\fB2>@\0\fIfileId\fR
-.
\fIFileId\fR must be the identifier for an open file, such as the return
value from a previous call to \fBopen\fR.
Standard error from all commands in the pipeline is
@@ -137,13 +120,11 @@ redirected to \fIfileId\fR's file.
The file must have been opened for writing.
.TP 15
\fB2>@1\0\fR
-.
Standard error from all commands in the pipeline is redirected to the
command result. This operator is only valid at the end of the command
pipeline.
.TP 15
\fB>&@\0\fIfileId\fR
-.
\fIFileId\fR must be the identifier for an open file, such as the return
value from a previous call to \fBopen\fR.
Both standard output from the last command and standard error from
@@ -152,9 +133,12 @@ The file must have been opened for writing.
.PP
If standard output has not been redirected then the \fBexec\fR
command returns the standard output from the last command
-in the pipeline, unless
+in the pipeline,
+.VS 8.5
+unless
.QW 2>@1
was specified, in which case standard error is included as well.
+.VE 8.5
If any of the commands in the pipeline exit abnormally or
are killed or suspended, then \fBexec\fR will return an error
and the error message will include the pipeline's output followed by
@@ -163,7 +147,9 @@ error messages describing the abnormal terminations; the
about the last abnormal termination encountered.
If any of the commands writes to its standard error file and that
standard error is not redirected
+.VS 8.5
and \fB\-ignorestderr\fR is not specified,
+.VE 8.5
then \fBexec\fR will return an error; the error message
will include the pipeline's standard output, followed by messages
about abnormal terminations (if any), followed by the standard error
@@ -221,53 +207,10 @@ information is instead sent to the console, if one is present, or is
discarded.
.RS
.PP
-Note that the current escape resp. quoting of arguments for windows works only
-with executables using CommandLineToArgv, CRT-library or similar, as well as
-with the windows batch files (excepting the newline, see below).
-Although it is the common escape algorithm, but, in fact, the way how the
-executable parses the command-line (resp. splits it into single arguments)
-is decisive.
-.PP
-Unfortunately, there is currently no way to supply newline character within
-an argument to the batch files (\fB.cmd\fR or \fB.bat\fR) or to the command
-processor (\fBcmd.exe /c\fR), because this causes truncation of command-line
-(also the argument chain) on the first newline character.
-But it works properly with an executable (using CommandLineToArgv, etc).
-.PP
-\fBArgument quoting\fR
-.RS
-The arguments of the \fBexec\fR command are mapped to the arguments of the called
-program. Additional quote characters (\fB"\fR) are automatically added around
-arguments if expected. Special characters are escaped by inserting backslash
-characters.
-.PP
-The MS-Windows environment does execute programs mentioned in the arguments and
-called batch files (conspec) replace environment variables, which may have side
-effects (vulnerabilities) or break any already existing quoting (for example,
-if the environment variable contains a special character like a \fB"\fR).
-Examples are:
-.CS
-% exec my-echo.cmd {test&whoami}
- test
- mylogin
-% exec my-echo.cmd "ENV X:%X%"
- ENV X: CONTENT OF X
-.CE
-The following formatting is automatically performed on any
-argument item to avoid subprogram execution:
-Any special character argument containing a special character (\fB&\fR, \fB|\fR,
-\fB^\fR, \fB<\fR, \fB>\fR, \fB!\fR, \fB(\fR, \fB)\fR, \fB(\fR, \fB%\fR)
-is automatically enclosed in quotes (\fB"\fR). Any data quote is escaped by
-insertion of backslash characters.
-.PP
-The automatic resolving of environment variables using "\fB%var%\fR" is critical,
-but has more use than danger and is not escaped.
-.RE
-.PP
The Tk console text widget does not provide real standard IO capabilities.
Under Tk, when redirecting from standard input, all applications will see an
immediate end-of-file; information redirected to standard output or standard
-error will be discarded.
+error will be discarded.
.PP
Either forward or backward slashes are accepted as path separators for
arguments to Tcl commands. When executing an application, the path name
@@ -278,7 +221,16 @@ backslashes only in paths. Any arguments to an application that specify a
path name with forward slashes will not automatically be converted to use
the backslash character. If an argument contains forward slashes as the
path separator, it may or may not be recognized as a path name, depending on
-the program.
+the program.
+.PP
+Additionally, when calling a 16-bit DOS or Windows 3.X application, all path
+names must use the short, cryptic, path format (e.g., using
+.QW applba~1.def
+instead of
+.QW applbakery.default ),
+which can be obtained with the
+.QW "\fBfile attributes \fIfileName \fB\-shortname\fR"
+command.
.PP
Two or more forward or backward slashes in a row in a path refer to a
network path. For example, a simple concatenation of the root directory
@@ -307,23 +259,28 @@ present, as is done when launching applications under wish. It is desirable
to have console applications hidden and detached. This is a designed-in
limitation as \fBexec\fR wants to communicate over pipes. The Expect
extension addresses this issue when communicating with a TUI application.
-.PP
+.RE
+.TP
+\fBWindows NT\fR
+.
When attempting to execute an application, \fBexec\fR first searches for
-the name as it was specified. Then, in order,
-\fB.com\fR, \fB.exe\fR, \fB.bat\fR and \fB.cmd\fR
-are appended to the end of the specified name and it searches
+the name as it was specified. Then, in order, \fB.com\fR, \fB.exe\fR, and
+\fB.bat\fR are appended to the end of the specified name and it searches
for the longer name. If a directory name was not specified as part of the
application name, the following directories are automatically searched in
order when attempting to locate the application:
-.IP \(bu 3
+.RS
+.IP \(bu
The directory from which the Tcl executable was loaded.
-.IP \(bu 3
+.IP \(bu
The current directory.
-.IP \(bu 3
-The Windows 32-bit system directory.
-.IP \(bu 3
-The Windows home directory.
-.IP \(bu 3
+.IP \(bu
+The Windows NT 32-bit system directory.
+.IP \(bu
+The Windows NT 16-bit system directory.
+.IP \(bu
+The Windows NT home directory.
+.IP \(bu
The directories listed in the path.
.PP
In order to execute shell built-in commands like \fBdir\fR and \fBcopy\fR,
@@ -332,126 +289,134 @@ the caller must prepend the desired command with
because built-in commands are not implemented using executables.
.RE
.TP
-\fBUnix\fR (including Mac OS X)
+\fBWindows 9x\fR
.
+When attempting to execute an application, \fBexec\fR first searches for
+the name as it was specified. Then, in order, \fB.com\fR, \fB.exe\fR, and
+\fB.bat\fR are appended to the end of the specified name and it searches
+for the longer name. If a directory name was not specified as part of the
+application name, the following directories are automatically searched in
+order when attempting to locate the application:
+.RS
+.IP \(bu
+The directory from which the Tcl executable was loaded.
+.IP \(bu
+The current directory.
+.IP \(bu
+The Windows 9x system directory.
+.IP \(bu
+The Windows 9x home directory.
+.IP \(bu
+The directories listed in the path.
+.RE
+.RS
+.PP
+In order to execute shell built-in commands like \fBdir\fR and \fBcopy\fR,
+the caller must prepend the desired command with
+.QW "\fBcommand.com /c\0\fR"
+because built-in commands are not implemented using executables.
+.PP
+Once a 16-bit DOS application has read standard input from a console and
+then quit, all subsequently run 16-bit DOS applications will see the
+standard input as already closed. 32-bit applications do not have this
+problem and will run correctly, even after a 16-bit DOS application thinks
+that standard input is closed. There is no known workaround for this bug
+at this time.
+.PP
+Redirection between the \fBNUL:\fR device and a 16-bit application does not
+always work. When redirecting from \fBNUL:\fR, some applications may hang,
+others will get an infinite stream of
+.QW 0x01
+bytes, and some will actually
+correctly get an immediate end-of-file; the behavior seems to depend upon
+something compiled into the application itself. When redirecting greater than
+4K or so to \fBNUL:\fR, some applications will hang. The above problems do not
+happen with 32-bit applications.
+.PP
+All DOS 16-bit applications are run synchronously. All standard input from
+a pipe to a 16-bit DOS application is collected into a temporary file; the
+other end of the pipe must be closed before the 16-bit DOS application
+begins executing. All standard output or error from a 16-bit DOS
+application to a pipe is collected into temporary files; the application
+must terminate before the temporary files are redirected to the next stage
+of the pipeline. This is due to a workaround for a Windows 95 bug in the
+implementation of pipes, and is how the standard Windows 95 DOS shell
+handles pipes itself.
+.PP
+Certain applications, such as \fBcommand.com\fR, should not be executed
+interactively. Applications which directly access the console window,
+rather than reading from their standard input and writing to their standard
+output may fail, hang Tcl, or even hang the system if their own private
+console window is not available to them.
+.RE
+.TP
+\fBUnix\fR\0\0\0\0\0\0\0
The \fBexec\fR command is fully functional and works as described.
.SH "UNIX EXAMPLES"
-.PP
Here are some examples of the use of the \fBexec\fR command on Unix.
-To execute a simple program and get its result:
.PP
+To execute a simple program and get its result:
.CS
\fBexec\fR uname -a
.CE
-.SS "WORKING WITH NON-ZERO RESULTS"
.PP
To execute a program that can return a non-zero result, you should
wrap the call to \fBexec\fR in \fBcatch\fR and check the contents
of the \fB\-errorcode\fR return option if you have an error:
-.PP
.CS
set status 0
if {[catch {\fBexec\fR grep foo bar.txt} results options]} {
- set details [dict get $options -errorcode]
- if {[lindex $details 0] eq "CHILDSTATUS"} {
- set status [lindex $details 2]
- } else {
- # Some other error; regenerate it to let caller handle
- return -options $options -level 0 $results
- }
+ set details [dict get $options -errorcode]
+ if {[lindex $details 0] eq "CHILDSTATUS"} {
+ set status [lindex $details 2]
+ } else {
+ # Some kind of unexpected failure
+ }
}
.CE
.PP
-This is more easily written using the \fBtry\fR command, as that makes
-it simpler to trap specific types of errors. This is
-done using code like this:
-.PP
-.CS
-try {
- set results [\fBexec\fR grep foo bar.txt]
- set status 0
-} trap CHILDSTATUS {results options} {
- set status [lindex [dict get $options -errorcode] 2]
-}
-.CE
-.SS "WORKING WITH QUOTED ARGUMENTS"
-.PP
When translating a command from a Unix shell invocation, care should
be taken over the fact that single quote characters have no special
significance to Tcl. Thus:
-.PP
.CS
awk '{sum += $1} END {print sum}' numbers.list
.CE
-.PP
would be translated into something like:
-.PP
.CS
\fBexec\fR awk {{sum += $1} END {print sum}} numbers.list
.CE
-.SS "WORKING WITH GLOBBING"
.PP
If you are converting invocations involving shell globbing, you should
remember that Tcl does not handle globbing or expand things into
multiple arguments by default. Instead you should write things like
this:
-.PP
.CS
\fBexec\fR ls -l {*}[glob *.tcl]
.CE
-.SS "WORKING WITH USER-SUPPLIED SHELL SCRIPT FRAGMENTS"
-.PP
-One useful technique can be to expose to users of a script the ability
-to specify a fragment of shell script to execute that will have some
-data passed in on standard input that was produced by the Tcl program.
-This is a common technique for using the \fIlpr\fR program for
-printing. By far the simplest way of doing this is to pass the user's
-script to the user's shell for processing, as this avoids a lot of
-complexity with parsing other languages.
-.PP
-.CS
-set lprScript [\fIget from user...\fR]
-set postscriptData [\fIgenerate somehow...\fR]
-
-\fBexec\fR $env(SHELL) -c $lprScript << $postscriptData
-.CE
.SH "WINDOWS EXAMPLES"
-.PP
Here are some examples of the use of the \fBexec\fR command on Windows.
+.PP
To start an instance of \fInotepad\fR editing a file without waiting
for the user to finish editing the file:
-.PP
.CS
\fBexec\fR notepad myfile.txt &
.CE
.PP
To print a text file using \fInotepad\fR:
-.PP
.CS
\fBexec\fR notepad /p myfile.txt
.CE
-.SS "WORKING WITH CONSOLE PROGRAMS"
.PP
If a program calls other programs, such as is common with compilers,
then you may need to resort to batch files to hide the console windows
that sometimes pop up:
-.PP
.CS
\fBexec\fR cmp.bat somefile.c -o somefile
.CE
-.PP
With the file \fIcmp.bat\fR looking something like:
-.PP
-.CS
-@gcc %*
-.CE
-.PP
-or like another variant using single parameters:
-.PP
.CS
@gcc %1 %2 %3 %4 %5 %6 %7 %8 %9
.CE
-.SS "WORKING WITH COMMAND BUILT-INS"
.PP
Sometimes you need to be careful, as different programs may have the
same name and be in the path. It can then happen that typing a command
@@ -466,27 +431,10 @@ applies especially when you want to run
commands like
\fIdir\fR from a Tcl script (if you just want to list filenames, use
the \fBglob\fR command.) To do that, use this:
-.PP
.CS
\fBexec\fR {*}[auto_execok dir] *.tcl
.CE
-.SS "WORKING WITH NATIVE FILENAMES"
-.PP
-Many programs on Windows require filename arguments to be passed in with
-backslashes as pathname separators. This is done with the help of the
-\fBfile nativename\fR command. For example, to make a directory (on NTFS)
-encrypted so that only the current user can access it requires use of
-the \fICIPHER\fR command, like this:
-.PP
-.CS
-set secureDir "~/Desktop/Secure Directory"
-file mkdir $secureDir
-\fBexec\fR CIPHER /e /s:[file nativename $secureDir]
-.CE
.SH "SEE ALSO"
-error(n), file(n), open(n)
+error(n), open(n)
.SH KEYWORDS
execute, pipeline, redirection, subprocess
-'\" Local Variables:
-'\" mode: nroff
-'\" End: