version 1.9, 2008/12/30 18:15:26 |
version 1.21, 2009/01/09 14:45:44 |
|
|
#include "private.h" |
#include "private.h" |
|
|
|
|
typedef int (*v_sz)(struct mdoc *, int, int, int); |
typedef int (*v_pre)(struct mdoc *, struct mdoc_node *); |
typedef int (*v_extra)(struct mdoc *, int, int, int, |
typedef int (*v_post)(struct mdoc *); |
const char *[], int, const struct mdoc_arg *); |
|
|
|
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 { |
struct valids { |
v_sz sz; |
v_pre pre; |
v_extra extra; |
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] = { |
const struct valids mdoc_valids[MDOC_MAX] = { |
{ NULL, NULL }, /* \" */ |
{ NULL, NULL }, /* \" */ |
{ NULL, NULL }, /* Dd */ |
{ pre_prologue, posts_dd }, /* Dd */ |
{ NULL, NULL }, /* Dt */ |
{ pre_prologue, NULL }, /* Dt */ |
{ NULL, NULL }, /* Os */ |
{ pre_prologue, NULL }, /* Os */ |
{ need_ge1, v_Sh }, /* Sh */ |
{ NULL, posts_sh }, /* Sh */ /* FIXME: preceding Pp. */ |
{ need_ge1, NULL }, /* Ss */ |
{ NULL, NULL }, /* Ss */ /* FIXME: preceding Pp. */ |
{ want_eq0, NULL }, /* Pp */ |
{ NULL, NULL }, /* Pp */ |
{ NULL, NULL }, /* D1 */ |
{ NULL, NULL }, /* D1 */ |
{ NULL, NULL }, /* Dl */ |
{ NULL, NULL }, /* Dl */ |
{ NULL, v_Bd }, /* Bd */ |
{ NULL, NULL }, /* Bd */ /* FIXME: preceding Pp. */ |
{ NULL, NULL }, /* Ed */ |
{ NULL, NULL }, /* Ed */ |
{ NULL, NULL }, /* Bl */ |
{ NULL, NULL }, /* Bl */ /* FIXME: preceding Pp. */ |
{ NULL, NULL }, /* El */ |
{ NULL, NULL }, /* El */ |
{ NULL, NULL }, /* It */ |
{ NULL, NULL }, /* It */ |
{ need_ge1, NULL }, /* Ad */ |
{ NULL, NULL }, /* Ad */ |
{ NULL, NULL }, /* An */ |
{ NULL, NULL }, /* An */ |
{ NULL, NULL }, /* Ar */ |
{ NULL, NULL }, /* Ar */ |
{ NULL, NULL }, /* Cd */ |
{ NULL, NULL }, /* Cd */ |
{ NULL, NULL }, /* Cm */ |
{ NULL, NULL }, /* Cm */ |
{ need_ge1, NULL }, /* Dv */ |
{ NULL, NULL }, /* Dv */ |
{ need_ge1, NULL }, /* Er */ |
{ NULL, NULL }, /* Er */ |
{ need_ge1, NULL }, /* Ev */ |
{ NULL, NULL }, /* Ev */ |
{ NULL, NULL }, /* Ex */ |
{ NULL, NULL }, /* Ex */ |
{ need_ge1, NULL }, /* Fa */ |
{ NULL, NULL }, /* Fa */ |
{ NULL, NULL }, /* Fd */ |
{ NULL, NULL }, /* Fd */ |
{ NULL, NULL }, /* Fl */ |
{ NULL, NULL }, /* Fl */ |
{ NULL, NULL }, /* Fn */ |
{ NULL, NULL }, /* Fn */ |
{ want_ge1, NULL }, /* Ft */ |
{ NULL, NULL }, /* Ft */ |
{ need_ge1, NULL }, /* Ic */ |
{ NULL, NULL }, /* Ic */ |
{ NULL, NULL }, /* In */ |
{ NULL, NULL }, /* In */ |
{ want_ge1, NULL }, /* Li */ |
{ NULL, NULL }, /* Li */ |
{ want_ge1, NULL }, /* Nd */ |
{ NULL, NULL }, /* Nd */ |
{ NULL, NULL }, /* Nm */ |
{ NULL, NULL }, /* Nm */ |
{ NULL, NULL }, /* Op */ |
{ NULL, NULL }, /* Op */ |
{ NULL, NULL }, /* Ot */ |
{ NULL, NULL }, /* Ot */ |
{ want_ge1, NULL }, /* Pa */ |
{ NULL, NULL }, /* Pa */ |
{ NULL, NULL }, /* Rv */ |
{ NULL, NULL }, /* Rv */ |
{ NULL, NULL }, /* St */ |
{ NULL, NULL }, /* St */ |
{ need_ge1, NULL }, /* Va */ |
{ NULL, NULL }, /* Va */ |
{ need_ge1, NULL }, /* Vt */ |
{ NULL, NULL }, /* Vt */ |
{ NULL, NULL }, /* Xr */ |
{ NULL, NULL }, /* Xr */ |
{ NULL, NULL }, /* %A */ |
{ NULL, NULL }, /* %A */ |
{ NULL, NULL }, /* %B */ |
{ NULL, NULL }, /* %B */ |
Line 103 const struct valids mdoc_valids[MDOC_MAX] = { |
|
Line 101 const struct valids mdoc_valids[MDOC_MAX] = { |
|
{ NULL, NULL }, /* Ac */ |
{ NULL, NULL }, /* Ac */ |
{ NULL, NULL }, /* Ao */ |
{ NULL, NULL }, /* Ao */ |
{ NULL, NULL }, /* Aq */ |
{ NULL, NULL }, /* Aq */ |
{ need_le2, v_At }, /* At */ |
{ NULL, NULL }, /* At */ /* FIXME */ |
{ NULL, NULL }, /* Bc */ |
{ NULL, NULL }, /* Bc */ |
{ NULL, NULL }, /* Bf */ |
{ NULL, NULL }, /* Bf */ |
{ NULL, NULL }, /* Bo */ |
{ NULL, NULL }, /* Bo */ |
{ NULL, NULL }, /* Bq */ |
{ NULL, NULL }, /* Bq */ |
{ assert_le1, NULL }, /* Bsx */ |
{ NULL, NULL }, /* Bsx */ |
{ assert_le1, NULL }, /* Bx */ |
{ NULL, NULL }, /* Bx */ |
{ NULL, NULL }, /* Db */ |
{ NULL, NULL }, /* Db */ |
{ NULL, NULL }, /* Dc */ |
{ NULL, NULL }, /* Dc */ |
{ NULL, NULL }, /* Do */ |
{ NULL, NULL }, /* Do */ |
{ NULL, NULL }, /* Dq */ |
{ NULL, NULL }, /* Dq */ |
{ NULL, NULL }, /* Ec */ |
{ NULL, NULL }, /* Ec */ |
{ NULL, NULL }, /* Ef */ |
{ NULL, NULL }, /* Ef */ /* -symbolic, etc. */ |
{ need_ge1, NULL }, /* Em */ |
{ NULL, NULL }, /* Em */ |
{ NULL, NULL }, /* Eo */ |
{ NULL, NULL }, /* Eo */ |
{ assert_le1, NULL }, /* Fx */ |
{ NULL, NULL }, /* Fx */ |
{ want_ge1, NULL }, /* Ms */ |
{ NULL, NULL }, /* Ms */ |
{ NULL, NULL }, /* No */ |
{ NULL, NULL }, /* No */ |
{ NULL, NULL }, /* Ns */ |
{ NULL, NULL }, /* Ns */ |
{ assert_le1, NULL }, /* Nx */ |
{ NULL, NULL }, /* Nx */ |
{ assert_le1, NULL }, /* Ox */ |
{ NULL, NULL }, /* Ox */ |
{ NULL, NULL }, /* Pc */ |
{ NULL, NULL }, /* Pc */ |
{ NULL, NULL }, /* Pf */ |
{ NULL, NULL }, /* Pf */ /* 2 or more arguments */ |
{ NULL, NULL }, /* Po */ |
{ NULL, NULL }, /* Po */ |
{ NULL, NULL }, /* Pq */ |
{ NULL, NULL }, /* Pq */ /* FIXME: ignore following Sh/Ss */ |
{ NULL, NULL }, /* Qc */ |
{ NULL, NULL }, /* Qc */ |
{ NULL, NULL }, /* Ql */ |
{ NULL, NULL }, /* Ql */ |
{ NULL, NULL }, /* Qo */ |
{ NULL, NULL }, /* Qo */ |
Line 138 const struct valids mdoc_valids[MDOC_MAX] = { |
|
Line 136 const struct valids mdoc_valids[MDOC_MAX] = { |
|
{ NULL, NULL }, /* So */ |
{ NULL, NULL }, /* So */ |
{ NULL, NULL }, /* Sq */ |
{ NULL, NULL }, /* Sq */ |
{ NULL, NULL }, /* Sm */ |
{ NULL, NULL }, /* Sm */ |
{ need_ge1, NULL }, /* Sx */ |
{ NULL, NULL }, /* Sx */ |
{ need_ge1, NULL }, /* Sy */ |
{ NULL, NULL }, /* Sy */ |
{ want_ge1, NULL }, /* Tn */ |
{ NULL, NULL }, /* Tn */ |
{ assert_eq0, NULL }, /* Ux */ |
{ NULL, NULL }, /* Ux */ |
{ NULL, NULL }, /* Xc */ |
{ NULL, NULL }, /* Xc */ |
{ NULL, NULL }, /* Xo */ |
{ NULL, NULL }, /* Xo */ |
{ NULL, NULL }, /* Fo */ |
{ NULL, NULL }, /* Fo */ |
Line 150 const struct valids mdoc_valids[MDOC_MAX] = { |
|
Line 148 const struct valids mdoc_valids[MDOC_MAX] = { |
|
{ NULL, NULL }, /* Oc */ |
{ NULL, NULL }, /* Oc */ |
{ NULL, NULL }, /* Bk */ |
{ NULL, NULL }, /* Bk */ |
{ NULL, NULL }, /* Ek */ |
{ NULL, NULL }, /* Ek */ |
{ need_eq0, NULL }, /* Bt */ |
{ NULL, NULL }, /* Bt */ |
{ need_eq1, NULL }, /* Hf */ |
{ NULL, NULL }, /* Hf */ |
{ NULL, NULL }, /* Fr */ |
{ NULL, NULL }, /* Fr */ |
{ need_eq0, NULL }, /* Ud */ |
{ NULL, NULL }, /* Ud */ |
}; |
}; |
|
|
|
|
static int |
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)); |
|
} |
|
|
|
|
if (MDOC_BODY != mdoc->last->type) |
static int |
|
want_ge1(struct mdoc *mdoc, int tok, int pos, int sz) |
|
{ |
|
if (sz > 0) |
|
return(1); |
return(1); |
return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1)); |
if (mdoc->last->child) |
} |
|
|
|
|
|
static int |
|
want_eq0(struct mdoc *mdoc, int tok, int pos, int sz) |
|
{ |
|
if (sz == 0) |
|
return(1); |
return(1); |
return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_EQ0)); |
|
} |
|
|
|
|
return(mdoc_warn(mdoc, WARN_ARGS_GE1)); |
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)); |
|
} |
} |
|
|
|
|
static int |
static int |
assert_le1(struct mdoc *mdoc, int tok, int pos, int sz) |
post_elemchild_err_ge1(struct mdoc *mdoc) |
{ |
{ |
|
|
assert(sz <= 1); |
assert(MDOC_ELEM == mdoc->last->type); |
return(1); |
if (mdoc->last->child) |
|
return(1); |
|
return(mdoc_err(mdoc, ERR_ARGS_GE1)); |
} |
} |
|
|
|
|
static int |
static int |
assert_eq0(struct mdoc *mdoc, int tok, int pos, int sz) |
post_headchild_err_ge1(struct mdoc *mdoc) |
{ |
{ |
|
|
assert(sz == 0); |
if (MDOC_HEAD != mdoc->last->type) |
return(1); |
return(1); |
|
if (mdoc->last->child) |
|
return(1); |
|
return(mdoc_err(mdoc, ERR_ARGS_GE1)); |
} |
} |
|
|
|
|
static int |
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 |
/* Check for ordering. */ |
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)); |
|
} |
|
|
|
|
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 |
/* Check for repetition. */ |
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; |
|
|
|
sec = mdoc_atosec((size_t)sz, args); |
switch (node->data.elem.tok) { |
if (SEC_CUSTOM != sec && sec < mdoc->sec_lastn) |
case (MDOC_Os): |
if ( ! mdoc_warn(mdoc, tok, pos, WARN_SEC_OO)) |
if (0 == mdoc->meta.os[0]) |
return(0); |
return(1); |
if (SEC_BODY == mdoc->sec_last && SEC_NAME != sec) |
break; |
return(mdoc_err(mdoc, tok, pos, ERR_SEC_NAME)); |
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 |
static int |
v_At(struct mdoc *mdoc, int tok, int pos, |
post_sh(struct mdoc *mdoc) |
int sz, const char *args[], |
|
int argc, const struct mdoc_arg *argv) |
|
{ |
{ |
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); |
return(1); |
|
|
|
assert(MDOC_Sh == mdoc->last->data.head.tok); |
|
|
i = 0; |
n = mdoc->last->child; |
if (ATT_DEFAULT == mdoc_atoatt(args[i])) |
assert(n); |
i++; |
|
|
|
for ( ; i < sz; i++) { |
for (i = 0; n && i < MDOC_LINEARG_MAX; n = n->next, i++) { |
if ( ! mdoc_isdelim(args[i])) |
assert(MDOC_TEXT == n->type); |
continue; |
assert(NULL == n->child); |
return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_NOPUNCT)); |
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 |
int |
v_Bd(struct mdoc *mdoc, int tok, int pos, |
mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *node) |
int sz, const char *args[], |
|
int argc, const struct mdoc_arg *argv) |
|
{ |
{ |
struct mdoc_node *node; |
int t; |
|
|
/* |
switch (node->type) { |
* We can't be nested within any other block displays (or really |
case (MDOC_BODY): |
* any other kind of display, although Bd is the only multi-line |
t = node->data.body.tok; |
* 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; |
|
break; |
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(1); |
return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NONEST)); |
return((*mdoc_valids[t].pre)(mdoc, node)); |
} |
} |
|
|
|
|
int |
int |
mdoc_valid(struct mdoc *mdoc, int tok, int pos, |
mdoc_valid_post(struct mdoc *mdoc) |
int sz, const char *args[], |
|
int argc, const struct mdoc_arg *argv) |
|
{ |
{ |
|
v_post *p; |
|
int t; |
|
|
assert(tok < MDOC_MAX); |
switch (mdoc->last->type) { |
if (mdoc_valids[tok].sz) |
case (MDOC_BODY): |
if ( ! (*mdoc_valids[tok].sz)(mdoc, tok, pos, sz)) |
t = mdoc->last->data.body.tok; |
return(0); |
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(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); |
} |
} |
|
|