]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1996-2015 Free Software Foundation, Inc. |
84384f5b UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
84384f5b UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
84384f5b | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
84384f5b UD |
18 | |
19 | #ifndef _REGEXP_H | |
20 | #define _REGEXP_H 1 | |
21 | ||
2ec11c2b ZW |
22 | /* The contents of this header file were standardized in the |
23 | Single Unix Specification, Version 2 (1997) but marked as | |
24 | LEGACY; new applications were already being encouraged to | |
25 | use <regex.h> instead. POSIX.1-2001 removed this header. | |
84384f5b | 26 | |
2ec11c2b ZW |
27 | This header is provided only for backward compatibility. |
28 | It will be removed in the next release of the GNU C Library. | |
29 | New code should use <regex.h> instead. */ | |
30 | ||
31 | #warning "<regexp.h> will be removed in the next release of the GNU C Library." | |
32 | #warning "Please update your code to use <regex.h> instead (no trailing 'p')." | |
84384f5b | 33 | |
dfd2257a | 34 | #include <features.h> |
84384f5b UD |
35 | #include <alloca.h> |
36 | #include <regex.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | ||
40 | /* The implementation provided here emulates the needed functionality | |
41 | by mapping to the POSIX regular expression matcher. The interface | |
42 | for the here included function is weird (this really is a harmless | |
43 | word). | |
44 | ||
714a562f | 45 | The user has to provide six macros before this header file can be |
84384f5b UD |
46 | included: |
47 | ||
714a562f UD |
48 | INIT Declarations vor variables which can be used by the |
49 | other macros. | |
50 | ||
84384f5b UD |
51 | GETC() Return the value of the next character in the regular |
52 | expression pattern. Successive calls should return | |
53 | successive characters. | |
54 | ||
55 | PEEKC() Return the value of the next character in the regular | |
56 | expression pattern. Immediately successive calls to | |
57 | PEEKC() should return the same character which should | |
58 | also be the next character returned by GETC(). | |
59 | ||
60 | UNGETC(c) Cause `c' to be returned by the next call to GETC() and | |
61 | PEEKC(). | |
62 | ||
63 | RETURN(ptr) Used for normal exit of the `compile' function. `ptr' | |
64 | is a pointer to the character after the last character of | |
65 | the compiled regular expression. | |
66 | ||
67 | ERROR(val) Used for abnormal return from `compile'. `val' is the | |
68 | error number. The error codes are: | |
69 | 11 Range endpoint too large. | |
70 | 16 Bad number. | |
71 | 25 \digit out of range. | |
72 | 36 Illegal or missing delimiter. | |
73 | 41 No remembered search string. | |
74 | 42 \( \) imbalance. | |
75 | 43 Too many \(. | |
76 | 44 More tan two numbers given in \{ \}. | |
77 | 45 } expected after \. | |
78 | 46 First number exceeds second in \{ \}. | |
79 | 49 [ ] imbalance. | |
80 | 50 Regular expression overflow. | |
81 | ||
82 | */ | |
83 | ||
84 | __BEGIN_DECLS | |
85 | ||
86 | /* Interface variables. They contain the results of the successful | |
87 | calls to `setp' and `advance'. */ | |
88 | extern char *loc1; | |
89 | extern char *loc2; | |
90 | ||
91 | /* The use of this variable in the `advance' function is not | |
92 | supported. */ | |
93 | extern char *locs; | |
94 | ||
95 | ||
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 | |
9963a779 | 99 | placed in the buffer starting at EXPBUF and delimited by ENDBUF. |
84384f5b UD |
100 | |
101 | This function cannot be defined in the libc itself since it depends | |
102 | on the macros. */ | |
103 | char * | |
9963a779 | 104 | compile (char *__restrict instring, char *__restrict expbuf, |
a784e502 | 105 | const char *__restrict endbuf, int eof) |
84384f5b UD |
106 | { |
107 | char *__input_buffer = NULL; | |
108 | size_t __input_size = 0; | |
714a562f | 109 | size_t __current_size = 0; |
84384f5b | 110 | int __ch; |
8d57beea | 111 | int __error; |
714a562f | 112 | INIT |
84384f5b UD |
113 | |
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; | |
dfd2257a | 118 | # if defined __GNUC__ && __GNUC__ >= 2 |
84384f5b | 119 | const size_t __req = __alignof__ (regex_t *); |
dfd2257a | 120 | # else |
84384f5b UD |
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; | |
dfd2257a | 124 | # endif |
9756dfe1 UD |
125 | expbuf += __req; |
126 | expbuf -= (expbuf - ((char *) 0)) % __req; | |
127 | if (endbuf < expbuf + sizeof (regex_t)) | |
84384f5b UD |
128 | { |
129 | ERROR (50); | |
130 | } | |
9756dfe1 | 131 | __expr_ptr = (regex_t *) expbuf; |
84384f5b UD |
132 | /* The remaining space in the buffer can be used for the compiled |
133 | pattern. */ | |
eef8a803 JJ |
134 | __expr_ptr->__REPB_PREFIX (buffer) = expbuf + sizeof (regex_t); |
135 | __expr_ptr->__REPB_PREFIX (allocated) | |
136 | = endbuf - (char *) __expr_ptr->__REPB_PREFIX (buffer); | |
84384f5b | 137 | |
9756dfe1 | 138 | while ((__ch = (GETC ())) != eof) |
84384f5b | 139 | { |
9963a779 | 140 | if (__ch == '\0' || __ch == '\n') |
84384f5b UD |
141 | { |
142 | UNGETC (__ch); | |
143 | break; | |
144 | } | |
145 | ||
146 | if (__current_size + 1 >= __input_size) | |
147 | { | |
148 | size_t __new_size = __input_size ? 2 * __input_size : 128; | |
b4751608 | 149 | char *__new_room = (char *) alloca (__new_size); |
84384f5b UD |
150 | /* See whether we can use the old buffer. */ |
151 | if (__new_room + __new_size == __input_buffer) | |
152 | { | |
153 | __input_size += __new_size; | |
b4751608 AS |
154 | __input_buffer = (char *) memcpy (__new_room, __input_buffer, |
155 | __current_size); | |
84384f5b UD |
156 | } |
157 | else if (__input_buffer + __input_size == __new_room) | |
158 | __input_size += __new_size; | |
159 | else | |
160 | { | |
161 | __input_size = __new_size; | |
b4751608 AS |
162 | __input_buffer = (char *) memcpy (__new_room, __input_buffer, |
163 | __current_size); | |
84384f5b UD |
164 | } |
165 | } | |
166 | __input_buffer[__current_size++] = __ch; | |
167 | } | |
eef8a803 JJ |
168 | if (__current_size) |
169 | __input_buffer[__current_size++] = '\0'; | |
170 | else | |
171 | __input_buffer = ""; | |
84384f5b UD |
172 | |
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) | |
178 | { | |
179 | case REG_BADPAT: | |
180 | case REG_ECOLLATE: | |
181 | case REG_ECTYPE: | |
182 | case REG_EESCAPE: | |
183 | case REG_BADRPT: | |
184 | case REG_EEND: | |
8d57beea | 185 | case REG_ERPAREN: |
84384f5b UD |
186 | default: |
187 | /* There is no matching error code. */ | |
2ec11c2b | 188 | ERROR (36); |
84384f5b | 189 | case REG_ESUBREG: |
2ec11c2b | 190 | ERROR (25); |
84384f5b | 191 | case REG_EBRACK: |
2ec11c2b | 192 | ERROR (49); |
84384f5b | 193 | case REG_EPAREN: |
2ec11c2b | 194 | ERROR (42); |
84384f5b | 195 | case REG_EBRACE: |
2ec11c2b | 196 | ERROR (44); |
84384f5b | 197 | case REG_BADBR: |
2ec11c2b | 198 | ERROR (46); |
84384f5b | 199 | case REG_ERANGE: |
2ec11c2b | 200 | ERROR (11); |
84384f5b UD |
201 | case REG_ESPACE: |
202 | case REG_ESIZE: | |
203 | ERROR (50); | |
204 | } | |
205 | ||
206 | /* Everything is ok. */ | |
eef8a803 JJ |
207 | RETURN ((char *) (__expr_ptr->__REPB_PREFIX (buffer) |
208 | + __expr_ptr->__REPB_PREFIX (used))); | |
84384f5b UD |
209 | } |
210 | #endif | |
211 | ||
212 | ||
213 | /* Find the next match in STRING. The compiled regular expression is | |
214 | found in the buffer starting at EXPBUF. `loc1' will return the | |
215 | first character matched and `loc2' points to the next unmatched | |
216 | character. */ | |
a784e502 UD |
217 | extern int step (const char *__restrict __string, |
218 | const char *__restrict __expbuf) __THROW; | |
84384f5b UD |
219 | |
220 | /* Match the beginning of STRING with the compiled regular expression | |
221 | in EXPBUF. If the match is successful `loc2' will contain the | |
222 | position of the first unmatched character. */ | |
a784e502 UD |
223 | extern int advance (const char *__restrict __string, |
224 | const char *__restrict __expbuf) __THROW; | |
84384f5b UD |
225 | |
226 | ||
227 | __END_DECLS | |
228 | ||
229 | #endif /* regexp.h */ |