chiark / gitweb /
Monoalphabetic match filter. Simplify the button list.
[anag] / anag.c
CommitLineData
6e403221 1/* -*-c-*-
2 *
fe9969ff 3 * $Id: anag.c,v 1.6 2003/09/15 02:48:54 mdw Exp $
6e403221 4 *
5 * Main driver for anag
6 *
7 * (c) 2001 Mark Wooding
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Anag: a simple wordgame helper.
13 *
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.
18 *
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.
23 *
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.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: anag.c,v $
fe9969ff 32 * Revision 1.6 2003/09/15 02:48:54 mdw
33 * Monoalphabetic match filter.
34 *
a10122de 35 * Revision 1.5 2002/08/11 12:58:09 mdw
36 * Added support for regular expression matching, if supported by the C
37 * library.
38 *
1d2d1062 39 * Revision 1.4 2001/02/19 19:18:50 mdw
40 * Minor big fixes to parser.
41 *
2668675c 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.
45 *
60dffc01 46 * Revision 1.2 2001/02/07 09:09:11 mdw
47 * Fix spurious error when `-file' is used.
48 *
6e403221 49 * Revision 1.1 2001/02/04 17:14:42 mdw
50 * Initial checkin
51 *
52 */
53
54/*----- Header files ------------------------------------------------------*/
55
56#include "anag.h"
57
58/*----- Static variables --------------------------------------------------*/
59
60static const char *file = DICTIONARY;
61
62/*----- Help text functions -----------------------------------------------*/
63
64static void usage(FILE *fp)
65{
66 pquis(fp, "Usage: $ [-f file] expression\n");
67}
68
69static void version(FILE *fp)
70{
71 pquis(fp, "$, version " VERSION "\n");
72}
73
74static void help(FILE *fp)
75{
76 version(fp);
77 fputc('\n', fp);
78 usage(fp);
79 fputs("\n\
80Searches a wordlist, printing all of the words which match an expression.\n\
2668675c 81\n\
82Options supported are:\n\
83\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\
88\n\
6e403221 89The basic tests in the expression are:\n\
90\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\
fe9969ff 95-mono PATTERN matches words isomorphic to the given PATTERN\n\
a10122de 96"
97#ifdef HAVE_REGCOMP
98"\
99-regexp REGEXP matches with an (extended) regular expression\n\
100"
101#endif
102"\
6e403221 103\n\
104These 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\
106you like), and grouped using parentheses `(' and `)'.\n\
107", fp);
108}
109
110/*----- The options parser ------------------------------------------------*/
111
112/* --- Options table structure --- */
113
114struct opt {
115 const char *name;
116 unsigned nargs;
117 unsigned f;
118 unsigned tag;
119};
120
121enum {
122 O_HELP, O_VERSION, O_USAGE,
123 O_FILE,
124 O_AND, O_OR, O_NOT, O_LPAREN, O_RPAREN,
fe9969ff 125 O_ANAG, O_SUBG, O_WILD, O_TRACK, O_REGEXP, O_MONO,
6e403221 126 O_EOF
127};
128
129#define OF_SHORT 1u
130
131static const struct opt opttab[] = {
132
133 /* --- Options -- don't form part of the language --- */
134
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 },
139
140 /* --- Operators -- provide the basic structure of the language --- *
141 *
142 * These are also given magical names by the parser.
143 */
144
145 { "and", 0, OF_SHORT, O_AND },
146 { "or", 0, OF_SHORT, O_OR },
147 { "not", 0, OF_SHORT, O_NOT },
148
149 /* --- Actual matching oeprations -- do something useful --- */
150
151 { "anagram", 1, 0, O_ANAG },
152 { "subgram", 1, 0, O_SUBG },
153 { "wildcard", 1, 0, O_WILD },
154 { "trackword", 1, 0, O_TRACK },
fe9969ff 155 { "mono", 1, 0, O_MONO },
a10122de 156#ifdef HAVE_REGCOMP
157 { "regexp", 1, 0, O_REGEXP },
158#endif
6e403221 159
160 /* --- End marker --- */
161
162 { 0, 0, 0, 0 }
163};
164
165static int ac;
166static const char *const *av;
167static int ai;
168
169/* --- @nextopt@ --- *
170 *
171 * Arguments: @const char ***arg@ = where to store the arg pointer
172 *
173 * Returns: The tag of the next option.
174 *
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.
181 */
182
183static unsigned nextopt(const char *const **arg)
184{
185 for (;;) {
186 const struct opt *o, *oo;
187 size_t sz;
188 const char *p;
189
190 /* --- Pick the next option off the front --- */
191
192 *arg = av + ai;
193 if (ai >= ac)
194 return (O_EOF);
195 p = av[ai++];
196
197 /* --- Cope with various forms of magic --- */
198
199 if (p[0] != '-') {
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);
206 }
207 goto bad;
208 }
209
210 /* --- Now cope with other sorts of weirdies --- *
211 *
212 * By the end of this, a leading `-' or `--' will have been stripped.
213 */
214
215 p++;
216 if (!*p)
217 goto bad;
218 if (*p == '-')
219 p++;
220 if (!*p) {
221 if (ai < ac)
222 die("syntax error near `--': rubbish at end of line");
223 return (O_EOF);
224 }
225
226 /* --- Now look the word up in my table --- */
227
228 sz = strlen(p);
229 oo = 0;
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)) {
233 oo = o;
234 break;
235 }
236 if (oo) {
237 die("ambiguous option name `-%s' (could match `-%s' or `-%s')",
238 p, oo->name, o->name);
239 }
240 oo = o;
241 }
242 }
243 if (!oo)
244 die("unrecognized option name `-%s'", p);
245
246 /* --- Sort out the arguments --- */
247
248 if (ai + oo->nargs > ac)
249 die("too few arguments for `-%s' (need %u)", oo->name, oo->nargs);
250 ai += oo->nargs;
251
252 /* --- Now process the option --- */
253
254 switch (oo->tag) {
255 case O_HELP:
256 help(stdout);
257 exit(0);
258 case O_VERSION:
259 version(stdout);
260 exit(0);
261 case O_USAGE:
262 usage(stdout);
263 exit(0);
264 case O_FILE:
265 file = (*arg)[1];
266 break;
267 default:
268 return (oo->tag);
269 }
60dffc01 270 continue;
6e403221 271 bad:
272 die("syntax error near `%s': unknown token type", av[ai - 1]);
273 }
274}
275
276/*----- Node types for operators ------------------------------------------*/
277
278/* --- Node structures --- */
279
280typedef struct node_bin {
281 node n;
282 node *left;
283 node *right;
284} node_bin;
285
286typedef struct node_un {
287 node n;
288 node *arg;
289} node_un;
290
291/* --- Node functions --- */
292
293static int n_or(node *nn, const char *p, size_t sz)
294{
295 node_bin *n = (node_bin *)nn;
296 return (n->left->func(n->left, p, sz) || n->right->func(n->right, p, sz));
297}
298
299static int n_and(node *nn, const char *p, size_t sz)
300{
301 node_bin *n = (node_bin *)nn;
302 return (n->left->func(n->left, p, sz) && n->right->func(n->right, p, sz));
303}
304
305static int n_not(node *nn, const char *p, size_t sz)
306{
307 node_un *n = (node_un *)nn;
308 return (!n->arg->func(n->arg, p, sz));
309}
310
311/*----- Parser for the expression syntax ----------------------------------*/
312
313/* --- A parser context --- */
314
315typedef struct p_ctx {
316 unsigned t;
317 const char *const *a;
318} p_ctx;
319
320/* --- Parser structure --- *
321 *
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.
326 */
327
328static void p_expr(p_ctx *p, node **/*nn*/);
329
330static void p_next(p_ctx *p)
331{
332 static const char *const eof[] = { "<end>", 0 };
333 p->t = nextopt(&p->a);
334 if (p->t == O_EOF)
335 p->a = eof;
336}
337
338static void p_factor(p_ctx *p, node **nn)
339{
340 node_un *n;
341 if (p->t == O_LPAREN) {
342 p_next(p);
343 p_expr(p, nn);
344 if (p->t != O_RPAREN)
1d2d1062 345 die("syntax error near `%s': missing `)'", *p->a);
6e403221 346 p_next(p);
347 } else if (p->t == O_NOT) {
348 n = xmalloc(sizeof(node_un));
349 n->n.func = n_not;
350 *nn = &n->n;
351 p_next(p);
352 p_factor(p, &n->arg);
353 } else {
354 switch (p->t) {
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;
a10122de 359#ifdef HAVE_REGCOMP
360 case O_REGEXP: *nn = regexp(p->a + 1); break;
361#endif
fe9969ff 362 case O_MONO: *nn = mono(p->a + 1); break;
6e403221 363 default: die("syntax error near `%s': unexpected token", *p->a);
364 }
365 p_next(p);
366 }
367}
368
369static void p_term(p_ctx *p, node **nn)
370{
371 node_bin *n;
372 for (;;) {
373 p_factor(p, nn);
374 switch (p->t) {
375 case O_AND:
376 p_next(p);
377 default:
378 break;
6e403221 379 case O_RPAREN:
380 case O_OR:
381 case O_EOF:
382 return;
383 }
384 n = xmalloc(sizeof(node_bin));
385 n->left = *nn;
386 n->n.func = n_and;
387 *nn = &n->n;
388 nn = &n->right;
389 }
390}
391
392static void p_expr(p_ctx *p, node **nn)
393{
394 node_bin *n;
395 for (;;) {
396 p_term(p, nn);
397 if (p->t != O_OR)
398 break;
399 p_next(p);
400 n = xmalloc(sizeof(node_bin));
401 n->left = *nn;
402 n->n.func = n_or;
403 *nn = &n->n;
404 nn = &n->right;
405 }
406}
407
408/* --- @p_argv@ --- *
409 *
410 * Arguments: @int argc@ = number of command-line arguments
411 * @const char *const argv[]@ = vectoor of arguments
412 *
413 * Returns: A compiled node, parsed from the arguments.
414 *
415 * Use: Does the donkey-work of parsing a command-line.
416 */
417
418static node *p_argv(int argc, const char *const argv[])
419{
420 p_ctx p;
421 node *n;
422
423 av = argv;
424 ac = argc;
425 ai = 1;
426 p_next(&p);
2668675c 427 if (p.t == O_EOF) {
428 usage(stderr);
429 pquis(stderr, "(Run `$ --help' for more detail.)\n");
430 exit(EXIT_FAILURE);
431 }
6e403221 432 p_expr(&p, &n);
433 if (p.t != O_EOF) {
434 die("syntax error near `%s': rubbish at end of line (too many `)'s?)",
435 *p.a);
436 }
437 return (n);
438}
439
440/*----- Main code ---------------------------------------------------------*/
441
442/* --- @main@ --- *
443 *
444 * Arguments: @int argc@ = number of command-line arguments
445 * @char *argv[]@ = vector of argument words
446 *
447 * Returns: Zero on success, nonzero on failure.
448 *
449 * Use: Picks entries from a word list which match particular
450 * expressions. This might be of assistance to word-game types.
451 */
452
453int main(int argc, char *argv[])
454{
455 node *n;
456 FILE *fp;
457 dstr d = DSTR_INIT;
458 char *p, *q, *l;
459
460 ego(argv[0]);
461 n = p_argv(argc, (const char *const *)argv);
462
463 if ((fp = fopen(file, "r")) == 0)
464 die("error opening `%s': %s", file, strerror(errno));
465 for (;;) {
466 dstr_reset(&d);
467 if (dstr_putline(&d, fp) < 0)
468 break;
469 l = d.buf + d.len;
470 for (p = q = d.buf; p < l; p++) {
471 if (!isalnum((unsigned char)*p))
472 continue;
473 *q++ = tolower((unsigned char)*p);
474 }
475 *q = 0;
476 d.len = q - d.buf;
477 if (n->func(n, d.buf, d.len)) {
478 fwrite(d.buf, 1, d.len, stdout);
479 fputc('\n', stdout);
480 }
481 }
482 if (!feof(fp))
483 die("error reading `%s': %s", file, strerror(errno));
484 fclose(fp);
485 return (0);
486}
487
488/*----- That's all, folks -------------------------------------------------*/