]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
FreeBSD fix (uqs@spoerlein.net).
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.21 2009/08/20 11:51:07 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 #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_eline(CHKARGS);
40 static int check_eq0(CHKARGS);
41 static int check_eq1(CHKARGS);
42 static int check_ge2(CHKARGS);
43 static int check_le5(CHKARGS);
44 static int check_par(CHKARGS);
45 static int check_root(CHKARGS);
46 static int check_sec(CHKARGS);
47 static int check_sp(CHKARGS);
48 static int check_text(CHKARGS);
49
50 static v_check posts_eq0[] = { check_eq0, NULL };
51 static v_check posts_ge2_le5[] = { check_ge2, check_le5, NULL };
52 static v_check posts_par[] = { check_par, NULL };
53 static v_check posts_sec[] = { check_sec, NULL };
54 static v_check posts_sp[] = { check_sp, NULL };
55 static v_check pres_eline[] = { check_eline, NULL };
56 static v_check pres_bline[] = { check_bline, NULL };
57
58 static const struct man_valid man_valids[MAN_MAX] = {
59 { pres_bline, posts_eq0 }, /* br */
60 { pres_bline, posts_ge2_le5 }, /* TH */
61 { pres_bline, posts_sec }, /* SH */
62 { pres_bline, posts_sec }, /* SS */
63 { pres_bline, posts_par }, /* TP */
64 { pres_bline, posts_par }, /* LP */
65 { pres_bline, posts_par }, /* PP */
66 { pres_bline, posts_par }, /* P */
67 { pres_bline, posts_par }, /* IP */
68 { pres_bline, posts_par }, /* HP */
69 { pres_eline, NULL }, /* SM */
70 { pres_eline, NULL }, /* SB */
71 { NULL, NULL }, /* BI */
72 { NULL, NULL }, /* IB */
73 { NULL, NULL }, /* BR */
74 { NULL, NULL }, /* RB */
75 { pres_eline, NULL }, /* R */
76 { pres_eline, NULL }, /* B */
77 { pres_eline, NULL }, /* I */
78 { NULL, NULL }, /* IR */
79 { NULL, NULL }, /* RI */
80 { pres_bline, posts_eq0 }, /* na */
81 { NULL, NULL }, /* i */
82 { pres_bline, posts_sp }, /* sp */
83 { pres_bline, posts_eq0 }, /* nf */
84 { pres_bline, posts_eq0 }, /* fi */
85 { NULL, NULL }, /* r */
86 { NULL, NULL }, /* RE */
87 { NULL, NULL }, /* RS */ /* FIXME: warn if empty body. */
88 { NULL, NULL }, /* DT */
89 };
90
91
92 int
93 man_valid_pre(struct man *m, const struct man_node *n)
94 {
95 v_check *cp;
96
97 if (MAN_TEXT == n->type)
98 return(1);
99 if (MAN_ROOT == n->type)
100 return(1);
101
102 if (NULL == (cp = man_valids[n->tok].pres))
103 return(1);
104 for ( ; *cp; cp++)
105 if ( ! (*cp)(m, n))
106 return(0);
107 return(1);
108 }
109
110
111 int
112 man_valid_post(struct man *m)
113 {
114 v_check *cp;
115
116 if (MAN_VALID & m->last->flags)
117 return(1);
118 m->last->flags |= MAN_VALID;
119
120 switch (m->last->type) {
121 case (MAN_TEXT):
122 return(check_text(m, m->last));
123 case (MAN_ROOT):
124 return(check_root(m, m->last));
125 default:
126 break;
127 }
128
129 if (NULL == (cp = man_valids[m->last->tok].posts))
130 return(1);
131 for ( ; *cp; cp++)
132 if ( ! (*cp)(m, m->last))
133 return(0);
134
135 return(1);
136 }
137
138
139 static int
140 check_root(CHKARGS)
141 {
142
143 /* XXX - make this into a warning? */
144 if (MAN_BLINE & m->flags)
145 return(man_nerr(m, n, WEXITSCOPE));
146 /* XXX - make this into a warning? */
147 if (MAN_ELINE & m->flags)
148 return(man_nerr(m, n, WEXITSCOPE));
149
150 if (NULL == m->first->child)
151 return(man_nerr(m, n, WNODATA));
152 if (NULL == m->meta.title)
153 return(man_nerr(m, n, WNOTITLE));
154
155 return(1);
156 }
157
158
159 static int
160 check_text(CHKARGS)
161 {
162 const char *p;
163 int pos, c;
164
165 assert(n->string);
166
167 for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
168 if ('\\' == *p) {
169 c = mandoc_special(p);
170 if (c) {
171 p += c - 1;
172 pos += c - 1;
173 continue;
174 }
175 if ( ! (MAN_IGN_ESCAPE & m->pflags))
176 return(man_perr(m, n->line, pos, WESCAPE));
177 if ( ! man_pwarn(m, n->line, pos, WESCAPE))
178 return(0);
179 continue;
180 }
181
182 if ('\t' == *p || isprint((u_char)*p))
183 continue;
184
185 if (MAN_IGN_CHARS & m->pflags)
186 return(man_pwarn(m, n->line, pos, WNPRINT));
187 return(man_perr(m, n->line, pos, WNPRINT));
188 }
189
190 return(1);
191 }
192
193
194 #define INEQ_DEFINE(x, ineq, name) \
195 static int \
196 check_##name(CHKARGS) \
197 { \
198 if (n->nchild ineq (x)) \
199 return(1); \
200 return(man_verr(m, n->line, n->pos, \
201 "expected line arguments %s %d, have %d", \
202 #ineq, (x), n->nchild)); \
203 }
204
205 INEQ_DEFINE(0, ==, eq0)
206 INEQ_DEFINE(1, ==, eq1)
207 INEQ_DEFINE(2, >=, ge2)
208 INEQ_DEFINE(5, <=, le5)
209
210
211 static int
212 check_sp(CHKARGS)
213 {
214 long lval;
215 char *ep, *buf;
216
217 if (NULL == n->child)
218 return(1);
219 else if ( ! check_eq1(m, n))
220 return(0);
221
222 assert(MAN_TEXT == n->child->type);
223 buf = n->child->string;
224 assert(buf);
225
226 /* From OpenBSD's strtol(3). */
227
228 errno = 0;
229 lval = strtol(buf, &ep, 10);
230 if (buf[0] == '\0' || *ep != '\0')
231 return(man_nerr(m, n->child, WNUMFMT));
232
233 if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
234 (lval > INT_MAX || lval < 0))
235 return(man_nerr(m, n->child, WNUMFMT));
236
237 return(1);
238 }
239
240
241 static int
242 check_sec(CHKARGS)
243 {
244
245 if (MAN_BODY == n->type && 0 == n->nchild)
246 return(man_nwarn(m, n, WBODYARGS));
247 if (MAN_HEAD == n->type && 0 == n->nchild)
248 return(man_nerr(m, n, WHEADARGS));
249 return(1);
250 }
251
252
253 static int
254 check_par(CHKARGS)
255 {
256
257 if (MAN_BODY == n->type)
258 switch (n->tok) {
259 case (MAN_IP):
260 /* FALLTHROUGH */
261 case (MAN_HP):
262 /* FALLTHROUGH */
263 case (MAN_TP):
264 /* Body-less lists are ok. */
265 break;
266 default:
267 if (n->nchild)
268 break;
269 return(man_nwarn(m, n, WBODYARGS));
270 }
271 if (MAN_HEAD == n->type)
272 switch (n->tok) {
273 case (MAN_PP):
274 /* FALLTHROUGH */
275 case (MAN_P):
276 /* FALLTHROUGH */
277 case (MAN_LP):
278 if (0 == n->nchild)
279 break;
280 return(man_nwarn(m, n, WNHEADARGS));
281 default:
282 if (n->nchild)
283 break;
284 return(man_nwarn(m, n, WHEADARGS));
285 }
286
287 return(1);
288 }
289
290
291 static int
292 check_eline(CHKARGS)
293 {
294
295 if (MAN_ELINE & m->flags)
296 return(man_nerr(m, n, WLNSCOPE));
297 return(1);
298 }
299
300
301 static int
302 check_bline(CHKARGS)
303 {
304
305 if (MAN_BLINE & m->flags)
306 return(man_nerr(m, n, WLNSCOPE));
307 if (MAN_ELINE & m->flags)
308 return(man_nerr(m, n, WLNSCOPE));
309 return(1);
310 }
311