=================================================================== RCS file: /cvs/mandoc/mdoc_validate.c,v retrieving revision 1.306 retrieving revision 1.314 diff -u -p -r1.306 -r1.314 --- mandoc/mdoc_validate.c 2016/08/11 10:47:16 1.306 +++ mandoc/mdoc_validate.c 2017/01/10 13:47:00 1.314 @@ -1,7 +1,7 @@ -/* $Id: mdoc_validate.c,v 1.306 2016/08/11 10:47:16 schwarze Exp $ */ +/* $Id: mdoc_validate.c,v 1.314 2017/01/10 13:47:00 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons - * Copyright (c) 2010-2016 Ingo Schwarze + * Copyright (c) 2010-2017 Ingo Schwarze * Copyright (c) 2010 Joerg Sonnenberger * * Permission to use, copy, modify, and distribute this software for any @@ -67,7 +67,6 @@ static void post_bf(POST_ARGS); static void post_bk(POST_ARGS); static void post_bl(POST_ARGS); static void post_bl_block(POST_ARGS); -static void post_bl_block_tag(POST_ARGS); static void post_bl_head(POST_ARGS); static void post_bl_norm(POST_ARGS); static void post_bx(POST_ARGS); @@ -104,6 +103,7 @@ static void post_sh_authors(POST_ARGS); static void post_sm(POST_ARGS); static void post_st(POST_ARGS); static void post_std(POST_ARGS); +static void post_xr(POST_ARGS); static v_post mdoc_valids[MDOC_MAX] = { NULL, /* Ap */ @@ -146,7 +146,7 @@ static v_post mdoc_valids[MDOC_MAX] = { post_st, /* St */ NULL, /* Va */ NULL, /* Vt */ - NULL, /* Xr */ + post_xr, /* Xr */ NULL, /* %A */ post_hyph, /* %B */ /* FIXME: can be used outside Rs/Re. */ NULL, /* %D */ @@ -297,7 +297,8 @@ mdoc_node_validate(struct roff_man *mdoc) mdoc->next = ROFF_NEXT_SIBLING; switch (n->type) { case ROFFT_TEXT: - if (n->sec != SEC_SYNOPSIS || n->parent->tok != MDOC_Fd) + if (n->sec != SEC_SYNOPSIS || + (n->parent->tok != MDOC_Cd && n->parent->tok != MDOC_Fd)) check_text(mdoc, n->line, n->pos, n->string); break; case ROFFT_EQN: @@ -316,9 +317,9 @@ mdoc_node_validate(struct roff_man *mdoc) */ if (n->child != NULL) - n->child->flags &= ~MDOC_DELIMC; + n->child->flags &= ~NODE_DELIMC; if (n->last != NULL) - n->last->flags &= ~MDOC_DELIMO; + n->last->flags &= ~NODE_DELIMO; /* Call the macro's postprocessor. */ @@ -851,12 +852,11 @@ post_nm(POST_ARGS) n->last->tok == MDOC_Lp)) mdoc_node_relink(mdoc, n->last); - if (mdoc->meta.name != NULL) - return; - - deroff(&mdoc->meta.name, n); - if (mdoc->meta.name == NULL) + deroff(&mdoc->meta.name, n); + + if (mdoc->meta.name == NULL || + (mdoc->lastsec == SEC_NAME && n->child == NULL)) mandoc_msg(MANDOCERR_NM_NONAME, mdoc->parse, n->line, n->pos, "Nm"); } @@ -887,7 +887,8 @@ post_display(POST_ARGS) switch (n->type) { case ROFFT_BODY: if (n->end != ENDBODY_NOT) { - if (n->tok == MDOC_Bd && n->parent->args == NULL) + if (n->tok == MDOC_Bd && + n->body->parent->args == NULL) roff_node_delete(mdoc, n); } else if (n->child == NULL) mandoc_msg(MANDOCERR_BLK_EMPTY, mdoc->parse, @@ -944,12 +945,15 @@ post_defaults(POST_ARGS) case MDOC_Ar: mdoc->next = ROFF_NEXT_CHILD; roff_word_alloc(mdoc, nn->line, nn->pos, "file"); + mdoc->last->flags |= NODE_NOSRC; roff_word_alloc(mdoc, nn->line, nn->pos, "..."); + mdoc->last->flags |= NODE_NOSRC; break; case MDOC_Pa: case MDOC_Mt: mdoc->next = ROFF_NEXT_CHILD; roff_word_alloc(mdoc, nn->line, nn->pos, "~"); + mdoc->last->flags |= NODE_NOSRC; break; default: abort(); @@ -1098,22 +1102,7 @@ post_bl_block(POST_ARGS) post_prevpar(mdoc); - /* - * 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 (n->norm->Bl.type == LIST_tag && - n->norm->Bl.width == NULL) { - post_bl_block_tag(mdoc); - assert(n->norm->Bl.width != NULL); - } - for (ni = n->body->child; ni != NULL; ni = ni->next) { if (ni->body == NULL) continue; @@ -1171,71 +1160,6 @@ rewrite_macro2len(char **arg) } static void -post_bl_block_tag(POST_ARGS) -{ - struct roff_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 - * rewrite_macro2len() for converting the -width string. - */ - - sz = 10; - n = mdoc->last; - - for (nn = n->body->child; nn != NULL; nn = nn->next) { - if (nn->tok != MDOC_It) - continue; - - assert(nn->type == ROFFT_BLOCK); - nn = nn->head->child; - - if (nn == NULL) - break; - - if (nn->type == ROFFT_TEXT) { - 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 != NULL); - 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]; -} - -static void post_bl_head(POST_ARGS) { struct roff_node *nbl, *nh, *nch, *nnext; @@ -1328,14 +1252,44 @@ post_bl(POST_ARGS) return; } while (nchild != NULL) { + nnext = nchild->next; if (nchild->tok == MDOC_It || (nchild->tok == MDOC_Sm && - nchild->next != NULL && - nchild->next->tok == MDOC_It)) { - nchild = nchild->next; + nnext != NULL && nnext->tok == MDOC_It)) { + nchild = nnext; continue; } + /* + * In .Bl -column, the first rows may be implicit, + * that is, they may not start with .It macros. + * Such rows may be followed by nodes generated on the + * roff level, for example .TS, which cannot be moved + * out of the list. In that case, wrap such roff nodes + * into an implicit row. + */ + + if (nchild->prev != NULL) { + mdoc->last = nchild; + mdoc->next = ROFF_NEXT_SIBLING; + roff_block_alloc(mdoc, nchild->line, + nchild->pos, MDOC_It); + roff_head_alloc(mdoc, nchild->line, + nchild->pos, MDOC_It); + mdoc->next = ROFF_NEXT_SIBLING; + roff_body_alloc(mdoc, nchild->line, + nchild->pos, MDOC_It); + while (nchild->tok != MDOC_It) { + mdoc_node_relink(mdoc, nchild); + if ((nchild = nnext) == NULL) + break; + nnext = nchild->next; + mdoc->next = ROFF_NEXT_SIBLING; + } + mdoc->last = nbody; + continue; + } + mandoc_msg(MANDOCERR_BL_MOVE, mdoc->parse, nchild->line, nchild->pos, mdoc_macronames[nchild->tok]); @@ -1348,13 +1302,11 @@ post_bl(POST_ARGS) nblock = nbody->parent; nprev = nblock->prev; nparent = nblock->parent; - nnext = nchild->next; /* * Unlink this child. */ - assert(nchild->prev == NULL); nbody->child = nnext; if (nnext == NULL) nbody->last = NULL; @@ -1603,7 +1555,7 @@ static void post_ns(POST_ARGS) { - if (mdoc->last->flags & MDOC_LINE) + if (mdoc->last->flags & NODE_LINE) mandoc_msg(MANDOCERR_NS_SKIP, mdoc->parse, mdoc->last->line, mdoc->last->pos, NULL); } @@ -1649,8 +1601,12 @@ post_sh_name(POST_ARGS) for (n = mdoc->last->child; n != NULL; n = n->next) { switch (n->tok) { case MDOC_Nm: + if (hasnm && n->child != NULL) + mandoc_vmsg(MANDOCERR_NAMESEC_PUNCT, + mdoc->parse, n->line, n->pos, + "Nm %s", n->child->string); hasnm = 1; - break; + continue; case MDOC_Nd: hasnd = 1; if (n->next != NULL) @@ -1658,14 +1614,19 @@ post_sh_name(POST_ARGS) mdoc->parse, n->line, n->pos, NULL); break; case TOKEN_NONE: - if (hasnm) - break; + if (n->type == ROFFT_TEXT && + n->string[0] == ',' && n->string[1] == '\0' && + n->next != NULL && n->next->tok == MDOC_Nm) { + n = n->next; + continue; + } /* FALLTHROUGH */ default: mandoc_msg(MANDOCERR_NAMESEC_BAD, mdoc->parse, n->line, n->pos, mdoc_macronames[n->tok]); - break; + continue; } + break; } if ( ! hasnm) @@ -1859,6 +1820,21 @@ post_sh_head(POST_ARGS) } static void +post_xr(POST_ARGS) +{ + struct roff_node *n, *nch; + + n = mdoc->last; + nch = n->child; + if (nch->next == NULL) { + mandoc_vmsg(MANDOCERR_XR_NOSEC, mdoc->parse, + n->line, n->pos, "Xr %s", nch->string); + return; + } + assert(nch->next == n->last); +} + +static void post_ignpar(POST_ARGS) { struct roff_node *np; @@ -1968,6 +1944,8 @@ post_dd(POST_ARGS) char *datestr; n = mdoc->last; + n->flags |= NODE_NOPRT; + if (mdoc->meta.date != NULL) { mandoc_msg(MANDOCERR_PROLOG_REP, mdoc->parse, n->line, n->pos, "Dd"); @@ -1985,7 +1963,7 @@ post_dd(POST_ARGS) if (n->child == NULL || n->child->string[0] == '\0') { mdoc->meta.date = mdoc->quick ? mandoc_strdup("") : mandoc_normdate(mdoc->parse, NULL, n->line, n->pos); - goto out; + return; } datestr = NULL; @@ -1997,8 +1975,6 @@ post_dd(POST_ARGS) datestr, n->line, n->pos); free(datestr); } -out: - roff_node_delete(mdoc, n); } static void @@ -2009,10 +1985,12 @@ post_dt(POST_ARGS) char *p; n = mdoc->last; + n->flags |= NODE_NOPRT; + if (mdoc->flags & MDOC_PBODY) { mandoc_msg(MANDOCERR_DT_LATE, mdoc->parse, n->line, n->pos, "Dt"); - goto out; + return; } if (mdoc->meta.title != NULL) @@ -2054,7 +2032,7 @@ post_dt(POST_ARGS) } } - /* Mandatory second argument: section. */ + /* Mandatory second argument: section. */ if (nn != NULL) nn = nn->next; @@ -2064,7 +2042,7 @@ post_dt(POST_ARGS) mdoc->parse, n->line, n->pos, "Dt %s", mdoc->meta.title); mdoc->meta.vol = mandoc_strdup("LOCAL"); - goto out; /* msec and arch remain NULL. */ + return; /* msec and arch remain NULL. */ } mdoc->meta.msec = mandoc_strdup(nn->string); @@ -2082,7 +2060,7 @@ post_dt(POST_ARGS) /* Optional third argument: architecture. */ if ((nn = nn->next) == NULL) - goto out; + return; for (p = nn->string; *p != '\0'; p++) *p = tolower((unsigned char)*p); @@ -2093,9 +2071,6 @@ post_dt(POST_ARGS) if ((nn = nn->next) != NULL) mandoc_vmsg(MANDOCERR_ARG_EXCESS, mdoc->parse, nn->line, nn->pos, "Dt ... %s", nn->string); - -out: - roff_node_delete(mdoc, n); } static void @@ -2123,6 +2098,8 @@ post_os(POST_ARGS) struct roff_node *n; n = mdoc->last; + n->flags |= NODE_NOPRT; + if (mdoc->meta.os != NULL) mandoc_msg(MANDOCERR_PROLOG_REP, mdoc->parse, n->line, n->pos, "Os"); @@ -2143,11 +2120,11 @@ post_os(POST_ARGS) mdoc->meta.os = NULL; deroff(&mdoc->meta.os, n); if (mdoc->meta.os) - goto out; + return; if (mdoc->defos) { mdoc->meta.os = mandoc_strdup(mdoc->defos); - goto out; + return; } #ifdef OSNAME @@ -2164,9 +2141,6 @@ post_os(POST_ARGS) } mdoc->meta.os = mandoc_strdup(defbuf); #endif /*!OSNAME*/ - -out: - roff_node_delete(mdoc, n); } /*