]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
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>
31 #include "mandoc_aux.h"
35 #include "libmandoc.h"
39 #define CHKARGS struct roff_man *man, struct roff_node *n
41 typedef void (*v_check
)(CHKARGS
);
43 static void check_abort(CHKARGS
);
44 static void check_par(CHKARGS
);
45 static void check_part(CHKARGS
);
46 static void check_root(CHKARGS
);
47 static void check_text(CHKARGS
);
49 static void post_AT(CHKARGS
);
50 static void post_IP(CHKARGS
);
51 static void post_OP(CHKARGS
);
52 static void post_TH(CHKARGS
);
53 static void post_UC(CHKARGS
);
54 static void post_UR(CHKARGS
);
55 static void post_in(CHKARGS
);
56 static void post_vs(CHKARGS
);
58 static const v_check man_valids
[MAN_MAX
- MAN_TH
] = {
101 /* Validate the subtree rooted at man->last. */
103 man_node_validate(struct roff_man
*man
)
109 * Translate obsolete macros such that later code
110 * does not need to look for them.
124 * Iterate over all children, recursing into each one
125 * in turn, depth-first.
128 man
->last
= man
->last
->child
;
129 while (man
->last
!= NULL
) {
130 man_node_validate(man
);
132 man
->last
= man
->last
->child
;
134 man
->last
= man
->last
->next
;
137 /* Finally validate the macro itself. */
140 man
->next
= ROFF_NEXT_SIBLING
;
153 if (n
->tok
< ROFF_MAX
) {
165 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
166 cp
= man_valids
+ (n
->tok
- MAN_TH
);
178 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
180 if (n
->last
== NULL
|| n
->last
->type
== ROFFT_COMMENT
)
181 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
182 n
->line
, n
->pos
, NULL
);
184 man
->meta
.hasbody
= 1;
186 if (NULL
== man
->meta
.title
) {
187 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
188 n
->line
, n
->pos
, NULL
);
191 * If a title hasn't been set, do so now (by
192 * implication, date and section also aren't set).
195 man
->meta
.title
= mandoc_strdup("");
196 man
->meta
.msec
= mandoc_strdup("");
197 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
198 mandoc_normdate(man
, NULL
, n
->line
, n
->pos
);
201 if (man
->meta
.os_e
&&
202 (man
->meta
.rcsids
& (1 << man
->meta
.os_e
)) == 0)
203 mandoc_msg(MANDOCERR_RCS_MISSING
, man
->parse
, 0, 0,
204 man
->meta
.os_e
== MANDOC_OS_OPENBSD
?
205 "(OpenBSD)" : "(NetBSD)");
219 if (MAN_LITERAL
& man
->flags
)
223 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
224 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
225 n
->line
, n
->pos
+ (p
- cp
), NULL
);
232 if (n
->child
== NULL
)
233 mandoc_msg(MANDOCERR_OP_EMPTY
, man
->parse
,
234 n
->line
, n
->pos
, "OP");
235 else if (n
->child
->next
!= NULL
&& n
->child
->next
->next
!= NULL
) {
236 n
= n
->child
->next
->next
;
237 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
238 n
->line
, n
->pos
, "OP ... %s", n
->string
);
245 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
246 mandoc_msg(MANDOCERR_UR_NOHEAD
, man
->parse
,
247 n
->line
, n
->pos
, roff_name
[n
->tok
]);
255 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
256 mandoc_msg(MANDOCERR_BLK_EMPTY
, man
->parse
,
257 n
->line
, n
->pos
, roff_name
[n
->tok
]);
266 if (n
->body
->child
== NULL
)
267 roff_node_delete(man
, n
);
270 if (n
->child
== NULL
)
271 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
272 man
->parse
, n
->line
, n
->pos
,
273 "%s empty", roff_name
[n
->tok
]);
276 if (n
->child
!= NULL
)
277 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
278 man
->parse
, n
->line
, n
->pos
, "%s %s%s",
279 roff_name
[n
->tok
], n
->child
->string
,
280 n
->child
->next
!= NULL
? " ..." : "");
293 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
294 roff_node_delete(man
, n
);
297 if (n
->parent
->head
->child
== NULL
&& n
->child
== NULL
)
298 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
299 man
->parse
, n
->line
, n
->pos
,
300 "%s empty", roff_name
[n
->tok
]);
310 struct roff_node
*nb
;
313 free(man
->meta
.title
);
316 free(man
->meta
.msec
);
317 free(man
->meta
.date
);
319 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
320 man
->meta
.msec
= man
->meta
.os
= NULL
;
324 /* ->TITLE<- MSEC DATE OS VOL */
327 if (n
&& n
->string
) {
328 for (p
= n
->string
; '\0' != *p
; p
++) {
329 /* Only warn about this once... */
330 if (isalpha((unsigned char)*p
) &&
331 ! isupper((unsigned char)*p
)) {
332 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
334 n
->pos
+ (p
- n
->string
),
339 man
->meta
.title
= mandoc_strdup(n
->string
);
341 man
->meta
.title
= mandoc_strdup("");
342 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
343 nb
->line
, nb
->pos
, "TH");
346 /* TITLE ->MSEC<- DATE OS VOL */
351 man
->meta
.msec
= mandoc_strdup(n
->string
);
353 man
->meta
.msec
= mandoc_strdup("");
354 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
355 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
358 /* TITLE MSEC ->DATE<- OS VOL */
362 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
363 man
->meta
.date
= man
->quick
?
364 mandoc_strdup(n
->string
) :
365 mandoc_normdate(man
, n
->string
, n
->line
, n
->pos
);
367 man
->meta
.date
= mandoc_strdup("");
368 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
369 n
? n
->line
: nb
->line
,
370 n
? n
->pos
: nb
->pos
, "TH");
373 /* TITLE MSEC DATE ->OS<- VOL */
375 if (n
&& (n
= n
->next
))
376 man
->meta
.os
= mandoc_strdup(n
->string
);
377 else if (man
->os_s
!= NULL
)
378 man
->meta
.os
= mandoc_strdup(man
->os_s
);
379 if (man
->meta
.os_e
== MANDOC_OS_OTHER
&& man
->meta
.os
!= NULL
) {
380 if (strstr(man
->meta
.os
, "OpenBSD") != NULL
)
381 man
->meta
.os_e
= MANDOC_OS_OPENBSD
;
382 else if (strstr(man
->meta
.os
, "NetBSD") != NULL
)
383 man
->meta
.os_e
= MANDOC_OS_NETBSD
;
386 /* TITLE MSEC DATE OS ->VOL<- */
387 /* If missing, use the default VOL name for MSEC. */
389 if (n
&& (n
= n
->next
))
390 man
->meta
.vol
= mandoc_strdup(n
->string
);
391 else if ('\0' != man
->meta
.msec
[0] &&
392 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
393 man
->meta
.vol
= mandoc_strdup(p
);
395 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
396 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
397 n
->line
, n
->pos
, "TH ... %s", n
->string
);
400 * Remove the `TH' node after we've processed it for our
403 roff_node_delete(man
, man
->last
);
409 static const char * const bsd_versions
[] = {
410 "3rd Berkeley Distribution",
411 "4th Berkeley Distribution",
412 "4.2 Berkeley Distribution",
413 "4.3 Berkeley Distribution",
414 "4.4 Berkeley Distribution",
421 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
425 if (0 == strcmp(s
, "3"))
427 else if (0 == strcmp(s
, "4"))
429 else if (0 == strcmp(s
, "5"))
431 else if (0 == strcmp(s
, "6"))
433 else if (0 == strcmp(s
, "7"))
440 man
->meta
.os
= mandoc_strdup(p
);
446 static const char * const unix_versions
[] = {
450 "System V Release 2",
453 struct roff_node
*nn
;
458 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
459 p
= unix_versions
[0];
462 if (0 == strcmp(s
, "3"))
463 p
= unix_versions
[0];
464 else if (0 == strcmp(s
, "4"))
465 p
= unix_versions
[1];
466 else if (0 == strcmp(s
, "5")) {
469 nn
->type
== ROFFT_TEXT
&&
470 nn
->string
[0] != '\0')
471 p
= unix_versions
[3];
473 p
= unix_versions
[2];
475 p
= unix_versions
[0];
479 man
->meta
.os
= mandoc_strdup(p
);
487 if (n
->parent
->tok
!= MAN_TP
||
488 n
->parent
->type
!= ROFFT_HEAD
||
490 *n
->child
->string
== '+' ||
491 *n
->child
->string
== '-')
493 mandoc_asprintf(&s
, "+%s", n
->child
->string
);
494 free(n
->child
->string
);
495 n
->child
->string
= s
;
505 switch (n
->parent
->tok
) {
509 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
510 "%s after %s", roff_name
[n
->tok
],
511 roff_name
[n
->parent
->tok
]);
515 * Don't warn about this because it occurs in pod2man
516 * and would cause considerable (unfixable) warnage.
518 roff_node_delete(man
, n
);