]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.142 2018/12/16 00:21:05 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
] = {
102 /* Validate the subtree rooted at man->last. */
104 man_node_validate(struct roff_man
*man
)
110 * Translate obsolete macros such that later code
111 * does not need to look for them.
125 * Iterate over all children, recursing into each one
126 * in turn, depth-first.
129 man
->last
= man
->last
->child
;
130 while (man
->last
!= NULL
) {
131 man_node_validate(man
);
133 man
->last
= man
->last
->child
;
135 man
->last
= man
->last
->next
;
138 /* Finally validate the macro itself. */
141 man
->next
= ROFF_NEXT_SIBLING
;
154 if (n
->tok
< ROFF_MAX
) {
158 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
159 cp
= man_valids
+ (n
->tok
- MAN_TH
);
171 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
173 if (n
->last
== NULL
|| n
->last
->type
== ROFFT_COMMENT
)
174 mandoc_msg(MANDOCERR_DOC_EMPTY
, n
->line
, n
->pos
, NULL
);
176 man
->meta
.hasbody
= 1;
178 if (NULL
== man
->meta
.title
) {
179 mandoc_msg(MANDOCERR_TH_NOTITLE
, n
->line
, n
->pos
, NULL
);
182 * If a title hasn't been set, do so now (by
183 * implication, date and section also aren't set).
186 man
->meta
.title
= mandoc_strdup("");
187 man
->meta
.msec
= mandoc_strdup("");
188 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
189 mandoc_normdate(man
, NULL
, n
->line
, n
->pos
);
192 if (man
->meta
.os_e
&&
193 (man
->meta
.rcsids
& (1 << man
->meta
.os_e
)) == 0)
194 mandoc_msg(MANDOCERR_RCS_MISSING
, 0, 0,
195 man
->meta
.os_e
== MANDOC_OS_OPENBSD
?
196 "(OpenBSD)" : "(NetBSD)");
210 if (MAN_LITERAL
& man
->flags
)
214 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
215 mandoc_msg(MANDOCERR_FI_TAB
,
216 n
->line
, n
->pos
+ (int)(p
- cp
), NULL
);
223 if (n
->child
== NULL
)
224 mandoc_msg(MANDOCERR_OP_EMPTY
, n
->line
, n
->pos
, "OP");
225 else if (n
->child
->next
!= NULL
&& n
->child
->next
->next
!= NULL
) {
226 n
= n
->child
->next
->next
;
227 mandoc_msg(MANDOCERR_ARG_EXCESS
,
228 n
->line
, n
->pos
, "OP ... %s", n
->string
);
235 struct roff_node
*nc
;
237 if (n
->type
!= ROFFT_BODY
|| (nc
= n
->child
) == NULL
)
240 if (nc
->tok
== MAN_PP
&& nc
->body
->child
!= NULL
) {
241 while (nc
->body
->last
!= NULL
) {
242 man
->next
= ROFF_NEXT_CHILD
;
243 roff_node_relink(man
, nc
->body
->last
);
248 if (nc
->tok
== MAN_PP
|| nc
->tok
== ROFF_sp
|| nc
->tok
== ROFF_br
) {
249 mandoc_msg(MANDOCERR_PAR_SKIP
, nc
->line
, nc
->pos
,
250 "%s after %s", roff_name
[nc
->tok
], roff_name
[n
->tok
]);
251 roff_node_delete(man
, nc
);
255 * Trailing PP is empty, so it is deleted by check_par().
256 * Trailing sp is significant.
259 if ((nc
= n
->last
) != NULL
&& nc
->tok
== ROFF_br
) {
260 mandoc_msg(MANDOCERR_PAR_SKIP
,
261 nc
->line
, nc
->pos
, "%s at the end of %s",
262 roff_name
[nc
->tok
], roff_name
[n
->tok
]);
263 roff_node_delete(man
, nc
);
270 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
271 mandoc_msg(MANDOCERR_UR_NOHEAD
, n
->line
, n
->pos
,
272 "%s", roff_name
[n
->tok
]);
280 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
281 mandoc_msg(MANDOCERR_BLK_EMPTY
, n
->line
, n
->pos
,
282 "%s", roff_name
[n
->tok
]);
291 if (n
->body
->child
== NULL
)
292 roff_node_delete(man
, n
);
295 if (n
->child
!= NULL
&&
296 (n
->child
->tok
== ROFF_sp
|| n
->child
->tok
== ROFF_br
)) {
297 mandoc_msg(MANDOCERR_PAR_SKIP
,
298 n
->child
->line
, n
->child
->pos
,
299 "%s after %s", roff_name
[n
->child
->tok
],
301 roff_node_delete(man
, n
->child
);
303 if (n
->child
== NULL
)
304 mandoc_msg(MANDOCERR_PAR_SKIP
, n
->line
, n
->pos
,
305 "%s empty", roff_name
[n
->tok
]);
308 if (n
->child
!= NULL
)
309 mandoc_msg(MANDOCERR_ARG_SKIP
,
310 n
->line
, n
->pos
, "%s %s%s",
311 roff_name
[n
->tok
], n
->child
->string
,
312 n
->child
->next
!= NULL
? " ..." : "");
325 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
326 roff_node_delete(man
, n
);
329 if (n
->parent
->head
->child
== NULL
&& n
->child
== NULL
)
330 mandoc_msg(MANDOCERR_PAR_SKIP
, n
->line
, n
->pos
,
331 "%s empty", roff_name
[n
->tok
]);
341 struct roff_node
*nb
;
344 free(man
->meta
.title
);
347 free(man
->meta
.msec
);
348 free(man
->meta
.date
);
350 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
351 man
->meta
.msec
= man
->meta
.os
= NULL
;
355 /* ->TITLE<- MSEC DATE OS VOL */
358 if (n
&& n
->string
) {
359 for (p
= n
->string
; '\0' != *p
; p
++) {
360 /* Only warn about this once... */
361 if (isalpha((unsigned char)*p
) &&
362 ! isupper((unsigned char)*p
)) {
363 mandoc_msg(MANDOCERR_TITLE_CASE
, n
->line
,
364 n
->pos
+ (int)(p
- n
->string
),
369 man
->meta
.title
= mandoc_strdup(n
->string
);
371 man
->meta
.title
= mandoc_strdup("");
372 mandoc_msg(MANDOCERR_TH_NOTITLE
, nb
->line
, nb
->pos
, "TH");
375 /* TITLE ->MSEC<- DATE OS VOL */
380 man
->meta
.msec
= mandoc_strdup(n
->string
);
382 man
->meta
.msec
= mandoc_strdup("");
383 mandoc_msg(MANDOCERR_MSEC_MISSING
,
384 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
387 /* TITLE MSEC ->DATE<- OS VOL */
391 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
392 man
->meta
.date
= man
->quick
?
393 mandoc_strdup(n
->string
) :
394 mandoc_normdate(man
, n
->string
, n
->line
, n
->pos
);
396 man
->meta
.date
= mandoc_strdup("");
397 mandoc_msg(MANDOCERR_DATE_MISSING
,
398 n
? n
->line
: nb
->line
,
399 n
? n
->pos
: nb
->pos
, "TH");
402 /* TITLE MSEC DATE ->OS<- VOL */
404 if (n
&& (n
= n
->next
))
405 man
->meta
.os
= mandoc_strdup(n
->string
);
406 else if (man
->os_s
!= NULL
)
407 man
->meta
.os
= mandoc_strdup(man
->os_s
);
408 if (man
->meta
.os_e
== MANDOC_OS_OTHER
&& man
->meta
.os
!= NULL
) {
409 if (strstr(man
->meta
.os
, "OpenBSD") != NULL
)
410 man
->meta
.os_e
= MANDOC_OS_OPENBSD
;
411 else if (strstr(man
->meta
.os
, "NetBSD") != NULL
)
412 man
->meta
.os_e
= MANDOC_OS_NETBSD
;
415 /* TITLE MSEC DATE OS ->VOL<- */
416 /* If missing, use the default VOL name for MSEC. */
418 if (n
&& (n
= n
->next
))
419 man
->meta
.vol
= mandoc_strdup(n
->string
);
420 else if ('\0' != man
->meta
.msec
[0] &&
421 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
422 man
->meta
.vol
= mandoc_strdup(p
);
424 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
425 mandoc_msg(MANDOCERR_ARG_EXCESS
,
426 n
->line
, n
->pos
, "TH ... %s", n
->string
);
429 * Remove the `TH' node after we've processed it for our
432 roff_node_delete(man
, man
->last
);
438 static const char * const bsd_versions
[] = {
439 "3rd Berkeley Distribution",
440 "4th Berkeley Distribution",
441 "4.2 Berkeley Distribution",
442 "4.3 Berkeley Distribution",
443 "4.4 Berkeley Distribution",
450 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
454 if (0 == strcmp(s
, "3"))
456 else if (0 == strcmp(s
, "4"))
458 else if (0 == strcmp(s
, "5"))
460 else if (0 == strcmp(s
, "6"))
462 else if (0 == strcmp(s
, "7"))
469 man
->meta
.os
= mandoc_strdup(p
);
475 static const char * const unix_versions
[] = {
479 "System V Release 2",
482 struct roff_node
*nn
;
487 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
488 p
= unix_versions
[0];
491 if (0 == strcmp(s
, "3"))
492 p
= unix_versions
[0];
493 else if (0 == strcmp(s
, "4"))
494 p
= unix_versions
[1];
495 else if (0 == strcmp(s
, "5")) {
498 nn
->type
== ROFFT_TEXT
&&
499 nn
->string
[0] != '\0')
500 p
= unix_versions
[3];
502 p
= unix_versions
[2];
504 p
= unix_versions
[0];
508 man
->meta
.os
= mandoc_strdup(p
);
516 if (n
->parent
->tok
!= MAN_TP
||
517 n
->parent
->type
!= ROFFT_HEAD
||
519 *n
->child
->string
== '+' ||
520 *n
->child
->string
== '-')
522 mandoc_asprintf(&s
, "+%s", n
->child
->string
);
523 free(n
->child
->string
);
524 n
->child
->string
= s
;