hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame manual/pattern.texi

Packit 6c4009
@node Pattern Matching, I/O Overview, Searching and Sorting, Top
Packit 6c4009
@c %MENU% Matching shell ``globs'' and regular expressions
Packit 6c4009
@chapter Pattern Matching
Packit 6c4009
Packit 6c4009
@Theglibc{} provides pattern matching facilities for two kinds of
Packit 6c4009
patterns: regular expressions and file-name wildcards.  The library also
Packit 6c4009
provides a facility for expanding variable and command references and
Packit 6c4009
parsing text into words in the way the shell does.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Wildcard Matching::    Matching a wildcard pattern against a single string.
Packit 6c4009
* Globbing::             Finding the files that match a wildcard pattern.
Packit 6c4009
* Regular Expressions::  Matching regular expressions against strings.
Packit 6c4009
* Word Expansion::       Expanding shell variables, nested commands,
Packit 6c4009
			    arithmetic, and wildcards.
Packit 6c4009
			    This is what the shell does with shell commands.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Wildcard Matching
Packit 6c4009
@section Wildcard Matching
Packit 6c4009
Packit 6c4009
@pindex fnmatch.h
Packit 6c4009
This section describes how to match a wildcard pattern against a
Packit 6c4009
particular string.  The result is a yes or no answer: does the
Packit 6c4009
string fit the pattern or not.  The symbols described here are all
Packit 6c4009
declared in @file{fnmatch.h}.
Packit 6c4009
Packit 6c4009
@deftypefun int fnmatch (const char *@var{pattern}, const char *@var{string}, int @var{flags})
Packit 6c4009
@standards{POSIX.2, fnmatch.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
@c fnmatch @mtsenv @mtslocale @ascuheap @acsmem
Packit 6c4009
@c  strnlen dup ok
Packit 6c4009
@c  mbsrtowcs
Packit 6c4009
@c  memset dup ok
Packit 6c4009
@c  malloc dup @ascuheap @acsmem
Packit 6c4009
@c  mbsinit dup ok
Packit 6c4009
@c  free dup @ascuheap @acsmem
Packit 6c4009
@c  FCT = internal_fnwmatch @mtsenv @mtslocale @ascuheap @acsmem
Packit 6c4009
@c   FOLD @mtslocale
Packit 6c4009
@c    towlower @mtslocale
Packit 6c4009
@c   EXT @mtsenv @mtslocale @ascuheap @acsmem
Packit 6c4009
@c    STRLEN = wcslen dup ok
Packit 6c4009
@c    getenv @mtsenv
Packit 6c4009
@c    malloc dup @ascuheap @acsmem
Packit 6c4009
@c    MEMPCPY = wmempcpy dup ok
Packit 6c4009
@c    FCT dup @mtsenv @mtslocale @ascuheap @acsmem
Packit 6c4009
@c    STRCAT = wcscat dup ok
Packit 6c4009
@c    free dup @ascuheap @acsmem
Packit 6c4009
@c   END @mtsenv
Packit 6c4009
@c    getenv @mtsenv
Packit 6c4009
@c   MEMCHR = wmemchr dup ok
Packit 6c4009
@c   getenv @mtsenv
Packit 6c4009
@c   IS_CHAR_CLASS = is_char_class @mtslocale
Packit 6c4009
@c    wctype @mtslocale
Packit 6c4009
@c   BTOWC ok
Packit 6c4009
@c   ISWCTYPE ok
Packit 6c4009
@c   auto findidx dup ok
Packit 6c4009
@c   elem_hash dup ok
Packit 6c4009
@c   memcmp dup ok
Packit 6c4009
@c   collseq_table_lookup dup ok
Packit 6c4009
@c   NO_LEADING_PERIOD ok
Packit 6c4009
This function tests whether the string @var{string} matches the pattern
Packit 6c4009
@var{pattern}.  It returns @code{0} if they do match; otherwise, it
Packit 6c4009
returns the nonzero value @code{FNM_NOMATCH}.  The arguments
Packit 6c4009
@var{pattern} and @var{string} are both strings.
Packit 6c4009
Packit 6c4009
The argument @var{flags} is a combination of flag bits that alter the
Packit 6c4009
details of matching.  See below for a list of the defined flags.
Packit 6c4009
Packit 6c4009
In @theglibc{}, @code{fnmatch} might sometimes report ``errors'' by
Packit 6c4009
returning nonzero values that are not equal to @code{FNM_NOMATCH}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
These are the available flags for the @var{flags} argument:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item FNM_FILE_NAME
Packit 6c4009
@standards{GNU, fnmatch.h}
Packit 6c4009
Treat the @samp{/} character specially, for matching file names.  If
Packit 6c4009
this flag is set, wildcard constructs in @var{pattern} cannot match
Packit 6c4009
@samp{/} in @var{string}.  Thus, the only way to match @samp{/} is with
Packit 6c4009
an explicit @samp{/} in @var{pattern}.
Packit 6c4009
Packit 6c4009
@item FNM_PATHNAME
Packit 6c4009
@standards{POSIX.2, fnmatch.h}
Packit 6c4009
This is an alias for @code{FNM_FILE_NAME}; it comes from POSIX.2.  We
Packit 6c4009
don't recommend this name because we don't use the term ``pathname'' for
Packit 6c4009
file names.
Packit 6c4009
Packit 6c4009
@item FNM_PERIOD
Packit 6c4009
@standards{POSIX.2, fnmatch.h}
Packit 6c4009
Treat the @samp{.} character specially if it appears at the beginning of
Packit 6c4009
@var{string}.  If this flag is set, wildcard constructs in @var{pattern}
Packit 6c4009
cannot match @samp{.} as the first character of @var{string}.
Packit 6c4009
Packit 6c4009
If you set both @code{FNM_PERIOD} and @code{FNM_FILE_NAME}, then the
Packit 6c4009
special treatment applies to @samp{.} following @samp{/} as well as to
Packit 6c4009
@samp{.} at the beginning of @var{string}.  (The shell uses the
Packit 6c4009
@code{FNM_PERIOD} and @code{FNM_FILE_NAME} flags together for matching
Packit 6c4009
file names.)
Packit 6c4009
Packit 6c4009
@item FNM_NOESCAPE
Packit 6c4009
@standards{POSIX.2, fnmatch.h}
Packit 6c4009
Don't treat the @samp{\} character specially in patterns.  Normally,
Packit 6c4009
@samp{\} quotes the following character, turning off its special meaning
Packit 6c4009
(if any) so that it matches only itself.  When quoting is enabled, the
Packit 6c4009
pattern @samp{\?} matches only the string @samp{?}, because the question
Packit 6c4009
mark in the pattern acts like an ordinary character.
Packit 6c4009
Packit 6c4009
If you use @code{FNM_NOESCAPE}, then @samp{\} is an ordinary character.
Packit 6c4009
Packit 6c4009
@item FNM_LEADING_DIR
Packit 6c4009
@standards{GNU, fnmatch.h}
Packit 6c4009
Ignore a trailing sequence of characters starting with a @samp{/} in
Packit 6c4009
@var{string}; that is to say, test whether @var{string} starts with a
Packit 6c4009
directory name that @var{pattern} matches.
Packit 6c4009
Packit 6c4009
If this flag is set, either @samp{foo*} or @samp{foobar} as a pattern
Packit 6c4009
would match the string @samp{foobar/frobozz}.
Packit 6c4009
Packit 6c4009
@item FNM_CASEFOLD
Packit 6c4009
@standards{GNU, fnmatch.h}
Packit 6c4009
Ignore case in comparing @var{string} to @var{pattern}.
Packit 6c4009
Packit 6c4009
@item FNM_EXTMATCH
Packit 6c4009
@standards{GNU, fnmatch.h}
Packit 6c4009
@cindex Korn Shell
Packit 6c4009
@pindex ksh
Packit 6c4009
Besides the normal patterns, also recognize the extended patterns
Packit 6c4009
introduced in @file{ksh}.  The patterns are written in the form
Packit 6c4009
explained in the following table where @var{pattern-list} is a @code{|}
Packit 6c4009
separated list of patterns.
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item ?(@var{pattern-list})
Packit 6c4009
The pattern matches if zero or one occurrences of any of the patterns
Packit 6c4009
in the @var{pattern-list} allow matching the input string.
Packit 6c4009
Packit 6c4009
@item *(@var{pattern-list})
Packit 6c4009
The pattern matches if zero or more occurrences of any of the patterns
Packit 6c4009
in the @var{pattern-list} allow matching the input string.
Packit 6c4009
Packit 6c4009
@item +(@var{pattern-list})
Packit 6c4009
The pattern matches if one or more occurrences of any of the patterns
Packit 6c4009
in the @var{pattern-list} allow matching the input string.
Packit 6c4009
Packit 6c4009
@item @@(@var{pattern-list})
Packit 6c4009
The pattern matches if exactly one occurrence of any of the patterns in
Packit 6c4009
the @var{pattern-list} allows matching the input string.
Packit 6c4009
Packit 6c4009
@item !(@var{pattern-list})
Packit 6c4009
The pattern matches if the input string cannot be matched with any of
Packit 6c4009
the patterns in the @var{pattern-list}.
Packit 6c4009
@end table
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node Globbing
Packit 6c4009
@section Globbing
Packit 6c4009
Packit 6c4009
@cindex globbing
Packit 6c4009
The archetypal use of wildcards is for matching against the files in a
Packit 6c4009
directory, and making a list of all the matches.  This is called
Packit 6c4009
@dfn{globbing}.
Packit 6c4009
Packit 6c4009
You could do this using @code{fnmatch}, by reading the directory entries
Packit 6c4009
one by one and testing each one with @code{fnmatch}.  But that would be
Packit 6c4009
slow (and complex, since you would have to handle subdirectories by
Packit 6c4009
hand).
Packit 6c4009
Packit 6c4009
The library provides a function @code{glob} to make this particular use
Packit 6c4009
of wildcards convenient.  @code{glob} and the other symbols in this
Packit 6c4009
section are declared in @file{glob.h}.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Calling Glob::             Basic use of @code{glob}.
Packit 6c4009
* Flags for Globbing::       Flags that enable various options in @code{glob}.
Packit 6c4009
* More Flags for Globbing::  GNU specific extensions to @code{glob}.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Calling Glob
Packit 6c4009
@subsection Calling @code{glob}
Packit 6c4009
Packit 6c4009
The result of globbing is a vector of file names (strings).  To return
Packit 6c4009
this vector, @code{glob} uses a special data type, @code{glob_t}, which
Packit 6c4009
is a structure.  You pass @code{glob} the address of the structure, and
Packit 6c4009
it fills in the structure's fields to tell you about the results.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} glob_t
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
This data type holds a pointer to a word vector.  More precisely, it
Packit 6c4009
records both the address of the word vector and its size.  The GNU
Packit 6c4009
implementation contains some more fields which are non-standard
Packit 6c4009
extensions.
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item gl_pathc
Packit 6c4009
The number of elements in the vector, excluding the initial null entries
Packit 6c4009
if the GLOB_DOOFFS flag is used (see gl_offs below).
Packit 6c4009
Packit 6c4009
@item gl_pathv
Packit 6c4009
The address of the vector.  This field has type @w{@code{char **}}.
Packit 6c4009
Packit 6c4009
@item gl_offs
Packit 6c4009
The offset of the first real element of the vector, from its nominal
Packit 6c4009
address in the @code{gl_pathv} field.  Unlike the other fields, this
Packit 6c4009
is always an input to @code{glob}, rather than an output from it.
Packit 6c4009
Packit 6c4009
If you use a nonzero offset, then that many elements at the beginning of
Packit 6c4009
the vector are left empty.  (The @code{glob} function fills them with
Packit 6c4009
null pointers.)
Packit 6c4009
Packit 6c4009
The @code{gl_offs} field is meaningful only if you use the
Packit 6c4009
@code{GLOB_DOOFFS} flag.  Otherwise, the offset is always zero
Packit 6c4009
regardless of what is in this field, and the first real element comes at
Packit 6c4009
the beginning of the vector.
Packit 6c4009
Packit 6c4009
@item gl_closedir
Packit 6c4009
The address of an alternative implementation of the @code{closedir}
Packit 6c4009
function.  It is used if the @code{GLOB_ALTDIRFUNC} bit is set in
Packit 6c4009
the flag parameter.  The type of this field is
Packit 6c4009
@w{@code{void (*) (void *)}}.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
Packit 6c4009
@item gl_readdir
Packit 6c4009
The address of an alternative implementation of the @code{readdir}
Packit 6c4009
function used to read the contents of a directory.  It is used if the
Packit 6c4009
@code{GLOB_ALTDIRFUNC} bit is set in the flag parameter.  The type of
Packit 6c4009
this field is @w{@code{struct dirent *(*) (void *)}}.
Packit 6c4009
Packit 6c4009
An implementation of @code{gl_readdir} needs to initialize the following
Packit 6c4009
members of the @code{struct dirent} object:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item d_type
Packit 6c4009
This member should be set to the file type of the entry if it is known.
Packit 6c4009
Otherwise, the value @code{DT_UNKNOWN} can be used.  The @code{glob}
Packit 6c4009
function may use the specified file type to avoid callbacks in cases
Packit 6c4009
where the file type indicates that the data is not required.
Packit 6c4009
Packit 6c4009
@item d_ino
Packit 6c4009
This member needs to be non-zero, otherwise @code{glob} may skip the
Packit 6c4009
current entry and call the @code{gl_readdir} callback function again to
Packit 6c4009
retrieve another entry.
Packit 6c4009
Packit 6c4009
@item d_name
Packit 6c4009
This member must be set to the name of the entry.  It must be
Packit 6c4009
null-terminated.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
The example below shows how to allocate a @code{struct dirent} object
Packit 6c4009
containing a given name.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include mkdirent.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The @code{glob} function reads the @code{struct dirent} members listed
Packit 6c4009
above and makes a copy of the file name in the @code{d_name} member
Packit 6c4009
immediately after the @code{gl_readdir} callback function returns.
Packit 6c4009
Future invocations of any of the callback functions may dealloacte or
Packit 6c4009
reuse the buffer.  It is the responsibility of the caller of the
Packit 6c4009
@code{glob} function to allocate and deallocate the buffer, around the
Packit 6c4009
call to @code{glob} or using the callback functions.  For example, an
Packit 6c4009
application could allocate the buffer in the @code{gl_readdir} callback
Packit 6c4009
function, and deallocate it in the @code{gl_closedir} callback function.
Packit 6c4009
Packit 6c4009
The @code{gl_readdir} member is a GNU extension.
Packit 6c4009
Packit 6c4009
@item gl_opendir
Packit 6c4009
The address of an alternative implementation of the @code{opendir}
Packit 6c4009
function.  It is used if the @code{GLOB_ALTDIRFUNC} bit is set in
Packit 6c4009
the flag parameter.  The type of this field is
Packit 6c4009
@w{@code{void *(*) (const char *)}}.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
Packit 6c4009
@item gl_stat
Packit 6c4009
The address of an alternative implementation of the @code{stat} function
Packit 6c4009
to get information about an object in the filesystem.  It is used if the
Packit 6c4009
@code{GLOB_ALTDIRFUNC} bit is set in the flag parameter.  The type of
Packit 6c4009
this field is @w{@code{int (*) (const char *, struct stat *)}}.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
Packit 6c4009
@item gl_lstat
Packit 6c4009
The address of an alternative implementation of the @code{lstat}
Packit 6c4009
function to get information about an object in the filesystems, not
Packit 6c4009
following symbolic links.  It is used if the @code{GLOB_ALTDIRFUNC} bit
Packit 6c4009
is set in the flag parameter.  The type of this field is @code{@w{int
Packit 6c4009
(*) (const char *,} @w{struct stat *)}}.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
Packit 6c4009
@item gl_flags
Packit 6c4009
The flags used when @code{glob} was called.  In addition, @code{GLOB_MAGCHAR}
Packit 6c4009
might be set.  See @ref{Flags for Globbing} for more details.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
For use in the @code{glob64} function @file{glob.h} contains another
Packit 6c4009
definition for a very similar type.  @code{glob64_t} differs from
Packit 6c4009
@code{glob_t} only in the types of the members @code{gl_readdir},
Packit 6c4009
@code{gl_stat}, and @code{gl_lstat}.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} glob64_t
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
This data type holds a pointer to a word vector.  More precisely, it
Packit 6c4009
records both the address of the word vector and its size.  The GNU
Packit 6c4009
implementation contains some more fields which are non-standard
Packit 6c4009
extensions.
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item gl_pathc
Packit 6c4009
The number of elements in the vector, excluding the initial null entries
Packit 6c4009
if the GLOB_DOOFFS flag is used (see gl_offs below).
Packit 6c4009
Packit 6c4009
@item gl_pathv
Packit 6c4009
The address of the vector.  This field has type @w{@code{char **}}.
Packit 6c4009
Packit 6c4009
@item gl_offs
Packit 6c4009
The offset of the first real element of the vector, from its nominal
Packit 6c4009
address in the @code{gl_pathv} field.  Unlike the other fields, this
Packit 6c4009
is always an input to @code{glob}, rather than an output from it.
Packit 6c4009
Packit 6c4009
If you use a nonzero offset, then that many elements at the beginning of
Packit 6c4009
the vector are left empty.  (The @code{glob} function fills them with
Packit 6c4009
null pointers.)
Packit 6c4009
Packit 6c4009
The @code{gl_offs} field is meaningful only if you use the
Packit 6c4009
@code{GLOB_DOOFFS} flag.  Otherwise, the offset is always zero
Packit 6c4009
regardless of what is in this field, and the first real element comes at
Packit 6c4009
the beginning of the vector.
Packit 6c4009
Packit 6c4009
@item gl_closedir
Packit 6c4009
The address of an alternative implementation of the @code{closedir}
Packit 6c4009
function.  It is used if the @code{GLOB_ALTDIRFUNC} bit is set in
Packit 6c4009
the flag parameter.  The type of this field is
Packit 6c4009
@w{@code{void (*) (void *)}}.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
Packit 6c4009
@item gl_readdir
Packit 6c4009
The address of an alternative implementation of the @code{readdir64}
Packit 6c4009
function used to read the contents of a directory.  It is used if the
Packit 6c4009
@code{GLOB_ALTDIRFUNC} bit is set in the flag parameter.  The type of
Packit 6c4009
this field is @w{@code{struct dirent64 *(*) (void *)}}.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
Packit 6c4009
@item gl_opendir
Packit 6c4009
The address of an alternative implementation of the @code{opendir}
Packit 6c4009
function.  It is used if the @code{GLOB_ALTDIRFUNC} bit is set in
Packit 6c4009
the flag parameter.  The type of this field is
Packit 6c4009
@w{@code{void *(*) (const char *)}}.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
Packit 6c4009
@item gl_stat
Packit 6c4009
The address of an alternative implementation of the @code{stat64} function
Packit 6c4009
to get information about an object in the filesystem.  It is used if the
Packit 6c4009
@code{GLOB_ALTDIRFUNC} bit is set in the flag parameter.  The type of
Packit 6c4009
this field is @w{@code{int (*) (const char *, struct stat64 *)}}.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
Packit 6c4009
@item gl_lstat
Packit 6c4009
The address of an alternative implementation of the @code{lstat64}
Packit 6c4009
function to get information about an object in the filesystems, not
Packit 6c4009
following symbolic links.  It is used if the @code{GLOB_ALTDIRFUNC} bit
Packit 6c4009
is set in the flag parameter.  The type of this field is @code{@w{int
Packit 6c4009
(*) (const char *,} @w{struct stat64 *)}}.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
Packit 6c4009
@item gl_flags
Packit 6c4009
The flags used when @code{glob} was called.  In addition, @code{GLOB_MAGCHAR}
Packit 6c4009
might be set.  See @ref{Flags for Globbing} for more details.
Packit 6c4009
Packit 6c4009
This is a GNU extension.
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@deftypefun int glob (const char *@var{pattern}, int @var{flags}, int (*@var{errfunc}) (const char *@var{filename}, int @var{error-code}), glob_t *@var{vector-ptr})
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtsenv{} @mtascusig{:ALRM} @mtascutimer{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
Packit 6c4009
@c glob @mtasurace:utent @mtsenv @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c  strlen dup ok
Packit 6c4009
@c  strchr dup ok
Packit 6c4009
@c  malloc dup @ascuheap @acsmem
Packit 6c4009
@c  mempcpy dup ok
Packit 6c4009
@c  next_brace_sub ok
Packit 6c4009
@c  free dup @ascuheap @acsmem
Packit 6c4009
@c  globfree dup @asucorrupt @ascuheap @acucorrupt @acsmem
Packit 6c4009
@c  glob_pattern_p ok
Packit 6c4009
@c   glob_pattern_type dup ok
Packit 6c4009
@c  getenv dup @mtsenv
Packit 6c4009
@c  GET_LOGIN_NAME_MAX ok
Packit 6c4009
@c  getlogin_r dup @mtasurace:utent @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c  GETPW_R_SIZE_MAX ok
Packit 6c4009
@c  getpwnam_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c  realloc dup @ascuheap @acsmem
Packit 6c4009
@c  memcpy dup ok
Packit 6c4009
@c  memchr dup ok
Packit 6c4009
@c  *pglob->gl_stat user-supplied
Packit 6c4009
@c  stat64 dup ok
Packit 6c4009
@c  S_ISDIR dup ok
Packit 6c4009
@c  strdup dup @ascuheap @acsmem
Packit 6c4009
@c  glob_pattern_type ok
Packit 6c4009
@c  glob_in_dir @mtsenv @mtslocale @asucorrupt @ascuheap @acucorrupt @acsfd @acsmem
Packit 6c4009
@c   strlen dup ok
Packit 6c4009
@c   glob_pattern_type dup ok
Packit 6c4009
@c   malloc dup @ascuheap @acsmem
Packit 6c4009
@c   mempcpy dup ok
Packit 6c4009
@c   *pglob->gl_stat user-supplied
Packit 6c4009
@c   stat64 dup ok
Packit 6c4009
@c   free dup @ascuheap @acsmem
Packit 6c4009
@c   *pglob->gl_opendir user-supplied
Packit 6c4009
@c   opendir dup @ascuheap @acsmem @acsfd
Packit 6c4009
@c   dirfd dup ok
Packit 6c4009
@c   *pglob->gl_readdir user-supplied
Packit 6c4009
@c   CONVERT_DIRENT_DIRENT64 ok
Packit 6c4009
@c   readdir64 ok [protected by exclusive use of the stream]
Packit 6c4009
@c   REAL_DIR_ENTRY ok
Packit 6c4009
@c   DIRENT_MIGHT_BE_DIR ok
Packit 6c4009
@c   fnmatch dup @mtsenv @mtslocale @ascuheap @acsmem
Packit 6c4009
@c   DIRENT_MIGHT_BE_SYMLINK ok
Packit 6c4009
@c   link_exists_p ok
Packit 6c4009
@c    link_exists2_p ok
Packit 6c4009
@c     strlen dup ok
Packit 6c4009
@c     mempcpy dup ok
Packit 6c4009
@c     *pglob->gl_stat user-supplied
Packit 6c4009
@c    fxstatat64 dup ok
Packit 6c4009
@c   realloc dup @ascuheap @acsmem
Packit 6c4009
@c   pglob->gl_closedir user-supplied
Packit 6c4009
@c   closedir @ascuheap @acsmem @acsfd
Packit 6c4009
@c  prefix_array dup @asucorrupt @ascuheap @acucorrupt @acsmem
Packit 6c4009
@c   strlen dup ok
Packit 6c4009
@c   malloc dup @ascuheap @acsmem
Packit 6c4009
@c   free dup @ascuheap @acsmem
Packit 6c4009
@c   mempcpy dup ok
Packit 6c4009
@c  strcpy dup ok
Packit 6c4009
The function @code{glob} does globbing using the pattern @var{pattern}
Packit 6c4009
in the current directory.  It puts the result in a newly allocated
Packit 6c4009
vector, and stores the size and address of this vector into
Packit 6c4009
@code{*@var{vector-ptr}}.  The argument @var{flags} is a combination of
Packit 6c4009
bit flags; see @ref{Flags for Globbing}, for details of the flags.
Packit 6c4009
Packit 6c4009
The result of globbing is a sequence of file names.  The function
Packit 6c4009
@code{glob} allocates a string for each resulting word, then
Packit 6c4009
allocates a vector of type @code{char **} to store the addresses of
Packit 6c4009
these strings.  The last element of the vector is a null pointer.
Packit 6c4009
This vector is called the @dfn{word vector}.
Packit 6c4009
Packit 6c4009
To return this vector, @code{glob} stores both its address and its
Packit 6c4009
length (number of elements, not counting the terminating null pointer)
Packit 6c4009
into @code{*@var{vector-ptr}}.
Packit 6c4009
Packit 6c4009
Normally, @code{glob} sorts the file names alphabetically before
Packit 6c4009
returning them.  You can turn this off with the flag @code{GLOB_NOSORT}
Packit 6c4009
if you want to get the information as fast as possible.  Usually it's
Packit 6c4009
a good idea to let @code{glob} sort them---if you process the files in
Packit 6c4009
alphabetical order, the users will have a feel for the rate of progress
Packit 6c4009
that your application is making.
Packit 6c4009
Packit 6c4009
If @code{glob} succeeds, it returns 0.  Otherwise, it returns one
Packit 6c4009
of these error codes:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item GLOB_ABORTED
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
There was an error opening a directory, and you used the flag
Packit 6c4009
@code{GLOB_ERR} or your specified @var{errfunc} returned a nonzero
Packit 6c4009
value.
Packit 6c4009
@iftex
Packit 6c4009
See below
Packit 6c4009
@end iftex
Packit 6c4009
@ifinfo
Packit 6c4009
@xref{Flags for Globbing},
Packit 6c4009
@end ifinfo
Packit 6c4009
for an explanation of the @code{GLOB_ERR} flag and @var{errfunc}.
Packit 6c4009
Packit 6c4009
@item GLOB_NOMATCH
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
The pattern didn't match any existing files.  If you use the
Packit 6c4009
@code{GLOB_NOCHECK} flag, then you never get this error code, because
Packit 6c4009
that flag tells @code{glob} to @emph{pretend} that the pattern matched
Packit 6c4009
at least one file.
Packit 6c4009
Packit 6c4009
@item GLOB_NOSPACE
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
It was impossible to allocate memory to hold the result.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
In the event of an error, @code{glob} stores information in
Packit 6c4009
@code{*@var{vector-ptr}} about all the matches it has found so far.
Packit 6c4009
Packit 6c4009
It is important to notice that the @code{glob} function will not fail if
Packit 6c4009
it encounters directories or files which cannot be handled without the
Packit 6c4009
LFS interfaces.  The implementation of @code{glob} is supposed to use
Packit 6c4009
these functions internally.  This at least is the assumption made by
Packit 6c4009
the Unix standard.  The GNU extension of allowing the user to provide their
Packit 6c4009
own directory handling and @code{stat} functions complicates things a
Packit 6c4009
bit.  If these callback functions are used and a large file or directory
Packit 6c4009
is encountered @code{glob} @emph{can} fail.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun int glob64 (const char *@var{pattern}, int @var{flags}, int (*@var{errfunc}) (const char *@var{filename}, int @var{error-code}), glob64_t *@var{vector-ptr})
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtsenv{} @mtascusig{:ALRM} @mtascutimer{} @mtslocale{}}@asunsafe{@ascudlopen{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
Packit 6c4009
@c Same code as glob, but with glob64_t #defined as glob_t.
Packit 6c4009
The @code{glob64} function was added as part of the Large File Summit
Packit 6c4009
extensions but is not part of the original LFS proposal.  The reason for
Packit 6c4009
this is simple: it is not necessary.  The necessity for a @code{glob64}
Packit 6c4009
function is added by the extensions of the GNU @code{glob}
Packit 6c4009
implementation which allows the user to provide their own directory handling
Packit 6c4009
and @code{stat} functions.  The @code{readdir} and @code{stat} functions
Packit 6c4009
do depend on the choice of @code{_FILE_OFFSET_BITS} since the definition
Packit 6c4009
of the types @code{struct dirent} and @code{struct stat} will change
Packit 6c4009
depending on the choice.
Packit 6c4009
Packit 6c4009
Besides this difference, @code{glob64} works just like @code{glob} in
Packit 6c4009
all aspects.
Packit 6c4009
Packit 6c4009
This function is a GNU extension.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Flags for Globbing
Packit 6c4009
@subsection Flags for Globbing
Packit 6c4009
Packit 6c4009
This section describes the standard flags that you can specify in the
Packit 6c4009
@var{flags} argument to @code{glob}.  Choose the flags you want,
Packit 6c4009
and combine them with the C bitwise OR operator @code{|}.
Packit 6c4009
Packit 6c4009
Note that there are @ref{More Flags for Globbing} available as GNU extensions.
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item GLOB_APPEND
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
Append the words from this expansion to the vector of words produced by
Packit 6c4009
previous calls to @code{glob}.  This way you can effectively expand
Packit 6c4009
several words as if they were concatenated with spaces between them.
Packit 6c4009
Packit 6c4009
In order for appending to work, you must not modify the contents of the
Packit 6c4009
word vector structure between calls to @code{glob}.  And, if you set
Packit 6c4009
@code{GLOB_DOOFFS} in the first call to @code{glob}, you must also
Packit 6c4009
set it when you append to the results.
Packit 6c4009
Packit 6c4009
Note that the pointer stored in @code{gl_pathv} may no longer be valid
Packit 6c4009
after you call @code{glob} the second time, because @code{glob} might
Packit 6c4009
have relocated the vector.  So always fetch @code{gl_pathv} from the
Packit 6c4009
@code{glob_t} structure after each @code{glob} call; @strong{never} save
Packit 6c4009
the pointer across calls.
Packit 6c4009
Packit 6c4009
@item GLOB_DOOFFS
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
Leave blank slots at the beginning of the vector of words.
Packit 6c4009
The @code{gl_offs} field says how many slots to leave.
Packit 6c4009
The blank slots contain null pointers.
Packit 6c4009
Packit 6c4009
@item GLOB_ERR
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
Give up right away and report an error if there is any difficulty
Packit 6c4009
reading the directories that must be read in order to expand @var{pattern}
Packit 6c4009
fully.  Such difficulties might include a directory in which you don't
Packit 6c4009
have the requisite access.  Normally, @code{glob} tries its best to keep
Packit 6c4009
on going despite any errors, reading whatever directories it can.
Packit 6c4009
Packit 6c4009
You can exercise even more control than this by specifying an
Packit 6c4009
error-handler function @var{errfunc} when you call @code{glob}.  If
Packit 6c4009
@var{errfunc} is not a null pointer, then @code{glob} doesn't give up
Packit 6c4009
right away when it can't read a directory; instead, it calls
Packit 6c4009
@var{errfunc} with two arguments, like this:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
(*@var{errfunc}) (@var{filename}, @var{error-code})
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
The argument @var{filename} is the name of the directory that
Packit 6c4009
@code{glob} couldn't open or couldn't read, and @var{error-code} is the
Packit 6c4009
@code{errno} value that was reported to @code{glob}.
Packit 6c4009
Packit 6c4009
If the error handler function returns nonzero, then @code{glob} gives up
Packit 6c4009
right away.  Otherwise, it continues.
Packit 6c4009
Packit 6c4009
@item GLOB_MARK
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
If the pattern matches the name of a directory, append @samp{/} to the
Packit 6c4009
directory's name when returning it.
Packit 6c4009
Packit 6c4009
@item GLOB_NOCHECK
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
If the pattern doesn't match any file names, return the pattern itself
Packit 6c4009
as if it were a file name that had been matched.  (Normally, when the
Packit 6c4009
pattern doesn't match anything, @code{glob} returns that there were no
Packit 6c4009
matches.)
Packit 6c4009
Packit 6c4009
@item GLOB_NOESCAPE
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
Don't treat the @samp{\} character specially in patterns.  Normally,
Packit 6c4009
@samp{\} quotes the following character, turning off its special meaning
Packit 6c4009
(if any) so that it matches only itself.  When quoting is enabled, the
Packit 6c4009
pattern @samp{\?} matches only the string @samp{?}, because the question
Packit 6c4009
mark in the pattern acts like an ordinary character.
Packit 6c4009
Packit 6c4009
If you use @code{GLOB_NOESCAPE}, then @samp{\} is an ordinary character.
Packit 6c4009
Packit 6c4009
@code{glob} does its work by calling the function @code{fnmatch}
Packit 6c4009
repeatedly.  It handles the flag @code{GLOB_NOESCAPE} by turning on the
Packit 6c4009
@code{FNM_NOESCAPE} flag in calls to @code{fnmatch}.
Packit 6c4009
Packit 6c4009
@item GLOB_NOSORT
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
Don't sort the file names; return them in no particular order.
Packit 6c4009
(In practice, the order will depend on the order of the entries in
Packit 6c4009
the directory.)  The only reason @emph{not} to sort is to save time.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node More Flags for Globbing
Packit 6c4009
@subsection More Flags for Globbing
Packit 6c4009
Packit 6c4009
Beside the flags described in the last section, the GNU implementation of
Packit 6c4009
@code{glob} allows a few more flags which are also defined in the
Packit 6c4009
@file{glob.h} file.  Some of the extensions implement functionality
Packit 6c4009
which is available in modern shell implementations.
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item GLOB_PERIOD
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
The @code{.} character (period) is treated special.  It cannot be
Packit 6c4009
matched by wildcards.  @xref{Wildcard Matching}, @code{FNM_PERIOD}.
Packit 6c4009
Packit 6c4009
@item GLOB_MAGCHAR
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
The @code{GLOB_MAGCHAR} value is not to be given to @code{glob} in the
Packit 6c4009
@var{flags} parameter.  Instead, @code{glob} sets this bit in the
Packit 6c4009
@var{gl_flags} element of the @var{glob_t} structure provided as the
Packit 6c4009
result if the pattern used for matching contains any wildcard character.
Packit 6c4009
Packit 6c4009
@item GLOB_ALTDIRFUNC
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
Instead of using the normal functions for accessing the
Packit 6c4009
filesystem the @code{glob} implementation uses the user-supplied
Packit 6c4009
functions specified in the structure pointed to by @var{pglob}
Packit 6c4009
parameter.  For more information about the functions refer to the
Packit 6c4009
sections about directory handling see @ref{Accessing Directories}, and
Packit 6c4009
@ref{Reading Attributes}.
Packit 6c4009
Packit 6c4009
@item GLOB_BRACE
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
If this flag is given, the handling of braces in the pattern is changed.
Packit 6c4009
It is now required that braces appear correctly grouped.  I.e., for each
Packit 6c4009
opening brace there must be a closing one.  Braces can be used
Packit 6c4009
recursively.  So it is possible to define one brace expression in
Packit 6c4009
another one.  It is important to note that the range of each brace
Packit 6c4009
expression is completely contained in the outer brace expression (if
Packit 6c4009
there is one).
Packit 6c4009
Packit 6c4009
The string between the matching braces is separated into single
Packit 6c4009
expressions by splitting at @code{,} (comma) characters.  The commas
Packit 6c4009
themselves are discarded.  Please note what we said above about recursive
Packit 6c4009
brace expressions.  The commas used to separate the subexpressions must
Packit 6c4009
be at the same level.  Commas in brace subexpressions are not matched.
Packit 6c4009
They are used during expansion of the brace expression of the deeper
Packit 6c4009
level.  The example below shows this
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
glob ("@{foo/@{,bar,biz@},baz@}", GLOB_BRACE, NULL, &result)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
is equivalent to the sequence
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
glob ("foo/", GLOB_BRACE, NULL, &result)
Packit 6c4009
glob ("foo/bar", GLOB_BRACE|GLOB_APPEND, NULL, &result)
Packit 6c4009
glob ("foo/biz", GLOB_BRACE|GLOB_APPEND, NULL, &result)
Packit 6c4009
glob ("baz", GLOB_BRACE|GLOB_APPEND, NULL, &result)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
if we leave aside error handling.
Packit 6c4009
Packit 6c4009
@item GLOB_NOMAGIC
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
If the pattern contains no wildcard constructs (it is a literal file name),
Packit 6c4009
return it as the sole ``matching'' word, even if no file exists by that name.
Packit 6c4009
Packit 6c4009
@item GLOB_TILDE
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
If this flag is used the character @code{~} (tilde) is handled specially
Packit 6c4009
if it appears at the beginning of the pattern.  Instead of being taken
Packit 6c4009
verbatim it is used to represent the home directory of a known user.
Packit 6c4009
Packit 6c4009
If @code{~} is the only character in pattern or it is followed by a
Packit 6c4009
@code{/} (slash), the home directory of the process owner is
Packit 6c4009
substituted.  Using @code{getlogin} and @code{getpwnam} the information
Packit 6c4009
is read from the system databases.  As an example take user @code{bart}
Packit 6c4009
with his home directory at @file{/home/bart}.  For him a call like
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
glob ("~/bin/*", GLOB_TILDE, NULL, &result)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
would return the contents of the directory @file{/home/bart/bin}.
Packit 6c4009
Instead of referring to the own home directory it is also possible to
Packit 6c4009
name the home directory of other users.  To do so one has to append the
Packit 6c4009
user name after the tilde character.  So the contents of user
Packit 6c4009
@code{homer}'s @file{bin} directory can be retrieved by
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
glob ("~homer/bin/*", GLOB_TILDE, NULL, &result)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
If the user name is not valid or the home directory cannot be determined
Packit 6c4009
for some reason the pattern is left untouched and itself used as the
Packit 6c4009
result.  I.e., if in the last example @code{home} is not available the
Packit 6c4009
tilde expansion yields to @code{"~homer/bin/*"} and @code{glob} is not
Packit 6c4009
looking for a directory named @code{~homer}.
Packit 6c4009
Packit 6c4009
This functionality is equivalent to what is available in C-shells if the
Packit 6c4009
@code{nonomatch} flag is set.
Packit 6c4009
Packit 6c4009
@item GLOB_TILDE_CHECK
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
If this flag is used @code{glob} behaves as if @code{GLOB_TILDE} is
Packit 6c4009
given.  The only difference is that if the user name is not available or
Packit 6c4009
the home directory cannot be determined for other reasons this leads to
Packit 6c4009
an error.  @code{glob} will return @code{GLOB_NOMATCH} instead of using
Packit 6c4009
the pattern itself as the name.
Packit 6c4009
Packit 6c4009
This functionality is equivalent to what is available in C-shells if
Packit 6c4009
the @code{nonomatch} flag is not set.
Packit 6c4009
Packit 6c4009
@item GLOB_ONLYDIR
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
If this flag is used the globbing function takes this as a
Packit 6c4009
@strong{hint} that the caller is only interested in directories
Packit 6c4009
matching the pattern.  If the information about the type of the file
Packit 6c4009
is easily available non-directories will be rejected but no extra
Packit 6c4009
work will be done to determine the information for each file.  I.e.,
Packit 6c4009
the caller must still be able to filter directories out.
Packit 6c4009
Packit 6c4009
This functionality is only available with the GNU @code{glob}
Packit 6c4009
implementation.  It is mainly used internally to increase the
Packit 6c4009
performance but might be useful for a user as well and therefore is
Packit 6c4009
documented here.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
Calling @code{glob} will in most cases allocate resources which are used
Packit 6c4009
to represent the result of the function call.  If the same object of
Packit 6c4009
type @code{glob_t} is used in multiple call to @code{glob} the resources
Packit 6c4009
are freed or reused so that no leaks appear.  But this does not include
Packit 6c4009
the time when all @code{glob} calls are done.
Packit 6c4009
Packit 6c4009
@deftypefun void globfree (glob_t *@var{pglob})
Packit 6c4009
@standards{POSIX.2, glob.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
Packit 6c4009
@c globfree dup @asucorrupt @ascuheap @acucorrupt @acsmem
Packit 6c4009
@c  free dup @ascuheap @acsmem
Packit 6c4009
The @code{globfree} function frees all resources allocated by previous
Packit 6c4009
calls to @code{glob} associated with the object pointed to by
Packit 6c4009
@var{pglob}.  This function should be called whenever the currently used
Packit 6c4009
@code{glob_t} typed object isn't used anymore.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void globfree64 (glob64_t *@var{pglob})
Packit 6c4009
@standards{GNU, glob.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
Packit 6c4009
This function is equivalent to @code{globfree} but it frees records of
Packit 6c4009
type @code{glob64_t} which were allocated by @code{glob64}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Regular Expressions
Packit 6c4009
@section Regular Expression Matching
Packit 6c4009
Packit 6c4009
@Theglibc{} supports two interfaces for matching regular
Packit 6c4009
expressions.  One is the standard POSIX.2 interface, and the other is
Packit 6c4009
what @theglibc{} has had for many years.
Packit 6c4009
Packit 6c4009
Both interfaces are declared in the header file @file{regex.h}.
Packit 6c4009
If you define @w{@code{_POSIX_C_SOURCE}}, then only the POSIX.2
Packit 6c4009
functions, structures, and constants are declared.
Packit 6c4009
@c !!! we only document the POSIX.2 interface here!!
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* POSIX Regexp Compilation::    Using @code{regcomp} to prepare to match.
Packit 6c4009
* Flags for POSIX Regexps::     Syntax variations for @code{regcomp}.
Packit 6c4009
* Matching POSIX Regexps::      Using @code{regexec} to match the compiled
Packit 6c4009
				   pattern that you get from @code{regcomp}.
Packit 6c4009
* Regexp Subexpressions::       Finding which parts of the string were matched.
Packit 6c4009
* Subexpression Complications:: Find points of which parts were matched.
Packit 6c4009
* Regexp Cleanup::		Freeing storage; reporting errors.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node POSIX Regexp Compilation
Packit 6c4009
@subsection POSIX Regular Expression Compilation
Packit 6c4009
Packit 6c4009
Before you can actually match a regular expression, you must
Packit 6c4009
@dfn{compile} it.  This is not true compilation---it produces a special
Packit 6c4009
data structure, not machine instructions.  But it is like ordinary
Packit 6c4009
compilation in that its purpose is to enable you to ``execute'' the
Packit 6c4009
pattern fast.  (@xref{Matching POSIX Regexps}, for how to use the
Packit 6c4009
compiled regular expression for matching.)
Packit 6c4009
Packit 6c4009
There is a special data type for compiled regular expressions:
Packit 6c4009
Packit 6c4009
@deftp {Data Type} regex_t
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
This type of object holds a compiled regular expression.
Packit 6c4009
It is actually a structure.  It has just one field that your programs
Packit 6c4009
should look at:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item re_nsub
Packit 6c4009
This field holds the number of parenthetical subexpressions in the
Packit 6c4009
regular expression that was compiled.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
There are several other fields, but we don't describe them here, because
Packit 6c4009
only the functions in the library should use them.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
After you create a @code{regex_t} object, you can compile a regular
Packit 6c4009
expression into it by calling @code{regcomp}.
Packit 6c4009
Packit 6c4009
@deftypefun int regcomp (regex_t *restrict @var{compiled}, const char *restrict @var{pattern}, int @var{cflags})
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
Packit 6c4009
@c All of the issues have to do with memory allocation and multi-byte
Packit 6c4009
@c character handling present in the input string, or implied by ranges
Packit 6c4009
@c or inverted character classes.
Packit 6c4009
@c (re_)malloc @ascuheap @acsmem
Packit 6c4009
@c re_compile_internal @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c  (re_)realloc @ascuheap @acsmem [no @asucorrupt @acucorrupt for we zero the buffer]
Packit 6c4009
@c  init_dfa @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   (re_)malloc @ascuheap @acsmem
Packit 6c4009
@c   calloc @ascuheap @acsmem
Packit 6c4009
@c   _NL_CURRENT ok
Packit 6c4009
@c   _NL_CURRENT_WORD ok
Packit 6c4009
@c   btowc @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c  libc_lock_init ok
Packit 6c4009
@c  re_string_construct @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   re_string_construct_common ok
Packit 6c4009
@c   re_string_realloc_buffers @ascuheap @acsmem
Packit 6c4009
@c    (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c   build_wcs_upper_buffer @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    isascii ok
Packit 6c4009
@c    mbsinit ok
Packit 6c4009
@c    toupper ok
Packit 6c4009
@c    mbrtowc dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    iswlower @mtslocale
Packit 6c4009
@c    towupper @mtslocale
Packit 6c4009
@c    wcrtomb dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c   build_upper_buffer ok (@mtslocale but optimized)
Packit 6c4009
@c    islower ok
Packit 6c4009
@c    toupper ok
Packit 6c4009
@c   build_wcs_buffer @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    mbrtowc dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   re_string_translate_buffer ok
Packit 6c4009
@c  parse @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   fetch_token @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    peek_token @mtslocale
Packit 6c4009
@c     re_string_eoi ok
Packit 6c4009
@c     re_string_peek_byte ok
Packit 6c4009
@c     re_string_cur_idx ok
Packit 6c4009
@c     re_string_length ok
Packit 6c4009
@c     re_string_peek_byte_case @mtslocale
Packit 6c4009
@c      re_string_peek_byte dup ok
Packit 6c4009
@c      re_string_is_single_byte_char ok
Packit 6c4009
@c      isascii ok
Packit 6c4009
@c      re_string_peek_byte dup ok
Packit 6c4009
@c     re_string_wchar_at ok
Packit 6c4009
@c     re_string_skip_bytes ok
Packit 6c4009
@c    re_string_skip_bytes dup ok
Packit 6c4009
@c   parse_reg_exp @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    parse_branch @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c     parse_expression @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c      create_token_tree dup @ascuheap @acsmem
Packit 6c4009
@c      re_string_eoi dup ok
Packit 6c4009
@c      re_string_first_byte ok
Packit 6c4009
@c      fetch_token dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c      create_tree dup @ascuheap @acsmem
Packit 6c4009
@c      parse_sub_exp @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c       fetch_token dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c       parse_reg_exp dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c       postorder() @ascuheap @acsmem
Packit 6c4009
@c        free_tree @ascuheap @acsmem
Packit 6c4009
@c         free_token dup @ascuheap @acsmem
Packit 6c4009
@c       create_tree dup @ascuheap @acsmem
Packit 6c4009
@c      parse_bracket_exp @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c       _NL_CURRENT dup ok
Packit 6c4009
@c       _NL_CURRENT_WORD dup ok
Packit 6c4009
@c       calloc dup @ascuheap @acsmem
Packit 6c4009
@c       (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c       peek_token_bracket ok
Packit 6c4009
@c        re_string_eoi dup ok
Packit 6c4009
@c        re_string_peek_byte dup ok
Packit 6c4009
@c        re_string_first_byte dup ok
Packit 6c4009
@c        re_string_cur_idx dup ok
Packit 6c4009
@c        re_string_length dup ok
Packit 6c4009
@c        re_string_skip_bytes dup ok
Packit 6c4009
@c       bitset_set ok
Packit 6c4009
@c       re_string_skip_bytes ok
Packit 6c4009
@c       parse_bracket_element @mtslocale
Packit 6c4009
@c        re_string_char_size_at ok
Packit 6c4009
@c        re_string_wchar_at dup ok
Packit 6c4009
@c        re_string_skip_bytes dup ok
Packit 6c4009
@c        parse_bracket_symbol @mtslocale
Packit 6c4009
@c         re_string_eoi dup ok
Packit 6c4009
@c         re_string_fetch_byte_case @mtslocale
Packit 6c4009
@c          re_string_fetch_byte ok
Packit 6c4009
@c          re_string_first_byte dup ok
Packit 6c4009
@c          isascii ok
Packit 6c4009
@c          re_string_char_size_at dup ok
Packit 6c4009
@c          re_string_skip_bytes dup ok
Packit 6c4009
@c         re_string_fetch_byte dup ok
Packit 6c4009
@c         re_string_peek_byte dup ok
Packit 6c4009
@c         re_string_skip_bytes dup ok
Packit 6c4009
@c        peek_token_bracket dup ok
Packit 6c4009
@c       auto build_range_exp @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c        auto lookup_collation_sequence_value @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c         btowc dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c         collseq_table_lookup ok
Packit 6c4009
@c         auto seek_collating_symbol_entry dup ok
Packit 6c4009
@c        (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c        collseq_table_lookup dup ok
Packit 6c4009
@c       bitset_set dup ok
Packit 6c4009
@c       (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c       build_equiv_class @mtslocale @ascuheap @acsmem
Packit 6c4009
@c        _NL_CURRENT ok
Packit 6c4009
@c        auto findidx ok
Packit 6c4009
@c        bitset_set dup ok
Packit 6c4009
@c        (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c       auto build_collating_symbol @ascuheap @acsmem
Packit 6c4009
@c        auto seek_collating_symbol_entry ok
Packit 6c4009
@c        bitset_set dup ok
Packit 6c4009
@c        (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c       build_charclass @mtslocale @ascuheap @acsmem
Packit 6c4009
@c        (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c        bitset_set dup ok
Packit 6c4009
@c        isalnum ok
Packit 6c4009
@c        iscntrl ok
Packit 6c4009
@c        isspace ok
Packit 6c4009
@c        isalpha ok
Packit 6c4009
@c        isdigit ok
Packit 6c4009
@c        isprint ok
Packit 6c4009
@c        isupper ok
Packit 6c4009
@c        isblank ok
Packit 6c4009
@c        isgraph ok
Packit 6c4009
@c        ispunct ok
Packit 6c4009
@c        isxdigit ok
Packit 6c4009
@c       bitset_not ok
Packit 6c4009
@c       bitset_mask ok
Packit 6c4009
@c       create_token_tree dup @ascuheap @acsmem
Packit 6c4009
@c       create_tree dup @ascuheap @acsmem
Packit 6c4009
@c       free_charset dup @ascuheap @acsmem
Packit 6c4009
@c      init_word_char @mtslocale
Packit 6c4009
@c       isalnum ok
Packit 6c4009
@c      build_charclass_op @mtslocale @ascuheap @acsmem
Packit 6c4009
@c       calloc dup @ascuheap @acsmem
Packit 6c4009
@c       build_charclass dup @mtslocale @ascuheap @acsmem
Packit 6c4009
@c       (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c       free_charset dup @ascuheap @acsmem
Packit 6c4009
@c       bitset_set dup ok
Packit 6c4009
@c       bitset_not dup ok
Packit 6c4009
@c       bitset_mask dup ok
Packit 6c4009
@c       create_token_tree dup @ascuheap @acsmem
Packit 6c4009
@c       create_tree dup @ascuheap @acsmem
Packit 6c4009
@c      parse_dup_op @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c       re_string_cur_idx dup ok
Packit 6c4009
@c       fetch_number @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c        fetch_token dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c       re_string_set_index ok
Packit 6c4009
@c       postorder() @ascuheap @acsmem
Packit 6c4009
@c        free_tree dup @ascuheap @acsmem
Packit 6c4009
@c        mark_opt_subexp ok
Packit 6c4009
@c       duplicate_tree @ascuheap @acsmem
Packit 6c4009
@c        create_token_tree dup @ascuheap @acsmem
Packit 6c4009
@c       create_tree dup @ascuheap @acsmem
Packit 6c4009
@c     postorder() @ascuheap @acsmem
Packit 6c4009
@c      free_tree dup @ascuheap @acsmem
Packit 6c4009
@c    fetch_token dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    parse_branch dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    create_tree dup @ascuheap @acsmem
Packit 6c4009
@c   create_tree @ascuheap @acsmem
Packit 6c4009
@c    create_token_tree @ascuheap @acsmem
Packit 6c4009
@c     (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c  analyze @ascuheap @acsmem
Packit 6c4009
@c   (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c   preorder() @ascuheap @acsmem
Packit 6c4009
@c    optimize_subexps ok
Packit 6c4009
@c    calc_next ok
Packit 6c4009
@c    link_nfa_nodes @ascuheap @acsmem
Packit 6c4009
@c     re_node_set_init_1 @ascuheap @acsmem
Packit 6c4009
@c      (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c     re_node_set_init_2 @ascuheap @acsmem
Packit 6c4009
@c      (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c   postorder() @ascuheap @acsmem
Packit 6c4009
@c    lower_subexps @ascuheap @acsmem
Packit 6c4009
@c     lower_subexp @ascuheap @acsmem
Packit 6c4009
@c      create_tree dup @ascuheap @acsmem
Packit 6c4009
@c    calc_first @ascuheap @acsmem
Packit 6c4009
@c     re_dfa_add_node @ascuheap @acsmem
Packit 6c4009
@c      (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_init_empty ok
Packit 6c4009
@c   calc_eclosure @ascuheap @acsmem
Packit 6c4009
@c    calc_eclosure_iter @ascuheap @acsmem
Packit 6c4009
@c     re_node_set_alloc @ascuheap @acsmem
Packit 6c4009
@c      (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c     duplicate_node_closure @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_empty ok
Packit 6c4009
@c      duplicate_node @ascuheap @acsmem
Packit 6c4009
@c       re_dfa_add_node dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_insert @ascuheap @acsmem
Packit 6c4009
@c       (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c      search_duplicated_node ok
Packit 6c4009
@c     re_node_set_merge @ascuheap @acsmem
Packit 6c4009
@c      (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c     re_node_set_free @ascuheap @acsmem
Packit 6c4009
@c      (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c     re_node_set_insert dup @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c   calc_inveclosure @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_init_empty dup ok
Packit 6c4009
@c    re_node_set_insert_last @ascuheap @acsmem
Packit 6c4009
@c     (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c  optimize_utf8 ok
Packit 6c4009
@c  create_initial_state @ascuheap @acsmem
Packit 6c4009
@c   re_node_set_init_copy @ascuheap @acsmem
Packit 6c4009
@c    (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_init_empty dup ok
Packit 6c4009
@c   re_node_set_contains ok
Packit 6c4009
@c   re_node_set_merge dup @ascuheap @acsmem
Packit 6c4009
@c   re_acquire_state_context @ascuheap @acsmem
Packit 6c4009
@c    calc_state_hash ok
Packit 6c4009
@c    re_node_set_compare ok
Packit 6c4009
@c    create_cd_newstate @ascuheap @acsmem
Packit 6c4009
@c     calloc dup @ascuheap @acsmem
Packit 6c4009
@c     re_node_set_init_copy dup @ascuheap @acsmem
Packit 6c4009
@c     (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c     free_state @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c      (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c     NOT_SATISFY_PREV_CONSTRAINT ok
Packit 6c4009
@c     re_node_set_remove_at ok
Packit 6c4009
@c     register_state @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_alloc dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_insert_last dup @ascuheap @acsmem
Packit 6c4009
@c      (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c   re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c  free_workarea_compile @ascuheap @acsmem
Packit 6c4009
@c   (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c  re_string_destruct @ascuheap @acsmem
Packit 6c4009
@c   (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c  free_dfa_content @ascuheap @acsmem
Packit 6c4009
@c   free_token @ascuheap @acsmem
Packit 6c4009
@c    free_charset @ascuheap @acsmem
Packit 6c4009
@c     (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c    (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c   (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c   re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c re_compile_fastmap @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c  re_compile_fastmap_iter @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   re_set_fastmap ok
Packit 6c4009
@c    tolower ok
Packit 6c4009
@c   mbrtowc dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   wcrtomb dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   towlower @mtslocale
Packit 6c4009
@c   _NL_CURRENT ok
Packit 6c4009
@c (re_)free @ascuheap @acsmem
Packit 6c4009
The function @code{regcomp} ``compiles'' a regular expression into a
Packit 6c4009
data structure that you can use with @code{regexec} to match against a
Packit 6c4009
string.  The compiled regular expression format is designed for
Packit 6c4009
efficient matching.  @code{regcomp} stores it into @code{*@var{compiled}}.
Packit 6c4009
Packit 6c4009
It's up to you to allocate an object of type @code{regex_t} and pass its
Packit 6c4009
address to @code{regcomp}.
Packit 6c4009
Packit 6c4009
The argument @var{cflags} lets you specify various options that control
Packit 6c4009
the syntax and semantics of regular expressions.  @xref{Flags for POSIX
Packit 6c4009
Regexps}.
Packit 6c4009
Packit 6c4009
If you use the flag @code{REG_NOSUB}, then @code{regcomp} omits from
Packit 6c4009
the compiled regular expression the information necessary to record
Packit 6c4009
how subexpressions actually match.  In this case, you might as well
Packit 6c4009
pass @code{0} for the @var{matchptr} and @var{nmatch} arguments when
Packit 6c4009
you call @code{regexec}.
Packit 6c4009
Packit 6c4009
If you don't use @code{REG_NOSUB}, then the compiled regular expression
Packit 6c4009
does have the capacity to record how subexpressions match.  Also,
Packit 6c4009
@code{regcomp} tells you how many subexpressions @var{pattern} has, by
Packit 6c4009
storing the number in @code{@var{compiled}->re_nsub}.  You can use that
Packit 6c4009
value to decide how long an array to allocate to hold information about
Packit 6c4009
subexpression matches.
Packit 6c4009
Packit 6c4009
@code{regcomp} returns @code{0} if it succeeds in compiling the regular
Packit 6c4009
expression; otherwise, it returns a nonzero error code (see the table
Packit 6c4009
below).  You can use @code{regerror} to produce an error message string
Packit 6c4009
describing the reason for a nonzero value; see @ref{Regexp Cleanup}.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Here are the possible nonzero values that @code{regcomp} can return:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item REG_BADBR
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
There was an invalid @samp{\@{@dots{}\@}} construct in the regular
Packit 6c4009
expression.  A valid @samp{\@{@dots{}\@}} construct must contain either
Packit 6c4009
a single number, or two numbers in increasing order separated by a
Packit 6c4009
comma.
Packit 6c4009
Packit 6c4009
@item REG_BADPAT
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
There was a syntax error in the regular expression.
Packit 6c4009
Packit 6c4009
@item REG_BADRPT
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
A repetition operator such as @samp{?} or @samp{*} appeared in a bad
Packit 6c4009
position (with no preceding subexpression to act on).
Packit 6c4009
Packit 6c4009
@item REG_ECOLLATE
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
The regular expression referred to an invalid collating element (one not
Packit 6c4009
defined in the current locale for string collation).  @xref{Locale
Packit 6c4009
Categories}.
Packit 6c4009
Packit 6c4009
@item REG_ECTYPE
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
The regular expression referred to an invalid character class name.
Packit 6c4009
Packit 6c4009
@item REG_EESCAPE
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
The regular expression ended with @samp{\}.
Packit 6c4009
Packit 6c4009
@item REG_ESUBREG
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
There was an invalid number in the @samp{\@var{digit}} construct.
Packit 6c4009
Packit 6c4009
@item REG_EBRACK
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
There were unbalanced square brackets in the regular expression.
Packit 6c4009
Packit 6c4009
@item REG_EPAREN
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
An extended regular expression had unbalanced parentheses,
Packit 6c4009
or a basic regular expression had unbalanced @samp{\(} and @samp{\)}.
Packit 6c4009
Packit 6c4009
@item REG_EBRACE
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
The regular expression had unbalanced @samp{\@{} and @samp{\@}}.
Packit 6c4009
Packit 6c4009
@item REG_ERANGE
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
One of the endpoints in a range expression was invalid.
Packit 6c4009
Packit 6c4009
@item REG_ESPACE
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
@code{regcomp} ran out of memory.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node Flags for POSIX Regexps
Packit 6c4009
@subsection Flags for POSIX Regular Expressions
Packit 6c4009
Packit 6c4009
These are the bit flags that you can use in the @var{cflags} operand when
Packit 6c4009
compiling a regular expression with @code{regcomp}.
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item REG_EXTENDED
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
Treat the pattern as an extended regular expression, rather than as a
Packit 6c4009
basic regular expression.
Packit 6c4009
Packit 6c4009
@item REG_ICASE
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
Ignore case when matching letters.
Packit 6c4009
Packit 6c4009
@item REG_NOSUB
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
Don't bother storing the contents of the @var{matchptr} array.
Packit 6c4009
Packit 6c4009
@item REG_NEWLINE
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
Treat a newline in @var{string} as dividing @var{string} into multiple
Packit 6c4009
lines, so that @samp{$} can match before the newline and @samp{^} can
Packit 6c4009
match after.  Also, don't permit @samp{.} to match a newline, and don't
Packit 6c4009
permit @samp{[^@dots{}]} to match a newline.
Packit 6c4009
Packit 6c4009
Otherwise, newline acts like any other ordinary character.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node Matching POSIX Regexps
Packit 6c4009
@subsection Matching a Compiled POSIX Regular Expression
Packit 6c4009
Packit 6c4009
Once you have compiled a regular expression, as described in @ref{POSIX
Packit 6c4009
Regexp Compilation}, you can match it against strings using
Packit 6c4009
@code{regexec}.  A match anywhere inside the string counts as success,
Packit 6c4009
unless the regular expression contains anchor characters (@samp{^} or
Packit 6c4009
@samp{$}).
Packit 6c4009
Packit 6c4009
@deftypefun int regexec (const regex_t *restrict @var{compiled}, const char *restrict @var{string}, size_t @var{nmatch}, regmatch_t @var{matchptr}[restrict], int @var{eflags})
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
Packit 6c4009
@c libc_lock_lock @asulock @aculock
Packit 6c4009
@c re_search_internal @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c  re_string_allocate @ascuheap @acsmem
Packit 6c4009
@c   re_string_construct_common dup ok
Packit 6c4009
@c   re_string_realloc_buffers dup @ascuheap @acsmem
Packit 6c4009
@c  match_ctx_init @ascuheap @acsmem
Packit 6c4009
@c   (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c  re_string_byte_at ok
Packit 6c4009
@c  re_string_first_byte dup ok
Packit 6c4009
@c  check_matching @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   re_string_cur_idx dup ok
Packit 6c4009
@c   acquire_init_state_context dup @ascuheap @acsmem
Packit 6c4009
@c    re_string_context_at ok
Packit 6c4009
@c     re_string_byte_at dup ok
Packit 6c4009
@c     bitset_contain ok
Packit 6c4009
@c    re_acquire_state_context dup @ascuheap @acsmem
Packit 6c4009
@c   check_subexp_matching_top @ascuheap @acsmem
Packit 6c4009
@c    match_ctx_add_subtop @ascuheap @acsmem
Packit 6c4009
@c     (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c     calloc dup @ascuheap @acsmem
Packit 6c4009
@c   transit_state_bkref @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    re_string_cur_idx dup ok
Packit 6c4009
@c    re_string_context_at dup ok
Packit 6c4009
@c    NOT_SATISFY_NEXT_CONSTRAINT ok
Packit 6c4009
@c    get_subexp @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c     re_string_get_buffer ok
Packit 6c4009
@c     search_cur_bkref_entry ok
Packit 6c4009
@c     clean_state_log_if_needed @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c      extend_buffers @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c       re_string_realloc_buffers dup @ascuheap @acsmem
Packit 6c4009
@c       (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c       build_wcs_upper_buffer dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c       build_upper_buffer dup ok (@mtslocale but optimized)
Packit 6c4009
@c       build_wcs_buffer dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c       re_string_translate_buffer dup ok
Packit 6c4009
@c     get_subexp_sub @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c      check_arrival @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c       (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c       re_string_context_at dup ok
Packit 6c4009
@c       re_node_set_init_1 dup @ascuheap @acsmem
Packit 6c4009
@c       check_arrival_expand_ecl @ascuheap @acsmem
Packit 6c4009
@c        re_node_set_alloc dup @ascuheap @acsmem
Packit 6c4009
@c        find_subexp_node ok
Packit 6c4009
@c        re_node_set_merge dup @ascuheap @acsmem
Packit 6c4009
@c        re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c        check_arrival_expand_ecl_sub @ascuheap @acsmem
Packit 6c4009
@c         re_node_set_contains dup ok
Packit 6c4009
@c         re_node_set_insert dup @ascuheap @acsmem
Packit 6c4009
@c       re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c       re_node_set_init_copy dup @ascuheap @acsmem
Packit 6c4009
@c       re_node_set_init_empty dup ok
Packit 6c4009
@c       expand_bkref_cache @ascuheap @acsmem
Packit 6c4009
@c        search_cur_bkref_entry dup ok
Packit 6c4009
@c        re_node_set_contains dup ok
Packit 6c4009
@c        re_node_set_init_1 dup @ascuheap @acsmem
Packit 6c4009
@c        check_arrival_expand_ecl dup @ascuheap @acsmem
Packit 6c4009
@c        re_node_set_merge dup @ascuheap @acsmem
Packit 6c4009
@c        re_node_set_init_copy dup @ascuheap @acsmem
Packit 6c4009
@c        re_node_set_insert dup @ascuheap @acsmem
Packit 6c4009
@c        re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c        re_acquire_state @ascuheap @acsmem
Packit 6c4009
@c         calc_state_hash dup ok
Packit 6c4009
@c         re_node_set_compare dup ok
Packit 6c4009
@c         create_ci_newstate @ascuheap @acsmem
Packit 6c4009
@c          calloc dup @ascuheap @acsmem
Packit 6c4009
@c          re_node_set_init_copy dup @ascuheap @acsmem
Packit 6c4009
@c          (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c          register_state dup @ascuheap @acsmem
Packit 6c4009
@c          free_state dup @ascuheap @acsmem
Packit 6c4009
@c       re_acquire_state_context dup @ascuheap @acsmem
Packit 6c4009
@c       re_node_set_merge dup @ascuheap @acsmem
Packit 6c4009
@c       check_arrival_add_next_nodes @mtslocale @ascuheap @acsmem
Packit 6c4009
@c        re_node_set_init_empty dup ok
Packit 6c4009
@c        check_node_accept_bytes @mtslocale @ascuheap @acsmem
Packit 6c4009
@c         re_string_byte_at dup ok
Packit 6c4009
@c         re_string_char_size_at dup ok
Packit 6c4009
@c         re_string_elem_size_at @mtslocale
Packit 6c4009
@c          _NL_CURRENT_WORD dup ok
Packit 6c4009
@c          _NL_CURRENT dup ok
Packit 6c4009
@c          auto findidx dup ok
Packit 6c4009
@c         _NL_CURRENT_WORD dup ok
Packit 6c4009
@c         _NL_CURRENT dup ok
Packit 6c4009
@c         collseq_table_lookup dup ok
Packit 6c4009
@c         find_collation_sequence_value @mtslocale
Packit 6c4009
@c          _NL_CURRENT_WORD dup ok
Packit 6c4009
@c          _NL_CURRENT dup ok
Packit 6c4009
@c         auto findidx dup ok
Packit 6c4009
@c         wcscoll @mtslocale @ascuheap @acsmem
Packit 6c4009
@c        re_node_set_empty dup ok
Packit 6c4009
@c        re_node_set_merge dup @ascuheap @acsmem
Packit 6c4009
@c        re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c        re_node_set_insert dup @ascuheap @acsmem
Packit 6c4009
@c        re_acquire_state dup @ascuheap @acsmem
Packit 6c4009
@c        check_node_accept ok
Packit 6c4009
@c         re_string_byte_at dup ok
Packit 6c4009
@c         bitset_contain dup ok
Packit 6c4009
@c         re_string_context_at dup ok
Packit 6c4009
@c         NOT_SATISFY_NEXT_CONSTRAINT dup ok
Packit 6c4009
@c      match_ctx_add_entry @ascuheap @acsmem
Packit 6c4009
@c       (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c       (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c      clean_state_log_if_needed dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c     extend_buffers dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c     find_subexp_node dup ok
Packit 6c4009
@c     calloc dup @ascuheap @acsmem
Packit 6c4009
@c     check_arrival dup ***
Packit 6c4009
@c     match_ctx_add_sublast @ascuheap @acsmem
Packit 6c4009
@c      (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c    re_acquire_state_context dup @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_init_union @ascuheap @acsmem
Packit 6c4009
@c     (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c     re_node_set_init_copy dup @ascuheap @acsmem
Packit 6c4009
@c     re_node_set_init_empty dup ok
Packit 6c4009
@c    re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c    check_subexp_matching_top dup @ascuheap @acsmem
Packit 6c4009
@c   check_halt_state_context ok
Packit 6c4009
@c    re_string_context_at dup ok
Packit 6c4009
@c    check_halt_node_context ok
Packit 6c4009
@c     NOT_SATISFY_NEXT_CONSTRAINT dup ok
Packit 6c4009
@c   re_string_eoi dup ok
Packit 6c4009
@c   extend_buffers dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   transit_state @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    transit_state_mb @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c     re_string_context_at dup ok
Packit 6c4009
@c     NOT_SATISFY_NEXT_CONSTRAINT dup ok
Packit 6c4009
@c     check_node_accept_bytes dup @mtslocale @ascuheap @acsmem
Packit 6c4009
@c     re_string_cur_idx dup ok
Packit 6c4009
@c     clean_state_log_if_needed @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c     re_node_set_init_union dup @ascuheap @acsmem
Packit 6c4009
@c     re_acquire_state_context dup @ascuheap @acsmem
Packit 6c4009
@c    re_string_fetch_byte dup ok
Packit 6c4009
@c    re_string_context_at dup ok
Packit 6c4009
@c    build_trtable @ascuheap @acsmem
Packit 6c4009
@c     (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c     group_nodes_into_DFAstates @ascuheap @acsmem
Packit 6c4009
@c      bitset_empty dup ok
Packit 6c4009
@c      bitset_set dup ok
Packit 6c4009
@c      bitset_merge dup ok
Packit 6c4009
@c      bitset_set_all ok
Packit 6c4009
@c      bitset_clear ok
Packit 6c4009
@c      bitset_contain dup ok
Packit 6c4009
@c      bitset_copy ok
Packit 6c4009
@c      re_node_set_init_copy dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_insert dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_init_1 dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c     re_node_set_alloc dup @ascuheap @acsmem
Packit 6c4009
@c     malloc dup @ascuheap @acsmem
Packit 6c4009
@c     free dup @ascuheap @acsmem
Packit 6c4009
@c     re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c     bitset_empty ok
Packit 6c4009
@c     re_node_set_empty dup ok
Packit 6c4009
@c     re_node_set_merge dup @ascuheap @acsmem
Packit 6c4009
@c     re_acquire_state_context dup @ascuheap @acsmem
Packit 6c4009
@c     bitset_merge ok
Packit 6c4009
@c     calloc dup @ascuheap @acsmem
Packit 6c4009
@c     bitset_contain dup ok
Packit 6c4009
@c   merge_state_with_log @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c    re_string_cur_idx dup ok
Packit 6c4009
@c    re_node_set_init_union dup @ascuheap @acsmem
Packit 6c4009
@c    re_string_context_at dup ok
Packit 6c4009
@c    re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c    check_subexp_matching_top @ascuheap @acsmem
Packit 6c4009
@c     match_ctx_add_subtop dup @ascuheap @acsmem
Packit 6c4009
@c    transit_state_bkref dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   find_recover_state
Packit 6c4009
@c    re_string_cur_idx dup ok
Packit 6c4009
@c    re_string_skip_bytes dup ok
Packit 6c4009
@c    merge_state_with_log dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c  check_halt_state_context dup ok
Packit 6c4009
@c  prune_impossible_nodes @mtslocale @ascuheap @acsmem
Packit 6c4009
@c   (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c   sift_ctx_init ok
Packit 6c4009
@c    re_node_set_init_empty dup ok
Packit 6c4009
@c   sift_states_backward @mtslocale @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_init_1 dup @ascuheap @acsmem
Packit 6c4009
@c    update_cur_sifted_state @mtslocale @ascuheap @acsmem
Packit 6c4009
@c     add_epsilon_src_nodes @ascuheap @acsmem
Packit 6c4009
@c      re_acquire_state dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_alloc dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_merge dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_add_intersect @ascuheap @acsmem
Packit 6c4009
@c       (re_)realloc dup @ascuheap @acsmem
Packit 6c4009
@c     check_subexp_limits @ascuheap @acsmem
Packit 6c4009
@c      sub_epsilon_src_nodes @ascuheap @acsmem
Packit 6c4009
@c       re_node_set_init_empty dup ok
Packit 6c4009
@c       re_node_set_contains dup ok
Packit 6c4009
@c       re_node_set_add_intersect dup @ascuheap @acsmem
Packit 6c4009
@c       re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c       re_node_set_remove_at dup ok
Packit 6c4009
@c      re_node_set_contains dup ok
Packit 6c4009
@c     re_acquire_state dup @ascuheap @acsmem
Packit 6c4009
@c     sift_states_bkref @mtslocale @ascuheap @acsmem
Packit 6c4009
@c      search_cur_bkref_entry dup ok
Packit 6c4009
@c      check_dst_limits ok
Packit 6c4009
@c       search_cur_bkref_entry dup ok
Packit 6c4009
@c       check_dst_limits_calc_pos ok
Packit 6c4009
@c        check_dst_limits_calc_pos_1 ok
Packit 6c4009
@c      re_node_set_init_copy dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_insert dup @ascuheap @acsmem
Packit 6c4009
@c      sift_states_backward dup @mtslocale @ascuheap @acsmem
Packit 6c4009
@c      merge_state_array dup @ascuheap @acsmem
Packit 6c4009
@c      re_node_set_remove ok
Packit 6c4009
@c       re_node_set_contains dup ok
Packit 6c4009
@c       re_node_set_remove_at dup ok
Packit 6c4009
@c      re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_empty dup ok
Packit 6c4009
@c    build_sifted_states @mtslocale @ascuheap @acsmem
Packit 6c4009
@c     sift_states_iter_mb @mtslocale @ascuheap @acsmem
Packit 6c4009
@c      check_node_accept_bytes dup @mtslocale @ascuheap @acsmem
Packit 6c4009
@c     check_node_accept dup ok
Packit 6c4009
@c     check_dst_limits dup ok
Packit 6c4009
@c     re_node_set_insert dup @ascuheap @acsmem
Packit 6c4009
@c   re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c   check_halt_state_context dup ok
Packit 6c4009
@c   merge_state_array @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_init_union dup @ascuheap @acsmem
Packit 6c4009
@c    re_acquire_state dup @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c   (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c  set_regs @ascuheap @acsmem
Packit 6c4009
@c   (re_)malloc dup @ascuheap @acsmem
Packit 6c4009
@c   re_node_set_init_empty dup ok
Packit 6c4009
@c   free_fail_stack_return @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c    (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c   update_regs ok
Packit 6c4009
@c   re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c   pop_fail_stack @ascuheap @acsmem
Packit 6c4009
@c    re_node_set_free dup @ascuheap @acsmem
Packit 6c4009
@c    (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c   (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c  (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c  match_ctx_free @ascuheap @acsmem
Packit 6c4009
@c   match_ctx_clean @ascuheap @acsmem
Packit 6c4009
@c    (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c   (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c  re_string_destruct dup @ascuheap @acsmem
Packit 6c4009
@c libc_lock_unlock @aculock
Packit 6c4009
This function tries to match the compiled regular expression
Packit 6c4009
@code{*@var{compiled}} against @var{string}.
Packit 6c4009
Packit 6c4009
@code{regexec} returns @code{0} if the regular expression matches;
Packit 6c4009
otherwise, it returns a nonzero value.  See the table below for
Packit 6c4009
what nonzero values mean.  You can use @code{regerror} to produce an
Packit 6c4009
error message string describing the reason for a nonzero value;
Packit 6c4009
see @ref{Regexp Cleanup}.
Packit 6c4009
Packit 6c4009
The argument @var{eflags} is a word of bit flags that enable various
Packit 6c4009
options.
Packit 6c4009
Packit 6c4009
If you want to get information about what part of @var{string} actually
Packit 6c4009
matched the regular expression or its subexpressions, use the arguments
Packit 6c4009
@var{matchptr} and @var{nmatch}.  Otherwise, pass @code{0} for
Packit 6c4009
@var{nmatch}, and @code{NULL} for @var{matchptr}.  @xref{Regexp
Packit 6c4009
Subexpressions}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
You must match the regular expression with the same set of current
Packit 6c4009
locales that were in effect when you compiled the regular expression.
Packit 6c4009
Packit 6c4009
The function @code{regexec} accepts the following flags in the
Packit 6c4009
@var{eflags} argument:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item REG_NOTBOL
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
Do not regard the beginning of the specified string as the beginning of
Packit 6c4009
a line; more generally, don't make any assumptions about what text might
Packit 6c4009
precede it.
Packit 6c4009
Packit 6c4009
@item REG_NOTEOL
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
Do not regard the end of the specified string as the end of a line; more
Packit 6c4009
generally, don't make any assumptions about what text might follow it.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
Here are the possible nonzero values that @code{regexec} can return:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item REG_NOMATCH
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
The pattern didn't match the string.  This isn't really an error.
Packit 6c4009
Packit 6c4009
@item REG_ESPACE
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
@code{regexec} ran out of memory.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node Regexp Subexpressions
Packit 6c4009
@subsection Match Results with Subexpressions
Packit 6c4009
Packit 6c4009
When @code{regexec} matches parenthetical subexpressions of
Packit 6c4009
@var{pattern}, it records which parts of @var{string} they match.  It
Packit 6c4009
returns that information by storing the offsets into an array whose
Packit 6c4009
elements are structures of type @code{regmatch_t}.  The first element of
Packit 6c4009
the array (index @code{0}) records the part of the string that matched
Packit 6c4009
the entire regular expression.  Each other element of the array records
Packit 6c4009
the beginning and end of the part that matched a single parenthetical
Packit 6c4009
subexpression.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} regmatch_t
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
This is the data type of the @var{matchptr} array that you pass to
Packit 6c4009
@code{regexec}.  It contains two structure fields, as follows:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item rm_so
Packit 6c4009
The offset in @var{string} of the beginning of a substring.  Add this
Packit 6c4009
value to @var{string} to get the address of that part.
Packit 6c4009
Packit 6c4009
@item rm_eo
Packit 6c4009
The offset in @var{string} of the end of the substring.
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@deftp {Data Type} regoff_t
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
@code{regoff_t} is an alias for another signed integer type.
Packit 6c4009
The fields of @code{regmatch_t} have type @code{regoff_t}.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
The @code{regmatch_t} elements correspond to subexpressions
Packit 6c4009
positionally; the first element (index @code{1}) records where the first
Packit 6c4009
subexpression matched, the second element records the second
Packit 6c4009
subexpression, and so on.  The order of the subexpressions is the order
Packit 6c4009
in which they begin.
Packit 6c4009
Packit 6c4009
When you call @code{regexec}, you specify how long the @var{matchptr}
Packit 6c4009
array is, with the @var{nmatch} argument.  This tells @code{regexec} how
Packit 6c4009
many elements to store.  If the actual regular expression has more than
Packit 6c4009
@var{nmatch} subexpressions, then you won't get offset information about
Packit 6c4009
the rest of them.  But this doesn't alter whether the pattern matches a
Packit 6c4009
particular string or not.
Packit 6c4009
Packit 6c4009
If you don't want @code{regexec} to return any information about where
Packit 6c4009
the subexpressions matched, you can either supply @code{0} for
Packit 6c4009
@var{nmatch}, or use the flag @code{REG_NOSUB} when you compile the
Packit 6c4009
pattern with @code{regcomp}.
Packit 6c4009
Packit 6c4009
@node Subexpression Complications
Packit 6c4009
@subsection Complications in Subexpression Matching
Packit 6c4009
Packit 6c4009
Sometimes a subexpression matches a substring of no characters.  This
Packit 6c4009
happens when @samp{f\(o*\)} matches the string @samp{fum}.  (It really
Packit 6c4009
matches just the @samp{f}.)  In this case, both of the offsets identify
Packit 6c4009
the point in the string where the null substring was found.  In this
Packit 6c4009
example, the offsets are both @code{1}.
Packit 6c4009
Packit 6c4009
Sometimes the entire regular expression can match without using some of
Packit 6c4009
its subexpressions at all---for example, when @samp{ba\(na\)*} matches the
Packit 6c4009
string @samp{ba}, the parenthetical subexpression is not used.  When
Packit 6c4009
this happens, @code{regexec} stores @code{-1} in both fields of the
Packit 6c4009
element for that subexpression.
Packit 6c4009
Packit 6c4009
Sometimes matching the entire regular expression can match a particular
Packit 6c4009
subexpression more than once---for example, when @samp{ba\(na\)*}
Packit 6c4009
matches the string @samp{bananana}, the parenthetical subexpression
Packit 6c4009
matches three times.  When this happens, @code{regexec} usually stores
Packit 6c4009
the offsets of the last part of the string that matched the
Packit 6c4009
subexpression.  In the case of @samp{bananana}, these offsets are
Packit 6c4009
@code{6} and @code{8}.
Packit 6c4009
Packit 6c4009
But the last match is not always the one that is chosen.  It's more
Packit 6c4009
accurate to say that the last @emph{opportunity} to match is the one
Packit 6c4009
that takes precedence.  What this means is that when one subexpression
Packit 6c4009
appears within another, then the results reported for the inner
Packit 6c4009
subexpression reflect whatever happened on the last match of the outer
Packit 6c4009
subexpression.  For an example, consider @samp{\(ba\(na\)*s \)*} matching
Packit 6c4009
the string @samp{bananas bas }.  The last time the inner expression
Packit 6c4009
actually matches is near the end of the first word.  But it is
Packit 6c4009
@emph{considered} again in the second word, and fails to match there.
Packit 6c4009
@code{regexec} reports nonuse of the ``na'' subexpression.
Packit 6c4009
Packit 6c4009
Another place where this rule applies is when the regular expression
Packit 6c4009
@smallexample
Packit 6c4009
\(ba\(na\)*s \|nefer\(ti\)* \)*
Packit 6c4009
@end smallexample
Packit 6c4009
@noindent
Packit 6c4009
matches @samp{bananas nefertiti}.  The ``na'' subexpression does match
Packit 6c4009
in the first word, but it doesn't match in the second word because the
Packit 6c4009
other alternative is used there.  Once again, the second repetition of
Packit 6c4009
the outer subexpression overrides the first, and within that second
Packit 6c4009
repetition, the ``na'' subexpression is not used.  So @code{regexec}
Packit 6c4009
reports nonuse of the ``na'' subexpression.
Packit 6c4009
Packit 6c4009
@node Regexp Cleanup
Packit 6c4009
@subsection POSIX Regexp Matching Cleanup
Packit 6c4009
Packit 6c4009
When you are finished using a compiled regular expression, you can
Packit 6c4009
free the storage it uses by calling @code{regfree}.
Packit 6c4009
Packit 6c4009
@deftypefun void regfree (regex_t *@var{compiled})
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit 6c4009
@c (re_)free dup @ascuheap @acsmem
Packit 6c4009
@c free_dfa_content dup @ascuheap @acsmem
Packit 6c4009
Calling @code{regfree} frees all the storage that @code{*@var{compiled}}
Packit 6c4009
points to.  This includes various internal fields of the @code{regex_t}
Packit 6c4009
structure that aren't documented in this manual.
Packit 6c4009
Packit 6c4009
@code{regfree} does not free the object @code{*@var{compiled}} itself.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
You should always free the space in a @code{regex_t} structure with
Packit 6c4009
@code{regfree} before using the structure to compile another regular
Packit 6c4009
expression.
Packit 6c4009
Packit 6c4009
When @code{regcomp} or @code{regexec} reports an error, you can use
Packit 6c4009
the function @code{regerror} to turn it into an error message string.
Packit 6c4009
Packit 6c4009
@deftypefun size_t regerror (int @var{errcode}, const regex_t *restrict @var{compiled}, char *restrict @var{buffer}, size_t @var{length})
Packit 6c4009
@standards{POSIX.2, regex.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsenv{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
Packit 6c4009
@c regerror calls gettext, strcmp and mempcpy or memcpy.
Packit 6c4009
This function produces an error message string for the error code
Packit 6c4009
@var{errcode}, and stores the string in @var{length} bytes of memory
Packit 6c4009
starting at @var{buffer}.  For the @var{compiled} argument, supply the
Packit 6c4009
same compiled regular expression structure that @code{regcomp} or
Packit 6c4009
@code{regexec} was working with when it got the error.  Alternatively,
Packit 6c4009
you can supply @code{NULL} for @var{compiled}; you will still get a
Packit 6c4009
meaningful error message, but it might not be as detailed.
Packit 6c4009
Packit 6c4009
If the error message can't fit in @var{length} bytes (including a
Packit 6c4009
terminating null character), then @code{regerror} truncates it.
Packit 6c4009
The string that @code{regerror} stores is always null-terminated
Packit 6c4009
even if it has been truncated.
Packit 6c4009
Packit 6c4009
The return value of @code{regerror} is the minimum length needed to
Packit 6c4009
store the entire error message.  If this is less than @var{length}, then
Packit 6c4009
the error message was not truncated, and you can use it.  Otherwise, you
Packit 6c4009
should call @code{regerror} again with a larger buffer.
Packit 6c4009
Packit 6c4009
Here is a function which uses @code{regerror}, but always dynamically
Packit 6c4009
allocates a buffer for the error message:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
char *get_regerror (int errcode, regex_t *compiled)
Packit 6c4009
@{
Packit 6c4009
  size_t length = regerror (errcode, compiled, NULL, 0);
Packit 6c4009
  char *buffer = xmalloc (length);
Packit 6c4009
  (void) regerror (errcode, compiled, buffer, length);
Packit 6c4009
  return buffer;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Word Expansion
Packit 6c4009
@section Shell-Style Word Expansion
Packit 6c4009
@cindex word expansion
Packit 6c4009
@cindex expansion of shell words
Packit 6c4009
Packit 6c4009
@dfn{Word expansion} means the process of splitting a string into
Packit 6c4009
@dfn{words} and substituting for variables, commands, and wildcards
Packit 6c4009
just as the shell does.
Packit 6c4009
Packit 6c4009
For example, when you write @samp{ls -l foo.c}, this string is split
Packit 6c4009
into three separate words---@samp{ls}, @samp{-l} and @samp{foo.c}.
Packit 6c4009
This is the most basic function of word expansion.
Packit 6c4009
Packit 6c4009
When you write @samp{ls *.c}, this can become many words, because
Packit 6c4009
the word @samp{*.c} can be replaced with any number of file names.
Packit 6c4009
This is called @dfn{wildcard expansion}, and it is also a part of
Packit 6c4009
word expansion.
Packit 6c4009
Packit 6c4009
When you use @samp{echo $PATH} to print your path, you are taking
Packit 6c4009
advantage of @dfn{variable substitution}, which is also part of word
Packit 6c4009
expansion.
Packit 6c4009
Packit 6c4009
Ordinary programs can perform word expansion just like the shell by
Packit 6c4009
calling the library function @code{wordexp}.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Expansion Stages::            What word expansion does to a string.
Packit 6c4009
* Calling Wordexp::             How to call @code{wordexp}.
Packit 6c4009
* Flags for Wordexp::           Options you can enable in @code{wordexp}.
Packit 6c4009
* Wordexp Example::             A sample program that does word expansion.
Packit 6c4009
* Tilde Expansion::             Details of how tilde expansion works.
Packit 6c4009
* Variable Substitution::       Different types of variable substitution.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Expansion Stages
Packit 6c4009
@subsection The Stages of Word Expansion
Packit 6c4009
Packit 6c4009
When word expansion is applied to a sequence of words, it performs the
Packit 6c4009
following transformations in the order shown here:
Packit 6c4009
Packit 6c4009
@enumerate
Packit 6c4009
@item
Packit 6c4009
@cindex tilde expansion
Packit 6c4009
@dfn{Tilde expansion}: Replacement of @samp{~foo} with the name of
Packit 6c4009
the home directory of @samp{foo}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Next, three different transformations are applied in the same step,
Packit 6c4009
from left to right:
Packit 6c4009
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
@cindex variable substitution
Packit 6c4009
@cindex substitution of variables and commands
Packit 6c4009
@dfn{Variable substitution}: Environment variables are substituted for
Packit 6c4009
references such as @samp{$foo}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@cindex command substitution
Packit 6c4009
@dfn{Command substitution}: Constructs such as @w{@samp{`cat foo`}} and
Packit 6c4009
the equivalent @w{@samp{$(cat foo)}} are replaced with the output from
Packit 6c4009
the inner command.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@cindex arithmetic expansion
Packit 6c4009
@dfn{Arithmetic expansion}: Constructs such as @samp{$(($x-1))} are
Packit 6c4009
replaced with the result of the arithmetic computation.
Packit 6c4009
@end itemize
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@cindex field splitting
Packit 6c4009
@dfn{Field splitting}: subdivision of the text into @dfn{words}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@cindex wildcard expansion
Packit 6c4009
@dfn{Wildcard expansion}: The replacement of a construct such as @samp{*.c}
Packit 6c4009
with a list of @samp{.c} file names.  Wildcard expansion applies to an
Packit 6c4009
entire word at a time, and replaces that word with 0 or more file names
Packit 6c4009
that are themselves words.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@cindex quote removal
Packit 6c4009
@cindex removal of quotes
Packit 6c4009
@dfn{Quote removal}: The deletion of string-quotes, now that they have
Packit 6c4009
done their job by inhibiting the above transformations when appropriate.
Packit 6c4009
@end enumerate
Packit 6c4009
Packit 6c4009
For the details of these transformations, and how to write the constructs
Packit 6c4009
that use them, see @w{@cite{The BASH Manual}} (to appear).
Packit 6c4009
Packit 6c4009
@node Calling Wordexp
Packit 6c4009
@subsection Calling @code{wordexp}
Packit 6c4009
Packit 6c4009
All the functions, constants and data types for word expansion are
Packit 6c4009
declared in the header file @file{wordexp.h}.
Packit 6c4009
Packit 6c4009
Word expansion produces a vector of words (strings).  To return this
Packit 6c4009
vector, @code{wordexp} uses a special data type, @code{wordexp_t}, which
Packit 6c4009
is a structure.  You pass @code{wordexp} the address of the structure,
Packit 6c4009
and it fills in the structure's fields to tell you about the results.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {wordexp_t}
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
This data type holds a pointer to a word vector.  More precisely, it
Packit 6c4009
records both the address of the word vector and its size.
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item we_wordc
Packit 6c4009
The number of elements in the vector.
Packit 6c4009
Packit 6c4009
@item we_wordv
Packit 6c4009
The address of the vector.  This field has type @w{@code{char **}}.
Packit 6c4009
Packit 6c4009
@item we_offs
Packit 6c4009
The offset of the first real element of the vector, from its nominal
Packit 6c4009
address in the @code{we_wordv} field.  Unlike the other fields, this
Packit 6c4009
is always an input to @code{wordexp}, rather than an output from it.
Packit 6c4009
Packit 6c4009
If you use a nonzero offset, then that many elements at the beginning of
Packit 6c4009
the vector are left empty.  (The @code{wordexp} function fills them with
Packit 6c4009
null pointers.)
Packit 6c4009
Packit 6c4009
The @code{we_offs} field is meaningful only if you use the
Packit 6c4009
@code{WRDE_DOOFFS} flag.  Otherwise, the offset is always zero
Packit 6c4009
regardless of what is in this field, and the first real element comes at
Packit 6c4009
the beginning of the vector.
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@deftypefun int wordexp (const char *@var{words}, wordexp_t *@var{word-vector-ptr}, int @var{flags})
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtasuconst{:@mtsenv{}} @mtsenv{} @mtascusig{:ALRM} @mtascutimer{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuintl{} @ascuheap{} @asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
Packit 6c4009
@c wordexp @mtasurace:utent @mtasuconst:@mtsenv @mtsenv @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuintl @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c  w_newword ok
Packit 6c4009
@c  wordfree dup @asucorrupt @ascuheap @acucorrupt @acsmem
Packit 6c4009
@c  calloc dup @ascuheap @acsmem
Packit 6c4009
@c  getenv dup @mtsenv
Packit 6c4009
@c  strcpy dup ok
Packit 6c4009
@c  parse_backslash @ascuheap @acsmem
Packit 6c4009
@c   w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c  parse_dollars @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c   w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c   parse_arith @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c    w_newword dup ok
Packit 6c4009
@c    parse_dollars dup @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c    parse_backtick dup @ascuplugin @ascuheap @aculock @acsfd @acsmem
Packit 6c4009
@c    parse_qtd_backslash dup @ascuheap @acsmem
Packit 6c4009
@c    eval_expr @mtslocale
Packit 6c4009
@c     eval_expr_multidiv @mtslocale
Packit 6c4009
@c      eval_expr_val @mtslocale
Packit 6c4009
@c       isspace dup @mtslocale
Packit 6c4009
@c       eval_expr dup @mtslocale
Packit 6c4009
@c      isspace dup @mtslocale
Packit 6c4009
@c     isspace dup @mtslocale
Packit 6c4009
@c    free dup @ascuheap @acsmem
Packit 6c4009
@c    w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c    w_addstr dup @ascuheap @acsmem
Packit 6c4009
@c    itoa_word dup ok
Packit 6c4009
@c   parse_comm @ascuplugin @ascuheap @aculock @acsfd @acsmem
Packit 6c4009
@c    w_newword dup ok
Packit 6c4009
@c    pthread_setcancelstate @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c      (disable cancellation around exec_comm; it may do_cancel the
Packit 6c4009
@c       second time, if async cancel is enabled)
Packit 6c4009
@c     THREAD_ATOMIC_CMPXCHG_VAL dup ok
Packit 6c4009
@c     CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS dup ok
Packit 6c4009
@c     do_cancel @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c      THREAD_ATOMIC_BIT_SET dup ok
Packit 6c4009
@c      pthread_unwind @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c       Unwind_ForcedUnwind if available @ascuplugin @ascuheap @acsmem
Packit 6c4009
@c       libc_unwind_longjmp otherwise
Packit 6c4009
@c       cleanups
Packit 6c4009
@c    exec_comm @ascuplugin @ascuheap @aculock @acsfd @acsmem
Packit 6c4009
@c     pipe2 dup ok
Packit 6c4009
@c     pipe dup ok
Packit 6c4009
@c     fork dup @ascuplugin @aculock
Packit 6c4009
@c     close dup @acsfd
Packit 6c4009
@c     on child: exec_comm_child -> exec or abort
Packit 6c4009
@c     waitpid dup ok
Packit 6c4009
@c     read dup ok
Packit 6c4009
@c     w_addmem dup @ascuheap @acsmem
Packit 6c4009
@c     strchr dup ok
Packit 6c4009
@c     w_addword dup @ascuheap @acsmem
Packit 6c4009
@c     w_newword dup ok
Packit 6c4009
@c     w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c     free dup @ascuheap @acsmem
Packit 6c4009
@c     kill dup ok
Packit 6c4009
@c    free dup @ascuheap @acsmem
Packit 6c4009
@c   parse_param @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c     reads from __libc_argc and __libc_argv without guards
Packit 6c4009
@c    w_newword dup ok
Packit 6c4009
@c    isalpha dup @mtslocale^^
Packit 6c4009
@c    w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c    isalnum dup @mtslocale^^
Packit 6c4009
@c    isdigit dup @mtslocale^^
Packit 6c4009
@c    strchr dup ok
Packit 6c4009
@c    itoa_word dup ok
Packit 6c4009
@c    atoi dup @mtslocale
Packit 6c4009
@c    getpid dup ok
Packit 6c4009
@c    w_addstr dup @ascuheap @acsmem
Packit 6c4009
@c    free dup @ascuheap @acsmem
Packit 6c4009
@c    strlen dup ok
Packit 6c4009
@c    malloc dup @ascuheap @acsmem
Packit 6c4009
@c    stpcpy dup ok
Packit 6c4009
@c    w_addword dup @ascuheap @acsmem
Packit 6c4009
@c    strdup dup @ascuheap @acsmem
Packit 6c4009
@c    getenv dup @mtsenv
Packit 6c4009
@c    parse_dollars dup @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c    parse_tilde dup @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c    fnmatch dup @mtsenv @mtslocale @ascuheap @acsmem
Packit 6c4009
@c    mempcpy dup ok
Packit 6c4009
@c    _ dup @ascuintl
Packit 6c4009
@c    fxprintf dup @aculock
Packit 6c4009
@c    setenv dup @mtasuconst:@mtsenv @ascuheap @asulock @acucorrupt @aculock @acsmem
Packit 6c4009
@c    strspn dup ok
Packit 6c4009
@c    strcspn dup ok
Packit 6c4009
@c  parse_backtick @ascuplugin @ascuheap @aculock @acsfd @acsmem
Packit 6c4009
@c   w_newword dup ok
Packit 6c4009
@c   exec_comm dup @ascuplugin @ascuheap @aculock @acsfd @acsmem
Packit 6c4009
@c   free dup @ascuheap @acsmem
Packit 6c4009
@c   parse_qtd_backslash dup @ascuheap @acsmem
Packit 6c4009
@c   parse_backslash dup @ascuheap @acsmem
Packit 6c4009
@c   w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c  parse_dquote @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c   parse_dollars dup @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c   parse_backtick dup @ascuplugin @ascuheap @aculock @acsfd @acsmem
Packit 6c4009
@c   parse_qtd_backslash dup @ascuheap @acsmem
Packit 6c4009
@c   w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c  w_addword dup @ascuheap @acsmem
Packit 6c4009
@c   strdup dup @ascuheap @acsmem
Packit 6c4009
@c   realloc dup @ascuheap @acsmem
Packit 6c4009
@c   free dup @ascuheap @acsmem
Packit 6c4009
@c  parse_squote dup @ascuheap @acsmem
Packit 6c4009
@c   w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c  parse_tilde @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c   strchr dup ok
Packit 6c4009
@c   w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c   getenv dup @mtsenv
Packit 6c4009
@c   w_addstr dup @ascuheap @acsmem
Packit 6c4009
@c    strlen dup ok
Packit 6c4009
@c    w_addmem dup @ascuheap @acsmem
Packit 6c4009
@c     realloc dup @ascuheap @acsmem
Packit 6c4009
@c     free dup @ascuheap @acsmem
Packit 6c4009
@c     mempcpy dup ok
Packit 6c4009
@c   getuid dup ok
Packit 6c4009
@c   getpwuid_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c   getpwnam_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c  parse_glob @mtasurace:utent @mtasuconst:@mtsenv @mtsenv @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c   strchr dup ok
Packit 6c4009
@c   parse_dollars dup @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c   parse_qtd_backslash @ascuheap @acsmem
Packit 6c4009
@c    w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c   parse_backslash dup @ascuheap @acsmem
Packit 6c4009
@c   w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c   w_addword dup @ascuheap @acsmem
Packit 6c4009
@c   w_newword dup ok
Packit 6c4009
@c   do_parse_glob @mtasurace:utent @mtsenv @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @aculock @acsfd @acsmem
Packit 6c4009
@c    glob dup @mtasurace:utent @mtsenv @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @aculock @acsfd @acsmem [auto glob_t avoids @asucorrupt @acucorrupt]
Packit 6c4009
@c    w_addstr dup @ascuheap @acsmem
Packit 6c4009
@c    w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c    globfree dup @ascuheap @acsmem [auto glob_t avoids @asucorrupt @acucorrupt]
Packit 6c4009
@c    free dup @ascuheap @acsmem
Packit 6c4009
@c    w_newword dup ok
Packit 6c4009
@c    strdup dup @ascuheap @acsmem
Packit 6c4009
@c    w_addword dup @ascuheap @acsmem
Packit 6c4009
@c   wordfree dup @asucorrupt @ascuheap @acucorrupt @acsmem
Packit 6c4009
@c  strchr dup ok
Packit 6c4009
@c  w_addchar dup @ascuheap @acsmem
Packit 6c4009
@c   realloc dup @ascuheap @acsmem
Packit 6c4009
@c   free dup @ascuheap @acsmem
Packit 6c4009
@c  free dup @ascuheap @acsmem
Packit 6c4009
Perform word expansion on the string @var{words}, putting the result in
Packit 6c4009
a newly allocated vector, and store the size and address of this vector
Packit 6c4009
into @code{*@var{word-vector-ptr}}.  The argument @var{flags} is a
Packit 6c4009
combination of bit flags; see @ref{Flags for Wordexp}, for details of
Packit 6c4009
the flags.
Packit 6c4009
Packit 6c4009
You shouldn't use any of the characters @samp{|&;<>} in the string
Packit 6c4009
@var{words} unless they are quoted; likewise for newline.  If you use
Packit 6c4009
these characters unquoted, you will get the @code{WRDE_BADCHAR} error
Packit 6c4009
code.  Don't use parentheses or braces unless they are quoted or part of
Packit 6c4009
a word expansion construct.  If you use quotation characters @samp{'"`},
Packit 6c4009
they should come in pairs that balance.
Packit 6c4009
Packit 6c4009
The results of word expansion are a sequence of words.  The function
Packit 6c4009
@code{wordexp} allocates a string for each resulting word, then
Packit 6c4009
allocates a vector of type @code{char **} to store the addresses of
Packit 6c4009
these strings.  The last element of the vector is a null pointer.
Packit 6c4009
This vector is called the @dfn{word vector}.
Packit 6c4009
Packit 6c4009
To return this vector, @code{wordexp} stores both its address and its
Packit 6c4009
length (number of elements, not counting the terminating null pointer)
Packit 6c4009
into @code{*@var{word-vector-ptr}}.
Packit 6c4009
Packit 6c4009
If @code{wordexp} succeeds, it returns 0.  Otherwise, it returns one
Packit 6c4009
of these error codes:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item WRDE_BADCHAR
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
The input string @var{words} contains an unquoted invalid character such
Packit 6c4009
as @samp{|}.
Packit 6c4009
Packit 6c4009
@item WRDE_BADVAL
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
The input string refers to an undefined shell variable, and you used the flag
Packit 6c4009
@code{WRDE_UNDEF} to forbid such references.
Packit 6c4009
Packit 6c4009
@item WRDE_CMDSUB
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
The input string uses command substitution, and you used the flag
Packit 6c4009
@code{WRDE_NOCMD} to forbid command substitution.
Packit 6c4009
Packit 6c4009
@item WRDE_NOSPACE
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
It was impossible to allocate memory to hold the result.  In this case,
Packit 6c4009
@code{wordexp} can store part of the results---as much as it could
Packit 6c4009
allocate room for.
Packit 6c4009
Packit 6c4009
@item WRDE_SYNTAX
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
There was a syntax error in the input string.  For example, an unmatched
Packit 6c4009
quoting character is a syntax error.  This error code is also used to
Packit 6c4009
signal division by zero and overflow in arithmetic expansion.
Packit 6c4009
@end vtable
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void wordfree (wordexp_t *@var{word-vector-ptr})
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
Packit 6c4009
@c wordfree dup @asucorrupt @ascuheap @acucorrupt @acsmem
Packit 6c4009
@c  free dup @ascuheap @acsmem
Packit 6c4009
Free the storage used for the word-strings and vector that
Packit 6c4009
@code{*@var{word-vector-ptr}} points to.  This does not free the
Packit 6c4009
structure @code{*@var{word-vector-ptr}} itself---only the other
Packit 6c4009
data it points to.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@node Flags for Wordexp
Packit 6c4009
@subsection Flags for Word Expansion
Packit 6c4009
Packit 6c4009
This section describes the flags that you can specify in the
Packit 6c4009
@var{flags} argument to @code{wordexp}.  Choose the flags you want,
Packit 6c4009
and combine them with the C operator @code{|}.
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item WRDE_APPEND
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
Append the words from this expansion to the vector of words produced by
Packit 6c4009
previous calls to @code{wordexp}.  This way you can effectively expand
Packit 6c4009
several words as if they were concatenated with spaces between them.
Packit 6c4009
Packit 6c4009
In order for appending to work, you must not modify the contents of the
Packit 6c4009
word vector structure between calls to @code{wordexp}.  And, if you set
Packit 6c4009
@code{WRDE_DOOFFS} in the first call to @code{wordexp}, you must also
Packit 6c4009
set it when you append to the results.
Packit 6c4009
Packit 6c4009
@item WRDE_DOOFFS
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
Leave blank slots at the beginning of the vector of words.
Packit 6c4009
The @code{we_offs} field says how many slots to leave.
Packit 6c4009
The blank slots contain null pointers.
Packit 6c4009
Packit 6c4009
@item WRDE_NOCMD
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
Don't do command substitution; if the input requests command substitution,
Packit 6c4009
report an error.
Packit 6c4009
Packit 6c4009
@item WRDE_REUSE
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
Reuse a word vector made by a previous call to @code{wordexp}.
Packit 6c4009
Instead of allocating a new vector of words, this call to @code{wordexp}
Packit 6c4009
will use the vector that already exists (making it larger if necessary).
Packit 6c4009
Packit 6c4009
Note that the vector may move, so it is not safe to save an old pointer
Packit 6c4009
and use it again after calling @code{wordexp}.  You must fetch
Packit 6c4009
@code{we_pathv} anew after each call.
Packit 6c4009
Packit 6c4009
@item WRDE_SHOWERR
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
Do show any error messages printed by commands run by command substitution.
Packit 6c4009
More precisely, allow these commands to inherit the standard error output
Packit 6c4009
stream of the current process.  By default, @code{wordexp} gives these
Packit 6c4009
commands a standard error stream that discards all output.
Packit 6c4009
Packit 6c4009
@item WRDE_UNDEF
Packit 6c4009
@standards{POSIX.2, wordexp.h}
Packit 6c4009
If the input refers to a shell variable that is not defined, report an
Packit 6c4009
error.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node Wordexp Example
Packit 6c4009
@subsection @code{wordexp} Example
Packit 6c4009
Packit 6c4009
Here is an example of using @code{wordexp} to expand several strings
Packit 6c4009
and use the results to run a shell command.  It also shows the use of
Packit 6c4009
@code{WRDE_APPEND} to concatenate the expansions and of @code{wordfree}
Packit 6c4009
to free the space allocated by @code{wordexp}.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
int
Packit 6c4009
expand_and_execute (const char *program, const char **options)
Packit 6c4009
@{
Packit 6c4009
  wordexp_t result;
Packit 6c4009
  pid_t pid
Packit 6c4009
  int status, i;
Packit 6c4009
Packit 6c4009
  /* @r{Expand the string for the program to run.}  */
Packit 6c4009
  switch (wordexp (program, &result, 0))
Packit 6c4009
    @{
Packit 6c4009
    case 0:			/* @r{Successful}.  */
Packit 6c4009
      break;
Packit 6c4009
    case WRDE_NOSPACE:
Packit 6c4009
      /* @r{If the error was @code{WRDE_NOSPACE},}
Packit 6c4009
         @r{then perhaps part of the result was allocated.}  */
Packit 6c4009
      wordfree (&result);
Packit 6c4009
    default:                    /* @r{Some other error.}  */
Packit 6c4009
      return -1;
Packit 6c4009
    @}
Packit 6c4009
Packit 6c4009
  /* @r{Expand the strings specified for the arguments.}  */
Packit 6c4009
  for (i = 0; options[i] != NULL; i++)
Packit 6c4009
    @{
Packit 6c4009
      if (wordexp (options[i], &result, WRDE_APPEND))
Packit 6c4009
        @{
Packit 6c4009
          wordfree (&result);
Packit 6c4009
          return -1;
Packit 6c4009
        @}
Packit 6c4009
    @}
Packit 6c4009
Packit 6c4009
  pid = fork ();
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    @{
Packit 6c4009
      /* @r{This is the child process.  Execute the command.} */
Packit 6c4009
      execv (result.we_wordv[0], result.we_wordv);
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    @}
Packit 6c4009
  else if (pid < 0)
Packit 6c4009
    /* @r{The fork failed.  Report failure.}  */
Packit 6c4009
    status = -1;
Packit 6c4009
  else
Packit 6c4009
    /* @r{This is the parent process.  Wait for the child to complete.}  */
Packit 6c4009
    if (waitpid (pid, &status, 0) != pid)
Packit 6c4009
      status = -1;
Packit 6c4009
Packit 6c4009
  wordfree (&result);
Packit 6c4009
  return status;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node Tilde Expansion
Packit 6c4009
@subsection Details of Tilde Expansion
Packit 6c4009
Packit 6c4009
It's a standard part of shell syntax that you can use @samp{~} at the
Packit 6c4009
beginning of a file name to stand for your own home directory.  You
Packit 6c4009
can use @samp{~@var{user}} to stand for @var{user}'s home directory.
Packit 6c4009
Packit 6c4009
@dfn{Tilde expansion} is the process of converting these abbreviations
Packit 6c4009
to the directory names that they stand for.
Packit 6c4009
Packit 6c4009
Tilde expansion applies to the @samp{~} plus all following characters up
Packit 6c4009
to whitespace or a slash.  It takes place only at the beginning of a
Packit 6c4009
word, and only if none of the characters to be transformed is quoted in
Packit 6c4009
any way.
Packit 6c4009
Packit 6c4009
Plain @samp{~} uses the value of the environment variable @code{HOME}
Packit 6c4009
as the proper home directory name.  @samp{~} followed by a user name
Packit 6c4009
uses @code{getpwname} to look up that user in the user database, and
Packit 6c4009
uses whatever directory is recorded there.  Thus, @samp{~} followed
Packit 6c4009
by your own name can give different results from plain @samp{~}, if
Packit 6c4009
the value of @code{HOME} is not really your home directory.
Packit 6c4009
Packit 6c4009
@node Variable Substitution
Packit 6c4009
@subsection Details of Variable Substitution
Packit 6c4009
Packit 6c4009
Part of ordinary shell syntax is the use of @samp{$@var{variable}} to
Packit 6c4009
substitute the value of a shell variable into a command.  This is called
Packit 6c4009
@dfn{variable substitution}, and it is one part of doing word expansion.
Packit 6c4009
Packit 6c4009
There are two basic ways you can write a variable reference for
Packit 6c4009
substitution:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item $@{@var{variable}@}
Packit 6c4009
If you write braces around the variable name, then it is completely
Packit 6c4009
unambiguous where the variable name ends.  You can concatenate
Packit 6c4009
additional letters onto the end of the variable value by writing them
Packit 6c4009
immediately after the close brace.  For example, @samp{$@{foo@}s}
Packit 6c4009
expands into @samp{tractors}.
Packit 6c4009
Packit 6c4009
@item $@var{variable}
Packit 6c4009
If you do not put braces around the variable name, then the variable
Packit 6c4009
name consists of all the alphanumeric characters and underscores that
Packit 6c4009
follow the @samp{$}.  The next punctuation character ends the variable
Packit 6c4009
name.  Thus, @samp{$foo-bar} refers to the variable @code{foo} and expands
Packit 6c4009
into @samp{tractor-bar}.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
When you use braces, you can also use various constructs to modify the
Packit 6c4009
value that is substituted, or test it in various ways.
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item $@{@var{variable}:-@var{default}@}
Packit 6c4009
Substitute the value of @var{variable}, but if that is empty or
Packit 6c4009
undefined, use @var{default} instead.
Packit 6c4009
Packit 6c4009
@item $@{@var{variable}:=@var{default}@}
Packit 6c4009
Substitute the value of @var{variable}, but if that is empty or
Packit 6c4009
undefined, use @var{default} instead and set the variable to
Packit 6c4009
@var{default}.
Packit 6c4009
Packit 6c4009
@item $@{@var{variable}:?@var{message}@}
Packit 6c4009
If @var{variable} is defined and not empty, substitute its value.
Packit 6c4009
Packit 6c4009
Otherwise, print @var{message} as an error message on the standard error
Packit 6c4009
stream, and consider word expansion a failure.
Packit 6c4009
Packit 6c4009
@c ??? How does wordexp report such an error?
Packit 6c4009
@c WRDE_BADVAL is returned.
Packit 6c4009
Packit 6c4009
@item $@{@var{variable}:+@var{replacement}@}
Packit 6c4009
Substitute @var{replacement}, but only if @var{variable} is defined and
Packit 6c4009
nonempty.  Otherwise, substitute nothing for this construct.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item $@{#@var{variable}@}
Packit 6c4009
Substitute a numeral which expresses in base ten the number of
Packit 6c4009
characters in the value of @var{variable}.  @samp{$@{#foo@}} stands for
Packit 6c4009
@samp{7}, because @samp{tractor} is seven characters.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
These variants of variable substitution let you remove part of the
Packit 6c4009
variable's value before substituting it.  The @var{prefix} and
Packit 6c4009
@var{suffix} are not mere strings; they are wildcard patterns, just
Packit 6c4009
like the patterns that you use to match multiple file names.  But
Packit 6c4009
in this context, they match against parts of the variable value
Packit 6c4009
rather than against file names.
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item $@{@var{variable}%%@var{suffix}@}
Packit 6c4009
Substitute the value of @var{variable}, but first discard from that
Packit 6c4009
variable any portion at the end that matches the pattern @var{suffix}.
Packit 6c4009
Packit 6c4009
If there is more than one alternative for how to match against
Packit 6c4009
@var{suffix}, this construct uses the longest possible match.
Packit 6c4009
Packit 6c4009
Thus, @samp{$@{foo%%r*@}} substitutes @samp{t}, because the largest
Packit 6c4009
match for @samp{r*} at the end of @samp{tractor} is @samp{ractor}.
Packit 6c4009
Packit 6c4009
@item $@{@var{variable}%@var{suffix}@}
Packit 6c4009
Substitute the value of @var{variable}, but first discard from that
Packit 6c4009
variable any portion at the end that matches the pattern @var{suffix}.
Packit 6c4009
Packit 6c4009
If there is more than one alternative for how to match against
Packit 6c4009
@var{suffix}, this construct uses the shortest possible alternative.
Packit 6c4009
Packit 6c4009
Thus, @samp{$@{foo%r*@}} substitutes @samp{tracto}, because the shortest
Packit 6c4009
match for @samp{r*} at the end of @samp{tractor} is just @samp{r}.
Packit 6c4009
Packit 6c4009
@item $@{@var{variable}##@var{prefix}@}
Packit 6c4009
Substitute the value of @var{variable}, but first discard from that
Packit 6c4009
variable any portion at the beginning that matches the pattern @var{prefix}.
Packit 6c4009
Packit 6c4009
If there is more than one alternative for how to match against
Packit 6c4009
@var{prefix}, this construct uses the longest possible match.
Packit 6c4009
Packit 6c4009
Thus, @samp{$@{foo##*t@}} substitutes @samp{or}, because the largest
Packit 6c4009
match for @samp{*t} at the beginning of @samp{tractor} is @samp{tract}.
Packit 6c4009
Packit 6c4009
@item $@{@var{variable}#@var{prefix}@}
Packit 6c4009
Substitute the value of @var{variable}, but first discard from that
Packit 6c4009
variable any portion at the beginning that matches the pattern @var{prefix}.
Packit 6c4009
Packit 6c4009
If there is more than one alternative for how to match against
Packit 6c4009
@var{prefix}, this construct uses the shortest possible alternative.
Packit 6c4009
Packit 6c4009
Thus, @samp{$@{foo#*t@}} substitutes @samp{ractor}, because the shortest
Packit 6c4009
match for @samp{*t} at the beginning of @samp{tractor} is just @samp{t}.
Packit 6c4009
Packit 6c4009
@end table