]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
734aad71 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
e9ce8d39 | 7 | * |
734aad71 A |
8 | * This file contains Original Code and/or Modifications of Original Code |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
e9ce8d39 A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
734aad71 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
e9ce8d39 A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * Copyright (c) 1992, 1993, 1994 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * This code is derived from software contributed to Berkeley by | |
30 | * Henry Spencer. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | */ | |
60 | ||
61 | /* | |
62 | * First, the stuff that ends up in the outside-world include file | |
63 | = typedef off_t regoff_t; | |
64 | = typedef struct { | |
65 | = int re_magic; | |
66 | = size_t re_nsub; // number of parenthesized subexpressions | |
67 | = const char *re_endp; // end pointer for REG_PEND | |
68 | = struct re_guts *re_g; // none of your business :-) | |
69 | = } regex_t; | |
70 | = typedef struct { | |
71 | = regoff_t rm_so; // start of match | |
72 | = regoff_t rm_eo; // end of match | |
73 | = } regmatch_t; | |
74 | */ | |
75 | /* | |
76 | * internals of regex_t | |
77 | */ | |
78 | #define MAGIC1 ((('r'^0200)<<8) | 'e') | |
79 | ||
80 | /* | |
81 | * The internal representation is a *strip*, a sequence of | |
82 | * operators ending with an endmarker. (Some terminology etc. is a | |
83 | * historical relic of earlier versions which used multiple strips.) | |
84 | * Certain oddities in the representation are there to permit running | |
85 | * the machinery backwards; in particular, any deviation from sequential | |
86 | * flow must be marked at both its source and its destination. Some | |
87 | * fine points: | |
88 | * | |
89 | * - OPLUS_ and O_PLUS are *inside* the loop they create. | |
90 | * - OQUEST_ and O_QUEST are *outside* the bypass they create. | |
91 | * - OCH_ and O_CH are *outside* the multi-way branch they create, while | |
92 | * OOR1 and OOR2 are respectively the end and the beginning of one of | |
93 | * the branches. Note that there is an implicit OOR2 following OCH_ | |
94 | * and an implicit OOR1 preceding O_CH. | |
95 | * | |
96 | * In state representations, an operator's bit is on to signify a state | |
97 | * immediately *preceding* "execution" of that operator. | |
98 | */ | |
99 | typedef unsigned long sop; /* strip operator */ | |
100 | typedef long sopno; | |
101 | #define OPRMASK 0xf8000000 | |
102 | #define OPDMASK 0x07ffffff | |
103 | #define OPSHIFT ((unsigned)27) | |
104 | #define OP(n) ((n)&OPRMASK) | |
105 | #define OPND(n) ((n)&OPDMASK) | |
106 | #define SOP(op, opnd) ((op)|(opnd)) | |
107 | /* operators meaning operand */ | |
108 | /* (back, fwd are offsets) */ | |
109 | #define OEND (1<<OPSHIFT) /* endmarker - */ | |
110 | #define OCHAR (2<<OPSHIFT) /* character unsigned char */ | |
111 | #define OBOL (3<<OPSHIFT) /* left anchor - */ | |
112 | #define OEOL (4<<OPSHIFT) /* right anchor - */ | |
113 | #define OANY (5<<OPSHIFT) /* . - */ | |
114 | #define OANYOF (6<<OPSHIFT) /* [...] set number */ | |
115 | #define OBACK_ (7<<OPSHIFT) /* begin \d paren number */ | |
116 | #define O_BACK (8<<OPSHIFT) /* end \d paren number */ | |
117 | #define OPLUS_ (9<<OPSHIFT) /* + prefix fwd to suffix */ | |
118 | #define O_PLUS (10<<OPSHIFT) /* + suffix back to prefix */ | |
119 | #define OQUEST_ (11<<OPSHIFT) /* ? prefix fwd to suffix */ | |
120 | #define O_QUEST (12<<OPSHIFT) /* ? suffix back to prefix */ | |
121 | #define OLPAREN (13<<OPSHIFT) /* ( fwd to ) */ | |
122 | #define ORPAREN (14<<OPSHIFT) /* ) back to ( */ | |
123 | #define OCH_ (15<<OPSHIFT) /* begin choice fwd to OOR2 */ | |
124 | #define OOR1 (16<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */ | |
125 | #define OOR2 (17<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */ | |
126 | #define O_CH (18<<OPSHIFT) /* end choice back to OOR1 */ | |
127 | #define OBOW (19<<OPSHIFT) /* begin word - */ | |
128 | #define OEOW (20<<OPSHIFT) /* end word - */ | |
129 | ||
130 | /* | |
131 | * Structure for [] character-set representation. Character sets are | |
132 | * done as bit vectors, grouped 8 to a byte vector for compactness. | |
133 | * The individual set therefore has both a pointer to the byte vector | |
134 | * and a mask to pick out the relevant bit of each byte. A hash code | |
135 | * simplifies testing whether two sets could be identical. | |
136 | * | |
137 | * This will get trickier for multicharacter collating elements. As | |
138 | * preliminary hooks for dealing with such things, we also carry along | |
139 | * a string of multi-character elements, and decide the size of the | |
140 | * vectors at run time. | |
141 | */ | |
142 | typedef struct { | |
143 | uch *ptr; /* -> uch [csetsize] */ | |
144 | uch mask; /* bit within array */ | |
145 | uch hash; /* hash code */ | |
146 | size_t smultis; | |
147 | char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */ | |
148 | } cset; | |
149 | /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */ | |
150 | #define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c)) | |
151 | #define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c)) | |
152 | #define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask) | |
153 | #define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */ | |
154 | #define MCsub(p, cs, cp) mcsub(p, cs, cp) | |
155 | #define MCin(p, cs, cp) mcin(p, cs, cp) | |
156 | ||
157 | /* stuff for character categories */ | |
158 | typedef unsigned char cat_t; | |
159 | ||
160 | /* | |
161 | * main compiled-expression structure | |
162 | */ | |
163 | struct re_guts { | |
164 | int magic; | |
165 | # define MAGIC2 ((('R'^0200)<<8)|'E') | |
166 | sop *strip; /* malloced area for strip */ | |
167 | int csetsize; /* number of bits in a cset vector */ | |
168 | int ncsets; /* number of csets in use */ | |
169 | cset *sets; /* -> cset [ncsets] */ | |
170 | uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */ | |
171 | int cflags; /* copy of regcomp() cflags argument */ | |
172 | sopno nstates; /* = number of sops */ | |
173 | sopno firststate; /* the initial OEND (normally 0) */ | |
174 | sopno laststate; /* the final OEND */ | |
175 | int iflags; /* internal flags */ | |
176 | # define USEBOL 01 /* used ^ */ | |
177 | # define USEEOL 02 /* used $ */ | |
178 | # define BAD 04 /* something wrong */ | |
179 | int nbol; /* number of ^ used */ | |
180 | int neol; /* number of $ used */ | |
181 | int ncategories; /* how many character categories */ | |
182 | cat_t *categories; /* ->catspace[-CHAR_MIN] */ | |
183 | char *must; /* match must contain this string */ | |
184 | int mlen; /* length of must */ | |
185 | size_t nsub; /* copy of re_nsub */ | |
186 | int backrefs; /* does it use back references? */ | |
187 | sopno nplus; /* how deep does it nest +s? */ | |
188 | /* catspace must be last */ | |
189 | cat_t catspace[1]; /* actually [NC] */ | |
190 | }; | |
191 | ||
192 | /* misc utilities */ | |
193 | #define OUT (CHAR_MAX+1) /* a non-character value */ | |
194 | #define ISWORD(c) (isalnum(c) || (c) == '_') |