| 490 | registers. |
| 491 | |
| 492 | It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for |
| 493 | the return codes and their meanings.) */ |
| 494 | |
| 495 | int |
| 496 | regcomp (regex_t *__restrict preg, |
| 497 | const char *__restrict pattern, |
| 498 | int cflags) |
| 499 | { |
| 500 | reg_errcode_t ret; |
| 501 | reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED |
| 502 | : RE_SYNTAX_POSIX_BASIC); |
| 503 | |
| 504 | preg->buffer = NULL; |
| 505 | preg->allocated = 0; |
| 506 | preg->used = 0; |
| 507 | |
| 508 | /* Try to allocate space for the fastmap. */ |
| 509 | preg->fastmap = re_malloc (char, SBC_MAX); |
| 510 | if (BE (preg->fastmap == NULL, 0)) |
| 511 | return REG_ESPACE; |
| 512 | |
| 513 | syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0; |
| 514 | |
| 515 | /* If REG_NEWLINE is set, newlines are treated differently. */ |
| 516 | if (cflags & REG_NEWLINE) |
| 517 | { /* REG_NEWLINE implies neither . nor [^...] match newline. */ |
| 518 | syntax &= ~RE_DOT_NEWLINE; |
| 519 | syntax |= RE_HAT_LISTS_NOT_NEWLINE; |
| 520 | /* It also changes the matching behavior. */ |
| 521 | preg->newline_anchor = 1; |
| 522 | } |
| 523 | else |
| 524 | preg->newline_anchor = 0; |
| 525 | preg->no_sub = !!(cflags & REG_NOSUB); |
| 526 | preg->translate = NULL; |
| 527 | |
| 528 | ret = re_compile_internal (preg, pattern, strlen (pattern), syntax); |
| 529 | |
| 530 | /* POSIX doesn't distinguish between an unmatched open-group and an |
| 531 | unmatched close-group: both are REG_EPAREN. */ |
| 532 | if (ret == REG_ERPAREN) |
| 533 | ret = REG_EPAREN; |
| 534 | |
| 535 | /* We have already checked preg->fastmap != NULL. */ |
| 536 | if (BE (ret == REG_NOERROR, 1)) |
| 537 | /* Compute the fastmap now, since regexec cannot modify the pattern |
| 538 | buffer. This function never fails in this implementation. */ |
| 539 | (void) re_compile_fastmap (preg); |
| 540 | else |
| 541 | { |
| 542 | /* Some error occurred while compiling the expression. */ |
| 543 | re_free (preg->fastmap); |
| 544 | preg->fastmap = NULL; |
| 545 | } |
| 546 | |
| 547 | return (int) ret; |
| 548 | } |