d9af4a2b |
1 | /* -*-c-*- |
2 | * |
d9af4a2b |
3 | * Matches Perl-compatible regular expressions |
4 | * |
5 | * (c) 2002 Mark Wooding |
6 | */ |
7 | |
0279756e |
8 | /*----- Licensing notice --------------------------------------------------* |
d9af4a2b |
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. |
0279756e |
16 | * |
d9af4a2b |
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. |
0279756e |
21 | * |
d9af4a2b |
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 | |
d9af4a2b |
27 | /*----- Header files ------------------------------------------------------*/ |
28 | |
29 | #ifdef HAVE_CONFIG_H |
30 | # include "config.h" |
31 | #endif |
32 | |
33 | #ifndef HAVE_PCRE |
34 | extern int dummy; |
35 | #else |
36 | |
37 | #include "anag.h" |
38 | #include <pcre.h> |
39 | |
40 | /*----- Data structures ---------------------------------------------------*/ |
41 | |
42 | typedef struct node_pcre { |
43 | node n; |
44 | const char *s; |
45 | pcre *rx; |
46 | pcre_extra *rx_study; |
47 | int *ovec; |
48 | int ovecsz; |
49 | } node_pcre; |
50 | |
51 | /*----- Main code ---------------------------------------------------------*/ |
52 | |
53 | /* --- Node matcher --- */ |
54 | |
55 | static int n_pcre(node *nn, const char *p, size_t sz) |
56 | { |
57 | node_pcre *n = (node_pcre *)nn; |
58 | int e; |
59 | |
60 | e = pcre_exec(n->rx, n->rx_study, p, sz, 0, 0, n->ovec, n->ovecsz); |
61 | if (e >= 0) |
62 | return (1); |
63 | if (e == PCRE_ERROR_NOMATCH) |
64 | return (0); |
65 | die("unexpected PCRE error code %d", e); |
66 | return (-1); |
67 | } |
68 | |
69 | /* --- Node creation --- */ |
70 | |
71 | node *pcrenode(const char *const *av) |
72 | { |
73 | node_pcre *n = xmalloc(sizeof(*n)); |
74 | const char *e; |
75 | int eo; |
76 | int c; |
77 | |
78 | n->n.func = n_pcre; |
79 | if ((n->rx = pcre_compile(av[0], PCRE_CASELESS, &e, &eo, 0)) == 0) |
80 | die("bad regular expression `%s': %s", av[0], e); |
81 | n->rx_study = pcre_study(n->rx, 0, &e); |
82 | if (e) |
83 | die("error studying pattern `%s': %s", av[0], e); |
84 | pcre_fullinfo(n->rx, n->rx_study, PCRE_INFO_BACKREFMAX, &c); |
85 | n->ovecsz = c * 2; |
86 | n->ovec = xmalloc(n->ovecsz * sizeof(*n->ovec)); |
87 | return (&n->n); |
88 | } |
89 | |
90 | /*----- That's all, folks -------------------------------------------------*/ |
91 | |
92 | #endif |