3 * $Id: anag.c,v 1.2 2001/02/07 09:09:11 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.2 2001/02/07 09:09:11 mdw
33 * Fix spurious error when `-file' is used.
35 * Revision 1.1 2001/02/04 17:14:42 mdw
40 /*----- Header files ------------------------------------------------------*/
44 /*----- Static variables --------------------------------------------------*/
46 static const char *file = DICTIONARY;
48 /*----- Help text functions -----------------------------------------------*/
50 static void usage(FILE *fp)
52 pquis(fp, "Usage: $ [-f file] expression\n");
55 static void version(FILE *fp)
57 pquis(fp, "$, version " VERSION "\n");
60 static void help(FILE *fp)
66 Searches a wordlist, printing all of the words which match an expression.\n\
67 The basic tests in the expression are:\n\
69 -anagram WORD matches a full-length anagram\n\
70 -subgram WORD matches words which only use letters in WORD\n\
71 -wildcard PATTERN matches with wildcards `*' and `?'\n\
72 -trackword WORD matches words which can be found in a trackword\n\
74 These simple tests can be combined using the operators `-a', `-o' and `-n'\n\
75 (for `and', `or' and `not'; they may also be written `&', `|' and `!' if\n\
76 you like), and grouped using parentheses `(' and `)'.\n\
80 /*----- The options parser ------------------------------------------------*/
82 /* --- Options table structure --- */
92 O_HELP, O_VERSION, O_USAGE,
94 O_AND, O_OR, O_NOT, O_LPAREN, O_RPAREN,
95 O_ANAG, O_SUBG, O_WILD, O_TRACK,
101 static const struct opt opttab[] = {
103 /* --- Options -- don't form part of the language --- */
105 { "help", 0, OF_SHORT, O_HELP },
106 { "version", 0, OF_SHORT, O_VERSION },
107 { "usage", 0, OF_SHORT, O_USAGE },
108 { "file", 1, OF_SHORT, O_FILE },
110 /* --- Operators -- provide the basic structure of the language --- *
112 * These are also given magical names by the parser.
115 { "and", 0, OF_SHORT, O_AND },
116 { "or", 0, OF_SHORT, O_OR },
117 { "not", 0, OF_SHORT, O_NOT },
119 /* --- Actual matching oeprations -- do something useful --- */
121 { "anagram", 1, 0, O_ANAG },
122 { "subgram", 1, 0, O_SUBG },
123 { "wildcard", 1, 0, O_WILD },
124 { "trackword", 1, 0, O_TRACK },
126 /* --- End marker --- */
132 static const char *const *av;
135 /* --- @nextopt@ --- *
137 * Arguments: @const char ***arg@ = where to store the arg pointer
139 * Returns: The tag of the next option.
141 * Use: Scans the next option off the command line. If the option
142 * doesn't form part of the language, it's processed internally,
143 * and you'll never see it from here. On exit, the @arg@
144 * pointer is set to contain the address of the option scanned,
145 * followed by its arguments if any. You're expected to know
146 * how many arguments there are for your option.
149 static unsigned nextopt(const char *const **arg)
152 const struct opt *o, *oo;
156 /* --- Pick the next option off the front --- */
163 /* --- Cope with various forms of magic --- */
166 if (!p[1]) switch (*p) {
167 case '&': return (O_AND);
168 case '|': return (O_OR);
169 case '!': return (O_NOT);
170 case '(': return (O_LPAREN);
171 case ')': return (O_RPAREN);
176 /* --- Now cope with other sorts of weirdies --- *
178 * By the end of this, a leading `-' or `--' will have been stripped.
188 die("syntax error near `--': rubbish at end of line");
192 /* --- Now look the word up in my table --- */
196 for (o = opttab; o->name; o++) {
197 if (strncmp(p, o->name, sz) == 0) {
198 if (strlen(o->name) == sz || ((o->f & OF_SHORT) && sz == 1)) {
203 die("ambiguous option name `-%s' (could match `-%s' or `-%s')",
204 p, oo->name, o->name);
210 die("unrecognized option name `-%s'", p);
212 /* --- Sort out the arguments --- */
214 if (ai + oo->nargs > ac)
215 die("too few arguments for `-%s' (need %u)", oo->name, oo->nargs);
218 /* --- Now process the option --- */
238 die("syntax error near `%s': unknown token type", av[ai - 1]);
242 /*----- Node types for operators ------------------------------------------*/
244 /* --- Node structures --- */
246 typedef struct node_bin {
252 typedef struct node_un {
257 /* --- Node functions --- */
259 static int n_or(node *nn, const char *p, size_t sz)
261 node_bin *n = (node_bin *)nn;
262 return (n->left->func(n->left, p, sz) || n->right->func(n->right, p, sz));
265 static int n_and(node *nn, const char *p, size_t sz)
267 node_bin *n = (node_bin *)nn;
268 return (n->left->func(n->left, p, sz) && n->right->func(n->right, p, sz));
271 static int n_not(node *nn, const char *p, size_t sz)
273 node_un *n = (node_un *)nn;
274 return (!n->arg->func(n->arg, p, sz));
277 /*----- Parser for the expression syntax ----------------------------------*/
279 /* --- A parser context --- */
281 typedef struct p_ctx {
283 const char *const *a;
286 /* --- Parser structure --- *
288 * This is a simple recursive descent parser. The context retains
289 * information about the current token. Each function is passed the address
290 * of a node pointer to fill in. This simplifies the binary operator code
291 * somewhat, relative to returning pointers to node trees.
294 static void p_expr(p_ctx *p, node **/*nn*/);
296 static void p_next(p_ctx *p)
298 static const char *const eof[] = { "<end>", 0 };
299 p->t = nextopt(&p->a);
304 static void p_factor(p_ctx *p, node **nn)
307 if (p->t == O_LPAREN) {
310 if (p->t != O_RPAREN)
311 die("syntax error near `%s': missing `('", *p->a);
313 } else if (p->t == O_NOT) {
314 n = xmalloc(sizeof(node_un));
318 p_factor(p, &n->arg);
321 case O_ANAG: *nn = anagram(p->a + 1); break;
322 case O_SUBG: *nn = subgram(p->a + 1); break;
323 case O_WILD: *nn = wildcard(p->a + 1); break;
324 case O_TRACK: *nn = trackword(p->a + 1); break;
325 default: die("syntax error near `%s': unexpected token", *p->a);
331 static void p_term(p_ctx *p, node **nn)
347 n = xmalloc(sizeof(node_bin));
355 static void p_expr(p_ctx *p, node **nn)
363 n = xmalloc(sizeof(node_bin));
371 /* --- @p_argv@ --- *
373 * Arguments: @int argc@ = number of command-line arguments
374 * @const char *const argv[]@ = vectoor of arguments
376 * Returns: A compiled node, parsed from the arguments.
378 * Use: Does the donkey-work of parsing a command-line.
381 static node *p_argv(int argc, const char *const argv[])
392 die("syntax error near `%s': rubbish at end of line (too many `)'s?)",
398 /*----- Main code ---------------------------------------------------------*/
402 * Arguments: @int argc@ = number of command-line arguments
403 * @char *argv[]@ = vector of argument words
405 * Returns: Zero on success, nonzero on failure.
407 * Use: Picks entries from a word list which match particular
408 * expressions. This might be of assistance to word-game types.
411 int main(int argc, char *argv[])
419 n = p_argv(argc, (const char *const *)argv);
421 if ((fp = fopen(file, "r")) == 0)
422 die("error opening `%s': %s", file, strerror(errno));
425 if (dstr_putline(&d, fp) < 0)
428 for (p = q = d.buf; p < l; p++) {
429 if (!isalnum((unsigned char)*p))
431 *q++ = tolower((unsigned char)*p);
435 if (n->func(n, d.buf, d.len)) {
436 fwrite(d.buf, 1, d.len, stdout);
441 die("error reading `%s': %s", file, strerror(errno));
446 /*----- That's all, folks -------------------------------------------------*/