Update copyright notices with scripts/update-copyrights
[glibc.git] / misc / regexp.h
blob2af2bdf2c88a773db9ed2c940bd966724ae72777
1 /* Copyright (C) 1996-2014 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _REGEXP_H
20 #define _REGEXP_H 1
22 /* The contents of this header file was first standardized in X/Open
23 System Interface and Headers Issue 2, originally coming from SysV.
24 In issue 4, version 2, it is marked as TO BE WITDRAWN, and it has
25 been withdrawn in SUSv3.
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. */
31 #include <features.h>
32 #include <alloca.h>
33 #include <regex.h>
34 #include <stdlib.h>
35 #include <string.h>
37 /* The implementation provided here emulates the needed functionality
38 by mapping to the POSIX regular expression matcher. The interface
39 for the here included function is weird (this really is a harmless
40 word).
42 The user has to provide six macros before this header file can be
43 included:
45 INIT Declarations vor variables which can be used by the
46 other macros.
48 GETC() Return the value of the next character in the regular
49 expression pattern. Successive calls should return
50 successive characters.
52 PEEKC() Return the value of the next character in the regular
53 expression pattern. Immediately successive calls to
54 PEEKC() should return the same character which should
55 also be the next character returned by GETC().
57 UNGETC(c) Cause `c' to be returned by the next call to GETC() and
58 PEEKC().
60 RETURN(ptr) Used for normal exit of the `compile' function. `ptr'
61 is a pointer to the character after the last character of
62 the compiled regular expression.
64 ERROR(val) Used for abnormal return from `compile'. `val' is the
65 error number. The error codes are:
66 11 Range endpoint too large.
67 16 Bad number.
68 25 \digit out of range.
69 36 Illegal or missing delimiter.
70 41 No remembered search string.
71 42 \( \) imbalance.
72 43 Too many \(.
73 44 More tan two numbers given in \{ \}.
74 45 } expected after \.
75 46 First number exceeds second in \{ \}.
76 49 [ ] imbalance.
77 50 Regular expression overflow.
81 __BEGIN_DECLS
83 /* Interface variables. They contain the results of the successful
84 calls to `setp' and `advance'. */
85 extern char *loc1;
86 extern char *loc2;
88 /* The use of this variable in the `advance' function is not
89 supported. */
90 extern char *locs;
93 #ifndef __DO_NOT_DEFINE_COMPILE
94 /* Get and compile the user supplied pattern up to end of line or
95 string or until EOF is seen, whatever happens first. The result is
96 placed in the buffer starting at EXPBUF and delimited by ENDBUF.
98 This function cannot be defined in the libc itself since it depends
99 on the macros. */
100 char *
101 compile (char *__restrict instring, char *__restrict expbuf,
102 const char *__restrict endbuf, int eof)
104 char *__input_buffer = NULL;
105 size_t __input_size = 0;
106 size_t __current_size = 0;
107 int __ch;
108 int __error;
109 INIT
111 /* Align the expression buffer according to the needs for an object
112 of type `regex_t'. Then check for minimum size of the buffer for
113 the compiled regular expression. */
114 regex_t *__expr_ptr;
115 # if defined __GNUC__ && __GNUC__ >= 2
116 const size_t __req = __alignof__ (regex_t *);
117 # else
118 /* How shall we find out? We simply guess it and can change it is
119 this really proofs to be wrong. */
120 const size_t __req = 8;
121 # endif
122 expbuf += __req;
123 expbuf -= (expbuf - ((char *) 0)) % __req;
124 if (endbuf < expbuf + sizeof (regex_t))
126 ERROR (50);
128 __expr_ptr = (regex_t *) expbuf;
129 /* The remaining space in the buffer can be used for the compiled
130 pattern. */
131 __expr_ptr->__REPB_PREFIX (buffer) = expbuf + sizeof (regex_t);
132 __expr_ptr->__REPB_PREFIX (allocated)
133 = endbuf - (char *) __expr_ptr->__REPB_PREFIX (buffer);
135 while ((__ch = (GETC ())) != eof)
137 if (__ch == '\0' || __ch == '\n')
139 UNGETC (__ch);
140 break;
143 if (__current_size + 1 >= __input_size)
145 size_t __new_size = __input_size ? 2 * __input_size : 128;
146 char *__new_room = (char *) alloca (__new_size);
147 /* See whether we can use the old buffer. */
148 if (__new_room + __new_size == __input_buffer)
150 __input_size += __new_size;
151 __input_buffer = (char *) memcpy (__new_room, __input_buffer,
152 __current_size);
154 else if (__input_buffer + __input_size == __new_room)
155 __input_size += __new_size;
156 else
158 __input_size = __new_size;
159 __input_buffer = (char *) memcpy (__new_room, __input_buffer,
160 __current_size);
163 __input_buffer[__current_size++] = __ch;
165 if (__current_size)
166 __input_buffer[__current_size++] = '\0';
167 else
168 __input_buffer = "";
170 /* Now compile the pattern. */
171 __error = regcomp (__expr_ptr, __input_buffer, REG_NEWLINE);
172 if (__error != 0)
173 /* Oh well, we have to translate POSIX error codes. */
174 switch (__error)
176 case REG_BADPAT:
177 case REG_ECOLLATE:
178 case REG_ECTYPE:
179 case REG_EESCAPE:
180 case REG_BADRPT:
181 case REG_EEND:
182 case REG_ERPAREN:
183 default:
184 /* There is no matching error code. */
185 RETURN (36);
186 case REG_ESUBREG:
187 RETURN (25);
188 case REG_EBRACK:
189 RETURN (49);
190 case REG_EPAREN:
191 RETURN (42);
192 case REG_EBRACE:
193 RETURN (44);
194 case REG_BADBR:
195 RETURN (46);
196 case REG_ERANGE:
197 RETURN (11);
198 case REG_ESPACE:
199 case REG_ESIZE:
200 ERROR (50);
203 /* Everything is ok. */
204 RETURN ((char *) (__expr_ptr->__REPB_PREFIX (buffer)
205 + __expr_ptr->__REPB_PREFIX (used)));
207 #endif
210 /* Find the next match in STRING. The compiled regular expression is
211 found in the buffer starting at EXPBUF. `loc1' will return the
212 first character matched and `loc2' points to the next unmatched
213 character. */
214 extern int step (const char *__restrict __string,
215 const char *__restrict __expbuf) __THROW;
217 /* Match the beginning of STRING with the compiled regular expression
218 in EXPBUF. If the match is successful `loc2' will contain the
219 position of the first unmatched character. */
220 extern int advance (const char *__restrict __string,
221 const char *__restrict __expbuf) __THROW;
224 __END_DECLS
226 #endif /* regexp.h */