3 * $Id: anag.c,v 1.1 2001/02/04 17:14:42 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.1 2001/02/04 17:14:42 mdw
37 /*----- Header files ------------------------------------------------------*/
41 /*----- Static variables --------------------------------------------------*/
43 static const char *file
= DICTIONARY
;
45 /*----- Help text functions -----------------------------------------------*/
47 static void usage(FILE *fp
)
49 pquis(fp
, "Usage: $ [-f file] expression\n");
52 static void version(FILE *fp
)
54 pquis(fp
, "$, version " VERSION
"\n");
57 static void help(FILE *fp
)
63 Searches a wordlist, printing all of the words which match an expression.\n\
64 The basic tests in the expression are:\n\
66 -anagram WORD matches a full-length anagram\n\
67 -subgram WORD matches words which only use letters in WORD\n\
68 -wildcard PATTERN matches with wildcards `*' and `?'\n\
69 -trackword WORD matches words which can be found in a trackword\n\
71 These simple tests can be combined using the operators `-a', `-o' and `-n'\n\
72 (for `and', `or' and `not'; they may also be written `&', `|' and `!' if\n\
73 you like), and grouped using parentheses `(' and `)'.\n\
77 /*----- The options parser ------------------------------------------------*/
79 /* --- Options table structure --- */
89 O_HELP
, O_VERSION
, O_USAGE
,
91 O_AND
, O_OR
, O_NOT
, O_LPAREN
, O_RPAREN
,
92 O_ANAG
, O_SUBG
, O_WILD
, O_TRACK
,
98 static const struct opt opttab
[] = {
100 /* --- Options -- don't form part of the language --- */
102 { "help", 0, OF_SHORT
, O_HELP
},
103 { "version", 0, OF_SHORT
, O_VERSION
},
104 { "usage", 0, OF_SHORT
, O_USAGE
},
105 { "file", 1, OF_SHORT
, O_FILE
},
107 /* --- Operators -- provide the basic structure of the language --- *
109 * These are also given magical names by the parser.
112 { "and", 0, OF_SHORT
, O_AND
},
113 { "or", 0, OF_SHORT
, O_OR
},
114 { "not", 0, OF_SHORT
, O_NOT
},
116 /* --- Actual matching oeprations -- do something useful --- */
118 { "anagram", 1, 0, O_ANAG
},
119 { "subgram", 1, 0, O_SUBG
},
120 { "wildcard", 1, 0, O_WILD
},
121 { "trackword", 1, 0, O_TRACK
},
123 /* --- End marker --- */
129 static const char *const *av
;
132 /* --- @nextopt@ --- *
134 * Arguments: @const char ***arg@ = where to store the arg pointer
136 * Returns: The tag of the next option.
138 * Use: Scans the next option off the command line. If the option
139 * doesn't form part of the language, it's processed internally,
140 * and you'll never see it from here. On exit, the @arg@
141 * pointer is set to contain the address of the option scanned,
142 * followed by its arguments if any. You're expected to know
143 * how many arguments there are for your option.
146 static unsigned nextopt(const char *const **arg
)
149 const struct opt
*o
, *oo
;
153 /* --- Pick the next option off the front --- */
160 /* --- Cope with various forms of magic --- */
163 if (!p
[1]) switch (*p
) {
164 case '&': return (O_AND
);
165 case '|': return (O_OR
);
166 case '!': return (O_NOT
);
167 case '(': return (O_LPAREN
);
168 case ')': return (O_RPAREN
);
173 /* --- Now cope with other sorts of weirdies --- *
175 * By the end of this, a leading `-' or `--' will have been stripped.
185 die("syntax error near `--': rubbish at end of line");
189 /* --- Now look the word up in my table --- */
193 for (o
= opttab
; o
->name
; o
++) {
194 if (strncmp(p
, o
->name
, sz
) == 0) {
195 if (strlen(o
->name
) == sz
|| ((o
->f
& OF_SHORT
) && sz
== 1)) {
200 die("ambiguous option name `-%s' (could match `-%s' or `-%s')",
201 p
, oo
->name
, o
->name
);
207 die("unrecognized option name `-%s'", p
);
209 /* --- Sort out the arguments --- */
211 if (ai
+ oo
->nargs
> ac
)
212 die("too few arguments for `-%s' (need %u)", oo
->name
, oo
->nargs
);
215 /* --- Now process the option --- */
234 die("syntax error near `%s': unknown token type", av
[ai
- 1]);
238 /*----- Node types for operators ------------------------------------------*/
240 /* --- Node structures --- */
242 typedef struct node_bin
{
248 typedef struct node_un
{
253 /* --- Node functions --- */
255 static int n_or(node
*nn
, const char *p
, size_t sz
)
257 node_bin
*n
= (node_bin
*)nn
;
258 return (n
->left
->func(n
->left
, p
, sz
) || n
->right
->func(n
->right
, p
, sz
));
261 static int n_and(node
*nn
, const char *p
, size_t sz
)
263 node_bin
*n
= (node_bin
*)nn
;
264 return (n
->left
->func(n
->left
, p
, sz
) && n
->right
->func(n
->right
, p
, sz
));
267 static int n_not(node
*nn
, const char *p
, size_t sz
)
269 node_un
*n
= (node_un
*)nn
;
270 return (!n
->arg
->func(n
->arg
, p
, sz
));
273 /*----- Parser for the expression syntax ----------------------------------*/
275 /* --- A parser context --- */
277 typedef struct p_ctx
{
279 const char *const *a
;
282 /* --- Parser structure --- *
284 * This is a simple recursive descent parser. The context retains
285 * information about the current token. Each function is passed the address
286 * of a node pointer to fill in. This simplifies the binary operator code
287 * somewhat, relative to returning pointers to node trees.
290 static void p_expr(p_ctx
*p
, node
**/
*nn*/
);
292 static void p_next(p_ctx
*p
)
294 static const char *const eof
[] = { "<end>", 0 };
295 p
->t
= nextopt(&p
->a
);
300 static void p_factor(p_ctx
*p
, node
**nn
)
303 if (p
->t
== O_LPAREN
) {
306 if (p
->t
!= O_RPAREN
)
307 die("syntax error near `%s': missing `('", *p
->a
);
309 } else if (p
->t
== O_NOT
) {
310 n
= xmalloc(sizeof(node_un
));
314 p_factor(p
, &n
->arg
);
317 case O_ANAG
: *nn
= anagram(p
->a
+ 1); break;
318 case O_SUBG
: *nn
= subgram(p
->a
+ 1); break;
319 case O_WILD
: *nn
= wildcard(p
->a
+ 1); break;
320 case O_TRACK
: *nn
= trackword(p
->a
+ 1); break;
321 default: die("syntax error near `%s': unexpected token", *p
->a
);
327 static void p_term(p_ctx
*p
, node
**nn
)
343 n
= xmalloc(sizeof(node_bin
));
351 static void p_expr(p_ctx
*p
, node
**nn
)
359 n
= xmalloc(sizeof(node_bin
));
367 /* --- @p_argv@ --- *
369 * Arguments: @int argc@ = number of command-line arguments
370 * @const char *const argv[]@ = vectoor of arguments
372 * Returns: A compiled node, parsed from the arguments.
374 * Use: Does the donkey-work of parsing a command-line.
377 static node
*p_argv(int argc
, const char *const argv
[])
388 die("syntax error near `%s': rubbish at end of line (too many `)'s?)",
394 /*----- Main code ---------------------------------------------------------*/
398 * Arguments: @int argc@ = number of command-line arguments
399 * @char *argv[]@ = vector of argument words
401 * Returns: Zero on success, nonzero on failure.
403 * Use: Picks entries from a word list which match particular
404 * expressions. This might be of assistance to word-game types.
407 int main(int argc
, char *argv
[])
415 n
= p_argv(argc
, (const char *const *)argv
);
417 if ((fp
= fopen(file
, "r")) == 0)
418 die("error opening `%s': %s", file
, strerror(errno
));
421 if (dstr_putline(&d
, fp
) < 0)
424 for (p
= q
= d
.buf
; p
< l
; p
++) {
425 if (!isalnum((unsigned char)*p
))
427 *q
++ = tolower((unsigned char)*p
);
431 if (n
->func(n
, d
.buf
, d
.len
)) {
432 fwrite(d
.buf
, 1, d
.len
, stdout
);
437 die("error reading `%s': %s", file
, strerror(errno
));
442 /*----- That's all, folks -------------------------------------------------*/