]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.154 2020/04/24 12:02:33 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
:
243 case ESCAPE_FONTPREV
:
262 if (isalpha((unsigned char)*cp
))
263 tag_put(cp
, prio
, n
);
274 if (n
->flags
& NODE_NOFILL
)
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
);
286 if ((n
->flags
& NODE_NOFILL
) == 0)
287 mandoc_msg(MANDOCERR_FI_SKIP
, n
->line
, n
->pos
, "EE");
293 if (n
->flags
& NODE_NOFILL
)
294 mandoc_msg(MANDOCERR_NF_SKIP
, n
->line
, n
->pos
, "EX");
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
);
313 struct roff_node
*nc
;
322 for (cp
= tag
; *cp
!= '\0'; cp
++)
325 if (nc
!= NULL
&& nc
->type
== ROFFT_TEXT
&&
326 strcmp(nc
->string
, tag
) == 0)
327 tag_put(NULL
, TAG_WEAK
, n
);
329 tag_put(tag
, TAG_FALLBACK
, n
);
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
);
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
);
356 * Trailing PP is empty, so it is deleted by check_par().
357 * Trailing sp is significant.
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
);
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
]);
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
]);
392 if (n
->body
->child
== NULL
)
393 roff_node_delete(man
, n
);
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
],
402 roff_node_delete(man
, n
->child
);
404 if (n
->child
== NULL
)
405 mandoc_msg(MANDOCERR_PAR_SKIP
, n
->line
, n
->pos
,
406 "%s empty", roff_name
[n
->tok
]);
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
? " ..." : "");
425 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
426 roff_node_delete(man
, n
);
429 check_tag(n
, n
->child
);
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
]);
442 * The first next-line element in the head is the tag.
443 * If that's a font macro, use its first child instead.
448 struct roff_node
*nt
;
450 if (n
->type
!= ROFFT_HEAD
|| (nt
= n
->child
) == NULL
)
453 while ((nt
->flags
& NODE_LINE
) == 0)
454 if ((nt
= nt
->next
) == NULL
)
475 struct roff_node
*nb
;
478 free(man
->meta
.title
);
481 free(man
->meta
.msec
);
482 free(man
->meta
.date
);
484 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
485 man
->meta
.msec
= man
->meta
.os
= NULL
;
489 /* ->TITLE<- MSEC DATE OS VOL */
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
),
503 man
->meta
.title
= mandoc_strdup(n
->string
);
505 man
->meta
.title
= mandoc_strdup("");
506 mandoc_msg(MANDOCERR_TH_NOTITLE
, nb
->line
, nb
->pos
, "TH");
509 /* TITLE ->MSEC<- DATE OS VOL */
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
);
521 man
->meta
.msec
= mandoc_strdup("");
522 mandoc_msg(MANDOCERR_MSEC_MISSING
,
523 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
526 /* TITLE MSEC ->DATE<- OS VOL */
530 if (man
->quick
&& n
!= NULL
)
531 man
->meta
.date
= mandoc_strdup("");
533 man
->meta
.date
= mandoc_normdate(n
, nb
);
535 /* TITLE MSEC DATE ->OS<- VOL */
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
;
548 /* TITLE MSEC DATE OS ->VOL<- */
549 /* If missing, use the default VOL name for MSEC. */
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
);
557 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
558 mandoc_msg(MANDOCERR_ARG_EXCESS
,
559 n
->line
, n
->pos
, "TH ... %s", n
->string
);
562 * Remove the `TH' node after we've processed it for our
565 roff_node_delete(man
, man
->last
);
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",
583 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
587 if (0 == strcmp(s
, "3"))
589 else if (0 == strcmp(s
, "4"))
591 else if (0 == strcmp(s
, "5"))
593 else if (0 == strcmp(s
, "6"))
595 else if (0 == strcmp(s
, "7"))
602 man
->meta
.os
= mandoc_strdup(p
);
608 static const char * const unix_versions
[] = {
612 "System V Release 2",
615 struct roff_node
*nn
;
620 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
621 p
= unix_versions
[0];
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")) {
631 nn
->type
== ROFFT_TEXT
&&
632 nn
->string
[0] != '\0')
633 p
= unix_versions
[3];
635 p
= unix_versions
[2];
637 p
= unix_versions
[0];
641 man
->meta
.os
= mandoc_strdup(p
);
649 if (n
->parent
->tok
!= MAN_TP
||
650 n
->parent
->type
!= ROFFT_HEAD
||
652 *n
->child
->string
== '+' ||
653 *n
->child
->string
== '-')
655 mandoc_asprintf(&s
, "+%s", n
->child
->string
);
656 free(n
->child
->string
);
657 n
->child
->string
= s
;