/* $Id: mdoc_validate.c,v 1.301 2016/01/08 17:48:09 schwarze Exp $ */
/*
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2016 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#ifndef OSNAME
#endif
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mandoc_aux.h"
#include "mandoc.h"
#include "roff.h"
#include "mdoc.h"
#include "libmandoc.h"
#include "roff_int.h"
#include "libmdoc.h"
/* FIXME: .Bl -diag can't have non-text children in HEAD. */
enum check_ineq {
};
static void check_text(struct roff_man *, int, int, char *);
static void check_argv(struct roff_man *,
static void rewrite_macro2len(char **);
static void post_an_norm(POST_ARGS);
static void post_bl_block(POST_ARGS);
static void post_bl_block_tag(POST_ARGS);
static void post_bl_head(POST_ARGS);
static void post_bl_norm(POST_ARGS);
static void post_defaults(POST_ARGS);
static void post_display(POST_ARGS);
static void post_fname(POST_ARGS);
static void post_ignpar(POST_ARGS);
static void post_obsolete(POST_ARGS);
static void post_prevpar(POST_ARGS);
static void post_sh_head(POST_ARGS);
static void post_sh_name(POST_ARGS);
static void post_sh_see_also(POST_ARGS);
static void post_sh_authors(POST_ARGS);
NULL, /* Ap */
post_dd, /* Dd */
post_dt, /* Dt */
post_os, /* Os */
post_sh, /* Sh */
post_ignpar, /* Ss */
post_par, /* Pp */
post_display, /* D1 */
post_display, /* Dl */
post_display, /* Bd */
NULL, /* Ed */
post_bl, /* Bl */
NULL, /* El */
post_it, /* It */
NULL, /* Ad */
post_an, /* An */
post_defaults, /* Ar */
NULL, /* Cd */
NULL, /* Cm */
NULL, /* Dv */
NULL, /* Er */
NULL, /* Ev */
post_ex, /* Ex */
post_fa, /* Fa */
NULL, /* Fd */
NULL, /* Fl */
post_fn, /* Fn */
NULL, /* Ft */
NULL, /* Ic */
NULL, /* In */
post_defaults, /* Li */
post_nd, /* Nd */
post_nm, /* Nm */
NULL, /* Op */
post_obsolete, /* Ot */
post_defaults, /* Pa */
post_std, /* Rv */
post_st, /* St */
NULL, /* Va */
NULL, /* Vt */
NULL, /* Xr */
NULL, /* %A */
NULL, /* %D */
NULL, /* %I */
NULL, /* %J */
post_hyph, /* %N */
post_hyph, /* %O */
NULL, /* %P */
post_hyph, /* %R */
NULL, /* %V */
NULL, /* Ac */
NULL, /* Ao */
NULL, /* Aq */
post_at, /* At */
NULL, /* Bc */
post_bf, /* Bf */
NULL, /* Bo */
NULL, /* Bq */
NULL, /* Bsx */
post_bx, /* Bx */
post_obsolete, /* Db */
NULL, /* Dc */
NULL, /* Do */
NULL, /* Dq */
NULL, /* Ec */
NULL, /* Ef */
NULL, /* Em */
NULL, /* Eo */
NULL, /* Fx */
NULL, /* Ms */
NULL, /* No */
post_ns, /* Ns */
NULL, /* Nx */
NULL, /* Ox */
NULL, /* Pc */
NULL, /* Pf */
NULL, /* Po */
NULL, /* Pq */
NULL, /* Qc */
NULL, /* Ql */
NULL, /* Qo */
NULL, /* Qq */
NULL, /* Re */
post_rs, /* Rs */
NULL, /* Sc */
NULL, /* So */
NULL, /* Sq */
post_sm, /* Sm */
post_hyph, /* Sx */
NULL, /* Sy */
NULL, /* Tn */
NULL, /* Ux */
NULL, /* Xc */
NULL, /* Xo */
post_fo, /* Fo */
NULL, /* Fc */
NULL, /* Oo */
NULL, /* Oc */
post_bk, /* Bk */
NULL, /* Ek */
post_eoln, /* Bt */
NULL, /* Hf */
post_obsolete, /* Fr */
post_eoln, /* Ud */
post_lb, /* Lb */
post_par, /* Lp */
NULL, /* Lk */
post_defaults, /* Mt */
NULL, /* Brq */
NULL, /* Bro */
NULL, /* Brc */
NULL, /* %C */
post_es, /* Es */
post_en, /* En */
NULL, /* Dx */
NULL, /* %Q */
post_par, /* br */
post_par, /* sp */
NULL, /* %U */
NULL, /* Ta */
NULL, /* ll */
};
};
NULL,
"NAME",
"LIBRARY",
"SYNOPSIS",
"DESCRIPTION",
"CONTEXT",
"IMPLEMENTATION NOTES",
"RETURN VALUES",
"ENVIRONMENT",
"FILES",
"EXIT STATUS",
"EXAMPLES",
"DIAGNOSTICS",
"COMPATIBILITY",
"ERRORS",
"SEE ALSO",
"STANDARDS",
"HISTORY",
"AUTHORS",
"CAVEATS",
"BUGS",
"SECURITY CONSIDERATIONS",
};
void
{
struct roff_node *n;
v_post *p;
else
}
switch (n->type) {
case ROFFT_TEXT:
break;
case ROFFT_EQN:
case ROFFT_TBL:
break;
case ROFFT_ROOT:
break;
default:
/*
* Closing delimiters are not special at the
* beginning of a block, opening delimiters
* are not special at the end.
*/
/* Call the macro's postprocessor. */
p = mdoc_valids + n->tok;
if (*p)
(*p)(mdoc);
mdoc_state(mdoc, n);
break;
}
}
static void
{
int i;
return;
}
static void
{
int i;
for (i = 0; i < (int)v->sz; i++)
}
static void
{
char *cp;
return;
}
static void
{
struct roff_node *n;
int i;
/*
* First figure out which kind of list to use: bind ourselves to
* the first mentioned list type and warn about any remaining
* ones. If we find no list type, we default to LIST_item.
*/
lt = LIST__NONE;
/* Set list types. */
case MDOC_Bullet:
lt = LIST_bullet;
break;
case MDOC_Dash:
break;
case MDOC_Enum:
break;
case MDOC_Hyphen:
lt = LIST_hyphen;
break;
case MDOC_Item:
break;
case MDOC_Tag:
break;
case MDOC_Diag:
break;
case MDOC_Hang:
break;
case MDOC_Ohang:
lt = LIST_ohang;
break;
case MDOC_Inset:
lt = LIST_inset;
break;
case MDOC_Column:
lt = LIST_column;
break;
/* Set list arguments. */
case MDOC_Compact:
break;
case MDOC_Width:
break;
}
break;
case MDOC_Offset:
break;
}
break;
default:
continue;
}
if (LIST__NONE == lt)
continue;
/* Check: multiple list types. */
continue;
}
/* The list type should come first. */
if (LIST_column == lt) {
}
}
/* Allow lists to default to LIST_item. */
}
/*
* Validate the width field. Some list types don't need width
* types and should be warned about them. Others should have it
* and must also be warned. Yet others have a default and need
* no warning.
*/
case LIST_tag:
break;
case LIST_column:
case LIST_diag:
case LIST_ohang:
case LIST_inset:
case LIST_item:
break;
case LIST_bullet:
case LIST_dash:
case LIST_hyphen:
break;
case LIST_enum:
break;
default:
break;
}
}
static void
{
struct roff_node *n;
int i;
dt = DISP__NONE;
case MDOC_Centred:
dt = DISP_centered;
break;
case MDOC_Ragged:
dt = DISP_ragged;
break;
case MDOC_Unfilled:
dt = DISP_unfilled;
break;
case MDOC_Filled:
dt = DISP_filled;
break;
case MDOC_Literal:
dt = DISP_literal;
break;
case MDOC_File:
break;
case MDOC_Offset:
break;
}
break;
case MDOC_Compact:
break;
default:
abort();
}
if (DISP__NONE == dt)
continue;
else
}
}
}
static void
{
struct roff_node *n;
size_t i;
return;
}
else
abort();
}
static void
{
struct roff_node *n;
return;
}
static void
{
struct roff_node *n;
}
static void
{
/*
* Unlike other data pointers, these are "housed" by the HEAD
* element, which contains the goods.
*/
return;
/* Check the number of arguments. */
return;
}
}
/* Extract argument into data. */
case MDOC_Emphasis:
break;
case MDOC_Literal:
break;
case MDOC_Symbolic:
break;
default:
abort();
}
return;
}
/* Extract parameter into data. */
else
}
static void
{
struct roff_node *n;
const char *stdlibname;
char *libname;
"library \\(Lq%s\\(Rq", n->string);
else
}
static void
{
const struct roff_node *n;
}
static void
{
const struct roff_node *n;
const char *cp;
}
static void
{
}
static void
{
const struct roff_node *n;
if (n->type != ROFFT_HEAD)
return;
return;
}
}
}
static void
{
const struct roff_node *n;
const char *cp;
/* Ignore callbacks and alterations. */
break;
if (*cp != ',')
continue;
n->string);
break;
}
}
}
static void
{
struct roff_node *n;
return;
}
static void
{
struct roff_node *n;
if (n->type != ROFFT_BODY)
return;
}
static void
{
switch (n->type) {
case ROFFT_BODY:
if (n->end != ENDBODY_NOT)
break;
break;
case ROFFT_BLOCK:
roff_node_delete(mdoc, n);
break;
}
}
break;
}
}
break;
default:
break;
}
}
static void
{
/*
* The `Ar' defaults to "file ..." if no value is provided as an
* argument; the `Mt' and `Pa' macros use "~"; the `Li' just
* gets an empty string.
*/
return;
case MDOC_Ar:
break;
case MDOC_Pa:
case MDOC_Mt:
break;
default:
abort();
}
}
static void
{
struct roff_node *n;
const char *std_att;
char *att;
return;
}
/*
* If we have a child, look it up in the standard keys. If a
* key exist, use that instead of the child; if it doesn't,
* prefix "AT&T UNIX " to the existing data.
*/
n = n->child;
} else
}
static void
{
}
static void
{
}
static void
{
}
static void
{
int i, cols;
return;
switch (lt) {
case LIST_tag:
case LIST_hang:
case LIST_ohang:
case LIST_inset:
case LIST_diag:
"Bl -%s It",
break;
case LIST_bullet:
case LIST_dash:
case LIST_enum:
case LIST_hyphen:
"Bl -%s It",
/* FALLTHROUGH */
case LIST_item:
break;
case LIST_column:
i = 0;
i++;
"%d columns, %d cells", cols, i);
break;
default:
abort();
}
}
static void
{
/*
* These are fairly complicated, so we've broken them into two
* functions. post_bl_block_tag() is called when a -tag is
* specified, but no -width (it must be guessed). The second
* when a -width is specified (macro indicators must be
* rewritten into real lengths).
*/
}
continue;
case MDOC_Pp:
case MDOC_Lp:
case MDOC_br:
break;
default:
continue;
}
"%s before It",
} else
break;
}
}
}
/*
* If the argument of -offset or -width is a macro,
* replace it with the associated default width.
*/
void
{
int tok;
return;
width = 6;
return;
else
}
static void
{
int i;
/*
* Calculate the -width for a `Bl -tag' list if it hasn't been
* provided. Uses the first head macro. NOTE AGAIN: this is
* ONLY if the -width argument has NOT been provided. See
* rewrite_macro2len() for converting the -width string.
*/
sz = 10;
continue;
break;
break;
}
break;
}
/* Defaults to ten ens. */
/*
* We have to dynamically add this to the macro's argument list.
* We're guaranteed that a MDOC_Width doesn't already exist.
*/
/* Set our width! */
}
static void
{
int i, j;
return;
}
return;
}
/*
* Append old-style lists, where the column width specifiers
* trail as macro parameters, to the new-style ("normal-form")
* lists where they're argument values following -column.
*/
return;
break;
/*
* Accommodate for new-style groff column syntax. Shuffle the
* child nodes, all of which must be TEXT, as arguments for the
* column field. Then, delete the head children.
*/
}
}
static void
{
case ROFFT_BLOCK:
return;
case ROFFT_HEAD:
return;
case ROFFT_BODY:
break;
default:
return;
}
return;
return;
}
continue;
}
/*
* Move the node out of the Bl block.
* First, collect all required node pointers.
*/
/*
* Unlink this child.
*/
else
/*
* Relink this child.
*/
else
}
}
static void
{
struct roff_node *n;
roff_node_delete(mdoc, n);
}
}
static void
{
return;
}
return;
}
return;
}
return;
}
static void
{
struct roff_node *n;
/* Add missing prologue data. */
mandoc_strdup("") :
}
}
/* Check that we begin with a proper `Sh'. */
n = n->next;
if (n == NULL)
}
static void
{
const char *p;
roff_node_delete(mdoc, n);
} else {
}
}
static void
{
int i, j;
return;
return;
}
/*
* The full `Rs' block needs special handling to order the
* sub-elements according to `rsord'. Pick through each element
* and correctly order it. This is an insertion sort.
*/
/* Determine order number of this child. */
for (i = 0; i < RSORD_MAX; i++)
break;
if (i == RSORD_MAX) {
i = -1;
/*
* Remove this child from the chain. This somewhat
* repeats roff_node_unlink(), but since we're
* just re-ordering, there's no need for the
* full unlink process.
*/
/*
* Scan back until we reach a node that's
* to be ordered before this child.
*/
/* Determine order of `prev'. */
for (j = 0; j < RSORD_MAX; j++)
break;
if (j == RSORD_MAX)
j = -1;
if (j <= i)
break;
}
/*
* Set this child back into its correct place
* in front of the `prev' node.
*/
} else {
}
}
}
/*
* For some arguments of some macros,
* convert all breakable hyphens into ASCII_HYPH.
*/
static void
{
char *cp;
continue;
if (*cp == '\0')
continue;
while (*(++cp) != '\0')
if (*cp == '-' &&
*cp = ASCII_HYPH;
}
}
static void
{
}
static void
{
case ROFFT_HEAD:
break;
case ROFFT_BODY:
case SEC_NAME:
break;
case SEC_SEE_ALSO:
break;
case SEC_AUTHORS:
break;
default:
break;
}
break;
default:
break;
}
}
static void
{
struct roff_node *n;
switch (n->tok) {
case MDOC_Nm:
hasnm = 1;
break;
case MDOC_Nd:
hasnd = 1;
break;
case TOKEN_NONE:
if (hasnm)
break;
/* FALLTHROUGH */
default:
break;
}
}
if ( ! hasnm)
if ( ! hasnd)
}
static void
{
const struct roff_node *n;
int cmp;
while (n != NULL) {
break;
/* Process one .Xr node. */
"%s before %s(%s)", lastpunct,
if (cmp > 0)
"%s(%s) after %s(%s)", name,
else if (cmp == 0 &&
}
/* Process the following node. */
n = n->next;
if (n == NULL)
break;
lastpunct = "none";
continue;
}
if (n->type != ROFFT_TEXT)
break;
return;
n = n->next;
}
}
static int
{
return 1;
return 0;
}
static void
{
}
static void
{
const char *goodsec;
/*
* Process a new section. Sections are either "named" or
* "custom". Custom sections are user-defined, while named ones
* follow a conventional order and may only appear in certain
* manual sections.
*/
/* The NAME should be first. */
/* The SYNOPSIS gets special attention in other areas. */
if (sec == SEC_SYNOPSIS) {
} else {
}
/* Mark our last section. */
/* We don't care about custom sections after this. */
if (sec == SEC_CUSTOM)
return;
/*
* Check whether our non-custom section is being repeated or is
* out of order.
*/
/* Mark the last named section. */
return;
switch (sec) {
case SEC_ERRORS:
break;
goodsec = "2, 3, 4, 9";
/* FALLTHROUGH */
case SEC_RETURN_VALUES:
case SEC_LIBRARY:
break;
break;
goodsec = "2, 3, 9";
/* FALLTHROUGH */
case SEC_CONTEXT:
break;
goodsec = "9";
break;
default:
break;
}
}
static void
{
case ROFFT_HEAD:
return;
case ROFFT_BODY:
break;
default:
return;
}
}
}
}
static void
{
struct roff_node *n;
return;
return;
/*
* Don't allow prior `Lp' or `Pp' prior to a paragraph-type
* block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
*/
return;
return;
return;
return;
mdoc_macronames[n->tok]);
}
static void
{
return;
return;
}
static void
{
struct roff_node *n;
char *datestr;
goto out;
}
else {
}
out:
roff_node_delete(mdoc, n);
}
static void
{
const char *cp;
char *p;
goto out;
}
/* Mandatory first argument: title. */
} else {
/* Check that all characters are uppercase. */
if (islower((unsigned char)*p)) {
break;
}
}
/* Mandatory second argument: section.�*/
goto out; /* msec and arch remain NULL. */
}
/* Infer volume title from section number. */
} else
/* Optional third argument: architecture. */
goto out;
*p = tolower((unsigned char)*p);
/* Ignore fourth and later arguments. */
out:
roff_node_delete(mdoc, n);
}
static void
{
struct roff_node *n;
/*
* Make `Bx's second argument always start with an uppercase
* letter. Groff checks if it's an "accepted" term, but we just
* uppercase blindly.
*/
}
static void
{
#ifndef OSNAME
static char *defbuf;
#endif
struct roff_node *n;
/*
* Set the operating system by way of the `Os' macro.
* The order of precedence is:
* 1. the argument of the `Os' macro, unless empty
* 2. the -Ios=foo command line argument, if provided
* 3. -DOSNAME="\"foo\"", if provided during compilation
* 4. "sysname release" from uname(3)
*/
goto out;
goto out;
}
#ifdef OSNAME
#else /*!OSNAME */
} else
}
#endif /*!OSNAME*/
out:
roff_node_delete(mdoc, n);
}
/*
* If no argument is provided,
* fill in the name of the current manual page.
*/
static void
{
struct roff_node *n;
return;
return;
}
}
enum roff_sec
mdoc_a2sec(const char *p)
{
int i;
for (i = 0; i < (int)SEC__MAX; i++)
return (enum roff_sec)i;
return SEC_CUSTOM;
}
static size_t
{
switch (macro) {
case MDOC_Ad:
return 12;
case MDOC_Ao:
return 12;
case MDOC_An:
return 12;
case MDOC_Aq:
return 12;
case MDOC_Ar:
return 12;
case MDOC_Bo:
return 12;
case MDOC_Bq:
return 12;
case MDOC_Cd:
return 12;
case MDOC_Cm:
return 10;
case MDOC_Do:
return 10;
case MDOC_Dq:
return 12;
case MDOC_Dv:
return 12;
case MDOC_Eo:
return 12;
case MDOC_Em:
return 10;
case MDOC_Er:
return 17;
case MDOC_Ev:
return 15;
case MDOC_Fa:
return 12;
case MDOC_Fl:
return 10;
case MDOC_Fo:
return 16;
case MDOC_Fn:
return 16;
case MDOC_Ic:
return 10;
case MDOC_Li:
return 16;
case MDOC_Ms:
return 6;
case MDOC_Nm:
return 10;
case MDOC_No:
return 12;
case MDOC_Oo:
return 10;
case MDOC_Op:
return 14;
case MDOC_Pa:
return 32;
case MDOC_Pf:
return 12;
case MDOC_Po:
return 12;
case MDOC_Pq:
return 12;
case MDOC_Ql:
return 16;
case MDOC_Qo:
return 12;
case MDOC_So:
return 12;
case MDOC_Sq:
return 12;
case MDOC_Sy:
return 6;
case MDOC_Sx:
return 16;
case MDOC_Tn:
return 10;
case MDOC_Va:
return 12;
case MDOC_Vt:
return 12;
case MDOC_Xr:
return 10;
default:
break;
};
return 0;
}