1*4d131170SRobert Mustacchi /* $Id: man_validate.c,v 1.156 2021/08/10 12:55:03 schwarze Exp $ */
295c635efSGarrett D'Amore /*
3*4d131170SRobert Mustacchi * Copyright (c) 2010, 2012-2020 Ingo Schwarze <schwarze@openbsd.org>
495c635efSGarrett D'Amore * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
595c635efSGarrett D'Amore *
695c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
795c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
895c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies.
995c635efSGarrett D'Amore *
10371584c2SYuri Pankov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1195c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12371584c2SYuri Pankov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1395c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1495c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1595c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1695c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*4d131170SRobert Mustacchi *
18*4d131170SRobert Mustacchi * Validation module for man(7) syntax trees used by mandoc(1).
1995c635efSGarrett D'Amore */
2095c635efSGarrett D'Amore #include "config.h"
2195c635efSGarrett D'Amore
2295c635efSGarrett D'Amore #include <sys/types.h>
2395c635efSGarrett D'Amore
2495c635efSGarrett D'Amore #include <assert.h>
2595c635efSGarrett D'Amore #include <ctype.h>
2695c635efSGarrett D'Amore #include <errno.h>
2795c635efSGarrett D'Amore #include <limits.h>
2895c635efSGarrett D'Amore #include <stdarg.h>
29cec8643bSMichal Nowak #include <stdio.h>
3095c635efSGarrett D'Amore #include <stdlib.h>
3195c635efSGarrett D'Amore #include <string.h>
3295c635efSGarrett D'Amore #include <time.h>
3395c635efSGarrett D'Amore
34260e9a87SYuri Pankov #include "mandoc_aux.h"
35371584c2SYuri Pankov #include "mandoc.h"
36371584c2SYuri Pankov #include "roff.h"
37371584c2SYuri Pankov #include "man.h"
3895c635efSGarrett D'Amore #include "libmandoc.h"
39371584c2SYuri Pankov #include "roff_int.h"
40371584c2SYuri Pankov #include "libman.h"
41*4d131170SRobert Mustacchi #include "tag.h"
4295c635efSGarrett D'Amore
43371584c2SYuri Pankov #define CHKARGS struct roff_man *man, struct roff_node *n
4495c635efSGarrett D'Amore
45260e9a87SYuri Pankov typedef void (*v_check)(CHKARGS);
4695c635efSGarrett D'Amore
47*4d131170SRobert Mustacchi static void check_abort(CHKARGS) __attribute__((__noreturn__));
48260e9a87SYuri Pankov static void check_par(CHKARGS);
49260e9a87SYuri Pankov static void check_part(CHKARGS);
50260e9a87SYuri Pankov static void check_root(CHKARGS);
51*4d131170SRobert Mustacchi static void check_tag(struct roff_node *, struct roff_node *);
5295c635efSGarrett D'Amore static void check_text(CHKARGS);
5395c635efSGarrett D'Amore
54260e9a87SYuri Pankov static void post_AT(CHKARGS);
55cec8643bSMichal Nowak static void post_EE(CHKARGS);
56cec8643bSMichal Nowak static void post_EX(CHKARGS);
57260e9a87SYuri Pankov static void post_IP(CHKARGS);
58260e9a87SYuri Pankov static void post_OP(CHKARGS);
59cec8643bSMichal Nowak static void post_SH(CHKARGS);
60260e9a87SYuri Pankov static void post_TH(CHKARGS);
61*4d131170SRobert Mustacchi static void post_TP(CHKARGS);
62260e9a87SYuri Pankov static void post_UC(CHKARGS);
63260e9a87SYuri Pankov static void post_UR(CHKARGS);
64c66b8046SYuri Pankov static void post_in(CHKARGS);
6595c635efSGarrett D'Amore
66cec8643bSMichal Nowak static const v_check man_valids[MAN_MAX - MAN_TH] = {
67260e9a87SYuri Pankov post_TH, /* TH */
68cec8643bSMichal Nowak post_SH, /* SH */
69cec8643bSMichal Nowak post_SH, /* SS */
70*4d131170SRobert Mustacchi post_TP, /* TP */
71*4d131170SRobert Mustacchi post_TP, /* TQ */
72cec8643bSMichal Nowak check_abort,/* LP */
73260e9a87SYuri Pankov check_par, /* PP */
74cec8643bSMichal Nowak check_abort,/* P */
75260e9a87SYuri Pankov post_IP, /* IP */
76260e9a87SYuri Pankov NULL, /* HP */
77260e9a87SYuri Pankov NULL, /* SM */
78260e9a87SYuri Pankov NULL, /* SB */
79260e9a87SYuri Pankov NULL, /* BI */
80260e9a87SYuri Pankov NULL, /* IB */
81260e9a87SYuri Pankov NULL, /* BR */
82260e9a87SYuri Pankov NULL, /* RB */
83260e9a87SYuri Pankov NULL, /* R */
84260e9a87SYuri Pankov NULL, /* B */
85260e9a87SYuri Pankov NULL, /* I */
86260e9a87SYuri Pankov NULL, /* IR */
87260e9a87SYuri Pankov NULL, /* RI */
88260e9a87SYuri Pankov NULL, /* RE */
89260e9a87SYuri Pankov check_part, /* RS */
90260e9a87SYuri Pankov NULL, /* DT */
91260e9a87SYuri Pankov post_UC, /* UC */
92260e9a87SYuri Pankov NULL, /* PD */
93260e9a87SYuri Pankov post_AT, /* AT */
94c66b8046SYuri Pankov post_in, /* in */
95cec8643bSMichal Nowak NULL, /* SY */
96cec8643bSMichal Nowak NULL, /* YS */
97260e9a87SYuri Pankov post_OP, /* OP */
98cec8643bSMichal Nowak post_EX, /* EX */
99cec8643bSMichal Nowak post_EE, /* EE */
100260e9a87SYuri Pankov post_UR, /* UR */
101260e9a87SYuri Pankov NULL, /* UE */
102c66b8046SYuri Pankov post_UR, /* MT */
103c66b8046SYuri Pankov NULL, /* ME */
10495c635efSGarrett D'Amore };
10595c635efSGarrett D'Amore
10695c635efSGarrett D'Amore
107cec8643bSMichal Nowak /* Validate the subtree rooted at man->last. */
108260e9a87SYuri Pankov void
man_validate(struct roff_man * man)109cec8643bSMichal Nowak man_validate(struct roff_man *man)
11095c635efSGarrett D'Amore {
111371584c2SYuri Pankov struct roff_node *n;
112c66b8046SYuri Pankov const v_check *cp;
11395c635efSGarrett D'Amore
114cec8643bSMichal Nowak /*
115cec8643bSMichal Nowak * Translate obsolete macros such that later code
116cec8643bSMichal Nowak * does not need to look for them.
117cec8643bSMichal Nowak */
118cec8643bSMichal Nowak
119260e9a87SYuri Pankov n = man->last;
120cec8643bSMichal Nowak switch (n->tok) {
121cec8643bSMichal Nowak case MAN_LP:
122cec8643bSMichal Nowak case MAN_P:
123cec8643bSMichal Nowak n->tok = MAN_PP;
124cec8643bSMichal Nowak break;
125cec8643bSMichal Nowak default:
126cec8643bSMichal Nowak break;
127cec8643bSMichal Nowak }
128cec8643bSMichal Nowak
129cec8643bSMichal Nowak /*
130cec8643bSMichal Nowak * Iterate over all children, recursing into each one
131cec8643bSMichal Nowak * in turn, depth-first.
132cec8643bSMichal Nowak */
133cec8643bSMichal Nowak
134371584c2SYuri Pankov man->last = man->last->child;
135371584c2SYuri Pankov while (man->last != NULL) {
136cec8643bSMichal Nowak man_validate(man);
137371584c2SYuri Pankov if (man->last == n)
138371584c2SYuri Pankov man->last = man->last->child;
139371584c2SYuri Pankov else
140371584c2SYuri Pankov man->last = man->last->next;
141371584c2SYuri Pankov }
14295c635efSGarrett D'Amore
143cec8643bSMichal Nowak /* Finally validate the macro itself. */
144cec8643bSMichal Nowak
145371584c2SYuri Pankov man->last = n;
146371584c2SYuri Pankov man->next = ROFF_NEXT_SIBLING;
147260e9a87SYuri Pankov switch (n->type) {
148371584c2SYuri Pankov case ROFFT_TEXT:
149260e9a87SYuri Pankov check_text(man, n);
150260e9a87SYuri Pankov break;
151371584c2SYuri Pankov case ROFFT_ROOT:
152260e9a87SYuri Pankov check_root(man, n);
153260e9a87SYuri Pankov break;
1546640c13bSYuri Pankov case ROFFT_COMMENT:
155371584c2SYuri Pankov case ROFFT_EQN:
156371584c2SYuri Pankov case ROFFT_TBL:
157260e9a87SYuri Pankov break;
15895c635efSGarrett D'Amore default:
159c66b8046SYuri Pankov if (n->tok < ROFF_MAX) {
160c66b8046SYuri Pankov roff_validate(man);
161c66b8046SYuri Pankov break;
162c66b8046SYuri Pankov }
163c66b8046SYuri Pankov assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
164cec8643bSMichal Nowak cp = man_valids + (n->tok - MAN_TH);
165260e9a87SYuri Pankov if (*cp)
166260e9a87SYuri Pankov (*cp)(man, n);
167371584c2SYuri Pankov if (man->last == n)
168cec8643bSMichal Nowak n->flags |= NODE_VALID;
16995c635efSGarrett D'Amore break;
17095c635efSGarrett D'Amore }
17195c635efSGarrett D'Amore }
17295c635efSGarrett D'Amore
173260e9a87SYuri Pankov static void
check_root(CHKARGS)17495c635efSGarrett D'Amore check_root(CHKARGS)
17595c635efSGarrett D'Amore {
176260e9a87SYuri Pankov assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
17795c635efSGarrett D'Amore
1786640c13bSYuri Pankov if (n->last == NULL || n->last->type == ROFFT_COMMENT)
179cec8643bSMichal Nowak mandoc_msg(MANDOCERR_DOC_EMPTY, n->line, n->pos, NULL);
180260e9a87SYuri Pankov else
181260e9a87SYuri Pankov man->meta.hasbody = 1;
18295c635efSGarrett D'Amore
183260e9a87SYuri Pankov if (NULL == man->meta.title) {
184cec8643bSMichal Nowak mandoc_msg(MANDOCERR_TH_NOTITLE, n->line, n->pos, NULL);
18595c635efSGarrett D'Amore
18695c635efSGarrett D'Amore /*
18795c635efSGarrett D'Amore * If a title hasn't been set, do so now (by
18895c635efSGarrett D'Amore * implication, date and section also aren't set).
18995c635efSGarrett D'Amore */
19095c635efSGarrett D'Amore
191260e9a87SYuri Pankov man->meta.title = mandoc_strdup("");
192260e9a87SYuri Pankov man->meta.msec = mandoc_strdup("");
193*4d131170SRobert Mustacchi man->meta.date = mandoc_normdate(NULL, NULL);
19495c635efSGarrett D'Amore }
195c66b8046SYuri Pankov
196c66b8046SYuri Pankov if (man->meta.os_e &&
197c66b8046SYuri Pankov (man->meta.rcsids & (1 << man->meta.os_e)) == 0)
198cec8643bSMichal Nowak mandoc_msg(MANDOCERR_RCS_MISSING, 0, 0,
199c66b8046SYuri Pankov man->meta.os_e == MANDOC_OS_OPENBSD ?
200c66b8046SYuri Pankov "(OpenBSD)" : "(NetBSD)");
20195c635efSGarrett D'Amore }
20295c635efSGarrett D'Amore
20395c635efSGarrett D'Amore static void
check_abort(CHKARGS)204cec8643bSMichal Nowak check_abort(CHKARGS)
205cec8643bSMichal Nowak {
206cec8643bSMichal Nowak abort();
207cec8643bSMichal Nowak }
208cec8643bSMichal Nowak
209*4d131170SRobert Mustacchi /*
210*4d131170SRobert Mustacchi * Skip leading whitespace, dashes, backslashes, and font escapes,
211*4d131170SRobert Mustacchi * then create a tag if the first following byte is a letter.
212*4d131170SRobert Mustacchi * Priority is high unless whitespace is present.
213*4d131170SRobert Mustacchi */
214*4d131170SRobert Mustacchi static void
check_tag(struct roff_node * n,struct roff_node * nt)215*4d131170SRobert Mustacchi check_tag(struct roff_node *n, struct roff_node *nt)
216*4d131170SRobert Mustacchi {
217*4d131170SRobert Mustacchi const char *cp, *arg;
218*4d131170SRobert Mustacchi int prio, sz;
219*4d131170SRobert Mustacchi
220*4d131170SRobert Mustacchi if (nt == NULL || nt->type != ROFFT_TEXT)
221*4d131170SRobert Mustacchi return;
222*4d131170SRobert Mustacchi
223*4d131170SRobert Mustacchi cp = nt->string;
224*4d131170SRobert Mustacchi prio = TAG_STRONG;
225*4d131170SRobert Mustacchi for (;;) {
226*4d131170SRobert Mustacchi switch (*cp) {
227*4d131170SRobert Mustacchi case ' ':
228*4d131170SRobert Mustacchi case '\t':
229*4d131170SRobert Mustacchi prio = TAG_WEAK;
230*4d131170SRobert Mustacchi /* FALLTHROUGH */
231*4d131170SRobert Mustacchi case '-':
232*4d131170SRobert Mustacchi cp++;
233*4d131170SRobert Mustacchi break;
234*4d131170SRobert Mustacchi case '\\':
235*4d131170SRobert Mustacchi cp++;
236*4d131170SRobert Mustacchi switch (mandoc_escape(&cp, &arg, &sz)) {
237*4d131170SRobert Mustacchi case ESCAPE_FONT:
238*4d131170SRobert Mustacchi case ESCAPE_FONTBOLD:
239*4d131170SRobert Mustacchi case ESCAPE_FONTITALIC:
240*4d131170SRobert Mustacchi case ESCAPE_FONTBI:
241*4d131170SRobert Mustacchi case ESCAPE_FONTROMAN:
242*4d131170SRobert Mustacchi case ESCAPE_FONTCR:
243*4d131170SRobert Mustacchi case ESCAPE_FONTCB:
244*4d131170SRobert Mustacchi case ESCAPE_FONTCI:
245*4d131170SRobert Mustacchi case ESCAPE_FONTPREV:
246*4d131170SRobert Mustacchi case ESCAPE_IGNORE:
247*4d131170SRobert Mustacchi break;
248*4d131170SRobert Mustacchi case ESCAPE_SPECIAL:
249*4d131170SRobert Mustacchi if (sz != 1)
250*4d131170SRobert Mustacchi return;
251*4d131170SRobert Mustacchi switch (*arg) {
252*4d131170SRobert Mustacchi case '-':
253*4d131170SRobert Mustacchi case 'e':
254*4d131170SRobert Mustacchi break;
255*4d131170SRobert Mustacchi default:
256*4d131170SRobert Mustacchi return;
257*4d131170SRobert Mustacchi }
258*4d131170SRobert Mustacchi break;
259*4d131170SRobert Mustacchi default:
260*4d131170SRobert Mustacchi return;
261*4d131170SRobert Mustacchi }
262*4d131170SRobert Mustacchi break;
263*4d131170SRobert Mustacchi default:
264*4d131170SRobert Mustacchi if (isalpha((unsigned char)*cp))
265*4d131170SRobert Mustacchi tag_put(cp, prio, n);
266*4d131170SRobert Mustacchi return;
267*4d131170SRobert Mustacchi }
268*4d131170SRobert Mustacchi }
269*4d131170SRobert Mustacchi }
270*4d131170SRobert Mustacchi
271cec8643bSMichal Nowak static void
check_text(CHKARGS)27295c635efSGarrett D'Amore check_text(CHKARGS)
27395c635efSGarrett D'Amore {
27495c635efSGarrett D'Amore char *cp, *p;
27595c635efSGarrett D'Amore
276cec8643bSMichal Nowak if (n->flags & NODE_NOFILL)
27795c635efSGarrett D'Amore return;
27895c635efSGarrett D'Amore
27995c635efSGarrett D'Amore cp = n->string;
28095c635efSGarrett D'Amore for (p = cp; NULL != (p = strchr(p, '\t')); p++)
281cec8643bSMichal Nowak mandoc_msg(MANDOCERR_FI_TAB,
282cec8643bSMichal Nowak n->line, n->pos + (int)(p - cp), NULL);
283cec8643bSMichal Nowak }
284cec8643bSMichal Nowak
285cec8643bSMichal Nowak static void
post_EE(CHKARGS)286cec8643bSMichal Nowak post_EE(CHKARGS)
287cec8643bSMichal Nowak {
288cec8643bSMichal Nowak if ((n->flags & NODE_NOFILL) == 0)
289cec8643bSMichal Nowak mandoc_msg(MANDOCERR_FI_SKIP, n->line, n->pos, "EE");
290cec8643bSMichal Nowak }
291cec8643bSMichal Nowak
292cec8643bSMichal Nowak static void
post_EX(CHKARGS)293cec8643bSMichal Nowak post_EX(CHKARGS)
294cec8643bSMichal Nowak {
295cec8643bSMichal Nowak if (n->flags & NODE_NOFILL)
296cec8643bSMichal Nowak mandoc_msg(MANDOCERR_NF_SKIP, n->line, n->pos, "EX");
29795c635efSGarrett D'Amore }
29895c635efSGarrett D'Amore
299260e9a87SYuri Pankov static void
post_OP(CHKARGS)300260e9a87SYuri Pankov post_OP(CHKARGS)
301698f87a4SGarrett D'Amore {
302698f87a4SGarrett D'Amore
303371584c2SYuri Pankov if (n->child == NULL)
304cec8643bSMichal Nowak mandoc_msg(MANDOCERR_OP_EMPTY, n->line, n->pos, "OP");
305371584c2SYuri Pankov else if (n->child->next != NULL && n->child->next->next != NULL) {
306260e9a87SYuri Pankov n = n->child->next->next;
307cec8643bSMichal Nowak mandoc_msg(MANDOCERR_ARG_EXCESS,
308260e9a87SYuri Pankov n->line, n->pos, "OP ... %s", n->string);
309260e9a87SYuri Pankov }
310698f87a4SGarrett D'Amore }
311698f87a4SGarrett D'Amore
312260e9a87SYuri Pankov static void
post_SH(CHKARGS)313cec8643bSMichal Nowak post_SH(CHKARGS)
314cec8643bSMichal Nowak {
315cec8643bSMichal Nowak struct roff_node *nc;
316*4d131170SRobert Mustacchi char *cp, *tag;
317cec8643bSMichal Nowak
318*4d131170SRobert Mustacchi nc = n->child;
319*4d131170SRobert Mustacchi switch (n->type) {
320*4d131170SRobert Mustacchi case ROFFT_HEAD:
321*4d131170SRobert Mustacchi tag = NULL;
322*4d131170SRobert Mustacchi deroff(&tag, n);
323*4d131170SRobert Mustacchi if (tag != NULL) {
324*4d131170SRobert Mustacchi for (cp = tag; *cp != '\0'; cp++)
325*4d131170SRobert Mustacchi if (*cp == ' ')
326*4d131170SRobert Mustacchi *cp = '_';
327*4d131170SRobert Mustacchi if (nc != NULL && nc->type == ROFFT_TEXT &&
328*4d131170SRobert Mustacchi strcmp(nc->string, tag) == 0)
329*4d131170SRobert Mustacchi tag_put(NULL, TAG_STRONG, n);
330*4d131170SRobert Mustacchi else
331*4d131170SRobert Mustacchi tag_put(tag, TAG_FALLBACK, n);
332*4d131170SRobert Mustacchi free(tag);
333*4d131170SRobert Mustacchi }
334cec8643bSMichal Nowak return;
335*4d131170SRobert Mustacchi case ROFFT_BODY:
336*4d131170SRobert Mustacchi if (nc != NULL)
337*4d131170SRobert Mustacchi break;
338*4d131170SRobert Mustacchi return;
339*4d131170SRobert Mustacchi default:
340*4d131170SRobert Mustacchi return;
341*4d131170SRobert Mustacchi }
342cec8643bSMichal Nowak
343cec8643bSMichal Nowak if (nc->tok == MAN_PP && nc->body->child != NULL) {
344cec8643bSMichal Nowak while (nc->body->last != NULL) {
345cec8643bSMichal Nowak man->next = ROFF_NEXT_CHILD;
346cec8643bSMichal Nowak roff_node_relink(man, nc->body->last);
347cec8643bSMichal Nowak man->last = n;
348cec8643bSMichal Nowak }
349cec8643bSMichal Nowak }
350cec8643bSMichal Nowak
351cec8643bSMichal Nowak if (nc->tok == MAN_PP || nc->tok == ROFF_sp || nc->tok == ROFF_br) {
352cec8643bSMichal Nowak mandoc_msg(MANDOCERR_PAR_SKIP, nc->line, nc->pos,
353cec8643bSMichal Nowak "%s after %s", roff_name[nc->tok], roff_name[n->tok]);
354cec8643bSMichal Nowak roff_node_delete(man, nc);
355cec8643bSMichal Nowak }
356cec8643bSMichal Nowak
357cec8643bSMichal Nowak /*
358cec8643bSMichal Nowak * Trailing PP is empty, so it is deleted by check_par().
359cec8643bSMichal Nowak * Trailing sp is significant.
360cec8643bSMichal Nowak */
361cec8643bSMichal Nowak
362cec8643bSMichal Nowak if ((nc = n->last) != NULL && nc->tok == ROFF_br) {
363cec8643bSMichal Nowak mandoc_msg(MANDOCERR_PAR_SKIP,
364cec8643bSMichal Nowak nc->line, nc->pos, "%s at the end of %s",
365cec8643bSMichal Nowak roff_name[nc->tok], roff_name[n->tok]);
366cec8643bSMichal Nowak roff_node_delete(man, nc);
367cec8643bSMichal Nowak }
368cec8643bSMichal Nowak }
369cec8643bSMichal Nowak
370cec8643bSMichal Nowak static void
post_UR(CHKARGS)371260e9a87SYuri Pankov post_UR(CHKARGS)
372260e9a87SYuri Pankov {
373371584c2SYuri Pankov if (n->type == ROFFT_HEAD && n->child == NULL)
374cec8643bSMichal Nowak mandoc_msg(MANDOCERR_UR_NOHEAD, n->line, n->pos,
375cec8643bSMichal Nowak "%s", roff_name[n->tok]);
376260e9a87SYuri Pankov check_part(man, n);
377260e9a87SYuri Pankov }
378260e9a87SYuri Pankov
379260e9a87SYuri Pankov static void
check_part(CHKARGS)38095c635efSGarrett D'Amore check_part(CHKARGS)
38195c635efSGarrett D'Amore {
38295c635efSGarrett D'Amore
383371584c2SYuri Pankov if (n->type == ROFFT_BODY && n->child == NULL)
384cec8643bSMichal Nowak mandoc_msg(MANDOCERR_BLK_EMPTY, n->line, n->pos,
385cec8643bSMichal Nowak "%s", roff_name[n->tok]);
38695c635efSGarrett D'Amore }
38795c635efSGarrett D'Amore
388260e9a87SYuri Pankov static void
check_par(CHKARGS)38995c635efSGarrett D'Amore check_par(CHKARGS)
39095c635efSGarrett D'Amore {
39195c635efSGarrett D'Amore
39295c635efSGarrett D'Amore switch (n->type) {
393371584c2SYuri Pankov case ROFFT_BLOCK:
394371584c2SYuri Pankov if (n->body->child == NULL)
395371584c2SYuri Pankov roff_node_delete(man, n);
39695c635efSGarrett D'Amore break;
397371584c2SYuri Pankov case ROFFT_BODY:
398cec8643bSMichal Nowak if (n->child != NULL &&
399cec8643bSMichal Nowak (n->child->tok == ROFF_sp || n->child->tok == ROFF_br)) {
400cec8643bSMichal Nowak mandoc_msg(MANDOCERR_PAR_SKIP,
401cec8643bSMichal Nowak n->child->line, n->child->pos,
402cec8643bSMichal Nowak "%s after %s", roff_name[n->child->tok],
403cec8643bSMichal Nowak roff_name[n->tok]);
404cec8643bSMichal Nowak roff_node_delete(man, n->child);
405cec8643bSMichal Nowak }
406371584c2SYuri Pankov if (n->child == NULL)
407cec8643bSMichal Nowak mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos,
408c66b8046SYuri Pankov "%s empty", roff_name[n->tok]);
40995c635efSGarrett D'Amore break;
410371584c2SYuri Pankov case ROFFT_HEAD:
411371584c2SYuri Pankov if (n->child != NULL)
412cec8643bSMichal Nowak mandoc_msg(MANDOCERR_ARG_SKIP,
413cec8643bSMichal Nowak n->line, n->pos, "%s %s%s",
414c66b8046SYuri Pankov roff_name[n->tok], n->child->string,
415371584c2SYuri Pankov n->child->next != NULL ? " ..." : "");
41695c635efSGarrett D'Amore break;
41795c635efSGarrett D'Amore default:
41895c635efSGarrett D'Amore break;
41995c635efSGarrett D'Amore }
42095c635efSGarrett D'Amore }
42195c635efSGarrett D'Amore
422260e9a87SYuri Pankov static void
post_IP(CHKARGS)423698f87a4SGarrett D'Amore post_IP(CHKARGS)
424698f87a4SGarrett D'Amore {
425698f87a4SGarrett D'Amore switch (n->type) {
426371584c2SYuri Pankov case ROFFT_BLOCK:
427371584c2SYuri Pankov if (n->head->child == NULL && n->body->child == NULL)
428371584c2SYuri Pankov roff_node_delete(man, n);
429698f87a4SGarrett D'Amore break;
430*4d131170SRobert Mustacchi case ROFFT_HEAD:
431*4d131170SRobert Mustacchi check_tag(n, n->child);
432*4d131170SRobert Mustacchi break;
433371584c2SYuri Pankov case ROFFT_BODY:
434371584c2SYuri Pankov if (n->parent->head->child == NULL && n->child == NULL)
435cec8643bSMichal Nowak mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos,
436c66b8046SYuri Pankov "%s empty", roff_name[n->tok]);
437698f87a4SGarrett D'Amore break;
438698f87a4SGarrett D'Amore default:
439698f87a4SGarrett D'Amore break;
440698f87a4SGarrett D'Amore }
441698f87a4SGarrett D'Amore }
44295c635efSGarrett D'Amore
443*4d131170SRobert Mustacchi /*
444*4d131170SRobert Mustacchi * The first next-line element in the head is the tag.
445*4d131170SRobert Mustacchi * If that's a font macro, use its first child instead.
446*4d131170SRobert Mustacchi */
447*4d131170SRobert Mustacchi static void
post_TP(CHKARGS)448*4d131170SRobert Mustacchi post_TP(CHKARGS)
449*4d131170SRobert Mustacchi {
450*4d131170SRobert Mustacchi struct roff_node *nt;
451*4d131170SRobert Mustacchi
452*4d131170SRobert Mustacchi if (n->type != ROFFT_HEAD || (nt = n->child) == NULL)
453*4d131170SRobert Mustacchi return;
454*4d131170SRobert Mustacchi
455*4d131170SRobert Mustacchi while ((nt->flags & NODE_LINE) == 0)
456*4d131170SRobert Mustacchi if ((nt = nt->next) == NULL)
457*4d131170SRobert Mustacchi return;
458*4d131170SRobert Mustacchi
459*4d131170SRobert Mustacchi switch (nt->tok) {
460*4d131170SRobert Mustacchi case MAN_B:
461*4d131170SRobert Mustacchi case MAN_BI:
462*4d131170SRobert Mustacchi case MAN_BR:
463*4d131170SRobert Mustacchi case MAN_I:
464*4d131170SRobert Mustacchi case MAN_IB:
465*4d131170SRobert Mustacchi case MAN_IR:
466*4d131170SRobert Mustacchi nt = nt->child;
467*4d131170SRobert Mustacchi break;
468*4d131170SRobert Mustacchi default:
469*4d131170SRobert Mustacchi break;
470*4d131170SRobert Mustacchi }
471*4d131170SRobert Mustacchi check_tag(n, nt);
472*4d131170SRobert Mustacchi }
473*4d131170SRobert Mustacchi
474260e9a87SYuri Pankov static void
post_TH(CHKARGS)47595c635efSGarrett D'Amore post_TH(CHKARGS)
47695c635efSGarrett D'Amore {
477371584c2SYuri Pankov struct roff_node *nb;
47895c635efSGarrett D'Amore const char *p;
47995c635efSGarrett D'Amore
480698f87a4SGarrett D'Amore free(man->meta.title);
481698f87a4SGarrett D'Amore free(man->meta.vol);
482371584c2SYuri Pankov free(man->meta.os);
483698f87a4SGarrett D'Amore free(man->meta.msec);
484698f87a4SGarrett D'Amore free(man->meta.date);
48595c635efSGarrett D'Amore
486698f87a4SGarrett D'Amore man->meta.title = man->meta.vol = man->meta.date =
487371584c2SYuri Pankov man->meta.msec = man->meta.os = NULL;
48895c635efSGarrett D'Amore
489260e9a87SYuri Pankov nb = n;
490260e9a87SYuri Pankov
491371584c2SYuri Pankov /* ->TITLE<- MSEC DATE OS VOL */
49295c635efSGarrett D'Amore
49395c635efSGarrett D'Amore n = n->child;
494*4d131170SRobert Mustacchi if (n != NULL && n->string != NULL) {
495*4d131170SRobert Mustacchi for (p = n->string; *p != '\0'; p++) {
49695c635efSGarrett D'Amore /* Only warn about this once... */
49795c635efSGarrett D'Amore if (isalpha((unsigned char)*p) &&
49895c635efSGarrett D'Amore ! isupper((unsigned char)*p)) {
499cec8643bSMichal Nowak mandoc_msg(MANDOCERR_TITLE_CASE, n->line,
500cec8643bSMichal Nowak n->pos + (int)(p - n->string),
501260e9a87SYuri Pankov "TH %s", n->string);
50295c635efSGarrett D'Amore break;
50395c635efSGarrett D'Amore }
50495c635efSGarrett D'Amore }
505698f87a4SGarrett D'Amore man->meta.title = mandoc_strdup(n->string);
506260e9a87SYuri Pankov } else {
507698f87a4SGarrett D'Amore man->meta.title = mandoc_strdup("");
508cec8643bSMichal Nowak mandoc_msg(MANDOCERR_TH_NOTITLE, nb->line, nb->pos, "TH");
509260e9a87SYuri Pankov }
51095c635efSGarrett D'Amore
511371584c2SYuri Pankov /* TITLE ->MSEC<- DATE OS VOL */
51295c635efSGarrett D'Amore
513*4d131170SRobert Mustacchi if (n != NULL)
51495c635efSGarrett D'Amore n = n->next;
515*4d131170SRobert Mustacchi if (n != NULL && n->string != NULL) {
516698f87a4SGarrett D'Amore man->meta.msec = mandoc_strdup(n->string);
517*4d131170SRobert Mustacchi if (man->filesec != '\0' &&
518*4d131170SRobert Mustacchi man->filesec != *n->string &&
519*4d131170SRobert Mustacchi *n->string >= '1' && *n->string <= '9')
520*4d131170SRobert Mustacchi mandoc_msg(MANDOCERR_MSEC_FILE, n->line, n->pos,
521*4d131170SRobert Mustacchi "*.%c vs TH ... %c", man->filesec, *n->string);
522*4d131170SRobert Mustacchi } else {
523698f87a4SGarrett D'Amore man->meta.msec = mandoc_strdup("");
524cec8643bSMichal Nowak mandoc_msg(MANDOCERR_MSEC_MISSING,
525260e9a87SYuri Pankov nb->line, nb->pos, "TH %s", man->meta.title);
526260e9a87SYuri Pankov }
52795c635efSGarrett D'Amore
528371584c2SYuri Pankov /* TITLE MSEC ->DATE<- OS VOL */
52995c635efSGarrett D'Amore
530*4d131170SRobert Mustacchi if (n != NULL)
53195c635efSGarrett D'Amore n = n->next;
532*4d131170SRobert Mustacchi if (man->quick && n != NULL)
533698f87a4SGarrett D'Amore man->meta.date = mandoc_strdup("");
534*4d131170SRobert Mustacchi else
535*4d131170SRobert Mustacchi man->meta.date = mandoc_normdate(n, nb);
53695c635efSGarrett D'Amore
537371584c2SYuri Pankov /* TITLE MSEC DATE ->OS<- VOL */
53895c635efSGarrett D'Amore
53995c635efSGarrett D'Amore if (n && (n = n->next))
540371584c2SYuri Pankov man->meta.os = mandoc_strdup(n->string);
541c66b8046SYuri Pankov else if (man->os_s != NULL)
542c66b8046SYuri Pankov man->meta.os = mandoc_strdup(man->os_s);
543c66b8046SYuri Pankov if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) {
544c66b8046SYuri Pankov if (strstr(man->meta.os, "OpenBSD") != NULL)
545c66b8046SYuri Pankov man->meta.os_e = MANDOC_OS_OPENBSD;
546c66b8046SYuri Pankov else if (strstr(man->meta.os, "NetBSD") != NULL)
547c66b8046SYuri Pankov man->meta.os_e = MANDOC_OS_NETBSD;
548c66b8046SYuri Pankov }
54995c635efSGarrett D'Amore
550371584c2SYuri Pankov /* TITLE MSEC DATE OS ->VOL<- */
55195c635efSGarrett D'Amore /* If missing, use the default VOL name for MSEC. */
55295c635efSGarrett D'Amore
55395c635efSGarrett D'Amore if (n && (n = n->next))
554698f87a4SGarrett D'Amore man->meta.vol = mandoc_strdup(n->string);
555698f87a4SGarrett D'Amore else if ('\0' != man->meta.msec[0] &&
556698f87a4SGarrett D'Amore (NULL != (p = mandoc_a2msec(man->meta.msec))))
557698f87a4SGarrett D'Amore man->meta.vol = mandoc_strdup(p);
55895c635efSGarrett D'Amore
559260e9a87SYuri Pankov if (n != NULL && (n = n->next) != NULL)
560cec8643bSMichal Nowak mandoc_msg(MANDOCERR_ARG_EXCESS,
561260e9a87SYuri Pankov n->line, n->pos, "TH ... %s", n->string);
562260e9a87SYuri Pankov
56395c635efSGarrett D'Amore /*
56495c635efSGarrett D'Amore * Remove the `TH' node after we've processed it for our
56595c635efSGarrett D'Amore * meta-data.
56695c635efSGarrett D'Amore */
567371584c2SYuri Pankov roff_node_delete(man, man->last);
56895c635efSGarrett D'Amore }
56995c635efSGarrett D'Amore
570260e9a87SYuri Pankov static void
post_UC(CHKARGS)57195c635efSGarrett D'Amore post_UC(CHKARGS)
57295c635efSGarrett D'Amore {
57395c635efSGarrett D'Amore static const char * const bsd_versions[] = {
57495c635efSGarrett D'Amore "3rd Berkeley Distribution",
57595c635efSGarrett D'Amore "4th Berkeley Distribution",
57695c635efSGarrett D'Amore "4.2 Berkeley Distribution",
57795c635efSGarrett D'Amore "4.3 Berkeley Distribution",
57895c635efSGarrett D'Amore "4.4 Berkeley Distribution",
57995c635efSGarrett D'Amore };
58095c635efSGarrett D'Amore
58195c635efSGarrett D'Amore const char *p, *s;
58295c635efSGarrett D'Amore
58395c635efSGarrett D'Amore n = n->child;
58495c635efSGarrett D'Amore
585371584c2SYuri Pankov if (n == NULL || n->type != ROFFT_TEXT)
58695c635efSGarrett D'Amore p = bsd_versions[0];
58795c635efSGarrett D'Amore else {
58895c635efSGarrett D'Amore s = n->string;
58995c635efSGarrett D'Amore if (0 == strcmp(s, "3"))
59095c635efSGarrett D'Amore p = bsd_versions[0];
59195c635efSGarrett D'Amore else if (0 == strcmp(s, "4"))
59295c635efSGarrett D'Amore p = bsd_versions[1];
59395c635efSGarrett D'Amore else if (0 == strcmp(s, "5"))
59495c635efSGarrett D'Amore p = bsd_versions[2];
59595c635efSGarrett D'Amore else if (0 == strcmp(s, "6"))
59695c635efSGarrett D'Amore p = bsd_versions[3];
59795c635efSGarrett D'Amore else if (0 == strcmp(s, "7"))
59895c635efSGarrett D'Amore p = bsd_versions[4];
59995c635efSGarrett D'Amore else
60095c635efSGarrett D'Amore p = bsd_versions[0];
60195c635efSGarrett D'Amore }
60295c635efSGarrett D'Amore
603371584c2SYuri Pankov free(man->meta.os);
604371584c2SYuri Pankov man->meta.os = mandoc_strdup(p);
60595c635efSGarrett D'Amore }
60695c635efSGarrett D'Amore
607260e9a87SYuri Pankov static void
post_AT(CHKARGS)60895c635efSGarrett D'Amore post_AT(CHKARGS)
60995c635efSGarrett D'Amore {
61095c635efSGarrett D'Amore static const char * const unix_versions[] = {
61195c635efSGarrett D'Amore "7th Edition",
61295c635efSGarrett D'Amore "System III",
61395c635efSGarrett D'Amore "System V",
61495c635efSGarrett D'Amore "System V Release 2",
61595c635efSGarrett D'Amore };
61695c635efSGarrett D'Amore
617371584c2SYuri Pankov struct roff_node *nn;
61895c635efSGarrett D'Amore const char *p, *s;
61995c635efSGarrett D'Amore
62095c635efSGarrett D'Amore n = n->child;
62195c635efSGarrett D'Amore
622371584c2SYuri Pankov if (n == NULL || n->type != ROFFT_TEXT)
62395c635efSGarrett D'Amore p = unix_versions[0];
62495c635efSGarrett D'Amore else {
62595c635efSGarrett D'Amore s = n->string;
62695c635efSGarrett D'Amore if (0 == strcmp(s, "3"))
62795c635efSGarrett D'Amore p = unix_versions[0];
62895c635efSGarrett D'Amore else if (0 == strcmp(s, "4"))
62995c635efSGarrett D'Amore p = unix_versions[1];
63095c635efSGarrett D'Amore else if (0 == strcmp(s, "5")) {
63195c635efSGarrett D'Amore nn = n->next;
632371584c2SYuri Pankov if (nn != NULL &&
633371584c2SYuri Pankov nn->type == ROFFT_TEXT &&
634371584c2SYuri Pankov nn->string[0] != '\0')
63595c635efSGarrett D'Amore p = unix_versions[3];
63695c635efSGarrett D'Amore else
63795c635efSGarrett D'Amore p = unix_versions[2];
63895c635efSGarrett D'Amore } else
63995c635efSGarrett D'Amore p = unix_versions[0];
64095c635efSGarrett D'Amore }
64195c635efSGarrett D'Amore
642371584c2SYuri Pankov free(man->meta.os);
643371584c2SYuri Pankov man->meta.os = mandoc_strdup(p);
64495c635efSGarrett D'Amore }
64595c635efSGarrett D'Amore
646260e9a87SYuri Pankov static void
post_in(CHKARGS)647c66b8046SYuri Pankov post_in(CHKARGS)
648c66b8046SYuri Pankov {
649c66b8046SYuri Pankov char *s;
650c66b8046SYuri Pankov
651c66b8046SYuri Pankov if (n->parent->tok != MAN_TP ||
652c66b8046SYuri Pankov n->parent->type != ROFFT_HEAD ||
653c66b8046SYuri Pankov n->child == NULL ||
654c66b8046SYuri Pankov *n->child->string == '+' ||
655c66b8046SYuri Pankov *n->child->string == '-')
656c66b8046SYuri Pankov return;
657c66b8046SYuri Pankov mandoc_asprintf(&s, "+%s", n->child->string);
658c66b8046SYuri Pankov free(n->child->string);
659c66b8046SYuri Pankov n->child->string = s;
660c66b8046SYuri Pankov }
661