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