=================================================================== RCS file: /cvs/mandoc/mdoc_validate.c,v retrieving revision 1.245 retrieving revision 1.248 diff -u -p -r1.245 -r1.248 --- mandoc/mdoc_validate.c 2014/08/19 17:31:55 1.245 +++ mandoc/mdoc_validate.c 2014/09/11 23:53:30 1.248 @@ -1,4 +1,4 @@ -/* $Id: mdoc_validate.c,v 1.245 2014/08/19 17:31:55 schwarze Exp $ */ +/* $Id: mdoc_validate.c,v 1.248 2014/09/11 23:53:30 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2014 Ingo Schwarze @@ -67,6 +67,7 @@ static void check_text(struct mdoc *, int, int, char static void check_argv(struct mdoc *, struct mdoc_node *, struct mdoc_argv *); static void check_args(struct mdoc *, struct mdoc_node *); +static int child_an(const struct mdoc_node *); static enum mdoc_sec a2sec(const char *); static size_t macro2len(enum mdoct); @@ -114,8 +115,10 @@ static int post_par(POST_ARGS); static int post_root(POST_ARGS); static int post_rs(POST_ARGS); static int post_sh(POST_ARGS); -static int post_sh_body(POST_ARGS); static int post_sh_head(POST_ARGS); +static int post_sh_name(POST_ARGS); +static int post_sh_see_also(POST_ARGS); +static int post_sh_authors(POST_ARGS); static int post_st(POST_ARGS); static int post_vt(POST_ARGS); static int pre_an(PRE_ARGS); @@ -1643,18 +1646,17 @@ post_root(POST_ARGS) mdoc->meta.os = mandoc_strdup(""); } - n = mdoc->first; - assert(n); - /* Check that we begin with a proper `Sh'. */ - if (NULL == n->child) - mandoc_msg(MANDOCERR_DOC_EMPTY, mdoc->parse, - n->line, n->pos, NULL); - else if (MDOC_Sh != n->child->tok) + n = mdoc->first->child; + while (n != NULL && mdoc_macros[n->tok].flags & MDOC_PROLOGUE) + n = n->next; + + if (n == NULL) + mandoc_msg(MANDOCERR_DOC_EMPTY, mdoc->parse, 0, 0, NULL); + else if (n->tok != MDOC_Sh) mandoc_msg(MANDOCERR_SEC_BEFORE, mdoc->parse, - n->child->line, n->child->pos, - mdoc_macronames[n->child->tok]); + n->line, n->pos, mdoc_macronames[n->tok]); return(1); } @@ -1848,22 +1850,33 @@ post_sh(POST_ARGS) post_ignpar(mdoc); - if (MDOC_HEAD == mdoc->last->type) + switch (mdoc->last->type) { + case MDOC_HEAD: return(post_sh_head(mdoc)); - if (MDOC_BODY == mdoc->last->type) - return(post_sh_body(mdoc)); + case MDOC_BODY: + switch (mdoc->lastsec) { + case SEC_NAME: + return(post_sh_name(mdoc)); + case SEC_SEE_ALSO: + return(post_sh_see_also(mdoc)); + case SEC_AUTHORS: + return(post_sh_authors(mdoc)); + default: + break; + } + break; + default: + break; + } return(1); } static int -post_sh_body(POST_ARGS) +post_sh_name(POST_ARGS) { struct mdoc_node *n; - if (SEC_NAME != mdoc->lastsec) - return(1); - /* * Warn if the NAME section doesn't contain the `Nm' and `Nd' * macros (can have multiple `Nm' and one `Nd'). Note that the @@ -1891,6 +1904,88 @@ post_sh_body(POST_ARGS) mandoc_msg(MANDOCERR_NAMESEC_BAD, mdoc->parse, n->line, n->pos, mdoc_macronames[n->tok]); + return(1); +} + +static int +post_sh_see_also(POST_ARGS) +{ + const struct mdoc_node *n; + const char *name, *sec; + const char *lastname, *lastsec, *lastpunct; + int cmp; + + n = mdoc->last->child; + lastname = lastsec = lastpunct = NULL; + while (n != NULL) { + if (n->tok != MDOC_Xr || n->nchild < 2) + break; + + /* Process one .Xr node. */ + + name = n->child->string; + sec = n->child->next->string; + if (lastsec != NULL) { + if (lastpunct[0] != ',' || lastpunct[1] != '\0') + mandoc_vmsg(MANDOCERR_XR_PUNCT, + mdoc->parse, n->line, n->pos, + "%s before %s(%s)", lastpunct, + name, sec); + cmp = strcmp(lastsec, sec); + if (cmp > 0) + mandoc_vmsg(MANDOCERR_XR_ORDER, + mdoc->parse, n->line, n->pos, + "%s(%s) after %s(%s)", name, + sec, lastname, lastsec); + else if (cmp == 0 && strcmp(lastname, name) > 0) + mandoc_vmsg(MANDOCERR_XR_ORDER, + mdoc->parse, n->line, n->pos, + "%s after %s", name, lastname); + } + lastname = name; + lastsec = sec; + + /* Process the following node. */ + + n = n->next; + if (n == NULL) + break; + if (n->tok == MDOC_Xr) { + lastpunct = "none"; + continue; + } + if (n->type != MDOC_TEXT) + break; + for (name = n->string; *name != '\0'; name++) + if (isalpha((const unsigned char)*name)) + return(1); + lastpunct = n->string; + if (n->next == NULL) + mandoc_vmsg(MANDOCERR_XR_PUNCT, mdoc->parse, + n->line, n->pos, "%s after %s(%s)", + lastpunct, lastname, lastsec); + n = n->next; + } + return(1); +} + +static int +child_an(const struct mdoc_node *n) +{ + + for (n = n->child; n != NULL; n = n->next) + if ((n->tok == MDOC_An && n->nchild) || child_an(n)) + return(1); + return(0); +} + +static int +post_sh_authors(POST_ARGS) +{ + + if ( ! child_an(mdoc->last)) + mandoc_msg(MANDOCERR_AN_MISSING, mdoc->parse, + mdoc->last->line, mdoc->last->pos, NULL); return(1); }