'\"macro stdmacro
.if n .pH g5.regexp @(#)regexp	40.11 of 10/27/89
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} regexp 5 "" "\&"
.if \nX=1 .ds x} regexp 5 ""
.if \nX=2 .ds x} regexp 5 "" "\&"
.if \nX=3 .ds x} regexp "" "" "\&"
.TH \*(x}
.tr ~
.SH NAME
\f4regexp\f1: \f4 compile\f1, \f4step\f1, \f4advance\f1 \- regular expression compile and match routines
.SH SYNOPSIS
.nf
.ft 4
#define INIT \f2declarations\f4
#define GETC(void) \f2getc code\f4
#define PEEKC(void) \f2peekc code\f4
#define UNGETC(void) \f2ungetc code\f4
#define RETURN(\f2ptr\f4) \f2return code\f4
#define ERROR(\f2val\f4) \f2error code\f4
.sp0.5
#include <regexp.h>
.sp0.5
char *compile(char *instring, char *expbuf, char *endbuf, int eof);
.sp0.5
int step(char *string, char *expbuf);
.sp0.5
int advance(char *string, char *expbuf);
.sp0.5
extern char *loc1, *loc2, *locs;
.ft 1
.fi
.SH DESCRIPTION
These functions are general purpose regular expression matching
routines to be used in programs that perform regular expression matching.
These functions are defined by the \f4<regexp.h>\f1 header file.
.P
The functions \f4step\f1 and \f4advance\f1 do pattern matching given
a character string and a compiled regular expression as input.
.P
The function \f4compile\f1 takes as input a regular expression as defined
below and produces a compiled expression that can be used with \f4step\f1
or \f4advance\f1.
.P
A regular expression specifies a set of character strings.
A member of this set of strings is said to be matched by the regular
expression.
Some characters have special meaning when used in a regular
expression;
other characters stand for themselves.
.P
The regular expressions available for use with the
regexp functions are constructed as follows:
.TP 12
\f2Expression\f1
\f2Meaning\f1
.TP
\f2c\f1
the character \f2c\f1 where \f2c\f1 is not a special character.
.TP
\f4\e\f2c\f1
the character \f2c\f1 where \f2c\f1 is any character,
except a digit in the range \f41\-9\f1.
.TP
\f4^\f1
the beginning of the line being compared.
.TP
\f4$\f1
the end of the line being compared.
.TP
\f4\&\.\f1
any character in the input.
.TP
\f4[\f2s\f4]\f1
any character in the set \f2s\f1,
where \f2s\f1 is a sequence of characters and/or
a range of characters, \f2e.g.\f1,
\f4[\f2c\f4\-\f2c\f4]\f1.
.TP
\f4[^\f2s\f4]\f1
any character not in the set \f2s\f1, where \f2s\f1 is defined as above.
.TP
\f2r\f4*\f1
zero or more successive occurrences of the regular expression \f2r\f1.
The longest leftmost match is chosen.
.TP
\f2rx\f1
the occurrence of regular expression \f2r\f1
followed by the occurrence of regular expression \f2x\f1.
(Concatenation)
.TP
\f2r\f4\e{\f2m\f4,\f2n\f4\e}\f1
any number of \f2m\f1 through \f2n\f1 successive occurrences
of the regular expression \f2r\f1.
The regular expression \f2r\f4\e{\f2m\f4\e}\f1
matches exactly \f2m\f1 occurrences; \f2r\f4\e{\f2m\f4,\e}\f1
matches at least \f2m\f1 occurrences.
.TP
\f4\e(\f2r\f4\e)\f1
the regular expression \f2r\f1.
When \f4\e\f2n\f1 (where \f2n\f1 is a number greater than zero)
appears in a constructed regular expression,
it stands for the regular expression \f2x\f1
where \f2x\f1 is the \f2n\u\s-2th\s+2\d\f1 regular expression
enclosed in \f4\e(\f1 and \f4\e)\f1 that appeared
earlier in the constructed regular expression.
For example,
\f4\e(\f2r\f4\e)\f2x\f4\e(\f2y\f4\e)\f2z\f4\e2\f1
is the concatenation of regular expressions \f2rxyzy\f1.
.P
Characters that have special meaning except when they appear
within square brackets (\f4[\|]\f1) or are preceded by \f4\e\f1
are:  \f4.\f1, \f4*\f1, \f4[\f1, \f4\e\f1.
Other special characters, such as \f4$\f1 have special meaning
in more restricted contexts.
.P
The character \f4^\f1 at the beginning of an expression permits a
successful match only immediately after a newline, and the
character \f4$\f1 at the end of an expression requires a trailing newline.
.P
Two characters have special meaning only when used within square brackets.
The character \f4\-\f1 denotes a range,
\f4[\f2c\f4\-\f2c\f4]\f1, unless it is just after the
open bracket or before the closing bracket,
\f4[\-\f2c\f4]\f1 or \f4[\f2c\f4\-]\f1 in which
case it has no special meaning.
When used within brackets, the character \f4^\f1
has the meaning \f2complement of\f1 if it immediately
follows the open bracket (example: \f4[^\f2c\f4]\f1);
elsewhere between brackets (example: \f4[\f2c\f4^]\f1)
it stands for the ordinary character \f4^\f1.
.P
The special meaning of the \f4\e\f1 operator can be
escaped only by preceding it with another \f4\e\f1,
\f2e.g.\f1 \f4\e\e\f1.
.P
Programs must have the following five macros declared
before the \f4#include <regexp.h>\f1 statement.
These macros are used by the \f4compile\f1 routine.
The macros \f4GETC\f1, \f4PEEKC\f1, and \f4UNGETC\f1
operate on the regular expression given as input to \f4compile\f1.
.TP 15
\f4GETC\f1
This macro returns the value of the next
character (byte) in the regular expression pattern.
Successive calls to \f4GETC\f1 should return successive
characters of the regular expression.
.TP
\f4PEEKC\f1
This macro returns the next character (byte) in the regular expression.
Immediately successive calls to \f4PEEKC\f1 should return
the same character, which should also be the next character
returned by \f4GETC\f1.
.TP
\f4UNGETC\f1
This macro causes the argument \f4c\f1 to be returned
by the next call to \f4GETC\f1 and \f4PEEKC\f1.
No more than one character of pushback is ever needed and
this character is guaranteed to be the last character read by \f4GETC\f1.
The return value of the macro \f4UNGETC(c)\f1 is always ignored.
.TP
\f4RETURN(\f2ptr\f4)\f1
This macro is used on normal exit of the \f4compile\f1 routine.
The value of the argument \f2ptr\f1 is a pointer to the
character after the last character of the compiled regular expression.
This is useful to programs which have memory allocation to manage.
.TP
\f4ERROR(\f2val\f4)\f1
This macro is the abnormal return from the \f4compile\f1 routine.
The argument \f2val\f1 is an error number [see
.SM ERRORS
below for meanings].
This call should never return.
.P
The syntax of the \f4compile\f1 routine is as follows:
.P
.RS
.nf
.ft 4
compile(\f2instring\f4, \f2expbuf\f4, \f2endbuf\f4, \f2eof\f4)
.ft 1
.fi
.RE
.P
The first parameter, \f2instring\f1, is never used
explicitly by the \f4compile\f1 routine but is useful
for programs that pass down different pointers to input characters.
It is sometimes used in the \f4INIT\f1 declaration (see below).
Programs which call functions to input characters or have
characters in an external array can pass down a value of
\f4(char *)0\f1 for this parameter.
.P
The next parameter, \f2expbuf\f1, is a character pointer.
It points to the place where the compiled regular expression will be placed.
.P
The parameter \f2endbuf\f1 is one more than the highest address
where the compiled regular expression may be placed.
If the compiled expression cannot fit in \f4(endbuf\-expbuf)\f1 bytes,
a call to \f4ERROR(50)\f1 is made.
.P
The parameter \f2eof\f1 is the character which marks the end of the
regular expression.
This character is usually a \f4/\f1.
.P
Each program that includes the \f4<regexp.h>\f1 header
file must have a \f4#define\f1 statement for \f4INIT\f1.
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 \f4GETC\f1,
\f4PEEKC\f1, and \f4UNGETC\f1.
Otherwise it can be used to declare external variables that
might be used by \f4GETC\f1, \f4PEEKC\f1 and \f4UNGETC\f1.
[See
.SM EXAMPLE
below.]
.P
The first parameter to the \f4step\f1 and
\f4advance\f1 functions is a
pointer to a string of characters to be checked for a match.
This string should be null terminated.
.P
The second parameter, \f2expbuf\f1,
is the compiled regular expression which was obtained by a
call to the function \f4compile\f1.
.P
The function \f4step\f1 returns non-zero if some substring
of \f2string\f1 matches the regular expression in \f2expbuf\f1
and zero if there is no match.
If there is a match, two external character pointers are set as a
side effect to the call to \f4step\f1.
The variable \f4loc1\f1 points to the first character that
matched the regular expression; the variable \f4loc2\f1
points to the character after the last character that matches
the regular expression.
Thus if the regular expression matches the entire input string,
\f4loc1\f1 will point to the first character of \f2string\f1
and \f4loc2\f1 will point to the null at the end of \f2string\f1.
.P
The function \f4advance\f1 returns non-zero if the initial
substring of \f2string\f1 matches the regular expression in \f2expbuf\f1.
If there is a match, an external character pointer,
\f4loc2\f1, is set as a side effect.
The variable \f4loc2\f1 points to the next
character in \f2string\f1 after the last character that matched.
.P
When \f4advance\f1 encounters a \f4*\f1 or \f4\e{ \e}\f1
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, \f4advance\f1 will back up along the
string until it finds a match or reaches the point in the string that
initially matched the  \f4*\f1 or \f4\e{ \e}\f1.
It is sometimes desirable to stop this backing up before the initial
point in the string is reached.
If the external character pointer \f4locs\f1 is equal to the point
in the string at sometime during the backing up process,
\f4advance\f1 will break out of the loop that backs up and will return zero.
.P
The external variables \f4circf\f1, \f4sed\f1,
and \f4nbra\f1 are reserved.
.SH "DIAGNOSTICS"
The function \f4compile\f1 uses the macro \f4RETURN\f1 on success
and the macro \f4ERROR\f1 on failure (see above).
The functions \f4step\f1 and \f4advance\f1 return non-zero
on a successful match and zero if there is no match.
Errors are:
.TP
\f411\f1
range endpoint too large.
.TP
\f416\f1
bad number.
.TP
\f425\f1
\f4\e\f1 \f2digit\f1 out of range.
.TP
\f436\f1
illegal or missing delimiter.
.TP
\f441\f1
no remembered search string.
.TP
\f442\f1
\f4\e( \e)\f1 imbalance.
.TP
\f443\f1
too many \f4\e(\f1.
.TP
\f444\f1
more than 2 numbers given in \f4\e{ \e}\f1.
.TP
\f445\f1
\f4}\f1 expected after \f4\e\f1.
.TP
\f446\f1
first number exceeds second in \f4\e{ \e}\f1.
.TP
\f449\f1
\f4[ ]\f1 imbalance.
.TP
\f450\f1
regular expression overflow.
.SH EXAMPLE
The following is an example of how the regular expression
macros and calls might be defined by an application program:
.P
.nf
.ft 4
#define INIT         register char *sp = instring;
#define GETC       (*sp++)
#define PEEKC      (*sp)
#define UNGETC(c)    (\-\-sp)
#define RETURN(*c)    return;
#define ERROR(c)     regerr
.sp0.5
#include <regexp.h>
.sp0.5
 . . .
      (void) compile(*argv, expbuf, &expbuf[ESIZE],'\e0');
 . . .
      if (step(linebuf, expbuf))
                        succeed;
.ft 1
.fi
.\"	@(#)regexp.5	6.3 of 9/6/83
.Ee
