When The Number Of Patterns Is Not Known In Advance

The exp_expectl function is appropriate when the list of patterns is known in advance. At a minimum, the number of patterns must be known in advance.

When the number of patterns can vary, the function exp_fexpectv is more suitable. This function is called with only two arguments. The first is a file descriptor. The second is an array of pattern descriptors. The prototype is:

int exp_expectv(int fd, struct exp_case *pats);

struct exp_case is defined as follows:

struct exp_case {
    char *pattern;
    regexp *re;
    enum exp_type type;
    int value;
};

The information in an exp_case structure is exactly the same information that was passed as the direct arguments in exp_expectl. The pattern is stored in pattern. An optional compiled regular expression is stored in re. The type element describes the type of pattern and is an exp_type enumeration constant (see page 495). As before, the final pattern type must be exp_end. Finally, value is the integer returned when the associated pattern matches.

exp_expectv works slightly differently than exp_expectl when the pattern type is exp_regexp. In this case, exp_expectv compiles each pattern and stores the compiled form in re. The compiled form is left accessible in the exp_case structure for your use or reuse if exp_expectv is recalled with the same patterns. If the type is exp_regexp, then exp_expectv checks if re is initialized before compiling the pattern. The pattern is compiled only if re is not initialized. ...

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.