3 * $Id: anag.c,v 1.6 2003/09/15 02:48:54 mdw Exp $
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Anag: a simple wordgame helper.
14 * Anag is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Anag is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Anag; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.6 2003/09/15 02:48:54 mdw
33 * Monoalphabetic match filter.
35 * Revision 1.5 2002/08/11 12:58:09 mdw
36 * Added support for regular expression matching, if supported by the C
39 * Revision 1.4 2001/02/19 19:18:50 mdw
40 * Minor big fixes to parser.
42 * Revision 1.3 2001/02/16 21:45:19 mdw
43 * Be more helpful. Improve full help message. Special-case error for
44 * empty command strings.
46 * Revision 1.2 2001/02/07 09:09:11 mdw
47 * Fix spurious error when `-file' is used.
49 * Revision 1.1 2001/02/04 17:14:42 mdw
54 /*----- Header files ------------------------------------------------------*/
58 /*----- Static variables --------------------------------------------------*/
60 static const char *file = DICTIONARY;
62 /*----- Help text functions -----------------------------------------------*/
64 static void usage(FILE *fp)
66 pquis(fp, "Usage: $ [-f file] expression\n");
69 static void version(FILE *fp)
71 pquis(fp, "$, version " VERSION "\n");
74 static void help(FILE *fp)
80 Searches a wordlist, printing all of the words which match an expression.\n\
82 Options supported are:\n\
84 -h, --help display this help text\n\
85 -v, --version display the program's version number\n\
86 -u, --usage display a very brief usage message\n\
87 -f, --file FILE read wordlist from FILE, not `" DICTIONARY "'\n\
89 The basic tests in the expression are:\n\
91 -anagram WORD matches a full-length anagram\n\
92 -subgram WORD matches words which only use letters in WORD\n\
93 -wildcard PATTERN matches with wildcards `*' and `?'\n\
94 -trackword WORD matches words which can be found in a trackword\n\
95 -mono PATTERN matches words isomorphic to the given PATTERN\n\
99 -regexp REGEXP matches with an (extended) regular expression\n\
104 These simple tests can be combined using the operators `-a', `-o' and `-n'\n\
105 (for `and', `or' and `not'; they may also be written `&', `|' and `!' if\n\
106 you like), and grouped using parentheses `(' and `)'.\n\
110 /*----- The options parser ------------------------------------------------*/
112 /* --- Options table structure --- */
122 O_HELP, O_VERSION, O_USAGE,
124 O_AND, O_OR, O_NOT, O_LPAREN, O_RPAREN,
125 O_ANAG, O_SUBG, O_WILD, O_TRACK, O_REGEXP, O_MONO,
131 static const struct opt opttab[] = {
133 /* --- Options -- don't form part of the language --- */
135 { "help", 0, OF_SHORT, O_HELP },
136 { "version", 0, OF_SHORT, O_VERSION },
137 { "usage", 0, OF_SHORT, O_USAGE },
138 { "file", 1, OF_SHORT, O_FILE },
140 /* --- Operators -- provide the basic structure of the language --- *
142 * These are also given magical names by the parser.
145 { "and", 0, OF_SHORT, O_AND },
146 { "or", 0, OF_SHORT, O_OR },
147 { "not", 0, OF_SHORT, O_NOT },
149 /* --- Actual matching oeprations -- do something useful --- */
151 { "anagram", 1, 0, O_ANAG },
152 { "subgram", 1, 0, O_SUBG },
153 { "wildcard", 1, 0, O_WILD },
154 { "trackword", 1, 0, O_TRACK },
155 { "mono", 1, 0, O_MONO },
157 { "regexp", 1, 0, O_REGEXP },
160 /* --- End marker --- */
166 static const char *const *av;
169 /* --- @nextopt@ --- *
171 * Arguments: @const char ***arg@ = where to store the arg pointer
173 * Returns: The tag of the next option.
175 * Use: Scans the next option off the command line. If the option
176 * doesn't form part of the language, it's processed internally,
177 * and you'll never see it from here. On exit, the @arg@
178 * pointer is set to contain the address of the option scanned,
179 * followed by its arguments if any. You're expected to know
180 * how many arguments there are for your option.
183 static unsigned nextopt(const char *const **arg)
186 const struct opt *o, *oo;
190 /* --- Pick the next option off the front --- */
197 /* --- Cope with various forms of magic --- */
200 if (!p[1]) switch (*p) {
201 case '&': return (O_AND);
202 case '|': return (O_OR);
203 case '!': return (O_NOT);
204 case '(': return (O_LPAREN);
205 case ')': return (O_RPAREN);
210 /* --- Now cope with other sorts of weirdies --- *
212 * By the end of this, a leading `-' or `--' will have been stripped.
222 die("syntax error near `--': rubbish at end of line");
226 /* --- Now look the word up in my table --- */
230 for (o = opttab; o->name; o++) {
231 if (strncmp(p, o->name, sz) == 0) {
232 if (strlen(o->name) == sz || ((o->f & OF_SHORT) && sz == 1)) {
237 die("ambiguous option name `-%s' (could match `-%s' or `-%s')",
238 p, oo->name, o->name);
244 die("unrecognized option name `-%s'", p);
246 /* --- Sort out the arguments --- */
248 if (ai + oo->nargs > ac)
249 die("too few arguments for `-%s' (need %u)", oo->name, oo->nargs);
252 /* --- Now process the option --- */
272 die("syntax error near `%s': unknown token type", av[ai - 1]);
276 /*----- Node types for operators ------------------------------------------*/
278 /* --- Node structures --- */
280 typedef struct node_bin {
286 typedef struct node_un {
291 /* --- Node functions --- */
293 static int n_or(node *nn, const char *p, size_t sz)
295 node_bin *n = (node_bin *)nn;
296 return (n->left->func(n->left, p, sz) || n->right->func(n->right, p, sz));
299 static int n_and(node *nn, const char *p, size_t sz)
301 node_bin *n = (node_bin *)nn;
302 return (n->left->func(n->left, p, sz) && n->right->func(n->right, p, sz));
305 static int n_not(node *nn, const char *p, size_t sz)
307 node_un *n = (node_un *)nn;
308 return (!n->arg->func(n->arg, p, sz));
311 /*----- Parser for the expression syntax ----------------------------------*/
313 /* --- A parser context --- */
315 typedef struct p_ctx {
317 const char *const *a;
320 /* --- Parser structure --- *
322 * This is a simple recursive descent parser. The context retains
323 * information about the current token. Each function is passed the address
324 * of a node pointer to fill in. This simplifies the binary operator code
325 * somewhat, relative to returning pointers to node trees.
328 static void p_expr(p_ctx *p, node **/*nn*/);
330 static void p_next(p_ctx *p)
332 static const char *const eof[] = { "<end>", 0 };
333 p->t = nextopt(&p->a);
338 static void p_factor(p_ctx *p, node **nn)
341 if (p->t == O_LPAREN) {
344 if (p->t != O_RPAREN)
345 die("syntax error near `%s': missing `)'", *p->a);
347 } else if (p->t == O_NOT) {
348 n = xmalloc(sizeof(node_un));
352 p_factor(p, &n->arg);
355 case O_ANAG: *nn = anagram(p->a + 1); break;
356 case O_SUBG: *nn = subgram(p->a + 1); break;
357 case O_WILD: *nn = wildcard(p->a + 1); break;
358 case O_TRACK: *nn = trackword(p->a + 1); break;
360 case O_REGEXP: *nn = regexp(p->a + 1); break;
362 case O_MONO: *nn = mono(p->a + 1); break;
363 default: die("syntax error near `%s': unexpected token", *p->a);
369 static void p_term(p_ctx *p, node **nn)
384 n = xmalloc(sizeof(node_bin));
392 static void p_expr(p_ctx *p, node **nn)
400 n = xmalloc(sizeof(node_bin));
408 /* --- @p_argv@ --- *
410 * Arguments: @int argc@ = number of command-line arguments
411 * @const char *const argv[]@ = vectoor of arguments
413 * Returns: A compiled node, parsed from the arguments.
415 * Use: Does the donkey-work of parsing a command-line.
418 static node *p_argv(int argc, const char *const argv[])
429 pquis(stderr, "(Run `$ --help' for more detail.)\n");
434 die("syntax error near `%s': rubbish at end of line (too many `)'s?)",
440 /*----- Main code ---------------------------------------------------------*/
444 * Arguments: @int argc@ = number of command-line arguments
445 * @char *argv[]@ = vector of argument words
447 * Returns: Zero on success, nonzero on failure.
449 * Use: Picks entries from a word list which match particular
450 * expressions. This might be of assistance to word-game types.
453 int main(int argc, char *argv[])
461 n = p_argv(argc, (const char *const *)argv);
463 if ((fp = fopen(file, "r")) == 0)
464 die("error opening `%s': %s", file, strerror(errno));
467 if (dstr_putline(&d, fp) < 0)
470 for (p = q = d.buf; p < l; p++) {
471 if (!isalnum((unsigned char)*p))
473 *q++ = tolower((unsigned char)*p);
477 if (n->func(n, d.buf, d.len)) {
478 fwrite(d.buf, 1, d.len, stdout);
483 die("error reading `%s': %s", file, strerror(errno));
488 /*----- That's all, folks -------------------------------------------------*/