=================================================================== RCS file: /cvs/mandoc/Attic/validate.c,v retrieving revision 1.10 retrieving revision 1.21 diff -u -p -r1.10 -r1.21 --- mandoc/Attic/validate.c 2008/12/30 19:06:03 1.10 +++ mandoc/Attic/validate.c 2009/01/09 14:45:44 1.21 @@ -1,4 +1,4 @@ -/* $Id: validate.c,v 1.10 2008/12/30 19:06:03 kristaps Exp $ */ +/* $Id: validate.c,v 1.21 2009/01/09 14:45:44 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -22,72 +22,70 @@ #include "private.h" -typedef int (*v_sz)(struct mdoc *, int, int, int); -typedef int (*v_extra)(struct mdoc *, int, int, int, - const char *[], int, const struct mdoc_arg *); +typedef int (*v_pre)(struct mdoc *, struct mdoc_node *); +typedef int (*v_post)(struct mdoc *); -static int assert_eq0(struct mdoc *, int, int, int); -static int assert_le1(struct mdoc *, int, int, int); -static int need_eq0(struct mdoc *, int, int, int); -static int need_eq1(struct mdoc *, int, int, int); -static int need_ge1(struct mdoc *, int, int, int); -static int need_le2(struct mdoc *, int, int, int); -static int want_eq0(struct mdoc *, int, int, int); -static int want_ge1(struct mdoc *, int, int, int); -static int v_Sh(struct mdoc *, int, int, int, - const char *[], int, const struct mdoc_arg *); -static int v_Bd(struct mdoc *, int, int, int, - const char *[], int, const struct mdoc_arg *); -static int v_At(struct mdoc *, int, int, int, - const char *[], int, const struct mdoc_arg *); struct valids { - v_sz sz; - v_extra extra; + v_pre pre; + v_post *post; }; +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_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_sh, NULL }; +static v_post posts_dd[] = { post_elemchild_err_ge1, NULL }; + + const struct valids mdoc_valids[MDOC_MAX] = { { NULL, NULL }, /* \" */ - { NULL, NULL }, /* Dd */ - { NULL, NULL }, /* Dt */ - { NULL, NULL }, /* Os */ - { need_ge1, v_Sh }, /* Sh */ - { need_ge1, NULL }, /* Ss */ - { want_eq0, NULL }, /* Pp */ + { pre_prologue, posts_dd }, /* Dd */ + { pre_prologue, NULL }, /* Dt */ + { pre_prologue, NULL }, /* Os */ + { NULL, posts_sh }, /* Sh */ /* FIXME: preceding Pp. */ + { NULL, NULL }, /* Ss */ /* FIXME: preceding Pp. */ + { NULL, NULL }, /* Pp */ { NULL, NULL }, /* D1 */ { NULL, NULL }, /* Dl */ - { NULL, v_Bd }, /* Bd */ + { NULL, NULL }, /* Bd */ /* FIXME: preceding Pp. */ { NULL, NULL }, /* Ed */ - { NULL, NULL }, /* Bl */ + { NULL, NULL }, /* Bl */ /* FIXME: preceding Pp. */ { NULL, NULL }, /* El */ { NULL, NULL }, /* It */ - { need_ge1, NULL }, /* Ad */ - { NULL, NULL }, /* An */ + { NULL, NULL }, /* Ad */ + { NULL, NULL }, /* An */ { NULL, NULL }, /* Ar */ - { need_ge1, NULL }, /* Cd */ + { NULL, NULL }, /* Cd */ { NULL, NULL }, /* Cm */ - { need_ge1, NULL }, /* Dv */ - { need_ge1, NULL }, /* Er */ - { need_ge1, NULL }, /* Ev */ + { NULL, NULL }, /* Dv */ + { NULL, NULL }, /* Er */ + { NULL, NULL }, /* Ev */ { NULL, NULL }, /* Ex */ - { need_ge1, NULL }, /* Fa */ + { NULL, NULL }, /* Fa */ { NULL, NULL }, /* Fd */ { NULL, NULL }, /* Fl */ - { need_ge1, NULL }, /* Fn */ - { want_ge1, NULL }, /* Ft */ - { need_ge1, NULL }, /* Ic */ + { NULL, NULL }, /* Fn */ + { NULL, NULL }, /* Ft */ + { NULL, NULL }, /* Ic */ { NULL, NULL }, /* In */ - { want_ge1, NULL }, /* Li */ - { want_ge1, NULL }, /* Nd */ + { NULL, NULL }, /* Li */ + { NULL, NULL }, /* Nd */ { NULL, NULL }, /* Nm */ { NULL, NULL }, /* Op */ { NULL, NULL }, /* Ot */ - { want_ge1, NULL }, /* Pa */ + { NULL, NULL }, /* Pa */ { NULL, NULL }, /* Rv */ { NULL, NULL }, /* St */ - { need_ge1, NULL }, /* Va */ - { need_ge1, NULL }, /* Vt */ + { NULL, NULL }, /* Va */ + { NULL, NULL }, /* Vt */ { NULL, NULL }, /* Xr */ { NULL, NULL }, /* %A */ { NULL, NULL }, /* %B */ @@ -103,31 +101,31 @@ const struct valids mdoc_valids[MDOC_MAX] = { { NULL, NULL }, /* Ac */ { NULL, NULL }, /* Ao */ { NULL, NULL }, /* Aq */ - { need_le2, v_At }, /* At */ + { NULL, NULL }, /* At */ /* FIXME */ { NULL, NULL }, /* Bc */ { NULL, NULL }, /* Bf */ { NULL, NULL }, /* Bo */ { NULL, NULL }, /* Bq */ - { assert_le1, NULL }, /* Bsx */ - { assert_le1, NULL }, /* Bx */ + { NULL, NULL }, /* Bsx */ + { NULL, NULL }, /* Bx */ { NULL, NULL }, /* Db */ { NULL, NULL }, /* Dc */ { NULL, NULL }, /* Do */ { NULL, NULL }, /* Dq */ { NULL, NULL }, /* Ec */ - { NULL, NULL }, /* Ef */ - { need_ge1, NULL }, /* Em */ + { NULL, NULL }, /* Ef */ /* -symbolic, etc. */ + { NULL, NULL }, /* Em */ { NULL, NULL }, /* Eo */ - { assert_le1, NULL }, /* Fx */ - { want_ge1, NULL }, /* Ms */ + { NULL, NULL }, /* Fx */ + { NULL, NULL }, /* Ms */ { NULL, NULL }, /* No */ { NULL, NULL }, /* Ns */ - { assert_le1, NULL }, /* Nx */ - { assert_le1, NULL }, /* Ox */ + { NULL, NULL }, /* Nx */ + { NULL, NULL }, /* Ox */ { NULL, NULL }, /* Pc */ - { NULL, NULL }, /* Pf */ + { NULL, NULL }, /* Pf */ /* 2 or more arguments */ { NULL, NULL }, /* Po */ - { NULL, NULL }, /* Pq */ + { NULL, NULL }, /* Pq */ /* FIXME: ignore following Sh/Ss */ { NULL, NULL }, /* Qc */ { NULL, NULL }, /* Ql */ { NULL, NULL }, /* Qo */ @@ -138,10 +136,10 @@ const struct valids mdoc_valids[MDOC_MAX] = { { NULL, NULL }, /* So */ { NULL, NULL }, /* Sq */ { NULL, NULL }, /* Sm */ - { need_ge1, NULL }, /* Sx */ - { need_ge1, NULL }, /* Sy */ - { want_ge1, NULL }, /* Tn */ - { assert_eq0, NULL }, /* Ux */ + { NULL, NULL }, /* Sx */ + { NULL, NULL }, /* Sy */ + { NULL, NULL }, /* Tn */ + { NULL, NULL }, /* Ux */ { NULL, NULL }, /* Xc */ { NULL, NULL }, /* Xo */ { NULL, NULL }, /* Fo */ @@ -150,168 +148,198 @@ const struct valids mdoc_valids[MDOC_MAX] = { { NULL, NULL }, /* Oc */ { NULL, NULL }, /* Bk */ { NULL, NULL }, /* Ek */ - { need_eq0, NULL }, /* Bt */ - { need_eq1, NULL }, /* Hf */ + { NULL, NULL }, /* Bt */ + { NULL, NULL }, /* Hf */ { NULL, NULL }, /* Fr */ - { need_eq0, NULL }, /* Ud */ + { NULL, NULL }, /* Ud */ }; static int -need_le2(struct mdoc *mdoc, int tok, int pos, int sz) +post_bodychild_warn_ge1(struct mdoc *mdoc) { - if (sz > 2) - return(1); - return(mdoc_err(mdoc, tok, pos, ERR_ARGS_LE2)); -} - -static int -want_ge1(struct mdoc *mdoc, int tok, int pos, int sz) -{ - if (sz > 0) + if (MDOC_BODY != mdoc->last->type) return(1); - return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1)); -} - - -static int -want_eq0(struct mdoc *mdoc, int tok, int pos, int sz) -{ - if (sz == 0) + if (mdoc->last->child) return(1); - return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_EQ0)); -} - -static int -need_eq0(struct mdoc *mdoc, int tok, int pos, int sz) -{ - if (sz == 0) - return(1); - return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ0)); + return(mdoc_warn(mdoc, WARN_ARGS_GE1)); } static int -assert_le1(struct mdoc *mdoc, int tok, int pos, int sz) +post_elemchild_err_ge1(struct mdoc *mdoc) { - assert(sz <= 1); - return(1); + assert(MDOC_ELEM == mdoc->last->type); + if (mdoc->last->child) + return(1); + return(mdoc_err(mdoc, ERR_ARGS_GE1)); } static int -assert_eq0(struct mdoc *mdoc, int tok, int pos, int sz) +post_headchild_err_ge1(struct mdoc *mdoc) { - assert(sz == 0); - return(1); + if (MDOC_HEAD != mdoc->last->type) + return(1); + if (mdoc->last->child) + return(1); + return(mdoc_err(mdoc, ERR_ARGS_GE1)); } static int -need_eq1(struct mdoc *mdoc, int tok, int pos, int sz) +pre_prologue(struct mdoc *mdoc, struct mdoc_node *node) { - if (sz == 1) - return(1); - return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ1)); -} + if (SEC_PROLOGUE != mdoc->sec_lastn) + return(mdoc_verr(mdoc, node, ERR_SEC_NPROLOGUE)); + assert(MDOC_ELEM == node->type); -static int -need_ge1(struct mdoc *mdoc, int tok, int pos, int sz) -{ - if (sz > 0) - return(1); - return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1)); -} + /* 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 */ + } -static int -v_Sh(struct mdoc *mdoc, int tok, int pos, - int sz, const char *args[], - int argc, const struct mdoc_arg *argv) -{ - enum mdoc_sec sec; + /* Check for repetition. */ - sec = mdoc_atosec((size_t)sz, args); - if (SEC_CUSTOM != sec && sec < mdoc->sec_lastn) - if ( ! mdoc_warn(mdoc, tok, pos, WARN_SEC_OO)) - return(0); - if (SEC_BODY == mdoc->sec_last && SEC_NAME != sec) - return(mdoc_err(mdoc, tok, pos, ERR_SEC_NAME)); + 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(1); + 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 -v_At(struct mdoc *mdoc, int tok, int pos, - int sz, const char *args[], - int argc, const struct mdoc_arg *argv) +post_sh(struct mdoc *mdoc) { - int i; + enum mdoc_sec sec; + int i; + struct mdoc_node *n; + char *args[MDOC_LINEARG_MAX]; - if (0 == sz) + if (MDOC_HEAD != mdoc->last->type) return(1); + + assert(MDOC_Sh == mdoc->last->data.head.tok); - i = 0; - if (ATT_DEFAULT == mdoc_atoatt(args[i])) - i++; + n = mdoc->last->child; + assert(n); - for ( ; i < sz; i++) { - if ( ! mdoc_isdelim(args[i])) - continue; - return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_NOPUNCT)); + 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; } - return(1); + + 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)); } -static int -v_Bd(struct mdoc *mdoc, int tok, int pos, - int sz, const char *args[], - int argc, const struct mdoc_arg *argv) +int +mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *node) { - struct mdoc_node *node; + int t; - /* - * We can't be nested within any other block displays (or really - * any other kind of display, although Bd is the only multi-line - * one that will show up). - */ - assert(mdoc->last); - - /* LINTED */ - for (node = mdoc->last->parent ; node; node = node->parent) { - if (node->type != MDOC_BLOCK) - continue; - if (node->data.block.tok != MDOC_Bd) - continue; + 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); } - if (NULL == node) + + if (NULL == mdoc_valids[t].pre) return(1); - return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NONEST)); + return((*mdoc_valids[t].pre)(mdoc, node)); } int -mdoc_valid(struct mdoc *mdoc, int tok, int pos, - int sz, const char *args[], - int argc, const struct mdoc_arg *argv) +mdoc_valid_post(struct mdoc *mdoc) { + v_post *p; + int t; - assert(tok < MDOC_MAX); - if (mdoc_valids[tok].sz) - if ( ! (*mdoc_valids[tok].sz)(mdoc, tok, pos, sz)) - return(0); + switch (mdoc->last->type) { + case (MDOC_BODY): + t = mdoc->last->data.body.tok; + break; + case (MDOC_ELEM): + t = mdoc->last->data.elem.tok; + break; + case (MDOC_BLOCK): + t = mdoc->last->data.block.tok; + break; + case (MDOC_HEAD): + t = mdoc->last->data.head.tok; + break; + default: + return(1); + } - if (NULL == mdoc_valids[tok].extra) + if (NULL == mdoc_valids[t].post) return(1); - return(*mdoc_valids[tok].extra)(mdoc, - tok, pos, sz, args, argc, argv); + + for (p = mdoc_valids[t].post; *p; p++) + if ( ! (*p)(mdoc)) + return(0); + + return(1); }