Update.
[glibc.git] / misc / regexp.h
blobdc61c8dc70248b1ed5f07b8cc7b03efc36aca14a
1 /* Copyright (C) 1996, 1997 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. */
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.
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 <alloca.h>
32 #include <regex.h>
33 #include <stdlib.h>
34 #include <string.h>
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
39 word).
41 The user has to provide six macros before this header file can be
42 included:
44 INIT Declarations vor variables which can be used by the
45 other macros.
47 GETC() Return the value of the next character in the regular
48 expression pattern. Successive calls should return
49 successive characters.
51 PEEKC() Return the value of the next character in the regular
52 expression pattern. Immediately successive calls to
53 PEEKC() should return the same character which should
54 also be the next character returned by GETC().
56 UNGETC(c) Cause `c' to be returned by the next call to GETC() and
57 PEEKC().
59 RETURN(ptr) Used for normal exit of the `compile' function. `ptr'
60 is a pointer to the character after the last character of
61 the compiled regular expression.
63 ERROR(val) Used for abnormal return from `compile'. `val' is the
64 error number. The error codes are:
65 11 Range endpoint too large.
66 16 Bad number.
67 25 \digit out of range.
68 36 Illegal or missing delimiter.
69 41 No remembered search string.
70 42 \( \) imbalance.
71 43 Too many \(.
72 44 More tan two numbers given in \{ \}.
73 45 } expected after \.
74 46 First number exceeds second in \{ \}.
75 49 [ ] imbalance.
76 50 Regular expression overflow.
80 __BEGIN_DECLS
82 /* Interface variables. They contain the results of the successful
83 calls to `setp' and `advance'. */
84 extern char *loc1;
85 extern char *loc2;
87 /* The use of this variable in the `advance' function is not
88 supported. */
89 extern char *locs;
92 #ifndef __DO_NOT_DEFINE_COMPILE
93 /* Get and compile the user supplied pattern up to end of line or
94 string or until EOF is seen, whatever happens first. The result is
95 placed in the buffer starting at EXPBUG and delimited by ENDBUF.
97 This function cannot be defined in the libc itself since it depends
98 on the macros. */
99 char *
100 compile (char *instring, char *expbuf, __const char *endbuf, int eof)
102 char *__input_buffer = NULL;
103 size_t __input_size = 0;
104 size_t __current_size = 0;
105 int __ch;
106 int __error;
107 INIT
109 /* Align the expression buffer according to the needs for an object
110 of type `regex_t'. Then check for minimum size of the buffer for
111 the compiled regular expression. */
112 regex_t *__expr_ptr;
113 #if defined __GNUC__ && __GNUC__ >= 2
114 const size_t __req = __alignof__ (regex_t *);
115 #else
116 /* How shall we find out? We simply guess it and can change it is
117 this really proofs to be wrong. */
118 const size_t __req = 8;
119 #endif
120 expbuf += __req;
121 expbuf -= (expbuf - ((char *) 0)) % __req;
122 if (endbuf < expbuf + sizeof (regex_t))
124 ERROR (50);
126 __expr_ptr = (regex_t *) expbuf;
127 /* The remaining space in the buffer can be used for the compiled
128 pattern. */
129 __expr_ptr->buffer = expbuf + sizeof (regex_t);
130 __expr_ptr->allocated = endbuf - (char *) __expr_ptr->buffer;
132 while ((__ch = (GETC ())) != eof)
134 if (__ch == '\0' || __ch == 'n')
136 UNGETC (__ch);
137 break;
140 if (__current_size + 1 >= __input_size)
142 size_t __new_size = __input_size ? 2 * __input_size : 128;
143 char *__new_room = __alloca (__new_size);
144 /* See whether we can use the old buffer. */
145 if (__new_room + __new_size == __input_buffer)
147 __input_size += __new_size;
148 __input_buffer = memcpy (__new_room, __input_buffer,
149 __current_size);
151 else if (__input_buffer + __input_size == __new_room)
152 __input_size += __new_size;
153 else
155 __input_size = __new_size;
156 __input_buffer = memcpy (__new_room, __input_buffer,
157 __current_size);
160 __input_buffer[__current_size++] = __ch;
162 __input_buffer[__current_size++] = '\0';
164 /* Now compile the pattern. */
165 __error = regcomp (__expr_ptr, __input_buffer, REG_NEWLINE);
166 if (__error != 0)
167 /* Oh well, we have to translate POSIX error codes. */
168 switch (__error)
170 case REG_BADPAT:
171 case REG_ECOLLATE:
172 case REG_ECTYPE:
173 case REG_EESCAPE:
174 case REG_BADRPT:
175 case REG_EEND:
176 case REG_ERPAREN:
177 default:
178 /* There is no matching error code. */
179 RETURN (36);
180 case REG_ESUBREG:
181 RETURN (25);
182 case REG_EBRACK:
183 RETURN (49);
184 case REG_EPAREN:
185 RETURN (42);
186 case REG_EBRACE:
187 RETURN (44);
188 case REG_BADBR:
189 RETURN (46);
190 case REG_ERANGE:
191 RETURN (11);
192 case REG_ESPACE:
193 case REG_ESIZE:
194 ERROR (50);
197 /* Everything is ok. */
198 RETURN ((char *) (__expr_ptr->buffer + __expr_ptr->used));
200 #endif
203 /* Find the next match in STRING. The compiled regular expression is
204 found in the buffer starting at EXPBUF. `loc1' will return the
205 first character matched and `loc2' points to the next unmatched
206 character. */
207 extern int step __P ((__const char *__string, __const char *__expbuf));
209 /* Match the beginning of STRING with the compiled regular expression
210 in EXPBUF. If the match is successful `loc2' will contain the
211 position of the first unmatched character. */
212 extern int advance __P ((__const char *__string, __const char *__expbuf));
215 __END_DECLS
217 #endif /* regexp.h */