Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / man / man1 / expect.1
CommitLineData
920dae64
AT
1.TH EXPECT 1 "29 December 1994"
2.SH NAME
3expect \- programmed dialogue with interactive programs, Version 5
4.SH SYNOPSIS
5.B expect
6[
7.B \-dDinN
8]
9[
10.B \-c
11.I cmds
12]
13[
14[
15.BR \- [ f | b ]
16]
17.I cmdfile
18]
19[
20.I args
21]
22.SH INTRODUCTION
23.B Expect
24is a program that "talks" to other interactive programs according to a
25script. Following the script,
26.B Expect
27knows what can be expected from
28a program and what the correct response should be. An interpreted
29language provides branching and high-level control structures to
30direct the dialogue. In addition, the user can take control
31and interact directly when desired, afterward returning control to the
32script.
33.PP
34.B Expectk
35is a mixture of
36.B Expect
37and
38.BR Tk .
39It behaves just like
40.B Expect
41and
42.BR Tk 's
43.BR wish .
44.B Expect
45can also be used directly in C or C++ (that is, without Tcl).
46See libexpect(3).
47.PP
48The name "Expect" comes from the idea of
49.I send/expect
50sequences popularized
51by uucp, kermit and other modem control programs.
52However unlike uucp,
53.B Expect
54is generalized so that it can be run as a user-level command
55with any program and task in mind.
56.B Expect
57can actually talk to several programs at the same time.
58.PP
59For example, here are some things
60.B Expect
61can do:
62.RS
63.TP 4
64\(bu
65Cause your computer to dial you back,
66so that you can login without paying for the call.
67.TP
68\(bu
69Start a game (e.g., rogue) and if the optimal configuration doesn't appear,
70restart it (again and again) until it does,
71then hand over control to you.
72.TP
73\(bu
74Run fsck, and in response to its questions, answer "yes", "no" or give control back to you,
75based on predetermined criteria.
76.TP
77\(bu
78Connect to another network or BBS (e.g., MCI Mail, CompuServe) and
79automatically retrieve your mail so that it appears as if
80it was originally sent to your local system.
81.TP
82\(bu
83Carry environment variables, current directory,
84or any kind of information across rlogin, telnet, tip, su, chgrp, etc.
85.RE
86.PP
87There are a variety of reasons why the shell cannot perform these tasks.
88(Try, you'll see.)
89All are possible with
90.BR Expect .
91.PP
92In general,
93.B Expect
94is useful for running any program which requires
95interaction between the program and the user.
96All that is necessary is that the interaction can be characterized
97programmatically.
98.B Expect
99can also give the user back control
100(without halting the program being controlled) if desired.
101Similarly, the user can return control to the script at any time.
102.SH USAGE
103.B Expect
104reads
105.I cmdfile
106for a list of commands to execute.
107.B Expect
108may also be invoked implicitly on systems which support the #! notation
109by marking the script executable, and making the first line in your script:
110
111 #!/usr/local/bin/expect \-f
112
113Of course, the path must accurately describe where
114.B Expect
115lives. /usr/local/bin is just an example.
116
117The
118.B \-c
119flag prefaces a command to be executed before any in the script.
120The command should be quoted to prevent being broken up by the shell.
121This option may be used multiple times.
122Multiple commands may be
123executed with a single
124.B \-c
125by separating them with semicolons.
126Commands are executed in the order they appear.
127(When using Expectk, this option is specified as
128.BR \-command .)
129.PP
130The
131.B \-d
132flag enables some diagnostic output, which
133primarily reports internal activity of commands such as
134.B expect
135and
136.BR interact .
137This flag has the same effect as "exp_internal 1" at the beginning of an Expect
138script, plus the version of
139.B Expect
140is printed.
141(The
142.B strace
143command is useful for tracing statements, and the
144.B trace
145command is useful for tracing variable assignments.)
146(When using Expectk, this option is specified as
147.BR \-diag .)
148.PP
149The
150.B \-D
151flag enables an interactive debugger. An integer value should follow.
152The debugger will take control before the next Tcl procedure
153if the value is non-zero
154or if a ^C is pressed (or a breakpoint is hit, or other appropriate debugger
155command appears in the script). See the README file or SEE ALSO (below)
156for more information on the debugger.
157(When using Expectk, this option is specified as
158.BR \-Debug .)
159.PP
160The
161.B \-f
162flag prefaces a file from which to read commands from.
163The flag itself is optional as it is only useful when using
164the #! notation (see above),
165so that other arguments may be supplied on the command line.
166(When using Expectk, this option is specified as
167.BR \-file .)
168.PP
169By default, the command file is read into memory and executed in its entirety.
170It is occasionally desirable to read files one line at a time. For example,
171stdin is read this way. In order to force arbitrary files to be handled this
172way, use the
173.B \-b
174flag.
175(When using Expectk, this option is specified as
176.BR \-buffer .) Note that stdio-buffering may still take place however this shouldn't cause problems when reading from a fifo or stdin.
177.PP
178If the string "\-" is supplied as a filename, standard input is read instead.
179(Use "./\-" to read from a file actually named "\-".)
180.PP
181The
182.B \-i
183flag causes
184.B Expect
185to interactively prompt for commands instead of reading
186them from a file.
187Prompting is terminated via the
188.B exit
189command or upon EOF.
190See
191.B interpreter
192(below) for more information.
193.B \-i
194is assumed if neither a command file nor
195.B \-c
196is used.
197(When using Expectk, this option is specified as
198.BR \-interactive .)
199.PP
200.B \-\-
201may be used to delimit the end of the options. This is useful if
202you want to pass an option-like argument to your script without it being
203interpreted by
204.BR Expect .
205This can usefully be placed in the #! line to prevent any flag-like
206interpretation by Expect. For example, the following will leave the
207original arguments (including the script name) in the variable
208.IR argv .
209
210 #!/usr/local/bin/expect \-\-
211
212Note that the usual getopt(3) and execve(2) conventions must be observed
213when adding arguments to the #! line.
214.PP
215The file $exp_library/expect.rc is sourced automatically if present, unless
216the
217.B \-N
218flag is used.
219(When using Expectk, this option is specified as
220.BR \-NORC .)
221Immediately after this,
222the file ~/.expect.rc is sourced automatically, unless the
223.B \-n
224flag is used. If the environment variable DOTDIR is defined,
225it is treated as a directory and .expect.rc is read from there.
226(When using Expectk, this option is specified as
227.BR \-norc .)
228This sourcing occurs only after executing any
229.B \-c
230flags.
231.PP
232.B \-v
233causes Expect to print its version number and exit. (The corresponding flag
234in Expectk, which uses long flag names, is \-version.)
235.PP
236Optional
237.I args
238are constructed into a list and stored in the variable named
239.IR argv .
240.I argc
241is initialized to the length of argv.
242.PP
243.I argv0
244is defined to be the name of the script (or binary if no script is used).
245For example,
246the following prints out the name of the script and the first three arguments:
247.nf
248
249 send_user "$argv0 [lrange $argv 0 2]\\n"
250
251.fi
252.SH COMMANDS
253.B Expect
254uses
255.I Tcl
256(Tool Command Language).
257Tcl provides control flow (e.g., if, for, break),
258expression evaluation and several other features such as recursion,
259procedure definition, etc.
260Commands used here but not defined (e.g.,
261.BR set ,
262.BR if ,
263.BR exec )
264are Tcl commands (see tcl(3)).
265.B Expect
266supports additional commands, described below.
267Unless otherwise specified, commands return the empty string.
268.PP
269Commands are listed alphabetically so that they can be quickly located.
270However, new users may find it easier to start by reading the descriptions
271of
272.BR spawn ,
273.BR send ,
274.BR expect ,
275and
276.BR interact ,
277in that order.
278
279Note that the best introduction to the language (both Expect and Tcl)
280is provided in the book "Exploring Expect" (see SEE ALSO below).
281Examples are included in this man page but they are very limited since
282this man page is meant primarily as reference material.
283
284Note that in the text of this man page, "Expect" with an uppercase "E"
285refers to the
286.B Expect
287program while "expect" with a lower-case "e" refers to the
288.B expect
289command within the
290.B Expect
291program.)
292.I
293.TP 6
294.BI close " [-slave] [\-onexec 0|1] [\-i spawn_id]"
295closes the connection to the current process.
296Most interactive programs will detect EOF on their stdin and exit;
297thus
298.B close
299usually suffices to kill the process as well.
300The
301.B \-i
302flag declares the process to close corresponding to the named spawn_id.
303
304Both
305.B expect
306and
307.B interact
308will detect when the current process exits and implicitly do a
309.BR close .
310But if you kill the process by, say, "exec kill $pid",
311you will need to explicitly call
312.BR close .
313
314The
315.BR \-onexec
316flag determines whether the spawn id will be closed in any new spawned
317processes or if the process is overlayed. To leave a spawn id open,
318use the value 0. A non-zero integer value will force the spawn closed
319(the default) in any new processes.
320
321The
322.B \-slave
323flag closes the slave associated with the spawn id. (See "spawn -pty".)
324When the connection is closed, the slave is automatically closed as
325well if still open.
326
327No matter whether the connection is closed implicitly or explicitly,
328you should call
329.B wait
330to clear up the corresponding kernel process slot.
331.B close
332does not call
333.B wait
334since there is no guarantee that closing a process connection will cause
335it to exit.
336See
337.B wait
338below for more info.
339.TP
340.BI debug " [[-now] 0|1]"
341controls a Tcl debugger allowing you to step through statements, set
342breakpoints, etc.
343
344With no arguments, a 1 is returned if the debugger is not running, otherwise
345a 0 is returned.
346
347With a 1 argument, the debugger is started. With a 0 argument, the
348debugger is stopped. If a 1 argument is preceded by the
349.B \-now
350flag, the debugger is started immediately (i.e., in the middle of the
351.B debug
352command itself). Otherwise, the debugger is started with the next
353Tcl statement.
354
355The
356.B debug
357command does not change any traps. Compare this to starting Expect with the
358.B -D
359flag (see above).
360
361See the README file or SEE ALSO (below)
362for more information on the debugger.
363.TP
364.B disconnect
365disconnects a forked process from the terminal. It continues running in the
366background. The process is given its own process group (if possible).
367Standard I/O is redirected to /dev/null.
368.IP
369The following fragment uses
370.B disconnect
371to continue running the script in the background.
372.nf
373
374 if {[fork]!=0} exit
375 disconnect
376 . . .
377
378.fi
379The following script reads a password, and then runs a program
380every hour that demands a password each time it is run. The script supplies
381the password so that you only have to type it once.
382(See the
383.B stty
384command which demonstrates how to turn off password echoing.)
385.nf
386
387 send_user "password?\\ "
388 expect_user -re "(.*)\\n"
389 for {} 1 {} {
390 if {[fork]!=0} {sleep 3600;continue}
391 disconnect
392 spawn priv_prog
393 expect Password:
394 send "$expect_out(1,string)\\r"
395 . . .
396 exit
397 }
398
399.fi
400An advantage to using
401.B disconnect
402over the shell asynchronous process feature (&) is that
403.B Expect
404can
405save the terminal parameters prior to disconnection, and then later
406apply them to new ptys. With &,
407.B Expect
408does not have a chance
409to read the terminal's parameters since the terminal is already
410disconnected by the time
411.B Expect
412receives control.
413.TP
414.BI exit " [\-opts] [status]"
415causes
416.B Expect
417to exit or otherwise prepare to do so.
418
419The
420.B \-onexit
421flag causes the next argument to be used as an exit handler.
422Without an argument, the current exit handler is returned.
423
424The
425.B \-noexit
426flag causes
427.B Expect
428to prepare to exit but stop short of actually returning control to the
429operating system. The user-defined exit handler is run as well as Expect's
430own internal handlers.
431No further Expect commands should be executed.
432This is useful if you are running Expect with other Tcl extensions.
433The current interpreter (and main window if in the Tk environment) remain
434so that other Tcl extensions can clean up. If Expect's
435.B exit
436is called again (however this might occur), the handlers are not rerun.
437
438Upon exiting,
439all connections to spawned processes are closed. Closure will be detected
440as an EOF by spawned processes.
441.B exit
442takes no other actions beyond what the normal _exit(2) procedure does.
443Thus, spawned processes that do not check for EOF may continue to run.
444(A variety of conditions are important to determining, for example, what
445signals a spawned process will be sent, but these are system-dependent,
446typically documented under exit(3).)
447Spawned processes that continue to run will be inherited by init.
448
449.I status
450(or 0 if not specified) is returned as the exit status of
451.BR Expect .
452.B exit
453is implicitly executed if the end of the script is reached.
454.TP
455\fBexp_continue\fR [-continue_timer]
456The command
457.B exp_continue
458allows
459.B expect
460itself to continue
461executing rather than returning as it normally would. By
462default
463.B exp_continue
464resets the timeout timer. The
465.I -continue_timer
466flag prevents timer from being restarted. (See
467.B expect
468for more information.)
469.TP
470.BI exp_internal " [\-f file] value"
471causes further commands to send diagnostic information internal to
472.B Expect
473to stderr if
474.I value
475is non-zero. This output is disabled if
476.I value
477is 0. The diagnostic information includes every character received,
478and every attempt made to match the current output against the patterns.
479.IP
480If the optional
481.I file
482is supplied, all normal and debugging output is written to that file
483(regardless of the value of
484.IR value ).
485Any previous diagnostic output file is closed.
486
487The
488.B \-info
489flag causes exp_internal to return a description of the
490most recent non-info arguments given.
491.TP
492.BI exp_open " [args] [\-i spawn_id]"
493returns a Tcl file identifier that corresponds to the original spawn id.
494The file identifier can then be used as if it were opened by Tcl's
495.B open
496command. (The spawn id should no longer be used. A
497.B wait
498should not be executed.
499
500The
501.B \-leaveopen
502flag leaves the spawn id open for access through
503Expect commands. A
504.B wait
505must be executed on the spawn id.
506.TP
507.BI exp_pid " [\-i spawn_id]"
508returns the process id corresponding to the currently spawned process.
509If the
510.B \-i
511flag is used, the pid returned corresponds to that of the given spawn id.
512.TP
513.B exp_send
514is an alias for
515.BR send .
516.TP
517.B exp_send_error
518is an alias for
519.BR send_error .
520.TP
521.B exp_send_log
522is an alias for
523.BR send_log .
524.TP
525.B exp_send_tty
526is an alias for
527.BR send_tty .
528.TP
529.B exp_send_user
530is an alias for
531.BR send_user .
532.TP
533.BI exp_version " [[\-exit] version]"
534is useful for assuring that the script is compatible with the current
535version of Expect.
536.IP
537With no arguments, the current version of
538.B Expect
539is returned. This version
540may then be encoded in your script. If you actually know that you are not
541using features of recent versions, you can specify an earlier version.
542.IP
543Versions consist of three numbers separated by dots. First
544is the major number. Scripts written for versions of
545.B Expect
546with a
547different major number will almost certainly not work.
548.B exp_version
549returns an error if the major numbers do not match.
550.IP
551Second is the minor number. Scripts written for a version with a
552greater minor number than the current version
553may depend upon some new feature and might not run.
554.B exp_version
555returns an error if the major numbers match, but the script minor number
556is greater than that of the running
557.BR Expect .
558.IP
559Third is a number that plays no part in the version comparison.
560However, it is incremented when the
561.B Expect
562software
563distribution is changed in any way, such as by additional documentation
564or optimization. It is reset to 0 upon each new minor version.
565.IP
566With the
567.B \-exit
568flag,
569.B Expect
570prints an error and exits if the version is out of date.
571.TP
572.BI expect " [[\-opts] pat1 body1] ... [\-opts] patn [bodyn]"
573waits until one of the patterns matches the output of a spawned process,
574a specified time period has passed, or an end-of-file is seen.
575If the final body is empty, it may be omitted.
576.IP
577Patterns from the most recent
578.B expect_before
579command are implicitly used before any other patterns.
580Patterns from the most recent
581.B expect_after
582command are implicitly used after any other patterns.
583.IP
584If the arguments to the entire
585.B expect
586statement require more than one line,
587all the arguments may be "braced" into one so as to avoid terminating each
588line with a backslash. In this one case, the usual Tcl substitutions will
589occur despite the braces.
590.IP
591If a pattern is the keyword
592.BR eof ,
593the corresponding body is executed upon end-of-file.
594If a pattern is the keyword
595.BR timeout ,
596the corresponding body is executed upon timeout. If no timeout keyword
597is used, an implicit null action is executed upon timeout.
598The default timeout period is 10 seconds but may be set, for example to 30,
599by the command "set timeout 30". An infinite timeout may be designated
600by the value \-1.
601If a pattern is the keyword
602.BR default ,
603the corresponding body is executed upon either timeout or end-of-file.
604.IP
605If a pattern matches, then the corresponding body is executed.
606.B expect
607returns the result of the body (or the empty string if no pattern matched).
608In the event that multiple patterns match, the one appearing first is
609used to select a body.
610.IP
611Each time new output arrives, it is compared to each pattern in the order
612they are listed. Thus, you may test for absence of a match by making
613the last pattern something guaranteed to appear, such as a prompt.
614In situations where there is no prompt, you must use
615.B timeout
616(just like you would if you were interacting manually).
617.IP
618Patterns are specified in three ways. By default,
619patterns are specified as with Tcl's
620.B string match
621command. (Such patterns are also similar to C-shell regular expressions
622usually referred to as "glob" patterns). The
623.B \-gl
624flag may may
625be used to protect patterns that might otherwise match
626.B expect
627flags from doing so.
628Any pattern beginning with a "-" should be protected this way. (All strings
629starting with "-" are reserved for future options.)
630
631.IP
632For example, the following fragment looks for a successful login.
633(Note that
634.B abort
635is presumed to be a procedure defined elsewhere in the script.)
636.nf
637
638.ta \w' expect 'u +\w'invalid password 'u
639 expect {
640 busy {puts busy\\n ; exp_continue}
641 failed abort
642 "invalid password" abort
643 timeout abort
644 connected
645 }
646
647.fi
648Quotes are necessary on the fourth pattern since it contains a space, which
649would otherwise separate the pattern from the action.
650Patterns with the same action (such as the 3rd and 4th) require listing the
651actions again. This can be avoid by using regexp-style patterns (see below).
652More information on forming glob-style patterns can be found in the Tcl manual.
653.IP
654Regexp-style patterns follow the syntax defined by Tcl's
655.B regexp
656(short for "regular expression") command.
657regexp patterns are introduced with the flag
658.BR \-re .
659The previous example can be rewritten using a regexp as:
660.nf
661
662.ta \w' expect 'u +\w'connected 'u
663 expect {
664 busy {puts busy\\n ; exp_continue}
665 \-re "failed|invalid password" abort
666 timeout abort
667 connected
668 }
669
670.fi
671Both types of patterns are "unanchored". This means that patterns
672do not have to match the entire string, but can begin and end the
673match anywhere in the string (as long as everything else matches).
674Use ^ to match the beginning of a string, and $ to match the end.
675Note that if you do not wait for the end of a string, your responses
676can easily end up in the middle of the string as they are echoed from
677the spawned process. While still producing correct results, the output
678can look unnatural. Thus, use of $ is encouraged if you can exactly
679describe the characters at the end of a string.
680
681Note that in many editors, the ^ and $ match the beginning and end of
682lines respectively. However, because expect is not line oriented,
683these characters match the beginning and end of the data (as opposed
684to lines) currently in the expect matching buffer. (Also, see the
685note below on "system indigestion.")
686
687The
688.B \-ex
689flag causes the pattern to be matched as an "exact" string. No
690interpretation of *, ^, etc is made (although the usual Tcl
691conventions must still be observed).
692Exact patterns are always unanchored.
693
694.IP
695The
696.B \-nocase
697flag causes uppercase characters of the output to compare as if they were
698lowercase characters. The pattern is not affected.
699.IP
700While reading output,
701more than 2000 bytes can force earlier bytes to be "forgotten".
702This may be changed with the function
703.BR match_max .
704(Note that excessively large values can slow down the pattern matcher.)
705If
706.I patlist
707is
708.BR full_buffer ,
709the corresponding body is executed if
710.I match_max
711bytes have been received and no other patterns have matched.
712Whether or not the
713.B full_buffer
714keyword is used, the forgotten characters are written to
715expect_out(buffer).
716
717If
718.I patlist
719is the keyword
720.BR null ,
721and nulls are allowed (via the
722.B remove_nulls
723command), the corresponding body is executed if a single ASCII
7240 is matched.
725It is not possible to
726match 0 bytes via glob or regexp patterns.
727
728Upon matching a pattern (or eof or full_buffer),
729any matching and previously unmatched output is saved in the variable
730.IR expect_out(buffer) .
731Up to 9 regexp substring matches are saved in the variables
732.I expect_out(1,string)
733through
734.IR expect_out(9,string) .
735If the
736.B -indices
737flag is used before a pattern,
738the starting and ending indices (in a form suitable for
739.BR lrange )
740of the
74110 strings are stored in the variables
742.I expect_out(X,start)
743and
744.I expect_out(X,end)
745where X is a digit, corresponds to the substring position in the buffer.
7460 refers to strings which matched the entire pattern
747and is generated for glob patterns as well as regexp patterns.
748For example, if a process has produced output of "abcdefgh\\n", the result of:
749.nf
750
751 expect "cd"
752
753.fi
754is as if the following statements had executed:
755.nf
756
757 set expect_out(0,string) cd
758 set expect_out(buffer) abcd
759
760.fi
761and "efgh\\n" is left in the output buffer.
762If a process produced the output "abbbcabkkkka\\n", the result of:
763.nf
764
765 expect \-indices \-re "b(b*).*(k+)"
766
767.fi
768is as if the following statements had executed:
769.nf
770
771 set expect_out(0,start) 1
772 set expect_out(0,end) 10
773 set expect_out(0,string) bbbcabkkkk
774 set expect_out(1,start) 2
775 set expect_out(1,end) 3
776 set expect_out(1,string) bb
777 set expect_out(2,start) 10
778 set expect_out(2,end) 10
779 set expect_out(2,string) k
780 set expect_out(buffer) abbbcabkkkk
781
782.fi
783and "a\\n" is left in the output buffer. The pattern "*" (and -re ".*") will
784flush the output buffer without reading any more output from the
785process.
786.IP
787Normally, the matched output is discarded from Expect's internal buffers.
788This may be prevented by prefixing a pattern with the
789.B \-notransfer
790flag. This flag is especially useful in experimenting (and can be
791abbreviated to "-not" for convenience while experimenting).
792
793The spawn id associated with the matching output (or eof or
794full_buffer) is stored in
795.IR expect_out(spawn_id) .
796
797The
798.B \-timeout
799flag causes the current expect command to use the following value
800as a timeout instead of using the value of the timeout variable.
801
802By default,
803patterns are matched against output from the current process, however the
804.B \-i
805flag declares the output from the named spawn_id list be matched against
806any following patterns (up to the next
807.BR \-i ).
808The spawn_id list should either be a whitespace separated list of spawn_ids
809or a variable referring to such a list of spawn_ids.
810
811For example, the following example waits for
812"connected" from the current process, or "busy", "failed" or "invalid
813password" from the spawn_id named by $proc2.
814.nf
815
816 expect {
817 \-i $proc2 busy {puts busy\\n ; exp_continue}
818 \-re "failed|invalid password" abort
819 timeout abort
820 connected
821 }
822
823.fi
824The value of the global variable
825.I any_spawn_id
826may be used to match patterns to any spawn_ids that are named
827with all other
828.B \-i
829flags in the current
830.B expect
831command.
832The spawn_id from a
833.B \-i
834flag with no associated pattern (i.e., followed immediately
835by another
836.BR \-i )
837is made available to any other patterns
838in the same
839.B expect
840command associated with
841.I any_spawn_id.
842
843The
844.B \-i
845flag may also name a global variable in which case the variable is read
846for a list of spawn ids. The variable is reread whenever it changes.
847This provides a way of changing the I/O source while the command is in
848execution. Spawn ids provided this way are called "indirect" spawn ids.
849
850Actions such as
851.B break
852and
853.B continue
854cause control structures (i.e.,
855.BR for ,
856.BR proc )
857to behave in the usual way.
858The command
859.B exp_continue
860allows
861.B expect
862itself to continue
863executing rather than returning as it normally would.
864.IP
865This is useful for avoiding explicit loops or repeated expect statements.
866The following example is part of a fragment to automate rlogin. The
867.B exp_continue
868avoids having to write a second
869.B expect
870statement (to look for the prompt again) if the rlogin prompts for a password.
871.nf
872
873 expect {
874 Password: {
875 stty -echo
876 send_user "password (for $user) on $host: "
877 expect_user -re "(.*)\\n"
878 send_user "\\n"
879 send "$expect_out(1,string)\\r"
880 stty echo
881 exp_continue
882 } incorrect {
883 send_user "invalid password or account\\n"
884 exit
885 } timeout {
886 send_user "connection to $host timed out\\n"
887 exit
888 } eof {
889 send_user \\
890 "connection to host failed: $expect_out(buffer)"
891 exit
892 } -re $prompt
893 }
894
895.fi
896For example, the following fragment might help a user guide
897an interaction that is already totally automated. In this case, the terminal
898is put into raw mode. If the user presses "+", a variable is incremented.
899If "p" is pressed, several returns are sent to the process,
900perhaps to poke it in some way, and "i" lets the user interact with the
901process, effectively stealing away control from the script.
902In each case, the
903.B exp_continue
904allows the current
905.B expect
906to continue pattern matching after executing the
907current action.
908.nf
909
910 stty raw \-echo
911 expect_after {
912 \-i $user_spawn_id
913 "p" {send "\\r\\r\\r"; exp_continue}
914 "+" {incr foo; exp_continue}
915 "i" {interact; exp_continue}
916 "quit" exit
917 }
918
919.fi
920.IP
921By default,
922.B exp_continue
923resets the timeout timer. The timer is not restarted, if
924.B exp_continue
925is called with the
926.B \-continue_timer
927flag.
928.TP
929.BI expect_after " [expect_args]"
930works identically to the
931.B expect_before
932except that if patterns from both
933.B expect
934and
935.B expect_after
936can match, the
937.B expect
938pattern is used. See the
939.B expect_before
940command for more information.
941.TP
942.BI expect_background " [expect_args]"
943takes the same arguments as
944.BR expect ,
945however it returns immediately.
946Patterns are tested whenever new input arrives.
947The pattern
948.B timeout
949and
950.B default
951are meaningless to
952.BR expect_background
953and are silently discarded.
954Otherwise, the
955.B expect_background
956command uses
957.B expect_before
958and
959.B expect_after
960patterns just like
961.B expect
962does.
963
964When
965.B expect_background
966actions are being evaluated, background processing for the same
967spawn id is blocked. Background processing is unblocked when
968the action completes. While background processing is blocked,
969it is possible to do a (foreground)
970.B expect
971on the same spawn id.
972
973It is not possible to execute an
974.B expect
975while an
976.B expect_background
977is unblocked.
978.B expect_background
979for a particular spawn id is deleted by
980declaring a new expect_background with the same spawn id. Declaring
981.B expect_background
982with no pattern removes the given spawn id
983from the ability to match patterns in the background.
984.TP
985.BI expect_before " [expect_args]"
986takes the same arguments as
987.BR expect ,
988however it returns immediately.
989Pattern-action pairs from the most recent
990.B expect_before
991with the same spawn id are implicitly added to any following
992.B expect
993commands. If a pattern matches, it is treated as if it had been
994specified in the
995.B expect
996command itself, and the associated body is executed in the context
997of the
998.B expect
999command.
1000If patterns from both
1001.B expect_before
1002and
1003.B expect
1004can match, the
1005.B expect_before
1006pattern is used.
1007
1008If no pattern is specified, the spawn id is not checked for any patterns.
1009
1010Unless overridden by a
1011.B \-i
1012flag,
1013.B expect_before
1014patterns match against the spawn id defined at the time that the
1015.B expect_before
1016command was executed (not when its pattern is matched).
1017
1018The \-info flag causes
1019.B expect_before
1020to return the current specifications of what patterns it will match.
1021By default, it reports on the current spawn id. An optional spawn id specification may be given for information on that spawn id. For example
1022.nf
1023
1024 expect_before -info -i $proc
1025
1026.fi
1027At most one spawn id specification may be given. The flag \-indirect
1028suppresses direct spawn ids that come only from indirect specifications.
1029
1030Instead of a spawn id specification, the flag "-all" will cause
1031"-info" to report on all spawn ids.
1032
1033The output of the \-info flag can be reused as the argument to expect_before.
1034.TP
1035.BI expect_tty " [expect_args]"
1036is like
1037.B expect
1038but it reads characters from /dev/tty (i.e. keystrokes from the user).
1039By default, reading is performed in cooked mode.
1040Thus, lines must end with a return in order for
1041.B expect
1042to see them.
1043This may be changed via
1044.B stty
1045(see the
1046.B stty
1047command below).
1048.TP
1049.BI expect_user " [expect_args]"
1050is like
1051.B expect
1052but it reads characters from stdin (i.e. keystrokes from the user).
1053By default, reading is performed in cooked mode.
1054Thus, lines must end with a return in order for
1055.B expect
1056to see them.
1057This may be changed via
1058.B stty
1059(see the
1060.B stty
1061command below).
1062.TP
1063.B fork
1064creates a new process. The new process is an exact copy of the current
1065.B Expect
1066process. On success,
1067.B fork
1068returns 0 to the new (child) process and returns the process ID of the child
1069process to the parent process.
1070On failure (invariably due to lack of resources, e.g., swap space, memory),
1071.B fork
1072returns \-1 to the parent process, and no child process is created.
1073.IP
1074Forked processes exit via the
1075.B exit
1076command, just like the original process.
1077Forked processes are allowed to write to the log files. If you do not
1078disable debugging or logging in most of the processes, the result can be
1079confusing.
1080.IP
1081Some pty implementations may be confused by multiple readers and writers,
1082even momentarily. Thus, it is safest to
1083.B fork
1084before spawning processes.
1085.TP
1086.BI interact " [string1 body1] ... [stringn [bodyn]]"
1087gives control of the current process to the user, so that
1088keystrokes are sent to the current process,
1089and the stdout and stderr of the current process are returned.
1090.IP
1091String-body pairs may be specified as arguments, in which case the
1092body is executed when the corresponding string is entered. (By default, the
1093string is not sent to the current process.) The
1094.B interpreter
1095command is assumed, if the final body is missing.
1096.IP
1097If the arguments to the entire
1098.B interact
1099statement require more than one line,
1100all the arguments may be "braced" into one so as to avoid terminating each
1101line with a backslash. In this one case, the usual Tcl substitutions will
1102occur despite the braces.
1103.IP
1104For example, the following command runs interact with the following
1105string-body pairs defined: When ^Z is pressed,
1106.B Expect
1107is suspended.
1108(The
1109.B \-reset
1110flag restores the terminal modes.)
1111When ^A is pressed, the user sees "you typed a control-A" and the
1112process is sent a ^A. When $ is pressed, the user sees the date.
1113When ^C is pressed,
1114.B Expect
1115exits. If "foo" is entered, the user sees "bar".
1116When ~~ is pressed, the
1117.B Expect
1118interpreter runs interactively.
1119.nf
1120
1121.ta \w' interact 'u +\w'$CTRLZ 'u +\w'{'u
1122 set CTRLZ \\032
1123 interact {
1124 -reset $CTRLZ {exec kill \-STOP [pid]}
1125 \\001 {send_user "you typed a control\-A\\n";
1126 send "\\001"
1127 }
1128 $ {send_user "The date is [exec date]."}
1129 \\003 exit
1130 foo {send_user "bar"}
1131 ~~
1132 }
1133
1134.fi
1135.IP
1136In string-body pairs, strings are matched in the order they are listed
1137as arguments. Strings that partially match are not sent to the
1138current process in anticipation of the remainder coming. If
1139characters are then entered such that there can no longer possibly be
1140a match, only the part of the string will be sent to the process that cannot
1141possibly begin another match. Thus, strings that are substrings of
1142partial matches can match later, if the original strings that was attempting
1143to be match ultimately fails.
1144.IP
1145By default, string matching is exact with no wild cards. (In contrast,
1146the
1147.B expect
1148command uses glob-style patterns by default.) The
1149.B \-ex
1150flag may be used to protect patterns that might otherwise match
1151.B interact
1152flags from doing so.
1153Any pattern beginning with a "-" should be protected this way. (All strings
1154starting with "-" are reserved for future options.)
1155
1156The
1157.B \-re
1158flag forces the string to be interpreted as a regexp-style pattern. In this
1159case, matching substrings are stored in the variable
1160.I interact_out
1161similarly to the way
1162.B expect
1163stores its output in the variable
1164.BR expect_out .
1165The
1166.B \-indices
1167flag is similarly supported.
1168
1169The pattern
1170.B eof
1171introduces an action that is
1172executed upon end-of-file. A separate
1173.B eof
1174pattern may also follow the
1175.B \-output
1176flag in which case it is matched if an eof is detected while writing output.
1177The default
1178.B eof
1179action is "return", so that
1180.B interact
1181simply returns upon any EOF.
1182
1183The pattern
1184.B timeout
1185introduces a timeout (in seconds) and action that is executed
1186after no characters have been read for a given time.
1187The
1188.B timeout
1189pattern applies to the most recently specified process.
1190There is no default timeout.
1191The special variable "timeout" (used by the
1192.B expect
1193command) has no affect on this timeout.
1194
1195For example, the following statement could be used to autologout users who have
1196not typed anything for an hour but who still get frequent system
1197messages:
1198.nf
1199
1200 interact -input $user_spawn_id timeout 3600 return -output \\
1201 $spawn_id
1202
1203.fi
1204
1205If the pattern is the keyword
1206.BR null ,
1207and nulls are allowed (via the
1208.B remove_nulls
1209command), the corresponding body is executed if a single ASCII
12100 is matched.
1211It is not possible to
1212match 0 bytes via glob or regexp patterns.
1213
1214Prefacing a pattern with the flag
1215.B \-iwrite
1216causes the variable
1217.I interact_out(spawn_id)
1218to be set to the spawn_id which matched the pattern
1219(or eof).
1220
1221Actions such as
1222.B break
1223and
1224.B continue
1225cause control structures (i.e.,
1226.BR for ,
1227.BR proc )
1228to behave in the usual way.
1229However
1230.B return
1231causes interact to return to its caller, while
1232.B inter_return
1233causes
1234.B interact
1235to cause a return in its caller. For example, if "proc foo" called
1236.B interact
1237which then executed the action
1238.BR inter_return ,
1239.B proc foo
1240would return. (This means that if
1241.B interact
1242calls
1243.B interpreter
1244interactively typing
1245.B return
1246will cause the interact to continue, while
1247.B inter_return
1248will cause the interact to return to its caller.)
1249.IP
1250During
1251.BR interact ,
1252raw mode is used so that all characters may be passed to the current process.
1253If the current process does not catch job control signals,
1254it will stop if sent a stop signal (by default ^Z).
1255To restart it, send a continue signal (such as by "kill \-CONT <pid>").
1256If you really want to send a SIGSTOP to such a process (by ^Z),
1257consider spawning csh first and then running your program.
1258On the other hand, if you want to send a SIGSTOP to
1259.B Expect
1260itself, first call interpreter (perhaps by using an escape character), and then press ^Z.
1261.IP
1262String-body pairs can be used as a shorthand for avoiding having
1263to enter the interpreter and execute commands interactively. The previous
1264terminal mode is used while the body of a string-body pair is being executed.
1265.IP
1266For speed, actions execute in raw mode by default. The
1267.B \-reset
1268flag resets the terminal to the mode it had before
1269.B interact
1270was executed (invariably, cooked mode).
1271Note that characters entered when the mode is being switched may be lost
1272(an unfortunate feature of the terminal driver on some systems).
1273The only reason to use
1274.B \-reset
1275is if your action
1276depends on running in cooked mode.
1277.IP
1278The
1279.B \-echo
1280flag sends characters that match the following pattern back to the process
1281that generated them as each character is read. This may be useful
1282when the user needs to see feedback from partially typed patterns.
1283.IP
1284If a pattern is being echoed but eventually fails to match,
1285the characters are sent to the spawned process. If the spawned
1286process then echoes them, the user will see the characters twice.
1287.B \-echo
1288is probably only appropriate in situations where the user is
1289unlikely to not complete the pattern. For example, the following
1290excerpt is from rftp, the recursive-ftp script, where the user is
1291prompted to enter ~g, ~p, or ~l, to get, put, or list the current
1292directory recursively. These are so far away from the normal ftp
1293commands, that the user is unlikely to type ~ followed by anything
1294else, except mistakenly, in which case, they'll probably just ignore
1295the result anyway.
1296.nf
1297
1298 interact {
1299 -echo ~g {getcurdirectory 1}
1300 -echo ~l {getcurdirectory 0}
1301 -echo ~p {putcurdirectory}
1302 }
1303
1304.fi
1305The
1306.B \-nobuffer
1307flag sends characters that match the following pattern on to
1308the output process as characters are read.
1309
1310This is useful when you wish to let a program echo back the pattern.
1311For example, the following might be used to monitor where a person is
1312dialing (a Hayes-style modem). Each time "atd" is seen the script
1313logs the rest of the line.
1314.nf
1315
1316 proc lognumber {} {
1317 interact -nobuffer -re "(.*)\\r" return
1318 puts $log "[exec date]: dialed $interact_out(1,string)"
1319 }
1320
1321 interact -nobuffer "atd" lognumber
1322
1323.fi
1324.IP
1325During
1326.BR interact ,
1327previous use of
1328.B log_user
1329is ignored. In particular,
1330.B interact
1331will force its output to be logged (sent to the standard output)
1332since it is presumed the user doesn't wish to interact blindly.
1333.IP
1334The
1335.B \-o
1336flag causes any following key-body pairs to be applied to the output of
1337the current process.
1338This can be useful, for example, when dealing with hosts that
1339send unwanted characters during a telnet session.
1340.IP
1341By default,
1342.B interact
1343expects the user to be writing stdin and reading stdout of the
1344.B Expect
1345process
1346itself.
1347The
1348.B \-u
1349flag (for "user") makes
1350.B interact
1351look for the user as the process named by its argument
1352(which must be a spawned id).
1353.IP
1354This allows two unrelated processes to be joined
1355together without using an explicit loop. To aid in debugging, Expect
1356diagnostics always go to stderr (or stdout for certain logging and
1357debugging information). For the same reason, the
1358.B interpreter
1359command will read interactively from stdin.
1360.IP
1361For example, the following fragment creates a login process.
1362Then it dials the user (not shown), and finally connects the two together.
1363Of course, any process may be substituted for login.
1364A shell, for example, would allow the user to work without supplying an
1365account and password.
1366.nf
1367
1368 spawn login
1369 set login $spawn_id
1370 spawn tip modem
1371 # dial back out to user
1372 # connect user to login
1373 interact \-u $login
1374
1375.fi
1376To send output to multiple processes, list each spawn id list prefaced by a
1377.B \-output
1378flag. Input for a group of output spawn ids may be determined
1379by a spawn id list prefaced by a
1380.B \-input
1381flag. (Both
1382.B \-input
1383and
1384.B \-output
1385may take lists in the same form as the
1386.B \-i
1387flag in the
1388.B expect
1389command, except that any_spawn_id is not meaningful in
1390.BR interact .)
1391All following flags and
1392strings (or patterns) apply to this input until another -input flag appears.
1393If no
1394.B \-input
1395appears,
1396.B \-output
1397implies "\-input $user_spawn_id \-output".
1398(Similarly, with patterns that do not have
1399.BR \-input .)
1400If one
1401.B \-input
1402is specified, it overrides $user_spawn_id. If a second
1403.B \-input
1404is specified,
1405it overrides $spawn_id. Additional
1406.B \-input
1407flags may be specified.
1408
1409The two implied input processes default to having their outputs specified as
1410$spawn_id and $user_spawn_id (in reverse).
1411If a
1412.B \-input
1413flag appears
1414with no
1415.B \-output
1416flag, characters from that process are discarded.
1417
1418The
1419.B \-i
1420flag introduces a replacement for the current spawn_id when no
1421other
1422.B \-input
1423or
1424.B \-output
1425flags are used. A \-i flag implies a \-o flag.
1426
1427It is possible to change the processes that are being interacted with
1428by using indirect spawn ids. (Indirect spawn ids are described in the
1429section on the expect command.) Indirect spawn ids may be specified
1430with the -i, -u, -input, or -output flags.
1431.TP
1432.B interpreter " [args]"
1433causes the user to be interactively prompted for
1434.B Expect
1435and Tcl commands.
1436The result of each command is printed.
1437.IP
1438Actions such as
1439.B break
1440and
1441.B continue
1442cause control structures (i.e.,
1443.BR for ,
1444.BR proc )
1445to behave in the usual way.
1446However
1447.B return
1448causes interpreter to return to its caller, while
1449.B inter_return
1450causes
1451.B interpreter
1452to cause a return in its caller. For example, if "proc foo" called
1453.B interpreter
1454which then executed the action
1455.BR inter_return ,
1456.B proc foo
1457would return.
1458Any other command causes
1459.B interpreter
1460to continue prompting for new commands.
1461.IP
1462By default, the prompt contains two integers.
1463The first integer describes the depth of
1464the evaluation stack (i.e., how many times Tcl_Eval has been called). The
1465second integer is the Tcl history identifier. The prompt can be set by
1466defining a procedure called "prompt1" whose return value becomes the next
1467prompt. If a statement has open quotes, parens, braces, or brackets, a
1468secondary prompt (by default "+> ") is issued upon newline. The secondary
1469prompt may be set by defining a procedure called "prompt2".
1470.IP
1471During
1472.BR interpreter ,
1473cooked mode is used, even if the its caller was using raw mode.
1474.IP
1475If stdin is closed,
1476.B interpreter
1477will return unless the
1478.B \-eof
1479flag is used, in which case the subsequent argument is invoked.
1480.TP
1481.BI log_file " [args] [[\-a] file]"
1482If a filename is provided,
1483.B log_file
1484will record a transcript of the session (beginning at that point) in the file.
1485.B log_file
1486will stop recording if no argument is given. Any previous log file is closed.
1487
1488Instead of a filename, a Tcl file identifier may be provided by using the
1489.B \-open
1490or
1491.B \-leaveopen
1492flags. This is similar to the
1493.B spawn
1494command. (See
1495.B spawn
1496for more info.)
1497
1498The
1499.B \-a
1500flag forces output to be logged that was suppressed by the
1501.B log_user
1502command.
1503
1504By default, the
1505.B log_file
1506command
1507.I appends
1508to old files rather than truncating them,
1509for the convenience of being able to turn logging off and on multiple
1510times in one session.
1511To truncate files, use the
1512.B \-noappend
1513flag.
1514
1515The
1516.B -info
1517flag causes log_file to return a description of the
1518most recent non-info arguments given.
1519.TP
1520.BI log_user " -info|0|1"
1521By default, the send/expect dialogue is logged to stdout
1522(and a logfile if open).
1523The logging to stdout is disabled by the command "log_user 0"
1524and reenabled by "log_user 1". Logging to the logfile is unchanged.
1525
1526The
1527.B -info
1528flag causes log_user to return a description of the
1529most recent non-info arguments given.
1530.TP
1531.BI match_max " [\-d] [\-i spawn_id] [size]"
1532defines the size of the buffer (in bytes) used internally by
1533.BR expect .
1534With no
1535.I size
1536argument, the current size is returned.
1537.IP
1538With the
1539.B \-d
1540flag, the default size is set. (The initial default is 2000.)
1541With the
1542.B \-i
1543flag, the size is set for the named spawn id, otherwise it is set for
1544the current process.
1545.TP
1546.BI overlay " [\-# spawn_id] [\-# spawn_id] [...] program [args]"
1547executes
1548.IR "program args"
1549in place of the current
1550.B Expect
1551program, which terminates.
1552A bare hyphen argument forces a hyphen in front of the command name as if
1553it was a login shell.
1554All spawn_ids are closed except for those named as arguments. These
1555are mapped onto the named file identifiers.
1556.IP
1557Spawn_ids are mapped to file identifiers for the new program to inherit.
1558For example, the following line runs chess and allows it to be
1559controlled by the current process \- say, a chess master.
1560.nf
1561
1562 overlay \-0 $spawn_id \-1 $spawn_id \-2 $spawn_id chess
1563
1564.fi
1565This is more efficient than
1566"interact \-u", however, it sacrifices the ability to do programmed
1567interaction since the
1568.B Expect
1569process is no longer in control.
1570.IP
1571Note that no controlling terminal is provided. Thus, if you
1572disconnect or remap standard input, programs that do
1573job control (shells, login, etc) will not function properly.
1574.TP
1575.BI parity " [\-d] [\-i spawn_id] [value]"
1576defines whether parity should be retained or stripped from the output of
1577spawned processes. If
1578.I value
1579is zero, parity is stripped, otherwise it is not stripped.
1580With no
1581.I value
1582argument, the current value is returned.
1583.IP
1584With the
1585.B \-d
1586flag, the default parity value is set. (The initial default is 1, i.e.,
1587parity is not stripped.)
1588With the
1589.B \-i
1590flag, the parity value is set for the named spawn id, otherwise it is set for
1591the current process.
1592.TP
1593.BI remove_nulls " [\-d] [\-i spawn_id] [value]"
1594defines whether nulls are retained or removed from the output of
1595spawned processes before pattern matching
1596or storing in the variable
1597.I expect_out
1598or
1599.IR interact_out .
1600If
1601.I value
1602is 1, nulls are removed. If
1603.I value
1604is 0, nulls are not removed.
1605With no
1606.I value
1607argument, the current value is returned.
1608.IP
1609With the
1610.B \-d
1611flag, the default value is set. (The initial default is 1, i.e.,
1612nulls are removed.)
1613With the
1614.B \-i
1615flag, the value is set for the named spawn id, otherwise it is set for
1616the current process.
1617
1618Whether or not nulls are removed,
1619.B Expect
1620will record null bytes to the log and stdout.
1621.TP
1622.BI send " [\-flags] string"
1623Sends
1624.IR string
1625to the current process.
1626For example, the command
1627.nf
1628
1629 send "hello world\\r"
1630
1631.fi
1632sends the characters, h e l l o <blank> w o r l d <return> to the
1633current process.
1634(Tcl includes a printf-like command (called
1635.BR format )
1636which can build arbitrarily complex strings.)
1637.IP
1638Characters are sent immediately although programs with line-buffered input
1639will not read the characters until a return character is sent. A return
1640character is denoted "\\r".
1641
1642The
1643.B \-\-
1644flag forces the next argument to be interpreted as a string rather than a flag.
1645Any string can be preceded by "\-\-" whether or not it actually looks
1646like a flag. This provides a reliable mechanism to specify variable strings
1647without being tripped up by those that accidentally look like flags.
1648(All strings starting with "-" are reserved for future options.)
1649
1650The
1651.B \-i
1652flag declares that the string be sent to the named spawn_id.
1653If the spawn_id is
1654.IR user_spawn_id ,
1655and the terminal is in raw mode, newlines in the string are translated
1656to return-newline
1657sequences so that they appear as if the terminal was in cooked mode.
1658The
1659.B \-raw
1660flag disables this translation.
1661
1662The
1663.BR \-null
1664flag sends null characters (0 bytes). By default, one null is sent.
1665An integer may follow the
1666.BR \-null
1667to indicate how many nulls to send.
1668
1669The
1670.B \-break
1671flag generates a break condition. This only makes sense if the spawn
1672id refers to a tty device opened via "spawn -open". If you have
1673spawned a process such as tip, you should use tip's convention for
1674generating a break.
1675
1676The
1677.B \-s
1678flag forces output to be sent "slowly", thus avoid the common situation
1679where a computer outtypes an input buffer that was designed for a
1680human who would never outtype the same buffer. This output is
1681controlled by the value of the variable "send_slow" which takes a two
1682element list. The first element is an integer that describes the
1683number of bytes to send atomically. The second element is a real
1684number that describes the number of seconds by which the atomic sends
1685must be separated. For example, "set send_slow {10 .001}" would force
1686"send \-s" to send strings with 1 millisecond in between each 10
1687characters sent.
1688
1689The
1690.B \-h
1691flag forces output to be sent (somewhat) like a human actually typing.
1692Human-like delays appear between the characters. (The algorithm is
1693based upon a Weibull distribution, with modifications to suit this
1694particular application.) This output is controlled by the value of
1695the variable "send_human" which takes a five element list. The first
1696two elements are average interarrival time of characters in seconds.
1697The first is used by default. The second is used at word endings, to
1698simulate the subtle pauses that occasionally occur at such
1699transitions. The third parameter is a measure of variability where .1
1700is quite variable, 1 is reasonably variable, and 10 is quite
1701invariable. The extremes are 0 to infinity. The last two parameters
1702are, respectively, a minimum and maximum interarrival time.
1703The minimum and maximum are used last and "clip" the final time.
1704The ultimate average can be quite different from the given average
1705if the minimum and maximum clip enough values.
1706
1707As an
1708example, the following command emulates a fast and
1709consistent typist:
1710.nf
1711
1712 set send_human {.1 .3 1 .05 2}
1713 send \-h "I'm hungry. Let's do lunch."
1714
1715.fi
1716while the following might be more suitable after a hangover:
1717.nf
1718
1719 set send_human {.4 .4 .2 .5 100}
1720 send \-h "Goodd party lash night!"
1721
1722.fi
1723Note that errors are not simulated, although you can set up error
1724correction situations yourself by embedding mistakes and corrections
1725in a send argument.
1726
1727The flags for sending null characters, for sending breaks, for forcing slow
1728output and for human-style output are mutually exclusive. Only the one
1729specified last will be used. Furthermore, no
1730.I string
1731argument can be specified with the flags for sending null characters or breaks.
1732
1733It is a good idea to precede the first
1734.B send
1735to a process by an
1736.BR expect .
1737.B expect
1738will wait for the process to start, while
1739.B send
1740cannot.
1741In particular, if the first
1742.B send
1743completes before the process starts running,
1744you run the risk of having your data ignored.
1745In situations where interactive programs offer no initial prompt,
1746you can precede
1747.B send
1748by a delay as in:
1749.nf
1750
1751 # To avoid giving hackers hints on how to break in,
1752 # this system does not prompt for an external password.
1753 # Wait for 5 seconds for exec to complete
1754 spawn telnet very.secure.gov
1755 sleep 5
1756 send password\\r
1757
1758.fi
1759.B exp_send
1760is an alias for
1761.BI send .
1762If you are using Expectk or some other variant of Expect in the Tk environment,
1763.B send
1764is defined by Tk for an entirely different purpose.
1765.B exp_send
1766is provided for compatibility between environments.
1767Similar aliases are provided for other Expect's other send commands.
1768.TP
1769.BI send_error " [\-flags] string"
1770is like
1771.BR send ,
1772except that the output is sent to stderr rather than the current
1773process.
1774.TP
1775.BI send_log " [\--] string"
1776is like
1777.BR send ,
1778except that the string is only sent to the log file (see
1779.BR log_file .)
1780The arguments are ignored if no log file is open.
1781.TP
1782.BI send_tty " [\-flags] string"
1783is like
1784.BR send ,
1785except that the output is sent to /dev/tty rather than the current
1786process.
1787.TP
1788.BI send_user " [\-flags] string"
1789is like
1790.BR send ,
1791except that the output is sent to stdout rather than the current
1792process.
1793.TP
1794.BI sleep " seconds"
1795causes the script to sleep for the given number of seconds.
1796Seconds may be a decimal number. Interrupts (and Tk events if you
1797are using Expectk) are processed while Expect sleeps.
1798.TP
1799.BI spawn " [args] program [args]"
1800creates a new process running
1801.IR "program args" .
1802Its stdin, stdout and stderr are connected to Expect,
1803so that they may be read and written by other
1804.B Expect
1805commands.
1806The connection is broken by
1807.B close
1808or if the process itself closes any of the file identifiers.
1809.IP
1810When a process is started by
1811.BR spawn ,
1812the variable
1813.I spawn_id
1814is set to a descriptor referring to that process.
1815The process described by
1816.I spawn_id
1817is considered the
1818.IR "current process" .
1819.I spawn_id
1820may be read or written, in effect providing job control.
1821.IP
1822.I user_spawn_id
1823is a global variable containing a descriptor which refers to the user.
1824For example, when
1825.I spawn_id
1826is set to this value,
1827.B expect
1828behaves like
1829.BR expect_user .
1830
1831.I
1832.I error_spawn_id
1833is a global variable containing a descriptor which refers to the standard
1834error.
1835For example, when
1836.I spawn_id
1837is set to this value,
1838.B send
1839behaves like
1840.BR send_error .
1841.IP
1842.I tty_spawn_id
1843is a global variable containing a descriptor which refers to /dev/tty.
1844If /dev/tty does not exist (such as in a cron, at, or batch script), then
1845.I tty_spawn_id
1846is not defined. This may be tested as:
1847.nf
1848
1849 if {[info vars tty_spawn_id]} {
1850 # /dev/tty exists
1851 } else {
1852 # /dev/tty doesn't exist
1853 # probably in cron, batch, or at script
1854 }
1855
1856.fi
1857.IP
1858.B spawn
1859returns the UNIX process id. If no process is spawned, 0 is returned.
1860The variable
1861.I spawn_out(slave,name)
1862is set to the name of the pty slave device.
1863.IP
1864By default,
1865.B spawn
1866echoes the command name and arguments. The
1867.B \-noecho
1868flag stops
1869.B spawn
1870from doing this.
1871.IP
1872The
1873.B \-console
1874flag causes console output to be redirected to the spawned process.
1875This is not supported on all systems.
1876
1877Internally,
1878.B spawn
1879uses a pty, initialized the same way as the user's tty. This is further
1880initialized so that all settings are "sane" (according to stty(1)).
1881If the variable
1882.I stty_init
1883is defined, it is interpreted in the style of stty arguments
1884as further configuration.
1885For example, "set stty_init raw" will cause further spawned processes's
1886terminals to start in raw mode.
1887.B \-nottycopy
1888skips the initialization based on the user's tty.
1889.B \-nottyinit
1890skips the "sane" initialization.
1891.IP
1892Normally,
1893.B spawn
1894takes little time to execute. If you notice spawn taking a
1895significant amount of time, it is probably encountering ptys that are
1896wedged. A number of tests are run on ptys to avoid entanglements with
1897errant processes. (These take 10 seconds per wedged pty.) Running
1898Expect with the
1899.B \-d
1900option will show if
1901.B Expect
1902is encountering many ptys in odd states. If you cannot kill
1903the processes to which these ptys are attached, your only recourse may
1904be to reboot.
1905
1906If
1907.I program
1908cannot be spawned successfully because exec(2) fails (e.g. when
1909.I program
1910doesn't exist), an error message will be returned by the next
1911.B interact
1912or
1913.B expect
1914command as if
1915.I program
1916had run and produced the error message as output.
1917This behavior is a natural consequence of the implementation of
1918.BR spawn .
1919Internally, spawn forks, after which the spawned process has no
1920way to communicate with the original
1921.B Expect
1922process except by communication
1923via the spawn_id.
1924
1925The
1926.B \-open
1927flag causes the next argument to be interpreted as a Tcl file identifier
1928(i.e., returned by
1929.BR open .)
1930The spawn id can then be used as if it were a spawned process. (The file
1931identifier should no longer be used.)
1932This lets you treat raw devices, files, and
1933pipelines as spawned processes without using a pty. 0 is returned to
1934indicate there is no associated process. When the connection to
1935the spawned process is closed, so is the Tcl file identifier.
1936The
1937.B \-leaveopen
1938flag is similar to
1939.B \-open
1940except that
1941.B \-leaveopen
1942causes the file identifier to be left open even after the spawn id is closed.
1943
1944The
1945.B \-pty
1946flag causes a pty to be opened but no process spawned. 0 is returned
1947to indicate there is no associated process. Spawn_id is set as usual.
1948
1949The variable
1950.I spawn_out(slave,fd)
1951is set to a file identifier corresponding to the pty slave.
1952It can be closed using "close -slave".
1953
1954The
1955.B \-ignore
1956flag names a signal to be ignored in the spawned process.
1957Otherwise, signals get the default behavior.
1958Signals are named as in the
1959.B trap
1960command, except that each signal requires a separate flag.
1961.TP
1962.BI strace " level"
1963causes following statements to be printed before being executed.
1964(Tcl's trace command traces variables.)
1965.I level
1966indicates how far down in the call stack to trace.
1967For example,
1968the following command runs
1969.B Expect
1970while tracing the first 4 levels of calls,
1971but none below that.
1972.nf
1973
1974 expect \-c "strace 4" script.exp
1975
1976.fi
1977
1978The
1979.B -info
1980flag causes strace to return a description of the
1981most recent non-info arguments given.
1982.TP
1983.BI stty " args"
1984changes terminal modes similarly to the external stty command.
1985
1986By default, the controlling terminal is accessed. Other terminals can
1987be accessed by appending "< /dev/tty..." to the command. (Note that
1988the arguments should not be grouped into a single argument.)
1989
1990Requests for status return it as the result of the command. If no status
1991is requested and the controlling terminal is accessed, the previous
1992status of the raw and echo attributes are returned in a form which can
1993later be used by the command.
1994
1995For example, the arguments
1996.B raw
1997or
1998.B \-cooked
1999put the terminal into raw mode.
2000The arguments
2001.B \-raw
2002or
2003.B cooked
2004put the terminal into cooked mode.
2005The arguments
2006.B echo
2007and
2008.B \-echo
2009put the terminal into echo and noecho mode respectively.
2010.IP
2011The following example illustrates how to temporarily disable echoing.
2012This could be used in otherwise-automatic
2013scripts to avoid embedding passwords in them.
2014(See more discussion on this under EXPECT HINTS below.)
2015.nf
2016
2017 stty \-echo
2018 send_user "Password: "
2019 expect_user -re "(.*)\\n"
2020 set password $expect_out(1,string)
2021 stty echo
2022
2023.fi
2024.TP
2025.BI system " args"
2026gives
2027.I args
2028to sh(1) as input,
2029just as if it had been typed as a command from a terminal.
2030.B Expect
2031waits until the shell terminates.
2032The return status from sh is handled the same way that
2033.B exec
2034handles its return status.
2035.IP
2036In contrast to
2037.B exec
2038which redirects stdin and stdout to the script,
2039.B system
2040performs no redirection
2041(other than that indicated by the string itself).
2042Thus, it is possible to use programs which must talk directly to /dev/tty.
2043For the same reason, the results of
2044.B system
2045are not recorded in the log.
2046.TP
2047.BI timestamp " [args]"
2048returns a timestamp.
2049With no arguments, the number of
2050seconds since the epoch is returned.
2051
2052The
2053.B \-format
2054flag introduces a string which is returned but with
2055substitutions made according to the
2056POSIX rules for strftime. For example %a is replaced by an abbreviated
2057weekday name (i.e., Sat). Others are:
2058.nf
2059 %a abbreviated weekday name
2060 %A full weekday name
2061 %b abbreviated month name
2062 %B full month name
2063 %c date-time as in: Wed Oct 6 11:45:56 1993
2064 %d day of the month (01-31)
2065 %H hour (00-23)
2066 %I hour (01-12)
2067 %j day (001-366)
2068 %m month (01-12)
2069 %M minute (00-59)
2070 %p am or pm
2071 %S second (00-61)
2072 %u day (1-7, Monday is first day of week)
2073 %U week (00-53, first Sunday is first day of week one)
2074 %V week (01-53, ISO 8601 style)
2075 %w day (0-6)
2076 %W week (00-53, first Monday is first day of week one)
2077 %x date-time as in: Wed Oct 6 1993
2078 %X time as in: 23:59:59
2079 %y year (00-99)
2080 %Y year as in: 1993
2081 %Z timezone (or nothing if not determinable)
2082 %% a bare percent sign
2083
2084.fi
2085Other % specifications are undefined. Other characters will be passed
2086through untouched. Only the C locale is supported.
2087
2088The
2089.B \-seconds
2090flag introduces a number of seconds since the epoch to be used as a source
2091from which to format. Otherwise, the current time is used.
2092
2093The
2094.B \-gmt
2095flag forces timestamp output to use the GMT timezone. With no flag,
2096the local timezone is used.
2097.TP
2098.BI trap " [[command] signals]"
2099causes the given
2100.I command
2101to be executed upon future receipt of any of the given signals.
2102The command is executed in the global scope.
2103If
2104.I command
2105is absent, the signal action is returned.
2106If
2107.I command
2108is the string SIG_IGN, the signals are ignored.
2109If
2110.I command
2111is the string SIG_DFL, the signals are result to the system default.
2112.I signals
2113is either a single signal or a list of signals. Signals may be specified
2114numerically or symbolically as per signal(3). The "SIG" prefix may be omitted.
2115
2116With no arguments (or the argument \-number),
2117.B trap
2118returns the signal number of the trap command currently being executed.
2119
2120The
2121.B \-code
2122flag uses the return code of the command in place of whatever code Tcl
2123was about to return when the command originally started running.
2124
2125The
2126.B \-interp
2127flag causes the command to be evaluated using the interpreter
2128active at the time the command started running
2129rather than when the trap was declared.
2130
2131The
2132.B \-name
2133flag causes the
2134.B trap
2135command to return the signal name of the trap command currently being executed.
2136
2137The
2138.B \-max
2139flag causes the
2140.B trap
2141command to return the largest signal number that can be set.
2142
2143For example, the command "trap {send_user "Ouch!"} SIGINT" will print "Ouch!"
2144each time the user presses ^C.
2145
2146By default, SIGINT (which can usually be generated by pressing ^C) and
2147SIGTERM cause Expect to exit. This is due to the following trap, created
2148by default when Expect starts.
2149.nf
2150
2151 trap exit {SIGINT SIGTERM}
2152
2153.fi
2154If you use the -D flag to start the debugger, SIGINT is redefined
2155to start the interactive debugger. This is due to the following trap:
2156.nf
2157
2158 trap {exp_debug 1} SIGINT
2159
2160.fi
2161The debugger trap can be changed by setting the environment variable
2162EXPECT_DEBUG_INIT to a new trap command.
2163
2164You can, of course, override both of these just by adding trap
2165commands to your script. In particular, if you have your own "trap
2166exit SIGINT", this will override the debugger trap. This is useful
2167if you want to prevent users from getting to the debugger at all.
2168
2169If you want to define your own trap on SIGINT but still trap to the
2170debugger when it is running, use:
2171.nf
2172
2173 if {![exp_debug]} {trap mystuff SIGINT}
2174
2175.fi
2176Alternatively, you can trap to the debugger using some other signal.
2177
2178.B trap
2179will not let you override the action for SIGALRM as this is used internally
2180to
2181.BR Expect .
2182The disconnect command sets SIGALRM to SIG_IGN (ignore). You can reenable
2183this as long as you disable it during subsequent spawn commands.
2184
2185See signal(3) for more info.
2186.TP
2187.BI wait " [args]"
2188delays until a spawned process (or
2189the current process if none is named) terminates.
2190.IP
2191.B wait
2192normally returns a list of four integers.
2193The first integer is the pid of the process that was waited upon.
2194The second integer is the corresponding spawn id.
2195The third integer is -1 if an operating system error occurred, or 0 otherwise.
2196If the third integer was 0, the fourth integer is the status returned by
2197the spawned process. If the third integer was -1, the fourth integer is
2198the value of errno set by the operating system. The global variable
2199errorCode is also set.
2200
2201Additional elements may appear at the end of the return value from
2202.BR wait .
2203An optional fifth element identifies a class of information.
2204Currently, the only possible value for this element is CHILDKILLED in
2205which case the next two values are the C-style signal name and a short
2206textual description.
2207.IP
2208The
2209.B \-i
2210flag declares the process to wait corresponding to the named spawn_id
2211(NOT the process id).
2212Inside a SIGCHLD handler,
2213it is possible to wait for any spawned process by using the spawn id -1.
2214
2215The
2216.B \-nowait
2217flag causes the wait to return immediately with the indication of a
2218successful wait. When the process exits (later), it will automatically
2219disappear without the need for an explicit wait.
2220
2221The
2222.B wait
2223command may also be used wait for a forked process using the arguments
2224"-i -1". Unlike its use with spawned processes, this command can be
2225executed at any time. There is no control over which process is
2226reaped. However, the return value can be checked for the process id.
2227
2228.SH LIBRARIES
2229Expect automatically knows about two built-in libraries for Expect scripts.
2230These are defined by the directories named in the variables
2231exp_library and exp_exec_library. Both are meant to contain utility
2232files that can be used by other scripts.
2233
2234exp_library contains architecture-independent files. exp_exec_library
2235contains architecture-dependent files. Depending on your system, both
2236directories may be totally empty. The existence of the file
2237$exp_exec_library/cat-buffers describes whether your /bin/cat buffers
2238by default.
2239.SH PRETTY-PRINTING
2240A vgrind definition is available for pretty-printing
2241.B Expect
2242scripts.
2243Assuming the vgrind definition supplied with the
2244.B Expect
2245distribution is
2246correctly installed, you can use it as:
2247.nf
2248
2249 vgrind \-lexpect file
2250
2251.fi
2252.SH EXAMPLES
2253It many not be apparent how to put everything together that the man page
2254describes. I encourage you to read and try out the examples in
2255the example directory of the
2256.B Expect
2257distribution.
2258Some of them are real programs. Others are simply illustrative
2259of certain techniques, and of course, a couple are just quick hacks.
2260The INSTALL file has a quick overview of these programs.
2261.PP
2262The
2263.B Expect
2264papers (see SEE ALSO) are also useful. While some papers
2265use syntax corresponding to earlier versions of Expect, the accompanying
2266rationales are still valid and go into a lot more detail than this
2267man page.
2268.SH CAVEATS
2269Extensions may collide with Expect's command names. For example,
2270.B send
2271is defined by Tk for an entirely different purpose.
2272For this reason, most of the
2273.B Expect
2274commands are also available as "exp_XXXX".
2275Commands and variables beginning with "exp", "inter", "spawn",
2276and "timeout" do not have aliases.
2277Use the extended command names if you need this compatibility between environments.
2278
2279.B Expect
2280takes a rather liberal view of scoping.
2281In particular, variables read by commands specific to the
2282.B Expect
2283program will be sought first from the local scope, and if not found, in the
2284global scope. For example, this
2285obviates the need to place "global timeout" in every
2286procedure you write that uses
2287.BR expect .
2288On the other hand, variables written are always in the local scope (unless
2289a "global" command has been issued). The most common problem this causes
2290is when spawn is executed in a procedure. Outside the procedure,
2291.I spawn_id
2292no longer exists, so the spawned process is no longer accessible
2293simply because of scoping. Add a "global spawn_id" to such a procedure.
2294
2295If you cannot enable the multispawning capability
2296(i.e., your system supports neither select (BSD *.*), poll (SVR>2),
2297nor something equivalent),
2298.B Expect
2299will only be able to control a single process at a time.
2300In this case, do not attempt to set
2301.IR spawn_id ,
2302nor should you execute processes via exec while a spawned process
2303is running. Furthermore, you will not be able to
2304.B expect
2305from multiple processes (including the user as one) at the same time.
2306
2307Terminal parameters can have a big effect on scripts. For example, if
2308a script is written to look for echoing, it will misbehave if echoing
2309is turned off. For this reason, Expect forces sane terminal
2310parameters by default. Unfortunately, this can make things unpleasant
2311for other programs. As an example, the emacs shell wants to change
2312the "usual" mappings: newlines get mapped to newlines instead of
2313carriage-return newlines, and echoing is disabled. This allows one to
2314use emacs to edit the input line. Unfortunately, Expect cannot
2315possibly guess this.
2316
2317You can request that Expect not override its default setting of
2318terminal parameters, but you must then be very careful when writing
2319scripts for such environments. In the case of emacs, avoid depending
2320upon things like echoing and end-of-line mappings.
2321
2322The commands that accepted arguments braced into a single list (the
2323.B expect
2324variants and
2325.BR interact )
2326use a heuristic to decide if the list is actually one argument or
2327many. The heuristic can fail only in the case when the list actually
2328does represent a single argument which has multiple embedded \\n's
2329with non-whitespace characters between them. This seems sufficiently
2330improbable, however the argument "\-nobrace" can be used to force a
2331single argument to be handled as a single argument. This could
2332conceivably be used with machine-generated Expect code. Similarly,
2333-brace forces a single argument to be handle as multiple patterns/actions.
2334
2335.SH BUGS
2336It was really tempting to name the program "sex" (for either "Smart EXec"
2337or "Send-EXpect"), but good sense (or perhaps just Puritanism) prevailed.
2338
2339On some systems, when a shell is spawned, it complains about not being
2340able to access the tty but runs anyway. This means your system has a
2341mechanism for gaining the controlling tty that
2342.B Expect
2343doesn't know about. Please find out what it is, and send this information
2344back to me.
2345
2346Ultrix 4.1 (at least the latest versions around here) considers
2347timeouts of above 1000000 to be equivalent to 0.
2348
2349Digital UNIX 4.0A (and probably other versions) refuses to allocate
2350ptys if you define a SIGCHLD handler. See grantpt page for more info.
2351
2352IRIX 6.0 does not handle pty permissions correctly so that if Expect
2353attempts to allocate a pty previously used by someone else, it fails.
2354Upgrade to IRIX 6.1.
2355
2356Telnet (verified only under SunOS 4.1.2) hangs if TERM is not set.
2357This is a problem under cron, at and in cgi scripts, which do not
2358define TERM. Thus, you must set it explicitly - to what type is
2359usually irrelevant. It just has to be set to something! The
2360following probably suffices for most cases.
2361.nf
2362
2363 set env(TERM) vt100
2364
2365.fi
2366
2367Tip (verified only under BSDI BSD/OS 3.1 i386) hangs if SHELL and HOME
2368are not set. This is a problem under cron, at and in cgi scripts,
2369which do not define these environment variables. Thus, you must set
2370them explicitly - to what type is usually irrelevant. It just has to
2371be set to something! The following probably suffices for most cases.
2372.nf
2373
2374 set env(SHELL) /bin/sh
2375 set env(HOME) /usr/local/bin
2376
2377.fi
2378
2379
2380Some implementations of ptys are designed so that the kernel throws
2381away any unread output after 10 to 15 seconds (actual number is
2382implementation-dependent) after the process has closed the file
2383descriptor. Thus
2384.B Expect
2385programs such as
2386.nf
2387
2388 spawn date
2389 sleep 20
2390 expect
2391
2392.fi
2393will fail. To avoid this, invoke non-interactive programs with
2394.B exec
2395rather than
2396.BR spawn .
2397While such situations are conceivable, in practice I have never
2398encountered a situation in which the final output of a truly
2399interactive program would be lost due to this behavior.
2400
2401On the other hand, Cray UNICOS ptys throw away any unread output
2402immediately after the process has closed the file descriptor. I have
2403reported this to Cray and they are working on a fix.
2404
2405Sometimes a delay is required between a prompt and a response, such as
2406when a tty interface is changing UART settings or matching baud rates
2407by looking for start/stop bits. Usually, all this is require is to
2408sleep for a second or two. A more robust technique is to retry until
2409the hardware is ready to receive input. The following example uses
2410both strategies:
2411.nf
2412
2413 send "speed 9600\\r";
2414 sleep 1
2415 expect {
2416 timeout {send "\\r"; exp_continue}
2417 $prompt
2418 }
2419
2420.fi
2421
2422trap \-code will not work with any command that sits in Tcl's event
2423loop, such as sleep. The problem is that in the event loop, Tcl
2424discards the return codes from async event handlers. A workaround is
2425to set a flag in the trap code. Then check the flag immediately after
2426the command (i.e., sleep).
2427
2428The expect_background command ignores -timeout arguments and has no
2429concept of timeouts in general.
2430
2431.SH EXPECT HINTS
2432There are a couple of things about
2433.B Expect
2434that may be non-intuitive.
2435This section attempts to address some of these things with a couple of
2436suggestions.
2437
2438A common expect problem is how to recognize shell prompts. Since
2439these are customized differently by differently people and different
2440shells, portably automating rlogin can be difficult without knowing
2441the prompt. A reasonable convention is to have users store a regular
2442expression describing their prompt (in particular, the end of it) in
2443the environment variable EXPECT_PROMPT. Code like the following
2444can be used. If EXPECT_PROMPT doesn't exist, the code still has a good chance of functioning correctly.
2445.nf
2446
2447 set prompt "(%|#|\\\\$) $" ;# default prompt
2448 catch {set prompt $env(EXPECT_PROMPT)}
2449
2450 expect -re $prompt
2451
2452.fi
2453I encourage you to write
2454.B expect
2455patterns that include the end of whatever
2456you expect to see. This avoids the possibility of answering a question
2457before seeing the entire thing. In addition, while you may well be
2458able to answer questions before seeing them entirely, if you answer
2459early, your answer may appear echoed back in the middle of the question.
2460In other words, the resulting dialogue will be correct but look scrambled.
2461
2462Most prompts include a space character at the end.
2463For example, the prompt from ftp is 'f', 't', 'p', '>' and <blank>.
2464To match this prompt, you must account for each of these characters.
2465It is a common mistake not to include the blank.
2466Put the blank in explicitly.
2467
2468If you use a pattern of the form X*, the * will match all the output
2469received from the end of X to the last thing received.
2470This sounds intuitive but can be somewhat confusing because the phrase
2471"last thing received" can vary depending upon the speed of the computer
2472and the processing of I/O both by the kernel and the device driver.
2473.PP
2474In particular, humans tend to see program output arriving in huge chunks
2475(atomically) when in reality most programs produce output one
2476line at a time. Assuming this is the case, the * in the pattern of the
2477previous paragraph may only match the end of the current line even though
2478there seems to be more, because at the time of the match that was all
2479the output that had been received.
2480.PP
2481.B expect
2482has no way of knowing that further output is coming unless your
2483pattern specifically accounts for it.
2484.PP
2485Even depending on line-oriented buffering is unwise. Not only do programs
2486rarely make promises about the type of buffering they do, but system
2487indigestion can break output lines up so that lines break at seemingly
2488random places. Thus, if you can express the last few characters
2489of a prompt when writing patterns, it is wise to do so.
2490
2491If you are waiting for a pattern in the last output of a program
2492and the program emits something else instead, you will not be able to
2493detect that with the
2494.B timeout
2495keyword. The reason is that
2496.B expect
2497will not timeout \- instead it will get an
2498.B eof
2499indication.
2500Use that instead. Even better, use both. That way if that line
2501is ever moved around, you won't have to edit the line itself.
2502
2503Newlines are usually converted to carriage return, linefeed sequences
2504when output by the terminal driver. Thus, if you want a pattern that
2505explicitly matches the two lines, from, say, printf("foo\\nbar"),
2506you should use the pattern "foo\\r\\nbar".
2507.PP
2508A similar translation occurs when reading from the user, via
2509.BR expect_user .
2510In this case, when you press return, it will be
2511translated to a newline. If
2512.B Expect
2513then passes that to a program
2514which sets its terminal to raw mode (like telnet), there is going to
2515be a problem, as the program expects a true return. (Some programs
2516are actually forgiving in that they will automatically translate
2517newlines to returns, but most don't.) Unfortunately, there is no way to find
2518out that a program put its terminal into raw mode.
2519.PP
2520Rather than manually replacing newlines with returns, the solution is to
2521use the command "stty raw", which will stop the translation.
2522Note, however, that this means that you will no longer get the cooked
2523line-editing features.
2524.PP
2525.B interact
2526implicitly sets your terminal to raw mode so this problem will not arise then.
2527
2528It is often useful to store passwords (or other private information)
2529in
2530.B Expect
2531scripts. This is not recommended since anything that is
2532stored on a computer is susceptible to being accessed by anyone.
2533Thus, interactively prompting for passwords from a script is a smarter
2534idea than embedding them literally. Nonetheless, sometimes such embedding
2535is the only possibility.
2536.PP
2537Unfortunately, the UNIX file system has no direct way of creating
2538scripts which are executable but unreadable. Systems which support
2539setgid shell scripts may indirectly simulate this as follows:
2540.PP
2541Create the
2542.B Expect
2543script (that contains the secret data) as usual.
2544Make its permissions be 750 (\-rwxr\-x\-\-\-) and owned by a trusted group,
2545i.e., a group which is allowed to read it. If necessary, create a new
2546group for this purpose. Next, create a /bin/sh script with
2547permissions 2751 (\-rwxr\-s\-\-x) owned by the same group as before.
2548.PP
2549The result is a script which may be executed (and read) by anyone.
2550When invoked, it runs the
2551.B Expect
2552script.
2553.SH SEE ALSO
2554.BR Tcl (3),
2555.BR libexpect (3)
2556.br
2557.I
2558"Exploring Expect: A Tcl-Based Toolkit for Automating Interactive Programs"
2559\fRby Don Libes, pp. 602, ISBN 1-56592-090-2, O'Reilly and Associates, 1995.
2560.br
2561.I
2562"expect: Curing Those Uncontrollable Fits of Interactivity" \fRby Don Libes,
2563Proceedings of the Summer 1990 USENIX Conference,
2564Anaheim, California, June 11-15, 1990.
2565.br
2566.I
2567"Using
2568.B expect
2569to Automate System Administration Tasks" \fRby Don Libes,
2570Proceedings of the 1990 USENIX Large Installation Systems Administration
2571Conference, Colorado Springs, Colorado, October 17-19, 1990.
2572.br
2573.I
2574"Tcl: An Embeddable Command Language" \fRby John Ousterhout,
2575Proceedings of the Winter 1990 USENIX Conference,
2576Washington, D.C., January 22-26, 1990.
2577.br
2578.I
2579"expect: Scripts for Controlling Interactive Programs" \fRby Don Libes,
2580Computing Systems, Vol. 4, No. 2, University of California Press Journals,
2581November 1991.
2582.br
2583.I
2584"Regression Testing and Conformance Testing Interactive Programs", \fRby Don
2585Libes, Proceedings of the Summer 1992 USENIX Conference, pp. 135-144,
2586San Antonio, TX, June 12-15, 1992.
2587.br
2588.I
2589"Kibitz \- Connecting Multiple Interactive Programs Together", \fRby Don Libes,
2590Software \- Practice & Experience, John Wiley & Sons, West Sussex, England,
2591Vol. 23, No. 5, May, 1993.
2592.br
2593.I
2594"A Debugger for Tcl Applications", \fRby Don Libes,
2595Proceedings of the 1993 Tcl/Tk Workshop, Berkeley, CA, June 10-11, 1993.
2596.SH AUTHOR
2597Don Libes, National Institute of Standards and Technology
2598.SH ACKNOWLEDGMENTS
2599Thanks to John Ousterhout for Tcl, and Scott Paisley for inspiration.
2600Thanks to Rob Savoye for Expect's autoconfiguration code.
2601.PP
2602The HISTORY file documents much of the evolution of
2603.BR expect .
2604It makes interesting reading and might give you further insight to this
2605software. Thanks to the people mentioned in it who sent me bug fixes
2606and gave other assistance.
2607.PP
2608Design and implementation of
2609.B Expect
2610was paid for in part by the U.S. government and is therefore in the public
2611domain.
2612However the author and NIST would like credit
2613if this program and documentation or portions of them are used.