=================================================================== RCS file: /cvs/mandoc/Attic/validate.c,v retrieving revision 1.17 retrieving revision 1.22 diff -u -p -r1.17 -r1.22 --- mandoc/Attic/validate.c 2009/01/07 15:53:00 1.17 +++ mandoc/Attic/validate.c 2009/01/09 15:07:04 1.22 @@ -1,4 +1,4 @@ -/* $Id: validate.c,v 1.17 2009/01/07 15:53:00 kristaps Exp $ */ +/* $Id: validate.c,v 1.22 2009/01/09 15:07:04 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -22,9 +22,8 @@ #include "private.h" -typedef int (*v_pre)(struct mdoc *, int, int, - int, const struct mdoc_arg *); -typedef int (*v_post)(struct mdoc *, int, int); +typedef int (*v_pre)(struct mdoc *, struct mdoc_node *); +typedef int (*v_post)(struct mdoc *); struct valids { @@ -33,25 +32,30 @@ struct valids { }; -static int pre_sh(struct mdoc *, int, int, - int, const struct mdoc_arg *); -static int post_headchild_err_ge1(struct mdoc *, int, int); -static int post_headchild_err_le8(struct mdoc *, int, int); -static int post_bodychild_warn_ge1(struct mdoc *, int, int); +static int pre_prologue(struct mdoc *, struct mdoc_node *); +static int pre_prologue(struct mdoc *, struct mdoc_node *); +static int pre_prologue(struct mdoc *, struct mdoc_node *); +static int post_headchild_err_ge1(struct mdoc *); +static int post_elemchild_err_ge1(struct mdoc *); +static int post_elemchild_warn_eq0(struct mdoc *); +static int post_bodychild_warn_ge1(struct mdoc *); +static int post_sh(struct mdoc *); -static v_post posts_sh[] = { post_headchild_err_ge1, - post_bodychild_warn_ge1, - post_headchild_err_le8, NULL }; +static v_post posts_sh[] = { post_headchild_err_ge1, + post_bodychild_warn_ge1, post_sh, NULL }; +static v_post posts_ss[] = { post_headchild_err_ge1, NULL }; +static v_post posts_pp[] = { post_elemchild_warn_eq0, NULL }; +static v_post posts_dd[] = { post_elemchild_err_ge1, NULL }; const struct valids mdoc_valids[MDOC_MAX] = { { NULL, NULL }, /* \" */ - { NULL, NULL }, /* Dd */ /* TODO */ - { NULL, NULL }, /* Dt */ /* TODO */ - { NULL, NULL }, /* Os */ /* TODO */ - { pre_sh, posts_sh }, /* Sh */ /* FIXME: preceding Pp. */ - { NULL, NULL }, /* Ss */ /* FIXME: preceding Pp. */ - { NULL, NULL }, /* Pp */ + { pre_prologue, posts_dd }, /* Dd */ + { pre_prologue, NULL }, /* Dt */ + { pre_prologue, NULL }, /* Os */ + { NULL, posts_sh }, /* Sh */ /* FIXME: preceding Pp. */ + { NULL, posts_ss }, /* Ss */ /* FIXME: preceding Pp. */ + { NULL, posts_pp }, /* Pp */ /* FIXME: proceeding... */ { NULL, NULL }, /* D1 */ { NULL, NULL }, /* Dl */ { NULL, NULL }, /* Bd */ /* FIXME: preceding Pp. */ @@ -155,7 +159,7 @@ const struct valids mdoc_valids[MDOC_MAX] = { static int -post_bodychild_warn_ge1(struct mdoc *mdoc, int tok, int pos) +post_bodychild_warn_ge1(struct mdoc *mdoc) { if (MDOC_BODY != mdoc->last->type) @@ -163,60 +167,165 @@ post_bodychild_warn_ge1(struct mdoc *mdoc, int tok, in if (mdoc->last->child) return(1); - return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1)); + return(mdoc_warn(mdoc, WARN_ARGS_GE1)); } static int -post_headchild_err_ge1(struct mdoc *mdoc, int tok, int pos) +post_elemchild_warn_eq0(struct mdoc *mdoc) { - if (MDOC_HEAD != mdoc->last->type) + assert(MDOC_ELEM == mdoc->last->type); + if (NULL == mdoc->last->child) return(1); + return(mdoc_warn(mdoc, WARN_ARGS_EQ0)); +} + + +static int +post_elemchild_err_ge1(struct mdoc *mdoc) +{ + + assert(MDOC_ELEM == mdoc->last->type); if (mdoc->last->child) return(1); - return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1)); + return(mdoc_err(mdoc, ERR_ARGS_GE1)); } static int -post_headchild_err_le8(struct mdoc *mdoc, int tok, int pos) +post_headchild_err_ge1(struct mdoc *mdoc) { - int i; - struct mdoc_node *n; if (MDOC_HEAD != mdoc->last->type) return(1); - for (i = 0, n = mdoc->last->child; n; n = n->next, i++) - /* Do nothing. */ ; - if (i <= 8) + if (mdoc->last->child) return(1); - return(mdoc_err(mdoc, tok, pos, ERR_ARGS_LE8)); + return(mdoc_err(mdoc, ERR_ARGS_GE1)); } static int -pre_sh(struct mdoc *mdoc, int tok, int pos, - int argc, const struct mdoc_arg *argv) +pre_prologue(struct mdoc *mdoc, struct mdoc_node *node) { - return(1); + if (SEC_PROLOGUE != mdoc->sec_lastn) + return(mdoc_verr(mdoc, node, ERR_SEC_NPROLOGUE)); + assert(MDOC_ELEM == node->type); + + /* Check for ordering. */ + + switch (node->data.elem.tok) { + case (MDOC_Os): + if (mdoc->meta.title[0] && mdoc->meta.date) + break; + return(mdoc_verr(mdoc, node, ERR_SEC_PROLOGUE_OO)); + case (MDOC_Dt): + if (0 == mdoc->meta.title[0] && mdoc->meta.date) + break; + return(mdoc_verr(mdoc, node, ERR_SEC_PROLOGUE_OO)); + case (MDOC_Dd): + if (0 == mdoc->meta.title[0] && 0 == mdoc->meta.date) + break; + return(mdoc_verr(mdoc, node, ERR_SEC_PROLOGUE_OO)); + default: + abort(); + /* NOTREACHED */ + } + + /* Check for repetition. */ + + switch (node->data.elem.tok) { + case (MDOC_Os): + if (0 == mdoc->meta.os[0]) + return(1); + break; + case (MDOC_Dd): + if (0 == mdoc->meta.date) + return(1); + break; + case (MDOC_Dt): + if (0 == mdoc->meta.title[0]) + return(1); + break; + default: + abort(); + /* NOTREACHED */ + } + + return(mdoc_verr(mdoc, node, ERR_SEC_PROLOGUE_REP)); } +/* + * Warn if sections (those that are with a known title, such as NAME, + * DESCRIPTION, and so forth) are out of the conventional order. + */ +static int +post_sh(struct mdoc *mdoc) +{ + enum mdoc_sec sec; + int i; + struct mdoc_node *n; + char *args[MDOC_LINEARG_MAX]; + + if (MDOC_HEAD != mdoc->last->type) + return(1); + + assert(MDOC_Sh == mdoc->last->data.head.tok); + + n = mdoc->last->child; + assert(n); + + for (i = 0; n && i < MDOC_LINEARG_MAX; n = n->next, i++) { + assert(MDOC_TEXT == n->type); + assert(NULL == n->child); + assert(n->data.text.string); + args[i] = n->data.text.string; + } + + sec = mdoc_atosec((size_t)i, (const char **)args); + if (SEC_CUSTOM == sec) + return(1); + if (sec > mdoc->sec_lastn) + return(1); + + if (sec == mdoc->sec_lastn) + return(mdoc_warn(mdoc, WARN_SEC_REP)); + return(mdoc_warn(mdoc, WARN_SEC_OO)); +} + + int -mdoc_valid_pre(struct mdoc *mdoc, int tok, int pos, - int argc, const struct mdoc_arg *argv) +mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *node) { + int t; - if (NULL == mdoc_valids[tok].pre) + switch (node->type) { + case (MDOC_BODY): + t = node->data.body.tok; + break; + case (MDOC_ELEM): + t = node->data.elem.tok; + break; + case (MDOC_BLOCK): + t = node->data.block.tok; + break; + case (MDOC_HEAD): + t = node->data.head.tok; + break; + default: return(1); - return((*mdoc_valids[tok].pre)(mdoc, tok, pos, argc, argv)); + } + + if (NULL == mdoc_valids[t].pre) + return(1); + return((*mdoc_valids[t].pre)(mdoc, node)); } int -mdoc_valid_post(struct mdoc *mdoc, int pos) +mdoc_valid_post(struct mdoc *mdoc) { v_post *p; int t; @@ -242,7 +351,7 @@ mdoc_valid_post(struct mdoc *mdoc, int pos) return(1); for (p = mdoc_valids[t].post; *p; p++) - if ( ! (*p)(mdoc, t, pos)) + if ( ! (*p)(mdoc)) return(0); return(1);