Home | History | Annotate | Line # | Download | only in dist
man_validate.c revision 1.1.1.6
      1  1.1.1.6  joerg /*	$Vendor-Id: man_validate.c,v 1.34 2010/04/03 14:12:48 kristaps Exp $ */
      2      1.1  joerg /*
      3      1.1  joerg  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps (at) kth.se>
      4      1.1  joerg  *
      5      1.1  joerg  * Permission to use, copy, modify, and distribute this software for any
      6      1.1  joerg  * purpose with or without fee is hereby granted, provided that the above
      7      1.1  joerg  * copyright notice and this permission notice appear in all copies.
      8      1.1  joerg  *
      9      1.1  joerg  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     10      1.1  joerg  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     11      1.1  joerg  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     12      1.1  joerg  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     13      1.1  joerg  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     14      1.1  joerg  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     15      1.1  joerg  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     16      1.1  joerg  */
     17  1.1.1.4  joerg #ifdef HAVE_CONFIG_H
     18  1.1.1.4  joerg #include "config.h"
     19  1.1.1.4  joerg #endif
     20  1.1.1.4  joerg 
     21      1.1  joerg #include <sys/types.h>
     22      1.1  joerg 
     23      1.1  joerg #include <assert.h>
     24      1.1  joerg #include <ctype.h>
     25      1.1  joerg #include <errno.h>
     26      1.1  joerg #include <limits.h>
     27      1.1  joerg #include <stdarg.h>
     28      1.1  joerg #include <stdlib.h>
     29      1.1  joerg 
     30      1.1  joerg #include "libman.h"
     31      1.1  joerg #include "libmandoc.h"
     32      1.1  joerg 
     33      1.1  joerg #define	CHKARGS	  struct man *m, const struct man_node *n
     34      1.1  joerg 
     35      1.1  joerg typedef	int	(*v_check)(CHKARGS);
     36      1.1  joerg 
     37      1.1  joerg struct	man_valid {
     38      1.1  joerg 	v_check	 *pres;
     39      1.1  joerg 	v_check	 *posts;
     40      1.1  joerg };
     41      1.1  joerg 
     42      1.1  joerg static	int	  check_bline(CHKARGS);
     43      1.1  joerg static	int	  check_eq0(CHKARGS);
     44      1.1  joerg static	int	  check_le1(CHKARGS);
     45      1.1  joerg static	int	  check_ge2(CHKARGS);
     46      1.1  joerg static	int	  check_le5(CHKARGS);
     47      1.1  joerg static	int	  check_par(CHKARGS);
     48      1.1  joerg static	int	  check_part(CHKARGS);
     49  1.1.1.5  joerg static	int	  check_roff(CHKARGS);
     50      1.1  joerg static	int	  check_root(CHKARGS);
     51      1.1  joerg static	int	  check_sec(CHKARGS);
     52      1.1  joerg static	int	  check_text(CHKARGS);
     53  1.1.1.5  joerg static	int	  check_title(CHKARGS);
     54      1.1  joerg 
     55      1.1  joerg static	v_check	  posts_eq0[] = { check_eq0, NULL };
     56  1.1.1.5  joerg static	v_check	  posts_th[] = { check_ge2, check_le5, check_title, NULL };
     57      1.1  joerg static	v_check	  posts_par[] = { check_par, NULL };
     58      1.1  joerg static	v_check	  posts_part[] = { check_part, NULL };
     59      1.1  joerg static	v_check	  posts_sec[] = { check_sec, NULL };
     60  1.1.1.5  joerg static	v_check	  posts_le1[] = { check_le1, NULL };
     61      1.1  joerg static	v_check	  pres_bline[] = { check_bline, NULL };
     62  1.1.1.5  joerg static	v_check	  pres_roff[] = { check_roff, NULL };
     63      1.1  joerg 
     64      1.1  joerg static	const struct man_valid man_valids[MAN_MAX] = {
     65  1.1.1.5  joerg 	{ NULL, posts_eq0 }, /* br */
     66  1.1.1.5  joerg 	{ pres_bline, posts_th }, /* TH */
     67      1.1  joerg 	{ pres_bline, posts_sec }, /* SH */
     68      1.1  joerg 	{ pres_bline, posts_sec }, /* SS */
     69      1.1  joerg 	{ pres_bline, posts_par }, /* TP */
     70      1.1  joerg 	{ pres_bline, posts_par }, /* LP */
     71      1.1  joerg 	{ pres_bline, posts_par }, /* PP */
     72      1.1  joerg 	{ pres_bline, posts_par }, /* P */
     73      1.1  joerg 	{ pres_bline, posts_par }, /* IP */
     74      1.1  joerg 	{ pres_bline, posts_par }, /* HP */
     75      1.1  joerg 	{ NULL, NULL }, /* SM */
     76      1.1  joerg 	{ NULL, NULL }, /* SB */
     77      1.1  joerg 	{ NULL, NULL }, /* BI */
     78      1.1  joerg 	{ NULL, NULL }, /* IB */
     79      1.1  joerg 	{ NULL, NULL }, /* BR */
     80      1.1  joerg 	{ NULL, NULL }, /* RB */
     81      1.1  joerg 	{ NULL, NULL }, /* R */
     82      1.1  joerg 	{ NULL, NULL }, /* B */
     83      1.1  joerg 	{ NULL, NULL }, /* I */
     84      1.1  joerg 	{ NULL, NULL }, /* IR */
     85      1.1  joerg 	{ NULL, NULL }, /* RI */
     86  1.1.1.5  joerg 	{ NULL, posts_eq0 }, /* na */
     87      1.1  joerg 	{ NULL, NULL }, /* i */
     88  1.1.1.5  joerg 	{ NULL, posts_le1 }, /* sp */
     89      1.1  joerg 	{ pres_bline, posts_eq0 }, /* nf */
     90      1.1  joerg 	{ pres_bline, posts_eq0 }, /* fi */
     91      1.1  joerg 	{ NULL, NULL }, /* r */
     92      1.1  joerg 	{ NULL, NULL }, /* RE */
     93      1.1  joerg 	{ NULL, posts_part }, /* RS */
     94      1.1  joerg 	{ NULL, NULL }, /* DT */
     95      1.1  joerg 	{ NULL, NULL }, /* UC */
     96  1.1.1.2  joerg 	{ NULL, NULL }, /* PD */
     97  1.1.1.5  joerg 	{ NULL, posts_eq0 }, /* Sp */
     98  1.1.1.5  joerg 	{ pres_bline, posts_le1 }, /* Vb */
     99  1.1.1.5  joerg 	{ pres_bline, posts_eq0 }, /* Ve */
    100  1.1.1.5  joerg 	{ pres_roff, NULL }, /* de */
    101  1.1.1.5  joerg 	{ pres_roff, NULL }, /* dei */
    102  1.1.1.5  joerg 	{ pres_roff, NULL }, /* am */
    103  1.1.1.5  joerg 	{ pres_roff, NULL }, /* ami */
    104  1.1.1.5  joerg 	{ pres_roff, NULL }, /* ig */
    105  1.1.1.5  joerg 	{ NULL, NULL }, /* . */
    106      1.1  joerg };
    107      1.1  joerg 
    108      1.1  joerg 
    109      1.1  joerg int
    110      1.1  joerg man_valid_pre(struct man *m, const struct man_node *n)
    111      1.1  joerg {
    112      1.1  joerg 	v_check		*cp;
    113      1.1  joerg 
    114      1.1  joerg 	if (MAN_TEXT == n->type)
    115      1.1  joerg 		return(1);
    116      1.1  joerg 	if (MAN_ROOT == n->type)
    117      1.1  joerg 		return(1);
    118      1.1  joerg 
    119      1.1  joerg 	if (NULL == (cp = man_valids[n->tok].pres))
    120      1.1  joerg 		return(1);
    121      1.1  joerg 	for ( ; *cp; cp++)
    122      1.1  joerg 		if ( ! (*cp)(m, n))
    123      1.1  joerg 			return(0);
    124      1.1  joerg 	return(1);
    125      1.1  joerg }
    126      1.1  joerg 
    127      1.1  joerg 
    128      1.1  joerg int
    129      1.1  joerg man_valid_post(struct man *m)
    130      1.1  joerg {
    131      1.1  joerg 	v_check		*cp;
    132      1.1  joerg 
    133      1.1  joerg 	if (MAN_VALID & m->last->flags)
    134      1.1  joerg 		return(1);
    135      1.1  joerg 	m->last->flags |= MAN_VALID;
    136      1.1  joerg 
    137      1.1  joerg 	switch (m->last->type) {
    138      1.1  joerg 	case (MAN_TEXT):
    139      1.1  joerg 		return(check_text(m, m->last));
    140      1.1  joerg 	case (MAN_ROOT):
    141      1.1  joerg 		return(check_root(m, m->last));
    142      1.1  joerg 	default:
    143      1.1  joerg 		break;
    144      1.1  joerg 	}
    145      1.1  joerg 
    146      1.1  joerg 	if (NULL == (cp = man_valids[m->last->tok].posts))
    147      1.1  joerg 		return(1);
    148      1.1  joerg 	for ( ; *cp; cp++)
    149      1.1  joerg 		if ( ! (*cp)(m, m->last))
    150      1.1  joerg 			return(0);
    151      1.1  joerg 
    152      1.1  joerg 	return(1);
    153      1.1  joerg }
    154      1.1  joerg 
    155      1.1  joerg 
    156      1.1  joerg static int
    157      1.1  joerg check_root(CHKARGS)
    158      1.1  joerg {
    159      1.1  joerg 
    160      1.1  joerg 	if (MAN_BLINE & m->flags)
    161      1.1  joerg 		return(man_nwarn(m, n, WEXITSCOPE));
    162      1.1  joerg 	if (MAN_ELINE & m->flags)
    163      1.1  joerg 		return(man_nwarn(m, n, WEXITSCOPE));
    164      1.1  joerg 
    165      1.1  joerg 	m->flags &= ~MAN_BLINE;
    166      1.1  joerg 	m->flags &= ~MAN_ELINE;
    167      1.1  joerg 
    168      1.1  joerg 	if (NULL == m->first->child)
    169      1.1  joerg 		return(man_nerr(m, n, WNODATA));
    170  1.1.1.6  joerg 	if (NULL == m->meta.title) {
    171  1.1.1.6  joerg 		if ( ! man_nwarn(m, n, WNOTITLE))
    172  1.1.1.6  joerg 			return(0);
    173  1.1.1.6  joerg 		/*
    174  1.1.1.6  joerg 		 * If a title hasn't been set, do so now (by
    175  1.1.1.6  joerg 		 * implication, date and section also aren't set).
    176  1.1.1.6  joerg 		 *
    177  1.1.1.6  joerg 		 * FIXME: this should be in man_action.c.
    178  1.1.1.6  joerg 		 */
    179  1.1.1.6  joerg 	        m->meta.title = mandoc_strdup("unknown");
    180  1.1.1.6  joerg 		m->meta.date = time(NULL);
    181  1.1.1.6  joerg 		m->meta.msec = 1;
    182  1.1.1.6  joerg 	}
    183      1.1  joerg 
    184      1.1  joerg 	return(1);
    185      1.1  joerg }
    186      1.1  joerg 
    187      1.1  joerg 
    188      1.1  joerg static int
    189  1.1.1.5  joerg check_title(CHKARGS)
    190  1.1.1.5  joerg {
    191  1.1.1.5  joerg 	const char	*p;
    192  1.1.1.5  joerg 
    193  1.1.1.5  joerg 	assert(n->child);
    194  1.1.1.5  joerg 	if ('\0' == *n->child->string)
    195  1.1.1.5  joerg 		return(man_nerr(m, n, WNOTITLE));
    196  1.1.1.5  joerg 
    197  1.1.1.5  joerg 	for (p = n->child->string; '\0' != *p; p++)
    198  1.1.1.5  joerg 		if (isalpha((u_char)*p) && ! isupper((u_char)*p))
    199  1.1.1.5  joerg 			if ( ! man_nwarn(m, n, WTITLECASE))
    200  1.1.1.5  joerg 				return(0);
    201  1.1.1.5  joerg 
    202  1.1.1.5  joerg 	return(1);
    203  1.1.1.5  joerg }
    204  1.1.1.5  joerg 
    205  1.1.1.5  joerg 
    206  1.1.1.5  joerg static int
    207      1.1  joerg check_text(CHKARGS)
    208      1.1  joerg {
    209      1.1  joerg 	const char	*p;
    210      1.1  joerg 	int		 pos, c;
    211      1.1  joerg 
    212      1.1  joerg 	assert(n->string);
    213      1.1  joerg 
    214      1.1  joerg 	for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
    215      1.1  joerg 		if ('\\' == *p) {
    216      1.1  joerg 			c = mandoc_special(p);
    217      1.1  joerg 			if (c) {
    218      1.1  joerg 				p += c - 1;
    219      1.1  joerg 				pos += c - 1;
    220      1.1  joerg 				continue;
    221      1.1  joerg 			}
    222      1.1  joerg 			if ( ! (MAN_IGN_ESCAPE & m->pflags))
    223      1.1  joerg 				return(man_perr(m, n->line, pos, WESCAPE));
    224      1.1  joerg 			if ( ! man_pwarn(m, n->line, pos, WESCAPE))
    225      1.1  joerg 				return(0);
    226      1.1  joerg 			continue;
    227      1.1  joerg 		}
    228      1.1  joerg 
    229      1.1  joerg 		if ('\t' == *p || isprint((u_char)*p))
    230      1.1  joerg 			continue;
    231      1.1  joerg 
    232      1.1  joerg 		if (MAN_IGN_CHARS & m->pflags)
    233      1.1  joerg 			return(man_pwarn(m, n->line, pos, WNPRINT));
    234      1.1  joerg 		return(man_perr(m, n->line, pos, WNPRINT));
    235      1.1  joerg 	}
    236      1.1  joerg 
    237      1.1  joerg 	return(1);
    238      1.1  joerg }
    239      1.1  joerg 
    240      1.1  joerg 
    241      1.1  joerg #define	INEQ_DEFINE(x, ineq, name) \
    242      1.1  joerg static int \
    243      1.1  joerg check_##name(CHKARGS) \
    244      1.1  joerg { \
    245      1.1  joerg 	if (n->nchild ineq (x)) \
    246      1.1  joerg 		return(1); \
    247      1.1  joerg 	return(man_verr(m, n->line, n->pos, \
    248      1.1  joerg 			"expected line arguments %s %d, have %d", \
    249      1.1  joerg 			#ineq, (x), n->nchild)); \
    250      1.1  joerg }
    251      1.1  joerg 
    252      1.1  joerg INEQ_DEFINE(0, ==, eq0)
    253      1.1  joerg INEQ_DEFINE(1, <=, le1)
    254      1.1  joerg INEQ_DEFINE(2, >=, ge2)
    255      1.1  joerg INEQ_DEFINE(5, <=, le5)
    256      1.1  joerg 
    257      1.1  joerg 
    258      1.1  joerg static int
    259      1.1  joerg check_sec(CHKARGS)
    260      1.1  joerg {
    261      1.1  joerg 
    262      1.1  joerg 	if (MAN_BODY == n->type && 0 == n->nchild)
    263      1.1  joerg 		return(man_nwarn(m, n, WBODYARGS));
    264      1.1  joerg 	if (MAN_HEAD == n->type && 0 == n->nchild)
    265      1.1  joerg 		return(man_nerr(m, n, WHEADARGS));
    266      1.1  joerg 	return(1);
    267      1.1  joerg }
    268      1.1  joerg 
    269      1.1  joerg 
    270      1.1  joerg static int
    271      1.1  joerg check_part(CHKARGS)
    272      1.1  joerg {
    273      1.1  joerg 
    274      1.1  joerg 	if (MAN_BODY == n->type && 0 == n->nchild)
    275      1.1  joerg 		return(man_nwarn(m, n, WBODYARGS));
    276      1.1  joerg 	return(1);
    277      1.1  joerg }
    278      1.1  joerg 
    279      1.1  joerg 
    280      1.1  joerg static int
    281      1.1  joerg check_par(CHKARGS)
    282      1.1  joerg {
    283      1.1  joerg 
    284      1.1  joerg 	if (MAN_BODY == n->type)
    285      1.1  joerg 		switch (n->tok) {
    286      1.1  joerg 		case (MAN_IP):
    287      1.1  joerg 			/* FALLTHROUGH */
    288      1.1  joerg 		case (MAN_HP):
    289      1.1  joerg 			/* FALLTHROUGH */
    290      1.1  joerg 		case (MAN_TP):
    291      1.1  joerg 			/* Body-less lists are ok. */
    292      1.1  joerg 			break;
    293      1.1  joerg 		default:
    294      1.1  joerg 			if (n->nchild)
    295      1.1  joerg 				break;
    296      1.1  joerg 			return(man_nwarn(m, n, WBODYARGS));
    297      1.1  joerg 		}
    298      1.1  joerg 	if (MAN_HEAD == n->type)
    299      1.1  joerg 		switch (n->tok) {
    300      1.1  joerg 		case (MAN_PP):
    301      1.1  joerg 			/* FALLTHROUGH */
    302      1.1  joerg 		case (MAN_P):
    303      1.1  joerg 			/* FALLTHROUGH */
    304      1.1  joerg 		case (MAN_LP):
    305      1.1  joerg 			if (0 == n->nchild)
    306      1.1  joerg 				break;
    307      1.1  joerg 			return(man_nwarn(m, n, WNHEADARGS));
    308      1.1  joerg 		default:
    309      1.1  joerg 			if (n->nchild)
    310      1.1  joerg 				break;
    311      1.1  joerg 			return(man_nwarn(m, n, WHEADARGS));
    312      1.1  joerg 		}
    313      1.1  joerg 
    314      1.1  joerg 	return(1);
    315      1.1  joerg }
    316      1.1  joerg 
    317      1.1  joerg 
    318      1.1  joerg static int
    319      1.1  joerg check_bline(CHKARGS)
    320      1.1  joerg {
    321      1.1  joerg 
    322      1.1  joerg 	assert( ! (MAN_ELINE & m->flags));
    323      1.1  joerg 	if (MAN_BLINE & m->flags)
    324      1.1  joerg 		return(man_nerr(m, n, WLNSCOPE));
    325  1.1.1.5  joerg 
    326      1.1  joerg 	return(1);
    327      1.1  joerg }
    328      1.1  joerg 
    329  1.1.1.5  joerg 
    330  1.1.1.5  joerg static int
    331  1.1.1.5  joerg check_roff(CHKARGS)
    332  1.1.1.5  joerg {
    333  1.1.1.5  joerg 
    334  1.1.1.5  joerg 	if (MAN_BLOCK != n->type)
    335  1.1.1.5  joerg 		return(1);
    336  1.1.1.5  joerg 
    337  1.1.1.5  joerg 	for (n = n->parent; n; n = n->parent)
    338  1.1.1.5  joerg 		if (MAN_de == n->tok || MAN_dei == n->tok ||
    339  1.1.1.5  joerg 				MAN_am == n->tok ||
    340  1.1.1.5  joerg 				MAN_ami == n->tok ||
    341  1.1.1.5  joerg 				MAN_ig == n->tok)
    342  1.1.1.5  joerg 			return(man_nerr(m, n, WROFFNEST));
    343  1.1.1.5  joerg 
    344  1.1.1.5  joerg 	return(1);
    345  1.1.1.5  joerg }
    346