1 /* $Id: man_validate.c,v 1.28 2010/01/01 17:14:28 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
27 #include "libmandoc.h"
29 #define CHKARGS struct man *m, const struct man_node *n
31 typedef int (*v_check)(CHKARGS);
38 static int check_bline(CHKARGS);
39 static int check_eq0(CHKARGS);
40 static int check_le1(CHKARGS);
41 static int check_ge2(CHKARGS);
42 static int check_le5(CHKARGS);
43 static int check_par(CHKARGS);
44 static int check_part(CHKARGS);
45 static int check_root(CHKARGS);
46 static int check_sec(CHKARGS);
47 static int check_text(CHKARGS);
49 static v_check posts_eq0[] = { check_eq0, NULL };
50 static v_check posts_ge2_le5[] = { check_ge2, check_le5, NULL };
51 static v_check posts_par[] = { check_par, NULL };
52 static v_check posts_part[] = { check_part, NULL };
53 static v_check posts_sec[] = { check_sec, NULL };
54 static v_check posts_sp[] = { check_le1, NULL };
55 static v_check pres_bline[] = { check_bline, NULL };
57 static const struct man_valid man_valids[MAN_MAX] = {
58 { pres_bline, posts_eq0 }, /* br */
59 { pres_bline, posts_ge2_le5 }, /* TH */ /* FIXME: make sure capitalised. */
60 { pres_bline, posts_sec }, /* SH */
61 { pres_bline, posts_sec }, /* SS */
62 { pres_bline, posts_par }, /* TP */
63 { pres_bline, posts_par }, /* LP */
64 { pres_bline, posts_par }, /* PP */
65 { pres_bline, posts_par }, /* P */
66 { pres_bline, posts_par }, /* IP */
67 { pres_bline, posts_par }, /* HP */
68 { NULL, NULL }, /* SM */
69 { NULL, NULL }, /* SB */
70 { NULL, NULL }, /* BI */
71 { NULL, NULL }, /* IB */
72 { NULL, NULL }, /* BR */
73 { NULL, NULL }, /* RB */
74 { NULL, NULL }, /* R */
75 { NULL, NULL }, /* B */
76 { NULL, NULL }, /* I */
77 { NULL, NULL }, /* IR */
78 { NULL, NULL }, /* RI */
79 { pres_bline, posts_eq0 }, /* na */
80 { NULL, NULL }, /* i */
81 { pres_bline, posts_sp }, /* sp */
82 { pres_bline, posts_eq0 }, /* nf */
83 { pres_bline, posts_eq0 }, /* fi */
84 { NULL, NULL }, /* r */
85 { NULL, NULL }, /* RE */
86 { NULL, posts_part }, /* RS */
87 { NULL, NULL }, /* DT */
88 { NULL, NULL }, /* UC */
89 { NULL, NULL }, /* PD */
94 man_valid_pre(struct man *m, const struct man_node *n)
98 if (MAN_TEXT == n->type)
100 if (MAN_ROOT == n->type)
103 if (NULL == (cp = man_valids[n->tok].pres))
113 man_valid_post(struct man *m)
117 if (MAN_VALID & m->last->flags)
119 m->last->flags |= MAN_VALID;
121 switch (m->last->type) {
123 return(check_text(m, m->last));
125 return(check_root(m, m->last));
130 if (NULL == (cp = man_valids[m->last->tok].posts))
133 if ( ! (*cp)(m, m->last))
144 if (MAN_BLINE & m->flags)
145 return(man_nwarn(m, n, WEXITSCOPE));
146 if (MAN_ELINE & m->flags)
147 return(man_nwarn(m, n, WEXITSCOPE));
149 m->flags &= ~MAN_BLINE;
150 m->flags &= ~MAN_ELINE;
152 if (NULL == m->first->child)
153 return(man_nerr(m, n, WNODATA));
154 if (NULL == m->meta.title)
155 return(man_nerr(m, n, WNOTITLE));
169 for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
171 c = mandoc_special(p);
177 if ( ! (MAN_IGN_ESCAPE & m->pflags))
178 return(man_perr(m, n->line, pos, WESCAPE));
179 if ( ! man_pwarn(m, n->line, pos, WESCAPE))
184 if ('\t' == *p || isprint((u_char)*p))
187 if (MAN_IGN_CHARS & m->pflags)
188 return(man_pwarn(m, n->line, pos, WNPRINT));
189 return(man_perr(m, n->line, pos, WNPRINT));
196 #define INEQ_DEFINE(x, ineq, name) \
198 check_##name(CHKARGS) \
200 if (n->nchild ineq (x)) \
202 return(man_verr(m, n->line, n->pos, \
203 "expected line arguments %s %d, have %d", \
204 #ineq, (x), n->nchild)); \
207 INEQ_DEFINE(0, ==, eq0)
208 INEQ_DEFINE(1, <=, le1)
209 INEQ_DEFINE(2, >=, ge2)
210 INEQ_DEFINE(5, <=, le5)
217 if (MAN_BODY == n->type && 0 == n->nchild)
218 return(man_nwarn(m, n, WBODYARGS));
219 if (MAN_HEAD == n->type && 0 == n->nchild)
220 return(man_nerr(m, n, WHEADARGS));
229 if (MAN_BODY == n->type && 0 == n->nchild)
230 return(man_nwarn(m, n, WBODYARGS));
239 if (MAN_BODY == n->type)
246 /* Body-less lists are ok. */
251 return(man_nwarn(m, n, WBODYARGS));
253 if (MAN_HEAD == n->type)
262 return(man_nwarn(m, n, WNHEADARGS));
266 return(man_nwarn(m, n, WHEADARGS));
277 assert( ! (MAN_ELINE & m->flags));
278 if (MAN_BLINE & m->flags)
279 return(man_nerr(m, n, WLNSCOPE));