Services and Modeling for Embedded Software Development
Embecosm divider strip
Prev  Next

3.2.1.  Patterns for use with the expect command

There are a wide range of possible patterns that expect can use.

Plain strings

Plain strings. In the previous example the strings ERROR and "Test complete" were used. Double quotation marks are needed where the string contains a space.

eof

Matches if the stream from the spawned process reaches end of file. Typically because the spawned process has completed execution.

timeout

Matches if more than timeout seconds have passed since the spawned process started execution. The default timeout period is 10 seconds, but may be changed by setting the timeout global variable.

The timeout value may be also set for the current expect command by using the -timeout flag as a pattern (see below).

default

Equivalent to matching either eof or timeout.

-timeout t. Use t seconds as the timeout for the current expect command.

full_buffer

By default, the expect command buffers up to 2000 bytes. If more than this is encountered while reading bytes, earlier bytes will be forgotten. This keyword will match if the buffer is full, allowing this circumstance to be trapped.

[Note]Note

The size of the buffer can be increased using the procedure match_max. However very large values down will slow down the pattern matcher.

null

Matches a single ASCII character 0.

"Globbed" expressions

These are patterns specified as for the TCL string match command, which is similar to the syntax of shell regular expressions (commonly known as "glob" patterns).

It is possible that patterns might match flags to the expect command, in which case they can be protected by using the -gl flag. So for example the following would match the string "-gl".

expect {
    -gl -gl {puts "Matched -gl\n"}
}
		

In general any pattern starting with '-' should be protected using -gl to future proof against new flags.

Regular expressions

These patterns follow the syntax defined by the TCL regexp command. They are introduced with the flag -re.

Exact strings

These are prefixed by the -ex flag. This is needed for strings containing characters such as '*' that would otherwise be interpreted as part of a globbed pattern.

Case insensitive match

Prefixing any pattern by the -nocase flag will cause the input to be matched as though it were all lower case. The pattern should thus be all in lower case.

When matching a pattern, any matching and previously unmatched output is saved in the variable expect_out(buffer). The matched output may be found in expect_out(0,string).

If a regular expression with sub-expressions was used, then the matching sub-expressions (up to 9 in total) may be found in expect_out(1,string) through expect_out(1,string).

If the flag -indices was used before any match, the start and end indices of the matching string can be found in expect_out(0,start) and expect_out(0,end). Where the pattern was a regular expression, start and end positions of up to 9 sub-strings may be found in expect_out(1,start) and expect_out(1,end) through expect_out(9,start) and expect_out(9,end).

These strings are useful when evaluating the body associated with the pattern matched.

Once matching is complete, the matched output (and any preceding output) is discarded from the internal buffers. However it can be retained by preceding the match with the -notransfer flag. This has little value in finished scripts, but can help when developing and debugging tests.

Embecosm divider strip