| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Matches Perl-compatible regular expressions |
| 4 | * |
| 5 | * (c) 2002 Mark Wooding |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Anag: a simple wordgame helper. |
| 11 | * |
| 12 | * Anag is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * Anag is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with Anag; if not, write to the Free Software Foundation, |
| 24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | /*----- Header files ------------------------------------------------------*/ |
| 28 | |
| 29 | #include "anag.h" |
| 30 | |
| 31 | #ifdef HAVE_PCRE2 |
| 32 | # define PCRE2_CODE_UNIT_WIDTH 8 |
| 33 | # include <pcre2.h> |
| 34 | #endif |
| 35 | |
| 36 | #ifdef HAVE_PCRE |
| 37 | # include <pcre.h> |
| 38 | #endif |
| 39 | |
| 40 | /*----- Data structures ---------------------------------------------------*/ |
| 41 | |
| 42 | typedef struct node_pcre { |
| 43 | node n; |
| 44 | const char *s; |
| 45 | #ifdef HAVE_PCRE2 |
| 46 | pcre2_code *rx; |
| 47 | pcre2_match_data *m; |
| 48 | #endif |
| 49 | #ifdef HAVE_PCRE |
| 50 | pcre *rx; |
| 51 | pcre_extra *rx_study; |
| 52 | int *ovec; |
| 53 | int ovecsz; |
| 54 | #endif |
| 55 | } node_pcre; |
| 56 | |
| 57 | /*----- Main code ---------------------------------------------------------*/ |
| 58 | |
| 59 | /* --- Node matcher --- */ |
| 60 | |
| 61 | static int n_pcre(node *nn, const char *p, size_t sz) |
| 62 | { |
| 63 | node_pcre *n = (node_pcre *)nn; |
| 64 | #ifdef HAVE_PCRE2 |
| 65 | char buf[128]; |
| 66 | int rc; |
| 67 | #endif |
| 68 | #ifdef HAVE_PCRE |
| 69 | int e; |
| 70 | #endif |
| 71 | |
| 72 | #ifdef HAVE_PCRE2 |
| 73 | rc = pcre2_match(n->rx, (PCRE2_SPTR)p, sz, 0, 0, n->m, 0); |
| 74 | if (rc >= 0) return (1); |
| 75 | else switch (rc) { |
| 76 | case PCRE2_ERROR_NOMATCH: return (0); |
| 77 | default: |
| 78 | rc = pcre2_get_error_message(rc, (PCRE2_UCHAR *)buf, sizeof(buf)); |
| 79 | assert(!rc); die("pcre2 matching failed': %s", buf); |
| 80 | } |
| 81 | #endif |
| 82 | #ifdef HAVE_PCRE |
| 83 | e = pcre_exec(n->rx, n->rx_study, p, sz, 0, 0, n->ovec, n->ovecsz); |
| 84 | if (e >= 0) return (1); |
| 85 | if (e == PCRE_ERROR_NOMATCH) return (0); |
| 86 | die("unexpected PCRE error code %d", e); |
| 87 | #endif |
| 88 | } |
| 89 | |
| 90 | /* --- Node creation --- */ |
| 91 | |
| 92 | node *pcrenode(const char *const *av) |
| 93 | { |
| 94 | node_pcre *n = xmalloc(sizeof(*n)); |
| 95 | #ifdef HAVE_PCRE2 |
| 96 | char buf[128]; |
| 97 | int err; |
| 98 | PCRE2_SIZE eo; |
| 99 | uint32_t c; |
| 100 | #endif |
| 101 | #ifdef HAVE_PCRE |
| 102 | const char *e; |
| 103 | int eo; |
| 104 | int c; |
| 105 | #endif |
| 106 | |
| 107 | n->n.func = n_pcre; |
| 108 | |
| 109 | #ifdef HAVE_PCRE2 |
| 110 | n->rx = pcre2_compile((PCRE2_SPTR)av[0], strlen(av[0]), PCRE2_CASELESS, |
| 111 | &err, &eo, 0); |
| 112 | if (!n->rx) { |
| 113 | err = pcre2_get_error_message(err, (PCRE2_UCHAR *)buf, sizeof(buf)); |
| 114 | assert(!err); die("bad regular expression `%s': %s", av[0], buf); |
| 115 | } |
| 116 | err = pcre2_pattern_info(n->rx, PCRE2_INFO_BACKREFMAX, &c); |
| 117 | assert(!err); |
| 118 | n->m = pcre2_match_data_create_from_pattern(n->rx, 0); |
| 119 | if (!n->m) { |
| 120 | err = pcre2_get_error_message(err, (PCRE2_UCHAR *)buf, sizeof(buf)); |
| 121 | assert(!err); die("failed to allocate match data: %s", buf); |
| 122 | } |
| 123 | pcre2_jit_compile(n->rx, PCRE2_JIT_COMPLETE); |
| 124 | #endif |
| 125 | #ifdef HAVE_PCRE |
| 126 | n->rx = pcre_compile(av[0], PCRE_CASELESS, &e, &eo, 0); |
| 127 | if (!n->rx) die("bad regular expression `%s': %s", av[0], e); |
| 128 | n->rx_study = pcre_study(n->rx, 0, &e); |
| 129 | if (e) die("error studying pattern `%s': %s", av[0], e); |
| 130 | pcre_fullinfo(n->rx, n->rx_study, PCRE_INFO_CAPTURECOUNT, &c); |
| 131 | n->ovecsz = 2*c; |
| 132 | n->ovec = xmalloc(n->ovecsz*sizeof(*n->ovec)); |
| 133 | #endif |
| 134 | |
| 135 | return (&n->n); |
| 136 | } |
| 137 | |
| 138 | /*----- That's all, folks -------------------------------------------------*/ |