]>
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_par(CHKARGS
);
44 static void check_part(CHKARGS
);
45 static void check_root(CHKARGS
);
46 static void check_text(CHKARGS
);
48 static void post_AT(CHKARGS
);
49 static void post_IP(CHKARGS
);
50 static void post_OP(CHKARGS
);
51 static void post_TH(CHKARGS
);
52 static void post_UC(CHKARGS
);
53 static void post_UR(CHKARGS
);
54 static void post_in(CHKARGS
);
55 static void post_vs(CHKARGS
);
57 static const v_check man_valids
[MAN_MAX
- MAN_TH
] = {
101 man_node_validate(struct roff_man
*man
)
107 man
->last
= man
->last
->child
;
108 while (man
->last
!= NULL
) {
109 man_node_validate(man
);
111 man
->last
= man
->last
->child
;
113 man
->last
= man
->last
->next
;
117 man
->next
= ROFF_NEXT_SIBLING
;
130 if (n
->tok
< ROFF_MAX
) {
142 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
143 cp
= man_valids
+ (n
->tok
- MAN_TH
);
155 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
157 if (n
->last
== NULL
|| n
->last
->type
== ROFFT_COMMENT
)
158 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
159 n
->line
, n
->pos
, NULL
);
161 man
->meta
.hasbody
= 1;
163 if (NULL
== man
->meta
.title
) {
164 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
165 n
->line
, n
->pos
, NULL
);
168 * If a title hasn't been set, do so now (by
169 * implication, date and section also aren't set).
172 man
->meta
.title
= mandoc_strdup("");
173 man
->meta
.msec
= mandoc_strdup("");
174 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
175 mandoc_normdate(man
, NULL
, n
->line
, n
->pos
);
178 if (man
->meta
.os_e
&&
179 (man
->meta
.rcsids
& (1 << man
->meta
.os_e
)) == 0)
180 mandoc_msg(MANDOCERR_RCS_MISSING
, man
->parse
, 0, 0,
181 man
->meta
.os_e
== MANDOC_OS_OPENBSD
?
182 "(OpenBSD)" : "(NetBSD)");
190 if (MAN_LITERAL
& man
->flags
)
194 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
195 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
196 n
->line
, n
->pos
+ (p
- cp
), NULL
);
203 if (n
->child
== NULL
)
204 mandoc_msg(MANDOCERR_OP_EMPTY
, man
->parse
,
205 n
->line
, n
->pos
, "OP");
206 else if (n
->child
->next
!= NULL
&& n
->child
->next
->next
!= NULL
) {
207 n
= n
->child
->next
->next
;
208 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
209 n
->line
, n
->pos
, "OP ... %s", n
->string
);
216 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
217 mandoc_msg(MANDOCERR_UR_NOHEAD
, man
->parse
,
218 n
->line
, n
->pos
, roff_name
[n
->tok
]);
226 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
227 mandoc_msg(MANDOCERR_BLK_EMPTY
, man
->parse
,
228 n
->line
, n
->pos
, roff_name
[n
->tok
]);
237 if (n
->body
->child
== NULL
)
238 roff_node_delete(man
, n
);
241 if (n
->child
== NULL
)
242 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
243 man
->parse
, n
->line
, n
->pos
,
244 "%s empty", roff_name
[n
->tok
]);
247 if (n
->child
!= NULL
)
248 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
249 man
->parse
, n
->line
, n
->pos
, "%s %s%s",
250 roff_name
[n
->tok
], n
->child
->string
,
251 n
->child
->next
!= NULL
? " ..." : "");
264 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
265 roff_node_delete(man
, n
);
268 if (n
->parent
->head
->child
== NULL
&& n
->child
== NULL
)
269 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
270 man
->parse
, n
->line
, n
->pos
,
271 "%s empty", roff_name
[n
->tok
]);
281 struct roff_node
*nb
;
284 free(man
->meta
.title
);
287 free(man
->meta
.msec
);
288 free(man
->meta
.date
);
290 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
291 man
->meta
.msec
= man
->meta
.os
= NULL
;
295 /* ->TITLE<- MSEC DATE OS VOL */
298 if (n
&& n
->string
) {
299 for (p
= n
->string
; '\0' != *p
; p
++) {
300 /* Only warn about this once... */
301 if (isalpha((unsigned char)*p
) &&
302 ! isupper((unsigned char)*p
)) {
303 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
305 n
->pos
+ (p
- n
->string
),
310 man
->meta
.title
= mandoc_strdup(n
->string
);
312 man
->meta
.title
= mandoc_strdup("");
313 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
314 nb
->line
, nb
->pos
, "TH");
317 /* TITLE ->MSEC<- DATE OS VOL */
322 man
->meta
.msec
= mandoc_strdup(n
->string
);
324 man
->meta
.msec
= mandoc_strdup("");
325 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
326 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
329 /* TITLE MSEC ->DATE<- OS VOL */
333 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
334 man
->meta
.date
= man
->quick
?
335 mandoc_strdup(n
->string
) :
336 mandoc_normdate(man
, n
->string
, n
->line
, n
->pos
);
338 man
->meta
.date
= mandoc_strdup("");
339 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
340 n
? n
->line
: nb
->line
,
341 n
? n
->pos
: nb
->pos
, "TH");
344 /* TITLE MSEC DATE ->OS<- VOL */
346 if (n
&& (n
= n
->next
))
347 man
->meta
.os
= mandoc_strdup(n
->string
);
348 else if (man
->os_s
!= NULL
)
349 man
->meta
.os
= mandoc_strdup(man
->os_s
);
350 if (man
->meta
.os_e
== MANDOC_OS_OTHER
&& man
->meta
.os
!= NULL
) {
351 if (strstr(man
->meta
.os
, "OpenBSD") != NULL
)
352 man
->meta
.os_e
= MANDOC_OS_OPENBSD
;
353 else if (strstr(man
->meta
.os
, "NetBSD") != NULL
)
354 man
->meta
.os_e
= MANDOC_OS_NETBSD
;
357 /* TITLE MSEC DATE OS ->VOL<- */
358 /* If missing, use the default VOL name for MSEC. */
360 if (n
&& (n
= n
->next
))
361 man
->meta
.vol
= mandoc_strdup(n
->string
);
362 else if ('\0' != man
->meta
.msec
[0] &&
363 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
364 man
->meta
.vol
= mandoc_strdup(p
);
366 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
367 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
368 n
->line
, n
->pos
, "TH ... %s", n
->string
);
371 * Remove the `TH' node after we've processed it for our
374 roff_node_delete(man
, man
->last
);
380 static const char * const bsd_versions
[] = {
381 "3rd Berkeley Distribution",
382 "4th Berkeley Distribution",
383 "4.2 Berkeley Distribution",
384 "4.3 Berkeley Distribution",
385 "4.4 Berkeley Distribution",
392 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
396 if (0 == strcmp(s
, "3"))
398 else if (0 == strcmp(s
, "4"))
400 else if (0 == strcmp(s
, "5"))
402 else if (0 == strcmp(s
, "6"))
404 else if (0 == strcmp(s
, "7"))
411 man
->meta
.os
= mandoc_strdup(p
);
417 static const char * const unix_versions
[] = {
421 "System V Release 2",
424 struct roff_node
*nn
;
429 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
430 p
= unix_versions
[0];
433 if (0 == strcmp(s
, "3"))
434 p
= unix_versions
[0];
435 else if (0 == strcmp(s
, "4"))
436 p
= unix_versions
[1];
437 else if (0 == strcmp(s
, "5")) {
440 nn
->type
== ROFFT_TEXT
&&
441 nn
->string
[0] != '\0')
442 p
= unix_versions
[3];
444 p
= unix_versions
[2];
446 p
= unix_versions
[0];
450 man
->meta
.os
= mandoc_strdup(p
);
458 if (n
->parent
->tok
!= MAN_TP
||
459 n
->parent
->type
!= ROFFT_HEAD
||
461 *n
->child
->string
== '+' ||
462 *n
->child
->string
== '-')
464 mandoc_asprintf(&s
, "+%s", n
->child
->string
);
465 free(n
->child
->string
);
466 n
->child
->string
= s
;
476 switch (n
->parent
->tok
) {
482 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
483 "%s after %s", roff_name
[n
->tok
],
484 roff_name
[n
->parent
->tok
]);
488 * Don't warn about this because it occurs in pod2man
489 * and would cause considerable (unfixable) warnage.
491 roff_node_delete(man
, n
);