X-Git-Url: https://git.distorted.org.uk/~mdw/anag/blobdiff_plain/de6b7015b91d370f83e4189c05393eefceab0678..d9af4a2b674e5ab91df0f9af236283b617bb81c7:/pcre.c diff --git a/pcre.c b/pcre.c new file mode 100644 index 0000000..89ae8b4 --- /dev/null +++ b/pcre.c @@ -0,0 +1,102 @@ +/* -*-c-*- + * + * $Id: pcre.c,v 1.1 2003/11/29 23:38:37 mdw Exp $ + * + * Matches Perl-compatible regular expressions + * + * (c) 2002 Mark Wooding + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Anag: a simple wordgame helper. + * + * Anag is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Anag is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Anag; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: pcre.c,v $ + * Revision 1.1 2003/11/29 23:38:37 mdw + * Debianization. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifndef HAVE_PCRE + extern int dummy; +#else + +#include "anag.h" +#include + +/*----- Data structures ---------------------------------------------------*/ + +typedef struct node_pcre { + node n; + const char *s; + pcre *rx; + pcre_extra *rx_study; + int *ovec; + int ovecsz; +} node_pcre; + +/*----- Main code ---------------------------------------------------------*/ + +/* --- Node matcher --- */ + +static int n_pcre(node *nn, const char *p, size_t sz) +{ + node_pcre *n = (node_pcre *)nn; + int e; + + e = pcre_exec(n->rx, n->rx_study, p, sz, 0, 0, n->ovec, n->ovecsz); + if (e >= 0) + return (1); + if (e == PCRE_ERROR_NOMATCH) + return (0); + die("unexpected PCRE error code %d", e); + return (-1); +} + +/* --- Node creation --- */ + +node *pcrenode(const char *const *av) +{ + node_pcre *n = xmalloc(sizeof(*n)); + const char *e; + int eo; + int c; + + n->n.func = n_pcre; + if ((n->rx = pcre_compile(av[0], PCRE_CASELESS, &e, &eo, 0)) == 0) + die("bad regular expression `%s': %s", av[0], e); + n->rx_study = pcre_study(n->rx, 0, &e); + if (e) + die("error studying pattern `%s': %s", av[0], e); + pcre_fullinfo(n->rx, n->rx_study, PCRE_INFO_BACKREFMAX, &c); + n->ovecsz = c * 2; + n->ovec = xmalloc(n->ovecsz * sizeof(*n->ovec)); + return (&n->n); +} + +/*----- That's all, folks -------------------------------------------------*/ + +#endif