man_validate.c revision 1.1.1.5 1 1.1.1.5 joerg /* $Vendor-Id: man_validate.c,v 1.33 2010/03/29 10:10:35 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 joerg if (NULL == m->meta.title)
171 1.1 joerg return(man_nerr(m, n, WNOTITLE));
172 1.1 joerg
173 1.1 joerg return(1);
174 1.1 joerg }
175 1.1 joerg
176 1.1 joerg
177 1.1 joerg static int
178 1.1.1.5 joerg check_title(CHKARGS)
179 1.1.1.5 joerg {
180 1.1.1.5 joerg const char *p;
181 1.1.1.5 joerg
182 1.1.1.5 joerg assert(n->child);
183 1.1.1.5 joerg if ('\0' == *n->child->string)
184 1.1.1.5 joerg return(man_nerr(m, n, WNOTITLE));
185 1.1.1.5 joerg
186 1.1.1.5 joerg for (p = n->child->string; '\0' != *p; p++)
187 1.1.1.5 joerg if (isalpha((u_char)*p) && ! isupper((u_char)*p))
188 1.1.1.5 joerg if ( ! man_nwarn(m, n, WTITLECASE))
189 1.1.1.5 joerg return(0);
190 1.1.1.5 joerg
191 1.1.1.5 joerg return(1);
192 1.1.1.5 joerg }
193 1.1.1.5 joerg
194 1.1.1.5 joerg
195 1.1.1.5 joerg static int
196 1.1 joerg check_text(CHKARGS)
197 1.1 joerg {
198 1.1 joerg const char *p;
199 1.1 joerg int pos, c;
200 1.1 joerg
201 1.1 joerg assert(n->string);
202 1.1 joerg
203 1.1 joerg for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
204 1.1 joerg if ('\\' == *p) {
205 1.1 joerg c = mandoc_special(p);
206 1.1 joerg if (c) {
207 1.1 joerg p += c - 1;
208 1.1 joerg pos += c - 1;
209 1.1 joerg continue;
210 1.1 joerg }
211 1.1 joerg if ( ! (MAN_IGN_ESCAPE & m->pflags))
212 1.1 joerg return(man_perr(m, n->line, pos, WESCAPE));
213 1.1 joerg if ( ! man_pwarn(m, n->line, pos, WESCAPE))
214 1.1 joerg return(0);
215 1.1 joerg continue;
216 1.1 joerg }
217 1.1 joerg
218 1.1 joerg if ('\t' == *p || isprint((u_char)*p))
219 1.1 joerg continue;
220 1.1 joerg
221 1.1 joerg if (MAN_IGN_CHARS & m->pflags)
222 1.1 joerg return(man_pwarn(m, n->line, pos, WNPRINT));
223 1.1 joerg return(man_perr(m, n->line, pos, WNPRINT));
224 1.1 joerg }
225 1.1 joerg
226 1.1 joerg return(1);
227 1.1 joerg }
228 1.1 joerg
229 1.1 joerg
230 1.1 joerg #define INEQ_DEFINE(x, ineq, name) \
231 1.1 joerg static int \
232 1.1 joerg check_##name(CHKARGS) \
233 1.1 joerg { \
234 1.1 joerg if (n->nchild ineq (x)) \
235 1.1 joerg return(1); \
236 1.1 joerg return(man_verr(m, n->line, n->pos, \
237 1.1 joerg "expected line arguments %s %d, have %d", \
238 1.1 joerg #ineq, (x), n->nchild)); \
239 1.1 joerg }
240 1.1 joerg
241 1.1 joerg INEQ_DEFINE(0, ==, eq0)
242 1.1 joerg INEQ_DEFINE(1, <=, le1)
243 1.1 joerg INEQ_DEFINE(2, >=, ge2)
244 1.1 joerg INEQ_DEFINE(5, <=, le5)
245 1.1 joerg
246 1.1 joerg
247 1.1 joerg static int
248 1.1 joerg check_sec(CHKARGS)
249 1.1 joerg {
250 1.1 joerg
251 1.1 joerg if (MAN_BODY == n->type && 0 == n->nchild)
252 1.1 joerg return(man_nwarn(m, n, WBODYARGS));
253 1.1 joerg if (MAN_HEAD == n->type && 0 == n->nchild)
254 1.1 joerg return(man_nerr(m, n, WHEADARGS));
255 1.1 joerg return(1);
256 1.1 joerg }
257 1.1 joerg
258 1.1 joerg
259 1.1 joerg static int
260 1.1 joerg check_part(CHKARGS)
261 1.1 joerg {
262 1.1 joerg
263 1.1 joerg if (MAN_BODY == n->type && 0 == n->nchild)
264 1.1 joerg return(man_nwarn(m, n, WBODYARGS));
265 1.1 joerg return(1);
266 1.1 joerg }
267 1.1 joerg
268 1.1 joerg
269 1.1 joerg static int
270 1.1 joerg check_par(CHKARGS)
271 1.1 joerg {
272 1.1 joerg
273 1.1 joerg if (MAN_BODY == n->type)
274 1.1 joerg switch (n->tok) {
275 1.1 joerg case (MAN_IP):
276 1.1 joerg /* FALLTHROUGH */
277 1.1 joerg case (MAN_HP):
278 1.1 joerg /* FALLTHROUGH */
279 1.1 joerg case (MAN_TP):
280 1.1 joerg /* Body-less lists are ok. */
281 1.1 joerg break;
282 1.1 joerg default:
283 1.1 joerg if (n->nchild)
284 1.1 joerg break;
285 1.1 joerg return(man_nwarn(m, n, WBODYARGS));
286 1.1 joerg }
287 1.1 joerg if (MAN_HEAD == n->type)
288 1.1 joerg switch (n->tok) {
289 1.1 joerg case (MAN_PP):
290 1.1 joerg /* FALLTHROUGH */
291 1.1 joerg case (MAN_P):
292 1.1 joerg /* FALLTHROUGH */
293 1.1 joerg case (MAN_LP):
294 1.1 joerg if (0 == n->nchild)
295 1.1 joerg break;
296 1.1 joerg return(man_nwarn(m, n, WNHEADARGS));
297 1.1 joerg default:
298 1.1 joerg if (n->nchild)
299 1.1 joerg break;
300 1.1 joerg return(man_nwarn(m, n, WHEADARGS));
301 1.1 joerg }
302 1.1 joerg
303 1.1 joerg return(1);
304 1.1 joerg }
305 1.1 joerg
306 1.1 joerg
307 1.1 joerg static int
308 1.1 joerg check_bline(CHKARGS)
309 1.1 joerg {
310 1.1 joerg
311 1.1 joerg assert( ! (MAN_ELINE & m->flags));
312 1.1 joerg if (MAN_BLINE & m->flags)
313 1.1 joerg return(man_nerr(m, n, WLNSCOPE));
314 1.1.1.5 joerg
315 1.1 joerg return(1);
316 1.1 joerg }
317 1.1 joerg
318 1.1.1.5 joerg
319 1.1.1.5 joerg static int
320 1.1.1.5 joerg check_roff(CHKARGS)
321 1.1.1.5 joerg {
322 1.1.1.5 joerg
323 1.1.1.5 joerg if (MAN_BLOCK != n->type)
324 1.1.1.5 joerg return(1);
325 1.1.1.5 joerg
326 1.1.1.5 joerg for (n = n->parent; n; n = n->parent)
327 1.1.1.5 joerg if (MAN_de == n->tok || MAN_dei == n->tok ||
328 1.1.1.5 joerg MAN_am == n->tok ||
329 1.1.1.5 joerg MAN_ami == n->tok ||
330 1.1.1.5 joerg MAN_ig == n->tok)
331 1.1.1.5 joerg return(man_nerr(m, n, WROFFNEST));
332 1.1.1.5 joerg
333 1.1.1.5 joerg return(1);
334 1.1.1.5 joerg }
335