-/* $Id: mdoc_validate.c,v 1.105 2010/07/02 12:54:33 kristaps Exp $ */
+/* $Id: mdoc_validate.c,v 1.119 2010/09/27 23:03:44 schwarze Exp $ */
/*
- * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
#include "libmandoc.h"
/* FIXME: .Bl -diag can't have non-text children in HEAD. */
-/* TODO: ignoring Pp (it's superfluous in some invocations). */
#define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
#define POST_ARGS struct mdoc *mdoc
static int berr_ge1(POST_ARGS);
static int bwarn_ge1(POST_ARGS);
static int ebool(POST_ARGS);
-static int eerr_eq0(POST_ARGS);
static int eerr_eq1(POST_ARGS);
static int eerr_ge1(POST_ARGS);
static int eerr_le1(POST_ARGS);
+static int ewarn_eq0(POST_ARGS);
static int ewarn_ge1(POST_ARGS);
static int herr_eq0(POST_ARGS);
static int herr_ge1(POST_ARGS);
static int pre_dt(PRE_ARGS);
static int pre_it(PRE_ARGS);
static int pre_os(PRE_ARGS);
+static int pre_pp(PRE_ARGS);
static int pre_rv(PRE_ARGS);
static int pre_sh(PRE_ARGS);
static int pre_ss(PRE_ARGS);
static v_post posts_lb[] = { eerr_eq1, post_lb, NULL };
static v_post posts_nd[] = { berr_ge1, NULL };
static v_post posts_nm[] = { post_nm, NULL };
-static v_post posts_notext[] = { eerr_eq0, NULL };
+static v_post posts_notext[] = { ewarn_eq0, NULL };
static v_post posts_rs[] = { berr_ge1, herr_eq0, post_rs, NULL };
static v_post posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
static v_post posts_sp[] = { eerr_le1, NULL };
static v_post posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
static v_post posts_wtext[] = { ewarn_ge1, NULL };
static v_pre pres_an[] = { pre_an, NULL };
-static v_pre pres_bd[] = { pre_display, pre_bd, NULL };
-static v_pre pres_bl[] = { pre_bl, NULL };
+static v_pre pres_bd[] = { pre_display, pre_bd, pre_pp, NULL };
+static v_pre pres_bl[] = { pre_bl, pre_pp, NULL };
static v_pre pres_d1[] = { pre_display, NULL };
static v_pre pres_dd[] = { pre_dd, NULL };
static v_pre pres_dt[] = { pre_dt, NULL };
static v_pre pres_fd[] = { NULL, NULL };
static v_pre pres_it[] = { pre_it, NULL };
static v_pre pres_os[] = { pre_os, NULL };
+static v_pre pres_pp[] = { pre_pp, NULL };
static v_pre pres_rv[] = { pre_rv, NULL };
static v_pre pres_sh[] = { pre_sh, NULL };
static v_pre pres_ss[] = { pre_ss, NULL };
const struct valids mdoc_valids[MDOC_MAX] = {
{ NULL, NULL }, /* Ap */
- { pres_dd, posts_text }, /* Dd */
+ { pres_dd, posts_wtext }, /* Dd */
{ pres_dt, posts_dt }, /* Dt */
{ pres_os, NULL }, /* Os */
{ pres_sh, posts_sh }, /* Sh */
{ pres_ss, posts_ss }, /* Ss */
- { NULL, posts_notext }, /* Pp */
+ { pres_pp, posts_notext }, /* Pp */
{ pres_d1, posts_wline }, /* D1 */
{ pres_d1, posts_wline }, /* Dl */
{ pres_bd, posts_bd_bk }, /* Bd */
{ NULL, NULL }, /* Dx */
{ NULL, posts_text }, /* %Q */
{ NULL, posts_notext }, /* br */
- { NULL, posts_sp }, /* sp */
+ { pres_pp, posts_sp }, /* sp */
{ NULL, posts_text1 }, /* %U */
{ NULL, NULL }, /* Ta */
};
CHECK_CHILD_DEFN(warn, lt, <) /* warn_child_lt() */
CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0) /* bwarn_ge1() */
CHECK_BODY_DEFN(ge1, err, err_child_gt, 0) /* berr_ge1() */
+CHECK_ELEM_DEFN(eq0, warn, warn_child_eq, 0) /* ewarn_eq0() */
CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0) /* ewarn_ge1() */
CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1) /* eerr_eq1() */
CHECK_ELEM_DEFN(le1, err, err_child_lt, 2) /* eerr_le1() */
-CHECK_ELEM_DEFN(eq0, err, err_child_eq, 0) /* eerr_eq0() */
CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0) /* eerr_ge1() */
CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0) /* herr_eq0() */
CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2) /* hwarn_le1() */
static int
-check_text(struct mdoc *mdoc, int line, int pos, char *p)
+check_text(struct mdoc *m, int ln, int pos, char *p)
{
int c;
-
- /*
- * FIXME: we absolutely cannot let \b get through or it will
- * destroy some assumptions in terms of format.
- */
+ size_t sz;
for ( ; *p; p++, pos++) {
+ sz = strcspn(p, "\t\\");
+ p += (int)sz;
+
+ if ('\0' == *p)
+ break;
+
+ pos += (int)sz;
+
if ('\t' == *p) {
- if ( ! (MDOC_LITERAL & mdoc->flags))
- if ( ! mdoc_pmsg(mdoc, line, pos, MANDOCERR_BADCHAR))
- return(0);
- } else if ( ! isprint((u_char)*p) && ASCII_HYPH != *p)
- if ( ! mdoc_pmsg(mdoc, line, pos, MANDOCERR_BADCHAR))
- return(0);
+ if (MDOC_LITERAL & m->flags)
+ continue;
+ if (mdoc_pmsg(m, ln, pos, MANDOCERR_BADTAB))
+ continue;
+ return(0);
+ }
- if ('\\' != *p)
- continue;
+ /* Check the special character. */
c = mandoc_special(p);
if (c) {
p += c - 1;
pos += c - 1;
- continue;
- }
-
- c = mdoc_pmsg(mdoc, line, pos, MANDOCERR_BADESCAPE);
- if ( ! (MDOC_IGN_ESCAPE & mdoc->pflags) && ! c)
- return(c);
+ } else
+ mdoc_pmsg(m, ln, pos, MANDOCERR_BADESCAPE);
}
return(1);
}
-
-
static int
check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
{
}
-
static int
pre_display(PRE_ARGS)
{
if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV))
return(0);
break;
+ default:
+ continue;
}
/* Check: duplicate auxiliary arguments. */
/* Assign list type. */
- if (LIST__NONE != lt && n->data.Bl->type == LIST__NONE)
+ if (LIST__NONE != lt && n->data.Bl->type == LIST__NONE) {
n->data.Bl->type = lt;
+ /* Set column information, too. */
+ if (LIST_column == lt) {
+ n->data.Bl->ncols =
+ n->args->argv[i].sz;
+ n->data.Bl->cols = (const char **)
+ n->args->argv[i].value;
+ }
+ }
/* The list type should come first. */
pre_an(PRE_ARGS)
{
- if (NULL == n->args || 1 == n->args->argc)
+ if (NULL == n->args)
return(1);
- mdoc_vmsg(mdoc, MANDOCERR_SYNTARGCOUNT,
- n->line, n->pos,
- "line arguments == 1 (have %d)",
- n->args->argc);
- return(0);
+ if (n->args->argc > 1)
+ if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_ARGCOUNT))
+ return(0);
+
+ if (MDOC_Split == n->args->argv[0].arg)
+ n->data.An.auth = AUTH_split;
+ else if (MDOC_Nosplit == n->args->argv[0].arg)
+ n->data.An.auth = AUTH_nosplit;
+ else
+ abort();
+
+ return(1);
}
post_bf(POST_ARGS)
{
struct mdoc_node *np;
- int arg;
+ enum mdocargt arg;
/*
* Unlike other data pointers, these are "housed" by the HEAD
}
np = mdoc->last;
+ assert(MDOC_BLOCK == np->parent->type);
+ assert(MDOC_Bf == np->parent->tok);
np->data.Bf = mandoc_calloc(1, sizeof(struct mdoc_bf));
/*
* If neither is specified, let it through with a warning.
*/
- if (np->args && np->child) {
+ if (np->parent->args && np->child) {
mdoc_nmsg(mdoc, np, MANDOCERR_SYNTARGVCOUNT);
return(0);
- } else if (NULL == np->args && NULL == np->child)
+ } else if (NULL == np->parent->args && NULL == np->child)
return(mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE));
/* Extract argument into data. */
- if (np->args) {
- arg = np->args->argv[0].arg;
+ if (np->parent->args) {
+ arg = np->parent->args->argv[0].arg;
if (MDOC_Emphasis == arg)
np->data.Bf->font = FONT_Em;
else if (MDOC_Literal == arg)
static int
post_an(POST_ARGS)
{
+ struct mdoc_node *np;
- if (mdoc->last->args) {
- if (NULL == mdoc->last->child)
- return(1);
- return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGCOUNT));
- }
-
- if (mdoc->last->child)
+ np = mdoc->last;
+ if (AUTH__NONE != np->data.An.auth && np->child)
+ return(mdoc_nmsg(mdoc, np, MANDOCERR_ARGCOUNT));
+ if (AUTH__NONE != np->data.An.auth || np->child)
return(1);
- return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS));
+ return(mdoc_nmsg(mdoc, np, MANDOCERR_NOARGS));
}
if (NULL == mdoc->last->head->child)
if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS))
return(0);
- if (NULL == mdoc->last->body->child)
- if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
- return(0);
break;
case (LIST_bullet):
/* FALLTHROUGH */
case (LIST_enum):
/* FALLTHROUGH */
case (LIST_hyphen):
+ if (NULL == mdoc->last->body->child)
+ if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
+ return(0);
/* FALLTHROUGH */
case (LIST_item):
if (mdoc->last->head->child)
if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST))
return(0);
- if (NULL == mdoc->last->body->child)
- if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
- return(0);
break;
case (LIST_column):
- cols = -1;
- for (i = 0; i < (int)n->args->argc; i++)
- if (MDOC_Column == n->args->argv[i].arg) {
- cols = (int)n->args->argv[i].sz;
- break;
- }
+ cols = (int)n->data.Bl->ncols;
- assert(-1 != cols);
assert(NULL == mdoc->last->head->child);
if (NULL == mdoc->last->body->child)
n = mdoc->last->parent;
if (LIST_column == n->data.Bl->type) {
- for (i = 0; i < (int)n->args->argc; i++)
- if (MDOC_Column == n->args->argv[i].arg)
- break;
- assert(i < (int)n->args->argc);
-
- if (n->args->argv[i].sz && mdoc->last->nchild) {
+ if (n->data.Bl->ncols && mdoc->last->nchild) {
mdoc_nmsg(mdoc, n, MANDOCERR_COLUMNS);
return(0);
}
return(1);
}
+
+
+static int
+pre_pp(PRE_ARGS)
+{
+
+ if (NULL == mdoc->last)
+ return(1);
+
+ /* Don't allow prior `Lp' or `Pp'. */
+
+ if (MDOC_Pp != mdoc->last->tok && MDOC_Lp != mdoc->last->tok)
+ return(1);
+
+ if (MDOC_Bl == n->tok && n->data.Bl->comp)
+ return(1);
+ if (MDOC_Bd == n->tok && n->data.Bd->comp)
+ return(1);
+
+ mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNPAR);
+ mdoc_node_delete(mdoc, mdoc->last);
+ return(1);
+}