xref: /openbsd/usr.bin/mandoc/man_validate.c (revision 73471bf0)
1 /* $OpenBSD: man_validate.c,v 1.126 2021/08/10 12:36:42 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 <sys/types.h>
21 
22 #include <assert.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31 
32 #include "mandoc_aux.h"
33 #include "mandoc.h"
34 #include "roff.h"
35 #include "man.h"
36 #include "libmandoc.h"
37 #include "roff_int.h"
38 #include "libman.h"
39 #include "tag.h"
40 
41 #define	CHKARGS	  struct roff_man *man, struct roff_node *n
42 
43 typedef	void	(*v_check)(CHKARGS);
44 
45 static	void	  check_abort(CHKARGS) __attribute__((__noreturn__));
46 static	void	  check_par(CHKARGS);
47 static	void	  check_part(CHKARGS);
48 static	void	  check_root(CHKARGS);
49 static	void	  check_tag(struct roff_node *, struct roff_node *);
50 static	void	  check_text(CHKARGS);
51 
52 static	void	  post_AT(CHKARGS);
53 static	void	  post_EE(CHKARGS);
54 static	void	  post_EX(CHKARGS);
55 static	void	  post_IP(CHKARGS);
56 static	void	  post_OP(CHKARGS);
57 static	void	  post_SH(CHKARGS);
58 static	void	  post_TH(CHKARGS);
59 static	void	  post_TP(CHKARGS);
60 static	void	  post_UC(CHKARGS);
61 static	void	  post_UR(CHKARGS);
62 static	void	  post_in(CHKARGS);
63 
64 static	const v_check man_valids[MAN_MAX - MAN_TH] = {
65 	post_TH,    /* TH */
66 	post_SH,    /* SH */
67 	post_SH,    /* SS */
68 	post_TP,    /* TP */
69 	post_TP,    /* TQ */
70 	check_abort,/* LP */
71 	check_par,  /* PP */
72 	check_abort,/* P */
73 	post_IP,    /* IP */
74 	NULL,       /* HP */
75 	NULL,       /* SM */
76 	NULL,       /* SB */
77 	NULL,       /* BI */
78 	NULL,       /* IB */
79 	NULL,       /* BR */
80 	NULL,       /* RB */
81 	NULL,       /* R */
82 	NULL,       /* B */
83 	NULL,       /* I */
84 	NULL,       /* IR */
85 	NULL,       /* RI */
86 	NULL,       /* RE */
87 	check_part, /* RS */
88 	NULL,       /* DT */
89 	post_UC,    /* UC */
90 	NULL,       /* PD */
91 	post_AT,    /* AT */
92 	post_in,    /* in */
93 	NULL,       /* SY */
94 	NULL,       /* YS */
95 	post_OP,    /* OP */
96 	post_EX,    /* EX */
97 	post_EE,    /* EE */
98 	post_UR,    /* UR */
99 	NULL,       /* UE */
100 	post_UR,    /* MT */
101 	NULL,       /* ME */
102 };
103 
104 
105 /* Validate the subtree rooted at man->last. */
106 void
107 man_validate(struct roff_man *man)
108 {
109 	struct roff_node *n;
110 	const v_check	 *cp;
111 
112 	/*
113 	 * Translate obsolete macros such that later code
114 	 * does not need to look for them.
115 	 */
116 
117 	n = man->last;
118 	switch (n->tok) {
119 	case MAN_LP:
120 	case MAN_P:
121 		n->tok = MAN_PP;
122 		break;
123 	default:
124 		break;
125 	}
126 
127 	/*
128 	 * Iterate over all children, recursing into each one
129 	 * in turn, depth-first.
130 	 */
131 
132 	man->last = man->last->child;
133 	while (man->last != NULL) {
134 		man_validate(man);
135 		if (man->last == n)
136 			man->last = man->last->child;
137 		else
138 			man->last = man->last->next;
139 	}
140 
141 	/* Finally validate the macro itself. */
142 
143 	man->last = n;
144 	man->next = ROFF_NEXT_SIBLING;
145 	switch (n->type) {
146 	case ROFFT_TEXT:
147 		check_text(man, n);
148 		break;
149 	case ROFFT_ROOT:
150 		check_root(man, n);
151 		break;
152 	case ROFFT_COMMENT:
153 	case ROFFT_EQN:
154 	case ROFFT_TBL:
155 		break;
156 	default:
157 		if (n->tok < ROFF_MAX) {
158 			roff_validate(man);
159 			break;
160 		}
161 		assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
162 		cp = man_valids + (n->tok - MAN_TH);
163 		if (*cp)
164 			(*cp)(man, n);
165 		if (man->last == n)
166 			n->flags |= NODE_VALID;
167 		break;
168 	}
169 }
170 
171 static void
172 check_root(CHKARGS)
173 {
174 	assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
175 
176 	if (n->last == NULL || n->last->type == ROFFT_COMMENT)
177 		mandoc_msg(MANDOCERR_DOC_EMPTY, n->line, n->pos, NULL);
178 	else
179 		man->meta.hasbody = 1;
180 
181 	if (NULL == man->meta.title) {
182 		mandoc_msg(MANDOCERR_TH_NOTITLE, n->line, n->pos, NULL);
183 
184 		/*
185 		 * If a title hasn't been set, do so now (by
186 		 * implication, date and section also aren't set).
187 		 */
188 
189 		man->meta.title = mandoc_strdup("");
190 		man->meta.msec = mandoc_strdup("");
191 		man->meta.date = mandoc_normdate(NULL, NULL);
192 	}
193 
194 	if (man->meta.os_e &&
195 	    (man->meta.rcsids & (1 << man->meta.os_e)) == 0)
196 		mandoc_msg(MANDOCERR_RCS_MISSING, 0, 0,
197 		    man->meta.os_e == MANDOC_OS_OPENBSD ?
198 		    "(OpenBSD)" : "(NetBSD)");
199 }
200 
201 static void
202 check_abort(CHKARGS)
203 {
204 	abort();
205 }
206 
207 /*
208  * Skip leading whitespace, dashes, backslashes, and font escapes,
209  * then create a tag if the first following byte is a letter.
210  * Priority is high unless whitespace is present.
211  */
212 static void
213 check_tag(struct roff_node *n, struct roff_node *nt)
214 {
215 	const char	*cp, *arg;
216 	int		 prio, sz;
217 
218 	if (nt == NULL || nt->type != ROFFT_TEXT)
219 		return;
220 
221 	cp = nt->string;
222 	prio = TAG_STRONG;
223 	for (;;) {
224 		switch (*cp) {
225 		case ' ':
226 		case '\t':
227 			prio = TAG_WEAK;
228 			/* FALLTHROUGH */
229 		case '-':
230 			cp++;
231 			break;
232 		case '\\':
233 			cp++;
234 			switch (mandoc_escape(&cp, &arg, &sz)) {
235 			case ESCAPE_FONT:
236 			case ESCAPE_FONTBOLD:
237 			case ESCAPE_FONTITALIC:
238 			case ESCAPE_FONTBI:
239 			case ESCAPE_FONTROMAN:
240 			case ESCAPE_FONTCR:
241 			case ESCAPE_FONTCB:
242 			case ESCAPE_FONTCI:
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_STRONG, 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 }
659