configure.ac, Makefile.am, pcre.c, regexp.c: Overhaul conditional building.
[anag] / pcre.c
CommitLineData
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
d9af4a2b 29#include "anag.h"
7c2b74ab 30
d9af4a2b 31#include <pcre.h>
32
33/*----- Data structures ---------------------------------------------------*/
34
35typedef struct node_pcre {
36 node n;
37 const char *s;
38 pcre *rx;
39 pcre_extra *rx_study;
40 int *ovec;
41 int ovecsz;
42} node_pcre;
43
44/*----- Main code ---------------------------------------------------------*/
45
46/* --- Node matcher --- */
47
48static int n_pcre(node *nn, const char *p, size_t sz)
49{
50 node_pcre *n = (node_pcre *)nn;
51 int e;
52
53 e = pcre_exec(n->rx, n->rx_study, p, sz, 0, 0, n->ovec, n->ovecsz);
7c2b74ab
MW
54 if (e >= 0) return (1);
55 if (e == PCRE_ERROR_NOMATCH) return (0);
d9af4a2b 56 die("unexpected PCRE error code %d", e);
d9af4a2b 57}
58
59/* --- Node creation --- */
60
61node *pcrenode(const char *const *av)
62{
63 node_pcre *n = xmalloc(sizeof(*n));
64 const char *e;
65 int eo;
66 int c;
67
68 n->n.func = n_pcre;
7c2b74ab
MW
69
70 n->rx = pcre_compile(av[0], PCRE_CASELESS, &e, &eo, 0);
71 if (!n->rx) die("bad regular expression `%s': %s", av[0], e);
d9af4a2b 72 n->rx_study = pcre_study(n->rx, 0, &e);
7c2b74ab
MW
73 if (e) die("error studying pattern `%s': %s", av[0], e);
74 pcre_fullinfo(n->rx, n->rx_study, PCRE_INFO_CAPTURECOUNT, &c);
75 n->ovecsz = 2*c;
76 n->ovec = xmalloc(n->ovecsz*sizeof(*n->ovec));
77
d9af4a2b 78 return (&n->n);
79}
80
81/*----- That's all, folks -------------------------------------------------*/