The full_buffer Keyword

On page 146, I described how expect discards input when its internal buffer is exceeded. The special pattern full_buffer matches when no other patterns match and expect would otherwise throw away part of the input to make room for more.[29] When full_buffer matches, all of the unmatched input is moved to expect_out(buffer).

As with other special patterns, such as eof and timeout, full_buffer is only recognized when none of the -gl, -re, or -ex flags has been used.

The following fragment was written for someone who needed a program that would “spool up” a relatively slow stream from the standard input and send it to a telnet process every 3 seconds. They wanted to feed telnet with a few big chunks of data rather than lots of tiny ones because they were running on a slow network that could not afford the overhead.

set timeout 3
while 1 {
      expect_user {
        eof exit
        timeout {
            expect_user "*"
            send $expect_out(buffer)
        }
        full_buffer {send $expect_out(buffer)}

}

The program works by sitting in a loop which waits for three seconds or a full buffer, whichever comes first. If the buffer fills, it is sent out immediately. If three seconds pass, another expect command is executed to retrieve whatever data has arrived, and that data is sent to the remote side.

The expect_user command is a special version of the expect command that reads from the standard input. I will describe this command in detail in Chapter 8 (p. 188).

[29] There is a wayto obtain the discarded input without ...

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.