]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
While we do not recommend the idiom ".Fl Fl long" for long options
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.154 2020/04/24 12:02:33 schwarze Exp $ */
2 /*
3 * Copyright (c) 2010, 2012-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 * Validation module for man(7) syntax trees used by mandoc(1).
19 */
20 #include "config.h"
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 <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33
34 #include "mandoc_aux.h"
35 #include "mandoc.h"
36 #include "roff.h"
37 #include "man.h"
38 #include "libmandoc.h"
39 #include "roff_int.h"
40 #include "libman.h"
41 #include "tag.h"
42
43 #define CHKARGS struct roff_man *man, struct roff_node *n
44
45 typedef void (*v_check)(CHKARGS);
46
47 static void check_abort(CHKARGS) __attribute__((__noreturn__));
48 static void check_par(CHKARGS);
49 static void check_part(CHKARGS);
50 static void check_root(CHKARGS);
51 static void check_tag(struct roff_node *, struct roff_node *);
52 static void check_text(CHKARGS);
53
54 static void post_AT(CHKARGS);
55 static void post_EE(CHKARGS);
56 static void post_EX(CHKARGS);
57 static void post_IP(CHKARGS);
58 static void post_OP(CHKARGS);
59 static void post_SH(CHKARGS);
60 static void post_TH(CHKARGS);
61 static void post_TP(CHKARGS);
62 static void post_UC(CHKARGS);
63 static void post_UR(CHKARGS);
64 static void post_in(CHKARGS);
65
66 static const v_check man_valids[MAN_MAX - MAN_TH] = {
67 post_TH, /* TH */
68 post_SH, /* SH */
69 post_SH, /* SS */
70 post_TP, /* TP */
71 post_TP, /* TQ */
72 check_abort,/* LP */
73 check_par, /* PP */
74 check_abort,/* P */
75 post_IP, /* IP */
76 NULL, /* HP */
77 NULL, /* SM */
78 NULL, /* SB */
79 NULL, /* BI */
80 NULL, /* IB */
81 NULL, /* BR */
82 NULL, /* RB */
83 NULL, /* R */
84 NULL, /* B */
85 NULL, /* I */
86 NULL, /* IR */
87 NULL, /* RI */
88 NULL, /* RE */
89 check_part, /* RS */
90 NULL, /* DT */
91 post_UC, /* UC */
92 NULL, /* PD */
93 post_AT, /* AT */
94 post_in, /* in */
95 NULL, /* SY */
96 NULL, /* YS */
97 post_OP, /* OP */
98 post_EX, /* EX */
99 post_EE, /* EE */
100 post_UR, /* UR */
101 NULL, /* UE */
102 post_UR, /* MT */
103 NULL, /* ME */
104 };
105
106
107 /* Validate the subtree rooted at man->last. */
108 void
109 man_validate(struct roff_man *man)
110 {
111 struct roff_node *n;
112 const v_check *cp;
113
114 /*
115 * Translate obsolete macros such that later code
116 * does not need to look for them.
117 */
118
119 n = man->last;
120 switch (n->tok) {
121 case MAN_LP:
122 case MAN_P:
123 n->tok = MAN_PP;
124 break;
125 default:
126 break;
127 }
128
129 /*
130 * Iterate over all children, recursing into each one
131 * in turn, depth-first.
132 */
133
134 man->last = man->last->child;
135 while (man->last != NULL) {
136 man_validate(man);
137 if (man->last == n)
138 man->last = man->last->child;
139 else
140 man->last = man->last->next;
141 }
142
143 /* Finally validate the macro itself. */
144
145 man->last = n;
146 man->next = ROFF_NEXT_SIBLING;
147 switch (n->type) {
148 case ROFFT_TEXT:
149 check_text(man, n);
150 break;
151 case ROFFT_ROOT:
152 check_root(man, n);
153 break;
154 case ROFFT_COMMENT:
155 case ROFFT_EQN:
156 case ROFFT_TBL:
157 break;
158 default:
159 if (n->tok < ROFF_MAX) {
160 roff_validate(man);
161 break;
162 }
163 assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
164 cp = man_valids + (n->tok - MAN_TH);
165 if (*cp)
166 (*cp)(man, n);
167 if (man->last == n)
168 n->flags |= NODE_VALID;
169 break;
170 }
171 }
172
173 static void
174 check_root(CHKARGS)
175 {
176 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
177
178 if (n->last == NULL || n->last->type == ROFFT_COMMENT)
179 mandoc_msg(MANDOCERR_DOC_EMPTY, n->line, n->pos, NULL);
180 else
181 man->meta.hasbody = 1;
182
183 if (NULL == man->meta.title) {
184 mandoc_msg(MANDOCERR_TH_NOTITLE, n->line, n->pos, NULL);
185
186 /*
187 * If a title hasn't been set, do so now (by
188 * implication, date and section also aren't set).
189 */
190
191 man->meta.title = mandoc_strdup("");
192 man->meta.msec = mandoc_strdup("");
193 man->meta.date = mandoc_normdate(NULL, NULL);
194 }
195
196 if (man->meta.os_e &&
197 (man->meta.rcsids & (1 << man->meta.os_e)) == 0)
198 mandoc_msg(MANDOCERR_RCS_MISSING, 0, 0,
199 man->meta.os_e == MANDOC_OS_OPENBSD ?
200 "(OpenBSD)" : "(NetBSD)");
201 }
202
203 static void
204 check_abort(CHKARGS)
205 {
206 abort();
207 }
208
209 /*
210 * Skip leading whitespace, dashes, backslashes, and font escapes,
211 * then create a tag if the first following byte is a letter.
212 * Priority is high unless whitespace is present.
213 */
214 static void
215 check_tag(struct roff_node *n, struct roff_node *nt)
216 {
217 const char *cp, *arg;
218 int prio, sz;
219
220 if (nt == NULL || nt->type != ROFFT_TEXT)
221 return;
222
223 cp = nt->string;
224 prio = TAG_STRONG;
225 for (;;) {
226 switch (*cp) {
227 case ' ':
228 case '\t':
229 prio = TAG_WEAK;
230 /* FALLTHROUGH */
231 case '-':
232 cp++;
233 break;
234 case '\\':
235 cp++;
236 switch (mandoc_escape(&cp, &arg, &sz)) {
237 case ESCAPE_FONT:
238 case ESCAPE_FONTBOLD:
239 case ESCAPE_FONTITALIC:
240 case ESCAPE_FONTBI:
241 case ESCAPE_FONTROMAN:
242 case ESCAPE_FONTCW:
243 case ESCAPE_FONTPREV:
244 case ESCAPE_IGNORE:
245 break;
246 case ESCAPE_SPECIAL:
247 if (sz != 1)
248 return;
249 switch (*arg) {
250 case '-':
251 case 'e':
252 break;
253 default:
254 return;
255 }
256 break;
257 default:
258 return;
259 }
260 break;
261 default:
262 if (isalpha((unsigned char)*cp))
263 tag_put(cp, prio, n);
264 return;
265 }
266 }
267 }
268
269 static void
270 check_text(CHKARGS)
271 {
272 char *cp, *p;
273
274 if (n->flags & NODE_NOFILL)
275 return;
276
277 cp = n->string;
278 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
279 mandoc_msg(MANDOCERR_FI_TAB,
280 n->line, n->pos + (int)(p - cp), NULL);
281 }
282
283 static void
284 post_EE(CHKARGS)
285 {
286 if ((n->flags & NODE_NOFILL) == 0)
287 mandoc_msg(MANDOCERR_FI_SKIP, n->line, n->pos, "EE");
288 }
289
290 static void
291 post_EX(CHKARGS)
292 {
293 if (n->flags & NODE_NOFILL)
294 mandoc_msg(MANDOCERR_NF_SKIP, n->line, n->pos, "EX");
295 }
296
297 static void
298 post_OP(CHKARGS)
299 {
300
301 if (n->child == NULL)
302 mandoc_msg(MANDOCERR_OP_EMPTY, n->line, n->pos, "OP");
303 else if (n->child->next != NULL && n->child->next->next != NULL) {
304 n = n->child->next->next;
305 mandoc_msg(MANDOCERR_ARG_EXCESS,
306 n->line, n->pos, "OP ... %s", n->string);
307 }
308 }
309
310 static void
311 post_SH(CHKARGS)
312 {
313 struct roff_node *nc;
314 char *cp, *tag;
315
316 nc = n->child;
317 switch (n->type) {
318 case ROFFT_HEAD:
319 tag = NULL;
320 deroff(&tag, n);
321 if (tag != NULL) {
322 for (cp = tag; *cp != '\0'; cp++)
323 if (*cp == ' ')
324 *cp = '_';
325 if (nc != NULL && nc->type == ROFFT_TEXT &&
326 strcmp(nc->string, tag) == 0)
327 tag_put(NULL, TAG_WEAK, n);
328 else
329 tag_put(tag, TAG_FALLBACK, n);
330 free(tag);
331 }
332 return;
333 case ROFFT_BODY:
334 if (nc != NULL)
335 break;
336 return;
337 default:
338 return;
339 }
340
341 if (nc->tok == MAN_PP && nc->body->child != NULL) {
342 while (nc->body->last != NULL) {
343 man->next = ROFF_NEXT_CHILD;
344 roff_node_relink(man, nc->body->last);
345 man->last = n;
346 }
347 }
348
349 if (nc->tok == MAN_PP || nc->tok == ROFF_sp || nc->tok == ROFF_br) {
350 mandoc_msg(MANDOCERR_PAR_SKIP, nc->line, nc->pos,
351 "%s after %s", roff_name[nc->tok], roff_name[n->tok]);
352 roff_node_delete(man, nc);
353 }
354
355 /*
356 * Trailing PP is empty, so it is deleted by check_par().
357 * Trailing sp is significant.
358 */
359
360 if ((nc = n->last) != NULL && nc->tok == ROFF_br) {
361 mandoc_msg(MANDOCERR_PAR_SKIP,
362 nc->line, nc->pos, "%s at the end of %s",
363 roff_name[nc->tok], roff_name[n->tok]);
364 roff_node_delete(man, nc);
365 }
366 }
367
368 static void
369 post_UR(CHKARGS)
370 {
371 if (n->type == ROFFT_HEAD && n->child == NULL)
372 mandoc_msg(MANDOCERR_UR_NOHEAD, n->line, n->pos,
373 "%s", roff_name[n->tok]);
374 check_part(man, n);
375 }
376
377 static void
378 check_part(CHKARGS)
379 {
380
381 if (n->type == ROFFT_BODY && n->child == NULL)
382 mandoc_msg(MANDOCERR_BLK_EMPTY, n->line, n->pos,
383 "%s", roff_name[n->tok]);
384 }
385
386 static void
387 check_par(CHKARGS)
388 {
389
390 switch (n->type) {
391 case ROFFT_BLOCK:
392 if (n->body->child == NULL)
393 roff_node_delete(man, n);
394 break;
395 case ROFFT_BODY:
396 if (n->child != NULL &&
397 (n->child->tok == ROFF_sp || n->child->tok == ROFF_br)) {
398 mandoc_msg(MANDOCERR_PAR_SKIP,
399 n->child->line, n->child->pos,
400 "%s after %s", roff_name[n->child->tok],
401 roff_name[n->tok]);
402 roff_node_delete(man, n->child);
403 }
404 if (n->child == NULL)
405 mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos,
406 "%s empty", roff_name[n->tok]);
407 break;
408 case ROFFT_HEAD:
409 if (n->child != NULL)
410 mandoc_msg(MANDOCERR_ARG_SKIP,
411 n->line, n->pos, "%s %s%s",
412 roff_name[n->tok], n->child->string,
413 n->child->next != NULL ? " ..." : "");
414 break;
415 default:
416 break;
417 }
418 }
419
420 static void
421 post_IP(CHKARGS)
422 {
423 switch (n->type) {
424 case ROFFT_BLOCK:
425 if (n->head->child == NULL && n->body->child == NULL)
426 roff_node_delete(man, n);
427 break;
428 case ROFFT_HEAD:
429 check_tag(n, n->child);
430 break;
431 case ROFFT_BODY:
432 if (n->parent->head->child == NULL && n->child == NULL)
433 mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos,
434 "%s empty", roff_name[n->tok]);
435 break;
436 default:
437 break;
438 }
439 }
440
441 /*
442 * The first next-line element in the head is the tag.
443 * If that's a font macro, use its first child instead.
444 */
445 static void
446 post_TP(CHKARGS)
447 {
448 struct roff_node *nt;
449
450 if (n->type != ROFFT_HEAD || (nt = n->child) == NULL)
451 return;
452
453 while ((nt->flags & NODE_LINE) == 0)
454 if ((nt = nt->next) == NULL)
455 return;
456
457 switch (nt->tok) {
458 case MAN_B:
459 case MAN_BI:
460 case MAN_BR:
461 case MAN_I:
462 case MAN_IB:
463 case MAN_IR:
464 nt = nt->child;
465 break;
466 default:
467 break;
468 }
469 check_tag(n, nt);
470 }
471
472 static void
473 post_TH(CHKARGS)
474 {
475 struct roff_node *nb;
476 const char *p;
477
478 free(man->meta.title);
479 free(man->meta.vol);
480 free(man->meta.os);
481 free(man->meta.msec);
482 free(man->meta.date);
483
484 man->meta.title = man->meta.vol = man->meta.date =
485 man->meta.msec = man->meta.os = NULL;
486
487 nb = n;
488
489 /* ->TITLE<- MSEC DATE OS VOL */
490
491 n = n->child;
492 if (n != NULL && n->string != NULL) {
493 for (p = n->string; *p != '\0'; p++) {
494 /* Only warn about this once... */
495 if (isalpha((unsigned char)*p) &&
496 ! isupper((unsigned char)*p)) {
497 mandoc_msg(MANDOCERR_TITLE_CASE, n->line,
498 n->pos + (int)(p - n->string),
499 "TH %s", n->string);
500 break;
501 }
502 }
503 man->meta.title = mandoc_strdup(n->string);
504 } else {
505 man->meta.title = mandoc_strdup("");
506 mandoc_msg(MANDOCERR_TH_NOTITLE, nb->line, nb->pos, "TH");
507 }
508
509 /* TITLE ->MSEC<- DATE OS VOL */
510
511 if (n != NULL)
512 n = n->next;
513 if (n != NULL && n->string != NULL) {
514 man->meta.msec = mandoc_strdup(n->string);
515 if (man->filesec != '\0' &&
516 man->filesec != *n->string &&
517 *n->string >= '1' && *n->string <= '9')
518 mandoc_msg(MANDOCERR_MSEC_FILE, n->line, n->pos,
519 "*.%c vs TH ... %c", man->filesec, *n->string);
520 } else {
521 man->meta.msec = mandoc_strdup("");
522 mandoc_msg(MANDOCERR_MSEC_MISSING,
523 nb->line, nb->pos, "TH %s", man->meta.title);
524 }
525
526 /* TITLE MSEC ->DATE<- OS VOL */
527
528 if (n != NULL)
529 n = n->next;
530 if (man->quick && n != NULL)
531 man->meta.date = mandoc_strdup("");
532 else
533 man->meta.date = mandoc_normdate(n, nb);
534
535 /* TITLE MSEC DATE ->OS<- VOL */
536
537 if (n && (n = n->next))
538 man->meta.os = mandoc_strdup(n->string);
539 else if (man->os_s != NULL)
540 man->meta.os = mandoc_strdup(man->os_s);
541 if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) {
542 if (strstr(man->meta.os, "OpenBSD") != NULL)
543 man->meta.os_e = MANDOC_OS_OPENBSD;
544 else if (strstr(man->meta.os, "NetBSD") != NULL)
545 man->meta.os_e = MANDOC_OS_NETBSD;
546 }
547
548 /* TITLE MSEC DATE OS ->VOL<- */
549 /* If missing, use the default VOL name for MSEC. */
550
551 if (n && (n = n->next))
552 man->meta.vol = mandoc_strdup(n->string);
553 else if ('\0' != man->meta.msec[0] &&
554 (NULL != (p = mandoc_a2msec(man->meta.msec))))
555 man->meta.vol = mandoc_strdup(p);
556
557 if (n != NULL && (n = n->next) != NULL)
558 mandoc_msg(MANDOCERR_ARG_EXCESS,
559 n->line, n->pos, "TH ... %s", n->string);
560
561 /*
562 * Remove the `TH' node after we've processed it for our
563 * meta-data.
564 */
565 roff_node_delete(man, man->last);
566 }
567
568 static void
569 post_UC(CHKARGS)
570 {
571 static const char * const bsd_versions[] = {
572 "3rd Berkeley Distribution",
573 "4th Berkeley Distribution",
574 "4.2 Berkeley Distribution",
575 "4.3 Berkeley Distribution",
576 "4.4 Berkeley Distribution",
577 };
578
579 const char *p, *s;
580
581 n = n->child;
582
583 if (n == NULL || n->type != ROFFT_TEXT)
584 p = bsd_versions[0];
585 else {
586 s = n->string;
587 if (0 == strcmp(s, "3"))
588 p = bsd_versions[0];
589 else if (0 == strcmp(s, "4"))
590 p = bsd_versions[1];
591 else if (0 == strcmp(s, "5"))
592 p = bsd_versions[2];
593 else if (0 == strcmp(s, "6"))
594 p = bsd_versions[3];
595 else if (0 == strcmp(s, "7"))
596 p = bsd_versions[4];
597 else
598 p = bsd_versions[0];
599 }
600
601 free(man->meta.os);
602 man->meta.os = mandoc_strdup(p);
603 }
604
605 static void
606 post_AT(CHKARGS)
607 {
608 static const char * const unix_versions[] = {
609 "7th Edition",
610 "System III",
611 "System V",
612 "System V Release 2",
613 };
614
615 struct roff_node *nn;
616 const char *p, *s;
617
618 n = n->child;
619
620 if (n == NULL || n->type != ROFFT_TEXT)
621 p = unix_versions[0];
622 else {
623 s = n->string;
624 if (0 == strcmp(s, "3"))
625 p = unix_versions[0];
626 else if (0 == strcmp(s, "4"))
627 p = unix_versions[1];
628 else if (0 == strcmp(s, "5")) {
629 nn = n->next;
630 if (nn != NULL &&
631 nn->type == ROFFT_TEXT &&
632 nn->string[0] != '\0')
633 p = unix_versions[3];
634 else
635 p = unix_versions[2];
636 } else
637 p = unix_versions[0];
638 }
639
640 free(man->meta.os);
641 man->meta.os = mandoc_strdup(p);
642 }
643
644 static void
645 post_in(CHKARGS)
646 {
647 char *s;
648
649 if (n->parent->tok != MAN_TP ||
650 n->parent->type != ROFFT_HEAD ||
651 n->child == NULL ||
652 *n->child->string == '+' ||
653 *n->child->string == '-')
654 return;
655 mandoc_asprintf(&s, "+%s", n->child->string);
656 free(n->child->string);
657 n->child->string = s;
658 }