+exprcomp(int argc, char *argv[], size_t *tt)
+{
+ int pos, lvl;
+ struct expr *e;
+
+ pos = lvl = 0;
+ *tt = 0;
+
+ e = exprexpr(argc, argv, &pos, &lvl, tt);
+
+ if (0 == lvl && pos >= argc)
+ return(e);
+
+ exprfree(e);
+ return(NULL);
+}
+
+/*
+ * Compile an array of tokens into an expression.
+ * An informal expression grammar is defined in apropos(1).
+ * Return NULL if we fail doing so. All memory will be cleaned up.
+ * Return the root of the expression sequence if alright.
+ */
+static struct expr *
+exprexpr(int argc, char *argv[], int *pos, int *lvl, size_t *tt)
+{
+ struct expr *e, *first, *next;
+ int log;
+
+ first = next = NULL;
+
+ for ( ; *pos < argc; (*pos)++) {
+ e = next;
+
+ /*
+ * Close out a subexpression.
+ */
+
+ if (NULL != e && 0 == strcmp(")", argv[*pos])) {
+ if (--(*lvl) < 0)
+ goto err;
+ break;
+ }
+
+ /*
+ * Small note: if we're just starting, don't let "-a"
+ * and "-o" be considered logical operators: they're
+ * just tokens unless pairwise joining, in which case we
+ * record their existence (or assume "OR").
+ */
+ log = 0;
+
+ if (NULL != e && 0 == strcmp("-a", argv[*pos]))
+ log = 1;
+ else if (NULL != e && 0 == strcmp("-o", argv[*pos]))
+ log = 2;
+
+ if (log > 0 && ++(*pos) >= argc)
+ goto err;
+
+ /*
+ * Now we parse the term part. This can begin with
+ * "-i", in which case the expression is case
+ * insensitive.
+ */
+
+ if (0 == strcmp("(", argv[*pos])) {
+ ++(*pos);
+ ++(*lvl);
+ next = mandoc_calloc(1, sizeof(struct expr));
+ next->subexpr = exprexpr(argc, argv, pos, lvl, tt);
+ if (NULL == next->subexpr) {
+ free(next);
+ next = NULL;
+ }
+ } else if (0 == strcmp("-i", argv[*pos])) {
+ if (++(*pos) >= argc)
+ goto err;
+ next = exprterm(argv[*pos], 0);
+ } else
+ next = exprterm(argv[*pos], 1);
+
+ if (NULL == next)
+ goto err;
+
+ next->and = log == 1;
+ next->index = (int)(*tt)++;
+
+ /* Append to our chain of expressions. */
+
+ if (NULL == first) {
+ assert(NULL == e);
+ first = next;
+ } else {
+ assert(NULL != e);
+ e->next = next;
+ }
+ }
+
+ return(first);
+err:
+ exprfree(first);
+ return(NULL);
+}
+
+/*
+ * Parse a terminal expression with the grammar as defined in
+ * apropos(1).
+ * Return NULL if we fail the parse.
+ */
+static struct expr *
+exprterm(char *buf, int cs)