From a10122de085ddf5c8bec6fc12c7164ad1dc1459c Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Sun, 11 Aug 2002 12:58:09 +0000 Subject: [PATCH 1/1] Added support for regular expression matching, if supported by the C library. Organization: Straylight/Edgeware From: mdw --- AnagGUI.java | 12 +++++- anag.c | 21 ++++++++++- anag.h | 7 +++- regexp.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 regexp.c diff --git a/AnagGUI.java b/AnagGUI.java index 1aee359..bf51835 100644 --- a/AnagGUI.java +++ b/AnagGUI.java @@ -1,6 +1,6 @@ /* -*-java-*- * - * $Id: AnagGUI.java,v 1.4 2001/02/19 19:19:11 mdw Exp $ + * $Id: AnagGUI.java,v 1.5 2002/08/11 12:58:09 mdw Exp $ * * Front-end GUI * @@ -29,6 +29,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: AnagGUI.java,v $ + * Revision 1.5 2002/08/11 12:58:09 mdw + * Added support for regular expression matching, if supported by the C + * library. + * * Revision 1.4 2001/02/19 19:19:11 mdw * Add `help' button. Lowercase input to the command. * @@ -258,6 +262,12 @@ class AnagPanel extends Panel { }); add(b, g); + b = new Button("Regexp"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { getlist("-regexp"); } + }); + add(b, g); + b = new Button("Trackword"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { getlist("-trackword"); } diff --git a/anag.c b/anag.c index 939f6b8..6be2a4e 100644 --- a/anag.c +++ b/anag.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: anag.c,v 1.4 2001/02/19 19:18:50 mdw Exp $ + * $Id: anag.c,v 1.5 2002/08/11 12:58:09 mdw Exp $ * * Main driver for anag * @@ -29,6 +29,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: anag.c,v $ + * Revision 1.5 2002/08/11 12:58:09 mdw + * Added support for regular expression matching, if supported by the C + * library. + * * Revision 1.4 2001/02/19 19:18:50 mdw * Minor big fixes to parser. * @@ -85,6 +89,13 @@ The basic tests in the expression are:\n\ -subgram WORD matches words which only use letters in WORD\n\ -wildcard PATTERN matches with wildcards `*' and `?'\n\ -trackword WORD matches words which can be found in a trackword\n\ +" +#ifdef HAVE_REGCOMP +"\ +-regexp REGEXP matches with an (extended) regular expression\n\ +" +#endif +"\ \n\ These simple tests can be combined using the operators `-a', `-o' and `-n'\n\ (for `and', `or' and `not'; they may also be written `&', `|' and `!' if\n\ @@ -107,7 +118,7 @@ enum { O_HELP, O_VERSION, O_USAGE, O_FILE, O_AND, O_OR, O_NOT, O_LPAREN, O_RPAREN, - O_ANAG, O_SUBG, O_WILD, O_TRACK, + O_ANAG, O_SUBG, O_WILD, O_TRACK, O_REGEXP, O_EOF }; @@ -137,6 +148,9 @@ static const struct opt opttab[] = { { "subgram", 1, 0, O_SUBG }, { "wildcard", 1, 0, O_WILD }, { "trackword", 1, 0, O_TRACK }, +#ifdef HAVE_REGCOMP + { "regexp", 1, 0, O_REGEXP }, +#endif /* --- End marker --- */ @@ -337,6 +351,9 @@ static void p_factor(p_ctx *p, node **nn) case O_SUBG: *nn = subgram(p->a + 1); break; case O_WILD: *nn = wildcard(p->a + 1); break; case O_TRACK: *nn = trackword(p->a + 1); break; +#ifdef HAVE_REGCOMP + case O_REGEXP: *nn = regexp(p->a + 1); break; +#endif default: die("syntax error near `%s': unexpected token", *p->a); } p_next(p); diff --git a/anag.h b/anag.h index 5f2b8e3..97c4bef 100644 --- a/anag.h +++ b/anag.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: anag.h,v 1.1 2001/02/04 17:14:42 mdw Exp $ + * $Id: anag.h,v 1.2 2002/08/11 12:58:09 mdw Exp $ * * External definitions for Anag * @@ -29,6 +29,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: anag.h,v $ + * Revision 1.2 2002/08/11 12:58:09 mdw + * Added support for regular expression matching, if supported by the C + * library. + * * Revision 1.1 2001/02/04 17:14:42 mdw * Initial checkin * @@ -74,6 +78,7 @@ extern node *anagram(const char *const */*av*/); extern node *subgram(const char *const */*av*/); extern node *wildcard(const char *const */*av*/); extern node *trackword(const char *const */*av*/); +extern node *regexp(const char *const */*av*/); /*----- Error reporting ---------------------------------------------------*/ diff --git a/regexp.c b/regexp.c new file mode 100644 index 0000000..24c40e3 --- /dev/null +++ b/regexp.c @@ -0,0 +1,104 @@ +/* -*-c-*- + * + * $Id: regexp.c,v 1.1 2002/08/11 12:58:09 mdw Exp $ + * + * Matches 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: regexp.c,v $ + * Revision 1.1 2002/08/11 12:58:09 mdw + * Added support for regular expression matching, if supported by the C + * library. + * + * Revision 1.1 2001/02/04 17:14:42 mdw + * Initial checkin + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifndef HAVE_REGCOMP + extern int dummy; +#else + +#include "anag.h" +#include + +/*----- Data structures ---------------------------------------------------*/ + +typedef struct node_regexp { + node n; + const char *s; + regex_t rx; +} node_regexp; + +/*----- Main code ---------------------------------------------------------*/ + +/* --- Node matcher --- */ + +static int n_regexp(node *nn, const char *p, size_t sz) +{ + node_regexp *n = (node_regexp *)nn; + int e; + + switch (e = regexec(&n->rx, p, 0, 0, 0)) { + case 0: + return 1; + case REG_NOMATCH: + return 0; + default: { + char buf[256]; + regerror(e, &n->rx, buf, sizeof(buf)); + die("error matching regexp `%s' against `%s': %s", + n->s, p, buf); + } break; + } + return (0); +} + +/* --- Node creation --- */ + +node *regexp(const char *const *av) +{ + node_regexp *n = xmalloc(sizeof(*n)); + int e; + n->n.func = n_regexp; + if ((e = regcomp(&n->rx, av[0], + REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) { + char buf[256]; + regerror(e, &n->rx, buf, sizeof(buf)); + die("bad regular expression `%s': %s", av[0], buf); + } + return (&n->n); +} + +/*----- That's all, folks -------------------------------------------------*/ + +#endif -- [mdw]