(preg, pattern, cflags)
| 4730 | the return codes and their meanings.) */ |
| 4731 | |
| 4732 | int |
| 4733 | regcomp (preg, pattern, cflags) |
| 4734 | regex_t *preg; |
| 4735 | const char *pattern; |
| 4736 | int cflags; |
| 4737 | { |
| 4738 | reg_errcode_t ret; |
| 4739 | unsigned syntax |
| 4740 | = (cflags & REG_EXTENDED) ? |
| 4741 | RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC; |
| 4742 | |
| 4743 | /* regex_compile will allocate the space for the compiled pattern. */ |
| 4744 | preg->buffer = 0; |
| 4745 | preg->allocated = 0; |
| 4746 | |
| 4747 | /* Don't bother to use a fastmap when searching. This simplifies the |
| 4748 | REG_NEWLINE case: if we used a fastmap, we'd have to put all the |
| 4749 | characters after newlines into the fastmap. This way, we just try |
| 4750 | every character. */ |
| 4751 | preg->fastmap = 0; |
| 4752 | |
| 4753 | if (cflags & REG_ICASE) |
| 4754 | { |
| 4755 | unsigned i; |
| 4756 | |
| 4757 | preg->translate = (char *) malloc (CHAR_SET_SIZE); |
| 4758 | if (preg->translate == NULL) |
| 4759 | return (int) REG_ESPACE; |
| 4760 | |
| 4761 | /* Map uppercase characters to corresponding lowercase ones. */ |
| 4762 | for (i = 0; i < CHAR_SET_SIZE; i++) |
| 4763 | preg->translate[i] = ISUPPER (i) ? tolower (i) : i; |
| 4764 | } |
| 4765 | else |
| 4766 | preg->translate = NULL; |
| 4767 | |
| 4768 | /* If REG_NEWLINE is set, newlines are treated differently. */ |
| 4769 | if (cflags & REG_NEWLINE) |
| 4770 | { /* REG_NEWLINE implies neither . nor [^...] match newline. */ |
| 4771 | syntax &= ~RE_DOT_NEWLINE; |
| 4772 | syntax |= RE_HAT_LISTS_NOT_NEWLINE; |
| 4773 | /* It also changes the matching behavior. */ |
| 4774 | preg->newline_anchor = 1; |
| 4775 | } |
| 4776 | else |
| 4777 | preg->newline_anchor = 0; |
| 4778 | |
| 4779 | preg->no_sub = !!(cflags & REG_NOSUB); |
| 4780 | |
| 4781 | /* POSIX says a null character in the pattern terminates it, so we |
| 4782 | can use strlen here in compiling the pattern. */ |
| 4783 | ret = regex_compile (pattern, strlen (pattern), syntax, preg); |
| 4784 | |
| 4785 | /* POSIX doesn't distinguish between an unmatched open-group and an |
| 4786 | unmatched close-group: both are REG_EPAREN. */ |
| 4787 | if (ret == REG_ERPAREN) ret = REG_EPAREN; |
| 4788 | |
| 4789 | return (int) ret; |
no test coverage detected