| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: pcre.c,v 1.2 2004/04/08 01:36:19 mdw Exp $ |
| 4 | * |
| 5 | * Matches Perl-compatible regular expressions |
| 6 | * |
| 7 | * (c) 2002 Mark Wooding |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Anag: a simple wordgame helper. |
| 13 | * |
| 14 | * Anag is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * Anag is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with Anag; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Header files ------------------------------------------------------*/ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | # include "config.h" |
| 33 | #endif |
| 34 | |
| 35 | #ifndef HAVE_PCRE |
| 36 | extern int dummy; |
| 37 | #else |
| 38 | |
| 39 | #include "anag.h" |
| 40 | #include <pcre.h> |
| 41 | |
| 42 | /*----- Data structures ---------------------------------------------------*/ |
| 43 | |
| 44 | typedef struct node_pcre { |
| 45 | node n; |
| 46 | const char *s; |
| 47 | pcre *rx; |
| 48 | pcre_extra *rx_study; |
| 49 | int *ovec; |
| 50 | int ovecsz; |
| 51 | } node_pcre; |
| 52 | |
| 53 | /*----- Main code ---------------------------------------------------------*/ |
| 54 | |
| 55 | /* --- Node matcher --- */ |
| 56 | |
| 57 | static int n_pcre(node *nn, const char *p, size_t sz) |
| 58 | { |
| 59 | node_pcre *n = (node_pcre *)nn; |
| 60 | int e; |
| 61 | |
| 62 | e = pcre_exec(n->rx, n->rx_study, p, sz, 0, 0, n->ovec, n->ovecsz); |
| 63 | if (e >= 0) |
| 64 | return (1); |
| 65 | if (e == PCRE_ERROR_NOMATCH) |
| 66 | return (0); |
| 67 | die("unexpected PCRE error code %d", e); |
| 68 | return (-1); |
| 69 | } |
| 70 | |
| 71 | /* --- Node creation --- */ |
| 72 | |
| 73 | node *pcrenode(const char *const *av) |
| 74 | { |
| 75 | node_pcre *n = xmalloc(sizeof(*n)); |
| 76 | const char *e; |
| 77 | int eo; |
| 78 | int c; |
| 79 | |
| 80 | n->n.func = n_pcre; |
| 81 | if ((n->rx = pcre_compile(av[0], PCRE_CASELESS, &e, &eo, 0)) == 0) |
| 82 | die("bad regular expression `%s': %s", av[0], e); |
| 83 | n->rx_study = pcre_study(n->rx, 0, &e); |
| 84 | if (e) |
| 85 | die("error studying pattern `%s': %s", av[0], e); |
| 86 | pcre_fullinfo(n->rx, n->rx_study, PCRE_INFO_BACKREFMAX, &c); |
| 87 | n->ovecsz = c * 2; |
| 88 | n->ovec = xmalloc(n->ovecsz * sizeof(*n->ovec)); |
| 89 | return (&n->n); |
| 90 | } |
| 91 | |
| 92 | /*----- That's all, folks -------------------------------------------------*/ |
| 93 | |
| 94 | #endif |