Home | History | Annotate | Line # | Download | only in dist
man_validate.c revision 1.1.1.9
      1  1.1.1.9  joerg /*	$Vendor-Id: man_validate.c,v 1.45 2010/06/28 14:39:17 kristaps Exp $ */
      2      1.1  joerg /*
      3  1.1.1.8  joerg  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps (at) bsd.lv>
      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.1.7  joerg #include "mandoc.h"
     31      1.1  joerg #include "libman.h"
     32      1.1  joerg #include "libmandoc.h"
     33      1.1  joerg 
     34  1.1.1.8  joerg #define	CHKARGS	  struct man *m, struct man_node *n
     35      1.1  joerg 
     36      1.1  joerg typedef	int	(*v_check)(CHKARGS);
     37      1.1  joerg 
     38      1.1  joerg struct	man_valid {
     39      1.1  joerg 	v_check	 *pres;
     40      1.1  joerg 	v_check	 *posts;
     41      1.1  joerg };
     42      1.1  joerg 
     43      1.1  joerg static	int	  check_bline(CHKARGS);
     44      1.1  joerg static	int	  check_eq0(CHKARGS);
     45      1.1  joerg static	int	  check_le1(CHKARGS);
     46      1.1  joerg static	int	  check_ge2(CHKARGS);
     47      1.1  joerg static	int	  check_le5(CHKARGS);
     48      1.1  joerg static	int	  check_par(CHKARGS);
     49      1.1  joerg static	int	  check_part(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  joerg 
     63      1.1  joerg static	const struct man_valid man_valids[MAN_MAX] = {
     64  1.1.1.5  joerg 	{ NULL, posts_eq0 }, /* br */
     65  1.1.1.5  joerg 	{ pres_bline, posts_th }, /* TH */
     66      1.1  joerg 	{ pres_bline, posts_sec }, /* SH */
     67      1.1  joerg 	{ pres_bline, posts_sec }, /* SS */
     68      1.1  joerg 	{ pres_bline, posts_par }, /* TP */
     69      1.1  joerg 	{ pres_bline, posts_par }, /* LP */
     70      1.1  joerg 	{ pres_bline, posts_par }, /* PP */
     71      1.1  joerg 	{ pres_bline, posts_par }, /* P */
     72      1.1  joerg 	{ pres_bline, posts_par }, /* IP */
     73      1.1  joerg 	{ pres_bline, posts_par }, /* HP */
     74      1.1  joerg 	{ NULL, NULL }, /* SM */
     75      1.1  joerg 	{ NULL, NULL }, /* SB */
     76      1.1  joerg 	{ NULL, NULL }, /* BI */
     77      1.1  joerg 	{ NULL, NULL }, /* IB */
     78      1.1  joerg 	{ NULL, NULL }, /* BR */
     79      1.1  joerg 	{ NULL, NULL }, /* RB */
     80      1.1  joerg 	{ NULL, NULL }, /* R */
     81      1.1  joerg 	{ NULL, NULL }, /* B */
     82      1.1  joerg 	{ NULL, NULL }, /* I */
     83      1.1  joerg 	{ NULL, NULL }, /* IR */
     84      1.1  joerg 	{ NULL, NULL }, /* RI */
     85  1.1.1.5  joerg 	{ NULL, posts_eq0 }, /* na */
     86      1.1  joerg 	{ NULL, NULL }, /* i */
     87  1.1.1.5  joerg 	{ NULL, posts_le1 }, /* sp */
     88      1.1  joerg 	{ pres_bline, posts_eq0 }, /* nf */
     89      1.1  joerg 	{ pres_bline, posts_eq0 }, /* fi */
     90      1.1  joerg 	{ NULL, NULL }, /* r */
     91      1.1  joerg 	{ NULL, NULL }, /* RE */
     92      1.1  joerg 	{ NULL, posts_part }, /* RS */
     93      1.1  joerg 	{ NULL, NULL }, /* DT */
     94      1.1  joerg 	{ NULL, NULL }, /* UC */
     95  1.1.1.2  joerg 	{ NULL, NULL }, /* PD */
     96  1.1.1.7  joerg 	{ NULL, posts_le1 }, /* Sp */
     97  1.1.1.5  joerg 	{ pres_bline, posts_le1 }, /* Vb */
     98  1.1.1.5  joerg 	{ pres_bline, posts_eq0 }, /* Ve */
     99  1.1.1.7  joerg 	{ NULL, NULL }, /* AT */
    100      1.1  joerg };
    101      1.1  joerg 
    102      1.1  joerg 
    103      1.1  joerg int
    104  1.1.1.8  joerg man_valid_pre(struct man *m, struct man_node *n)
    105      1.1  joerg {
    106      1.1  joerg 	v_check		*cp;
    107      1.1  joerg 
    108      1.1  joerg 	if (MAN_TEXT == n->type)
    109      1.1  joerg 		return(1);
    110      1.1  joerg 	if (MAN_ROOT == n->type)
    111      1.1  joerg 		return(1);
    112      1.1  joerg 
    113      1.1  joerg 	if (NULL == (cp = man_valids[n->tok].pres))
    114      1.1  joerg 		return(1);
    115      1.1  joerg 	for ( ; *cp; cp++)
    116      1.1  joerg 		if ( ! (*cp)(m, n))
    117      1.1  joerg 			return(0);
    118      1.1  joerg 	return(1);
    119      1.1  joerg }
    120      1.1  joerg 
    121      1.1  joerg 
    122      1.1  joerg int
    123      1.1  joerg man_valid_post(struct man *m)
    124      1.1  joerg {
    125      1.1  joerg 	v_check		*cp;
    126      1.1  joerg 
    127      1.1  joerg 	if (MAN_VALID & m->last->flags)
    128      1.1  joerg 		return(1);
    129      1.1  joerg 	m->last->flags |= MAN_VALID;
    130      1.1  joerg 
    131      1.1  joerg 	switch (m->last->type) {
    132      1.1  joerg 	case (MAN_TEXT):
    133      1.1  joerg 		return(check_text(m, m->last));
    134      1.1  joerg 	case (MAN_ROOT):
    135      1.1  joerg 		return(check_root(m, m->last));
    136      1.1  joerg 	default:
    137      1.1  joerg 		break;
    138      1.1  joerg 	}
    139      1.1  joerg 
    140      1.1  joerg 	if (NULL == (cp = man_valids[m->last->tok].posts))
    141      1.1  joerg 		return(1);
    142      1.1  joerg 	for ( ; *cp; cp++)
    143      1.1  joerg 		if ( ! (*cp)(m, m->last))
    144      1.1  joerg 			return(0);
    145      1.1  joerg 
    146      1.1  joerg 	return(1);
    147      1.1  joerg }
    148      1.1  joerg 
    149      1.1  joerg 
    150      1.1  joerg static int
    151      1.1  joerg check_root(CHKARGS)
    152      1.1  joerg {
    153      1.1  joerg 
    154      1.1  joerg 	if (MAN_BLINE & m->flags)
    155  1.1.1.7  joerg 		return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
    156      1.1  joerg 	if (MAN_ELINE & m->flags)
    157  1.1.1.7  joerg 		return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
    158      1.1  joerg 
    159      1.1  joerg 	m->flags &= ~MAN_BLINE;
    160      1.1  joerg 	m->flags &= ~MAN_ELINE;
    161      1.1  joerg 
    162  1.1.1.7  joerg 	if (NULL == m->first->child) {
    163  1.1.1.7  joerg 		man_nmsg(m, n, MANDOCERR_NODOCBODY);
    164  1.1.1.7  joerg 		return(0);
    165  1.1.1.7  joerg 	} else if (NULL == m->meta.title) {
    166  1.1.1.7  joerg 		if ( ! man_nmsg(m, n, MANDOCERR_NOTITLE))
    167  1.1.1.6  joerg 			return(0);
    168  1.1.1.6  joerg 		/*
    169  1.1.1.6  joerg 		 * If a title hasn't been set, do so now (by
    170  1.1.1.6  joerg 		 * implication, date and section also aren't set).
    171  1.1.1.6  joerg 		 *
    172  1.1.1.6  joerg 		 * FIXME: this should be in man_action.c.
    173  1.1.1.6  joerg 		 */
    174  1.1.1.6  joerg 	        m->meta.title = mandoc_strdup("unknown");
    175  1.1.1.6  joerg 		m->meta.date = time(NULL);
    176  1.1.1.7  joerg 		m->meta.msec = mandoc_strdup("1");
    177  1.1.1.6  joerg 	}
    178      1.1  joerg 
    179      1.1  joerg 	return(1);
    180      1.1  joerg }
    181      1.1  joerg 
    182      1.1  joerg 
    183      1.1  joerg static int
    184  1.1.1.5  joerg check_title(CHKARGS)
    185  1.1.1.5  joerg {
    186  1.1.1.5  joerg 	const char	*p;
    187  1.1.1.5  joerg 
    188  1.1.1.5  joerg 	assert(n->child);
    189  1.1.1.7  joerg 	/* FIXME: is this sufficient? */
    190  1.1.1.7  joerg 	if ('\0' == *n->child->string) {
    191  1.1.1.7  joerg 		man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
    192  1.1.1.7  joerg 		return(0);
    193  1.1.1.7  joerg 	}
    194  1.1.1.5  joerg 
    195  1.1.1.5  joerg 	for (p = n->child->string; '\0' != *p; p++)
    196  1.1.1.5  joerg 		if (isalpha((u_char)*p) && ! isupper((u_char)*p))
    197  1.1.1.7  joerg 			if ( ! man_nmsg(m, n, MANDOCERR_UPPERCASE))
    198  1.1.1.5  joerg 				return(0);
    199  1.1.1.5  joerg 
    200  1.1.1.5  joerg 	return(1);
    201  1.1.1.5  joerg }
    202  1.1.1.5  joerg 
    203  1.1.1.5  joerg 
    204  1.1.1.5  joerg static int
    205      1.1  joerg check_text(CHKARGS)
    206      1.1  joerg {
    207  1.1.1.8  joerg 	char		*p;
    208      1.1  joerg 	int		 pos, c;
    209      1.1  joerg 
    210      1.1  joerg 	assert(n->string);
    211      1.1  joerg 
    212      1.1  joerg 	for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
    213      1.1  joerg 		if ('\\' == *p) {
    214      1.1  joerg 			c = mandoc_special(p);
    215      1.1  joerg 			if (c) {
    216      1.1  joerg 				p += c - 1;
    217      1.1  joerg 				pos += c - 1;
    218      1.1  joerg 				continue;
    219      1.1  joerg 			}
    220  1.1.1.7  joerg 
    221  1.1.1.7  joerg 			c = man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
    222  1.1.1.7  joerg 			if ( ! (MAN_IGN_ESCAPE & m->pflags) && ! c)
    223  1.1.1.7  joerg 				return(c);
    224      1.1  joerg 		}
    225      1.1  joerg 
    226  1.1.1.9  joerg 		/*
    227  1.1.1.9  joerg 		 * FIXME: we absolutely cannot let \b get through or it
    228  1.1.1.9  joerg 		 * will destroy some assumptions in terms of format.
    229  1.1.1.9  joerg 	 	 */
    230  1.1.1.9  joerg 
    231  1.1.1.7  joerg 		if ('\t' == *p || isprint((u_char)*p) || ASCII_HYPH == *p)
    232      1.1  joerg 			continue;
    233  1.1.1.7  joerg 		if ( ! man_pmsg(m, n->line, pos, MANDOCERR_BADCHAR))
    234  1.1.1.7  joerg 			return(0);
    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.1.7  joerg 	man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
    248  1.1.1.7  joerg 			"line arguments %s %d (have %d)", \
    249  1.1.1.7  joerg 			#ineq, (x), n->nchild); \
    250  1.1.1.7  joerg 	return(0); \
    251      1.1  joerg }
    252      1.1  joerg 
    253      1.1  joerg INEQ_DEFINE(0, ==, eq0)
    254      1.1  joerg INEQ_DEFINE(1, <=, le1)
    255      1.1  joerg INEQ_DEFINE(2, >=, ge2)
    256      1.1  joerg INEQ_DEFINE(5, <=, le5)
    257      1.1  joerg 
    258      1.1  joerg 
    259      1.1  joerg static int
    260      1.1  joerg check_sec(CHKARGS)
    261      1.1  joerg {
    262      1.1  joerg 
    263  1.1.1.7  joerg 	if (MAN_HEAD == n->type && 0 == n->nchild) {
    264  1.1.1.7  joerg 		man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
    265  1.1.1.7  joerg 		return(0);
    266  1.1.1.7  joerg 	} else if (MAN_BODY == n->type && 0 == n->nchild)
    267  1.1.1.7  joerg 		return(man_nmsg(m, n, MANDOCERR_NOBODY));
    268  1.1.1.7  joerg 
    269      1.1  joerg 	return(1);
    270      1.1  joerg }
    271      1.1  joerg 
    272      1.1  joerg 
    273      1.1  joerg static int
    274      1.1  joerg check_part(CHKARGS)
    275      1.1  joerg {
    276      1.1  joerg 
    277      1.1  joerg 	if (MAN_BODY == n->type && 0 == n->nchild)
    278  1.1.1.7  joerg 		return(man_nmsg(m, n, MANDOCERR_NOBODY));
    279      1.1  joerg 	return(1);
    280      1.1  joerg }
    281      1.1  joerg 
    282      1.1  joerg 
    283      1.1  joerg static int
    284      1.1  joerg check_par(CHKARGS)
    285      1.1  joerg {
    286      1.1  joerg 
    287      1.1  joerg 	if (MAN_BODY == n->type)
    288      1.1  joerg 		switch (n->tok) {
    289      1.1  joerg 		case (MAN_IP):
    290      1.1  joerg 			/* FALLTHROUGH */
    291      1.1  joerg 		case (MAN_HP):
    292      1.1  joerg 			/* FALLTHROUGH */
    293      1.1  joerg 		case (MAN_TP):
    294      1.1  joerg 			/* Body-less lists are ok. */
    295      1.1  joerg 			break;
    296      1.1  joerg 		default:
    297      1.1  joerg 			if (n->nchild)
    298      1.1  joerg 				break;
    299  1.1.1.7  joerg 			return(man_nmsg(m, n, MANDOCERR_NOBODY));
    300      1.1  joerg 		}
    301      1.1  joerg 	if (MAN_HEAD == n->type)
    302      1.1  joerg 		switch (n->tok) {
    303      1.1  joerg 		case (MAN_PP):
    304      1.1  joerg 			/* FALLTHROUGH */
    305      1.1  joerg 		case (MAN_P):
    306      1.1  joerg 			/* FALLTHROUGH */
    307      1.1  joerg 		case (MAN_LP):
    308      1.1  joerg 			if (0 == n->nchild)
    309      1.1  joerg 				break;
    310  1.1.1.7  joerg 			return(man_nmsg(m, n, MANDOCERR_ARGSLOST));
    311      1.1  joerg 		default:
    312      1.1  joerg 			if (n->nchild)
    313      1.1  joerg 				break;
    314  1.1.1.7  joerg 			return(man_nmsg(m, n, MANDOCERR_NOARGS));
    315      1.1  joerg 		}
    316      1.1  joerg 
    317      1.1  joerg 	return(1);
    318      1.1  joerg }
    319      1.1  joerg 
    320      1.1  joerg 
    321      1.1  joerg static int
    322      1.1  joerg check_bline(CHKARGS)
    323      1.1  joerg {
    324      1.1  joerg 
    325      1.1  joerg 	assert( ! (MAN_ELINE & m->flags));
    326  1.1.1.7  joerg 	if (MAN_BLINE & m->flags) {
    327  1.1.1.7  joerg 		man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
    328  1.1.1.7  joerg 		return(0);
    329  1.1.1.7  joerg 	}
    330  1.1.1.5  joerg 
    331      1.1  joerg 	return(1);
    332      1.1  joerg }
    333      1.1  joerg 
    334