anag.h: Mark `die' as non-returning and accepting a `printf' format.
[anag] / regexp.c
CommitLineData
a10122de 1/* -*-c-*-
2 *
a10122de 3 * Matches regular expressions
4 *
5 * (c) 2002 Mark Wooding
6 */
7
0279756e 8/*----- Licensing notice --------------------------------------------------*
a10122de 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 *
a10122de 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 *
a10122de 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
a10122de 27/*----- Header files ------------------------------------------------------*/
28
29#ifdef HAVE_CONFIG_H
30# include "config.h"
31#endif
32
33#ifndef HAVE_REGCOMP
34 extern int dummy;
35#else
36
37#include "anag.h"
38#include <regex.h>
39
40/*----- Data structures ---------------------------------------------------*/
41
42typedef struct node_regexp {
43 node n;
44 const char *s;
45 regex_t rx;
46} node_regexp;
47
48/*----- Main code ---------------------------------------------------------*/
49
50/* --- Node matcher --- */
51
52static int n_regexp(node *nn, const char *p, size_t sz)
53{
54 node_regexp *n = (node_regexp *)nn;
55 int e;
56
57 switch (e = regexec(&n->rx, p, 0, 0, 0)) {
58 case 0:
59 return 1;
60 case REG_NOMATCH:
61 return 0;
62 default: {
63 char buf[256];
64 regerror(e, &n->rx, buf, sizeof(buf));
65 die("error matching regexp `%s' against `%s': %s",
66 n->s, p, buf);
67 } break;
68 }
69 return (0);
70}
71
72/* --- Node creation --- */
73
74node *regexp(const char *const *av)
75{
76 node_regexp *n = xmalloc(sizeof(*n));
77 int e;
78 n->n.func = n_regexp;
79 if ((e = regcomp(&n->rx, av[0],
80 REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
81 char buf[256];
82 regerror(e, &n->rx, buf, sizeof(buf));
83 die("bad regular expression `%s': %s", av[0], buf);
84 }
85 return (&n->n);
86}
87
88/*----- That's all, folks -------------------------------------------------*/
89
90#endif