]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
Slightly tweak www section/subsections.
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.72 2011/07/26 14:09:01 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <sys/types.h>
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include "man.h"
34 #include "mandoc.h"
35 #include "libman.h"
36 #include "libmandoc.h"
37
38 #define CHKARGS struct man *m, struct man_node *n
39
40 typedef int (*v_check)(CHKARGS);
41
42 struct man_valid {
43 v_check *pres;
44 v_check *posts;
45 };
46
47 static int check_bline(CHKARGS);
48 static int check_eq0(CHKARGS);
49 static int check_ft(CHKARGS);
50 static int check_le1(CHKARGS);
51 static int check_ge2(CHKARGS);
52 static int check_le5(CHKARGS);
53 static int check_par(CHKARGS);
54 static int check_part(CHKARGS);
55 static int check_root(CHKARGS);
56 static int check_sec(CHKARGS);
57 static void check_text(CHKARGS);
58
59 static int post_AT(CHKARGS);
60 static int post_vs(CHKARGS);
61 static int post_fi(CHKARGS);
62 static int post_nf(CHKARGS);
63 static int post_TH(CHKARGS);
64 static int post_UC(CHKARGS);
65
66 static v_check posts_at[] = { post_AT, NULL };
67 static v_check posts_br[] = { post_vs, check_eq0, NULL };
68 static v_check posts_eq0[] = { check_eq0, NULL };
69 static v_check posts_fi[] = { check_eq0, post_fi, NULL };
70 static v_check posts_ft[] = { check_ft, NULL };
71 static v_check posts_nf[] = { check_eq0, post_nf, NULL };
72 static v_check posts_par[] = { check_par, NULL };
73 static v_check posts_part[] = { check_part, NULL };
74 static v_check posts_sec[] = { check_sec, NULL };
75 static v_check posts_sp[] = { post_vs, check_le1, NULL };
76 static v_check posts_th[] = { check_ge2, check_le5, post_TH, NULL };
77 static v_check posts_uc[] = { post_UC, NULL };
78 static v_check pres_bline[] = { check_bline, NULL };
79
80 static const struct man_valid man_valids[MAN_MAX] = {
81 { NULL, posts_br }, /* br */
82 { pres_bline, posts_th }, /* TH */
83 { pres_bline, posts_sec }, /* SH */
84 { pres_bline, posts_sec }, /* SS */
85 { pres_bline, NULL }, /* TP */
86 { pres_bline, posts_par }, /* LP */
87 { pres_bline, posts_par }, /* PP */
88 { pres_bline, posts_par }, /* P */
89 { pres_bline, NULL }, /* IP */
90 { pres_bline, NULL }, /* HP */
91 { NULL, NULL }, /* SM */
92 { NULL, NULL }, /* SB */
93 { NULL, NULL }, /* BI */
94 { NULL, NULL }, /* IB */
95 { NULL, NULL }, /* BR */
96 { NULL, NULL }, /* RB */
97 { NULL, NULL }, /* R */
98 { NULL, NULL }, /* B */
99 { NULL, NULL }, /* I */
100 { NULL, NULL }, /* IR */
101 { NULL, NULL }, /* RI */
102 { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
103 { NULL, posts_sp }, /* sp */ /* FIXME: should warn only. */
104 { pres_bline, posts_nf }, /* nf */
105 { pres_bline, posts_fi }, /* fi */
106 { NULL, NULL }, /* RE */
107 { NULL, posts_part }, /* RS */
108 { NULL, NULL }, /* DT */
109 { NULL, posts_uc }, /* UC */
110 { NULL, NULL }, /* PD */
111 { NULL, posts_at }, /* AT */
112 { NULL, NULL }, /* in */
113 { NULL, posts_ft }, /* ft */
114 };
115
116
117 int
118 man_valid_pre(struct man *m, struct man_node *n)
119 {
120 v_check *cp;
121
122 switch (n->type) {
123 case (MAN_TEXT):
124 /* FALLTHROUGH */
125 case (MAN_ROOT):
126 /* FALLTHROUGH */
127 case (MAN_EQN):
128 /* FALLTHROUGH */
129 case (MAN_TBL):
130 return(1);
131 default:
132 break;
133 }
134
135 if (NULL == (cp = man_valids[n->tok].pres))
136 return(1);
137 for ( ; *cp; cp++)
138 if ( ! (*cp)(m, n))
139 return(0);
140 return(1);
141 }
142
143
144 int
145 man_valid_post(struct man *m)
146 {
147 v_check *cp;
148
149 if (MAN_VALID & m->last->flags)
150 return(1);
151 m->last->flags |= MAN_VALID;
152
153 switch (m->last->type) {
154 case (MAN_TEXT):
155 check_text(m, m->last);
156 return(1);
157 case (MAN_ROOT):
158 return(check_root(m, m->last));
159 case (MAN_EQN):
160 /* FALLTHROUGH */
161 case (MAN_TBL):
162 return(1);
163 default:
164 break;
165 }
166
167 if (NULL == (cp = man_valids[m->last->tok].posts))
168 return(1);
169 for ( ; *cp; cp++)
170 if ( ! (*cp)(m, m->last))
171 return(0);
172
173 return(1);
174 }
175
176
177 static int
178 check_root(CHKARGS)
179 {
180
181 if (MAN_BLINE & m->flags)
182 man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
183 else if (MAN_ELINE & m->flags)
184 man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
185
186 m->flags &= ~MAN_BLINE;
187 m->flags &= ~MAN_ELINE;
188
189 if (NULL == m->first->child) {
190 man_nmsg(m, n, MANDOCERR_NODOCBODY);
191 return(0);
192 } else if (NULL == m->meta.title) {
193 man_nmsg(m, n, MANDOCERR_NOTITLE);
194
195 /*
196 * If a title hasn't been set, do so now (by
197 * implication, date and section also aren't set).
198 */
199
200 m->meta.title = mandoc_strdup("unknown");
201 m->meta.msec = mandoc_strdup("1");
202 m->meta.date = mandoc_normdate
203 (m->parse, NULL, n->line, n->pos);
204 }
205
206 return(1);
207 }
208
209 static void
210 check_text(CHKARGS)
211 {
212 char *cp, *p;
213
214 cp = p = n->string;
215 for (cp = p; NULL != (p = strchr(p, '\t')); p++) {
216 if (MAN_LITERAL & m->flags)
217 continue;
218 man_pmsg(m, n->line, (int)(p - cp), MANDOCERR_BADTAB);
219 }
220 }
221
222 #define INEQ_DEFINE(x, ineq, name) \
223 static int \
224 check_##name(CHKARGS) \
225 { \
226 if (n->nchild ineq (x)) \
227 return(1); \
228 mandoc_vmsg(MANDOCERR_ARGCOUNT, m->parse, n->line, n->pos, \
229 "line arguments %s %d (have %d)", \
230 #ineq, (x), n->nchild); \
231 return(1); \
232 }
233
234 INEQ_DEFINE(0, ==, eq0)
235 INEQ_DEFINE(1, <=, le1)
236 INEQ_DEFINE(2, >=, ge2)
237 INEQ_DEFINE(5, <=, le5)
238
239 static int
240 check_ft(CHKARGS)
241 {
242 char *cp;
243 int ok;
244
245 if (0 == n->nchild)
246 return(1);
247
248 ok = 0;
249 cp = n->child->string;
250 switch (*cp) {
251 case ('1'):
252 /* FALLTHROUGH */
253 case ('2'):
254 /* FALLTHROUGH */
255 case ('3'):
256 /* FALLTHROUGH */
257 case ('4'):
258 /* FALLTHROUGH */
259 case ('I'):
260 /* FALLTHROUGH */
261 case ('P'):
262 /* FALLTHROUGH */
263 case ('R'):
264 if ('\0' == cp[1])
265 ok = 1;
266 break;
267 case ('B'):
268 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
269 ok = 1;
270 break;
271 case ('C'):
272 if ('W' == cp[1] && '\0' == cp[2])
273 ok = 1;
274 break;
275 default:
276 break;
277 }
278
279 if (0 == ok) {
280 mandoc_vmsg
281 (MANDOCERR_BADFONT, m->parse,
282 n->line, n->pos, "%s", cp);
283 *cp = '\0';
284 }
285
286 if (1 < n->nchild)
287 mandoc_vmsg
288 (MANDOCERR_ARGCOUNT, m->parse, n->line,
289 n->pos, "want one child (have %d)",
290 n->nchild);
291
292 return(1);
293 }
294
295 static int
296 check_sec(CHKARGS)
297 {
298
299 if ( ! (MAN_HEAD == n->type && 0 == n->nchild))
300 return(1);
301
302 man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
303 return(0);
304 }
305
306
307 static int
308 check_part(CHKARGS)
309 {
310
311 if (MAN_BODY == n->type && 0 == n->nchild)
312 mandoc_msg(MANDOCERR_ARGCWARN, m->parse, n->line,
313 n->pos, "want children (have none)");
314
315 return(1);
316 }
317
318
319 static int
320 check_par(CHKARGS)
321 {
322
323 switch (n->type) {
324 case (MAN_BLOCK):
325 if (0 == n->body->nchild)
326 man_node_delete(m, n);
327 break;
328 case (MAN_BODY):
329 if (0 == n->nchild)
330 man_nmsg(m, n, MANDOCERR_IGNPAR);
331 break;
332 case (MAN_HEAD):
333 if (n->nchild)
334 man_nmsg(m, n, MANDOCERR_ARGSLOST);
335 break;
336 default:
337 break;
338 }
339
340 return(1);
341 }
342
343
344 static int
345 check_bline(CHKARGS)
346 {
347
348 assert( ! (MAN_ELINE & m->flags));
349 if (MAN_BLINE & m->flags) {
350 man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
351 return(0);
352 }
353
354 return(1);
355 }
356
357 static int
358 post_TH(CHKARGS)
359 {
360 const char *p;
361 int line, pos;
362
363 if (m->meta.title)
364 free(m->meta.title);
365 if (m->meta.vol)
366 free(m->meta.vol);
367 if (m->meta.source)
368 free(m->meta.source);
369 if (m->meta.msec)
370 free(m->meta.msec);
371 if (m->meta.date)
372 free(m->meta.date);
373
374 line = n->line;
375 pos = n->pos;
376 m->meta.title = m->meta.vol = m->meta.date =
377 m->meta.msec = m->meta.source = NULL;
378
379 /* ->TITLE<- MSEC DATE SOURCE VOL */
380
381 n = n->child;
382 if (n && n->string) {
383 for (p = n->string; '\0' != *p; p++) {
384 /* Only warn about this once... */
385 if (isalpha((unsigned char)*p) &&
386 ! isupper((unsigned char)*p)) {
387 man_nmsg(m, n, MANDOCERR_UPPERCASE);
388 break;
389 }
390 }
391 m->meta.title = mandoc_strdup(n->string);
392 } else
393 m->meta.title = mandoc_strdup("");
394
395 /* TITLE ->MSEC<- DATE SOURCE VOL */
396
397 if (n)
398 n = n->next;
399 if (n && n->string)
400 m->meta.msec = mandoc_strdup(n->string);
401 else
402 m->meta.msec = mandoc_strdup("");
403
404 /* TITLE MSEC ->DATE<- SOURCE VOL */
405
406 if (n)
407 n = n->next;
408 if (n)
409 pos = n->pos;
410 m->meta.date = mandoc_normdate
411 (m->parse, n ? n->string : NULL, line, pos);
412
413 /* TITLE MSEC DATE ->SOURCE<- VOL */
414
415 if (n && (n = n->next))
416 m->meta.source = mandoc_strdup(n->string);
417
418 /* TITLE MSEC DATE SOURCE ->VOL<- */
419
420 if (n && (n = n->next))
421 m->meta.vol = mandoc_strdup(n->string);
422
423 /*
424 * Remove the `TH' node after we've processed it for our
425 * meta-data.
426 */
427 man_node_delete(m, m->last);
428 return(1);
429 }
430
431 static int
432 post_nf(CHKARGS)
433 {
434
435 if (MAN_LITERAL & m->flags)
436 man_nmsg(m, n, MANDOCERR_SCOPEREP);
437
438 m->flags |= MAN_LITERAL;
439 return(1);
440 }
441
442 static int
443 post_fi(CHKARGS)
444 {
445
446 if ( ! (MAN_LITERAL & m->flags))
447 man_nmsg(m, n, MANDOCERR_WNOSCOPE);
448
449 m->flags &= ~MAN_LITERAL;
450 return(1);
451 }
452
453 static int
454 post_UC(CHKARGS)
455 {
456 static const char * const bsd_versions[] = {
457 "3rd Berkeley Distribution",
458 "4th Berkeley Distribution",
459 "4.2 Berkeley Distribution",
460 "4.3 Berkeley Distribution",
461 "4.4 Berkeley Distribution",
462 };
463
464 const char *p, *s;
465
466 n = n->child;
467 n = m->last->child;
468
469 if (NULL == n || MAN_TEXT != n->type)
470 p = bsd_versions[0];
471 else {
472 s = n->string;
473 if (0 == strcmp(s, "3"))
474 p = bsd_versions[0];
475 else if (0 == strcmp(s, "4"))
476 p = bsd_versions[1];
477 else if (0 == strcmp(s, "5"))
478 p = bsd_versions[2];
479 else if (0 == strcmp(s, "6"))
480 p = bsd_versions[3];
481 else if (0 == strcmp(s, "7"))
482 p = bsd_versions[4];
483 else
484 p = bsd_versions[0];
485 }
486
487 if (m->meta.source)
488 free(m->meta.source);
489
490 m->meta.source = mandoc_strdup(p);
491 return(1);
492 }
493
494 static int
495 post_AT(CHKARGS)
496 {
497 static const char * const unix_versions[] = {
498 "7th Edition",
499 "System III",
500 "System V",
501 "System V Release 2",
502 };
503
504 const char *p, *s;
505 struct man_node *nn;
506
507 n = n->child;
508
509 if (NULL == n || MAN_TEXT != n->type)
510 p = unix_versions[0];
511 else {
512 s = n->string;
513 if (0 == strcmp(s, "3"))
514 p = unix_versions[0];
515 else if (0 == strcmp(s, "4"))
516 p = unix_versions[1];
517 else if (0 == strcmp(s, "5")) {
518 nn = n->next;
519 if (nn && MAN_TEXT == nn->type && nn->string[0])
520 p = unix_versions[3];
521 else
522 p = unix_versions[2];
523 } else
524 p = unix_versions[0];
525 }
526
527 if (m->meta.source)
528 free(m->meta.source);
529
530 m->meta.source = mandoc_strdup(p);
531 return(1);
532 }
533
534 static int
535 post_vs(CHKARGS)
536 {
537
538 /*
539 * Don't warn about this because it occurs in pod2man and would
540 * cause considerable (unfixable) warnage.
541 */
542 if (NULL == n->prev && MAN_ROOT == n->parent->type)
543 man_node_delete(m, n);
544
545 return(1);
546 }