]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.151 2020/03/13 15:32:28 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"
39 #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
;
315 if (n
->type
!= ROFFT_BODY
|| (nc
= n
->child
) == NULL
)
318 if (nc
->tok
== MAN_PP
&& nc
->body
->child
!= NULL
) {
319 while (nc
->body
->last
!= NULL
) {
320 man
->next
= ROFF_NEXT_CHILD
;
321 roff_node_relink(man
, nc
->body
->last
);
326 if (nc
->tok
== MAN_PP
|| nc
->tok
== ROFF_sp
|| nc
->tok
== ROFF_br
) {
327 mandoc_msg(MANDOCERR_PAR_SKIP
, nc
->line
, nc
->pos
,
328 "%s after %s", roff_name
[nc
->tok
], roff_name
[n
->tok
]);
329 roff_node_delete(man
, nc
);
333 * Trailing PP is empty, so it is deleted by check_par().
334 * Trailing sp is significant.
337 if ((nc
= n
->last
) != NULL
&& nc
->tok
== ROFF_br
) {
338 mandoc_msg(MANDOCERR_PAR_SKIP
,
339 nc
->line
, nc
->pos
, "%s at the end of %s",
340 roff_name
[nc
->tok
], roff_name
[n
->tok
]);
341 roff_node_delete(man
, nc
);
348 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
349 mandoc_msg(MANDOCERR_UR_NOHEAD
, n
->line
, n
->pos
,
350 "%s", roff_name
[n
->tok
]);
358 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
359 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
,
360 "%s", roff_name
[n
->tok
]);
369 if (n
->body
->child
== NULL
)
370 roff_node_delete(man
, n
);
373 if (n
->child
!= NULL
&&
374 (n
->child
->tok
== ROFF_sp
|| n
->child
->tok
== ROFF_br
)) {
375 mandoc_msg(MANDOCERR_PAR_SKIP
,
376 n
->child
->line
, n
->child
->pos
,
377 "%s after %s", roff_name
[n
->child
->tok
],
379 roff_node_delete(man
, n
->child
);
381 if (n
->child
== NULL
)
382 mandoc_msg(MANDOCERR_PAR_SKIP
, n
->line
, n
->pos
,
383 "%s empty", roff_name
[n
->tok
]);
386 if (n
->child
!= NULL
)
387 mandoc_msg(MANDOCERR_ARG_SKIP
,
388 n
->line
, n
->pos
, "%s %s%s",
389 roff_name
[n
->tok
], n
->child
->string
,
390 n
->child
->next
!= NULL
? " ..." : "");
402 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
403 roff_node_delete(man
, n
);
406 check_tag(n
, n
->child
);
409 if (n
->parent
->head
->child
== NULL
&& n
->child
== NULL
)
410 mandoc_msg(MANDOCERR_PAR_SKIP
, n
->line
, n
->pos
,
411 "%s empty", roff_name
[n
->tok
]);
419 * The first next-line element in the head is the tag.
420 * If that's a font macro, use its first child instead.
425 struct roff_node
*nt
;
427 if (n
->type
!= ROFFT_HEAD
|| (nt
= n
->child
) == NULL
)
430 while ((nt
->flags
& NODE_LINE
) == 0)
431 if ((nt
= nt
->next
) == NULL
)
452 struct roff_node
*nb
;
455 free(man
->meta
.title
);
458 free(man
->meta
.msec
);
459 free(man
->meta
.date
);
461 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
462 man
->meta
.msec
= man
->meta
.os
= NULL
;
466 /* ->TITLE<- MSEC DATE OS VOL */
469 if (n
!= NULL
&& n
->string
!= NULL
) {
470 for (p
= n
->string
; *p
!= '\0'; p
++) {
471 /* Only warn about this once... */
472 if (isalpha((unsigned char)*p
) &&
473 ! isupper((unsigned char)*p
)) {
474 mandoc_msg(MANDOCERR_TITLE_CASE
, n
->line
,
475 n
->pos
+ (int)(p
- n
->string
),
480 man
->meta
.title
= mandoc_strdup(n
->string
);
482 man
->meta
.title
= mandoc_strdup("");
483 mandoc_msg(MANDOCERR_TH_NOTITLE
, nb
->line
, nb
->pos
, "TH");
486 /* TITLE ->MSEC<- DATE OS VOL */
490 if (n
!= NULL
&& n
->string
!= NULL
)
491 man
->meta
.msec
= mandoc_strdup(n
->string
);
493 man
->meta
.msec
= mandoc_strdup("");
494 mandoc_msg(MANDOCERR_MSEC_MISSING
,
495 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
498 /* TITLE MSEC ->DATE<- OS VOL */
502 if (man
->quick
&& n
!= NULL
)
503 man
->meta
.date
= mandoc_strdup("");
505 man
->meta
.date
= mandoc_normdate(n
, nb
);
507 /* TITLE MSEC DATE ->OS<- VOL */
509 if (n
&& (n
= n
->next
))
510 man
->meta
.os
= mandoc_strdup(n
->string
);
511 else if (man
->os_s
!= NULL
)
512 man
->meta
.os
= mandoc_strdup(man
->os_s
);
513 if (man
->meta
.os_e
== MANDOC_OS_OTHER
&& man
->meta
.os
!= NULL
) {
514 if (strstr(man
->meta
.os
, "OpenBSD") != NULL
)
515 man
->meta
.os_e
= MANDOC_OS_OPENBSD
;
516 else if (strstr(man
->meta
.os
, "NetBSD") != NULL
)
517 man
->meta
.os_e
= MANDOC_OS_NETBSD
;
520 /* TITLE MSEC DATE OS ->VOL<- */
521 /* If missing, use the default VOL name for MSEC. */
523 if (n
&& (n
= n
->next
))
524 man
->meta
.vol
= mandoc_strdup(n
->string
);
525 else if ('\0' != man
->meta
.msec
[0] &&
526 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
527 man
->meta
.vol
= mandoc_strdup(p
);
529 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
530 mandoc_msg(MANDOCERR_ARG_EXCESS
,
531 n
->line
, n
->pos
, "TH ... %s", n
->string
);
534 * Remove the `TH' node after we've processed it for our
537 roff_node_delete(man
, man
->last
);
543 static const char * const bsd_versions
[] = {
544 "3rd Berkeley Distribution",
545 "4th Berkeley Distribution",
546 "4.2 Berkeley Distribution",
547 "4.3 Berkeley Distribution",
548 "4.4 Berkeley Distribution",
555 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
559 if (0 == strcmp(s
, "3"))
561 else if (0 == strcmp(s
, "4"))
563 else if (0 == strcmp(s
, "5"))
565 else if (0 == strcmp(s
, "6"))
567 else if (0 == strcmp(s
, "7"))
574 man
->meta
.os
= mandoc_strdup(p
);
580 static const char * const unix_versions
[] = {
584 "System V Release 2",
587 struct roff_node
*nn
;
592 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
593 p
= unix_versions
[0];
596 if (0 == strcmp(s
, "3"))
597 p
= unix_versions
[0];
598 else if (0 == strcmp(s
, "4"))
599 p
= unix_versions
[1];
600 else if (0 == strcmp(s
, "5")) {
603 nn
->type
== ROFFT_TEXT
&&
604 nn
->string
[0] != '\0')
605 p
= unix_versions
[3];
607 p
= unix_versions
[2];
609 p
= unix_versions
[0];
613 man
->meta
.os
= mandoc_strdup(p
);
621 if (n
->parent
->tok
!= MAN_TP
||
622 n
->parent
->type
!= ROFFT_HEAD
||
624 *n
->child
->string
== '+' ||
625 *n
->child
->string
== '-')
627 mandoc_asprintf(&s
, "+%s", n
->child
->string
);
628 free(n
->child
->string
);
629 n
->child
->string
= s
;