]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.144 2018/12/31 07:08:12 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2018 Ingo Schwarze <schwarze@openbsd.org>
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.
20 #include <sys/types.h>
32 #include "mandoc_aux.h"
36 #include "libmandoc.h"
40 #define CHKARGS struct roff_man *man, struct roff_node *n
42 typedef void (*v_check
)(CHKARGS
);
44 static void check_abort(CHKARGS
);
45 static void check_par(CHKARGS
);
46 static void check_part(CHKARGS
);
47 static void check_root(CHKARGS
);
48 static void check_text(CHKARGS
);
50 static void post_AT(CHKARGS
);
51 static void post_IP(CHKARGS
);
52 static void post_OP(CHKARGS
);
53 static void post_SH(CHKARGS
);
54 static void post_TH(CHKARGS
);
55 static void post_UC(CHKARGS
);
56 static void post_UR(CHKARGS
);
57 static void post_in(CHKARGS
);
59 static const v_check man_valids
[MAN_MAX
- MAN_TH
] = {
100 /* Validate the subtree rooted at man->last. */
102 man_validate(struct roff_man
*man
)
108 * Translate obsolete macros such that later code
109 * does not need to look for them.
123 * Iterate over all children, recursing into each one
124 * in turn, depth-first.
127 man
->last
= man
->last
->child
;
128 while (man
->last
!= NULL
) {
131 man
->last
= man
->last
->child
;
133 man
->last
= man
->last
->next
;
136 /* Finally validate the macro itself. */
139 man
->next
= ROFF_NEXT_SIBLING
;
152 if (n
->tok
< ROFF_MAX
) {
157 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
158 cp
= man_valids
+ (n
->tok
- MAN_TH
);
170 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
172 if (n
->last
== NULL
|| n
->last
->type
== ROFFT_COMMENT
)
173 mandoc_msg(MANDOCERR_DOC_EMPTY
, n
->line
, n
->pos
, NULL
);
175 man
->meta
.hasbody
= 1;
177 if (NULL
== man
->meta
.title
) {
178 mandoc_msg(MANDOCERR_TH_NOTITLE
, n
->line
, n
->pos
, NULL
);
181 * If a title hasn't been set, do so now (by
182 * implication, date and section also aren't set).
185 man
->meta
.title
= mandoc_strdup("");
186 man
->meta
.msec
= mandoc_strdup("");
187 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
188 mandoc_normdate(man
, NULL
, n
->line
, n
->pos
);
191 if (man
->meta
.os_e
&&
192 (man
->meta
.rcsids
& (1 << man
->meta
.os_e
)) == 0)
193 mandoc_msg(MANDOCERR_RCS_MISSING
, 0, 0,
194 man
->meta
.os_e
== MANDOC_OS_OPENBSD
?
195 "(OpenBSD)" : "(NetBSD)");
209 if (man
->flags
& ROFF_NOFILL
)
213 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
214 mandoc_msg(MANDOCERR_FI_TAB
,
215 n
->line
, n
->pos
+ (int)(p
- cp
), NULL
);
222 if (n
->child
== NULL
)
223 mandoc_msg(MANDOCERR_OP_EMPTY
, n
->line
, n
->pos
, "OP");
224 else if (n
->child
->next
!= NULL
&& n
->child
->next
->next
!= NULL
) {
225 n
= n
->child
->next
->next
;
226 mandoc_msg(MANDOCERR_ARG_EXCESS
,
227 n
->line
, n
->pos
, "OP ... %s", n
->string
);
234 struct roff_node
*nc
;
236 if (n
->type
!= ROFFT_BODY
|| (nc
= n
->child
) == NULL
)
239 if (nc
->tok
== MAN_PP
&& nc
->body
->child
!= NULL
) {
240 while (nc
->body
->last
!= NULL
) {
241 man
->next
= ROFF_NEXT_CHILD
;
242 roff_node_relink(man
, nc
->body
->last
);
247 if (nc
->tok
== MAN_PP
|| nc
->tok
== ROFF_sp
|| nc
->tok
== ROFF_br
) {
248 mandoc_msg(MANDOCERR_PAR_SKIP
, nc
->line
, nc
->pos
,
249 "%s after %s", roff_name
[nc
->tok
], roff_name
[n
->tok
]);
250 roff_node_delete(man
, nc
);
254 * Trailing PP is empty, so it is deleted by check_par().
255 * Trailing sp is significant.
258 if ((nc
= n
->last
) != NULL
&& nc
->tok
== ROFF_br
) {
259 mandoc_msg(MANDOCERR_PAR_SKIP
,
260 nc
->line
, nc
->pos
, "%s at the end of %s",
261 roff_name
[nc
->tok
], roff_name
[n
->tok
]);
262 roff_node_delete(man
, nc
);
269 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
270 mandoc_msg(MANDOCERR_UR_NOHEAD
, n
->line
, n
->pos
,
271 "%s", roff_name
[n
->tok
]);
279 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
280 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
,
281 "%s", roff_name
[n
->tok
]);
290 if (n
->body
->child
== NULL
)
291 roff_node_delete(man
, n
);
294 if (n
->child
!= NULL
&&
295 (n
->child
->tok
== ROFF_sp
|| n
->child
->tok
== ROFF_br
)) {
296 mandoc_msg(MANDOCERR_PAR_SKIP
,
297 n
->child
->line
, n
->child
->pos
,
298 "%s after %s", roff_name
[n
->child
->tok
],
300 roff_node_delete(man
, n
->child
);
302 if (n
->child
== NULL
)
303 mandoc_msg(MANDOCERR_PAR_SKIP
, n
->line
, n
->pos
,
304 "%s empty", roff_name
[n
->tok
]);
307 if (n
->child
!= NULL
)
308 mandoc_msg(MANDOCERR_ARG_SKIP
,
309 n
->line
, n
->pos
, "%s %s%s",
310 roff_name
[n
->tok
], n
->child
->string
,
311 n
->child
->next
!= NULL
? " ..." : "");
324 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
325 roff_node_delete(man
, n
);
328 if (n
->parent
->head
->child
== NULL
&& n
->child
== NULL
)
329 mandoc_msg(MANDOCERR_PAR_SKIP
, n
->line
, n
->pos
,
330 "%s empty", roff_name
[n
->tok
]);
340 struct roff_node
*nb
;
343 free(man
->meta
.title
);
346 free(man
->meta
.msec
);
347 free(man
->meta
.date
);
349 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
350 man
->meta
.msec
= man
->meta
.os
= NULL
;
354 /* ->TITLE<- MSEC DATE OS VOL */
357 if (n
&& n
->string
) {
358 for (p
= n
->string
; '\0' != *p
; p
++) {
359 /* Only warn about this once... */
360 if (isalpha((unsigned char)*p
) &&
361 ! isupper((unsigned char)*p
)) {
362 mandoc_msg(MANDOCERR_TITLE_CASE
, n
->line
,
363 n
->pos
+ (int)(p
- n
->string
),
368 man
->meta
.title
= mandoc_strdup(n
->string
);
370 man
->meta
.title
= mandoc_strdup("");
371 mandoc_msg(MANDOCERR_TH_NOTITLE
, nb
->line
, nb
->pos
, "TH");
374 /* TITLE ->MSEC<- DATE OS VOL */
379 man
->meta
.msec
= mandoc_strdup(n
->string
);
381 man
->meta
.msec
= mandoc_strdup("");
382 mandoc_msg(MANDOCERR_MSEC_MISSING
,
383 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
386 /* TITLE MSEC ->DATE<- OS VOL */
390 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
391 man
->meta
.date
= man
->quick
?
392 mandoc_strdup(n
->string
) :
393 mandoc_normdate(man
, n
->string
, n
->line
, n
->pos
);
395 man
->meta
.date
= mandoc_strdup("");
396 mandoc_msg(MANDOCERR_DATE_MISSING
,
397 n
? n
->line
: nb
->line
,
398 n
? n
->pos
: nb
->pos
, "TH");
401 /* TITLE MSEC DATE ->OS<- VOL */
403 if (n
&& (n
= n
->next
))
404 man
->meta
.os
= mandoc_strdup(n
->string
);
405 else if (man
->os_s
!= NULL
)
406 man
->meta
.os
= mandoc_strdup(man
->os_s
);
407 if (man
->meta
.os_e
== MANDOC_OS_OTHER
&& man
->meta
.os
!= NULL
) {
408 if (strstr(man
->meta
.os
, "OpenBSD") != NULL
)
409 man
->meta
.os_e
= MANDOC_OS_OPENBSD
;
410 else if (strstr(man
->meta
.os
, "NetBSD") != NULL
)
411 man
->meta
.os_e
= MANDOC_OS_NETBSD
;
414 /* TITLE MSEC DATE OS ->VOL<- */
415 /* If missing, use the default VOL name for MSEC. */
417 if (n
&& (n
= n
->next
))
418 man
->meta
.vol
= mandoc_strdup(n
->string
);
419 else if ('\0' != man
->meta
.msec
[0] &&
420 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
421 man
->meta
.vol
= mandoc_strdup(p
);
423 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
424 mandoc_msg(MANDOCERR_ARG_EXCESS
,
425 n
->line
, n
->pos
, "TH ... %s", n
->string
);
428 * Remove the `TH' node after we've processed it for our
431 roff_node_delete(man
, man
->last
);
437 static const char * const bsd_versions
[] = {
438 "3rd Berkeley Distribution",
439 "4th Berkeley Distribution",
440 "4.2 Berkeley Distribution",
441 "4.3 Berkeley Distribution",
442 "4.4 Berkeley Distribution",
449 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
453 if (0 == strcmp(s
, "3"))
455 else if (0 == strcmp(s
, "4"))
457 else if (0 == strcmp(s
, "5"))
459 else if (0 == strcmp(s
, "6"))
461 else if (0 == strcmp(s
, "7"))
468 man
->meta
.os
= mandoc_strdup(p
);
474 static const char * const unix_versions
[] = {
478 "System V Release 2",
481 struct roff_node
*nn
;
486 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
487 p
= unix_versions
[0];
490 if (0 == strcmp(s
, "3"))
491 p
= unix_versions
[0];
492 else if (0 == strcmp(s
, "4"))
493 p
= unix_versions
[1];
494 else if (0 == strcmp(s
, "5")) {
497 nn
->type
== ROFFT_TEXT
&&
498 nn
->string
[0] != '\0')
499 p
= unix_versions
[3];
501 p
= unix_versions
[2];
503 p
= unix_versions
[0];
507 man
->meta
.os
= mandoc_strdup(p
);
515 if (n
->parent
->tok
!= MAN_TP
||
516 n
->parent
->type
!= ROFFT_HEAD
||
518 *n
->child
->string
== '+' ||
519 *n
->child
->string
== '-')
521 mandoc_asprintf(&s
, "+%s", n
->child
->string
);
522 free(n
->child
->string
);
523 n
->child
->string
= s
;