Blame expect.man

Packit a69f91
.TH EXPECT 1 "29 December 1994"
Packit a69f91
.SH NAME
Packit a69f91
expect \- programmed dialogue with interactive programs, Version 5
Packit a69f91
.SH SYNOPSIS
Packit a69f91
.B expect
Packit a69f91
[
Packit a69f91
.B \-dDinN
Packit a69f91
]
Packit a69f91
[
Packit a69f91
.B \-c
Packit a69f91
.I cmds
Packit a69f91
]
Packit a69f91
[
Packit a69f91
[
Packit a69f91
.BR \- [ f | b ]
Packit a69f91
]
Packit a69f91
.I cmdfile
Packit a69f91
]
Packit a69f91
[
Packit a69f91
.I args
Packit a69f91
]
Packit a69f91
.SH INTRODUCTION
Packit a69f91
.B Expect
Packit a69f91
is a program that "talks" to other interactive programs according to a
Packit a69f91
script.  Following the script,
Packit a69f91
.B Expect
Packit a69f91
knows what can be expected from
Packit a69f91
a program and what the correct response should be.  An interpreted
Packit a69f91
language provides branching and high-level control structures to
Packit a69f91
direct the dialogue.  In addition, the user can take control
Packit a69f91
and interact directly when desired, afterward returning control to the
Packit a69f91
script.
Packit a69f91
.PP
Packit a69f91
.B Expectk
Packit a69f91
is a mixture of
Packit a69f91
.B Expect
Packit a69f91
and
Packit a69f91
.BR Tk .
Packit a69f91
It behaves just like
Packit a69f91
.B Expect
Packit a69f91
and
Packit a69f91
.BR Tk 's
Packit a69f91
.BR wish .
Packit a69f91
.B Expect
Packit a69f91
can also be used directly in C or C++ (that is, without Tcl).
Packit a69f91
See libexpect(3).
Packit a69f91
.PP
Packit a69f91
The name "Expect" comes from the idea of
Packit a69f91
.I send/expect
Packit a69f91
sequences popularized
Packit a69f91
by uucp, kermit and other modem control programs.
Packit a69f91
However unlike uucp,
Packit a69f91
.B Expect
Packit a69f91
is generalized so that it can be run as a user-level command
Packit a69f91
with any program and task in mind.
Packit a69f91
.B Expect
Packit a69f91
can actually talk to several programs at the same time.
Packit a69f91
.PP
Packit a69f91
For example, here are some things
Packit a69f91
.B Expect
Packit a69f91
can do:
Packit a69f91
.RS
Packit a69f91
.TP 4
Packit a69f91
\(bu
Packit a69f91
Cause your computer to dial you back,
Packit a69f91
so that you can login without paying for the call.
Packit a69f91
.TP
Packit a69f91
\(bu
Packit a69f91
Start a game (e.g., rogue) and if the optimal configuration doesn't appear,
Packit a69f91
restart it (again and again) until it does,
Packit a69f91
then hand over control to you.
Packit a69f91
.TP
Packit a69f91
\(bu
Packit a69f91
Run fsck, and in response to its questions, answer "yes", "no" or give control back to you,
Packit a69f91
based on predetermined criteria.
Packit a69f91
.TP
Packit a69f91
\(bu
Packit a69f91
Connect to another network or BBS (e.g., MCI Mail, CompuServe) and
Packit a69f91
automatically retrieve your mail so that it appears as if
Packit a69f91
it was originally sent to your local system.
Packit a69f91
.TP
Packit a69f91
\(bu
Packit a69f91
Carry environment variables, current directory,
Packit a69f91
or any kind of information across rlogin, telnet, tip, su, chgrp, etc.
Packit a69f91
.RE
Packit a69f91
.PP
Packit a69f91
There are a variety of reasons why the shell cannot perform these tasks.
Packit a69f91
(Try, you'll see.)
Packit a69f91
All are possible with
Packit a69f91
.BR Expect .
Packit a69f91
.PP
Packit a69f91
In general,
Packit a69f91
.B Expect
Packit a69f91
is useful for running any program which requires
Packit a69f91
interaction between the program and the user.
Packit a69f91
All that is necessary is that the interaction can be characterized
Packit a69f91
programmatically.
Packit a69f91
.B Expect
Packit a69f91
can also give the user back control
Packit a69f91
(without halting the program being controlled) if desired.
Packit a69f91
Similarly, the user can return control to the script at any time.
Packit a69f91
.SH USAGE
Packit a69f91
.B Expect
Packit a69f91
reads
Packit a69f91
.I cmdfile
Packit a69f91
for a list of commands to execute.
Packit a69f91
.B Expect
Packit a69f91
may also be invoked implicitly on systems which support the #! notation
Packit a69f91
by marking the script executable, and making the first line in your script:
Packit a69f91
Packit a69f91
    #!/usr/local/bin/expect \-f
Packit a69f91
Packit a69f91
Of course, the path must accurately describe where
Packit a69f91
.B Expect
Packit a69f91
lives.  /usr/local/bin is just an example.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-c
Packit a69f91
flag prefaces a command to be executed before any in the script.
Packit a69f91
The command should be quoted to prevent being broken up by the shell.
Packit a69f91
This option may be used multiple times.
Packit a69f91
Multiple commands may be
Packit a69f91
executed with a single
Packit a69f91
.B \-c
Packit a69f91
by separating them with semicolons.
Packit a69f91
Commands are executed in the order they appear.  
Packit a69f91
(When using Expectk, this option is specified as
Packit a69f91
.BR \-command .)
Packit a69f91
.PP
Packit a69f91
The
Packit a69f91
.B \-d
Packit a69f91
flag enables some diagnostic output, which
Packit a69f91
primarily reports internal activity of commands such as 
Packit a69f91
.B expect
Packit a69f91
and
Packit a69f91
.BR interact .
Packit a69f91
This flag has the same effect as "exp_internal 1" at the beginning of an Expect
Packit a69f91
script, plus the version of
Packit a69f91
.B Expect
Packit a69f91
is printed.
Packit a69f91
(The
Packit a69f91
.B strace
Packit a69f91
command is useful for tracing statements, and the
Packit a69f91
.B trace
Packit a69f91
command is useful for tracing variable assignments.)
Packit a69f91
(When using Expectk, this option is specified as
Packit a69f91
.BR \-diag .)
Packit a69f91
.PP
Packit a69f91
The
Packit a69f91
.B \-D
Packit a69f91
flag enables an interactive debugger.  An integer value should follow.
Packit a69f91
The debugger will take control before the next Tcl procedure
Packit a69f91
if the value is non-zero
Packit a69f91
or if a ^C is pressed (or a breakpoint is hit, or other appropriate debugger
Packit a69f91
command appears in the script).  See the README file or SEE ALSO (below)
Packit a69f91
for more information on the debugger.
Packit a69f91
(When using Expectk, this option is specified as
Packit a69f91
.BR \-Debug .)
Packit a69f91
.PP
Packit a69f91
The
Packit a69f91
.B \-f
Packit a69f91
flag prefaces a file from which to read commands from.
Packit a69f91
The flag itself is optional as it is only useful when using
Packit a69f91
the #! notation (see above),
Packit a69f91
so that other arguments may be supplied on the command line.
Packit a69f91
(When using Expectk, this option is specified as
Packit a69f91
.BR \-file .)
Packit a69f91
.PP
Packit a69f91
By default, the command file is read into memory and executed in its entirety.
Packit a69f91
It is occasionally desirable to read files one line at a time.  For example,
Packit a69f91
stdin is read this way.  In order to force arbitrary files to be handled this
Packit a69f91
way, use the
Packit a69f91
.B \-b
Packit a69f91
flag.
Packit a69f91
(When using Expectk, this option is specified as
Packit a69f91
.BR \-buffer .)
Packit a69f91
Note that stdio-buffering may still take place however this shouldn't cause
Packit a69f91
problems when reading from a fifo or stdin.
Packit a69f91
.PP
Packit a69f91
If the string "\-" is supplied as a filename, standard input is read instead.
Packit a69f91
(Use "./\-" to read from a file actually named "\-".)
Packit a69f91
.PP
Packit a69f91
The
Packit a69f91
.B \-i
Packit a69f91
flag causes
Packit a69f91
.B Expect
Packit a69f91
to interactively prompt for commands instead of reading
Packit a69f91
them from a file.
Packit a69f91
Prompting is terminated via the
Packit a69f91
.B exit
Packit a69f91
command or upon EOF.
Packit a69f91
See
Packit a69f91
.B interpreter
Packit a69f91
(below) for more information.
Packit a69f91
.B \-i
Packit a69f91
is assumed if neither a command file nor
Packit a69f91
.B \-c
Packit a69f91
is used.
Packit a69f91
(When using Expectk, this option is specified as
Packit a69f91
.BR \-interactive .)
Packit a69f91
.PP
Packit a69f91
.B \-\-
Packit a69f91
may be used to delimit the end of the options.  This is useful if
Packit a69f91
you want to pass an option-like argument to your script without it being
Packit a69f91
interpreted by
Packit a69f91
.BR Expect .
Packit a69f91
This can usefully be placed in the #! line to prevent any flag-like
Packit a69f91
interpretation by Expect.  For example, the following will leave the
Packit a69f91
original arguments (including the script name) in the variable
Packit a69f91
.IR argv .
Packit a69f91
Packit a69f91
    #!/usr/local/bin/expect \-\-
Packit a69f91
Packit a69f91
Note that the usual getopt(3) and execve(2) conventions must be observed
Packit a69f91
when adding arguments to the #! line.
Packit a69f91
.PP
Packit a69f91
The file $exp_library/expect.rc is sourced automatically if present, unless
Packit a69f91
the
Packit a69f91
.B \-N
Packit a69f91
flag is used.  
Packit a69f91
(When using Expectk, this option is specified as
Packit a69f91
.BR \-NORC .)
Packit a69f91
Immediately after this,
Packit a69f91
the file ~/.expect.rc is sourced automatically, unless the
Packit a69f91
.B \-n
Packit a69f91
flag is used.  If the environment variable DOTDIR is defined,
Packit a69f91
it is treated as a directory and .expect.rc is read from there.
Packit a69f91
(When using Expectk, this option is specified as
Packit a69f91
.BR \-norc .)
Packit a69f91
This sourcing occurs only after executing any
Packit a69f91
.B \-c
Packit a69f91
flags.
Packit a69f91
.PP
Packit a69f91
.B \-v
Packit a69f91
causes Expect to print its version number and exit.  (The corresponding flag
Packit a69f91
in Expectk, which uses long flag names, is \-version.)
Packit a69f91
.PP
Packit a69f91
Optional
Packit a69f91
.I args
Packit a69f91
are constructed into a list and stored in the variable named
Packit a69f91
.IR argv .
Packit a69f91
.I argc
Packit a69f91
is initialized to the length of argv.
Packit a69f91
.PP
Packit a69f91
.I argv0
Packit a69f91
is defined to be the name of the script (or binary if no script is used).
Packit a69f91
For example,
Packit a69f91
the following prints out the name of the script and the first three arguments:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    send_user "$argv0 [lrange $argv 0 2]\\n"
Packit a69f91
Packit a69f91
.fi
Packit a69f91
.SH COMMANDS
Packit a69f91
.B Expect
Packit a69f91
uses
Packit a69f91
.I Tcl
Packit a69f91
(Tool Command Language).
Packit a69f91
Tcl provides control flow (e.g., if, for, break),
Packit a69f91
expression evaluation and several other features such as recursion,
Packit a69f91
procedure definition, etc.
Packit a69f91
Commands used here but not defined (e.g.,
Packit a69f91
.BR set ,
Packit a69f91
.BR if ,
Packit a69f91
.BR exec )
Packit a69f91
are Tcl commands (see tcl(3)).
Packit a69f91
.B Expect
Packit a69f91
supports additional commands, described below.
Packit a69f91
Unless otherwise specified, commands return the empty string.
Packit a69f91
.PP
Packit a69f91
Commands are listed alphabetically so that they can be quickly located.
Packit a69f91
However, new users may find it easier to start by reading the descriptions
Packit a69f91
of
Packit a69f91
.BR spawn ,
Packit a69f91
.BR send ,
Packit a69f91
.BR expect ,
Packit a69f91
and
Packit a69f91
.BR interact ,
Packit a69f91
in that order.
Packit a69f91
Packit a69f91
Note that the best introduction to the language (both Expect and Tcl)
Packit a69f91
is provided in the book "Exploring Expect" (see SEE ALSO below).
Packit a69f91
Examples are included in this man page but they are very limited since
Packit a69f91
this man page is meant primarily as reference material.
Packit a69f91
Packit a69f91
Note that in the text of this man page, "Expect" with an uppercase "E"
Packit a69f91
refers to the
Packit a69f91
.B Expect
Packit a69f91
program while "expect" with a lower-case "e" refers to the
Packit a69f91
.B expect
Packit a69f91
command within the
Packit a69f91
.B Expect
Packit a69f91
program.)
Packit a69f91
.I
Packit a69f91
.TP 6
Packit a69f91
.BI close " [-slave] [\-onexec 0|1] [\-i spawn_id]"
Packit a69f91
closes the connection to the current process.
Packit a69f91
Most interactive programs will detect EOF on their stdin and exit;
Packit a69f91
thus
Packit a69f91
.B close
Packit a69f91
usually suffices to kill the process as well.
Packit a69f91
The
Packit a69f91
.B \-i
Packit a69f91
flag declares the process to close corresponding to the named spawn_id.
Packit a69f91
Packit a69f91
Both
Packit a69f91
.B expect
Packit a69f91
and
Packit a69f91
.B interact
Packit a69f91
will detect when the current process exits and implicitly do a
Packit a69f91
.BR close .
Packit a69f91
But if you kill the process by, say, "exec kill $pid",
Packit a69f91
you will need to explicitly call
Packit a69f91
.BR close .
Packit a69f91
Packit a69f91
The
Packit a69f91
.BR \-onexec
Packit a69f91
flag determines whether the spawn id will be closed in any new spawned
Packit a69f91
processes or if the process is overlayed.  To leave a spawn id open,
Packit a69f91
use the value 0.  A non-zero integer value will force the spawn closed
Packit a69f91
(the default) in any new processes.
Packit a69f91
Packit a69f91
The 
Packit a69f91
.B \-slave
Packit a69f91
flag closes the slave associated with the spawn id.  (See "spawn -pty".)
Packit a69f91
When the connection is closed, the slave is automatically closed as
Packit a69f91
well if still open.
Packit a69f91
Packit a69f91
No matter whether the connection is closed implicitly or explicitly,
Packit a69f91
you should call
Packit a69f91
.B wait
Packit a69f91
to clear up the corresponding kernel process slot.
Packit a69f91
.B close
Packit a69f91
does not call
Packit a69f91
.B wait
Packit a69f91
since there is no guarantee that closing a process connection will cause
Packit a69f91
it to exit.
Packit a69f91
See
Packit a69f91
.B wait
Packit a69f91
below for more info.
Packit a69f91
.TP
Packit a69f91
.BI debug " [[-now] 0|1]"
Packit a69f91
controls a Tcl debugger allowing you to step through statements, set
Packit a69f91
breakpoints, etc.
Packit a69f91
Packit a69f91
With no arguments, a 1 is returned if the debugger is not running, otherwise
Packit a69f91
a 0 is returned.
Packit a69f91
Packit a69f91
With a 1 argument, the debugger is started.  With a 0 argument, the
Packit a69f91
debugger is stopped.  If a 1 argument is preceded by the
Packit a69f91
.B \-now
Packit a69f91
flag, the debugger is started immediately (i.e., in the middle of the
Packit a69f91
.B debug
Packit a69f91
command itself).  Otherwise, the debugger is started with the next
Packit a69f91
Tcl statement.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B debug
Packit a69f91
command does not change any traps.  Compare this to starting Expect with the 
Packit a69f91
.B -D
Packit a69f91
flag (see above).
Packit a69f91
Packit a69f91
See the README file or SEE ALSO (below)
Packit a69f91
for more information on the debugger.
Packit a69f91
.TP
Packit a69f91
.B disconnect
Packit a69f91
disconnects a forked process from the terminal.  It continues running in the
Packit a69f91
background.  The process is given its own process group (if possible).
Packit a69f91
Standard I/O is redirected to /dev/null.
Packit a69f91
.IP
Packit a69f91
The following fragment uses
Packit a69f91
.B disconnect
Packit a69f91
to continue running the script in the background.  
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    if {[fork]!=0} exit
Packit a69f91
    disconnect
Packit a69f91
    . . .
Packit a69f91
Packit a69f91
.fi
Packit a69f91
The following script reads a password, and then runs a program
Packit a69f91
every hour that demands a password each time it is run.  The script supplies
Packit a69f91
the password so that you only have to type it once.
Packit a69f91
(See the
Packit a69f91
.B stty
Packit a69f91
command which demonstrates how to turn off password echoing.)
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    send_user "password?\\ "
Packit a69f91
    expect_user -re "(.*)\\n"
Packit a69f91
    for {} 1 {} {
Packit a69f91
        if {[fork]!=0} {sleep 3600;continue}
Packit a69f91
        disconnect
Packit a69f91
        spawn priv_prog
Packit a69f91
        expect Password:
Packit a69f91
        send "$expect_out(1,string)\\r"
Packit a69f91
        . . .
Packit a69f91
        exit
Packit a69f91
    }
Packit a69f91
Packit a69f91
.fi
Packit a69f91
An advantage to using
Packit a69f91
.B disconnect
Packit a69f91
over the shell asynchronous process feature (&) is that
Packit a69f91
.B Expect
Packit a69f91
can
Packit a69f91
save the terminal parameters prior to disconnection, and then later
Packit a69f91
apply them to new ptys.  With &,
Packit a69f91
.B Expect
Packit a69f91
does not have a chance
Packit a69f91
to read the terminal's parameters since the terminal is already
Packit a69f91
disconnected by the time
Packit a69f91
.B Expect
Packit a69f91
receives control.
Packit a69f91
.TP
Packit a69f91
.BI exit " [\-opts] [status]"
Packit a69f91
causes
Packit a69f91
.B Expect
Packit a69f91
to exit or otherwise prepare to do so.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-onexit
Packit a69f91
flag causes the next argument to be used as an exit handler.
Packit a69f91
Without an argument, the current exit handler is returned.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-noexit
Packit a69f91
flag causes
Packit a69f91
.B Expect
Packit a69f91
to prepare to exit but stop short of actually returning control to the
Packit a69f91
operating system.  The user-defined exit handler is run as well as Expect's
Packit a69f91
own internal handlers.
Packit a69f91
No further Expect commands should be executed.
Packit a69f91
This is useful if you are running Expect with other Tcl extensions.
Packit a69f91
The current interpreter (and main window if in the Tk environment) remain
Packit a69f91
so that other Tcl extensions can clean up.  If Expect's
Packit a69f91
.B exit
Packit a69f91
is called again (however this might occur), the handlers are not rerun.
Packit a69f91
Packit a69f91
Upon exiting, 
Packit a69f91
all connections to spawned processes are closed.  Closure will be detected
Packit a69f91
as an EOF by spawned processes.
Packit a69f91
.B exit
Packit a69f91
takes no other actions beyond what the normal _exit(2) procedure does.
Packit a69f91
Thus, spawned processes that do not check for EOF may continue to run.
Packit a69f91
(A variety of conditions are important to determining, for example, what
Packit a69f91
signals a spawned process will be sent, but these are system-dependent,
Packit a69f91
typically documented under exit(3).)
Packit a69f91
Spawned processes that continue to run will be inherited by init.
Packit a69f91
Packit a69f91
.I status
Packit a69f91
(or 0 if not specified) is returned as the exit status of
Packit a69f91
.BR Expect .
Packit a69f91
.B exit
Packit a69f91
is implicitly executed if the end of the script is reached.
Packit a69f91
.TP
Packit a69f91
\fBexp_continue\fR [-continue_timer]
Packit a69f91
The command
Packit a69f91
.B exp_continue
Packit a69f91
allows
Packit a69f91
.B expect
Packit a69f91
itself to continue
Packit a69f91
executing rather than returning as it normally would. By
Packit a69f91
default
Packit a69f91
.B exp_continue
Packit a69f91
resets the timeout timer. The
Packit a69f91
.I -continue_timer
Packit a69f91
flag prevents timer from being restarted. (See
Packit a69f91
.B expect
Packit a69f91
for more information.)
Packit a69f91
.TP
Packit a69f91
.BI exp_internal " [\-f file] value"
Packit a69f91
causes further commands to send diagnostic information internal to
Packit a69f91
.B Expect
Packit a69f91
to stderr if
Packit a69f91
.I value
Packit a69f91
is non-zero.  This output is disabled if
Packit a69f91
.I value
Packit a69f91
is 0.  The diagnostic information includes every character received,
Packit a69f91
and every attempt made to match the current output against the patterns.
Packit a69f91
.IP
Packit a69f91
If the optional
Packit a69f91
.I file
Packit a69f91
is supplied, all normal and debugging output is written to that file
Packit a69f91
(regardless of the value of
Packit a69f91
.IR value ).
Packit a69f91
Any previous diagnostic output file is closed.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-info
Packit a69f91
flag causes exp_internal to return a description of the
Packit a69f91
most recent non-info arguments given.
Packit a69f91
.TP
Packit a69f91
.BI exp_open " [args] [\-i spawn_id]"
Packit a69f91
returns a Tcl file identifier that corresponds to the original spawn id.
Packit a69f91
The file identifier can then be used as if it were opened by Tcl's
Packit a69f91
.B open
Packit a69f91
command.  (The spawn id should no longer be used.  A
Packit a69f91
.B wait
Packit a69f91
should not be executed.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-leaveopen
Packit a69f91
flag leaves the spawn id open for access through 
Packit a69f91
Expect commands.  A
Packit a69f91
.B wait
Packit a69f91
must be executed on the spawn id.
Packit a69f91
.TP
Packit a69f91
.BI exp_pid " [\-i spawn_id]"
Packit a69f91
returns the process id corresponding to the currently spawned process.
Packit a69f91
If the
Packit a69f91
.B \-i
Packit a69f91
flag is used, the pid returned corresponds to that of the given spawn id.
Packit a69f91
.TP
Packit a69f91
.B exp_send
Packit a69f91
is an alias for
Packit a69f91
.BR send .
Packit a69f91
.TP
Packit a69f91
.B exp_send_error
Packit a69f91
is an alias for
Packit a69f91
.BR send_error .
Packit a69f91
.TP
Packit a69f91
.B exp_send_log
Packit a69f91
is an alias for
Packit a69f91
.BR send_log .
Packit a69f91
.TP
Packit a69f91
.B exp_send_tty
Packit a69f91
is an alias for
Packit a69f91
.BR send_tty .
Packit a69f91
.TP
Packit a69f91
.B exp_send_user
Packit a69f91
is an alias for
Packit a69f91
.BR send_user .
Packit a69f91
.TP
Packit a69f91
.BI exp_version " [[\-exit] version]"
Packit a69f91
is useful for assuring that the script is compatible with the current
Packit a69f91
version of Expect.
Packit a69f91
.IP
Packit a69f91
With no arguments, the current version of
Packit a69f91
.B Expect
Packit a69f91
is returned.  This version
Packit a69f91
may then be encoded in your script.  If you actually know that you are not
Packit a69f91
using features of recent versions, you can specify an earlier version.
Packit a69f91
.IP
Packit a69f91
Versions consist of three numbers separated by dots.  First
Packit a69f91
is the major number.  Scripts written for versions of
Packit a69f91
.B Expect
Packit a69f91
with a
Packit a69f91
different major number will almost certainly not work.
Packit a69f91
.B exp_version 
Packit a69f91
returns an error if the major numbers do not match.
Packit a69f91
.IP
Packit a69f91
Second is the minor number.  Scripts written for a version with a
Packit a69f91
greater minor number than the current version
Packit a69f91
may depend upon some new feature and might not run.
Packit a69f91
.B exp_version
Packit a69f91
returns an error if the major numbers match, but the script minor number
Packit a69f91
is greater than that of the running
Packit a69f91
.BR Expect .
Packit a69f91
.IP
Packit a69f91
Third is a number that plays no part in the version comparison.
Packit a69f91
However, it is incremented when the
Packit a69f91
.B Expect
Packit a69f91
software
Packit a69f91
distribution is changed in any way, such as by additional documentation
Packit a69f91
or optimization.  It is reset to 0 upon each new minor version.
Packit a69f91
.IP
Packit a69f91
With the
Packit a69f91
.B \-exit
Packit a69f91
flag,
Packit a69f91
.B Expect
Packit a69f91
prints an error and exits if the version is out of date.
Packit a69f91
.TP
Packit a69f91
.BI expect " [[\-opts] pat1 body1] ... [\-opts] patn [bodyn]"
Packit a69f91
waits until one of the patterns matches the output of a spawned process,
Packit a69f91
a specified time period has passed, or an end-of-file is seen.
Packit a69f91
If the final body is empty, it may be omitted.
Packit a69f91
.IP
Packit a69f91
Patterns from the most recent
Packit a69f91
.B expect_before
Packit a69f91
command are implicitly used before any other patterns.
Packit a69f91
Patterns from the most recent
Packit a69f91
.B expect_after
Packit a69f91
command are implicitly used after any other patterns.
Packit a69f91
.IP
Packit a69f91
If the arguments to the entire
Packit a69f91
.B expect
Packit a69f91
statement require more than one line,
Packit a69f91
all the arguments may be "braced" into one so as to avoid terminating each
Packit a69f91
line with a backslash.  In this one case, the usual Tcl substitutions will
Packit a69f91
occur despite the braces.
Packit a69f91
.IP
Packit a69f91
If a pattern is the keyword
Packit a69f91
.BR eof ,
Packit a69f91
the corresponding body is executed upon end-of-file.
Packit a69f91
If a pattern is the keyword
Packit a69f91
.BR timeout ,
Packit a69f91
the corresponding body is executed upon timeout.  If no timeout keyword
Packit a69f91
is used, an implicit null action is executed upon timeout.
Packit a69f91
The default timeout period is 10 seconds but may be set, for example to 30,
Packit a69f91
by the command "set timeout 30".  An infinite timeout may be designated
Packit a69f91
by the value \-1.
Packit a69f91
If a pattern is the keyword
Packit a69f91
.BR default ,
Packit a69f91
the corresponding body is executed upon either timeout or end-of-file.
Packit a69f91
.IP
Packit a69f91
If a pattern matches, then the corresponding body is executed.
Packit a69f91
.B expect
Packit a69f91
returns the result of the body (or the empty string if no pattern matched).
Packit a69f91
In the event that multiple patterns match, the one appearing first is
Packit a69f91
used to select a body.
Packit a69f91
.IP
Packit a69f91
Each time new output arrives, it is compared to each pattern in the order
Packit a69f91
they are listed.  Thus, you may test for absence of a match by making
Packit a69f91
the last pattern something guaranteed to appear, such as a prompt.
Packit a69f91
In situations where there is no prompt, you must use
Packit a69f91
.B timeout
Packit a69f91
(just like you would if you were interacting manually).
Packit a69f91
.IP
Packit a69f91
Patterns are specified in three ways.  By default, 
Packit a69f91
patterns are specified as with Tcl's
Packit a69f91
.B string match
Packit a69f91
command.  (Such patterns are also similar to C-shell regular expressions
Packit a69f91
usually referred to as "glob" patterns).  The
Packit a69f91
.B \-gl
Packit a69f91
flag may may
Packit a69f91
be used to protect patterns that might otherwise match
Packit a69f91
.B expect
Packit a69f91
flags from doing so.
Packit a69f91
Any pattern beginning with a "-" should be protected this way.  (All strings
Packit a69f91
starting with "-" are reserved for future options.)
Packit a69f91
Packit a69f91
.IP
Packit a69f91
For example, the following fragment looks for a successful login.
Packit a69f91
(Note that
Packit a69f91
.B abort
Packit a69f91
is presumed to be a procedure defined elsewhere in the script.)
Packit a69f91
.nf
Packit a69f91
Packit a69f91
.ta \w'      expect 'u +\w'invalid password    'u
Packit a69f91
    expect {
Packit a69f91
        busy               {puts busy\\n ; exp_continue}
Packit a69f91
        failed             abort
Packit a69f91
        "invalid password" abort
Packit a69f91
        timeout            abort
Packit a69f91
        connected
Packit a69f91
    }
Packit a69f91
Packit a69f91
.fi
Packit a69f91
Quotes are necessary on the fourth pattern since it contains a space, which
Packit a69f91
would otherwise separate the pattern from the action.
Packit a69f91
Patterns with the same action (such as the 3rd and 4th) require listing the
Packit a69f91
actions again.  This can be avoid by using regexp-style patterns (see below).
Packit a69f91
More information on forming glob-style patterns can be found in the Tcl manual.
Packit a69f91
.IP
Packit a69f91
Regexp-style patterns follow the syntax defined by Tcl's
Packit a69f91
.B regexp
Packit a69f91
(short for "regular expression") command.
Packit a69f91
regexp patterns are introduced with the flag
Packit a69f91
.BR \-re .
Packit a69f91
The previous example can be rewritten using a regexp as:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
.ta \w'      expect 'u +\w'connected    'u
Packit a69f91
    expect {
Packit a69f91
        busy       {puts busy\\n ; exp_continue}
Packit a69f91
        \-re "failed|invalid password" abort
Packit a69f91
        timeout    abort
Packit a69f91
        connected
Packit a69f91
    }
Packit a69f91
Packit a69f91
.fi
Packit a69f91
Both types of patterns are "unanchored".  This means that patterns
Packit a69f91
do not have to match the entire string, but can begin and end the
Packit a69f91
match anywhere in the string (as long as everything else matches).
Packit a69f91
Use ^ to match the beginning of a string, and $ to match the end.
Packit a69f91
Note that if you do not wait for the end of a string, your responses
Packit a69f91
can easily end up in the middle of the string as they are echoed from
Packit a69f91
the spawned process.  While still producing correct results, the output
Packit a69f91
can look unnatural.  Thus, use of $ is encouraged if you can exactly
Packit a69f91
describe the characters at the end of a string.
Packit a69f91
Packit a69f91
Note that in many editors, the ^ and $ match the beginning and end of
Packit a69f91
lines respectively. However, because expect is not line oriented,
Packit a69f91
these characters match the beginning and end of the data (as opposed
Packit a69f91
to lines) currently in the expect matching buffer.  (Also, see the
Packit a69f91
note below on "system indigestion.")
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-ex
Packit a69f91
flag causes the pattern to be matched as an "exact" string.  No
Packit a69f91
interpretation of *, ^, etc is made (although the usual Tcl
Packit a69f91
conventions must still be observed).
Packit a69f91
Exact patterns are always unanchored.
Packit a69f91
Packit a69f91
.IP
Packit a69f91
The
Packit a69f91
.B \-nocase
Packit a69f91
flag causes uppercase characters of the output to compare as if they were
Packit a69f91
lowercase characters.  The pattern is not affected.
Packit a69f91
.IP
Packit a69f91
While reading output,
Packit a69f91
more than 2000 bytes can force earlier bytes to be "forgotten".
Packit a69f91
This may be changed with the function
Packit a69f91
.BR match_max .
Packit a69f91
(Note that excessively large values can slow down the pattern matcher.)
Packit a69f91
If
Packit a69f91
.I patlist
Packit a69f91
is
Packit a69f91
.BR full_buffer ,
Packit a69f91
the corresponding body is executed if
Packit a69f91
.I match_max
Packit a69f91
bytes have been received and no other patterns have matched.
Packit a69f91
Whether or not the
Packit a69f91
.B full_buffer
Packit a69f91
keyword is used, the forgotten characters are written to
Packit a69f91
expect_out(buffer).
Packit a69f91
Packit a69f91
If
Packit a69f91
.I patlist
Packit a69f91
is the keyword
Packit a69f91
.BR null ,
Packit a69f91
and nulls are allowed (via the
Packit a69f91
.B remove_nulls
Packit a69f91
command), the corresponding body is executed if a single ASCII
Packit a69f91
0 is matched.
Packit a69f91
It is not possible to
Packit a69f91
match 0 bytes via glob or regexp patterns.
Packit a69f91
Packit a69f91
Upon matching a pattern (or eof or full_buffer),
Packit a69f91
any matching and previously unmatched output is saved in the variable
Packit a69f91
.IR expect_out(buffer) .
Packit a69f91
Up to 9 regexp substring matches are saved in the variables
Packit a69f91
.I expect_out(1,string)
Packit a69f91
through
Packit a69f91
.IR expect_out(9,string) .
Packit a69f91
If the
Packit a69f91
.B -indices
Packit a69f91
flag is used before a pattern,
Packit a69f91
the starting and ending indices (in a form suitable for
Packit a69f91
.BR lrange )
Packit a69f91
of the
Packit a69f91
10 strings are stored in the variables
Packit a69f91
.I expect_out(X,start)
Packit a69f91
and
Packit a69f91
.I expect_out(X,end)
Packit a69f91
where X is a digit, corresponds to the substring position in the buffer.
Packit a69f91
0 refers to strings which matched the entire pattern
Packit a69f91
and is generated for glob patterns as well as regexp patterns.
Packit a69f91
For example, if a process has produced output of "abcdefgh\\n", the result of:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    expect "cd"
Packit a69f91
Packit a69f91
.fi
Packit a69f91
is as if the following statements had executed:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    set expect_out(0,string) cd
Packit a69f91
    set expect_out(buffer) abcd
Packit a69f91
Packit a69f91
.fi
Packit a69f91
and "efgh\\n" is left in the output buffer.
Packit a69f91
If a process produced the output "abbbcabkkkka\\n", the result of:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    expect \-indices \-re "b(b*).*(k+)"
Packit a69f91
Packit a69f91
.fi
Packit a69f91
is as if the following statements had executed:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    set expect_out(0,start) 1
Packit a69f91
    set expect_out(0,end) 10
Packit a69f91
    set expect_out(0,string) bbbcabkkkk
Packit a69f91
    set expect_out(1,start) 2
Packit a69f91
    set expect_out(1,end) 3
Packit a69f91
    set expect_out(1,string) bb
Packit a69f91
    set expect_out(2,start) 10
Packit a69f91
    set expect_out(2,end) 10
Packit a69f91
    set expect_out(2,string) k
Packit a69f91
    set expect_out(buffer) abbbcabkkkk
Packit a69f91
Packit a69f91
.fi
Packit a69f91
and "a\\n" is left in the output buffer.  The pattern "*" (and -re ".*") will
Packit a69f91
flush the output buffer without reading any more output from the
Packit a69f91
process.
Packit a69f91
.IP
Packit a69f91
Normally, the matched output is discarded from Expect's internal buffers.
Packit a69f91
This may be prevented by prefixing a pattern with the
Packit a69f91
.B \-notransfer
Packit a69f91
flag.  This flag is especially useful in experimenting (and can be
Packit a69f91
abbreviated to "-not" for convenience while experimenting).
Packit a69f91
Packit a69f91
The spawn id associated with the matching output (or eof or
Packit a69f91
full_buffer) is stored in
Packit a69f91
.IR expect_out(spawn_id) .
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-timeout
Packit a69f91
flag causes the current expect command to use the following value
Packit a69f91
as a timeout instead of using the value of the timeout variable.
Packit a69f91
Packit a69f91
By default, 
Packit a69f91
patterns are matched against output from the current process, however the
Packit a69f91
.B \-i
Packit a69f91
flag declares the output from the named spawn_id list be matched against
Packit a69f91
any following patterns (up to the next
Packit a69f91
.BR \-i ).
Packit a69f91
The spawn_id list should either be a whitespace separated list of spawn_ids
Packit a69f91
or a variable referring to such a list of spawn_ids.
Packit a69f91
Packit a69f91
For example, the following example waits for
Packit a69f91
"connected" from the current process, or "busy", "failed" or "invalid
Packit a69f91
password" from the spawn_id named by $proc2.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    expect {
Packit a69f91
        \-i $proc2 busy {puts busy\\n ; exp_continue}
Packit a69f91
        \-re "failed|invalid password" abort
Packit a69f91
        timeout abort
Packit a69f91
        connected
Packit a69f91
    }
Packit a69f91
Packit a69f91
.fi
Packit a69f91
The value of the global variable
Packit a69f91
.I any_spawn_id
Packit a69f91
may be used to match patterns to any spawn_ids that are named
Packit a69f91
with all other
Packit a69f91
.B \-i
Packit a69f91
flags in the current
Packit a69f91
.B expect
Packit a69f91
command.
Packit a69f91
The spawn_id from a
Packit a69f91
.B \-i
Packit a69f91
flag with no associated pattern (i.e., followed immediately
Packit a69f91
by another
Packit a69f91
.BR \-i )
Packit a69f91
is made available to any other patterns
Packit a69f91
in the same
Packit a69f91
.B expect
Packit a69f91
command associated with
Packit a69f91
.I any_spawn_id.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-i
Packit a69f91
flag may also name a global variable in which case the variable is read
Packit a69f91
for a list of spawn ids.  The variable is reread whenever it changes.
Packit a69f91
This provides a way of changing the I/O source while the command is in
Packit a69f91
execution.  Spawn ids provided this way are called "indirect" spawn ids.
Packit a69f91
Packit a69f91
Actions such as
Packit a69f91
.B break
Packit a69f91
and
Packit a69f91
.B continue
Packit a69f91
cause control structures (i.e.,
Packit a69f91
.BR for ,
Packit a69f91
.BR proc )
Packit a69f91
to behave in the usual way.
Packit a69f91
The command
Packit a69f91
.B exp_continue
Packit a69f91
allows
Packit a69f91
.B expect
Packit a69f91
itself to continue
Packit a69f91
executing rather than returning as it normally would.
Packit a69f91
.IP
Packit a69f91
This is useful for avoiding explicit loops or repeated expect statements.
Packit a69f91
The following example is part of a fragment to automate rlogin.  The
Packit a69f91
.B exp_continue
Packit a69f91
avoids having to write a second
Packit a69f91
.B expect
Packit a69f91
statement (to look for the prompt again) if the rlogin prompts for a password.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    expect {
Packit a69f91
        Password: {
Packit a69f91
            stty -echo
Packit a69f91
            send_user "password (for $user) on $host: "
Packit a69f91
            expect_user -re "(.*)\\n"
Packit a69f91
            send_user "\\n"
Packit a69f91
            send "$expect_out(1,string)\\r"
Packit a69f91
            stty echo
Packit a69f91
            exp_continue
Packit a69f91
        } incorrect {
Packit a69f91
            send_user "invalid password or account\\n"
Packit a69f91
            exit
Packit a69f91
        } timeout {
Packit a69f91
            send_user "connection to $host timed out\\n"
Packit a69f91
            exit
Packit a69f91
        } eof {
Packit a69f91
            send_user \\
Packit a69f91
                "connection to host failed: $expect_out(buffer)"
Packit a69f91
            exit
Packit a69f91
        } -re $prompt
Packit a69f91
    }
Packit a69f91
Packit a69f91
.fi
Packit a69f91
For example, the following fragment might help a user guide
Packit a69f91
an interaction that is already totally automated.  In this case, the terminal
Packit a69f91
is put into raw mode.  If the user presses "+", a variable is incremented.
Packit a69f91
If "p" is pressed, several returns are sent to the process,
Packit a69f91
perhaps to poke it in some way, and "i" lets the user interact with the
Packit a69f91
process, effectively stealing away control from the script.
Packit a69f91
In each case, the
Packit a69f91
.B exp_continue
Packit a69f91
allows the current
Packit a69f91
.B expect
Packit a69f91
to continue pattern matching after executing the
Packit a69f91
current action.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    stty raw \-echo
Packit a69f91
    expect_after {
Packit a69f91
        \-i $user_spawn_id
Packit a69f91
        "p" {send "\\r\\r\\r"; exp_continue}
Packit a69f91
        "+" {incr foo; exp_continue}
Packit a69f91
        "i" {interact; exp_continue}
Packit a69f91
        "quit" exit
Packit a69f91
    }
Packit a69f91
Packit a69f91
.fi
Packit a69f91
.IP
Packit a69f91
By default,
Packit a69f91
.B exp_continue
Packit a69f91
resets the timeout timer.  The timer is not restarted, if
Packit a69f91
.B exp_continue
Packit a69f91
is called with the 
Packit a69f91
.B \-continue_timer
Packit a69f91
flag.
Packit a69f91
.TP
Packit a69f91
.BI expect_after " [expect_args]"
Packit a69f91
works identically to the
Packit a69f91
.B expect_before
Packit a69f91
except that if patterns from both
Packit a69f91
.B expect
Packit a69f91
and
Packit a69f91
.B expect_after
Packit a69f91
can match, the
Packit a69f91
.B expect
Packit a69f91
pattern is used.  See the
Packit a69f91
.B expect_before
Packit a69f91
command for more information.
Packit a69f91
.TP
Packit a69f91
.BI expect_background " [expect_args]"
Packit a69f91
takes the same arguments as
Packit a69f91
.BR expect ,
Packit a69f91
however it returns immediately.
Packit a69f91
Patterns are tested whenever new input arrives.
Packit a69f91
The pattern
Packit a69f91
.B timeout
Packit a69f91
and
Packit a69f91
.B default
Packit a69f91
are meaningless to
Packit a69f91
.BR expect_background
Packit a69f91
and are silently discarded.
Packit a69f91
Otherwise, the
Packit a69f91
.B expect_background
Packit a69f91
command uses
Packit a69f91
.B expect_before
Packit a69f91
and
Packit a69f91
.B expect_after
Packit a69f91
patterns just like
Packit a69f91
.B expect
Packit a69f91
does.
Packit a69f91
Packit a69f91
When
Packit a69f91
.B expect_background
Packit a69f91
actions are being evaluated, background processing for the same
Packit a69f91
spawn id is blocked.  Background processing is unblocked when
Packit a69f91
the action completes.  While background processing is blocked,
Packit a69f91
it is possible to do a (foreground)
Packit a69f91
.B expect
Packit a69f91
on the same spawn id.
Packit a69f91
Packit a69f91
It is not possible to execute an
Packit a69f91
.B expect
Packit a69f91
while an
Packit a69f91
.B expect_background
Packit a69f91
is unblocked.
Packit a69f91
.B expect_background
Packit a69f91
for a particular spawn id is deleted by
Packit a69f91
declaring a new expect_background with the same spawn id.  Declaring
Packit a69f91
.B expect_background
Packit a69f91
with no pattern removes the given spawn id
Packit a69f91
from the ability to match patterns in the background.
Packit a69f91
.TP
Packit a69f91
.BI expect_before " [expect_args]"
Packit a69f91
takes the same arguments as
Packit a69f91
.BR expect ,
Packit a69f91
however it returns immediately.
Packit a69f91
Pattern-action pairs from the most recent
Packit a69f91
.B expect_before
Packit a69f91
with the same spawn id are implicitly added to any following
Packit a69f91
.B expect
Packit a69f91
commands.  If a pattern matches, it is treated as if it had been
Packit a69f91
specified in the
Packit a69f91
.B expect
Packit a69f91
command itself, and the associated body is executed in the context
Packit a69f91
of the
Packit a69f91
.B expect
Packit a69f91
command.
Packit a69f91
If patterns from both
Packit a69f91
.B expect_before
Packit a69f91
and
Packit a69f91
.B expect
Packit a69f91
can match, the
Packit a69f91
.B expect_before
Packit a69f91
pattern is used.
Packit a69f91
Packit a69f91
If no pattern is specified, the spawn id is not checked for any patterns.
Packit a69f91
Packit a69f91
Unless overridden by a
Packit a69f91
.B \-i
Packit a69f91
flag,
Packit a69f91
.B expect_before
Packit a69f91
patterns match against the spawn id defined at the time that the 
Packit a69f91
.B expect_before
Packit a69f91
command was executed (not when its pattern is matched).
Packit a69f91
Packit a69f91
The \-info flag causes
Packit a69f91
.B expect_before
Packit a69f91
to return the current specifications of what patterns it will match.
Packit a69f91
By default, it reports on the current spawn id.  An optional spawn id specification may be given for information on that spawn id.  For example
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    expect_before -info -i $proc
Packit a69f91
Packit a69f91
.fi
Packit a69f91
At most one spawn id specification may be given.  The flag \-indirect
Packit a69f91
suppresses direct spawn ids that come only from indirect specifications.
Packit a69f91
Packit a69f91
Instead of a spawn id specification, the flag "-all" will cause
Packit a69f91
"-info" to report on all spawn ids.
Packit a69f91
Packit a69f91
The output of the \-info flag can be reused as the argument to expect_before.
Packit a69f91
.TP
Packit a69f91
.BI expect_tty " [expect_args]"
Packit a69f91
is like
Packit a69f91
.B expect
Packit a69f91
but it reads characters from /dev/tty (i.e. keystrokes from the user).
Packit a69f91
By default, reading is performed in cooked mode.
Packit a69f91
Thus, lines must end with a return in order for
Packit a69f91
.B expect
Packit a69f91
to see them.
Packit a69f91
This may be changed via
Packit a69f91
.B stty
Packit a69f91
(see the
Packit a69f91
.B stty
Packit a69f91
command below).
Packit a69f91
.TP
Packit a69f91
.BI expect_user " [expect_args]"
Packit a69f91
is like
Packit a69f91
.B expect
Packit a69f91
but it reads characters from stdin (i.e. keystrokes from the user).
Packit a69f91
By default, reading is performed in cooked mode.
Packit a69f91
Thus, lines must end with a return in order for
Packit a69f91
.B expect
Packit a69f91
to see them.
Packit a69f91
This may be changed via
Packit a69f91
.B stty
Packit a69f91
(see the
Packit a69f91
.B stty
Packit a69f91
command below).
Packit a69f91
.TP
Packit a69f91
.B fork
Packit a69f91
creates a new process.  The new process is an exact copy of the current
Packit a69f91
.B Expect
Packit a69f91
process.  On success,
Packit a69f91
.B fork
Packit a69f91
returns 0 to the new (child) process and returns the process ID of the child
Packit a69f91
process to the parent process.
Packit a69f91
On failure (invariably due to lack of resources, e.g., swap space, memory),
Packit a69f91
.B fork
Packit a69f91
returns \-1 to the parent process, and no child process is created.
Packit a69f91
.IP
Packit a69f91
Forked processes exit via the
Packit a69f91
.B exit
Packit a69f91
command, just like the original process.
Packit a69f91
Forked processes are allowed to write to the log files.  If you do not
Packit a69f91
disable debugging or logging in most of the processes, the result can be
Packit a69f91
confusing.
Packit a69f91
.IP
Packit a69f91
Some pty implementations may be confused by multiple readers and writers,
Packit a69f91
even momentarily.  Thus, it is safest to
Packit a69f91
.B fork
Packit a69f91
before spawning processes.
Packit a69f91
.TP
Packit a69f91
.BI interact " [string1 body1] ... [stringn [bodyn]]"
Packit a69f91
gives control of the current process to the user, so that
Packit a69f91
keystrokes are sent to the current process,
Packit a69f91
and the stdout and stderr of the current process are returned.
Packit a69f91
.IP
Packit a69f91
String-body pairs may be specified as arguments, in which case the
Packit a69f91
body is executed when the corresponding string is entered.  (By default, the
Packit a69f91
string is not sent to the current process.)   The
Packit a69f91
.B interpreter
Packit a69f91
command is assumed, if the final body is missing.
Packit a69f91
.IP
Packit a69f91
If the arguments to the entire
Packit a69f91
.B interact
Packit a69f91
statement require more than one line,
Packit a69f91
all the arguments may be "braced" into one so as to avoid terminating each
Packit a69f91
line with a backslash.  In this one case, the usual Tcl substitutions will
Packit a69f91
occur despite the braces.
Packit a69f91
.IP
Packit a69f91
For example, the following command runs interact with the following
Packit a69f91
string-body pairs defined:  When ^Z is pressed,
Packit a69f91
.B Expect
Packit a69f91
is suspended.
Packit a69f91
(The
Packit a69f91
.B \-reset
Packit a69f91
flag restores the terminal modes.)
Packit a69f91
When ^A is pressed, the user sees "you typed a control-A" and the
Packit a69f91
process is sent a ^A.  When $ is pressed, the user sees the date.
Packit a69f91
When ^C is pressed,
Packit a69f91
.B Expect
Packit a69f91
exits.  If "foo" is entered, the user sees "bar".
Packit a69f91
When ~~ is pressed, the
Packit a69f91
.B Expect
Packit a69f91
interpreter runs interactively.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
.ta \w'    interact 'u +\w'$CTRLZ  'u +\w'{'u
Packit a69f91
    set CTRLZ \\032
Packit a69f91
    interact {
Packit a69f91
        -reset $CTRLZ {exec kill \-STOP [pid]}
Packit a69f91
        \\001   {send_user "you typed a control\-A\\n";
Packit a69f91
                send "\\001"
Packit a69f91
               }
Packit a69f91
        $      {send_user "The date is [clock format [clock seconds]]."}
Packit a69f91
        \\003   exit
Packit a69f91
        foo    {send_user "bar"}
Packit a69f91
        ~~
Packit a69f91
    }
Packit a69f91
Packit a69f91
.fi
Packit a69f91
.IP
Packit a69f91
In string-body pairs, strings are matched in the order they are listed
Packit a69f91
as arguments.  Strings that partially match are not sent to the
Packit a69f91
current process in anticipation of the remainder coming.  If
Packit a69f91
characters are then entered such that there can no longer possibly be
Packit a69f91
a match, only the part of the string will be sent to the process that cannot
Packit a69f91
possibly begin another match.  Thus, strings that are substrings of
Packit a69f91
partial matches can match later, if the original strings that was attempting
Packit a69f91
to be match ultimately fails.
Packit a69f91
.IP
Packit a69f91
By default, string matching is exact with no wild cards.  (In contrast,
Packit a69f91
the
Packit a69f91
.B expect
Packit a69f91
command uses glob-style patterns by default.)  The
Packit a69f91
.B \-ex
Packit a69f91
flag may be used to protect patterns that might otherwise match
Packit a69f91
.B interact
Packit a69f91
flags from doing so.
Packit a69f91
Any pattern beginning with a "-" should be protected this way.    (All strings
Packit a69f91
starting with "-" are reserved for future options.)
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-re
Packit a69f91
flag forces the string to be interpreted as a regexp-style pattern.  In this
Packit a69f91
case, matching substrings are stored in the variable
Packit a69f91
.I interact_out
Packit a69f91
similarly to the way
Packit a69f91
.B expect
Packit a69f91
stores its output in the variable
Packit a69f91
.BR expect_out .
Packit a69f91
The
Packit a69f91
.B \-indices
Packit a69f91
flag is similarly supported.
Packit a69f91
Packit a69f91
The pattern
Packit a69f91
.B eof
Packit a69f91
introduces an action that is 
Packit a69f91
executed upon end-of-file.  A separate
Packit a69f91
.B eof
Packit a69f91
pattern may also follow the
Packit a69f91
.B \-output
Packit a69f91
flag in which case it is matched if an eof is detected while writing output.
Packit a69f91
The default
Packit a69f91
.B eof
Packit a69f91
action is "return", so that
Packit a69f91
.B interact
Packit a69f91
simply returns upon any EOF.
Packit a69f91
Packit a69f91
The pattern
Packit a69f91
.B timeout
Packit a69f91
introduces a timeout (in seconds) and action that is executed
Packit a69f91
after no characters have been read for a given time.
Packit a69f91
The
Packit a69f91
.B timeout
Packit a69f91
pattern applies to the most recently specified process.
Packit a69f91
There is no default timeout.
Packit a69f91
The special variable "timeout" (used by the
Packit a69f91
.B expect
Packit a69f91
command) has no affect on this timeout.
Packit a69f91
Packit a69f91
For example, the following statement could be used to autologout users who have
Packit a69f91
not typed anything for an hour but who still get frequent system
Packit a69f91
messages:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    interact -input $user_spawn_id timeout 3600 return -output \\
Packit a69f91
        $spawn_id 
Packit a69f91
Packit a69f91
.fi
Packit a69f91
Packit a69f91
If the pattern is the keyword
Packit a69f91
.BR null ,
Packit a69f91
and nulls are allowed (via the
Packit a69f91
.B remove_nulls
Packit a69f91
command), the corresponding body is executed if a single ASCII
Packit a69f91
0 is matched.
Packit a69f91
It is not possible to
Packit a69f91
match 0 bytes via glob or regexp patterns.
Packit a69f91
Packit a69f91
Prefacing a pattern with the flag
Packit a69f91
.B \-iwrite
Packit a69f91
causes the variable
Packit a69f91
.I interact_out(spawn_id)
Packit a69f91
to be set to the spawn_id which matched the pattern
Packit a69f91
(or eof).
Packit a69f91
Packit a69f91
Actions such as
Packit a69f91
.B break
Packit a69f91
and
Packit a69f91
.B continue
Packit a69f91
cause control structures (i.e.,
Packit a69f91
.BR for ,
Packit a69f91
.BR proc )
Packit a69f91
to behave in the usual way.
Packit a69f91
However
Packit a69f91
.B return
Packit a69f91
causes interact to return to its caller, while
Packit a69f91
.B inter_return
Packit a69f91
causes
Packit a69f91
.B interact
Packit a69f91
to cause a return in its caller.  For example, if "proc foo" called
Packit a69f91
.B interact
Packit a69f91
which then executed the action
Packit a69f91
.BR inter_return ,
Packit a69f91
.B proc foo
Packit a69f91
would return.  (This means that if
Packit a69f91
.B interact
Packit a69f91
calls
Packit a69f91
.B interpreter
Packit a69f91
interactively typing
Packit a69f91
.B return
Packit a69f91
will cause the interact to continue, while
Packit a69f91
.B inter_return
Packit a69f91
will cause the interact to return to its caller.)
Packit a69f91
.IP
Packit a69f91
During
Packit a69f91
.BR interact ,
Packit a69f91
raw mode is used so that all characters may be passed to the current process.
Packit a69f91
If the current process does not catch job control signals,
Packit a69f91
it will stop if sent a stop signal (by default ^Z).
Packit a69f91
To restart it, send a continue signal (such as by "kill \-CONT <pid>").
Packit a69f91
If you really want to send a SIGSTOP to such a process (by ^Z),
Packit a69f91
consider spawning csh first and then running your program.
Packit a69f91
On the other hand, if you want to send a SIGSTOP to
Packit a69f91
.B Expect
Packit a69f91
itself, first call interpreter (perhaps by using an escape character), and then press ^Z.
Packit a69f91
.IP
Packit a69f91
String-body pairs can be used as a shorthand for avoiding having
Packit a69f91
to enter the interpreter and execute commands interactively.  The previous
Packit a69f91
terminal mode is used while the body of a string-body pair is being executed.
Packit a69f91
.IP
Packit a69f91
For speed, actions execute in raw mode by default.  The
Packit a69f91
.B \-reset
Packit a69f91
flag resets the terminal to the mode it had before
Packit a69f91
.B interact
Packit a69f91
was executed (invariably, cooked mode).
Packit a69f91
Note that characters entered when the mode is being switched may be lost
Packit a69f91
(an unfortunate feature of the terminal driver on some systems).
Packit a69f91
The only reason to use
Packit a69f91
.B \-reset
Packit a69f91
is if your action
Packit a69f91
depends on running in cooked mode.
Packit a69f91
.IP
Packit a69f91
The
Packit a69f91
.B \-echo
Packit a69f91
flag sends characters that match the following pattern back to the process
Packit a69f91
that generated them as each character is read.  This may be useful
Packit a69f91
when the user needs to see feedback from partially typed patterns.
Packit a69f91
.IP
Packit a69f91
If a pattern is being echoed but eventually fails to match,
Packit a69f91
the characters are sent to the spawned process.  If the spawned
Packit a69f91
process then echoes them, the user will see the characters twice.
Packit a69f91
.B \-echo
Packit a69f91
is probably only appropriate in situations where the user is
Packit a69f91
unlikely to not complete the pattern.  For example, the following
Packit a69f91
excerpt is from rftp, the recursive-ftp script, where the user is
Packit a69f91
prompted to enter ~g, ~p, or ~l, to get, put, or list the current
Packit a69f91
directory recursively.  These are so far away from the normal ftp
Packit a69f91
commands, that the user is unlikely to type ~ followed by anything
Packit a69f91
else, except mistakenly, in which case, they'll probably just ignore
Packit a69f91
the result anyway.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    interact {
Packit a69f91
        -echo ~g {getcurdirectory 1}
Packit a69f91
        -echo ~l {getcurdirectory 0}
Packit a69f91
        -echo ~p {putcurdirectory}
Packit a69f91
    }
Packit a69f91
Packit a69f91
.fi
Packit a69f91
The
Packit a69f91
.B \-nobuffer
Packit a69f91
flag sends characters that match the following pattern on to
Packit a69f91
the output process as characters are read.
Packit a69f91
Packit a69f91
This is useful when you wish to let a program echo back the pattern.
Packit a69f91
For example, the following might be used to monitor where a person is
Packit a69f91
dialing (a Hayes-style modem).  Each time "atd" is seen the script
Packit a69f91
logs the rest of the line.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    proc lognumber {} {
Packit a69f91
        interact -nobuffer -re "(.*)\\r" return
Packit a69f91
        puts $log "[clock format [clock seconds]]: dialed $interact_out(1,string)"
Packit a69f91
    }
Packit a69f91
Packit a69f91
    interact -nobuffer "atd" lognumber
Packit a69f91
Packit a69f91
.fi
Packit a69f91
.IP
Packit a69f91
During
Packit a69f91
.BR interact ,
Packit a69f91
previous use of
Packit a69f91
.B log_user
Packit a69f91
is ignored.  In particular,
Packit a69f91
.B interact
Packit a69f91
will force its output to be logged (sent to the standard output)
Packit a69f91
since it is presumed the user doesn't wish to interact blindly.
Packit a69f91
.IP
Packit a69f91
The
Packit a69f91
.B \-o
Packit a69f91
flag causes any following key-body pairs to be applied to the output of
Packit a69f91
the current process.
Packit a69f91
This can be useful, for example, when dealing with hosts that
Packit a69f91
send unwanted characters during a telnet session.  
Packit a69f91
.IP
Packit a69f91
By default, 
Packit a69f91
.B interact
Packit a69f91
expects the user to be writing stdin and reading stdout of the
Packit a69f91
.B Expect
Packit a69f91
process
Packit a69f91
itself.
Packit a69f91
The
Packit a69f91
.B \-u
Packit a69f91
flag (for "user") makes
Packit a69f91
.B interact
Packit a69f91
look for the user as the process named by its argument
Packit a69f91
(which must be a spawned id).  
Packit a69f91
.IP
Packit a69f91
This allows two unrelated processes to be joined
Packit a69f91
together without using an explicit loop.  To aid in debugging, Expect
Packit a69f91
diagnostics always go to stderr (or stdout for certain logging and
Packit a69f91
debugging information).  For the same reason, the
Packit a69f91
.B interpreter
Packit a69f91
command will read interactively from stdin.
Packit a69f91
.IP
Packit a69f91
For example, the following fragment creates a login process.
Packit a69f91
Then it dials the user (not shown), and finally connects the two together.
Packit a69f91
Of course, any process may be substituted for login.
Packit a69f91
A shell, for example, would allow the user to work without supplying an
Packit a69f91
account and password.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    spawn login
Packit a69f91
    set login $spawn_id
Packit a69f91
    spawn tip modem
Packit a69f91
    # dial back out to user
Packit a69f91
    # connect user to login
Packit a69f91
    interact \-u $login
Packit a69f91
Packit a69f91
.fi
Packit a69f91
To send output to multiple processes, list each spawn id list prefaced by a
Packit a69f91
.B \-output
Packit a69f91
flag.  Input for a group of output spawn ids may be determined
Packit a69f91
by a spawn id list prefaced by a
Packit a69f91
.B \-input
Packit a69f91
flag.  (Both
Packit a69f91
.B \-input
Packit a69f91
and
Packit a69f91
.B \-output
Packit a69f91
may take lists in the same form as the
Packit a69f91
.B \-i
Packit a69f91
flag in the
Packit a69f91
.B expect
Packit a69f91
command, except that any_spawn_id is not meaningful in
Packit a69f91
.BR interact .)
Packit a69f91
All following flags and
Packit a69f91
strings (or patterns) apply to this input until another -input flag appears.
Packit a69f91
If no
Packit a69f91
.B \-input
Packit a69f91
appears,
Packit a69f91
.B \-output
Packit a69f91
implies "\-input $user_spawn_id \-output".
Packit a69f91
(Similarly, with patterns that do not have
Packit a69f91
.BR \-input .)
Packit a69f91
If one
Packit a69f91
.B \-input
Packit a69f91
is specified, it overrides $user_spawn_id.  If a second
Packit a69f91
.B \-input
Packit a69f91
is specified,
Packit a69f91
it overrides $spawn_id.  Additional
Packit a69f91
.B \-input
Packit a69f91
flags may be specified.
Packit a69f91
Packit a69f91
The two implied input processes default to having their outputs specified as
Packit a69f91
$spawn_id and $user_spawn_id (in reverse).  
Packit a69f91
If a
Packit a69f91
.B \-input
Packit a69f91
flag appears
Packit a69f91
with no
Packit a69f91
.B \-output
Packit a69f91
flag, characters from that process are discarded.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-i
Packit a69f91
flag introduces a replacement for the current spawn_id when no
Packit a69f91
other
Packit a69f91
.B \-input
Packit a69f91
or
Packit a69f91
.B \-output
Packit a69f91
flags are used.  A \-i flag implies a \-o flag.
Packit a69f91
Packit a69f91
It is possible to change the processes that are being interacted with
Packit a69f91
by using indirect spawn ids.  (Indirect spawn ids are described in the
Packit a69f91
section on the expect command.)  Indirect spawn ids may be specified
Packit a69f91
with the -i, -u, -input, or -output flags.
Packit a69f91
.TP
Packit a69f91
.B interpreter " [args]"
Packit a69f91
causes the user to be interactively prompted for
Packit a69f91
.B Expect
Packit a69f91
and Tcl commands.
Packit a69f91
The result of each command is printed.
Packit a69f91
.IP
Packit a69f91
Actions such as
Packit a69f91
.B break
Packit a69f91
and
Packit a69f91
.B continue
Packit a69f91
cause control structures (i.e.,
Packit a69f91
.BR for ,
Packit a69f91
.BR proc )
Packit a69f91
to behave in the usual way.
Packit a69f91
However
Packit a69f91
.B return
Packit a69f91
causes interpreter to return to its caller, while
Packit a69f91
.B inter_return
Packit a69f91
causes
Packit a69f91
.B interpreter
Packit a69f91
to cause a return in its caller.  For example, if "proc foo" called
Packit a69f91
.B interpreter
Packit a69f91
which then executed the action
Packit a69f91
.BR inter_return ,
Packit a69f91
.B proc foo
Packit a69f91
would return.
Packit a69f91
Any other command causes
Packit a69f91
.B interpreter
Packit a69f91
to continue prompting for new commands.
Packit a69f91
.IP
Packit a69f91
By default, the prompt contains two integers.
Packit a69f91
The first integer describes the depth of
Packit a69f91
the evaluation stack (i.e., how many times Tcl_Eval has been called).  The
Packit a69f91
second integer is the Tcl history identifier.  The prompt can be set by
Packit a69f91
defining a procedure called "prompt1" whose return value becomes the next
Packit a69f91
prompt.  If a statement has open quotes, parens, braces, or brackets, a
Packit a69f91
secondary prompt (by default "+> ") is issued upon newline.  The secondary
Packit a69f91
prompt may be set by defining a procedure called "prompt2".
Packit a69f91
.IP
Packit a69f91
During
Packit a69f91
.BR interpreter ,
Packit a69f91
cooked mode is used, even if the its caller was using raw mode.
Packit a69f91
.IP
Packit a69f91
If stdin is closed,
Packit a69f91
.B interpreter
Packit a69f91
will return unless the
Packit a69f91
.B \-eof
Packit a69f91
flag is used, in which case the subsequent argument is invoked.
Packit a69f91
.TP
Packit a69f91
.BI log_file " [args] [[\-a] file]"
Packit a69f91
If a filename is provided,
Packit a69f91
.B log_file
Packit a69f91
will record a transcript of the session (beginning at that point) in the file.
Packit a69f91
.B log_file
Packit a69f91
will stop recording if no argument is given.  Any previous log file is closed.
Packit a69f91
Packit a69f91
Instead of a filename, a Tcl file identifier may be provided by using the
Packit a69f91
.B \-open
Packit a69f91
or
Packit a69f91
.B \-leaveopen
Packit a69f91
flags.  This is similar to the
Packit a69f91
.B spawn
Packit a69f91
command.  (See
Packit a69f91
.B spawn
Packit a69f91
for more info.)
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-a
Packit a69f91
flag forces output to be logged that was suppressed by the
Packit a69f91
.B log_user
Packit a69f91
command.
Packit a69f91
Packit a69f91
By default, the
Packit a69f91
.B log_file
Packit a69f91
command
Packit a69f91
.I appends
Packit a69f91
to old files rather than truncating them,
Packit a69f91
for the convenience of being able to turn logging off and on multiple
Packit a69f91
times in one session.
Packit a69f91
To truncate files, use the
Packit a69f91
.B \-noappend
Packit a69f91
flag.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B -info
Packit a69f91
flag causes log_file to return a description of the
Packit a69f91
most recent non-info arguments given.
Packit a69f91
.TP
Packit a69f91
.BI log_user " -info|0|1"
Packit a69f91
By default, the send/expect dialogue is logged to stdout
Packit a69f91
(and a logfile if open).
Packit a69f91
The logging to stdout is disabled by the command "log_user 0"
Packit a69f91
and reenabled by "log_user 1".  Logging to the logfile is unchanged.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B -info
Packit a69f91
flag causes log_user to return a description of the
Packit a69f91
most recent non-info arguments given.
Packit a69f91
.TP
Packit a69f91
.BI match_max " [\-d] [\-i spawn_id] [size]"
Packit a69f91
defines the size of the buffer (in bytes) used internally by
Packit a69f91
.BR expect .
Packit a69f91
With no
Packit a69f91
.I size
Packit a69f91
argument, the current size is returned.
Packit a69f91
.IP
Packit a69f91
With the
Packit a69f91
.B \-d
Packit a69f91
flag, the default size is set.  (The initial default is 2000.)
Packit a69f91
With the
Packit a69f91
.B \-i
Packit a69f91
flag, the size is set for the named spawn id, otherwise it is set for
Packit a69f91
the current process.
Packit a69f91
.TP
Packit a69f91
.BI overlay " [\-# spawn_id] [\-# spawn_id] [...] program [args]"
Packit a69f91
executes
Packit a69f91
.IR "program args"
Packit a69f91
in place of the current
Packit a69f91
.B Expect
Packit a69f91
program, which terminates.
Packit a69f91
A bare hyphen argument forces a hyphen in front of the command name as if
Packit a69f91
it was a login shell.
Packit a69f91
All spawn_ids are closed except for those named as arguments.  These
Packit a69f91
are mapped onto the named file identifiers.
Packit a69f91
.IP
Packit a69f91
Spawn_ids are mapped to file identifiers for the new program to inherit.
Packit a69f91
For example, the following line runs chess and allows it to be
Packit a69f91
controlled by the current process \- say, a chess master.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    overlay \-0 $spawn_id \-1 $spawn_id \-2 $spawn_id chess
Packit a69f91
Packit a69f91
.fi
Packit a69f91
This is more efficient than
Packit a69f91
"interact \-u", however, it sacrifices the ability to do programmed
Packit a69f91
interaction since the
Packit a69f91
.B Expect
Packit a69f91
process is no longer in control.
Packit a69f91
.IP
Packit a69f91
Note that no controlling terminal is provided.  Thus, if you
Packit a69f91
disconnect or remap standard input, programs that do
Packit a69f91
job control (shells, login, etc) will not function properly.
Packit a69f91
.TP
Packit a69f91
.BI parity " [\-d] [\-i spawn_id] [value]"
Packit a69f91
defines whether parity should be retained or stripped from the output of
Packit a69f91
spawned processes.  If
Packit a69f91
.I value
Packit a69f91
is zero, parity is stripped, otherwise it is not stripped.
Packit a69f91
With no
Packit a69f91
.I value
Packit a69f91
argument, the current value is returned.
Packit a69f91
.IP
Packit a69f91
With the
Packit a69f91
.B \-d
Packit a69f91
flag, the default parity value is set.  (The initial default is 1, i.e., 
Packit a69f91
parity is not stripped.)
Packit a69f91
With the
Packit a69f91
.B \-i
Packit a69f91
flag, the parity value is set for the named spawn id, otherwise it is set for
Packit a69f91
the current process.
Packit a69f91
.TP
Packit a69f91
.BI remove_nulls " [\-d] [\-i spawn_id] [value]"
Packit a69f91
defines whether nulls are retained or removed from the output of
Packit a69f91
spawned processes before pattern matching
Packit a69f91
or storing in the variable
Packit a69f91
.I expect_out 
Packit a69f91
or
Packit a69f91
.IR interact_out .
Packit a69f91
If
Packit a69f91
.I value
Packit a69f91
is 1, nulls are removed.  If
Packit a69f91
.I value
Packit a69f91
is 0, nulls are not removed.
Packit a69f91
With no
Packit a69f91
.I value
Packit a69f91
argument, the current value is returned.
Packit a69f91
.IP
Packit a69f91
With the
Packit a69f91
.B \-d
Packit a69f91
flag, the default value is set.  (The initial default is 1, i.e., 
Packit a69f91
nulls are removed.)
Packit a69f91
With the
Packit a69f91
.B \-i
Packit a69f91
flag, the value is set for the named spawn id, otherwise it is set for
Packit a69f91
the current process.
Packit a69f91
Packit a69f91
Whether or not nulls are removed,
Packit a69f91
.B Expect
Packit a69f91
will record null bytes to the log and stdout.
Packit a69f91
.TP
Packit a69f91
.BI send " [\-flags] string"
Packit a69f91
Sends
Packit a69f91
.IR string
Packit a69f91
to the current process.
Packit a69f91
For example, the command
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    send "hello world\\r"
Packit a69f91
Packit a69f91
.fi
Packit a69f91
sends the characters, h e l l o <blank> w o r l d <return> to the 
Packit a69f91
current process.  
Packit a69f91
(Tcl includes a printf-like command (called
Packit a69f91
.BR format )
Packit a69f91
which can build arbitrarily complex strings.)
Packit a69f91
.IP
Packit a69f91
Characters are sent immediately although programs with line-buffered input
Packit a69f91
will not read the characters until a return character is sent.  A return
Packit a69f91
character is denoted "\\r".
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-\-
Packit a69f91
flag forces the next argument to be interpreted as a string rather than a flag.
Packit a69f91
Any string can be preceded by "\-\-" whether or not it actually looks
Packit a69f91
like a flag.  This provides a reliable mechanism to specify variable strings
Packit a69f91
without being tripped up by those that accidentally look like flags.
Packit a69f91
(All strings starting with "-" are reserved for future options.)
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-i
Packit a69f91
flag declares that the string be sent to the named spawn_id.
Packit a69f91
If the spawn_id is
Packit a69f91
.IR user_spawn_id ,
Packit a69f91
and the terminal is in raw mode, newlines in the string are translated
Packit a69f91
to return-newline
Packit a69f91
sequences so that they appear as if the terminal was in cooked mode.
Packit a69f91
The
Packit a69f91
.B \-raw
Packit a69f91
flag disables this translation.
Packit a69f91
Packit a69f91
The
Packit a69f91
.BR \-null
Packit a69f91
flag sends null characters (0 bytes).  By default, one null is sent.
Packit a69f91
An integer may follow the
Packit a69f91
.BR \-null
Packit a69f91
to indicate how many nulls to send.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-break
Packit a69f91
flag generates a break condition.  This only makes sense if the spawn
Packit a69f91
id refers to a tty device opened via "spawn -open".  If you have
Packit a69f91
spawned a process such as tip, you should use tip's convention for
Packit a69f91
generating a break.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-s
Packit a69f91
flag forces output to be sent "slowly", thus avoid the common situation
Packit a69f91
where a computer outtypes an input buffer that was designed for a
Packit a69f91
human who would never outtype the same buffer.  This output is
Packit a69f91
controlled by the value of the variable "send_slow" which takes a two
Packit a69f91
element list.  The first element is an integer that describes the
Packit a69f91
number of bytes to send atomically.  The second element is a real
Packit a69f91
number that describes the number of seconds by which the atomic sends
Packit a69f91
must be separated.  For example, "set send_slow {10 .001}" would force
Packit a69f91
"send \-s" to send strings with 1 millisecond in between each 10
Packit a69f91
characters sent.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-h
Packit a69f91
flag forces output to be sent (somewhat) like a human actually typing.
Packit a69f91
Human-like delays appear between the characters.  (The algorithm is
Packit a69f91
based upon a Weibull distribution, with modifications to suit this
Packit a69f91
particular application.)  This output is controlled by the value of
Packit a69f91
the variable "send_human" which takes a five element list.  The first
Packit a69f91
two elements are average interarrival time of characters in seconds.
Packit a69f91
The first is used by default.  The second is used at word endings, to
Packit a69f91
simulate the subtle pauses that occasionally occur at such
Packit a69f91
transitions.  The third parameter is a measure of variability where .1
Packit a69f91
is quite variable, 1 is reasonably variable, and 10 is quite
Packit a69f91
invariable.  The extremes are 0 to infinity.  The last two parameters
Packit a69f91
are, respectively, a minimum and maximum interarrival time.
Packit a69f91
The minimum and maximum are used last and "clip" the final time.
Packit a69f91
The ultimate average can be quite different from the given average
Packit a69f91
if the minimum and maximum clip enough values.
Packit a69f91
Packit a69f91
As an
Packit a69f91
example, the following command emulates a fast and
Packit a69f91
consistent typist:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    set send_human {.1 .3 1 .05 2}
Packit a69f91
    send \-h "I'm hungry.  Let's do lunch."
Packit a69f91
Packit a69f91
.fi
Packit a69f91
while the following might be more suitable after a hangover:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    set send_human {.4 .4 .2 .5 100}
Packit a69f91
    send \-h "Goodd party lash night!"
Packit a69f91
Packit a69f91
.fi
Packit a69f91
Note that errors are not simulated, although you can set up error
Packit a69f91
correction situations yourself by embedding mistakes and corrections
Packit a69f91
in a send argument.
Packit a69f91
Packit a69f91
The flags for sending null characters, for sending breaks, for forcing slow
Packit a69f91
output and for human-style output are mutually exclusive. Only the one
Packit a69f91
specified last will be used. Furthermore, no
Packit a69f91
.I string
Packit a69f91
argument can be specified with the flags for sending null characters or breaks.
Packit a69f91
Packit a69f91
It is a good idea to precede the first
Packit a69f91
.B send
Packit a69f91
to a process by an
Packit a69f91
.BR expect .
Packit a69f91
.B expect
Packit a69f91
will wait for the process to start, while
Packit a69f91
.B send
Packit a69f91
cannot.
Packit a69f91
In particular, if the first
Packit a69f91
.B send
Packit a69f91
completes before the process starts running,
Packit a69f91
you run the risk of having your data ignored.
Packit a69f91
In situations where interactive programs offer no initial prompt,
Packit a69f91
you can precede
Packit a69f91
.B send
Packit a69f91
by a delay as in:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    # To avoid giving hackers hints on how to break in,
Packit a69f91
    # this system does not prompt for an external password.
Packit a69f91
    # Wait for 5 seconds for exec to complete
Packit a69f91
    spawn telnet very.secure.gov
Packit a69f91
    sleep 5
Packit a69f91
    send password\\r
Packit a69f91
Packit a69f91
.fi
Packit a69f91
.B exp_send
Packit a69f91
is an alias for
Packit a69f91
.BI send .
Packit a69f91
If you are using Expectk or some other variant of Expect in the Tk environment,
Packit a69f91
.B send
Packit a69f91
is defined by Tk for an entirely different purpose.
Packit a69f91
.B exp_send
Packit a69f91
is provided for compatibility between environments.
Packit a69f91
Similar aliases are provided for other Expect's other send commands.
Packit a69f91
.TP
Packit a69f91
.BI send_error " [\-flags] string"
Packit a69f91
is like
Packit a69f91
.BR send ,
Packit a69f91
except that the output is sent to stderr rather than the current
Packit a69f91
process.
Packit a69f91
.TP
Packit a69f91
.BI send_log " [\--] string"
Packit a69f91
is like
Packit a69f91
.BR send ,
Packit a69f91
except that the string is only sent to the log file (see
Packit a69f91
.BR log_file .)
Packit a69f91
The arguments are ignored if no log file is open.
Packit a69f91
.TP
Packit a69f91
.BI send_tty " [\-flags] string"
Packit a69f91
is like
Packit a69f91
.BR send ,
Packit a69f91
except that the output is sent to /dev/tty rather than the current
Packit a69f91
process.
Packit a69f91
.TP
Packit a69f91
.BI send_user " [\-flags] string"
Packit a69f91
is like
Packit a69f91
.BR send ,
Packit a69f91
except that the output is sent to stdout rather than the current
Packit a69f91
process.
Packit a69f91
.TP
Packit a69f91
.BI sleep " seconds"
Packit a69f91
causes the script to sleep for the given number of seconds.
Packit a69f91
Seconds may be a decimal number.  Interrupts (and Tk events if you
Packit a69f91
are using Expectk) are processed while Expect sleeps.
Packit a69f91
.TP
Packit a69f91
.BI spawn " [args] program [args]"
Packit a69f91
creates a new process running
Packit a69f91
.IR "program args" .
Packit a69f91
Its stdin, stdout and stderr are connected to Expect,
Packit a69f91
so that they may be read and written by other
Packit a69f91
.B Expect
Packit a69f91
commands.
Packit a69f91
The connection is broken by
Packit a69f91
.B close
Packit a69f91
or if the process itself closes any of the file identifiers.
Packit a69f91
.IP
Packit a69f91
When a process is started by
Packit a69f91
.BR spawn ,
Packit a69f91
the variable
Packit a69f91
.I spawn_id
Packit a69f91
is set to a descriptor referring to that process.
Packit a69f91
The process described by
Packit a69f91
.I spawn_id
Packit a69f91
is considered the
Packit a69f91
.IR "current process" .
Packit a69f91
.I spawn_id
Packit a69f91
may be read or written, in effect providing job control.
Packit a69f91
.IP
Packit a69f91
.I user_spawn_id
Packit a69f91
is a global variable containing a descriptor which refers to the user.
Packit a69f91
For example, when
Packit a69f91
.I spawn_id
Packit a69f91
is set to this value,
Packit a69f91
.B expect
Packit a69f91
behaves like
Packit a69f91
.BR expect_user .
Packit a69f91
Packit a69f91
.I
Packit a69f91
.I error_spawn_id
Packit a69f91
is a global variable containing a descriptor which refers to the standard
Packit a69f91
error.
Packit a69f91
For example, when
Packit a69f91
.I spawn_id
Packit a69f91
is set to this value,
Packit a69f91
.B send
Packit a69f91
behaves like
Packit a69f91
.BR send_error .
Packit a69f91
.IP
Packit a69f91
.I tty_spawn_id
Packit a69f91
is a global variable containing a descriptor which refers to /dev/tty.
Packit a69f91
If /dev/tty does not exist (such as in a cron, at, or batch script), then
Packit a69f91
.I tty_spawn_id
Packit a69f91
is not defined.  This may be tested as:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    if {[info vars tty_spawn_id]} {
Packit a69f91
        # /dev/tty exists
Packit a69f91
    } else {
Packit a69f91
        # /dev/tty doesn't exist
Packit a69f91
        # probably in cron, batch, or at script
Packit a69f91
    }
Packit a69f91
Packit a69f91
.fi
Packit a69f91
.IP
Packit a69f91
.B spawn
Packit a69f91
returns the UNIX process id.  If no process is spawned, 0 is returned.
Packit a69f91
The variable
Packit a69f91
.I spawn_out(slave,name)
Packit a69f91
is set to the name of the pty slave device.
Packit a69f91
.IP
Packit a69f91
By default,
Packit a69f91
.B spawn
Packit a69f91
echoes the command name and arguments.  The
Packit a69f91
.B \-noecho
Packit a69f91
flag stops
Packit a69f91
.B spawn
Packit a69f91
from doing this.
Packit a69f91
.IP
Packit a69f91
The
Packit a69f91
.B \-console
Packit a69f91
flag causes console output to be redirected to the spawned process.
Packit a69f91
This is not supported on all systems.
Packit a69f91
Packit a69f91
Internally,
Packit a69f91
.B spawn
Packit a69f91
uses a pty, initialized the same way as the user's tty.  This is further
Packit a69f91
initialized so that all settings are "sane" (according to stty(1)).
Packit a69f91
If the variable
Packit a69f91
.I stty_init
Packit a69f91
is defined, it is interpreted in the style of stty arguments
Packit a69f91
as further configuration.
Packit a69f91
For example, "set stty_init raw" will cause further spawned processes's
Packit a69f91
terminals to start in raw mode.
Packit a69f91
.B \-nottycopy
Packit a69f91
skips the initialization based on the user's tty.
Packit a69f91
.B \-nottyinit
Packit a69f91
skips the "sane" initialization.
Packit a69f91
.IP
Packit a69f91
Normally,
Packit a69f91
.B spawn
Packit a69f91
takes little time to execute.  If you notice spawn taking a
Packit a69f91
significant amount of time, it is probably encountering ptys that are
Packit a69f91
wedged.  A number of tests are run on ptys to avoid entanglements with
Packit a69f91
errant processes.  (These take 10 seconds per wedged pty.)  Running
Packit a69f91
Expect with the
Packit a69f91
.B \-d
Packit a69f91
option will show if
Packit a69f91
.B Expect
Packit a69f91
is encountering many ptys in odd states.  If you cannot kill
Packit a69f91
the processes to which these ptys are attached, your only recourse may
Packit a69f91
be to reboot.
Packit a69f91
Packit a69f91
If
Packit a69f91
.I program
Packit a69f91
cannot be spawned successfully because exec(2) fails (e.g. when
Packit a69f91
.I program
Packit a69f91
doesn't exist), an error message will be returned by the next
Packit a69f91
.B interact
Packit a69f91
or
Packit a69f91
.B expect
Packit a69f91
command as if
Packit a69f91
.I program
Packit a69f91
had run and produced the error message as output.
Packit a69f91
This behavior is a natural consequence of the implementation of
Packit a69f91
.BR spawn .
Packit a69f91
Internally, spawn forks, after which the spawned process has no
Packit a69f91
way to communicate with the original
Packit a69f91
.B Expect
Packit a69f91
process except by communication
Packit a69f91
via the spawn_id.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-open
Packit a69f91
flag causes the next argument to be interpreted as a Tcl file identifier
Packit a69f91
(i.e., returned by
Packit a69f91
.BR open .)
Packit a69f91
The spawn id can then be used as if it were a spawned process.  (The file
Packit a69f91
identifier should no longer be used.)
Packit a69f91
This lets you treat raw devices, files, and
Packit a69f91
pipelines as spawned processes without using a pty.  0 is returned to
Packit a69f91
indicate there is no associated process.  When the connection to
Packit a69f91
the spawned process is closed, so is the Tcl file identifier.
Packit a69f91
The
Packit a69f91
.B \-leaveopen
Packit a69f91
flag is similar to
Packit a69f91
.B \-open
Packit a69f91
except that
Packit a69f91
.B \-leaveopen
Packit a69f91
causes the file identifier to be left open even after the spawn id is closed.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-pty
Packit a69f91
flag causes a pty to be opened but no process spawned.  0 is returned
Packit a69f91
to indicate there is no associated process.  Spawn_id is set as usual.
Packit a69f91
Packit a69f91
The variable
Packit a69f91
.I spawn_out(slave,fd)
Packit a69f91
is set to a file identifier corresponding to the pty slave.
Packit a69f91
It can be closed using "close -slave".
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-ignore
Packit a69f91
flag names a signal to be ignored in the spawned process.
Packit a69f91
Otherwise, signals get the default behavior.
Packit a69f91
Signals are named as in the
Packit a69f91
.B trap
Packit a69f91
command, except that each signal requires a separate flag.
Packit a69f91
.TP
Packit a69f91
.BI strace " level"
Packit a69f91
causes following statements to be printed before being executed.
Packit a69f91
(Tcl's trace command traces variables.)
Packit a69f91
.I level
Packit a69f91
indicates how far down in the call stack to trace.
Packit a69f91
For example,
Packit a69f91
the following command runs
Packit a69f91
.B Expect
Packit a69f91
while tracing the first 4 levels of calls,
Packit a69f91
but none below that.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    expect \-c "strace 4" script.exp
Packit a69f91
Packit a69f91
.fi
Packit a69f91
Packit a69f91
The
Packit a69f91
.B -info
Packit a69f91
flag causes strace to return a description of the
Packit a69f91
most recent non-info arguments given.
Packit a69f91
.TP
Packit a69f91
.BI stty " args"
Packit a69f91
changes terminal modes similarly to the external stty command.
Packit a69f91
Packit a69f91
By default, the controlling terminal is accessed.  Other terminals can
Packit a69f91
be accessed by appending "< /dev/tty..." to the command.  (Note that
Packit a69f91
the arguments should not be grouped into a single argument.)
Packit a69f91
Packit a69f91
Requests for status return it as the result of the command.  If no status
Packit a69f91
is requested and the controlling terminal is accessed, the previous
Packit a69f91
status of the raw and echo attributes are returned in a form which can
Packit a69f91
later be used by the command.
Packit a69f91
Packit a69f91
For example, the arguments
Packit a69f91
.B raw
Packit a69f91
or
Packit a69f91
.B \-cooked
Packit a69f91
put the terminal into raw mode.
Packit a69f91
The arguments
Packit a69f91
.B \-raw
Packit a69f91
or
Packit a69f91
.B cooked
Packit a69f91
put the terminal into cooked mode.
Packit a69f91
The arguments
Packit a69f91
.B echo
Packit a69f91
and
Packit a69f91
.B \-echo
Packit a69f91
put the terminal into echo and noecho mode respectively.
Packit a69f91
.IP
Packit a69f91
The following example illustrates how to temporarily disable echoing.
Packit a69f91
This could be used in otherwise-automatic
Packit a69f91
scripts to avoid embedding passwords in them.
Packit a69f91
(See more discussion on this under EXPECT HINTS below.)
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    stty \-echo
Packit a69f91
    send_user "Password: "
Packit a69f91
    expect_user -re "(.*)\\n"
Packit a69f91
    set password $expect_out(1,string)
Packit a69f91
    stty echo
Packit a69f91
Packit a69f91
.fi
Packit a69f91
.TP
Packit a69f91
.BI system " args"
Packit a69f91
gives
Packit a69f91
.I args
Packit a69f91
to sh(1) as input,
Packit a69f91
just as if it had been typed as a command from a terminal.
Packit a69f91
.B Expect
Packit a69f91
waits until the shell terminates.
Packit a69f91
The return status from sh is handled the same way that
Packit a69f91
.B exec
Packit a69f91
handles its return status.
Packit a69f91
.IP
Packit a69f91
In contrast to
Packit a69f91
.B exec
Packit a69f91
which redirects stdin and stdout to the script,
Packit a69f91
.B system
Packit a69f91
performs no redirection
Packit a69f91
(other than that indicated by the string itself).
Packit a69f91
Thus, it is possible to use programs which must talk directly to /dev/tty.
Packit a69f91
For the same reason, the results of
Packit a69f91
.B system
Packit a69f91
are not recorded in the log.
Packit a69f91
.TP
Packit a69f91
.BI timestamp " [args]"
Packit a69f91
returns a timestamp.
Packit a69f91
With no arguments, the number of
Packit a69f91
seconds since the epoch is returned.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-format
Packit a69f91
flag introduces a string which is returned but with 
Packit a69f91
substitutions made according to the
Packit a69f91
POSIX rules for strftime.  For example %a is replaced by an abbreviated
Packit a69f91
weekday name (i.e., Sat).  Others are:
Packit a69f91
.nf
Packit a69f91
    %a      abbreviated weekday name
Packit a69f91
    %A      full weekday name
Packit a69f91
    %b      abbreviated month name
Packit a69f91
    %B      full month name
Packit a69f91
    %c      date-time as in: Wed Oct  6 11:45:56 1993
Packit a69f91
    %d      day of the month (01-31)
Packit a69f91
    %H      hour (00-23)
Packit a69f91
    %I      hour (01-12)
Packit a69f91
    %j      day (001-366)
Packit a69f91
    %m      month (01-12)
Packit a69f91
    %M      minute (00-59)
Packit a69f91
    %p      am or pm
Packit a69f91
    %S      second (00-61)
Packit a69f91
    %u      day (1-7, Monday is first day of week)
Packit a69f91
    %U      week (00-53, first Sunday is first day of week one)
Packit a69f91
    %V      week (01-53, ISO 8601 style)
Packit a69f91
    %w      day (0-6)
Packit a69f91
    %W      week (00-53, first Monday is first day of week one)
Packit a69f91
    %x      date-time as in: Wed Oct  6 1993
Packit a69f91
    %X      time as in: 23:59:59
Packit a69f91
    %y      year (00-99)
Packit a69f91
    %Y      year as in: 1993
Packit a69f91
    %Z      timezone (or nothing if not determinable)
Packit a69f91
    %%      a bare percent sign
Packit a69f91
Packit a69f91
.fi
Packit a69f91
Other % specifications are undefined.  Other characters will be passed
Packit a69f91
through untouched.  Only the C locale is supported.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-seconds
Packit a69f91
flag introduces a number of seconds since the epoch to be used as a source
Packit a69f91
from which to format.  Otherwise, the current time is used.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-gmt
Packit a69f91
flag forces timestamp output to use the GMT timezone.  With no flag,
Packit a69f91
the local timezone is used.
Packit a69f91
.TP
Packit a69f91
.BI trap " [[command] signals]"
Packit a69f91
causes the given 
Packit a69f91
.I command
Packit a69f91
to be executed upon future receipt of any of the given signals.
Packit a69f91
The command is executed in the global scope.
Packit a69f91
If
Packit a69f91
.I command
Packit a69f91
is absent, the signal action is returned.
Packit a69f91
If
Packit a69f91
.I command 
Packit a69f91
is the string SIG_IGN, the signals are ignored.
Packit a69f91
If
Packit a69f91
.I command
Packit a69f91
is the string SIG_DFL, the signals are result to the system default.
Packit a69f91
.I signals
Packit a69f91
is either a single signal or a list of signals.  Signals may be specified
Packit a69f91
numerically or symbolically as per signal(3).  The "SIG" prefix may be omitted.
Packit a69f91
Packit a69f91
With no arguments (or the argument \-number),
Packit a69f91
.B trap
Packit a69f91
returns the signal number of the trap command currently being executed.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-code
Packit a69f91
flag uses the return code of the command in place of whatever code Tcl
Packit a69f91
was about to return when the command originally started running.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-interp
Packit a69f91
flag causes the command to be evaluated using the interpreter
Packit a69f91
active at the time the command started running
Packit a69f91
rather than when the trap was declared.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-name
Packit a69f91
flag causes the
Packit a69f91
.B trap
Packit a69f91
command to return the signal name of the trap command currently being executed.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-max
Packit a69f91
flag causes the
Packit a69f91
.B trap
Packit a69f91
command to return the largest signal number that can be set.
Packit a69f91
Packit a69f91
For example, the command "trap {send_user "Ouch!"} SIGINT" will print "Ouch!"
Packit a69f91
each time the user presses ^C.
Packit a69f91
Packit a69f91
By default, SIGINT (which can usually be generated by pressing ^C) and 
Packit a69f91
SIGTERM cause Expect to exit.  This is due to the following trap, created
Packit a69f91
by default when Expect starts.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    trap exit {SIGINT SIGTERM}
Packit a69f91
Packit a69f91
.fi
Packit a69f91
If you use the -D flag to start the debugger, SIGINT is redefined
Packit a69f91
to start the interactive debugger.  This is due to the following trap:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    trap {exp_debug 1} SIGINT
Packit a69f91
Packit a69f91
.fi
Packit a69f91
The debugger trap can be changed by setting the environment variable
Packit a69f91
EXPECT_DEBUG_INIT to a new trap command.  
Packit a69f91
Packit a69f91
You can, of course, override both of these just by adding trap
Packit a69f91
commands to your script.  In particular, if you have your own "trap
Packit a69f91
exit SIGINT", this will override the debugger trap.  This is useful
Packit a69f91
if you want to prevent users from getting to the debugger at all.
Packit a69f91
Packit a69f91
If you want to define your own trap on SIGINT but still trap to the
Packit a69f91
debugger when it is running, use:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    if {![exp_debug]} {trap mystuff SIGINT}
Packit a69f91
Packit a69f91
.fi
Packit a69f91
Alternatively, you can trap to the debugger using some other signal.
Packit a69f91
Packit a69f91
.B trap
Packit a69f91
will not let you override the action for SIGALRM as this is used internally
Packit a69f91
to
Packit a69f91
.BR Expect .
Packit a69f91
The disconnect command sets SIGALRM to SIG_IGN (ignore).  You can reenable
Packit a69f91
this as long as you disable it during subsequent spawn commands.
Packit a69f91
Packit a69f91
See signal(3) for more info.
Packit a69f91
.TP
Packit a69f91
.BI wait " [args]"
Packit a69f91
delays until a spawned process (or
Packit a69f91
the current process if none is named) terminates.
Packit a69f91
.IP
Packit a69f91
.B wait
Packit a69f91
normally returns a list of four integers.
Packit a69f91
The first integer is the pid of the process that was waited upon.
Packit a69f91
The second integer is the corresponding spawn id.
Packit a69f91
The third integer is -1 if an operating system error occurred, or 0 otherwise.
Packit a69f91
If the third integer was 0, the fourth integer is the status returned by
Packit a69f91
the spawned process.  If the third integer was -1, the fourth integer is
Packit a69f91
the value of errno set by the operating system.  The global variable
Packit a69f91
errorCode is also set.
Packit a69f91
Packit a69f91
Additional elements may appear at the end of the return value from
Packit a69f91
.BR wait .
Packit a69f91
An optional fifth element identifies a class of information.
Packit a69f91
Currently, the only possible value for this element is CHILDKILLED in
Packit a69f91
which case the next two values are the C-style signal name and a short
Packit a69f91
textual description.
Packit a69f91
.IP
Packit a69f91
The
Packit a69f91
.B \-i
Packit a69f91
flag declares the process to wait corresponding to the named spawn_id
Packit a69f91
(NOT the process id).
Packit a69f91
Inside a SIGCHLD handler,
Packit a69f91
it is possible to wait for any spawned process by using the spawn id -1.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B \-nowait
Packit a69f91
flag causes the wait to return immediately with the indication of a
Packit a69f91
successful wait.  When the process exits (later), it will automatically
Packit a69f91
disappear without the need for an explicit wait.
Packit a69f91
Packit a69f91
The
Packit a69f91
.B wait
Packit a69f91
command may also be used wait for a forked process using the arguments
Packit a69f91
"-i -1".  Unlike its use with spawned processes, this command can be
Packit a69f91
executed at any time.  There is no control over which process is
Packit a69f91
reaped.  However, the return value can be checked for the process id.
Packit a69f91
Packit a69f91
.SH LIBRARIES
Packit a69f91
Expect automatically knows about two built-in libraries for Expect scripts.
Packit a69f91
These are defined by the directories named in the variables
Packit a69f91
exp_library and exp_exec_library.  Both are meant to contain utility
Packit a69f91
files that can be used by other scripts.
Packit a69f91
Packit a69f91
exp_library contains architecture-independent files.  exp_exec_library
Packit a69f91
contains architecture-dependent files.  Depending on your system, both
Packit a69f91
directories may be totally empty.  The existence of the file
Packit a69f91
$exp_exec_library/cat-buffers describes whether your /bin/cat buffers
Packit a69f91
by default.
Packit a69f91
.SH PRETTY-PRINTING
Packit a69f91
A vgrind definition is available for pretty-printing
Packit a69f91
.B Expect
Packit a69f91
scripts.
Packit a69f91
Assuming the vgrind definition supplied with the
Packit a69f91
.B Expect
Packit a69f91
distribution is
Packit a69f91
correctly installed, you can use it as:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    vgrind \-lexpect file
Packit a69f91
Packit a69f91
.fi
Packit a69f91
.SH EXAMPLES
Packit a69f91
It many not be apparent how to put everything together that the man page
Packit a69f91
describes.  I encourage you to read and try out the examples in
Packit a69f91
the example directory of the
Packit a69f91
.B Expect
Packit a69f91
distribution.
Packit a69f91
Some of them are real programs.  Others are simply illustrative
Packit a69f91
of certain techniques, and of course, a couple are just quick hacks.
Packit a69f91
The INSTALL file has a quick overview of these programs.
Packit a69f91
.PP
Packit a69f91
The
Packit a69f91
.B Expect
Packit a69f91
papers (see SEE ALSO) are also useful.  While some papers
Packit a69f91
use syntax corresponding to earlier versions of Expect, the accompanying
Packit a69f91
rationales are still valid and go into a lot more detail than this
Packit a69f91
man page.
Packit a69f91
.SH CAVEATS
Packit a69f91
Extensions may collide with Expect's command names.  For example, 
Packit a69f91
.B send
Packit a69f91
is defined by Tk for an entirely different purpose.
Packit a69f91
For this reason, most of the
Packit a69f91
.B Expect
Packit a69f91
commands are also available as "exp_XXXX".
Packit a69f91
Commands and variables beginning with "exp", "inter", "spawn",
Packit a69f91
and "timeout" do not have aliases.
Packit a69f91
Use the extended command names if you need this compatibility between environments.
Packit a69f91
Packit a69f91
.B Expect
Packit a69f91
takes a rather liberal view of scoping.
Packit a69f91
In particular, variables read by commands specific to the
Packit a69f91
.B Expect
Packit a69f91
program will be sought first from the local scope, and if not found, in the
Packit a69f91
global scope.  For example, this
Packit a69f91
obviates the need to place "global timeout" in every
Packit a69f91
procedure you write that uses
Packit a69f91
.BR expect .
Packit a69f91
On the other hand, variables written are always in the local scope (unless
Packit a69f91
a "global" command has been issued).  The most common problem this causes
Packit a69f91
is when spawn is executed in a procedure.  Outside the procedure, 
Packit a69f91
.I spawn_id
Packit a69f91
no longer exists, so the spawned process is no longer accessible
Packit a69f91
simply because of scoping.  Add a "global spawn_id" to such a procedure.
Packit a69f91
Packit a69f91
If you cannot enable the multispawning capability
Packit a69f91
(i.e., your system supports neither select (BSD *.*), poll (SVR>2),
Packit a69f91
nor something equivalent),
Packit a69f91
.B Expect
Packit a69f91
will only be able to control a single process at a time.
Packit a69f91
In this case, do not attempt to set
Packit a69f91
.IR spawn_id ,
Packit a69f91
nor should you execute processes via exec while a spawned process
Packit a69f91
is running.  Furthermore, you will not be able to
Packit a69f91
.B expect
Packit a69f91
from multiple processes (including the user as one) at the same time.
Packit a69f91
Packit a69f91
Terminal parameters can have a big effect on scripts.  For example, if
Packit a69f91
a script is written to look for echoing, it will misbehave if echoing
Packit a69f91
is turned off.  For this reason, Expect forces sane terminal
Packit a69f91
parameters by default.  Unfortunately, this can make things unpleasant
Packit a69f91
for other programs.  As an example, the emacs shell wants to change
Packit a69f91
the "usual" mappings: newlines get mapped to newlines instead of
Packit a69f91
carriage-return newlines, and echoing is disabled.  This allows one to
Packit a69f91
use emacs to edit the input line.  Unfortunately, Expect cannot
Packit a69f91
possibly guess this.
Packit a69f91
Packit a69f91
You can request that Expect not override its default setting of
Packit a69f91
terminal parameters, but you must then be very careful when writing
Packit a69f91
scripts for such environments.  In the case of emacs, avoid depending
Packit a69f91
upon things like echoing and end-of-line mappings.
Packit a69f91
Packit a69f91
The commands that accepted arguments braced into a single list (the
Packit a69f91
.B expect
Packit a69f91
variants and
Packit a69f91
.BR interact )
Packit a69f91
use a heuristic to decide if the list is actually one argument or
Packit a69f91
many.  The heuristic can fail only in the case when the list actually
Packit a69f91
does represent a single argument which has multiple embedded \\n's
Packit a69f91
with non-whitespace characters between them.  This seems sufficiently
Packit a69f91
improbable, however the argument "\-nobrace" can be used to force a
Packit a69f91
single argument to be handled as a single argument.  This could
Packit a69f91
conceivably be used with machine-generated Expect code.  Similarly,
Packit a69f91
-brace forces a single argument to be handle as multiple patterns/actions.
Packit a69f91
Packit a69f91
.SH BUGS
Packit a69f91
It was really tempting to name the program "sex" (for either "Smart EXec"
Packit a69f91
or "Send-EXpect"), but good sense (or perhaps just Puritanism) prevailed.
Packit a69f91
Packit a69f91
On some systems, when a shell is spawned, it complains about not being
Packit a69f91
able to access the tty but runs anyway.  This means your system has a
Packit a69f91
mechanism for gaining the controlling tty that
Packit a69f91
.B Expect
Packit a69f91
doesn't know about.  Please find out what it is, and send this information
Packit a69f91
back to me.
Packit a69f91
Packit a69f91
Ultrix 4.1 (at least the latest versions around here) considers
Packit a69f91
timeouts of above 1000000 to be equivalent to 0.
Packit a69f91
Packit a69f91
Digital UNIX 4.0A (and probably other versions) refuses to allocate
Packit a69f91
ptys if you define a SIGCHLD handler.  See grantpt page for more info.
Packit a69f91
Packit a69f91
IRIX 6.0 does not handle pty permissions correctly so that if Expect
Packit a69f91
attempts to allocate a pty previously used by someone else, it fails.
Packit a69f91
Upgrade to IRIX 6.1.
Packit a69f91
Packit a69f91
Telnet (verified only under SunOS 4.1.2) hangs if TERM is not set.
Packit a69f91
This is a problem under cron, at and in cgi scripts, which do not
Packit a69f91
define TERM.  Thus, you must set it explicitly - to what type is
Packit a69f91
usually irrelevant.  It just has to be set to something!  The
Packit a69f91
following probably suffices for most cases.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    set env(TERM) vt100
Packit a69f91
Packit a69f91
.fi
Packit a69f91
Packit a69f91
Tip (verified only under BSDI BSD/OS 3.1 i386) hangs if SHELL and HOME
Packit a69f91
are not set.  This is a problem under cron, at and in cgi scripts,
Packit a69f91
which do not define these environment variables.  Thus, you must set
Packit a69f91
them explicitly - to what type is usually irrelevant.  It just has to
Packit a69f91
be set to something!  The following probably suffices for most cases.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    set env(SHELL) /bin/sh
Packit a69f91
    set env(HOME) /usr/local/bin
Packit a69f91
Packit a69f91
.fi
Packit a69f91
Packit a69f91
Packit a69f91
Some implementations of ptys are designed so that the kernel throws
Packit a69f91
away any unread output after 10 to 15 seconds (actual number is
Packit a69f91
implementation-dependent) after the process has closed the file
Packit a69f91
descriptor.  Thus
Packit a69f91
.B Expect
Packit a69f91
programs such as
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    spawn date
Packit a69f91
    sleep 20
Packit a69f91
    expect
Packit a69f91
Packit a69f91
.fi
Packit a69f91
will fail.  To avoid this, invoke non-interactive programs with
Packit a69f91
.B exec
Packit a69f91
rather than
Packit a69f91
.BR spawn .
Packit a69f91
While such situations are conceivable, in practice I have never
Packit a69f91
encountered a situation in which the final output of a truly
Packit a69f91
interactive program would be lost due to this behavior.
Packit a69f91
Packit a69f91
On the other hand, Cray UNICOS ptys throw away any unread output
Packit a69f91
immediately after the process has closed the file descriptor.  I have
Packit a69f91
reported this to Cray and they are working on a fix.
Packit a69f91
Packit a69f91
Sometimes a delay is required between a prompt and a response, such as
Packit a69f91
when a tty interface is changing UART settings or matching baud rates
Packit a69f91
by looking for start/stop bits.  Usually, all this is require is to
Packit a69f91
sleep for a second or two.  A more robust technique is to retry until
Packit a69f91
the hardware is ready to receive input.  The following example uses
Packit a69f91
both strategies:
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    send "speed 9600\\r";
Packit a69f91
    sleep 1
Packit a69f91
    expect {
Packit a69f91
        timeout {send "\\r"; exp_continue}
Packit a69f91
        $prompt
Packit a69f91
    }
Packit a69f91
Packit a69f91
.fi
Packit a69f91
Packit a69f91
trap \-code will not work with any command that sits in Tcl's event
Packit a69f91
loop, such as sleep.  The problem is that in the event loop, Tcl
Packit a69f91
discards the return codes from async event handlers.  A workaround is
Packit a69f91
to set a flag in the trap code.  Then check the flag immediately after
Packit a69f91
the command (i.e., sleep).
Packit a69f91
Packit a69f91
The expect_background command ignores -timeout arguments and has no
Packit a69f91
concept of timeouts in general.
Packit a69f91
Packit a69f91
.SH "EXPECT HINTS"
Packit a69f91
There are a couple of things about
Packit a69f91
.B Expect
Packit a69f91
that may be non-intuitive.
Packit a69f91
This section attempts to address some of these things with a couple of
Packit a69f91
suggestions.
Packit a69f91
Packit a69f91
A common expect problem is how to recognize shell prompts.  Since
Packit a69f91
these are customized differently by differently people and different
Packit a69f91
shells, portably automating rlogin can be difficult without knowing
Packit a69f91
the prompt.  A reasonable convention is to have users store a regular
Packit a69f91
expression describing their prompt (in particular, the end of it) in
Packit a69f91
the environment variable EXPECT_PROMPT.  Code like the following
Packit a69f91
can be used.  If EXPECT_PROMPT doesn't exist, the code still has a good chance of functioning correctly.
Packit a69f91
.nf
Packit a69f91
Packit a69f91
    set prompt "(%|#|\\\\$) $"          ;# default prompt
Packit a69f91
    catch {set prompt $env(EXPECT_PROMPT)}
Packit a69f91
Packit a69f91
    expect -re $prompt
Packit a69f91
Packit a69f91
.fi
Packit a69f91
I encourage you to write
Packit a69f91
.B expect
Packit a69f91
patterns that include the end of whatever
Packit a69f91
you expect to see.  This avoids the possibility of answering a question
Packit a69f91
before seeing the entire thing.  In addition, while you may well be
Packit a69f91
able to answer questions before seeing them entirely, if you answer
Packit a69f91
early,  your answer may appear echoed back in the middle of the question.
Packit a69f91
In other words, the resulting dialogue will be correct but look scrambled.
Packit a69f91
Packit a69f91
Most prompts include a space character at the end.
Packit a69f91
For example, the prompt from ftp is 'f', 't', 'p', '>' and <blank>.
Packit a69f91
To match this prompt, you must account for each of these characters.
Packit a69f91
It is a common mistake not to include the blank.
Packit a69f91
Put the blank in explicitly.
Packit a69f91
Packit a69f91
If you use a pattern of the form X*, the * will match all the output
Packit a69f91
received from the end of X to the last thing received.
Packit a69f91
This sounds intuitive but can be somewhat confusing because the phrase
Packit a69f91
"last thing received" can vary depending upon the speed of the computer
Packit a69f91
and the processing of I/O both by the kernel and the device driver.
Packit a69f91
.PP
Packit a69f91
In particular, humans tend to see program output arriving in huge chunks
Packit a69f91
(atomically) when in reality most programs produce output one
Packit a69f91
line at a time.  Assuming this is the case, the * in the pattern of the
Packit a69f91
previous paragraph may only match the end of the current line even though
Packit a69f91
there seems to be more, because at the time of the match that was all
Packit a69f91
the output that had been received.
Packit a69f91
.PP
Packit a69f91
.B expect
Packit a69f91
has no way of knowing that further output is coming unless your
Packit a69f91
pattern specifically accounts for it.
Packit a69f91
.PP
Packit a69f91
Even depending on line-oriented buffering is unwise.  Not only do programs
Packit a69f91
rarely make promises about the type of buffering they do, but system
Packit a69f91
indigestion can break output lines up so that lines break at seemingly
Packit a69f91
random places.  Thus, if you can express the last few characters
Packit a69f91
of a prompt when writing patterns, it is wise to do so.
Packit a69f91
Packit a69f91
If you are waiting for a pattern in the last output of a program
Packit a69f91
and the program emits something else instead, you will not be able to
Packit a69f91
detect that with the
Packit a69f91
.B timeout
Packit a69f91
keyword.  The reason is that
Packit a69f91
.B expect
Packit a69f91
will not timeout \- instead it will get an
Packit a69f91
.B eof
Packit a69f91
indication.
Packit a69f91
Use that instead.  Even better, use both.  That way if that line
Packit a69f91
is ever moved around, you won't have to edit the line itself.
Packit a69f91
Packit a69f91
Newlines are usually converted to carriage return, linefeed sequences
Packit a69f91
when output by the terminal driver.  Thus, if you want a pattern that
Packit a69f91
explicitly matches the two lines, from, say, printf("foo\\nbar"),
Packit a69f91
you should use the pattern "foo\\r\\nbar".
Packit a69f91
.PP
Packit a69f91
A similar translation occurs when reading from the user, via
Packit a69f91
.BR expect_user .
Packit a69f91
In this case, when you press return, it will be
Packit a69f91
translated to a newline.  If
Packit a69f91
.B Expect
Packit a69f91
then passes that to a program
Packit a69f91
which sets its terminal to raw mode (like telnet), there is going to
Packit a69f91
be a problem, as the program expects a true return.  (Some programs
Packit a69f91
are actually forgiving in that they will automatically translate
Packit a69f91
newlines to returns, but most don't.)  Unfortunately, there is no way to find
Packit a69f91
out that a program put its terminal into raw mode.
Packit a69f91
.PP
Packit a69f91
Rather than manually replacing newlines with returns, the solution is to
Packit a69f91
use the command "stty raw", which will stop the translation.
Packit a69f91
Note, however, that this means that you will no longer get the cooked
Packit a69f91
line-editing features.
Packit a69f91
.PP
Packit a69f91
.B interact
Packit a69f91
implicitly sets your terminal to raw mode so this problem will not arise then.
Packit a69f91
Packit a69f91
It is often useful to store passwords (or other private information)
Packit a69f91
in
Packit a69f91
.B Expect
Packit a69f91
scripts.  This is not recommended since anything that is
Packit a69f91
stored on a computer is susceptible to being accessed by anyone.
Packit a69f91
Thus, interactively prompting for passwords from a script is a smarter
Packit a69f91
idea than embedding them literally.  Nonetheless, sometimes such embedding
Packit a69f91
is the only possibility.
Packit a69f91
.PP
Packit a69f91
Unfortunately, the UNIX file system has no direct way of creating
Packit a69f91
scripts which are executable but unreadable.  Systems which support
Packit a69f91
setgid shell scripts may indirectly simulate this as follows:
Packit a69f91
.PP
Packit a69f91
Create the
Packit a69f91
.B Expect
Packit a69f91
script (that contains the secret data) as usual.
Packit a69f91
Make its permissions be 750 (\-rwxr\-x\-\-\-) and owned by a trusted group,
Packit a69f91
i.e., a group which is allowed to read it.  If necessary, create a new
Packit a69f91
group for this purpose.  Next, create a /bin/sh script with
Packit a69f91
permissions 2751 (\-rwxr\-s\-\-x) owned by the same group as before.
Packit a69f91
.PP
Packit a69f91
The result is a script which may be executed (and read) by anyone.
Packit a69f91
When invoked, it runs the
Packit a69f91
.B Expect
Packit a69f91
script.
Packit a69f91
.SH "SEE ALSO"
Packit a69f91
.BR Tcl (3),
Packit a69f91
.BR libexpect (3)
Packit a69f91
.br
Packit a69f91
.I
Packit a69f91
"Exploring Expect: A Tcl-Based Toolkit for Automating Interactive Programs"
Packit a69f91
\fRby Don Libes, pp. 602, ISBN 1-56592-090-2, O'Reilly and Associates, 1995.
Packit a69f91
.br
Packit a69f91
.I
Packit a69f91
"expect: Curing Those Uncontrollable Fits of Interactivity" \fRby Don Libes,
Packit a69f91
Proceedings of the Summer 1990 USENIX Conference,
Packit a69f91
Anaheim, California, June 11-15, 1990.
Packit a69f91
.br
Packit a69f91
.I
Packit a69f91
"Using
Packit a69f91
.B expect
Packit a69f91
to Automate System Administration Tasks" \fRby Don Libes,
Packit a69f91
Proceedings of the 1990 USENIX Large Installation Systems Administration
Packit a69f91
Conference, Colorado Springs, Colorado, October 17-19, 1990.
Packit a69f91
.br
Packit a69f91
.I
Packit a69f91
"Tcl: An Embeddable Command Language" \fRby John Ousterhout,
Packit a69f91
Proceedings of the Winter 1990 USENIX Conference,
Packit a69f91
Washington, D.C., January 22-26, 1990.
Packit a69f91
.br
Packit a69f91
.I
Packit a69f91
"expect: Scripts for Controlling Interactive Programs" \fRby Don Libes,
Packit a69f91
Computing Systems, Vol. 4, No. 2, University of California Press Journals,
Packit a69f91
November 1991.
Packit a69f91
.br
Packit a69f91
.I
Packit a69f91
"Regression Testing and Conformance Testing Interactive Programs", \fRby Don
Packit a69f91
Libes, Proceedings of the Summer 1992 USENIX Conference, pp. 135-144,
Packit a69f91
San Antonio, TX, June 12-15, 1992.
Packit a69f91
.br
Packit a69f91
.I
Packit a69f91
"Kibitz \- Connecting Multiple Interactive Programs Together", \fRby Don Libes,
Packit a69f91
Software \- Practice & Experience, John Wiley & Sons, West Sussex, England,
Packit a69f91
Vol. 23, No. 5, May, 1993.
Packit a69f91
.br
Packit a69f91
.I
Packit a69f91
"A Debugger for Tcl Applications", \fRby Don Libes,
Packit a69f91
Proceedings of the 1993 Tcl/Tk Workshop, Berkeley, CA, June 10-11, 1993.
Packit a69f91
.SH AUTHOR
Packit a69f91
Don Libes, National Institute of Standards and Technology
Packit a69f91
.SH ACKNOWLEDGMENTS
Packit a69f91
Thanks to John Ousterhout for Tcl, and Scott Paisley for inspiration.
Packit a69f91
Thanks to Rob Savoye for Expect's autoconfiguration code.
Packit a69f91
.PP
Packit a69f91
The HISTORY file documents much of the evolution of
Packit a69f91
.BR expect .
Packit a69f91
It makes interesting reading and might give you further insight to this
Packit a69f91
software.  Thanks to the people mentioned in it who sent me bug fixes
Packit a69f91
and gave other assistance.
Packit a69f91
.PP
Packit a69f91
Design and implementation of
Packit a69f91
.B Expect
Packit a69f91
was paid for in part by the U.S. government and is therefore in the public
Packit a69f91
domain.
Packit a69f91
However the author and NIST would like credit
Packit a69f91
if this program and documentation or portions of them are used.