1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
23 /* The contents of this header file was first standardized in X/Open
24 System Interface and Headers Issue 2, originally coming from SysV.
25 In issue 4, version 2, it is marked as TO BE WITDRAWN.
27 This code shouldn't be used in any newly written code. It is
28 included only for compatibility reasons. Use the POSIX definition
29 in <regex.h> for portable applications and a reasonable interface. */
36 /* The implementation provided here emulates the needed functionality
37 by mapping to the POSIX regular expression matcher. The interface
38 for the here included function is weird (this really is a harmless
41 The user has to provide five macros before this header file can be
44 GETC() Return the value of the next character in the regular
45 expression pattern. Successive calls should return
46 successive characters.
48 PEEKC() Return the value of the next character in the regular
49 expression pattern. Immediately successive calls to
50 PEEKC() should return the same character which should
51 also be the next character returned by GETC().
53 UNGETC(c) Cause `c' to be returned by the next call to GETC() and
56 RETURN(ptr) Used for normal exit of the `compile' function. `ptr'
57 is a pointer to the character after the last character of
58 the compiled regular expression.
60 ERROR(val) Used for abnormal return from `compile'. `val' is the
61 error number. The error codes are:
62 11 Range endpoint too large.
64 25 \digit out of range.
65 36 Illegal or missing delimiter.
66 41 No remembered search string.
69 44 More tan two numbers given in \{ \}.
70 45 } expected after \.
71 46 First number exceeds second in \{ \}.
73 50 Regular expression overflow.
79 /* Interface variables. They contain the results of the successful
80 calls to `setp' and `advance'. */
84 /* The use of this variable in the `advance' function is not
89 #ifndef __DO_NOT_DEFINE_COMPILE
90 /* Get and compile the user supplied pattern up to end of line or
91 string or until EOF is seen, whatever happens first. The result is
92 placed in the buffer starting at EXPBUG and delimited by ENDBUF.
94 This function cannot be defined in the libc itself since it depends
97 compile (char *__instring
, char *__expbuf
, __const
char *__endbuf
, int __eof
)
99 char *__input_buffer
= NULL
;
100 size_t __input_size
= 0;
101 size_t __current_size
;
105 /* Align the expression buffer according to the needs for an object
106 of type `regex_t'. Then check for minimum size of the buffer for
107 the compiled regular expression. */
109 #if defined __GNUC__ && __GNUC__ >= 2
110 const size_t __req
= __alignof__ (regex_t
*);
112 /* How shall we find out? We simply guess it and can change it is
113 this really proofs to be wrong. */
114 const size_t __req
= 8;
117 __expbuf
-= (__expbuf
- ((char *) 0)) % __req
;
118 if (__endbuf
< __expbuf
+ sizeof (regex_t
))
122 __expr_ptr
= (regex_t
*) __expbuf
;
123 /* The remaining space in the buffer can be used for the compiled
125 __expr_ptr
->buffer
= __expbuf
+ sizeof (regex_t
);
126 __expr_ptr
->allocated
= __endbuf
- (char *) __expr_ptr
->buffer
;
128 while ((__ch
= (GETC ())) != __eof
)
130 if (__ch
== '\0' || __ch
== 'n')
136 if (__current_size
+ 1 >= __input_size
)
138 size_t __new_size
= __input_size
? 2 * __input_size
: 128;
139 char *__new_room
= __alloca (__new_size
);
140 /* See whether we can use the old buffer. */
141 if (__new_room
+ __new_size
== __input_buffer
)
143 __input_size
+= __new_size
;
144 __input_buffer
= memcpy (__new_room
, __input_buffer
,
147 else if (__input_buffer
+ __input_size
== __new_room
)
148 __input_size
+= __new_size
;
151 __input_size
= __new_size
;
152 __input_buffer
= memcpy (__new_room
, __input_buffer
,
156 __input_buffer
[__current_size
++] = __ch
;
158 __input_buffer
[__current_size
++] = '\0';
160 /* Now compile the pattern. */
161 __error
= regcomp (__expr_ptr
, __input_buffer
, REG_NEWLINE
);
163 /* Oh well, we have to translate POSIX error codes. */
174 /* There is no matching error code. */
193 /* Everything is ok. */
194 RETURN ((char *) (__expr_ptr
->buffer
+ __expr
->used
));
199 /* Find the next match in STRING. The compiled regular expression is
200 found in the buffer starting at EXPBUF. `loc1' will return the
201 first character matched and `loc2' points to the next unmatched
203 extern int step
__P ((__const
char *__string
, __const
char *__expbuf
));
205 /* Match the beginning of STRING with the compiled regular expression
206 in EXPBUF. If the match is successful `loc2' will contain the
207 position of the first unmatched character. */
208 extern int advance
__P ((__const
char *__string
, __const
char *__expbuf
));
213 #endif /* regexp.h */