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