#define INIT declarations#define GETC(void) getc code#define PEEKC(void) peekc code#define UNGETC(void) ungetc code#define RETURN(ptr) return code#define ERROR(val) error code
#include <regexp.h>
char *compile(char *instring, char *expbuf, char *endbuf, int eof);
int step(char *string, char *expbuf);
int advance(char *string, char *expbuf);
extern char *loc1, *loc2, *locs;
- only Basic Regular Expressions are supported
- the Internationalization features--character class, equivalence class, and multi-character collation--are not supported.
The functions step(), advance(), and compile() are general purpose regular expression matching routines to be used in programs that perform regular expression matching. These functions are defined by the <regexp.h> header.
The functions step() and advance() do pattern matching given a character string and a compiled regular expression as input.
The function compile() takes as input a regular expression as defined below and produces a compiled expression that can be used with step() or advance().
The following one-character RE s match a single character:
The following rules may be used to construct RE s from one-character RE s:
A RE may be constrained to match words.
An entire RE may be constrained to match only an initial segment or final segment of a line (or both).
The null RE (for example, //) is equivalent to the last RE encountered.
The character ^ at the beginning of an expression permits a successful match only immediately after a newline, and the character $ at the end of an expression requires a trailing newline.
Two characters have special meaning only when used within square brackets. The character - denotes a range, [c-c], unless it is just after the open bracket or before the closing bracket, [-c] or [c-] in which case it has no special meaning. When used within brackets, the character ^ has the meaning complement of if it immediately follows the open bracket (example: [^c]); elsewhere between brackets (example: [c^]) it stands for the ordinary character ^.
The special meaning of the \ operator can be escaped only by preceding it with another \, for example \\.
compile(instring, expbuf, endbuf, eof)
The first parameter, instring, is never used explicitly by the compile() routine but is useful for programs that pass down different pointers to input characters. It is sometimes used in the INIT declaration (see below). Programs which call functions to input characters or have characters in an external array can pass down a value of (char *)0 for this parameter.
The next parameter, expbuf, is a character pointer. It points to the place where the compiled regular expression will be placed.
The parameter endbuf is one more than the highest address where the compiled regular expression may be placed. If the compiled expression cannot fit in (endbuf-expbuf) bytes, a call to ERROR(50) is made.
The parameter eof is the character which marks the end of the regular expression. This character is usually a /.
Each program that includes the <regexp.h> header file must have a #define statement for INIT . It is used for dependent declarations and initializations. Most often it is used to set a register variable to point to the beginning of the regular expression so that this register variable can be used in the declarations for GETC , PEEKC , and UNGETC . Otherwise it can be used to declare external variables that might be used by GETC , PEEKC and UNGETC . (See EXAMPLES below.)
The second parameter, expbuf, is the compiled regular expression which was obtained by a call to the function compile().
The function step() returns non-zero if some substring of string matches the regular expression in expbuf and 0 if there is no match. If there is a match, two external character pointers are set as a side effect to the call to step(). The variable loc1 points to the first character that matched the regular expression; the variable loc2 points to the character after the last character that matches the regular expression. Thus if the regular expression matches the entire input string, loc1 will point to the first character of string and loc2 will point to the null at the end of string.
The function advance() returns non-zero if the initial substring of string matches the regular expression in expbuf. If there is a match, an external character pointer, loc2, is set as a side effect. The variable loc2 points to the next character in string after the last character that matched.
When advance() encounters a * or \{ \} sequence in the regular expression, it will advance its pointer to the string to be matched as far as possible and will recursively call itself trying to match the rest of the string to the rest of the regular expression. As long as there is no match, advance() will back up along the string until it finds a match or reaches the point in the string that initially matched the * or \{ \}. It is sometimes desirable to stop this backing up before the initial point in the string is reached. If the external character pointer locs is equal to the point in the string at sometime during the backing up process, advance() will break out of the loop that backs up and will return zero.
The external variables circf, sed, and nbra are reserved.
#define INIT register char *sp = instring; #define GETC (*sp++) #define PEEKC (*sp) #define UNGETC(c) (--sp) #define RETURN(*c) return; #define ERROR(c) regerr #include <regexp.h> . . . (void) compile(*argv, expbuf, &expbuf[ESIZE],’\0’); . . . if (step(linebuf, expbuf)) succeed;