OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [expect/] [expect.man] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.