- regcomp - compile a regular expression into internal code * * We can't allocate space until we know how big the compiled form will be, * but we can't compile it (and thus know how big it is) until we've got a * place to put the code. So we cheat: we compile it twice, once with code * generation turned off and size counting turned on, and once "for real". * This also means that we don't a
| 214 | * of the structure of the compiled regexp. |
| 215 | */ |
| 216 | regexp * |
| 217 | regcomp( const char *exp ) |
| 218 | { |
| 219 | register regexp *r; |
| 220 | register char *scan; |
| 221 | register char *longest; |
| 222 | register unsigned len; |
| 223 | int flags; |
| 224 | |
| 225 | if (exp == NULL) |
| 226 | FAIL("NULL argument"); |
| 227 | |
| 228 | /* First pass: determine size, legality. */ |
| 229 | #ifdef notdef |
| 230 | if (exp[0] == '.' && exp[1] == '*') exp += 2; /* aid grep */ |
| 231 | #endif |
| 232 | regparse = (char *)exp; |
| 233 | regnpar = 1; |
| 234 | regsize = 0L; |
| 235 | regcode = ®dummy; |
| 236 | regc(MAGIC); |
| 237 | if (reg(0, &flags) == NULL) |
| 238 | return(NULL); |
| 239 | |
| 240 | /* Small enough for pointer-storage convention? */ |
| 241 | if (regsize >= 32767L) /* Probably could be 65535L. */ |
| 242 | FAIL("regexp too big"); |
| 243 | |
| 244 | /* Allocate space. */ |
| 245 | r = (regexp *)BJAM_MALLOC(sizeof(regexp) + (unsigned)regsize); |
| 246 | if (r == NULL) |
| 247 | FAIL("out of space"); |
| 248 | |
| 249 | /* Second pass: emit code. */ |
| 250 | regparse = (char *)exp; |
| 251 | regnpar = 1; |
| 252 | regcode = r->program; |
| 253 | regc(MAGIC); |
| 254 | if (reg(0, &flags) == NULL) |
| 255 | return(NULL); |
| 256 | |
| 257 | /* Dig out information for optimizations. */ |
| 258 | r->regstart = '\0'; /* Worst-case defaults. */ |
| 259 | r->reganch = 0; |
| 260 | r->regmust = NULL; |
| 261 | r->regmlen = 0; |
| 262 | scan = r->program+1; /* First BRANCH. */ |
| 263 | if (OP(regnext(scan)) == END) { /* Only one top-level choice. */ |
| 264 | scan = OPERAND(scan); |
| 265 | |
| 266 | /* Starting-point info. */ |
| 267 | if (OP(scan) == EXACTLY) |
| 268 | r->regstart = *OPERAND(scan); |
| 269 | else if (OP(scan) == BOL) |
| 270 | r->reganch++; |
| 271 | |
| 272 | /* |
| 273 | * If there's something expensive in the r.e., find the |
no test coverage detected