+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
+ return(1);
+}
+
+static int
+post_vt(POST_ARGS)
+{
+ const struct mdoc_node *n;
+
+ /*
+ * The Vt macro comes in both ELEM and BLOCK form, both of which
+ * have different syntaxes (yet more context-sensitive
+ * behaviour). ELEM types must have a child, which is already
+ * guaranteed by the in_line parsing routine; BLOCK types,
+ * specifically the BODY, should only have TEXT children.
+ */
+
+ if (MDOC_BODY != mdoc->last->type)
+ return(1);
+
+ for (n = mdoc->last->child; n; n = n->next)
+ if (MDOC_TEXT != n->type)
+ mandoc_msg(MANDOCERR_VT_CHILD, mdoc->parse,
+ n->line, n->pos, mdoc_macronames[n->tok]);
+
+ return(1);
+}
+
+static int
+post_nm(POST_ARGS)
+{
+
+ if (NULL != mdoc->meta.name)
+ return(1);
+
+ mdoc_deroff(&mdoc->meta.name, mdoc->last);
+
+ if (NULL == mdoc->meta.name) {
+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME);
+ mdoc->meta.name = mandoc_strdup("UNKNOWN");
+ }
+ return(1);
+}
+
+static int
+post_literal(POST_ARGS)
+{
+
+ /*
+ * The `Dl' (note "el" not "one") and `Bd' macros unset the
+ * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
+ * this in literal mode, but it doesn't hurt to just switch it
+ * off in general since displays can't be nested.
+ */
+
+ if (MDOC_BODY == mdoc->last->type)
+ mdoc->flags &= ~MDOC_LITERAL;
+
+ return(1);
+}
+
+static int
+post_defaults(POST_ARGS)
+{
+ struct mdoc_node *nn;
+
+ /*
+ * The `Ar' defaults to "file ..." if no value is provided as an
+ * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
+ * gets an empty string.
+ */
+
+ if (mdoc->last->child)
+ return(1);
+
+ nn = mdoc->last;
+ mdoc->next = MDOC_NEXT_CHILD;
+
+ switch (nn->tok) {
+ case MDOC_Ar:
+ if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "file"))
+ return(0);
+ if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "..."))
+ return(0);
+ break;
+ case MDOC_At:
+ if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "AT&T"))
+ return(0);
+ if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "UNIX"))
+ return(0);
+ break;
+ case MDOC_Li:
+ if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, ""))
+ return(0);
+ break;
+ case MDOC_Pa:
+ /* FALLTHROUGH */
+ case MDOC_Mt:
+ if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "~"))
+ return(0);
+ break;
+ default:
+ abort();
+ /* NOTREACHED */
+ }
+
+ mdoc->last = nn;
+ return(1);
+}
+
+static int
+post_at(POST_ARGS)
+{
+ struct mdoc_node *n;
+ const char *std_att;
+ char *att;
+
+ /*
+ * If we have a child, look it up in the standard keys. If a
+ * key exist, use that instead of the child; if it doesn't,
+ * prefix "AT&T UNIX " to the existing data.
+ */
+
+ if (NULL == (n = mdoc->last->child))
+ return(1);
+
+ assert(MDOC_TEXT == n->type);
+ if (NULL == (std_att = mdoc_a2att(n->string))) {
+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT);
+ mandoc_asprintf(&att, "AT&T UNIX %s", n->string);
+ } else
+ att = mandoc_strdup(std_att);
+
+ free(n->string);
+ n->string = att;
+ return(1);
+}
+
+static int
+post_an(POST_ARGS)
+{
+ struct mdoc_node *np;
+
+ np = mdoc->last;
+ if (AUTH__NONE == np->norm->An.auth) {
+ if (0 == np->child)
+ check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0);
+ } else if (np->child)
+ check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0);
+
+ return(1);
+}
+
+static int
+post_en(POST_ARGS)
+{
+
+ if (MDOC_BLOCK == mdoc->last->type)
+ mdoc->last->norm->Es = mdoc->last_es;
+ return(1);
+}
+
+static int
+post_es(POST_ARGS)
+{
+
+ mdoc->last_es = mdoc->last;
+ return(1);
+}
+
+static int
+post_it(POST_ARGS)
+{
+ int i, cols;
+ enum mdoc_list lt;
+ struct mdoc_node *n, *c;
+ enum mandocerr er;
+
+ if (MDOC_BLOCK != mdoc->last->type)
+ return(1);
+
+ n = mdoc->last->parent->parent;
+ lt = n->norm->Bl.type;
+
+ if (LIST__NONE == lt) {
+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
+ return(1);
+ }
+
+ switch (lt) {
+ case LIST_tag:
+ if (mdoc->last->head->child)
+ break;
+ /* FIXME: give this a dummy value. */
+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
+ break;
+ case LIST_hang:
+ /* FALLTHROUGH */
+ case LIST_ohang:
+ /* FALLTHROUGH */
+ case LIST_inset:
+ /* FALLTHROUGH */
+ case LIST_diag:
+ if (NULL == mdoc->last->head->child)
+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
+ break;
+ case LIST_bullet:
+ /* FALLTHROUGH */
+ case LIST_dash:
+ /* FALLTHROUGH */
+ case LIST_enum:
+ /* FALLTHROUGH */
+ case LIST_hyphen:
+ if (NULL == mdoc->last->body->child)
+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
+ /* FALLTHROUGH */
+ case LIST_item:
+ if (mdoc->last->head->child)
+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
+ break;
+ case LIST_column:
+ cols = (int)n->norm->Bl.ncols;
+
+ assert(NULL == mdoc->last->head->child);
+
+ if (NULL == mdoc->last->body->child)
+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
+
+ for (i = 0, c = mdoc->last->child; c; c = c->next)
+ if (MDOC_BODY == c->type)
+ i++;
+
+ if (i < cols)
+ er = MANDOCERR_ARGCOUNT;
+ else if (i == cols || i == cols + 1)
+ break;
+ else
+ er = MANDOCERR_SYNTARGCOUNT;
+
+ mandoc_vmsg(er, mdoc->parse,
+ mdoc->last->line, mdoc->last->pos,
+ "columns == %d (have %d)", cols, i);
+ return(MANDOCERR_ARGCOUNT == er);
+ default:
+ break;
+ }
+
+ return(1);
+}
+
+static int
+post_bl_block(POST_ARGS)
+{
+ struct mdoc_node *n, *ni, *nc;
+
+ /*
+ * These are fairly complicated, so we've broken them into two
+ * functions. post_bl_block_tag() is called when a -tag is
+ * specified, but no -width (it must be guessed). The second
+ * when a -width is specified (macro indicators must be
+ * rewritten into real lengths).
+ */
+
+ n = mdoc->last;
+
+ if (LIST_tag == n->norm->Bl.type &&
+ NULL == n->norm->Bl.width) {
+ if ( ! post_bl_block_tag(mdoc))
+ return(0);
+ assert(n->norm->Bl.width);
+ } else if (NULL != n->norm->Bl.width) {
+ if ( ! post_bl_block_width(mdoc))
+ return(0);
+ assert(n->norm->Bl.width);
+ }
+
+ for (ni = n->body->child; ni; ni = ni->next) {
+ if (NULL == ni->body)
+ continue;
+ nc = ni->body->last;
+ while (NULL != nc) {
+ switch (nc->tok) {
+ case MDOC_Pp:
+ /* FALLTHROUGH */
+ case MDOC_Lp:
+ /* FALLTHROUGH */
+ case MDOC_br:
+ break;
+ default:
+ nc = NULL;
+ continue;
+ }
+ if (NULL == ni->next) {
+ mandoc_msg(MANDOCERR_PAR_MOVE,
+ mdoc->parse, nc->line, nc->pos,
+ mdoc_macronames[nc->tok]);
+ if ( ! mdoc_node_relink(mdoc, nc))
+ return(0);
+ } else if (0 == n->norm->Bl.comp &&
+ LIST_column != n->norm->Bl.type) {
+ mandoc_vmsg(MANDOCERR_PAR_SKIP,
+ mdoc->parse, nc->line, nc->pos,
+ "%s before It",
+ mdoc_macronames[nc->tok]);
+ mdoc_node_delete(mdoc, nc);
+ } else
+ break;
+ nc = ni->body->last;
+ }
+ }
+ return(1);
+}
+
+static int
+post_bl_block_width(POST_ARGS)
+{
+ size_t width;
+ int i;
+ enum mdoct tok;
+ struct mdoc_node *n;
+ char buf[24];
+
+ n = mdoc->last;
+
+ /*
+ * Calculate the real width of a list from the -width string,
+ * which may contain a macro (with a known default width), a
+ * literal string, or a scaling width.
+ *
+ * If the value to -width is a macro, then we re-write it to be
+ * the macro's width as set in share/tmac/mdoc/doc-common.
+ */
+
+ if (0 == strcmp(n->norm->Bl.width, "Ds"))
+ width = 6;
+ else if (MDOC_MAX == (tok = mdoc_hash_find(n->norm->Bl.width)))
+ return(1);
+ else if (0 == (width = macro2len(tok))) {
+ mdoc_nmsg(mdoc, n, MANDOCERR_BADWIDTH);
+ return(1);
+ }
+
+ /* The value already exists: free and reallocate it. */
+
+ assert(n->args);
+
+ for (i = 0; i < (int)n->args->argc; i++)
+ if (MDOC_Width == n->args->argv[i].arg)
+ break;
+
+ assert(i < (int)n->args->argc);
+
+ (void)snprintf(buf, sizeof(buf), "%un", (unsigned int)width);
+ free(n->args->argv[i].value[0]);
+ n->args->argv[i].value[0] = mandoc_strdup(buf);
+
+ /* Set our width! */
+ n->norm->Bl.width = n->args->argv[i].value[0];
+ return(1);
+}
+
+static int
+post_bl_block_tag(POST_ARGS)
+{
+ struct mdoc_node *n, *nn;
+ size_t sz, ssz;
+ int i;
+ char buf[24];
+
+ /*
+ * Calculate the -width for a `Bl -tag' list if it hasn't been
+ * provided. Uses the first head macro. NOTE AGAIN: this is
+ * ONLY if the -width argument has NOT been provided. See
+ * post_bl_block_width() for converting the -width string.
+ */
+
+ sz = 10;
+ n = mdoc->last;
+
+ for (nn = n->body->child; nn; nn = nn->next) {
+ if (MDOC_It != nn->tok)
+ continue;
+
+ assert(MDOC_BLOCK == nn->type);
+ nn = nn->head->child;
+
+ if (nn == NULL)
+ break;
+
+ if (MDOC_TEXT == nn->type) {
+ sz = strlen(nn->string) + 1;
+ break;
+ }
+
+ if (0 != (ssz = macro2len(nn->tok)))
+ sz = ssz;
+
+ break;
+ }
+
+ /* Defaults to ten ens. */
+
+ (void)snprintf(buf, sizeof(buf), "%un", (unsigned int)sz);
+
+ /*
+ * We have to dynamically add this to the macro's argument list.
+ * We're guaranteed that a MDOC_Width doesn't already exist.
+ */
+
+ assert(n->args);
+ i = (int)(n->args->argc)++;
+
+ n->args->argv = mandoc_reallocarray(n->args->argv,
+ n->args->argc, sizeof(struct mdoc_argv));
+
+ n->args->argv[i].arg = MDOC_Width;
+ n->args->argv[i].line = n->line;
+ n->args->argv[i].pos = n->pos;
+ n->args->argv[i].sz = 1;
+ n->args->argv[i].value = mandoc_malloc(sizeof(char *));
+ n->args->argv[i].value[0] = mandoc_strdup(buf);
+
+ /* Set our width! */
+ n->norm->Bl.width = n->args->argv[i].value[0];
+ return(1);
+}
+
+static int
+post_bl_head(POST_ARGS)
+{
+ struct mdoc_node *np, *nn, *nnp;
+ int i, j;
+
+ if (LIST_column != mdoc->last->norm->Bl.type)
+ /* FIXME: this should be ERROR class... */
+ return(hwarn_eq0(mdoc));
+
+ /*
+ * Convert old-style lists, where the column width specifiers
+ * trail as macro parameters, to the new-style ("normal-form")
+ * lists where they're argument values following -column.
+ */
+
+ /* First, disallow both types and allow normal-form. */
+
+ /*
+ * TODO: technically, we can accept both and just merge the two
+ * lists, but I'll leave that for another day.
+ */
+
+ if (mdoc->last->norm->Bl.ncols && mdoc->last->nchild) {
+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_COLUMNS);
+ return(0);
+ } else if (NULL == mdoc->last->child)