summaryrefslogtreecommitdiff
path: root/lib/libc/regex/regex.3
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/regex/regex.3')
-rw-r--r--lib/libc/regex/regex.3251
1 files changed, 171 insertions, 80 deletions
diff --git a/lib/libc/regex/regex.3 b/lib/libc/regex/regex.3
index 1fc7a82e3f9..39dc19de6bc 100644
--- a/lib/libc/regex/regex.3
+++ b/lib/libc/regex/regex.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: regex.3,v 1.8 1999/05/23 14:11:02 aaron Exp $
+.\" $OpenBSD: regex.3,v 1.9 1999/06/05 04:47:41 aaron Exp $
.\"
.\" Copyright (c) 1997, Phillip F Knaack. All rights reserved.
.\"
@@ -70,7 +70,8 @@
.Fa "regex_t *preg"
.Fc
.Sh DESCRIPTION
-These routines implement POSIX 1003.2 regular expressions (``RE''s);
+These routines implement POSIX 1003.2 regular expressions
+.Pq Dq REs ;
see
.Xr re_format 7 .
.Fn regcomp
@@ -84,16 +85,17 @@ frees any dynamically allocated storage used by the internal form
of an RE.
.Pp
The header
-.Em <regex.h>
+.Aq Pa regex.h
declares two structure types,
-.Em regex_t
+.Li regex_t
and
-.Em regmatch_t ,
+.Li regmatch_t ,
the former for compiled internal forms and the latter for match reporting.
It also declares the four functions,
a type
-.Em regoff_t ,
-and a number of constants with names starting with ``REG_''.
+.Li regoff_t ,
+and a number of constants with names starting with
+.Dv REG_ .
.Pp
.Fn regcomp
compiles the regular expression contained in the
@@ -102,48 +104,64 @@ string,
subject to the flags in
.Fa cflags ,
and places the results in the
-.Em regex_t
+.Li regex_t
structure pointed to by
.Fa preg .
.Fa cflags
-is the bitwise OR of zero or more of the following flags:
+is the bitwise
+.Tn OR
+of zero or more of the following flags:
.Bl -tag -width XREG_EXTENDEDX
-.It REG_EXTENDED
-Compile modern (``extended'') REs,
-rather than the obsolete (``basic'') REs that
-are the default.
-.It REG_BASIC
+.It Dv REG_EXTENDED
+Compile modern
+.Pq Dq extended
+REs,
+rather than the obsolete
+.Pq Dq basic
+REs that are the default.
+.It Dv REG_BASIC
This is a synonym for 0,
-provided as a counterpart to REG_EXTENDED to improve readability.
-.It REG_NOSPEC
+provided as a counterpart to
+.Dv REG_EXTENDED
+to improve readability.
+.It Dv REG_NOSPEC
Compile with recognition of all special characters turned off.
All characters are thus considered ordinary,
-so the ``RE'' is a literal string.
+so the RE is a literal string.
This is an extension,
compatible with but not specified by POSIX 1003.2,
and should be used with
caution in software intended to be portable to other systems.
-REG_EXTENDED and REG_NOSPEC may not be used
-in the same call to
+.Dv REG_EXTENDED
+and
+.Dv REG_NOSPEC
+may not be used in the same call to
.Fn regcomp .
-.It REG_ICASE
+.It Dv REG_ICASE
Compile for matching that ignores upper/lower case distinctions.
See
.Xr re_format 7 .
-.It REG_NOSUB
+.It Dv REG_NOSUB
Compile for matching that need only report success or failure,
not what was matched.
-.It REG_NEWLINE
+.It Dv REG_NEWLINE
Compile for newline-sensitive matching.
By default, newline is a completely ordinary character with no special
meaning in either REs or strings.
With this flag,
-`[^' bracket expressions and `.' never match newline,
-a `^' anchor matches the null string after any newline in the string
+.Ql \&[^
+bracket expressions and
+.Ql \&.
+never match newline,
+a
+.Ql ^
+anchor matches the null string after any newline in the string
in addition to its normal function,
-and the `$' anchor matches the null string before any newline in the
+and the
+.Ql $
+anchor matches the null string before any newline in the
string in addition to its normal function.
-.It REG_PEND
+.It Dv REG_PEND
The regular expression ends,
not at the first NUL,
but just before the character pointed to by the
@@ -175,7 +193,8 @@ of type
.Fa size_t ,
contains the number of parenthesized subexpressions within the RE
(except that the value of this member is undefined if the
-REG_NOSUB flag was used).
+.Dv REG_NOSUB
+flag was used).
If
.Fn regcomp
fails, it returns a non-zero error code;
@@ -199,25 +218,33 @@ The compiled form is not altered during execution of
so a single compiled RE can be used simultaneously by multiple threads.
.Pp
By default,
-the NUL-terminated string pointed to by
+the null-terminated string pointed to by
.Fa string
is considered to be the text of an entire line, minus any terminating
newline.
The
.Fa eflags
-argument is the bitwise OR of zero or more of the following flags:
+argument is the bitwise
+.Tn OR
+of zero or more of the following flags:
.Bl -tag -width XREG_STARTENDX
-.It REG_NOTBOL
+.It Dv REG_NOTBOL
The first character of
the string
-is not the beginning of a line, so the `^' anchor should not match before it.
-This does not affect the behavior of newlines under REG_NEWLINE.
-.It REG_NOTEOL
+is not the beginning of a line, so the
+.Ql ^
+anchor should not match before it.
+This does not affect the behavior of newlines under
+.Dv REG_NEWLINE .
+.It Dv REG_NOTEOL
The NUL terminating
the string
-does not end a line, so the `$' anchor should not match before it.
-This does not affect the behavior of newlines under REG_NEWLINE.
-.It REG_STARTEND
+does not end a line, so the
+.Ql $
+anchor should not match before it.
+This does not affect the behavior of newlines under
+.Dv REG_NEWLINE .
+.It Dv REG_STARTEND
The string is considered to start at
\fIstring\fR\ + \fIpmatch\fR[0].\fIrm_so\fR
and to have a terminating NUL located at
@@ -233,8 +260,10 @@ This is an extension,
compatible with but not specified by POSIX 1003.2,
and should be used with
caution in software intended to be portable to other systems.
-Note that a non-zero \fIrm_so\fR does not imply REG_NOTBOL;
-REG_STARTEND affects only the location of the string,
+Note that a non-zero \fIrm_so\fR does not imply
+.Dv REG_NOTBOL ;
+.Dv REG_STARTEND
+affects only the location of the string,
not how it is matched.
.El
.Pp
@@ -246,24 +275,30 @@ portion thereof could match any of several substrings of
.Pp
Normally,
.Fn regexec
-returns 0 for success and the non-zero code REG_NOMATCH for failure.
+returns 0 for success and the non-zero code
+.Dv REG_NOMATCH
+for failure.
Other non-zero error codes may be returned in exceptional situations;
see DIAGNOSTICS.
.Pp
-If REG_NOSUB was specified in the compilation of the RE,
+If
+.Dv REG_NOSUB
+was specified in the compilation of the RE,
or if
.Fa nmatch
is 0,
.Fn regexec
ignores the
.Fa pmatch
-argument (but see below for the case where REG_STARTEND is specified).
+argument (but see below for the case where
+.Dv REG_STARTEND
+is specified).
Otherwise,
.Fa pmatch
points to an array of
.Fa nmatch
structures of type
-.Em regmatch_t .
+.Li regmatch_t .
Such a structure has at least the members
.Fa rm_so
and
@@ -273,7 +308,7 @@ both of type
(a signed arithmetic type at least as large as an
.I off_t
and a
-.Em ssize_t ),
+.Li ssize_t ),
containing respectively the offset of the first character of a substring
and the offset of the first character after the end of the substring.
Offsets are measured from the beginning of the
@@ -305,24 +340,37 @@ and
set to \-1.
If a subexpression participated in the match several times,
the reported substring is the last one it matched.
-(Note, as an example in particular, that when the RE `(b*)+' matches `bbb',
-the parenthesized subexpression matches each of the three `b's and then
-an infinite number of empty strings following the last `b',
+(Note, as an example in particular, that when the RE
+.Dq (b*)+
+matches
+.Dq bbb ,
+the parenthesized subexpression matches each of the three
+.Sq b Ns s
+and then
+an infinite number of empty strings following the last
+.Sq b ,
so the reported substring is one of the empties.)
.Pp
-If REG_STARTEND is specified,
+If
+.Dv REG_STARTEND
+is specified,
.Fa pmatch
must point to at least one
-.Em regmatch_t
+.Li regmatch_t
(even if
.Fa nmatch
-is 0 or REG_NOSUB was specified),
-to hold the input offsets for REG_STARTEND.
+is 0 or
+.Dv REG_NOSUB
+was specified),
+to hold the input offsets for
+.Dv REG_STARTEND .
Use for output is still entirely controlled by
.Fa nmatch ;
if
.Fa nmatch
-is 0 or REG_NOSUB was specified,
+is 0 or
+.Dv REG_NOSUB
+was specified,
the value of
.Fa pmatch[0]
will not be changed by a successful
@@ -341,7 +389,7 @@ If
is non-NULL,
the error code should have arisen from use of
the
-.Em regex_t
+.Li regex_t
pointed to by
.Fa preg ,
and if the error code came from
@@ -349,13 +397,13 @@ and if the error code came from
it should have been the result from the most recent
.Fn regcomp
using that
-.Em regex_t . (
+.Li regex_t . (
.Fn regerror
may be able to supply a more detailed message using information
from the
-.Em regex_t . )
+.Li regex_t . )
.Fn regerror
-places the NUL-terminated message into the buffer pointed to by
+places the null-terminated message into the buffer pointed to by
.Fa errbuf ,
limiting the length (including the NUL) to at most
.Fa errbuf_size
@@ -364,7 +412,7 @@ If the whole message won't fit,
as much of it as will fit before the terminating NUL is supplied.
In any case,
the returned value is the size of buffer needed to hold the whole
-message (including terminating NUL).
+message (including the terminating NUL).
If
.Fa errbuf_size
is 0,
@@ -375,16 +423,23 @@ If the
.Fa errcode
given to
.Fn regerror
-is first ORed with REG_ITOA,
-the ``message'' that results is the printable name of the error code,
-e.g. ``REG_NOMATCH'',
+is first
+.Tn OR Ns 'ed
+with
+.Dv REG_ITOA ,
+the
+.Dq message
+that results is the printable name of the error code,
+e.g.,
+.Dq REG_NOMATCH ,
rather than an explanation thereof.
If
.Fa errcode
-is REG_ATOI,
+is
+.Dv REG_ATOI ,
then
.Fa preg
-shall be non-NULL and the
+shall be non-null and the
.Fa re_endp
member of the structure it points to
must point to the printable name of an error code;
@@ -393,7 +448,10 @@ in this case, the result in
is the decimal digits of
the numeric value of the error code
(0 if the name is not recognized).
-REG_ITOA and REG_ATOI are intended primarily as debugging facilities;
+.Dv REG_ITOA
+and
+.Dv REG_ATOI
+are intended primarily as debugging facilities;
they are extensions,
compatible with but not specified by POSIX 1003.2,
and should be used with
@@ -405,7 +463,7 @@ frees any dynamically allocated storage associated with the compiled RE
pointed to by
.Fa preg .
The remaining
-.Em regex_t
+.Li regex_t
is no longer a valid compiled RE
and the effect of supplying it to
.Fn regexec
@@ -418,7 +476,9 @@ of constants;
all are safe for use from multiple threads if the arguments are safe.
.Sh IMPLEMENTATION CHOICES
There are a number of decisions that 1003.2 leaves up to the implementor,
-either by explicitly saying ``undefined'' or by virtue of them being
+either by explicitly saying
+.Dq undefined
+or by virtue of them being
forbidden by the RE grammar.
This implementation treats them as follows.
.Pp
@@ -436,10 +496,14 @@ for one short RE using them
that will run almost any system out of memory.
.Pp
A backslashed character other than one specifically given a magic meaning
-by 1003.2 (such magic meanings occur only in obsolete [``basic''] REs)
+by 1003.2 (such magic meanings occur only in obsolete REs)
is taken as an ordinary character.
.Pp
-Any unmatched `[' is a REG_EBRACK error.
+Any unmatched
+.Ql \&[
+is a
+.Dv REG_EBRACK
+error.
.Pp
Equivalence classes cannot begin or end bracket-expression ranges.
The endpoint of one range cannot begin another.
@@ -449,19 +513,38 @@ RE_DUP_MAX, the limit on repetition counts in bounded repetitions, is 255.
A repetition operator (?, *, +, or bounds) cannot follow another
repetition operator.
A repetition operator cannot begin an expression or subexpression
-or follow `^' or `|'.
-.Pp
-`|' cannot appear first or last in a (sub)expression or after another `|',
-i.e. an operand of `|' cannot be an empty subexpression.
-An empty parenthesized subexpression, `()', is legal and matches an
+or follow
+.Ql ^
+or
+.Ql | .
+.Pp
+A
+.Ql |
+cannot appear first or last in a (sub)expression, or after another
+.Ql | ,
+i.e., an operand of
+.Ql |
+cannot be an empty subexpression.
+An empty parenthesized subexpression,
+.Ql \&(\&) ,
+is legal and matches an
empty (sub)string.
An empty string is not a legal RE.
.Pp
-A `{' followed by a digit is considered the beginning of bounds for a
+A
+.Ql {
+followed by a digit is considered the beginning of bounds for a
bounded repetition, which must then follow the syntax for bounds.
-A `{' \fInot\fR followed by a digit is considered an ordinary character.
+A
+.Ql {
+.Em not
+followed by a digit is considered an ordinary character.
.Pp
-`^' and `$' beginning and ending subexpressions in obsolete (``basic'')
+.Ql ^
+and
+.Ql $
+beginning and ending subexpressions in obsolete
+.Pq Dq basic
REs are anchors, not ordinary characters.
.Sh SEE ALSO
.Xr grep 1 ,
@@ -507,7 +590,8 @@ ran out of memory
.It Er REG_EMPTY
empty (sub)expression
.It Er REG_ASSERT
-``can't happen''\(emyou found a bug
+.Dq can't happen
+\(emyou found a bug
.It Er REG_INVARG
invalid argument, e.g. negative-length string
.El
@@ -547,8 +631,8 @@ with most special characters counting roughly double.
implements bounded repetitions by macro expansion,
which is costly in time and space if counts are large
or bounded repetitions are nested.
-An RE like, say,
-`((((a{1,100}){1,100}){1,100}){1,100}){1,100}'
+A RE like, say,
+.Dq ((((a{1,100}){1,100}){1,100}){1,100}){1,100}
will (eventually) run almost any existing machine out of swap space.
.Pp
There are suspected problems with response to obscure error conditions.
@@ -557,13 +641,20 @@ certain kinds of internal overflow,
produced only by truly enormous REs or by multiply nested bounded repetitions,
are probably not handled well.
.Pp
-Due to a mistake in 1003.2, things like `a)b' are legal REs because `)' is
-a special character only in the presence of a previous unmatched `('.
+Due to a mistake in 1003.2, things like
+.Ql a)b
+are legal REs because
+.Ql \&)
+is
+a special character only in the presence of a previous unmatched
+.Ql \&( .
This can't be fixed until the spec is fixed.
.Pp
The standard's definition of back references is vague.
For example, does
-`a\e(\e(b\e)*\e2\e)*d' match `abbbd'?
+.Dq a\e(\e(b\e)*\e2\e)*d
+match
+.Dq abbbd ?
Until the standard is clarified,
behavior in such cases should not be relied on.
.Pp