]>
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, 2013, 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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>
33 #include "mandoc_aux.h"
35 #include "libmandoc.h"
37 #define CHKARGS struct man *man, struct man_node *n
39 typedef void (*v_check
)(CHKARGS
);
41 static void check_eq0(CHKARGS
);
42 static void check_eq2(CHKARGS
);
43 static void check_le1(CHKARGS
);
44 static void check_le5(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_vs(CHKARGS
);
53 static void post_fi(CHKARGS
);
54 static void post_ft(CHKARGS
);
55 static void post_nf(CHKARGS
);
56 static void post_TH(CHKARGS
);
57 static void post_UC(CHKARGS
);
58 static void post_UR(CHKARGS
);
60 static v_check man_valids
[MAN_MAX
] = {
103 man_valid_post(struct man
*man
)
109 if (n
->flags
& MAN_VALID
)
111 n
->flags
|= MAN_VALID
;
125 cp
= man_valids
+ n
->tok
;
136 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
138 if (NULL
== man
->first
->child
)
139 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
140 n
->line
, n
->pos
, NULL
);
142 man
->meta
.hasbody
= 1;
144 if (NULL
== man
->meta
.title
) {
145 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
146 n
->line
, n
->pos
, NULL
);
149 * If a title hasn't been set, do so now (by
150 * implication, date and section also aren't set).
153 man
->meta
.title
= mandoc_strdup("");
154 man
->meta
.msec
= mandoc_strdup("");
155 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
156 mandoc_normdate(man
->parse
, NULL
, n
->line
, n
->pos
);
165 if (MAN_LITERAL
& man
->flags
)
169 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
170 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
171 n
->line
, n
->pos
+ (p
- cp
), NULL
);
174 #define INEQ_DEFINE(x, ineq, name) \
176 check_##name(CHKARGS) \
178 if (n->nchild ineq (x)) \
180 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
181 "line arguments %s %d (have %d)", \
182 #ineq, (x), n->nchild); \
185 INEQ_DEFINE(0, ==, eq0
)
186 INEQ_DEFINE(2, ==, eq2
)
187 INEQ_DEFINE(1, <=, le1
)
188 INEQ_DEFINE(5, <=, le5
)
194 if (MAN_HEAD
== n
->type
&& 1 != n
->nchild
)
195 mandoc_vmsg(MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
196 n
->pos
, "line arguments eq 1 (have %d)", n
->nchild
);
210 cp
= n
->child
->string
;
229 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
233 if ('W' == cp
[1] && '\0' == cp
[2])
241 mandoc_vmsg(MANDOCERR_FT_BAD
, man
->parse
,
242 n
->line
, n
->pos
, "ft %s", cp
);
247 mandoc_vmsg(MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
248 n
->pos
, "want one child (have %d)", n
->nchild
);
255 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
256 mandoc_msg(MANDOCERR_ARGCWARN
, man
->parse
, n
->line
,
257 n
->pos
, "want children (have none)");
266 if (0 == n
->body
->nchild
)
267 man_node_delete(man
, n
);
271 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
272 man
->parse
, n
->line
, n
->pos
,
273 "%s empty", man_macronames
[n
->tok
]);
277 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
278 man
->parse
, n
->line
, n
->pos
,
279 "%s %s%s", man_macronames
[n
->tok
],
281 n
->nchild
> 1 ? " ..." : "");
294 if (0 == n
->head
->nchild
&& 0 == n
->body
->nchild
)
295 man_node_delete(man
, n
);
298 if (0 == n
->parent
->head
->nchild
&& 0 == n
->nchild
)
299 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
300 man
->parse
, n
->line
, n
->pos
,
301 "%s empty", man_macronames
[n
->tok
]);
316 free(man
->meta
.title
);
318 free(man
->meta
.source
);
319 free(man
->meta
.msec
);
320 free(man
->meta
.date
);
322 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
323 man
->meta
.msec
= man
->meta
.source
= NULL
;
327 /* ->TITLE<- MSEC DATE SOURCE VOL */
330 if (n
&& n
->string
) {
331 for (p
= n
->string
; '\0' != *p
; p
++) {
332 /* Only warn about this once... */
333 if (isalpha((unsigned char)*p
) &&
334 ! isupper((unsigned char)*p
)) {
335 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
337 n
->pos
+ (p
- n
->string
),
342 man
->meta
.title
= mandoc_strdup(n
->string
);
344 man
->meta
.title
= mandoc_strdup("");
345 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
346 nb
->line
, nb
->pos
, "TH");
349 /* TITLE ->MSEC<- DATE SOURCE VOL */
354 man
->meta
.msec
= mandoc_strdup(n
->string
);
356 man
->meta
.msec
= mandoc_strdup("");
357 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
358 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
361 /* TITLE MSEC ->DATE<- SOURCE VOL */
365 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
366 man
->meta
.date
= man
->quick
?
367 mandoc_strdup(n
->string
) :
368 mandoc_normdate(man
->parse
, n
->string
,
371 man
->meta
.date
= mandoc_strdup("");
372 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
373 n
? n
->line
: nb
->line
,
374 n
? n
->pos
: nb
->pos
, "TH");
377 /* TITLE MSEC DATE ->SOURCE<- VOL */
379 if (n
&& (n
= n
->next
))
380 man
->meta
.source
= mandoc_strdup(n
->string
);
381 else if (man
->defos
!= NULL
)
382 man
->meta
.source
= mandoc_strdup(man
->defos
);
384 /* TITLE MSEC DATE SOURCE ->VOL<- */
385 /* If missing, use the default VOL name for MSEC. */
387 if (n
&& (n
= n
->next
))
388 man
->meta
.vol
= mandoc_strdup(n
->string
);
389 else if ('\0' != man
->meta
.msec
[0] &&
390 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
391 man
->meta
.vol
= mandoc_strdup(p
);
394 * Remove the `TH' node after we've processed it for our
397 man_node_delete(man
, man
->last
);
406 if (MAN_LITERAL
& man
->flags
)
407 mandoc_msg(MANDOCERR_NF_SKIP
, man
->parse
,
408 n
->line
, n
->pos
, "nf");
410 man
->flags
|= MAN_LITERAL
;
419 if ( ! (MAN_LITERAL
& man
->flags
))
420 mandoc_msg(MANDOCERR_FI_SKIP
, man
->parse
,
421 n
->line
, n
->pos
, "fi");
423 man
->flags
&= ~MAN_LITERAL
;
429 static const char * const bsd_versions
[] = {
430 "3rd Berkeley Distribution",
431 "4th Berkeley Distribution",
432 "4.2 Berkeley Distribution",
433 "4.3 Berkeley Distribution",
434 "4.4 Berkeley Distribution",
441 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
445 if (0 == strcmp(s
, "3"))
447 else if (0 == strcmp(s
, "4"))
449 else if (0 == strcmp(s
, "5"))
451 else if (0 == strcmp(s
, "6"))
453 else if (0 == strcmp(s
, "7"))
459 free(man
->meta
.source
);
460 man
->meta
.source
= mandoc_strdup(p
);
466 static const char * const unix_versions
[] = {
470 "System V Release 2",
478 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
479 p
= unix_versions
[0];
482 if (0 == strcmp(s
, "3"))
483 p
= unix_versions
[0];
484 else if (0 == strcmp(s
, "4"))
485 p
= unix_versions
[1];
486 else if (0 == strcmp(s
, "5")) {
488 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
489 p
= unix_versions
[3];
491 p
= unix_versions
[2];
493 p
= unix_versions
[0];
496 free(man
->meta
.source
);
497 man
->meta
.source
= mandoc_strdup(p
);
504 if (n
->tok
== MAN_br
)
512 switch (n
->parent
->tok
) {
516 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
517 "%s after %s", man_macronames
[n
->tok
],
518 man_macronames
[n
->parent
->tok
]);
522 * Don't warn about this because it occurs in pod2man
523 * and would cause considerable (unfixable) warnage.
525 man_node_delete(man
, n
);