X-Git-Url: https://git.distorted.org.uk/~mdw/anag/blobdiff_plain/650bb9da7cf5b677960c03e0a6a5616d48340845..refs/heads/mdw/sodomy:/anag.c diff --git a/anag.c b/anag.c index 80da3c3..315930d 100644 --- a/anag.c +++ b/anag.c @@ -35,14 +35,10 @@ static const char *file = DICTIONARY; /*----- Help text functions -----------------------------------------------*/ static void usage(FILE *fp) -{ - pquis(fp, "Usage: $ [-f file] expression\n"); -} + { pquis(fp, "Usage: $ [-f file] expression\n"); } static void version(FILE *fp) -{ - pquis(fp, "$, version " VERSION "\n"); -} + { pquis(fp, "$, version " VERSION "\n"); } static void help(FILE *fp) { @@ -175,15 +171,12 @@ static unsigned nextopt(const char *const **arg) size_t sz; const char *p; - /* --- Pick the next option off the front --- */ - + /* Pick the next option off the front. */ *arg = av + ai; - if (ai >= ac) - return (O_EOF); + if (ai >= ac) return (O_EOF); p = av[ai++]; - /* --- Cope with various forms of magic --- */ - + /* Cope with various forms of magic. */ if (p[0] != '-') { if (!p[1]) switch (*p) { case '&': return (O_AND); @@ -195,65 +188,44 @@ static unsigned nextopt(const char *const **arg) goto bad; } - /* --- Now cope with other sorts of weirdies --- * - * - * By the end of this, a leading `-' or `--' will have been stripped. + /* 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) goto bad; + if (*p == '-') p++; if (!*p) { - if (ai < ac) - die("syntax error near `--': rubbish at end of line"); + if (ai < ac) die("syntax error near `--': rubbish at end of line"); return (O_EOF); } - /* --- Now look the word up in my table --- */ - + /*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) { + 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 (!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 --- */ - + /* 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); + 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); } continue; bad: @@ -372,8 +344,7 @@ static void p_next(p_ctx *p) { static const char *const eof[] = { "", 0 }; p->t = nextopt(&p->a); - if (p->t == O_EOF) - p->a = eof; + if (p->t == O_EOF) p->a = eof; } static void p_factor(p_ctx *p, node **nn) @@ -382,8 +353,7 @@ static void p_factor(p_ctx *p, node **nn) 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); + 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)); @@ -419,14 +389,9 @@ static void p_term(p_ctx *p, node **nn) for (;;) { p_factor(p, nn); switch (p->t) { - case O_AND: - p_next(p); - default: - break; - case O_RPAREN: - case O_OR: - case O_EOF: - return; + case O_AND: p_next(p); break; + default: break; + case O_RPAREN: case O_OR: case O_EOF: return; } n = xmalloc(sizeof(node_bin)); n->left = *nn; @@ -441,8 +406,7 @@ static void p_expr(p_ctx *p, node **nn) node_bin *n; for (;;) { p_term(p, nn); - if (p->t != O_OR) - break; + if (p->t != O_OR) break; p_next(p); n = xmalloc(sizeof(node_bin)); n->left = *nn; @@ -477,10 +441,9 @@ static node *p_argv(int argc, const char *const argv[]) exit(EXIT_FAILURE); } p_expr(&p, &n); - if (p.t != O_EOF) { + if (p.t != O_EOF) die("syntax error near `%s': rubbish at end of line (too many `)'s?)", *p.a); - } return (n); } @@ -545,12 +508,10 @@ int main(int argc, char *argv[]) die("error opening `%s': %s", file, strerror(errno)); for (;;) { dstr_reset(&d); - if (dstr_putline(&d, fp) < 0) - break; + 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; + if (!isalnum((unsigned char)*p)) continue; *q++ = tolower((unsigned char)*p); } *q = 0; @@ -563,10 +524,8 @@ int main(int argc, char *argv[]) } if (ferror(fp) || fclose(fp)) die("error reading `%s': %s", file, strerror(errno)); - for (a = aa_head; a; a = a->next) { - if (a->func(a->p)) - ok = 1; - } + for (a = aa_head; a; a = a->next) + if (a->func(a->p)) ok = 1; if (fflush(stdout) || ferror(stdout) || fclose(stdout)) die("error writing output: %s", strerror(errno)); if (!ok) pquis(stderr, "$: no matches found\n");