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