xref: /netbsd-src/external/bsd/mdocml/dist/man_validate.c (revision d90f6d4d226c33bf97117facccccfac1fa715850)
1544c191cSchristos /*	Id: man_validate.c,v 1.146 2018/12/31 10:04:39 schwarze Exp  */
24154958bSjoerg /*
348741257Sjoerg  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4c9bcef03Schristos  * Copyright (c) 2010, 2012-2018 Ingo Schwarze <schwarze@openbsd.org>
54154958bSjoerg  *
64154958bSjoerg  * Permission to use, copy, modify, and distribute this software for any
74154958bSjoerg  * purpose with or without fee is hereby granted, provided that the above
84154958bSjoerg  * copyright notice and this permission notice appear in all copies.
94154958bSjoerg  *
109ff1f2acSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
114154958bSjoerg  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
129ff1f2acSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
134154958bSjoerg  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
144154958bSjoerg  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
154154958bSjoerg  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
164154958bSjoerg  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
174154958bSjoerg  */
18d5e63c8dSjoerg #include "config.h"
19d5e63c8dSjoerg 
204154958bSjoerg #include <sys/types.h>
214154958bSjoerg 
224154958bSjoerg #include <assert.h>
234154958bSjoerg #include <ctype.h>
244154958bSjoerg #include <errno.h>
254154958bSjoerg #include <limits.h>
264154958bSjoerg #include <stdarg.h>
27544c191cSchristos #include <stdio.h>
284154958bSjoerg #include <stdlib.h>
297da9b934Sjoerg #include <string.h>
30c0d9444aSjoerg #include <time.h>
314154958bSjoerg 
32fec65c98Schristos #include "mandoc_aux.h"
339ff1f2acSchristos #include "mandoc.h"
349ff1f2acSchristos #include "roff.h"
359ff1f2acSchristos #include "man.h"
364154958bSjoerg #include "libmandoc.h"
379ff1f2acSchristos #include "roff_int.h"
389ff1f2acSchristos #include "libman.h"
394154958bSjoerg 
409ff1f2acSchristos #define	CHKARGS	  struct roff_man *man, struct roff_node *n
414154958bSjoerg 
42fec65c98Schristos typedef	void	(*v_check)(CHKARGS);
434154958bSjoerg 
44*d90f6d4dSchristos static	void	  check_abort(CHKARGS) __dead;
45fec65c98Schristos static	void	  check_par(CHKARGS);
46fec65c98Schristos static	void	  check_part(CHKARGS);
47fec65c98Schristos static	void	  check_root(CHKARGS);
48c5f73b34Sjoerg static	void	  check_text(CHKARGS);
494154958bSjoerg 
50fec65c98Schristos static	void	  post_AT(CHKARGS);
51544c191cSchristos static	void	  post_EE(CHKARGS);
52544c191cSchristos static	void	  post_EX(CHKARGS);
53fec65c98Schristos static	void	  post_IP(CHKARGS);
54fec65c98Schristos static	void	  post_OP(CHKARGS);
55544c191cSchristos static	void	  post_SH(CHKARGS);
56fec65c98Schristos static	void	  post_TH(CHKARGS);
57fec65c98Schristos static	void	  post_UC(CHKARGS);
58fec65c98Schristos static	void	  post_UR(CHKARGS);
59c9bcef03Schristos static	void	  post_in(CHKARGS);
60c0d9444aSjoerg 
61544c191cSchristos static	const v_check man_valids[MAN_MAX - MAN_TH] = {
62fec65c98Schristos 	post_TH,    /* TH */
63544c191cSchristos 	post_SH,    /* SH */
64544c191cSchristos 	post_SH,    /* SS */
65fec65c98Schristos 	NULL,       /* TP */
66544c191cSchristos 	NULL,       /* TQ */
67544c191cSchristos 	check_abort,/* LP */
68fec65c98Schristos 	check_par,  /* PP */
69544c191cSchristos 	check_abort,/* P */
70fec65c98Schristos 	post_IP,    /* IP */
71fec65c98Schristos 	NULL,       /* HP */
72fec65c98Schristos 	NULL,       /* SM */
73fec65c98Schristos 	NULL,       /* SB */
74fec65c98Schristos 	NULL,       /* BI */
75fec65c98Schristos 	NULL,       /* IB */
76fec65c98Schristos 	NULL,       /* BR */
77fec65c98Schristos 	NULL,       /* RB */
78fec65c98Schristos 	NULL,       /* R */
79fec65c98Schristos 	NULL,       /* B */
80fec65c98Schristos 	NULL,       /* I */
81fec65c98Schristos 	NULL,       /* IR */
82fec65c98Schristos 	NULL,       /* RI */
83fec65c98Schristos 	NULL,       /* RE */
84fec65c98Schristos 	check_part, /* RS */
85fec65c98Schristos 	NULL,       /* DT */
86fec65c98Schristos 	post_UC,    /* UC */
87fec65c98Schristos 	NULL,       /* PD */
88fec65c98Schristos 	post_AT,    /* AT */
89c9bcef03Schristos 	post_in,    /* in */
90544c191cSchristos 	NULL,       /* SY */
91544c191cSchristos 	NULL,       /* YS */
92fec65c98Schristos 	post_OP,    /* OP */
93544c191cSchristos 	post_EX,    /* EX */
94544c191cSchristos 	post_EE,    /* EE */
95fec65c98Schristos 	post_UR,    /* UR */
96fec65c98Schristos 	NULL,       /* UE */
97c9bcef03Schristos 	post_UR,    /* MT */
98c9bcef03Schristos 	NULL,       /* ME */
994154958bSjoerg };
1004154958bSjoerg 
1014154958bSjoerg 
102544c191cSchristos /* Validate the subtree rooted at man->last. */
103fec65c98Schristos void
man_validate(struct roff_man * man)104544c191cSchristos man_validate(struct roff_man *man)
1054154958bSjoerg {
1069ff1f2acSchristos 	struct roff_node *n;
107c9bcef03Schristos 	const v_check	 *cp;
1084154958bSjoerg 
109544c191cSchristos 	/*
110544c191cSchristos 	 * Translate obsolete macros such that later code
111544c191cSchristos 	 * does not need to look for them.
112544c191cSchristos 	 */
113544c191cSchristos 
114fec65c98Schristos 	n = man->last;
115544c191cSchristos 	switch (n->tok) {
116544c191cSchristos 	case MAN_LP:
117544c191cSchristos 	case MAN_P:
118544c191cSchristos 		n->tok = MAN_PP;
119544c191cSchristos 		break;
120544c191cSchristos 	default:
121544c191cSchristos 		break;
122544c191cSchristos 	}
123544c191cSchristos 
124544c191cSchristos 	/*
125544c191cSchristos 	 * Iterate over all children, recursing into each one
126544c191cSchristos 	 * in turn, depth-first.
127544c191cSchristos 	 */
128544c191cSchristos 
1299ff1f2acSchristos 	man->last = man->last->child;
1309ff1f2acSchristos 	while (man->last != NULL) {
131544c191cSchristos 		man_validate(man);
1329ff1f2acSchristos 		if (man->last == n)
1339ff1f2acSchristos 			man->last = man->last->child;
1349ff1f2acSchristos 		else
1359ff1f2acSchristos 			man->last = man->last->next;
1369ff1f2acSchristos 	}
1374154958bSjoerg 
138544c191cSchristos 	/* Finally validate the macro itself. */
139544c191cSchristos 
1409ff1f2acSchristos 	man->last = n;
1419ff1f2acSchristos 	man->next = ROFF_NEXT_SIBLING;
142fec65c98Schristos 	switch (n->type) {
1439ff1f2acSchristos 	case ROFFT_TEXT:
144fec65c98Schristos 		check_text(man, n);
145fec65c98Schristos 		break;
1469ff1f2acSchristos 	case ROFFT_ROOT:
147fec65c98Schristos 		check_root(man, n);
148fec65c98Schristos 		break;
149c9bcef03Schristos 	case ROFFT_COMMENT:
1509ff1f2acSchristos 	case ROFFT_EQN:
1519ff1f2acSchristos 	case ROFFT_TBL:
152fec65c98Schristos 		break;
1534154958bSjoerg 	default:
154c9bcef03Schristos 		if (n->tok < ROFF_MAX) {
155c9bcef03Schristos 			roff_validate(man);
156c9bcef03Schristos 			break;
157c9bcef03Schristos 		}
158c9bcef03Schristos 		assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
159544c191cSchristos 		cp = man_valids + (n->tok - MAN_TH);
160fec65c98Schristos 		if (*cp)
161fec65c98Schristos 			(*cp)(man, n);
1629ff1f2acSchristos 		if (man->last == n)
163544c191cSchristos 			n->flags |= NODE_VALID;
1644154958bSjoerg 		break;
1654154958bSjoerg 	}
1664154958bSjoerg }
1674154958bSjoerg 
168fec65c98Schristos static void
check_root(CHKARGS)1694154958bSjoerg check_root(CHKARGS)
1704154958bSjoerg {
171fec65c98Schristos 	assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
1724154958bSjoerg 
173c9bcef03Schristos 	if (n->last == NULL || n->last->type == ROFFT_COMMENT)
174544c191cSchristos 		mandoc_msg(MANDOCERR_DOC_EMPTY, n->line, n->pos, NULL);
175fec65c98Schristos 	else
176fec65c98Schristos 		man->meta.hasbody = 1;
1774154958bSjoerg 
178fec65c98Schristos 	if (NULL == man->meta.title) {
179544c191cSchristos 		mandoc_msg(MANDOCERR_TH_NOTITLE, n->line, n->pos, NULL);
180c0d9444aSjoerg 
1817bcc2a5fSjoerg 		/*
1827bcc2a5fSjoerg 		 * If a title hasn't been set, do so now (by
1837bcc2a5fSjoerg 		 * implication, date and section also aren't set).
1847bcc2a5fSjoerg 		 */
185c0d9444aSjoerg 
186fec65c98Schristos 		man->meta.title = mandoc_strdup("");
187fec65c98Schristos 		man->meta.msec = mandoc_strdup("");
188fec65c98Schristos 		man->meta.date = man->quick ? mandoc_strdup("") :
189c9bcef03Schristos 		    mandoc_normdate(man, NULL, n->line, n->pos);
190c0d9444aSjoerg 	}
191c9bcef03Schristos 
192c9bcef03Schristos 	if (man->meta.os_e &&
193c9bcef03Schristos 	    (man->meta.rcsids & (1 << man->meta.os_e)) == 0)
194544c191cSchristos 		mandoc_msg(MANDOCERR_RCS_MISSING, 0, 0,
195c9bcef03Schristos 		    man->meta.os_e == MANDOC_OS_OPENBSD ?
196c9bcef03Schristos 		    "(OpenBSD)" : "(NetBSD)");
19731e1f4e3Sjoerg }
19831e1f4e3Sjoerg 
199c5f73b34Sjoerg static void
check_abort(CHKARGS)200544c191cSchristos check_abort(CHKARGS)
201544c191cSchristos {
202544c191cSchristos 	abort();
203544c191cSchristos }
204544c191cSchristos 
205544c191cSchristos static void
check_text(CHKARGS)2064154958bSjoerg check_text(CHKARGS)
2074154958bSjoerg {
208c5f73b34Sjoerg 	char		*cp, *p;
2094154958bSjoerg 
210544c191cSchristos 	if (n->flags & NODE_NOFILL)
211cf816816Sjoerg 		return;
212cf816816Sjoerg 
213cf816816Sjoerg 	cp = n->string;
214cf816816Sjoerg 	for (p = cp; NULL != (p = strchr(p, '\t')); p++)
215544c191cSchristos 		mandoc_msg(MANDOCERR_FI_TAB,
216544c191cSchristos 		    n->line, n->pos + (int)(p - cp), NULL);
217544c191cSchristos }
218544c191cSchristos 
219544c191cSchristos static void
post_EE(CHKARGS)220544c191cSchristos post_EE(CHKARGS)
221544c191cSchristos {
222544c191cSchristos 	if ((n->flags & NODE_NOFILL) == 0)
223544c191cSchristos 		mandoc_msg(MANDOCERR_FI_SKIP, n->line, n->pos, "EE");
224544c191cSchristos }
225544c191cSchristos 
226544c191cSchristos static void
post_EX(CHKARGS)227544c191cSchristos post_EX(CHKARGS)
228544c191cSchristos {
229544c191cSchristos 	if (n->flags & NODE_NOFILL)
230544c191cSchristos 		mandoc_msg(MANDOCERR_NF_SKIP, n->line, n->pos, "EX");
2317da9b934Sjoerg }
2324154958bSjoerg 
233fec65c98Schristos static void
post_OP(CHKARGS)234fec65c98Schristos post_OP(CHKARGS)
23570f041f9Sjoerg {
23670f041f9Sjoerg 
2379ff1f2acSchristos 	if (n->child == NULL)
238544c191cSchristos 		mandoc_msg(MANDOCERR_OP_EMPTY, n->line, n->pos, "OP");
2399ff1f2acSchristos 	else if (n->child->next != NULL && n->child->next->next != NULL) {
240fec65c98Schristos 		n = n->child->next->next;
241544c191cSchristos 		mandoc_msg(MANDOCERR_ARG_EXCESS,
242fec65c98Schristos 		    n->line, n->pos, "OP ... %s", n->string);
243fec65c98Schristos 	}
24470f041f9Sjoerg }
24570f041f9Sjoerg 
246fec65c98Schristos static void
post_SH(CHKARGS)247544c191cSchristos post_SH(CHKARGS)
248544c191cSchristos {
249544c191cSchristos 	struct roff_node	*nc;
250544c191cSchristos 
251544c191cSchristos 	if (n->type != ROFFT_BODY || (nc = n->child) == NULL)
252544c191cSchristos 		return;
253544c191cSchristos 
254544c191cSchristos 	if (nc->tok == MAN_PP && nc->body->child != NULL) {
255544c191cSchristos 		while (nc->body->last != NULL) {
256544c191cSchristos 			man->next = ROFF_NEXT_CHILD;
257544c191cSchristos 			roff_node_relink(man, nc->body->last);
258544c191cSchristos 			man->last = n;
259544c191cSchristos 		}
260544c191cSchristos 	}
261544c191cSchristos 
262544c191cSchristos 	if (nc->tok == MAN_PP || nc->tok == ROFF_sp || nc->tok == ROFF_br) {
263544c191cSchristos 		mandoc_msg(MANDOCERR_PAR_SKIP, nc->line, nc->pos,
264544c191cSchristos 		    "%s after %s", roff_name[nc->tok], roff_name[n->tok]);
265544c191cSchristos 		roff_node_delete(man, nc);
266544c191cSchristos 	}
267544c191cSchristos 
268544c191cSchristos 	/*
269544c191cSchristos 	 * Trailing PP is empty, so it is deleted by check_par().
270544c191cSchristos 	 * Trailing sp is significant.
271544c191cSchristos 	 */
272544c191cSchristos 
273544c191cSchristos 	if ((nc = n->last) != NULL && nc->tok == ROFF_br) {
274544c191cSchristos 		mandoc_msg(MANDOCERR_PAR_SKIP,
275544c191cSchristos 		    nc->line, nc->pos, "%s at the end of %s",
276544c191cSchristos 		    roff_name[nc->tok], roff_name[n->tok]);
277544c191cSchristos 		roff_node_delete(man, nc);
278544c191cSchristos 	}
279544c191cSchristos }
280544c191cSchristos 
281544c191cSchristos static void
post_UR(CHKARGS)282fec65c98Schristos post_UR(CHKARGS)
283fec65c98Schristos {
2849ff1f2acSchristos 	if (n->type == ROFFT_HEAD && n->child == NULL)
285544c191cSchristos 		mandoc_msg(MANDOCERR_UR_NOHEAD, n->line, n->pos,
286544c191cSchristos 		    "%s", roff_name[n->tok]);
287fec65c98Schristos 	check_part(man, n);
288fec65c98Schristos }
289fec65c98Schristos 
290fec65c98Schristos static void
check_part(CHKARGS)2914154958bSjoerg check_part(CHKARGS)
2924154958bSjoerg {
2934154958bSjoerg 
2949ff1f2acSchristos 	if (n->type == ROFFT_BODY && n->child == NULL)
295544c191cSchristos 		mandoc_msg(MANDOCERR_BLK_EMPTY, n->line, n->pos,
296544c191cSchristos 		    "%s", roff_name[n->tok]);
2974154958bSjoerg }
2984154958bSjoerg 
299fec65c98Schristos static void
check_par(CHKARGS)3004154958bSjoerg check_par(CHKARGS)
3014154958bSjoerg {
3024154958bSjoerg 
30348741257Sjoerg 	switch (n->type) {
3049ff1f2acSchristos 	case ROFFT_BLOCK:
3059ff1f2acSchristos 		if (n->body->child == NULL)
3069ff1f2acSchristos 			roff_node_delete(man, n);
3074154958bSjoerg 		break;
3089ff1f2acSchristos 	case ROFFT_BODY:
309544c191cSchristos 		if (n->child != NULL &&
310544c191cSchristos 		    (n->child->tok == ROFF_sp || n->child->tok == ROFF_br)) {
311544c191cSchristos 			mandoc_msg(MANDOCERR_PAR_SKIP,
312544c191cSchristos 			    n->child->line, n->child->pos,
313544c191cSchristos 			    "%s after %s", roff_name[n->child->tok],
314544c191cSchristos 			    roff_name[n->tok]);
315544c191cSchristos 			roff_node_delete(man, n->child);
316544c191cSchristos 		}
3179ff1f2acSchristos 		if (n->child == NULL)
318544c191cSchristos 			mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos,
319c9bcef03Schristos 			    "%s empty", roff_name[n->tok]);
3204154958bSjoerg 		break;
3219ff1f2acSchristos 	case ROFFT_HEAD:
3229ff1f2acSchristos 		if (n->child != NULL)
323544c191cSchristos 			mandoc_msg(MANDOCERR_ARG_SKIP,
324544c191cSchristos 			    n->line, n->pos, "%s %s%s",
325c9bcef03Schristos 			    roff_name[n->tok], n->child->string,
3269ff1f2acSchristos 			    n->child->next != NULL ? " ..." : "");
3274154958bSjoerg 		break;
328c0d9444aSjoerg 	default:
329c0d9444aSjoerg 		break;
3304154958bSjoerg 	}
3314154958bSjoerg }
3324154958bSjoerg 
333fec65c98Schristos static void
post_IP(CHKARGS)33470f041f9Sjoerg post_IP(CHKARGS)
33570f041f9Sjoerg {
33670f041f9Sjoerg 
33770f041f9Sjoerg 	switch (n->type) {
3389ff1f2acSchristos 	case ROFFT_BLOCK:
3399ff1f2acSchristos 		if (n->head->child == NULL && n->body->child == NULL)
3409ff1f2acSchristos 			roff_node_delete(man, n);
34170f041f9Sjoerg 		break;
3429ff1f2acSchristos 	case ROFFT_BODY:
3439ff1f2acSchristos 		if (n->parent->head->child == NULL && n->child == NULL)
344544c191cSchristos 			mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos,
345c9bcef03Schristos 			    "%s empty", roff_name[n->tok]);
34670f041f9Sjoerg 		break;
34770f041f9Sjoerg 	default:
34870f041f9Sjoerg 		break;
34970f041f9Sjoerg 	}
35070f041f9Sjoerg }
3514154958bSjoerg 
352fec65c98Schristos static void
post_TH(CHKARGS)353c0d9444aSjoerg post_TH(CHKARGS)
354c0d9444aSjoerg {
3559ff1f2acSchristos 	struct roff_node *nb;
35648741257Sjoerg 	const char	*p;
357c0d9444aSjoerg 
35870f041f9Sjoerg 	free(man->meta.title);
35970f041f9Sjoerg 	free(man->meta.vol);
3609ff1f2acSchristos 	free(man->meta.os);
36170f041f9Sjoerg 	free(man->meta.msec);
36270f041f9Sjoerg 	free(man->meta.date);
363c0d9444aSjoerg 
36470f041f9Sjoerg 	man->meta.title = man->meta.vol = man->meta.date =
3659ff1f2acSchristos 	    man->meta.msec = man->meta.os = NULL;
366c0d9444aSjoerg 
367fec65c98Schristos 	nb = n;
368fec65c98Schristos 
3699ff1f2acSchristos 	/* ->TITLE<- MSEC DATE OS VOL */
370c0d9444aSjoerg 
371c0d9444aSjoerg 	n = n->child;
37248741257Sjoerg 	if (n && n->string) {
37348741257Sjoerg 		for (p = n->string; '\0' != *p; p++) {
37448741257Sjoerg 			/* Only warn about this once... */
375c5f73b34Sjoerg 			if (isalpha((unsigned char)*p) &&
376c5f73b34Sjoerg 			    ! isupper((unsigned char)*p)) {
377544c191cSchristos 				mandoc_msg(MANDOCERR_TITLE_CASE, n->line,
378544c191cSchristos 				    n->pos + (int)(p - n->string),
379fec65c98Schristos 				    "TH %s", n->string);
38048741257Sjoerg 				break;
38148741257Sjoerg 			}
38248741257Sjoerg 		}
38370f041f9Sjoerg 		man->meta.title = mandoc_strdup(n->string);
384fec65c98Schristos 	} else {
38570f041f9Sjoerg 		man->meta.title = mandoc_strdup("");
386544c191cSchristos 		mandoc_msg(MANDOCERR_TH_NOTITLE, nb->line, nb->pos, "TH");
387fec65c98Schristos 	}
388c0d9444aSjoerg 
3899ff1f2acSchristos 	/* TITLE ->MSEC<- DATE OS VOL */
390c0d9444aSjoerg 
39148741257Sjoerg 	if (n)
392c0d9444aSjoerg 		n = n->next;
39348741257Sjoerg 	if (n && n->string)
39470f041f9Sjoerg 		man->meta.msec = mandoc_strdup(n->string);
395fec65c98Schristos 	else {
39670f041f9Sjoerg 		man->meta.msec = mandoc_strdup("");
397544c191cSchristos 		mandoc_msg(MANDOCERR_MSEC_MISSING,
398fec65c98Schristos 		    nb->line, nb->pos, "TH %s", man->meta.title);
399fec65c98Schristos 	}
400c0d9444aSjoerg 
4019ff1f2acSchristos 	/* TITLE MSEC ->DATE<- OS VOL */
402c0d9444aSjoerg 
40348741257Sjoerg 	if (n)
404c0d9444aSjoerg 		n = n->next;
405cf816816Sjoerg 	if (n && n->string && '\0' != n->string[0]) {
406fec65c98Schristos 		man->meta.date = man->quick ?
407fec65c98Schristos 		    mandoc_strdup(n->string) :
408c9bcef03Schristos 		    mandoc_normdate(man, n->string, n->line, n->pos);
409fec65c98Schristos 	} else {
41070f041f9Sjoerg 		man->meta.date = mandoc_strdup("");
411544c191cSchristos 		mandoc_msg(MANDOCERR_DATE_MISSING,
412fec65c98Schristos 		    n ? n->line : nb->line,
413fec65c98Schristos 		    n ? n->pos : nb->pos, "TH");
414fec65c98Schristos 	}
415c0d9444aSjoerg 
4169ff1f2acSchristos 	/* TITLE MSEC DATE ->OS<- VOL */
417c0d9444aSjoerg 
418c0d9444aSjoerg 	if (n && (n = n->next))
4199ff1f2acSchristos 		man->meta.os = mandoc_strdup(n->string);
420c9bcef03Schristos 	else if (man->os_s != NULL)
421c9bcef03Schristos 		man->meta.os = mandoc_strdup(man->os_s);
422c9bcef03Schristos 	if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) {
423c9bcef03Schristos 		if (strstr(man->meta.os, "OpenBSD") != NULL)
424c9bcef03Schristos 			man->meta.os_e = MANDOC_OS_OPENBSD;
425c9bcef03Schristos 		else if (strstr(man->meta.os, "NetBSD") != NULL)
426c9bcef03Schristos 			man->meta.os_e = MANDOC_OS_NETBSD;
427c9bcef03Schristos 	}
428c0d9444aSjoerg 
4299ff1f2acSchristos 	/* TITLE MSEC DATE OS ->VOL<- */
430cf816816Sjoerg 	/* If missing, use the default VOL name for MSEC. */
431c0d9444aSjoerg 
432c0d9444aSjoerg 	if (n && (n = n->next))
43370f041f9Sjoerg 		man->meta.vol = mandoc_strdup(n->string);
43470f041f9Sjoerg 	else if ('\0' != man->meta.msec[0] &&
43570f041f9Sjoerg 	    (NULL != (p = mandoc_a2msec(man->meta.msec))))
43670f041f9Sjoerg 		man->meta.vol = mandoc_strdup(p);
437c0d9444aSjoerg 
438fec65c98Schristos 	if (n != NULL && (n = n->next) != NULL)
439544c191cSchristos 		mandoc_msg(MANDOCERR_ARG_EXCESS,
440fec65c98Schristos 		    n->line, n->pos, "TH ... %s", n->string);
441fec65c98Schristos 
442c0d9444aSjoerg 	/*
443c0d9444aSjoerg 	 * Remove the `TH' node after we've processed it for our
444c0d9444aSjoerg 	 * meta-data.
445c0d9444aSjoerg 	 */
4469ff1f2acSchristos 	roff_node_delete(man, man->last);
447c0d9444aSjoerg }
448c0d9444aSjoerg 
449fec65c98Schristos static void
post_UC(CHKARGS)450c0d9444aSjoerg post_UC(CHKARGS)
451c0d9444aSjoerg {
452c0d9444aSjoerg 	static const char * const bsd_versions[] = {
453c0d9444aSjoerg 	    "3rd Berkeley Distribution",
454c0d9444aSjoerg 	    "4th Berkeley Distribution",
455c0d9444aSjoerg 	    "4.2 Berkeley Distribution",
456c0d9444aSjoerg 	    "4.3 Berkeley Distribution",
457c0d9444aSjoerg 	    "4.4 Berkeley Distribution",
458c0d9444aSjoerg 	};
459c0d9444aSjoerg 
460c0d9444aSjoerg 	const char	*p, *s;
461c0d9444aSjoerg 
462c0d9444aSjoerg 	n = n->child;
463c0d9444aSjoerg 
4649ff1f2acSchristos 	if (n == NULL || n->type != ROFFT_TEXT)
465c0d9444aSjoerg 		p = bsd_versions[0];
466c0d9444aSjoerg 	else {
467c0d9444aSjoerg 		s = n->string;
468c0d9444aSjoerg 		if (0 == strcmp(s, "3"))
469c0d9444aSjoerg 			p = bsd_versions[0];
470c0d9444aSjoerg 		else if (0 == strcmp(s, "4"))
471c0d9444aSjoerg 			p = bsd_versions[1];
472c0d9444aSjoerg 		else if (0 == strcmp(s, "5"))
473c0d9444aSjoerg 			p = bsd_versions[2];
474c0d9444aSjoerg 		else if (0 == strcmp(s, "6"))
475c0d9444aSjoerg 			p = bsd_versions[3];
476c0d9444aSjoerg 		else if (0 == strcmp(s, "7"))
477c0d9444aSjoerg 			p = bsd_versions[4];
478c0d9444aSjoerg 		else
479c0d9444aSjoerg 			p = bsd_versions[0];
480c0d9444aSjoerg 	}
481c0d9444aSjoerg 
4829ff1f2acSchristos 	free(man->meta.os);
4839ff1f2acSchristos 	man->meta.os = mandoc_strdup(p);
484c0d9444aSjoerg }
485c0d9444aSjoerg 
486fec65c98Schristos static void
post_AT(CHKARGS)487c0d9444aSjoerg post_AT(CHKARGS)
488c0d9444aSjoerg {
489c0d9444aSjoerg 	static const char * const unix_versions[] = {
490c0d9444aSjoerg 	    "7th Edition",
491c0d9444aSjoerg 	    "System III",
492c0d9444aSjoerg 	    "System V",
493c0d9444aSjoerg 	    "System V Release 2",
494c0d9444aSjoerg 	};
495c0d9444aSjoerg 
4969ff1f2acSchristos 	struct roff_node *nn;
497c0d9444aSjoerg 	const char	*p, *s;
498c0d9444aSjoerg 
499c0d9444aSjoerg 	n = n->child;
500c0d9444aSjoerg 
5019ff1f2acSchristos 	if (n == NULL || n->type != ROFFT_TEXT)
502c0d9444aSjoerg 		p = unix_versions[0];
503c0d9444aSjoerg 	else {
504c0d9444aSjoerg 		s = n->string;
505c0d9444aSjoerg 		if (0 == strcmp(s, "3"))
506c0d9444aSjoerg 			p = unix_versions[0];
507c0d9444aSjoerg 		else if (0 == strcmp(s, "4"))
508c0d9444aSjoerg 			p = unix_versions[1];
509c0d9444aSjoerg 		else if (0 == strcmp(s, "5")) {
510c0d9444aSjoerg 			nn = n->next;
5119ff1f2acSchristos 			if (nn != NULL &&
5129ff1f2acSchristos 			    nn->type == ROFFT_TEXT &&
5139ff1f2acSchristos 			    nn->string[0] != '\0')
514c0d9444aSjoerg 				p = unix_versions[3];
515c0d9444aSjoerg 			else
516c0d9444aSjoerg 				p = unix_versions[2];
517c0d9444aSjoerg 		} else
518c0d9444aSjoerg 			p = unix_versions[0];
519c0d9444aSjoerg 	}
520c0d9444aSjoerg 
5219ff1f2acSchristos 	free(man->meta.os);
5229ff1f2acSchristos 	man->meta.os = mandoc_strdup(p);
523c0d9444aSjoerg }
524c5f73b34Sjoerg 
525fec65c98Schristos static void
post_in(CHKARGS)526c9bcef03Schristos post_in(CHKARGS)
527c9bcef03Schristos {
528c9bcef03Schristos 	char	*s;
529c9bcef03Schristos 
530c9bcef03Schristos 	if (n->parent->tok != MAN_TP ||
531c9bcef03Schristos 	    n->parent->type != ROFFT_HEAD ||
532c9bcef03Schristos 	    n->child == NULL ||
533c9bcef03Schristos 	    *n->child->string == '+' ||
534c9bcef03Schristos 	    *n->child->string == '-')
535c9bcef03Schristos 		return;
536c9bcef03Schristos 	mandoc_asprintf(&s, "+%s", n->child->string);
537c9bcef03Schristos 	free(n->child->string);
538c9bcef03Schristos 	n->child->string = s;
539c9bcef03Schristos }
540