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