prepare for 1.0.1
[uclibc-ng.git] / include / regexp.h
blob17879aed113016f0facf35e44815cd9fea6112c1
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2004, 2008
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef _REGEXP_H
21 #define _REGEXP_H 1
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, and it has
26 been withdrawn in SUSv3.
28 This code shouldn't be used in any newly written code. It is
29 included only for compatibility reasons. Use the POSIX definition
30 in <regex.h> for portable applications and a reasonable interface. */
32 #include <features.h>
33 #include <alloca.h>
34 #include <regex.h>
35 #include <stdlib.h>
36 #include <string.h>
38 /* The implementation provided here emulates the needed functionality
39 by mapping to the POSIX regular expression matcher. The interface
40 for the here included function is weird (this really is a harmless
41 word).
43 The user has to provide six macros before this header file can be
44 included:
46 INIT Declarations vor variables which can be used by the
47 other macros.
49 GETC() Return the value of the next character in the regular
50 expression pattern. Successive calls should return
51 successive characters.
53 PEEKC() Return the value of the next character in the regular
54 expression pattern. Immediately successive calls to
55 PEEKC() should return the same character which should
56 also be the next character returned by GETC().
58 UNGETC(c) Cause `c' to be returned by the next call to GETC() and
59 PEEKC().
61 RETURN(ptr) Used for normal exit of the `compile' function. `ptr'
62 is a pointer to the character after the last character of
63 the compiled regular expression.
65 ERROR(val) Used for abnormal return from `compile'. `val' is the
66 error number. The error codes are:
67 11 Range endpoint too large.
68 16 Bad number.
69 25 \digit out of range.
70 36 Illegal or missing delimiter.
71 41 No remembered search string.
72 42 \( \) imbalance.
73 43 Too many \(.
74 44 More tan two numbers given in \{ \}.
75 45 } expected after \.
76 46 First number exceeds second in \{ \}.
77 49 [ ] imbalance.
78 50 Regular expression overflow.
82 __BEGIN_DECLS
84 #if 0
85 /* Interface variables. They contain the results of the successful
86 calls to `setp' and `advance'. */
87 extern char *loc1;
88 extern char *loc2;
90 /* The use of this variable in the `advance' function is not
91 supported. */
92 extern char *locs;
93 #endif
96 #ifndef __DO_NOT_DEFINE_COMPILE
97 /* Get and compile the user supplied pattern up to end of line or
98 string or until EOF is seen, whatever happens first. The result is
99 placed in the buffer starting at EXPBUF and delimited by ENDBUF.
101 This function cannot be defined in the libc itself since it depends
102 on the macros. */
103 char *
104 compile (char *__restrict instring, char *__restrict expbuf,
105 const char *__restrict endbuf, int eof)
107 char *__input_buffer = NULL;
108 size_t __input_size = 0;
109 size_t __current_size = 0;
110 int __ch;
111 int __error;
112 INIT
114 /* Align the expression buffer according to the needs for an object
115 of type `regex_t'. Then check for minimum size of the buffer for
116 the compiled regular expression. */
117 regex_t *__expr_ptr;
118 # if defined __GNUC__ && __GNUC__ >= 2
119 const size_t __req = __alignof__ (regex_t *);
120 # else
121 /* How shall we find out? We simply guess it and can change it is
122 this really proofs to be wrong. */
123 const size_t __req = 8;
124 # endif
125 expbuf += __req;
126 expbuf -= (expbuf - ((char *) 0)) % __req;
127 if (endbuf < expbuf + sizeof (regex_t))
129 ERROR (50);
131 __expr_ptr = (regex_t *) expbuf;
132 /* The remaining space in the buffer can be used for the compiled
133 pattern. */
134 __expr_ptr->__REPB_PREFIX (buffer) = expbuf + sizeof (regex_t);
135 __expr_ptr->__REPB_PREFIX (allocated)
136 = endbuf - (char *) __expr_ptr->__REPB_PREFIX (buffer);
138 while ((__ch = (GETC ())) != eof)
140 if (__ch == '\0' || __ch == '\n')
142 UNGETC (__ch);
143 break;
146 if (__current_size + 1 >= __input_size)
148 size_t __new_size = __input_size ? 2 * __input_size : 128;
149 char *__new_room = (char *) alloca (__new_size);
150 /* See whether we can use the old buffer. */
151 if (__new_room + __new_size == __input_buffer)
153 __input_size += __new_size;
154 __input_buffer = (char *) memcpy (__new_room, __input_buffer,
155 __current_size);
157 else if (__input_buffer + __input_size == __new_room)
158 __input_size += __new_size;
159 else
161 __input_size = __new_size;
162 __input_buffer = (char *) memcpy (__new_room, __input_buffer,
163 __current_size);
166 __input_buffer[__current_size++] = __ch;
168 if (__current_size)
169 __input_buffer[__current_size++] = '\0';
170 else
171 __input_buffer = "";
173 /* Now compile the pattern. */
174 __error = regcomp (__expr_ptr, __input_buffer, REG_NEWLINE);
175 if (__error != 0)
176 /* Oh well, we have to translate POSIX error codes. */
177 switch (__error)
179 case REG_BADPAT:
180 case REG_ECOLLATE:
181 case REG_ECTYPE:
182 case REG_EESCAPE:
183 case REG_BADRPT:
184 case REG_EEND:
185 case REG_ERPAREN:
186 default:
187 /* There is no matching error code. */
188 RETURN (36);
189 case REG_ESUBREG:
190 RETURN (25);
191 case REG_EBRACK:
192 RETURN (49);
193 case REG_EPAREN:
194 RETURN (42);
195 case REG_EBRACE:
196 RETURN (44);
197 case REG_BADBR:
198 RETURN (46);
199 case REG_ERANGE:
200 RETURN (11);
201 case REG_ESPACE:
202 case REG_ESIZE:
203 ERROR (50);
206 /* Everything is ok. */
207 RETURN ((char *) (__expr_ptr->__REPB_PREFIX (buffer)
208 + __expr_ptr->__REPB_PREFIX (used)));
210 #endif
213 #if 0
214 /* Find the next match in STRING. The compiled regular expression is
215 found in the buffer starting at EXPBUF. `loc1' will return the
216 first character matched and `loc2' points to the next unmatched
217 character. */
218 extern int step (const char *__restrict __string,
219 const char *__restrict __expbuf) __THROW;
221 /* Match the beginning of STRING with the compiled regular expression
222 in EXPBUF. If the match is successful `loc2' will contain the
223 position of the first unmatched character. */
224 extern int advance (const char *__restrict __string,
225 const char *__restrict __expbuf) __THROW;
226 #endif
229 __END_DECLS
231 #endif /* regexp.h */