+/* -*-c-*-
+ *
+ * $Id: anag.c,v 1.1 2001/02/04 17:14:42 mdw Exp $
+ *
+ * Main driver for anag
+ *
+ * (c) 2001 Mark Wooding
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Anag: a simple wordgame helper.
+ *
+ * Anag is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Anag is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Anag; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: anag.c,v $
+ * Revision 1.1 2001/02/04 17:14:42 mdw
+ * Initial checkin
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "anag.h"
+
+/*----- Static variables --------------------------------------------------*/
+
+static const char *file = DICTIONARY;
+
+/*----- Help text functions -----------------------------------------------*/
+
+static void usage(FILE *fp)
+{
+ pquis(fp, "Usage: $ [-f file] expression\n");
+}
+
+static void version(FILE *fp)
+{
+ pquis(fp, "$, version " VERSION "\n");
+}
+
+static void help(FILE *fp)
+{
+ version(fp);
+ fputc('\n', fp);
+ usage(fp);
+ fputs("\n\
+Searches a wordlist, printing all of the words which match an expression.\n\
+The basic tests in the expression are:\n\
+\n\
+-anagram WORD matches a full-length anagram\n\
+-subgram WORD matches words which only use letters in WORD\n\
+-wildcard PATTERN matches with wildcards `*' and `?'\n\
+-trackword WORD matches words which can be found in a trackword\n\
+\n\
+These simple tests can be combined using the operators `-a', `-o' and `-n'\n\
+(for `and', `or' and `not'; they may also be written `&', `|' and `!' if\n\
+you like), and grouped using parentheses `(' and `)'.\n\
+", fp);
+}
+
+/*----- The options parser ------------------------------------------------*/
+
+/* --- Options table structure --- */
+
+struct opt {
+ const char *name;
+ unsigned nargs;
+ unsigned f;
+ unsigned tag;
+};
+
+enum {
+ O_HELP, O_VERSION, O_USAGE,
+ O_FILE,
+ O_AND, O_OR, O_NOT, O_LPAREN, O_RPAREN,
+ O_ANAG, O_SUBG, O_WILD, O_TRACK,
+ O_EOF
+};
+
+#define OF_SHORT 1u
+
+static const struct opt opttab[] = {
+
+ /* --- Options -- don't form part of the language --- */
+
+ { "help", 0, OF_SHORT, O_HELP },
+ { "version", 0, OF_SHORT, O_VERSION },
+ { "usage", 0, OF_SHORT, O_USAGE },
+ { "file", 1, OF_SHORT, O_FILE },
+
+ /* --- Operators -- provide the basic structure of the language --- *
+ *
+ * These are also given magical names by the parser.
+ */
+
+ { "and", 0, OF_SHORT, O_AND },
+ { "or", 0, OF_SHORT, O_OR },
+ { "not", 0, OF_SHORT, O_NOT },
+
+ /* --- Actual matching oeprations -- do something useful --- */
+
+ { "anagram", 1, 0, O_ANAG },
+ { "subgram", 1, 0, O_SUBG },
+ { "wildcard", 1, 0, O_WILD },
+ { "trackword", 1, 0, O_TRACK },
+
+ /* --- End marker --- */
+
+ { 0, 0, 0, 0 }
+};
+
+static int ac;
+static const char *const *av;
+static int ai;
+
+/* --- @nextopt@ --- *
+ *
+ * Arguments: @const char ***arg@ = where to store the arg pointer
+ *
+ * Returns: The tag of the next option.
+ *
+ * Use: Scans the next option off the command line. If the option
+ * doesn't form part of the language, it's processed internally,
+ * and you'll never see it from here. On exit, the @arg@
+ * pointer is set to contain the address of the option scanned,
+ * followed by its arguments if any. You're expected to know
+ * how many arguments there are for your option.
+ */
+
+static unsigned nextopt(const char *const **arg)
+{
+ for (;;) {
+ const struct opt *o, *oo;
+ size_t sz;
+ const char *p;
+
+ /* --- Pick the next option off the front --- */
+
+ *arg = av + ai;
+ if (ai >= ac)
+ return (O_EOF);
+ p = av[ai++];
+
+ /* --- Cope with various forms of magic --- */
+
+ if (p[0] != '-') {
+ if (!p[1]) switch (*p) {
+ case '&': return (O_AND);
+ case '|': return (O_OR);
+ case '!': return (O_NOT);
+ case '(': return (O_LPAREN);
+ case ')': return (O_RPAREN);
+ }
+ goto bad;
+ }
+
+ /* --- Now cope with other sorts of weirdies --- *
+ *
+ * By the end of this, a leading `-' or `--' will have been stripped.
+ */
+
+ p++;
+ if (!*p)
+ goto bad;
+ if (*p == '-')
+ p++;
+ if (!*p) {
+ if (ai < ac)
+ die("syntax error near `--': rubbish at end of line");
+ return (O_EOF);
+ }
+
+ /* --- Now look the word up in my table --- */
+
+ sz = strlen(p);
+ oo = 0;
+ for (o = opttab; o->name; o++) {
+ if (strncmp(p, o->name, sz) == 0) {
+ if (strlen(o->name) == sz || ((o->f & OF_SHORT) && sz == 1)) {
+ oo = o;
+ break;
+ }
+ if (oo) {
+ die("ambiguous option name `-%s' (could match `-%s' or `-%s')",
+ p, oo->name, o->name);
+ }
+ oo = o;
+ }
+ }
+ if (!oo)
+ die("unrecognized option name `-%s'", p);
+
+ /* --- Sort out the arguments --- */
+
+ if (ai + oo->nargs > ac)
+ die("too few arguments for `-%s' (need %u)", oo->name, oo->nargs);
+ ai += oo->nargs;
+
+ /* --- Now process the option --- */
+
+ switch (oo->tag) {
+ case O_HELP:
+ help(stdout);
+ exit(0);
+ case O_VERSION:
+ version(stdout);
+ exit(0);
+ case O_USAGE:
+ usage(stdout);
+ exit(0);
+ case O_FILE:
+ file = (*arg)[1];
+ break;
+ default:
+ return (oo->tag);
+ }
+ bad:
+ die("syntax error near `%s': unknown token type", av[ai - 1]);
+ }
+}
+
+/*----- Node types for operators ------------------------------------------*/
+
+/* --- Node structures --- */
+
+typedef struct node_bin {
+ node n;
+ node *left;
+ node *right;
+} node_bin;
+
+typedef struct node_un {
+ node n;
+ node *arg;
+} node_un;
+
+/* --- Node functions --- */
+
+static int n_or(node *nn, const char *p, size_t sz)
+{
+ node_bin *n = (node_bin *)nn;
+ return (n->left->func(n->left, p, sz) || n->right->func(n->right, p, sz));
+}
+
+static int n_and(node *nn, const char *p, size_t sz)
+{
+ node_bin *n = (node_bin *)nn;
+ return (n->left->func(n->left, p, sz) && n->right->func(n->right, p, sz));
+}
+
+static int n_not(node *nn, const char *p, size_t sz)
+{
+ node_un *n = (node_un *)nn;
+ return (!n->arg->func(n->arg, p, sz));
+}
+
+/*----- Parser for the expression syntax ----------------------------------*/
+
+/* --- A parser context --- */
+
+typedef struct p_ctx {
+ unsigned t;
+ const char *const *a;
+} p_ctx;
+
+/* --- Parser structure --- *
+ *
+ * This is a simple recursive descent parser. The context retains
+ * information about the current token. Each function is passed the address
+ * of a node pointer to fill in. This simplifies the binary operator code
+ * somewhat, relative to returning pointers to node trees.
+ */
+
+static void p_expr(p_ctx *p, node **/*nn*/);
+
+static void p_next(p_ctx *p)
+{
+ static const char *const eof[] = { "<end>", 0 };
+ p->t = nextopt(&p->a);
+ if (p->t == O_EOF)
+ p->a = eof;
+}
+
+static void p_factor(p_ctx *p, node **nn)
+{
+ node_un *n;
+ if (p->t == O_LPAREN) {
+ p_next(p);
+ p_expr(p, nn);
+ if (p->t != O_RPAREN)
+ die("syntax error near `%s': missing `('", *p->a);
+ p_next(p);
+ } else if (p->t == O_NOT) {
+ n = xmalloc(sizeof(node_un));
+ n->n.func = n_not;
+ *nn = &n->n;
+ p_next(p);
+ p_factor(p, &n->arg);
+ } else {
+ switch (p->t) {
+ case O_ANAG: *nn = anagram(p->a + 1); break;
+ case O_SUBG: *nn = subgram(p->a + 1); break;
+ case O_WILD: *nn = wildcard(p->a + 1); break;
+ case O_TRACK: *nn = trackword(p->a + 1); break;
+ default: die("syntax error near `%s': unexpected token", *p->a);
+ }
+ p_next(p);
+ }
+}
+
+static void p_term(p_ctx *p, node **nn)
+{
+ node_bin *n;
+ for (;;) {
+ p_factor(p, nn);
+ switch (p->t) {
+ case O_AND:
+ p_next(p);
+ default:
+ break;
+ case O_LPAREN:
+ case O_RPAREN:
+ case O_OR:
+ case O_EOF:
+ return;
+ }
+ n = xmalloc(sizeof(node_bin));
+ n->left = *nn;
+ n->n.func = n_and;
+ *nn = &n->n;
+ nn = &n->right;
+ }
+}
+
+static void p_expr(p_ctx *p, node **nn)
+{
+ node_bin *n;
+ for (;;) {
+ p_term(p, nn);
+ if (p->t != O_OR)
+ break;
+ p_next(p);
+ n = xmalloc(sizeof(node_bin));
+ n->left = *nn;
+ n->n.func = n_or;
+ *nn = &n->n;
+ nn = &n->right;
+ }
+}
+
+/* --- @p_argv@ --- *
+ *
+ * Arguments: @int argc@ = number of command-line arguments
+ * @const char *const argv[]@ = vectoor of arguments
+ *
+ * Returns: A compiled node, parsed from the arguments.
+ *
+ * Use: Does the donkey-work of parsing a command-line.
+ */
+
+static node *p_argv(int argc, const char *const argv[])
+{
+ p_ctx p;
+ node *n;
+
+ av = argv;
+ ac = argc;
+ ai = 1;
+ p_next(&p);
+ p_expr(&p, &n);
+ if (p.t != O_EOF) {
+ die("syntax error near `%s': rubbish at end of line (too many `)'s?)",
+ *p.a);
+ }
+ return (n);
+}
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @main@ --- *
+ *
+ * Arguments: @int argc@ = number of command-line arguments
+ * @char *argv[]@ = vector of argument words
+ *
+ * Returns: Zero on success, nonzero on failure.
+ *
+ * Use: Picks entries from a word list which match particular
+ * expressions. This might be of assistance to word-game types.
+ */
+
+int main(int argc, char *argv[])
+{
+ node *n;
+ FILE *fp;
+ dstr d = DSTR_INIT;
+ char *p, *q, *l;
+
+ ego(argv[0]);
+ n = p_argv(argc, (const char *const *)argv);
+
+ if ((fp = fopen(file, "r")) == 0)
+ die("error opening `%s': %s", file, strerror(errno));
+ for (;;) {
+ dstr_reset(&d);
+ if (dstr_putline(&d, fp) < 0)
+ break;
+ l = d.buf + d.len;
+ for (p = q = d.buf; p < l; p++) {
+ if (!isalnum((unsigned char)*p))
+ continue;
+ *q++ = tolower((unsigned char)*p);
+ }
+ *q = 0;
+ d.len = q - d.buf;
+ if (n->func(n, d.buf, d.len)) {
+ fwrite(d.buf, 1, d.len, stdout);
+ fputc('\n', stdout);
+ }
+ }
+ if (!feof(fp))
+ die("error reading `%s': %s", file, strerror(errno));
+ fclose(fp);
+ return (0);
+}
+
+/*----- That's all, folks -------------------------------------------------*/