]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.106 2014/08/10 23:54:41 schwarze Exp $ */
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 int (*v_check
)(CHKARGS
);
41 static int check_eq0(CHKARGS
);
42 static int check_eq2(CHKARGS
);
43 static int check_le1(CHKARGS
);
44 static int check_le5(CHKARGS
);
45 static int check_par(CHKARGS
);
46 static int check_part(CHKARGS
);
47 static int check_root(CHKARGS
);
48 static int check_text(CHKARGS
);
50 static int post_AT(CHKARGS
);
51 static int post_IP(CHKARGS
);
52 static int post_vs(CHKARGS
);
53 static int post_fi(CHKARGS
);
54 static int post_ft(CHKARGS
);
55 static int post_nf(CHKARGS
);
56 static int post_TH(CHKARGS
);
57 static int post_UC(CHKARGS
);
58 static int 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
;
116 return(check_text(man
, n
));
118 return(check_root(man
, n
));
124 cp
= man_valids
+ n
->tok
;
125 return(*cp
? (*cp
)(man
, n
) : 1);
133 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
135 if (NULL
== man
->first
->child
)
136 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
137 n
->line
, n
->pos
, NULL
);
139 man
->meta
.hasbody
= 1;
141 if (NULL
== man
->meta
.title
) {
142 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
143 n
->line
, n
->pos
, NULL
);
146 * If a title hasn't been set, do so now (by
147 * implication, date and section also aren't set).
150 man
->meta
.title
= mandoc_strdup("");
151 man
->meta
.msec
= mandoc_strdup("");
152 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
153 mandoc_normdate(man
->parse
, NULL
, n
->line
, n
->pos
);
164 if (MAN_LITERAL
& man
->flags
)
168 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
169 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
170 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); \
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
);
199 return(check_part(man
, n
));
212 cp
= n
->child
->string
;
231 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
235 if ('W' == cp
[1] && '\0' == cp
[2])
243 mandoc_vmsg(MANDOCERR_FT_BAD
, man
->parse
,
244 n
->line
, n
->pos
, "ft %s", cp
);
249 mandoc_vmsg(MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
250 n
->pos
, "want one child (have %d)", n
->nchild
);
259 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
260 mandoc_msg(MANDOCERR_ARGCWARN
, man
->parse
, n
->line
,
261 n
->pos
, "want children (have none)");
272 if (0 == n
->body
->nchild
)
273 man_node_delete(man
, n
);
277 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
278 man
->parse
, n
->line
, n
->pos
,
279 "%s empty", man_macronames
[n
->tok
]);
283 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
284 man
->parse
, n
->line
, n
->pos
,
285 "%s %s%s", man_macronames
[n
->tok
],
287 n
->nchild
> 1 ? " ..." : "");
302 if (0 == n
->head
->nchild
&& 0 == n
->body
->nchild
)
303 man_node_delete(man
, n
);
306 if (0 == n
->parent
->head
->nchild
&& 0 == n
->nchild
)
307 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
308 man
->parse
, n
->line
, n
->pos
,
309 "%s empty", man_macronames
[n
->tok
]);
325 free(man
->meta
.title
);
327 free(man
->meta
.source
);
328 free(man
->meta
.msec
);
329 free(man
->meta
.date
);
331 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
332 man
->meta
.msec
= man
->meta
.source
= NULL
;
336 /* ->TITLE<- MSEC DATE SOURCE VOL */
339 if (n
&& n
->string
) {
340 for (p
= n
->string
; '\0' != *p
; p
++) {
341 /* Only warn about this once... */
342 if (isalpha((unsigned char)*p
) &&
343 ! isupper((unsigned char)*p
)) {
344 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
346 n
->pos
+ (p
- n
->string
),
351 man
->meta
.title
= mandoc_strdup(n
->string
);
353 man
->meta
.title
= mandoc_strdup("");
354 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
355 nb
->line
, nb
->pos
, "TH");
358 /* TITLE ->MSEC<- DATE SOURCE VOL */
363 man
->meta
.msec
= mandoc_strdup(n
->string
);
365 man
->meta
.msec
= mandoc_strdup("");
366 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
367 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
370 /* TITLE MSEC ->DATE<- SOURCE VOL */
374 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
375 man
->meta
.date
= man
->quick
?
376 mandoc_strdup(n
->string
) :
377 mandoc_normdate(man
->parse
, n
->string
,
380 man
->meta
.date
= mandoc_strdup("");
381 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
382 n
? n
->line
: nb
->line
,
383 n
? n
->pos
: nb
->pos
, "TH");
386 /* TITLE MSEC DATE ->SOURCE<- VOL */
388 if (n
&& (n
= n
->next
))
389 man
->meta
.source
= mandoc_strdup(n
->string
);
391 /* TITLE MSEC DATE SOURCE ->VOL<- */
392 /* If missing, use the default VOL name for MSEC. */
394 if (n
&& (n
= n
->next
))
395 man
->meta
.vol
= mandoc_strdup(n
->string
);
396 else if ('\0' != man
->meta
.msec
[0] &&
397 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
398 man
->meta
.vol
= mandoc_strdup(p
);
401 * Remove the `TH' node after we've processed it for our
404 man_node_delete(man
, man
->last
);
414 if (MAN_LITERAL
& man
->flags
)
415 mandoc_msg(MANDOCERR_NF_SKIP
, man
->parse
,
416 n
->line
, n
->pos
, "nf");
418 man
->flags
|= MAN_LITERAL
;
428 if ( ! (MAN_LITERAL
& man
->flags
))
429 mandoc_msg(MANDOCERR_FI_SKIP
, man
->parse
,
430 n
->line
, n
->pos
, "fi");
432 man
->flags
&= ~MAN_LITERAL
;
439 static const char * const bsd_versions
[] = {
440 "3rd Berkeley Distribution",
441 "4th Berkeley Distribution",
442 "4.2 Berkeley Distribution",
443 "4.3 Berkeley Distribution",
444 "4.4 Berkeley Distribution",
451 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
455 if (0 == strcmp(s
, "3"))
457 else if (0 == strcmp(s
, "4"))
459 else if (0 == strcmp(s
, "5"))
461 else if (0 == strcmp(s
, "6"))
463 else if (0 == strcmp(s
, "7"))
469 free(man
->meta
.source
);
470 man
->meta
.source
= mandoc_strdup(p
);
477 static const char * const unix_versions
[] = {
481 "System V Release 2",
489 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
490 p
= unix_versions
[0];
493 if (0 == strcmp(s
, "3"))
494 p
= unix_versions
[0];
495 else if (0 == strcmp(s
, "4"))
496 p
= unix_versions
[1];
497 else if (0 == strcmp(s
, "5")) {
499 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
500 p
= unix_versions
[3];
502 p
= unix_versions
[2];
504 p
= unix_versions
[0];
507 free(man
->meta
.source
);
508 man
->meta
.source
= mandoc_strdup(p
);
516 if (n
->tok
== MAN_br
)
524 switch (n
->parent
->tok
) {
528 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
529 "%s after %s", man_macronames
[n
->tok
],
530 man_macronames
[n
->parent
->tok
]);
534 * Don't warn about this because it occurs in pod2man
535 * and would cause considerable (unfixable) warnage.
537 man_node_delete(man
, n
);