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