]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.104 2014/08/01 21:24:17 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.
22 #include <sys/types.h>
35 #include "mandoc_aux.h"
37 #include "libmandoc.h"
39 #define CHKARGS struct man *man, struct man_node *n
41 typedef int (*v_check
)(CHKARGS
);
43 static int check_eq0(CHKARGS
);
44 static int check_eq2(CHKARGS
);
45 static int check_le1(CHKARGS
);
46 static int check_ge2(CHKARGS
);
47 static int check_le5(CHKARGS
);
48 static int check_par(CHKARGS
);
49 static int check_part(CHKARGS
);
50 static int check_root(CHKARGS
);
51 static int check_text(CHKARGS
);
53 static int post_AT(CHKARGS
);
54 static int post_IP(CHKARGS
);
55 static int post_vs(CHKARGS
);
56 static int post_fi(CHKARGS
);
57 static int post_ft(CHKARGS
);
58 static int post_nf(CHKARGS
);
59 static int post_TH(CHKARGS
);
60 static int post_UC(CHKARGS
);
61 static int post_UR(CHKARGS
);
63 static v_check man_valids
[MAN_MAX
] = {
107 man_valid_post(struct man
*man
)
113 if (n
->flags
& MAN_VALID
)
115 n
->flags
|= MAN_VALID
;
119 return(check_text(man
, n
));
121 return(check_root(man
, n
));
127 cp
= man_valids
+ n
->tok
;
128 return(*cp
? (*cp
)(man
, n
) : 1);
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_MISSING
, 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("unknown");
154 man
->meta
.msec
= mandoc_strdup("1");
155 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
156 mandoc_normdate(man
->parse
, NULL
, n
->line
, n
->pos
);
167 if (MAN_LITERAL
& man
->flags
)
171 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
172 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
173 n
->line
, n
->pos
+ (p
- cp
), NULL
);
177 #define INEQ_DEFINE(x, ineq, name) \
179 check_##name(CHKARGS) \
181 if (n->nchild ineq (x)) \
183 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
184 "line arguments %s %d (have %d)", \
185 #ineq, (x), n->nchild); \
189 INEQ_DEFINE(0, ==, eq0
)
190 INEQ_DEFINE(2, ==, eq2
)
191 INEQ_DEFINE(1, <=, le1
)
192 INEQ_DEFINE(2, >=, ge2
)
193 INEQ_DEFINE(5, <=, le5
)
199 if (MAN_HEAD
== n
->type
&& 1 != n
->nchild
)
200 mandoc_vmsg(MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
201 n
->pos
, "line arguments eq 1 (have %d)", n
->nchild
);
203 return(check_part(man
, n
));
216 cp
= n
->child
->string
;
235 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
239 if ('W' == cp
[1] && '\0' == cp
[2])
247 mandoc_vmsg(MANDOCERR_FT_BAD
, man
->parse
,
248 n
->line
, n
->pos
, "ft %s", cp
);
253 mandoc_vmsg(MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
254 n
->pos
, "want one child (have %d)", n
->nchild
);
263 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
264 mandoc_msg(MANDOCERR_ARGCWARN
, man
->parse
, n
->line
,
265 n
->pos
, "want children (have none)");
276 if (0 == n
->body
->nchild
)
277 man_node_delete(man
, n
);
281 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
282 man
->parse
, n
->line
, n
->pos
,
283 "%s empty", man_macronames
[n
->tok
]);
287 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
288 man
->parse
, n
->line
, n
->pos
,
289 "%s %s%s", man_macronames
[n
->tok
],
291 n
->nchild
> 1 ? " ..." : "");
306 if (0 == n
->head
->nchild
&& 0 == n
->body
->nchild
)
307 man_node_delete(man
, n
);
310 if (0 == n
->parent
->head
->nchild
&& 0 == n
->nchild
)
311 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
312 man
->parse
, n
->line
, n
->pos
,
313 "%s empty", man_macronames
[n
->tok
]);
330 free(man
->meta
.title
);
332 free(man
->meta
.source
);
333 free(man
->meta
.msec
);
334 free(man
->meta
.date
);
336 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
337 man
->meta
.msec
= man
->meta
.source
= NULL
;
341 /* ->TITLE<- MSEC DATE SOURCE VOL */
344 if (n
&& n
->string
) {
345 for (p
= n
->string
; '\0' != *p
; p
++) {
346 /* Only warn about this once... */
347 if (isalpha((unsigned char)*p
) &&
348 ! isupper((unsigned char)*p
)) {
349 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
351 n
->pos
+ (p
- n
->string
),
356 man
->meta
.title
= mandoc_strdup(n
->string
);
358 man
->meta
.title
= mandoc_strdup("");
360 /* TITLE ->MSEC<- DATE SOURCE VOL */
365 man
->meta
.msec
= mandoc_strdup(n
->string
);
367 man
->meta
.msec
= mandoc_strdup("");
369 /* TITLE MSEC ->DATE<- SOURCE VOL */
373 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
374 man
->meta
.date
= man
->quick
?
375 mandoc_strdup(n
->string
) :
376 mandoc_normdate(man
->parse
, n
->string
,
379 man
->meta
.date
= mandoc_strdup("");
380 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
381 n
? n
->line
: nb
->line
,
382 n
? n
->pos
: nb
->pos
, "TH");
385 /* TITLE MSEC DATE ->SOURCE<- VOL */
387 if (n
&& (n
= n
->next
))
388 man
->meta
.source
= mandoc_strdup(n
->string
);
390 /* TITLE MSEC DATE SOURCE ->VOL<- */
391 /* If missing, use the default VOL name for MSEC. */
393 if (n
&& (n
= n
->next
))
394 man
->meta
.vol
= mandoc_strdup(n
->string
);
395 else if ('\0' != man
->meta
.msec
[0] &&
396 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
397 man
->meta
.vol
= mandoc_strdup(p
);
400 * Remove the `TH' node after we've processed it for our
403 man_node_delete(man
, man
->last
);
413 if (MAN_LITERAL
& man
->flags
)
414 mandoc_msg(MANDOCERR_NF_SKIP
, man
->parse
,
415 n
->line
, n
->pos
, "nf");
417 man
->flags
|= MAN_LITERAL
;
427 if ( ! (MAN_LITERAL
& man
->flags
))
428 mandoc_msg(MANDOCERR_FI_SKIP
, man
->parse
,
429 n
->line
, n
->pos
, "fi");
431 man
->flags
&= ~MAN_LITERAL
;
438 static const char * const bsd_versions
[] = {
439 "3rd Berkeley Distribution",
440 "4th Berkeley Distribution",
441 "4.2 Berkeley Distribution",
442 "4.3 Berkeley Distribution",
443 "4.4 Berkeley Distribution",
450 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
454 if (0 == strcmp(s
, "3"))
456 else if (0 == strcmp(s
, "4"))
458 else if (0 == strcmp(s
, "5"))
460 else if (0 == strcmp(s
, "6"))
462 else if (0 == strcmp(s
, "7"))
468 free(man
->meta
.source
);
469 man
->meta
.source
= mandoc_strdup(p
);
476 static const char * const unix_versions
[] = {
480 "System V Release 2",
488 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
489 p
= unix_versions
[0];
492 if (0 == strcmp(s
, "3"))
493 p
= unix_versions
[0];
494 else if (0 == strcmp(s
, "4"))
495 p
= unix_versions
[1];
496 else if (0 == strcmp(s
, "5")) {
498 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
499 p
= unix_versions
[3];
501 p
= unix_versions
[2];
503 p
= unix_versions
[0];
506 free(man
->meta
.source
);
507 man
->meta
.source
= mandoc_strdup(p
);
515 if (n
->tok
== MAN_br
)
523 switch (n
->parent
->tok
) {
527 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
528 "%s after %s", man_macronames
[n
->tok
],
529 man_macronames
[n
->parent
->tok
]);
533 * Don't warn about this because it occurs in pod2man
534 * and would cause considerable (unfixable) warnage.
536 man_node_delete(man
, n
);