]>
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
] = {
104 man_valid_post(struct man
*man
)
110 if (n
->flags
& MAN_VALID
)
112 n
->flags
|= MAN_VALID
;
126 cp
= man_valids
+ n
->tok
;
137 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
139 if (NULL
== man
->first
->child
)
140 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
141 n
->line
, n
->pos
, NULL
);
143 man
->meta
.hasbody
= 1;
145 if (NULL
== man
->meta
.title
) {
146 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
147 n
->line
, n
->pos
, NULL
);
150 * If a title hasn't been set, do so now (by
151 * implication, date and section also aren't set).
154 man
->meta
.title
= mandoc_strdup("");
155 man
->meta
.msec
= mandoc_strdup("");
156 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
157 mandoc_normdate(man
->parse
, NULL
, n
->line
, n
->pos
);
166 if (MAN_LITERAL
& man
->flags
)
170 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
171 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
172 n
->line
, n
->pos
+ (p
- cp
), NULL
);
175 #define INEQ_DEFINE(x, ineq, name) \
177 check_##name(CHKARGS) \
179 if (n->nchild ineq (x)) \
181 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
182 "line arguments %s %d (have %d)", \
183 #ineq, (x), n->nchild); \
186 INEQ_DEFINE(0, ==, eq0
)
187 INEQ_DEFINE(2, ==, eq2
)
188 INEQ_DEFINE(1, <=, le1
)
189 INEQ_DEFINE(5, <=, le5
)
195 if (MAN_HEAD
== n
->type
&& 1 != n
->nchild
)
196 mandoc_vmsg(MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
197 n
->pos
, "line arguments eq 1 (have %d)", n
->nchild
);
211 cp
= n
->child
->string
;
230 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
234 if ('W' == cp
[1] && '\0' == cp
[2])
242 mandoc_vmsg(MANDOCERR_FT_BAD
, man
->parse
,
243 n
->line
, n
->pos
, "ft %s", cp
);
248 mandoc_vmsg(MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
249 n
->pos
, "want one child (have %d)", n
->nchild
);
256 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
257 mandoc_msg(MANDOCERR_ARGCWARN
, man
->parse
, n
->line
,
258 n
->pos
, "want children (have none)");
267 if (0 == n
->body
->nchild
)
268 man_node_delete(man
, n
);
272 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
273 man
->parse
, n
->line
, n
->pos
,
274 "%s empty", man_macronames
[n
->tok
]);
278 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
279 man
->parse
, n
->line
, n
->pos
,
280 "%s %s%s", man_macronames
[n
->tok
],
282 n
->nchild
> 1 ? " ..." : "");
295 if (0 == n
->head
->nchild
&& 0 == n
->body
->nchild
)
296 man_node_delete(man
, n
);
299 if (0 == n
->parent
->head
->nchild
&& 0 == n
->nchild
)
300 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
301 man
->parse
, n
->line
, n
->pos
,
302 "%s empty", man_macronames
[n
->tok
]);
317 free(man
->meta
.title
);
319 free(man
->meta
.source
);
320 free(man
->meta
.msec
);
321 free(man
->meta
.date
);
323 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
324 man
->meta
.msec
= man
->meta
.source
= NULL
;
328 /* ->TITLE<- MSEC DATE SOURCE VOL */
331 if (n
&& n
->string
) {
332 for (p
= n
->string
; '\0' != *p
; p
++) {
333 /* Only warn about this once... */
334 if (isalpha((unsigned char)*p
) &&
335 ! isupper((unsigned char)*p
)) {
336 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
338 n
->pos
+ (p
- n
->string
),
343 man
->meta
.title
= mandoc_strdup(n
->string
);
345 man
->meta
.title
= mandoc_strdup("");
346 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
347 nb
->line
, nb
->pos
, "TH");
350 /* TITLE ->MSEC<- DATE SOURCE VOL */
355 man
->meta
.msec
= mandoc_strdup(n
->string
);
357 man
->meta
.msec
= mandoc_strdup("");
358 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
359 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
362 /* TITLE MSEC ->DATE<- SOURCE VOL */
366 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
367 man
->meta
.date
= man
->quick
?
368 mandoc_strdup(n
->string
) :
369 mandoc_normdate(man
->parse
, n
->string
,
372 man
->meta
.date
= mandoc_strdup("");
373 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
374 n
? n
->line
: nb
->line
,
375 n
? n
->pos
: nb
->pos
, "TH");
378 /* TITLE MSEC DATE ->SOURCE<- VOL */
380 if (n
&& (n
= n
->next
))
381 man
->meta
.source
= mandoc_strdup(n
->string
);
383 /* TITLE MSEC DATE SOURCE ->VOL<- */
384 /* If missing, use the default VOL name for MSEC. */
386 if (n
&& (n
= n
->next
))
387 man
->meta
.vol
= mandoc_strdup(n
->string
);
388 else if ('\0' != man
->meta
.msec
[0] &&
389 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
390 man
->meta
.vol
= mandoc_strdup(p
);
393 * Remove the `TH' node after we've processed it for our
396 man_node_delete(man
, man
->last
);
405 if (MAN_LITERAL
& man
->flags
)
406 mandoc_msg(MANDOCERR_NF_SKIP
, man
->parse
,
407 n
->line
, n
->pos
, "nf");
409 man
->flags
|= MAN_LITERAL
;
418 if ( ! (MAN_LITERAL
& man
->flags
))
419 mandoc_msg(MANDOCERR_FI_SKIP
, man
->parse
,
420 n
->line
, n
->pos
, "fi");
422 man
->flags
&= ~MAN_LITERAL
;
428 static const char * const bsd_versions
[] = {
429 "3rd Berkeley Distribution",
430 "4th Berkeley Distribution",
431 "4.2 Berkeley Distribution",
432 "4.3 Berkeley Distribution",
433 "4.4 Berkeley Distribution",
440 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
444 if (0 == strcmp(s
, "3"))
446 else if (0 == strcmp(s
, "4"))
448 else if (0 == strcmp(s
, "5"))
450 else if (0 == strcmp(s
, "6"))
452 else if (0 == strcmp(s
, "7"))
458 free(man
->meta
.source
);
459 man
->meta
.source
= mandoc_strdup(p
);
465 static const char * const unix_versions
[] = {
469 "System V Release 2",
477 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
478 p
= unix_versions
[0];
481 if (0 == strcmp(s
, "3"))
482 p
= unix_versions
[0];
483 else if (0 == strcmp(s
, "4"))
484 p
= unix_versions
[1];
485 else if (0 == strcmp(s
, "5")) {
487 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
488 p
= unix_versions
[3];
490 p
= unix_versions
[2];
492 p
= unix_versions
[0];
495 free(man
->meta
.source
);
496 man
->meta
.source
= mandoc_strdup(p
);
503 if (n
->tok
== MAN_br
)
511 switch (n
->parent
->tok
) {
515 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
516 "%s after %s", man_macronames
[n
->tok
],
517 man_macronames
[n
->parent
->tok
]);
521 * Don't warn about this because it occurs in pod2man
522 * and would cause considerable (unfixable) warnage.
524 man_node_delete(man
, n
);