]>
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-2015 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_vs(CHKARGS
);
51 static void post_fi(CHKARGS
);
52 static void post_ft(CHKARGS
);
53 static void post_nf(CHKARGS
);
54 static void post_OP(CHKARGS
);
55 static void post_TH(CHKARGS
);
56 static void post_UC(CHKARGS
);
57 static void post_UR(CHKARGS
);
59 static v_check man_valids
[MAN_MAX
] = {
102 man_valid_post(struct roff_man
*man
)
108 if (n
->flags
& MAN_VALID
)
110 n
->flags
|= MAN_VALID
;
123 cp
= man_valids
+ n
->tok
;
134 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
136 if (NULL
== man
->first
->child
)
137 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
138 n
->line
, n
->pos
, NULL
);
140 man
->meta
.hasbody
= 1;
142 if (NULL
== man
->meta
.title
) {
143 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
144 n
->line
, n
->pos
, NULL
);
147 * If a title hasn't been set, do so now (by
148 * implication, date and section also aren't set).
151 man
->meta
.title
= mandoc_strdup("");
152 man
->meta
.msec
= mandoc_strdup("");
153 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
154 mandoc_normdate(man
->parse
, NULL
, n
->line
, n
->pos
);
163 if (MAN_LITERAL
& man
->flags
)
167 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
168 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
169 n
->line
, n
->pos
+ (p
- cp
), NULL
);
177 mandoc_msg(MANDOCERR_OP_EMPTY
, man
->parse
,
178 n
->line
, n
->pos
, "OP");
179 else if (n
->nchild
> 2) {
180 n
= n
->child
->next
->next
;
181 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
182 n
->line
, n
->pos
, "OP ... %s", n
->string
);
190 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
191 mandoc_vmsg(MANDOCERR_UR_NOHEAD
, man
->parse
,
192 n
->line
, n
->pos
, "UR");
206 cp
= n
->child
->string
;
219 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
223 if ('W' == cp
[1] && '\0' == cp
[2])
231 mandoc_vmsg(MANDOCERR_FT_BAD
, man
->parse
,
232 n
->line
, n
->pos
, "ft %s", cp
);
241 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
242 mandoc_msg(MANDOCERR_BLK_EMPTY
, man
->parse
,
243 n
->line
, n
->pos
, man_macronames
[n
->tok
]);
252 if (0 == n
->body
->nchild
)
253 roff_node_delete(man
, n
);
257 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
258 man
->parse
, n
->line
, n
->pos
,
259 "%s empty", man_macronames
[n
->tok
]);
263 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
264 man
->parse
, n
->line
, n
->pos
,
265 "%s %s%s", man_macronames
[n
->tok
],
267 n
->nchild
> 1 ? " ..." : "");
280 if (0 == n
->head
->nchild
&& 0 == n
->body
->nchild
)
281 roff_node_delete(man
, n
);
284 if (0 == n
->parent
->head
->nchild
&& 0 == n
->nchild
)
285 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
286 man
->parse
, n
->line
, n
->pos
,
287 "%s empty", man_macronames
[n
->tok
]);
297 struct roff_node
*nb
;
300 free(man
->meta
.title
);
303 free(man
->meta
.msec
);
304 free(man
->meta
.date
);
306 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
307 man
->meta
.msec
= man
->meta
.os
= NULL
;
311 /* ->TITLE<- MSEC DATE OS VOL */
314 if (n
&& n
->string
) {
315 for (p
= n
->string
; '\0' != *p
; p
++) {
316 /* Only warn about this once... */
317 if (isalpha((unsigned char)*p
) &&
318 ! isupper((unsigned char)*p
)) {
319 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
321 n
->pos
+ (p
- n
->string
),
326 man
->meta
.title
= mandoc_strdup(n
->string
);
328 man
->meta
.title
= mandoc_strdup("");
329 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
330 nb
->line
, nb
->pos
, "TH");
333 /* TITLE ->MSEC<- DATE OS VOL */
338 man
->meta
.msec
= mandoc_strdup(n
->string
);
340 man
->meta
.msec
= mandoc_strdup("");
341 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
342 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
345 /* TITLE MSEC ->DATE<- OS VOL */
349 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
350 man
->meta
.date
= man
->quick
?
351 mandoc_strdup(n
->string
) :
352 mandoc_normdate(man
->parse
, n
->string
,
355 man
->meta
.date
= mandoc_strdup("");
356 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
357 n
? n
->line
: nb
->line
,
358 n
? n
->pos
: nb
->pos
, "TH");
361 /* TITLE MSEC DATE ->OS<- VOL */
363 if (n
&& (n
= n
->next
))
364 man
->meta
.os
= mandoc_strdup(n
->string
);
365 else if (man
->defos
!= NULL
)
366 man
->meta
.os
= mandoc_strdup(man
->defos
);
368 /* TITLE MSEC DATE OS ->VOL<- */
369 /* If missing, use the default VOL name for MSEC. */
371 if (n
&& (n
= n
->next
))
372 man
->meta
.vol
= mandoc_strdup(n
->string
);
373 else if ('\0' != man
->meta
.msec
[0] &&
374 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
375 man
->meta
.vol
= mandoc_strdup(p
);
377 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
378 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
379 n
->line
, n
->pos
, "TH ... %s", n
->string
);
382 * Remove the `TH' node after we've processed it for our
385 roff_node_delete(man
, man
->last
);
392 if (man
->flags
& MAN_LITERAL
)
393 mandoc_msg(MANDOCERR_NF_SKIP
, man
->parse
,
394 n
->line
, n
->pos
, "nf");
396 man
->flags
|= MAN_LITERAL
;
403 if ( ! (MAN_LITERAL
& man
->flags
))
404 mandoc_msg(MANDOCERR_FI_SKIP
, man
->parse
,
405 n
->line
, n
->pos
, "fi");
407 man
->flags
&= ~MAN_LITERAL
;
413 static const char * const bsd_versions
[] = {
414 "3rd Berkeley Distribution",
415 "4th Berkeley Distribution",
416 "4.2 Berkeley Distribution",
417 "4.3 Berkeley Distribution",
418 "4.4 Berkeley Distribution",
425 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
429 if (0 == strcmp(s
, "3"))
431 else if (0 == strcmp(s
, "4"))
433 else if (0 == strcmp(s
, "5"))
435 else if (0 == strcmp(s
, "6"))
437 else if (0 == strcmp(s
, "7"))
444 man
->meta
.os
= mandoc_strdup(p
);
450 static const char * const unix_versions
[] = {
454 "System V Release 2",
457 struct roff_node
*nn
;
462 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
463 p
= unix_versions
[0];
466 if (0 == strcmp(s
, "3"))
467 p
= unix_versions
[0];
468 else if (0 == strcmp(s
, "4"))
469 p
= unix_versions
[1];
470 else if (0 == strcmp(s
, "5")) {
473 nn
->type
== ROFFT_TEXT
&&
474 nn
->string
[0] != '\0')
475 p
= unix_versions
[3];
477 p
= unix_versions
[2];
479 p
= unix_versions
[0];
483 man
->meta
.os
= mandoc_strdup(p
);
493 switch (n
->parent
->tok
) {
496 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
497 "%s after %s", man_macronames
[n
->tok
],
498 man_macronames
[n
->parent
->tok
]);
502 * Don't warn about this because it occurs in pod2man
503 * and would cause considerable (unfixable) warnage.
505 roff_node_delete(man
, n
);