]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
Fixed missing check for open ELINE scope in BLINE macro.
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.18 2009/08/18 11:46:44 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 };
87
88
89 int
90 man_valid_pre(struct man *m, const struct man_node *n)
91 {
92 v_check *cp;
93
94 if (MAN_TEXT == n->type)
95 return(1);
96 if (MAN_ROOT == n->type)
97 return(1);
98
99 if (NULL == (cp = man_valids[n->tok].pres))
100 return(1);
101 for ( ; *cp; cp++)
102 if ( ! (*cp)(m, n))
103 return(0);
104 return(1);
105 }
106
107
108 int
109 man_valid_post(struct man *m)
110 {
111 v_check *cp;
112
113 if (MAN_VALID & m->last->flags)
114 return(1);
115 m->last->flags |= MAN_VALID;
116
117 switch (m->last->type) {
118 case (MAN_TEXT):
119 return(check_text(m, m->last));
120 case (MAN_ROOT):
121 return(check_root(m, m->last));
122 default:
123 break;
124 }
125
126 if (NULL == (cp = man_valids[m->last->tok].posts))
127 return(1);
128 for ( ; *cp; cp++)
129 if ( ! (*cp)(m, m->last))
130 return(0);
131
132 return(1);
133 }
134
135
136 static int
137 check_root(CHKARGS)
138 {
139
140 /* XXX - make this into a warning? */
141 if (MAN_BLINE & m->flags)
142 return(man_nerr(m, n, WEXITSCOPE));
143 /* XXX - make this into a warning? */
144 if (MAN_ELINE & m->flags)
145 return(man_nerr(m, n, WEXITSCOPE));
146
147 if (NULL == m->first->child)
148 return(man_nerr(m, n, WNODATA));
149 if (NULL == m->meta.title)
150 return(man_nerr(m, n, WNOTITLE));
151
152 return(1);
153 }
154
155
156 static int
157 check_text(CHKARGS)
158 {
159 const char *p;
160 int pos, c;
161
162 assert(n->string);
163
164 for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
165 if ('\\' == *p) {
166 c = mandoc_special(p);
167 if (c) {
168 p += c - 1;
169 pos += c - 1;
170 continue;
171 }
172 if ( ! (MAN_IGN_ESCAPE & m->pflags))
173 return(man_perr(m, n->line, pos, WESCAPE));
174 if ( ! man_pwarn(m, n->line, pos, WESCAPE))
175 return(0);
176 continue;
177 }
178
179 if ('\t' == *p || isprint((u_char)*p))
180 continue;
181
182 if (MAN_IGN_CHARS & m->pflags)
183 return(man_pwarn(m, n->line, pos, WNPRINT));
184 return(man_perr(m, n->line, pos, WNPRINT));
185 }
186
187 return(1);
188 }
189
190
191 #define INEQ_DEFINE(x, ineq, name) \
192 static int \
193 check_##name(CHKARGS) \
194 { \
195 if (n->nchild ineq (x)) \
196 return(1); \
197 return(man_verr(m, n->line, n->pos, \
198 "expected line arguments %s %d, have %d", \
199 #ineq, (x), n->nchild)); \
200 }
201
202 INEQ_DEFINE(0, ==, eq0)
203 INEQ_DEFINE(1, ==, eq1)
204 INEQ_DEFINE(2, >=, ge2)
205 INEQ_DEFINE(5, <=, le5)
206
207
208 static int
209 check_sp(CHKARGS)
210 {
211 long lval;
212 char *ep, *buf;
213
214 if (NULL == n->child)
215 return(1);
216 else if ( ! check_eq1(m, n))
217 return(0);
218
219 assert(MAN_TEXT == n->child->type);
220 buf = n->child->string;
221 assert(buf);
222
223 /* From OpenBSD's strtol(3). */
224
225 errno = 0;
226 lval = strtol(buf, &ep, 10);
227 if (buf[0] == '\0' || *ep != '\0')
228 return(man_nerr(m, n->child, WNUMFMT));
229
230 if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
231 (lval > INT_MAX || lval < 0))
232 return(man_nerr(m, n->child, WNUMFMT));
233
234 return(1);
235 }
236
237
238 static int
239 check_sec(CHKARGS)
240 {
241
242 if (MAN_BODY == n->type && 0 == n->nchild)
243 return(man_nwarn(m, n, WBODYARGS));
244 if (MAN_HEAD == n->type && 0 == n->nchild)
245 return(man_nerr(m, n, WHEADARGS));
246 return(1);
247 }
248
249
250 static int
251 check_par(CHKARGS)
252 {
253
254 if (MAN_BODY == n->type)
255 switch (n->tok) {
256 case (MAN_IP):
257 /* FALLTHROUGH */
258 case (MAN_HP):
259 /* FALLTHROUGH */
260 case (MAN_TP):
261 /* Body-less lists are ok. */
262 break;
263 default:
264 if (n->nchild)
265 break;
266 return(man_nwarn(m, n, WBODYARGS));
267 }
268 if (MAN_HEAD == n->type)
269 switch (n->tok) {
270 case (MAN_PP):
271 /* FALLTHROUGH */
272 case (MAN_P):
273 /* FALLTHROUGH */
274 case (MAN_LP):
275 if (0 == n->nchild)
276 break;
277 return(man_nwarn(m, n, WNHEADARGS));
278 default:
279 if (n->nchild)
280 break;
281 return(man_nwarn(m, n, WHEADARGS));
282 }
283
284 return(1);
285 }
286
287
288 static int
289 check_eline(CHKARGS)
290 {
291
292 if (MAN_ELINE & m->flags)
293 return(man_nerr(m, n, WLNSCOPE));
294 return(1);
295 }
296
297
298 static int
299 check_bline(CHKARGS)
300 {
301
302 if (MAN_BLINE & m->flags)
303 return(man_nerr(m, n, WLNSCOPE));
304 if (MAN_ELINE & m->flags)
305 return(man_nerr(m, n, WLNSCOPE));
306 return(1);
307 }
308