6e403221 |
1 | /* -*-c-*- |
2 | * |
60dffc01 |
3 | * $Id: anag.c,v 1.2 2001/02/07 09:09:11 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 $ |
60dffc01 |
32 | * Revision 1.2 2001/02/07 09:09:11 mdw |
33 | * Fix spurious error when `-file' is used. |
34 | * |
6e403221 |
35 | * Revision 1.1 2001/02/04 17:14:42 mdw |
36 | * Initial checkin |
37 | * |
38 | */ |
39 | |
40 | /*----- Header files ------------------------------------------------------*/ |
41 | |
42 | #include "anag.h" |
43 | |
44 | /*----- Static variables --------------------------------------------------*/ |
45 | |
46 | static const char *file = DICTIONARY; |
47 | |
48 | /*----- Help text functions -----------------------------------------------*/ |
49 | |
50 | static void usage(FILE *fp) |
51 | { |
52 | pquis(fp, "Usage: $ [-f file] expression\n"); |
53 | } |
54 | |
55 | static void version(FILE *fp) |
56 | { |
57 | pquis(fp, "$, version " VERSION "\n"); |
58 | } |
59 | |
60 | static void help(FILE *fp) |
61 | { |
62 | version(fp); |
63 | fputc('\n', fp); |
64 | usage(fp); |
65 | fputs("\n\ |
66 | Searches a wordlist, printing all of the words which match an expression.\n\ |
67 | The basic tests in the expression are:\n\ |
68 | \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\ |
73 | \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\ |
77 | ", fp); |
78 | } |
79 | |
80 | /*----- The options parser ------------------------------------------------*/ |
81 | |
82 | /* --- Options table structure --- */ |
83 | |
84 | struct opt { |
85 | const char *name; |
86 | unsigned nargs; |
87 | unsigned f; |
88 | unsigned tag; |
89 | }; |
90 | |
91 | enum { |
92 | O_HELP, O_VERSION, O_USAGE, |
93 | O_FILE, |
94 | O_AND, O_OR, O_NOT, O_LPAREN, O_RPAREN, |
95 | O_ANAG, O_SUBG, O_WILD, O_TRACK, |
96 | O_EOF |
97 | }; |
98 | |
99 | #define OF_SHORT 1u |
100 | |
101 | static const struct opt opttab[] = { |
102 | |
103 | /* --- Options -- don't form part of the language --- */ |
104 | |
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 }, |
109 | |
110 | /* --- Operators -- provide the basic structure of the language --- * |
111 | * |
112 | * These are also given magical names by the parser. |
113 | */ |
114 | |
115 | { "and", 0, OF_SHORT, O_AND }, |
116 | { "or", 0, OF_SHORT, O_OR }, |
117 | { "not", 0, OF_SHORT, O_NOT }, |
118 | |
119 | /* --- Actual matching oeprations -- do something useful --- */ |
120 | |
121 | { "anagram", 1, 0, O_ANAG }, |
122 | { "subgram", 1, 0, O_SUBG }, |
123 | { "wildcard", 1, 0, O_WILD }, |
124 | { "trackword", 1, 0, O_TRACK }, |
125 | |
126 | /* --- End marker --- */ |
127 | |
128 | { 0, 0, 0, 0 } |
129 | }; |
130 | |
131 | static int ac; |
132 | static const char *const *av; |
133 | static int ai; |
134 | |
135 | /* --- @nextopt@ --- * |
136 | * |
137 | * Arguments: @const char ***arg@ = where to store the arg pointer |
138 | * |
139 | * Returns: The tag of the next option. |
140 | * |
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. |
147 | */ |
148 | |
149 | static unsigned nextopt(const char *const **arg) |
150 | { |
151 | for (;;) { |
152 | const struct opt *o, *oo; |
153 | size_t sz; |
154 | const char *p; |
155 | |
156 | /* --- Pick the next option off the front --- */ |
157 | |
158 | *arg = av + ai; |
159 | if (ai >= ac) |
160 | return (O_EOF); |
161 | p = av[ai++]; |
162 | |
163 | /* --- Cope with various forms of magic --- */ |
164 | |
165 | if (p[0] != '-') { |
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); |
172 | } |
173 | goto bad; |
174 | } |
175 | |
176 | /* --- Now cope with other sorts of weirdies --- * |
177 | * |
178 | * By the end of this, a leading `-' or `--' will have been stripped. |
179 | */ |
180 | |
181 | p++; |
182 | if (!*p) |
183 | goto bad; |
184 | if (*p == '-') |
185 | p++; |
186 | if (!*p) { |
187 | if (ai < ac) |
188 | die("syntax error near `--': rubbish at end of line"); |
189 | return (O_EOF); |
190 | } |
191 | |
192 | /* --- Now look the word up in my table --- */ |
193 | |
194 | sz = strlen(p); |
195 | oo = 0; |
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)) { |
199 | oo = o; |
200 | break; |
201 | } |
202 | if (oo) { |
203 | die("ambiguous option name `-%s' (could match `-%s' or `-%s')", |
204 | p, oo->name, o->name); |
205 | } |
206 | oo = o; |
207 | } |
208 | } |
209 | if (!oo) |
210 | die("unrecognized option name `-%s'", p); |
211 | |
212 | /* --- Sort out the arguments --- */ |
213 | |
214 | if (ai + oo->nargs > ac) |
215 | die("too few arguments for `-%s' (need %u)", oo->name, oo->nargs); |
216 | ai += oo->nargs; |
217 | |
218 | /* --- Now process the option --- */ |
219 | |
220 | switch (oo->tag) { |
221 | case O_HELP: |
222 | help(stdout); |
223 | exit(0); |
224 | case O_VERSION: |
225 | version(stdout); |
226 | exit(0); |
227 | case O_USAGE: |
228 | usage(stdout); |
229 | exit(0); |
230 | case O_FILE: |
231 | file = (*arg)[1]; |
232 | break; |
233 | default: |
234 | return (oo->tag); |
235 | } |
60dffc01 |
236 | continue; |
6e403221 |
237 | bad: |
238 | die("syntax error near `%s': unknown token type", av[ai - 1]); |
239 | } |
240 | } |
241 | |
242 | /*----- Node types for operators ------------------------------------------*/ |
243 | |
244 | /* --- Node structures --- */ |
245 | |
246 | typedef struct node_bin { |
247 | node n; |
248 | node *left; |
249 | node *right; |
250 | } node_bin; |
251 | |
252 | typedef struct node_un { |
253 | node n; |
254 | node *arg; |
255 | } node_un; |
256 | |
257 | /* --- Node functions --- */ |
258 | |
259 | static int n_or(node *nn, const char *p, size_t sz) |
260 | { |
261 | node_bin *n = (node_bin *)nn; |
262 | return (n->left->func(n->left, p, sz) || n->right->func(n->right, p, sz)); |
263 | } |
264 | |
265 | static int n_and(node *nn, const char *p, size_t sz) |
266 | { |
267 | node_bin *n = (node_bin *)nn; |
268 | return (n->left->func(n->left, p, sz) && n->right->func(n->right, p, sz)); |
269 | } |
270 | |
271 | static int n_not(node *nn, const char *p, size_t sz) |
272 | { |
273 | node_un *n = (node_un *)nn; |
274 | return (!n->arg->func(n->arg, p, sz)); |
275 | } |
276 | |
277 | /*----- Parser for the expression syntax ----------------------------------*/ |
278 | |
279 | /* --- A parser context --- */ |
280 | |
281 | typedef struct p_ctx { |
282 | unsigned t; |
283 | const char *const *a; |
284 | } p_ctx; |
285 | |
286 | /* --- Parser structure --- * |
287 | * |
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. |
292 | */ |
293 | |
294 | static void p_expr(p_ctx *p, node **/*nn*/); |
295 | |
296 | static void p_next(p_ctx *p) |
297 | { |
298 | static const char *const eof[] = { "<end>", 0 }; |
299 | p->t = nextopt(&p->a); |
300 | if (p->t == O_EOF) |
301 | p->a = eof; |
302 | } |
303 | |
304 | static void p_factor(p_ctx *p, node **nn) |
305 | { |
306 | node_un *n; |
307 | if (p->t == O_LPAREN) { |
308 | p_next(p); |
309 | p_expr(p, nn); |
310 | if (p->t != O_RPAREN) |
311 | die("syntax error near `%s': missing `('", *p->a); |
312 | p_next(p); |
313 | } else if (p->t == O_NOT) { |
314 | n = xmalloc(sizeof(node_un)); |
315 | n->n.func = n_not; |
316 | *nn = &n->n; |
317 | p_next(p); |
318 | p_factor(p, &n->arg); |
319 | } else { |
320 | switch (p->t) { |
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); |
326 | } |
327 | p_next(p); |
328 | } |
329 | } |
330 | |
331 | static void p_term(p_ctx *p, node **nn) |
332 | { |
333 | node_bin *n; |
334 | for (;;) { |
335 | p_factor(p, nn); |
336 | switch (p->t) { |
337 | case O_AND: |
338 | p_next(p); |
339 | default: |
340 | break; |
341 | case O_LPAREN: |
342 | case O_RPAREN: |
343 | case O_OR: |
344 | case O_EOF: |
345 | return; |
346 | } |
347 | n = xmalloc(sizeof(node_bin)); |
348 | n->left = *nn; |
349 | n->n.func = n_and; |
350 | *nn = &n->n; |
351 | nn = &n->right; |
352 | } |
353 | } |
354 | |
355 | static void p_expr(p_ctx *p, node **nn) |
356 | { |
357 | node_bin *n; |
358 | for (;;) { |
359 | p_term(p, nn); |
360 | if (p->t != O_OR) |
361 | break; |
362 | p_next(p); |
363 | n = xmalloc(sizeof(node_bin)); |
364 | n->left = *nn; |
365 | n->n.func = n_or; |
366 | *nn = &n->n; |
367 | nn = &n->right; |
368 | } |
369 | } |
370 | |
371 | /* --- @p_argv@ --- * |
372 | * |
373 | * Arguments: @int argc@ = number of command-line arguments |
374 | * @const char *const argv[]@ = vectoor of arguments |
375 | * |
376 | * Returns: A compiled node, parsed from the arguments. |
377 | * |
378 | * Use: Does the donkey-work of parsing a command-line. |
379 | */ |
380 | |
381 | static node *p_argv(int argc, const char *const argv[]) |
382 | { |
383 | p_ctx p; |
384 | node *n; |
385 | |
386 | av = argv; |
387 | ac = argc; |
388 | ai = 1; |
389 | p_next(&p); |
390 | p_expr(&p, &n); |
391 | if (p.t != O_EOF) { |
392 | die("syntax error near `%s': rubbish at end of line (too many `)'s?)", |
393 | *p.a); |
394 | } |
395 | return (n); |
396 | } |
397 | |
398 | /*----- Main code ---------------------------------------------------------*/ |
399 | |
400 | /* --- @main@ --- * |
401 | * |
402 | * Arguments: @int argc@ = number of command-line arguments |
403 | * @char *argv[]@ = vector of argument words |
404 | * |
405 | * Returns: Zero on success, nonzero on failure. |
406 | * |
407 | * Use: Picks entries from a word list which match particular |
408 | * expressions. This might be of assistance to word-game types. |
409 | */ |
410 | |
411 | int main(int argc, char *argv[]) |
412 | { |
413 | node *n; |
414 | FILE *fp; |
415 | dstr d = DSTR_INIT; |
416 | char *p, *q, *l; |
417 | |
418 | ego(argv[0]); |
419 | n = p_argv(argc, (const char *const *)argv); |
420 | |
421 | if ((fp = fopen(file, "r")) == 0) |
422 | die("error opening `%s': %s", file, strerror(errno)); |
423 | for (;;) { |
424 | dstr_reset(&d); |
425 | if (dstr_putline(&d, fp) < 0) |
426 | break; |
427 | l = d.buf + d.len; |
428 | for (p = q = d.buf; p < l; p++) { |
429 | if (!isalnum((unsigned char)*p)) |
430 | continue; |
431 | *q++ = tolower((unsigned char)*p); |
432 | } |
433 | *q = 0; |
434 | d.len = q - d.buf; |
435 | if (n->func(n, d.buf, d.len)) { |
436 | fwrite(d.buf, 1, d.len, stdout); |
437 | fputc('\n', stdout); |
438 | } |
439 | } |
440 | if (!feof(fp)) |
441 | die("error reading `%s': %s", file, strerror(errno)); |
442 | fclose(fp); |
443 | return (0); |
444 | } |
445 | |
446 | /*----- That's all, folks -------------------------------------------------*/ |