expect_out

term

programmingnetworking

Kept in this browser · My desk

The array in which Expect leaves what its last expect command matched: the match, up to nine regular-expression groups, and the output read up to the end of the match.

expect_out(0,string) holds the matched text, expect_out(1,string) to expect_out(9,string) the groups of a -re pattern, and expect_out(buffer) everything from the start of the unmatched output to the end of the match; what follows stays in Expect's buffer for the next expect. Two habits avoid wrong readings: a group that took no part in a match keeps the value of an earlier one, so clear the array before reading groups, and inside a procedure the array is local unless it is declared global.

Expect, the Tcl (Tool Command Language) extension that Don Libes wrote at the United States National Institute of Standards and Technology (NIST) to automate interactive programs such as telnet, ftp and passwd, waits for output with its expect command and reports what it found through a Tcl array named expect_out. The manual for release 5.45.4 sets the rule: upon matching a pattern, or on eof or full_buffer, any matching and previously unmatched output is saved in expect_out(buffer), the text that matched the whole pattern in expect_out(0,string), and up to nine regular-expression substring matches in expect_out(1,string) to expect_out(9,string). With the -indices flag before a pattern, the start and end of each of those strings are added as expect_out(X,start) and expect_out(X,end), character positions counted from 0 with the end inclusive. The spawn id the output came from is stored in expect_out(spawn_id), which matters when one expect watches several processes.

The manual's two worked examples are the clearest definition. If a process has printed abcdefgh followed by a newline, expect "cd" behaves as if expect_out(0,string) had been set to cd and expect_out(buffer) to abcd, and efgh with its newline stays in Expect's internal buffer for the next expect. If it printed abbbcabkkkka and a newline, expect -indices -re "b(b*).*(k+)" sets expect_out(0,string) to bbbcabkkkk at positions 1 to 10, expect_out(1,string) to bb at 2 to 3, expect_out(2,string) to k at 10, and expect_out(buffer) to abbbcabkkkk, leaving a and the newline. Two consequences follow. The buffer element includes the match, so the text before the match is the buffer with the match removed from its end. And the part after the match is not lost, only deferred, unless the pattern was * or -re ".*", which the manual notes flush the whole buffer without reading more. The -notransfer flag leaves even the matched text in the internal buffer, which helps when experimenting.

Python's pexpect, which copied the model, splits the same information differently, and the difference trips people who move between them: after each expect(), pexpect's before holds the text up to the pattern, without it, after holds the text the pattern matched, and match holds the regular expression's match object. Expect's buffer element is pexpect's before and after joined.

The group elements are where scripts go wrong, and the source code shows why. In expect.c of release 5.45.4, after a regular expression matches, a loop walks every subexpression Tcl reports, skips any whose start is -1, meaning it took no part in the match, and writes X,string for the rest. A skipped group's element is therefore not cleared: if one expect matched -re "(a)(b)" against ab and the next matched -re "(c)|(d)" against d, expect_out(1,string) still says a. The same loop computes each group's end as the match end minus one, so an empty group at the very start of the buffer gets an end index of -1, and Tcl's own Tcl_GetRange treats a negative last index as the end of the value, returning the whole buffer instead of an empty string. The loop also runs over every subexpression rather than stopping at nine, so the limit of nine is the manual's statement rather than the code's. For glob and exact patterns only element 0 is written. The safe habit is to run array unset expect_out before an expect whose groups will be read, and to read groups only in the body of the pattern that matched.

Events other than a match matter too. The manual names a match, eof and full_buffer as the events that set the array; a timeout is not among them, so after a timeout expect_out still describes the previous match, and a script that reads it there reads stale data. At eof the source still records the spawn id and whatever output was left in expect_out(buffer), which is how a script collects the last lines a program printed before exiting. When more output arrives than match_max allows (the manual's default is 2000 bytes), earlier bytes are forgotten; whether or not a full_buffer pattern is used, the forgotten characters are written to expect_out(buffer), so the element can hold the discarded head of a long answer rather than the part a pattern was looking for. By default remove_nulls strips null bytes before output is matched or stored, so they never appear in the array.

Scope is the second trap. The manual describes Expect's liberal scoping: variables that Expect's own commands read are looked up first in the local scope and then in the global one, but variables written are always local unless a global command has been issued. An expect inside a procedure therefore fills a local expect_out that disappears when the procedure returns; either return the value or declare global expect_out. The source shows the exception: expect_background writes the array with Tcl's global-only flag, because there is no procedure to write into when a background pattern fires. The interact command keeps a parallel array, interact_out, for its own -re patterns.

When a value is not what the script expected, the fastest check is Expect's own diagnostics. exp_internal 1, or running the interpreter with -d, sends to standard error every character received and every attempt to match it against the patterns, and the source logs each assignment as a line naming the element being set, such as set expect_out(0,string) followed by its value, so the exact contents of the buffer at the moment of the match are visible rather than guessed.

Against network devices the array is how output is captured, and terminal behaviour shapes what it contains. A script that sends a show command and then expects the device prompt finds, in expect_out(buffer), the echoed command, the command's output and the prompt itself, all of which the script must trim. Lines arrive with carriage return and linefeed, because, as the manual warns, the terminal driver usually converts newlines into that pair, so a pattern spanning two lines needs \r\n rather than \n. Pagers that pause output behind a More prompt, long outputs that exceed match_max, and prompts that change with the configuration mode each produce a buffer that looks valid and is incomplete, which is why scripts disable paging first and raise match_max before asking for large outputs.

Also known as: expect_out(buffer), expect_out(0,string), expect_out(1,string)

Sources

← Back to E Back to the glossary