1 /* $Id: man_validate.c,v 1.156 2021/08/10 12:55:03 schwarze Exp $ */
3 * Copyright (c) 2010, 2012-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
18 * Validation module for man(7) syntax trees used by mandoc(1).
22 #include <sys/types.h>
34 #include "mandoc_aux.h"
38 #include "libmandoc.h"
43 #define CHKARGS struct roff_man *man, struct roff_node *n
45 typedef void (*v_check)(CHKARGS);
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);
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);
66 static const v_check man_valids[MAN_MAX - MAN_TH] = {
107 /* Validate the subtree rooted at man->last. */
109 man_validate(struct roff_man *man)
115 * Translate obsolete macros such that later code
116 * does not need to look for them.
130 * Iterate over all children, recursing into each one
131 * in turn, depth-first.
134 man->last = man->last->child;
135 while (man->last != NULL) {
138 man->last = man->last->child;
140 man->last = man->last->next;
143 /* Finally validate the macro itself. */
146 man->next = ROFF_NEXT_SIBLING;
159 if (n->tok < ROFF_MAX) {
163 assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
164 cp = man_valids + (n->tok - MAN_TH);
168 n->flags |= NODE_VALID;
176 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
178 if (n->last == NULL || n->last->type == ROFFT_COMMENT)
179 mandoc_msg(MANDOCERR_DOC_EMPTY, n->line, n->pos, NULL);
181 man->meta.hasbody = 1;
183 if (NULL == man->meta.title) {
184 mandoc_msg(MANDOCERR_TH_NOTITLE, n->line, n->pos, NULL);
187 * If a title hasn't been set, do so now (by
188 * implication, date and section also aren't set).
191 man->meta.title = mandoc_strdup("");
192 man->meta.msec = mandoc_strdup("");
193 man->meta.date = mandoc_normdate(NULL, NULL);
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)");
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.
215 check_tag(struct roff_node *n, struct roff_node *nt)
217 const char *cp, *arg;
220 if (nt == NULL || nt->type != ROFFT_TEXT)
236 switch (mandoc_escape(&cp, &arg, &sz)) {
238 case ESCAPE_FONTBOLD:
239 case ESCAPE_FONTITALIC:
241 case ESCAPE_FONTROMAN:
245 case ESCAPE_FONTPREV:
264 if (isalpha((unsigned char)*cp))
265 tag_put(cp, prio, n);
276 if (n->flags & NODE_NOFILL)
280 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
281 mandoc_msg(MANDOCERR_FI_TAB,
282 n->line, n->pos + (int)(p - cp), NULL);
288 if ((n->flags & NODE_NOFILL) == 0)
289 mandoc_msg(MANDOCERR_FI_SKIP, n->line, n->pos, "EE");
295 if (n->flags & NODE_NOFILL)
296 mandoc_msg(MANDOCERR_NF_SKIP, n->line, n->pos, "EX");
303 if (n->child == NULL)
304 mandoc_msg(MANDOCERR_OP_EMPTY, n->line, n->pos, "OP");
305 else if (n->child->next != NULL && n->child->next->next != NULL) {
306 n = n->child->next->next;
307 mandoc_msg(MANDOCERR_ARG_EXCESS,
308 n->line, n->pos, "OP ... %s", n->string);
315 struct roff_node *nc;
324 for (cp = tag; *cp != '\0'; cp++)
327 if (nc != NULL && nc->type == ROFFT_TEXT &&
328 strcmp(nc->string, tag) == 0)
329 tag_put(NULL, TAG_STRONG, n);
331 tag_put(tag, TAG_FALLBACK, n);
343 if (nc->tok == MAN_PP && nc->body->child != NULL) {
344 while (nc->body->last != NULL) {
345 man->next = ROFF_NEXT_CHILD;
346 roff_node_relink(man, nc->body->last);
351 if (nc->tok == MAN_PP || nc->tok == ROFF_sp || nc->tok == ROFF_br) {
352 mandoc_msg(MANDOCERR_PAR_SKIP, nc->line, nc->pos,
353 "%s after %s", roff_name[nc->tok], roff_name[n->tok]);
354 roff_node_delete(man, nc);
358 * Trailing PP is empty, so it is deleted by check_par().
359 * Trailing sp is significant.
362 if ((nc = n->last) != NULL && nc->tok == ROFF_br) {
363 mandoc_msg(MANDOCERR_PAR_SKIP,
364 nc->line, nc->pos, "%s at the end of %s",
365 roff_name[nc->tok], roff_name[n->tok]);
366 roff_node_delete(man, nc);
373 if (n->type == ROFFT_HEAD && n->child == NULL)
374 mandoc_msg(MANDOCERR_UR_NOHEAD, n->line, n->pos,
375 "%s", roff_name[n->tok]);
383 if (n->type == ROFFT_BODY && n->child == NULL)
384 mandoc_msg(MANDOCERR_BLK_EMPTY, n->line, n->pos,
385 "%s", roff_name[n->tok]);
394 if (n->body->child == NULL)
395 roff_node_delete(man, n);
398 if (n->child != NULL &&
399 (n->child->tok == ROFF_sp || n->child->tok == ROFF_br)) {
400 mandoc_msg(MANDOCERR_PAR_SKIP,
401 n->child->line, n->child->pos,
402 "%s after %s", roff_name[n->child->tok],
404 roff_node_delete(man, n->child);
406 if (n->child == NULL)
407 mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos,
408 "%s empty", roff_name[n->tok]);
411 if (n->child != NULL)
412 mandoc_msg(MANDOCERR_ARG_SKIP,
413 n->line, n->pos, "%s %s%s",
414 roff_name[n->tok], n->child->string,
415 n->child->next != NULL ? " ..." : "");
427 if (n->head->child == NULL && n->body->child == NULL)
428 roff_node_delete(man, n);
431 check_tag(n, n->child);
434 if (n->parent->head->child == NULL && n->child == NULL)
435 mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos,
436 "%s empty", roff_name[n->tok]);
444 * The first next-line element in the head is the tag.
445 * If that's a font macro, use its first child instead.
450 struct roff_node *nt;
452 if (n->type != ROFFT_HEAD || (nt = n->child) == NULL)
455 while ((nt->flags & NODE_LINE) == 0)
456 if ((nt = nt->next) == NULL)
477 struct roff_node *nb;
480 free(man->meta.title);
483 free(man->meta.msec);
484 free(man->meta.date);
486 man->meta.title = man->meta.vol = man->meta.date =
487 man->meta.msec = man->meta.os = NULL;
491 /* ->TITLE<- MSEC DATE OS VOL */
494 if (n != NULL && n->string != NULL) {
495 for (p = n->string; *p != '\0'; p++) {
496 /* Only warn about this once... */
497 if (isalpha((unsigned char)*p) &&
498 ! isupper((unsigned char)*p)) {
499 mandoc_msg(MANDOCERR_TITLE_CASE, n->line,
500 n->pos + (int)(p - n->string),
505 man->meta.title = mandoc_strdup(n->string);
507 man->meta.title = mandoc_strdup("");
508 mandoc_msg(MANDOCERR_TH_NOTITLE, nb->line, nb->pos, "TH");
511 /* TITLE ->MSEC<- DATE OS VOL */
515 if (n != NULL && n->string != NULL) {
516 man->meta.msec = mandoc_strdup(n->string);
517 if (man->filesec != '\0' &&
518 man->filesec != *n->string &&
519 *n->string >= '1' && *n->string <= '9')
520 mandoc_msg(MANDOCERR_MSEC_FILE, n->line, n->pos,
521 "*.%c vs TH ... %c", man->filesec, *n->string);
523 man->meta.msec = mandoc_strdup("");
524 mandoc_msg(MANDOCERR_MSEC_MISSING,
525 nb->line, nb->pos, "TH %s", man->meta.title);
528 /* TITLE MSEC ->DATE<- OS VOL */
532 if (man->quick && n != NULL)
533 man->meta.date = mandoc_strdup("");
535 man->meta.date = mandoc_normdate(n, nb);
537 /* TITLE MSEC DATE ->OS<- VOL */
539 if (n && (n = n->next))
540 man->meta.os = mandoc_strdup(n->string);
541 else if (man->os_s != NULL)
542 man->meta.os = mandoc_strdup(man->os_s);
543 if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) {
544 if (strstr(man->meta.os, "OpenBSD") != NULL)
545 man->meta.os_e = MANDOC_OS_OPENBSD;
546 else if (strstr(man->meta.os, "NetBSD") != NULL)
547 man->meta.os_e = MANDOC_OS_NETBSD;
550 /* TITLE MSEC DATE OS ->VOL<- */
551 /* If missing, use the default VOL name for MSEC. */
553 if (n && (n = n->next))
554 man->meta.vol = mandoc_strdup(n->string);
555 else if ('\0' != man->meta.msec[0] &&
556 (NULL != (p = mandoc_a2msec(man->meta.msec))))
557 man->meta.vol = mandoc_strdup(p);
559 if (n != NULL && (n = n->next) != NULL)
560 mandoc_msg(MANDOCERR_ARG_EXCESS,
561 n->line, n->pos, "TH ... %s", n->string);
564 * Remove the `TH' node after we've processed it for our
567 roff_node_delete(man, man->last);
573 static const char * const bsd_versions[] = {
574 "3rd Berkeley Distribution",
575 "4th Berkeley Distribution",
576 "4.2 Berkeley Distribution",
577 "4.3 Berkeley Distribution",
578 "4.4 Berkeley Distribution",
585 if (n == NULL || n->type != ROFFT_TEXT)
589 if (0 == strcmp(s, "3"))
591 else if (0 == strcmp(s, "4"))
593 else if (0 == strcmp(s, "5"))
595 else if (0 == strcmp(s, "6"))
597 else if (0 == strcmp(s, "7"))
604 man->meta.os = mandoc_strdup(p);
610 static const char * const unix_versions[] = {
614 "System V Release 2",
617 struct roff_node *nn;
622 if (n == NULL || n->type != ROFFT_TEXT)
623 p = unix_versions[0];
626 if (0 == strcmp(s, "3"))
627 p = unix_versions[0];
628 else if (0 == strcmp(s, "4"))
629 p = unix_versions[1];
630 else if (0 == strcmp(s, "5")) {
633 nn->type == ROFFT_TEXT &&
634 nn->string[0] != '\0')
635 p = unix_versions[3];
637 p = unix_versions[2];
639 p = unix_versions[0];
643 man->meta.os = mandoc_strdup(p);
651 if (n->parent->tok != MAN_TP ||
652 n->parent->type != ROFFT_HEAD ||
654 *n->child->string == '+' ||
655 *n->child->string == '-')
657 mandoc_asprintf(&s, "+%s", n->child->string);
658 free(n->child->string);
659 n->child->string = s;