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