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
)
38 { pquis(fp
, "Usage: $ [-f file] expression\n"); }
40 static void version(FILE *fp
)
41 { pquis(fp
, "$, version " VERSION
"\n"); }
43 static void help(FILE *fp
)
49 Searches a wordlist, printing all of the words which match an expression.\n\
51 Options supported are:\n\
53 -h, --help display this help text\n\
54 -v, --version display the program's version number\n\
55 -u, --usage display a very brief usage message\n\
56 -f, --file FILE read wordlist from FILE, not `" DICTIONARY
"'\n\
58 The basic tests in the expression are:\n\
60 -anagram WORD matches a full-length anagram\n\
61 -subgram WORD matches words which only use letters in WORD\n\
62 -wildcard PATTERN matches with wildcards `*' and `?'\n\
63 -trackword WORD matches words which can be found in a trackword\n\
64 -mono PATTERN matches words isomorphic to the given PATTERN\n\
68 -regexp REGEXP matches with an (extended) regular expression\n\
71 #if defined(HAVE_PCRE) || defined(HAVE_PCRE2)
73 -pcre REGEXP matches with a Perl-like regular expression\n\
77 -length [+|-]N matches if length is [at least|at most] N\n\
78 -longest output longest matches found here\n\
79 -shortest output shortest matches found here\n\
81 These simple tests can be combined using the operators `-a', `-o' and `-n'\n\
82 (for `and', `or' and `not'; they may also be written `&', `|' and `!' if\n\
83 you like), and grouped using parentheses `(' and `)'.\n\
87 /*----- The options parser ------------------------------------------------*/
89 /* --- Options table structure --- */
99 O_HELP
, O_VERSION
, O_USAGE
,
101 O_AND
, O_OR
, O_NOT
, O_LPAREN
, O_RPAREN
,
102 O_ANAG
, O_SUBG
, O_WILD
, O_TRACK
, O_REGEXP
, O_PCRE
, O_MONO
, O_LENGTH
,
103 O_LONGEST
, O_SHORTEST
,
109 static const struct opt opttab
[] = {
111 /* --- Options -- don't form part of the language --- */
113 { "help", 0, OF_SHORT
, O_HELP
},
114 { "version", 0, OF_SHORT
, O_VERSION
},
115 { "usage", 0, OF_SHORT
, O_USAGE
},
116 { "file", 1, OF_SHORT
, O_FILE
},
118 /* --- Operators -- provide the basic structure of the language --- *
120 * These are also given magical names by the parser.
123 { "and", 0, OF_SHORT
, O_AND
},
124 { "or", 0, OF_SHORT
, O_OR
},
125 { "not", 0, OF_SHORT
, O_NOT
},
127 /* --- Actual matching operations -- do something useful --- */
129 { "anagram", 1, 0, O_ANAG
},
130 { "subgram", 1, 0, O_SUBG
},
131 { "wildcard", 1, 0, O_WILD
},
132 { "trackword", 1, 0, O_TRACK
},
133 { "mono", 1, 0, O_MONO
},
135 { "regexp", 1, 0, O_REGEXP
},
137 #if defined(HAVE_PCRE) || defined(HAVE_PCRE2)
138 { "pcre", 1, 0, O_PCRE
},
140 { "length", 1, 0, O_LENGTH
},
141 { "longest", 0, 0, O_LONGEST
},
142 { "shortest", 0, 0, O_SHORTEST
},
144 /* --- End marker --- */
150 static const char *const *av
;
153 /* --- @nextopt@ --- *
155 * Arguments: @const char ***arg@ = where to store the arg pointer
157 * Returns: The tag of the next option.
159 * Use: Scans the next option off the command line. If the option
160 * doesn't form part of the language, it's processed internally,
161 * and you'll never see it from here. On exit, the @arg@
162 * pointer is set to contain the address of the option scanned,
163 * followed by its arguments if any. You're expected to know
164 * how many arguments there are for your option.
167 static unsigned nextopt(const char *const **arg
)
170 const struct opt
*o
, *oo
;
174 /* Pick the next option off the front. */
176 if (ai
>= ac
) return (O_EOF
);
179 /* Cope with various forms of magic. */
181 if (!p
[1]) switch (*p
) {
182 case '&': return (O_AND
);
183 case '|': return (O_OR
);
184 case '!': return (O_NOT
);
185 case '(': return (O_LPAREN
);
186 case ')': return (O_RPAREN
);
191 /* Now cope with other sorts of weirdies. By the end of this, a leading
192 * `-' or `--' will have been stripped.
198 if (ai
< ac
) die("syntax error near `--': rubbish at end of line");
202 /*Now look the word up in my table. */
205 for (o
= opttab
; o
->name
; o
++) {
206 if (strncmp(p
, o
->name
, sz
) == 0) {
207 if (strlen(o
->name
) == sz
|| ((o
->f
& OF_SHORT
) && sz
== 1))
210 die("ambiguous option name `-%s' (could match `-%s' or `-%s')",
211 p
, oo
->name
, o
->name
);
215 if (!oo
) die("unrecognized option name `-%s'", p
);
217 /* Sort out the arguments. */
218 if (ai
+ oo
->nargs
> ac
)
219 die("too few arguments for `-%s' (need %u)", oo
->name
, oo
->nargs
);
222 /* Now process the option. */
224 case O_HELP
: help(stdout
); exit(0);
225 case O_VERSION
: version(stdout
); exit(0);
226 case O_USAGE
: usage(stdout
); exit(0);
227 case O_FILE
: file
= (*arg
)[1]; break;
228 default: return (oo
->tag
);
232 die("syntax error near `%s': unknown token type", av
[ai
- 1]);
236 /*----- Node types for operators ------------------------------------------*/
238 /* --- Node structures --- */
240 typedef struct node_bin
{
246 typedef struct node_un
{
251 /* --- Node functions --- */
253 static int n_or(node
*nn
, const char *p
, size_t sz
)
255 node_bin
*n
= (node_bin
*)nn
;
256 return (n
->left
->func(n
->left
, p
, sz
) || n
->right
->func(n
->right
, p
, sz
));
259 static int n_and(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_not(node
*nn
, const char *p
, size_t sz
)
267 node_un
*n
= (node_un
*)nn
;
268 return (!n
->arg
->func(n
->arg
, p
, sz
));
271 /*----- Other simple node types -------------------------------------------*/
273 enum { LESS
= -1, EQUAL
= 0, GREATER
= 1 };
275 typedef struct node_numeric
{
281 static void parse_numeric(const char *p
, int *dir
, int *i
)
288 case '-': *dir
= LESS
; pp
++; break;
289 case '+': *dir
= GREATER
; pp
++; break;
290 default: *dir
= EQUAL
; break;
293 l
= strtol(pp
, &q
, 0);
294 if (*q
|| errno
|| l
< INT_MIN
|| l
> INT_MAX
)
295 die("bad numeric parameter `%s'", p
);
299 static node
*make_numeric(const char *const *av
,
300 int (*func
)(struct node
*, const char *, size_t))
302 node_numeric
*n
= xmalloc(sizeof(*n
));
303 parse_numeric(av
[0], &n
->dir
, &n
->i
);
308 static int cmp_numeric(int x
, int dir
, int n
)
311 case LESS
: return (x
<= n
);
312 case EQUAL
: return (x
== n
);
313 case GREATER
: return (x
>= n
);
318 static int n_length(node
*nn
, const char *p
, size_t sz
)
320 node_numeric
*n
= (node_numeric
*)nn
;
321 return (cmp_numeric(sz
, n
->dir
, n
->i
));
324 /*----- Parser for the expression syntax ----------------------------------*/
326 /* --- A parser context --- */
328 typedef struct p_ctx
{
330 const char *const *a
;
333 /* --- Parser structure --- *
335 * This is a simple recursive descent parser. The context retains
336 * information about the current token. Each function is passed the address
337 * of a node pointer to fill in. This simplifies the binary operator code
338 * somewhat, relative to returning pointers to node trees.
341 static void p_expr(p_ctx
*p
, node
**/
*nn*/
);
343 static void p_next(p_ctx
*p
)
345 static const char *const eof
[] = { "<end>", 0 };
346 p
->t
= nextopt(&p
->a
);
347 if (p
->t
== O_EOF
) p
->a
= eof
;
350 static void p_factor(p_ctx
*p
, node
**nn
)
353 if (p
->t
== O_LPAREN
) {
356 if (p
->t
!= O_RPAREN
) die("syntax error near `%s': missing `)'", *p
->a
);
358 } else if (p
->t
== O_NOT
) {
359 n
= xmalloc(sizeof(node_un
));
363 p_factor(p
, &n
->arg
);
366 case O_ANAG
: *nn
= anagram(p
->a
+ 1); break;
367 case O_SUBG
: *nn
= subgram(p
->a
+ 1); break;
368 case O_WILD
: *nn
= wildcard(p
->a
+ 1); break;
369 case O_TRACK
: *nn
= trackword(p
->a
+ 1); break;
371 case O_REGEXP
: *nn
= regexp(p
->a
+ 1); break;
373 #if defined(HAVE_PCRE) || defined(HAVE_PCRE2)
374 case O_PCRE
: *nn
= pcrenode(p
->a
+ 1); break;
376 case O_MONO
: *nn
= mono(p
->a
+ 1); break;
377 case O_LENGTH
: *nn
= make_numeric(p
->a
+ 1, n_length
); break;
378 case O_LONGEST
: *nn
= longest(p
->a
+ 1); break;
379 case O_SHORTEST
: *nn
= shortest(p
->a
+ 1); break;
380 default: die("syntax error near `%s': unexpected token", *p
->a
);
386 static void p_term(p_ctx
*p
, node
**nn
)
392 case O_AND
: p_next(p
); break;
394 case O_RPAREN
: case O_OR
: case O_EOF
: return;
396 n
= xmalloc(sizeof(node_bin
));
404 static void p_expr(p_ctx
*p
, node
**nn
)
409 if (p
->t
!= O_OR
) break;
411 n
= xmalloc(sizeof(node_bin
));
419 /* --- @p_argv@ --- *
421 * Arguments: @int argc@ = number of command-line arguments
422 * @const char *const argv[]@ = vectoor of arguments
424 * Returns: A compiled node, parsed from the arguments.
426 * Use: Does the donkey-work of parsing a command-line.
429 static node
*p_argv(int argc
, const char *const argv
[])
440 pquis(stderr
, "(Run `$ --help' for more detail.)\n");
445 die("syntax error near `%s': rubbish at end of line (too many `)'s?)",
450 /*----- At-end stuff ------------------------------------------------------*/
452 /* --- @atend_register@ --- *
454 * Arguments: @int (*func)(void *)@ = function to call
455 * @void *p@ = handle to pass to it
459 * Use: Adds a function to the list of things to do at the end of the
460 * program. The function should return nonzero if it produced
464 typedef struct atend
{
466 int (*func
)(void */
*p*/
);
470 static atend
*aa_head
= 0, **aa_tail
= &aa_head
;
472 void atend_register(int (*func
)(void */
*p*/
), void *p
)
474 atend
*a
= xmalloc(sizeof(*a
));
482 /*----- Main code ---------------------------------------------------------*/
486 * Arguments: @int argc@ = number of command-line arguments
487 * @char *argv[]@ = vector of argument words
489 * Returns: Zero on success, nonzero on failure.
491 * Use: Picks entries from a word list which match particular
492 * expressions. This might be of assistance to word-game types.
495 int main(int argc
, char *argv
[])
505 n
= p_argv(argc
, (const char *const *)argv
);
507 if ((fp
= fopen(file
, "r")) == 0)
508 die("error opening `%s': %s", file
, strerror(errno
));
511 if (dstr_putline(&d
, fp
) < 0) break;
513 for (p
= q
= d
.buf
; p
< l
; p
++) {
514 if (!isalnum((unsigned char)*p
)) continue;
515 *q
++ = tolower((unsigned char)*p
);
519 if (n
->func(n
, d
.buf
, d
.len
)) {
520 fwrite(d
.buf
, 1, d
.len
, stdout
);
525 if (ferror(fp
) || fclose(fp
))
526 die("error reading `%s': %s", file
, strerror(errno
));
527 for (a
= aa_head
; a
; a
= a
->next
)
528 if (a
->func(a
->p
)) ok
= 1;
529 if (fflush(stdout
) || ferror(stdout
) || fclose(stdout
))
530 die("error writing output: %s", strerror(errno
));
531 if (!ok
) pquis(stderr
, "$: no matches found\n");
532 return (ok ? EX_OK
: EX_NONE
);
535 /*----- That's all, folks -------------------------------------------------*/