5 * (c) 2001 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Anag: a simple wordgame helper.
12 * Anag is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Anag is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Anag; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Static variables --------------------------------------------------*/
33 static const char *file = DICTIONARY;
35 /*----- Help text functions -----------------------------------------------*/
37 static void usage(FILE *fp)
39 pquis(fp, "Usage: $ [-f file] expression\n");
42 static void version(FILE *fp)
44 pquis(fp, "$, version " VERSION "\n");
47 static void help(FILE *fp)
53 Searches a wordlist, printing all of the words which match an expression.\n\
55 Options supported are:\n\
57 -h, --help display this help text\n\
58 -v, --version display the program's version number\n\
59 -u, --usage display a very brief usage message\n\
60 -f, --file FILE read wordlist from FILE, not `" DICTIONARY "'\n\
62 The basic tests in the expression are:\n\
64 -anagram WORD matches a full-length anagram\n\
65 -subgram WORD matches words which only use letters in WORD\n\
66 -wildcard PATTERN matches with wildcards `*' and `?'\n\
67 -trackword WORD matches words which can be found in a trackword\n\
68 -mono PATTERN matches words isomorphic to the given PATTERN\n\
72 -regexp REGEXP matches with an (extended) regular expression\n\
75 #if defined(HAVE_PCRE) || defined(HAVE_PCRE2)
77 -pcre REGEXP matches with a Perl-like regular expression\n\
81 -length [+|-]N matches if length is [at least|at most] N\n\
82 -longest output longest matches found here\n\
83 -shortest output shortest matches found here\n\
85 These simple tests can be combined using the operators `-a', `-o' and `-n'\n\
86 (for `and', `or' and `not'; they may also be written `&', `|' and `!' if\n\
87 you like), and grouped using parentheses `(' and `)'.\n\
91 /*----- The options parser ------------------------------------------------*/
93 /* --- Options table structure --- */
103 O_HELP, O_VERSION, O_USAGE,
105 O_AND, O_OR, O_NOT, O_LPAREN, O_RPAREN,
106 O_ANAG, O_SUBG, O_WILD, O_TRACK, O_REGEXP, O_PCRE, O_MONO, O_LENGTH,
107 O_LONGEST, O_SHORTEST,
113 static const struct opt opttab[] = {
115 /* --- Options -- don't form part of the language --- */
117 { "help", 0, OF_SHORT, O_HELP },
118 { "version", 0, OF_SHORT, O_VERSION },
119 { "usage", 0, OF_SHORT, O_USAGE },
120 { "file", 1, OF_SHORT, O_FILE },
122 /* --- Operators -- provide the basic structure of the language --- *
124 * These are also given magical names by the parser.
127 { "and", 0, OF_SHORT, O_AND },
128 { "or", 0, OF_SHORT, O_OR },
129 { "not", 0, OF_SHORT, O_NOT },
131 /* --- Actual matching operations -- do something useful --- */
133 { "anagram", 1, 0, O_ANAG },
134 { "subgram", 1, 0, O_SUBG },
135 { "wildcard", 1, 0, O_WILD },
136 { "trackword", 1, 0, O_TRACK },
137 { "mono", 1, 0, O_MONO },
139 { "regexp", 1, 0, O_REGEXP },
141 #if defined(HAVE_PCRE) || defined(HAVE_PCRE2)
142 { "pcre", 1, 0, O_PCRE },
144 { "length", 1, 0, O_LENGTH },
145 { "longest", 0, 0, O_LONGEST },
146 { "shortest", 0, 0, O_SHORTEST },
148 /* --- End marker --- */
154 static const char *const *av;
157 /* --- @nextopt@ --- *
159 * Arguments: @const char ***arg@ = where to store the arg pointer
161 * Returns: The tag of the next option.
163 * Use: Scans the next option off the command line. If the option
164 * doesn't form part of the language, it's processed internally,
165 * and you'll never see it from here. On exit, the @arg@
166 * pointer is set to contain the address of the option scanned,
167 * followed by its arguments if any. You're expected to know
168 * how many arguments there are for your option.
171 static unsigned nextopt(const char *const **arg)
174 const struct opt *o, *oo;
178 /* --- Pick the next option off the front --- */
185 /* --- Cope with various forms of magic --- */
188 if (!p[1]) switch (*p) {
189 case '&': return (O_AND);
190 case '|': return (O_OR);
191 case '!': return (O_NOT);
192 case '(': return (O_LPAREN);
193 case ')': return (O_RPAREN);
198 /* --- Now cope with other sorts of weirdies --- *
200 * By the end of this, a leading `-' or `--' will have been stripped.
210 die("syntax error near `--': rubbish at end of line");
214 /* --- Now look the word up in my table --- */
218 for (o = opttab; o->name; o++) {
219 if (strncmp(p, o->name, sz) == 0) {
220 if (strlen(o->name) == sz || ((o->f & OF_SHORT) && sz == 1)) {
225 die("ambiguous option name `-%s' (could match `-%s' or `-%s')",
226 p, oo->name, o->name);
232 die("unrecognized option name `-%s'", p);
234 /* --- Sort out the arguments --- */
236 if (ai + oo->nargs > ac)
237 die("too few arguments for `-%s' (need %u)", oo->name, oo->nargs);
240 /* --- Now process the option --- */
260 die("syntax error near `%s': unknown token type", av[ai - 1]);
264 /*----- Node types for operators ------------------------------------------*/
266 /* --- Node structures --- */
268 typedef struct node_bin {
274 typedef struct node_un {
279 /* --- Node functions --- */
281 static int n_or(node *nn, const char *p, size_t sz)
283 node_bin *n = (node_bin *)nn;
284 return (n->left->func(n->left, p, sz) || n->right->func(n->right, p, sz));
287 static int n_and(node *nn, const char *p, size_t sz)
289 node_bin *n = (node_bin *)nn;
290 return (n->left->func(n->left, p, sz) && n->right->func(n->right, p, sz));
293 static int n_not(node *nn, const char *p, size_t sz)
295 node_un *n = (node_un *)nn;
296 return (!n->arg->func(n->arg, p, sz));
299 /*----- Other simple node types -------------------------------------------*/
301 enum { LESS = -1, EQUAL = 0, GREATER = 1 };
303 typedef struct node_numeric {
309 static void parse_numeric(const char *p, int *dir, int *i)
316 case '-': *dir = LESS; pp++; break;
317 case '+': *dir = GREATER; pp++; break;
318 default: *dir = EQUAL; break;
321 l = strtol(pp, &q, 0);
322 if (*q || errno || l < INT_MIN || l > INT_MAX)
323 die("bad numeric parameter `%s'", p);
327 static node *make_numeric(const char *const *av,
328 int (*func)(struct node *, const char *, size_t))
330 node_numeric *n = xmalloc(sizeof(*n));
331 parse_numeric(av[0], &n->dir, &n->i);
336 static int cmp_numeric(int x, int dir, int n)
339 case LESS: return (x <= n);
340 case EQUAL: return (x == n);
341 case GREATER: return (x >= n);
346 static int n_length(node *nn, const char *p, size_t sz)
348 node_numeric *n = (node_numeric *)nn;
349 return (cmp_numeric(sz, n->dir, n->i));
352 /*----- Parser for the expression syntax ----------------------------------*/
354 /* --- A parser context --- */
356 typedef struct p_ctx {
358 const char *const *a;
361 /* --- Parser structure --- *
363 * This is a simple recursive descent parser. The context retains
364 * information about the current token. Each function is passed the address
365 * of a node pointer to fill in. This simplifies the binary operator code
366 * somewhat, relative to returning pointers to node trees.
369 static void p_expr(p_ctx *p, node **/*nn*/);
371 static void p_next(p_ctx *p)
373 static const char *const eof[] = { "<end>", 0 };
374 p->t = nextopt(&p->a);
379 static void p_factor(p_ctx *p, node **nn)
382 if (p->t == O_LPAREN) {
385 if (p->t != O_RPAREN)
386 die("syntax error near `%s': missing `)'", *p->a);
388 } else if (p->t == O_NOT) {
389 n = xmalloc(sizeof(node_un));
393 p_factor(p, &n->arg);
396 case O_ANAG: *nn = anagram(p->a + 1); break;
397 case O_SUBG: *nn = subgram(p->a + 1); break;
398 case O_WILD: *nn = wildcard(p->a + 1); break;
399 case O_TRACK: *nn = trackword(p->a + 1); break;
401 case O_REGEXP: *nn = regexp(p->a + 1); break;
403 #if defined(HAVE_PCRE) || defined(HAVE_PCRE2)
404 case O_PCRE: *nn = pcrenode(p->a + 1); break;
406 case O_MONO: *nn = mono(p->a + 1); break;
407 case O_LENGTH: *nn = make_numeric(p->a + 1, n_length); break;
408 case O_LONGEST: *nn = longest(p->a + 1); break;
409 case O_SHORTEST: *nn = shortest(p->a + 1); break;
410 default: die("syntax error near `%s': unexpected token", *p->a);
416 static void p_term(p_ctx *p, node **nn)
431 n = xmalloc(sizeof(node_bin));
439 static void p_expr(p_ctx *p, node **nn)
447 n = xmalloc(sizeof(node_bin));
455 /* --- @p_argv@ --- *
457 * Arguments: @int argc@ = number of command-line arguments
458 * @const char *const argv[]@ = vectoor of arguments
460 * Returns: A compiled node, parsed from the arguments.
462 * Use: Does the donkey-work of parsing a command-line.
465 static node *p_argv(int argc, const char *const argv[])
476 pquis(stderr, "(Run `$ --help' for more detail.)\n");
481 die("syntax error near `%s': rubbish at end of line (too many `)'s?)",
487 /*----- At-end stuff ------------------------------------------------------*/
489 /* --- @atend_register@ --- *
491 * Arguments: @int (*func)(void *)@ = function to call
492 * @void *p@ = handle to pass to it
496 * Use: Adds a function to the list of things to do at the end of the
497 * program. The function should return nonzero if it produced
501 typedef struct atend {
503 int (*func)(void */*p*/);
507 static atend *aa_head = 0, **aa_tail = &aa_head;
509 void atend_register(int (*func)(void */*p*/), void *p)
511 atend *a = xmalloc(sizeof(*a));
519 /*----- Main code ---------------------------------------------------------*/
523 * Arguments: @int argc@ = number of command-line arguments
524 * @char *argv[]@ = vector of argument words
526 * Returns: Zero on success, nonzero on failure.
528 * Use: Picks entries from a word list which match particular
529 * expressions. This might be of assistance to word-game types.
532 int main(int argc, char *argv[])
542 n = p_argv(argc, (const char *const *)argv);
544 if ((fp = fopen(file, "r")) == 0)
545 die("error opening `%s': %s", file, strerror(errno));
548 if (dstr_putline(&d, fp) < 0)
551 for (p = q = d.buf; p < l; p++) {
552 if (!isalnum((unsigned char)*p))
554 *q++ = tolower((unsigned char)*p);
558 if (n->func(n, d.buf, d.len)) {
559 fwrite(d.buf, 1, d.len, stdout);
564 if (ferror(fp) || fclose(fp))
565 die("error reading `%s': %s", file, strerror(errno));
566 for (a = aa_head; a; a = a->next) {
570 if (fflush(stdout) || ferror(stdout) || fclose(stdout))
571 die("error writing output: %s", strerror(errno));
572 if (!ok) pquis(stderr, "$: no matches found\n");
573 return (ok ? EX_OK : EX_NONE);
576 /*----- That's all, folks -------------------------------------------------*/