]>
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"
38 #define CHKARGS struct man *man, struct roff_node *n
40 typedef void (*v_check
)(CHKARGS
);
42 static void check_par(CHKARGS
);
43 static void check_part(CHKARGS
);
44 static void check_root(CHKARGS
);
45 static void check_text(CHKARGS
);
47 static void post_AT(CHKARGS
);
48 static void post_IP(CHKARGS
);
49 static void post_vs(CHKARGS
);
50 static void post_fi(CHKARGS
);
51 static void post_ft(CHKARGS
);
52 static void post_nf(CHKARGS
);
53 static void post_OP(CHKARGS
);
54 static void post_TH(CHKARGS
);
55 static void post_UC(CHKARGS
);
56 static void post_UR(CHKARGS
);
58 static v_check man_valids
[MAN_MAX
] = {
101 man_valid_post(struct man
*man
)
107 if (n
->flags
& MAN_VALID
)
109 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
;
225 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
229 if ('W' == cp
[1] && '\0' == cp
[2])
237 mandoc_vmsg(MANDOCERR_FT_BAD
, man
->parse
,
238 n
->line
, n
->pos
, "ft %s", cp
);
247 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
248 mandoc_msg(MANDOCERR_BLK_EMPTY
, man
->parse
,
249 n
->line
, n
->pos
, man_macronames
[n
->tok
]);
258 if (0 == n
->body
->nchild
)
259 man_node_delete(man
, n
);
263 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
264 man
->parse
, n
->line
, n
->pos
,
265 "%s empty", man_macronames
[n
->tok
]);
269 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
270 man
->parse
, n
->line
, n
->pos
,
271 "%s %s%s", man_macronames
[n
->tok
],
273 n
->nchild
> 1 ? " ..." : "");
286 if (0 == n
->head
->nchild
&& 0 == n
->body
->nchild
)
287 man_node_delete(man
, n
);
290 if (0 == n
->parent
->head
->nchild
&& 0 == n
->nchild
)
291 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
292 man
->parse
, n
->line
, n
->pos
,
293 "%s empty", man_macronames
[n
->tok
]);
303 struct roff_node
*nb
;
306 free(man
->meta
.title
);
308 free(man
->meta
.source
);
309 free(man
->meta
.msec
);
310 free(man
->meta
.date
);
312 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
313 man
->meta
.msec
= man
->meta
.source
= NULL
;
317 /* ->TITLE<- MSEC DATE SOURCE VOL */
320 if (n
&& n
->string
) {
321 for (p
= n
->string
; '\0' != *p
; p
++) {
322 /* Only warn about this once... */
323 if (isalpha((unsigned char)*p
) &&
324 ! isupper((unsigned char)*p
)) {
325 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
327 n
->pos
+ (p
- n
->string
),
332 man
->meta
.title
= mandoc_strdup(n
->string
);
334 man
->meta
.title
= mandoc_strdup("");
335 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
336 nb
->line
, nb
->pos
, "TH");
339 /* TITLE ->MSEC<- DATE SOURCE VOL */
344 man
->meta
.msec
= mandoc_strdup(n
->string
);
346 man
->meta
.msec
= mandoc_strdup("");
347 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
348 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
351 /* TITLE MSEC ->DATE<- SOURCE VOL */
355 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
356 man
->meta
.date
= man
->quick
?
357 mandoc_strdup(n
->string
) :
358 mandoc_normdate(man
->parse
, n
->string
,
361 man
->meta
.date
= mandoc_strdup("");
362 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
363 n
? n
->line
: nb
->line
,
364 n
? n
->pos
: nb
->pos
, "TH");
367 /* TITLE MSEC DATE ->SOURCE<- VOL */
369 if (n
&& (n
= n
->next
))
370 man
->meta
.source
= mandoc_strdup(n
->string
);
371 else if (man
->defos
!= NULL
)
372 man
->meta
.source
= mandoc_strdup(man
->defos
);
374 /* TITLE MSEC DATE SOURCE ->VOL<- */
375 /* If missing, use the default VOL name for MSEC. */
377 if (n
&& (n
= n
->next
))
378 man
->meta
.vol
= mandoc_strdup(n
->string
);
379 else if ('\0' != man
->meta
.msec
[0] &&
380 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
381 man
->meta
.vol
= mandoc_strdup(p
);
383 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
384 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
385 n
->line
, n
->pos
, "TH ... %s", n
->string
);
388 * Remove the `TH' node after we've processed it for our
391 man_node_delete(man
, man
->last
);
398 if (man
->flags
& MAN_LITERAL
)
399 mandoc_msg(MANDOCERR_NF_SKIP
, man
->parse
,
400 n
->line
, n
->pos
, "nf");
402 man
->flags
|= MAN_LITERAL
;
409 if ( ! (MAN_LITERAL
& man
->flags
))
410 mandoc_msg(MANDOCERR_FI_SKIP
, man
->parse
,
411 n
->line
, n
->pos
, "fi");
413 man
->flags
&= ~MAN_LITERAL
;
419 static const char * const bsd_versions
[] = {
420 "3rd Berkeley Distribution",
421 "4th Berkeley Distribution",
422 "4.2 Berkeley Distribution",
423 "4.3 Berkeley Distribution",
424 "4.4 Berkeley Distribution",
431 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
435 if (0 == strcmp(s
, "3"))
437 else if (0 == strcmp(s
, "4"))
439 else if (0 == strcmp(s
, "5"))
441 else if (0 == strcmp(s
, "6"))
443 else if (0 == strcmp(s
, "7"))
449 free(man
->meta
.source
);
450 man
->meta
.source
= mandoc_strdup(p
);
456 static const char * const unix_versions
[] = {
460 "System V Release 2",
463 struct roff_node
*nn
;
468 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
469 p
= unix_versions
[0];
472 if (0 == strcmp(s
, "3"))
473 p
= unix_versions
[0];
474 else if (0 == strcmp(s
, "4"))
475 p
= unix_versions
[1];
476 else if (0 == strcmp(s
, "5")) {
479 nn
->type
== ROFFT_TEXT
&&
480 nn
->string
[0] != '\0')
481 p
= unix_versions
[3];
483 p
= unix_versions
[2];
485 p
= unix_versions
[0];
488 free(man
->meta
.source
);
489 man
->meta
.source
= mandoc_strdup(p
);
499 switch (n
->parent
->tok
) {
503 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
504 "%s after %s", man_macronames
[n
->tok
],
505 man_macronames
[n
->parent
->tok
]);
509 * Don't warn about this because it occurs in pod2man
510 * and would cause considerable (unfixable) warnage.
512 man_node_delete(man
, n
);