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