]>
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_eq2(CHKARGS
);
42 static void check_le1(CHKARGS
);
43 static void check_le5(CHKARGS
);
44 static void check_par(CHKARGS
);
45 static void check_part(CHKARGS
);
46 static void check_root(CHKARGS
);
47 static void check_text(CHKARGS
);
49 static void post_AT(CHKARGS
);
50 static void post_IP(CHKARGS
);
51 static void post_vs(CHKARGS
);
52 static void post_fi(CHKARGS
);
53 static void post_ft(CHKARGS
);
54 static void post_nf(CHKARGS
);
55 static void post_TH(CHKARGS
);
56 static void post_UC(CHKARGS
);
57 static void post_UR(CHKARGS
);
59 static v_check man_valids
[MAN_MAX
] = {
102 man_valid_post(struct man
*man
)
108 if (n
->flags
& MAN_VALID
)
110 n
->flags
|= MAN_VALID
;
124 cp
= man_valids
+ n
->tok
;
135 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
137 if (NULL
== man
->first
->child
)
138 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
139 n
->line
, n
->pos
, NULL
);
141 man
->meta
.hasbody
= 1;
143 if (NULL
== man
->meta
.title
) {
144 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
145 n
->line
, n
->pos
, NULL
);
148 * If a title hasn't been set, do so now (by
149 * implication, date and section also aren't set).
152 man
->meta
.title
= mandoc_strdup("");
153 man
->meta
.msec
= mandoc_strdup("");
154 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
155 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
);
173 #define INEQ_DEFINE(x, ineq, name) \
175 check_##name(CHKARGS) \
177 if (n->nchild ineq (x)) \
179 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
180 "line arguments %s %d (have %d)", \
181 #ineq, (x), n->nchild); \
184 INEQ_DEFINE(2, ==, eq2
)
185 INEQ_DEFINE(1, <=, le1
)
186 INEQ_DEFINE(5, <=, le5
)
192 if (MAN_HEAD
== n
->type
&& 1 != n
->nchild
)
193 mandoc_vmsg(MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
194 n
->pos
, "line arguments eq 1 (have %d)", n
->nchild
);
208 cp
= n
->child
->string
;
227 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
231 if ('W' == cp
[1] && '\0' == cp
[2])
239 mandoc_vmsg(MANDOCERR_FT_BAD
, man
->parse
,
240 n
->line
, n
->pos
, "ft %s", cp
);
245 mandoc_vmsg(MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
246 n
->pos
, "want one child (have %d)", n
->nchild
);
253 if (n
->type
== MAN_BODY
&& n
->child
== NULL
)
254 mandoc_msg(MANDOCERR_BLK_EMPTY
, man
->parse
,
255 n
->line
, n
->pos
, man_macronames
[n
->tok
]);
264 if (0 == n
->body
->nchild
)
265 man_node_delete(man
, n
);
269 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
270 man
->parse
, n
->line
, n
->pos
,
271 "%s empty", man_macronames
[n
->tok
]);
275 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
276 man
->parse
, n
->line
, n
->pos
,
277 "%s %s%s", man_macronames
[n
->tok
],
279 n
->nchild
> 1 ? " ..." : "");
292 if (0 == n
->head
->nchild
&& 0 == n
->body
->nchild
)
293 man_node_delete(man
, n
);
296 if (0 == n
->parent
->head
->nchild
&& 0 == n
->nchild
)
297 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
298 man
->parse
, n
->line
, n
->pos
,
299 "%s empty", man_macronames
[n
->tok
]);
314 free(man
->meta
.title
);
316 free(man
->meta
.source
);
317 free(man
->meta
.msec
);
318 free(man
->meta
.date
);
320 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
321 man
->meta
.msec
= man
->meta
.source
= NULL
;
325 /* ->TITLE<- MSEC DATE SOURCE VOL */
328 if (n
&& n
->string
) {
329 for (p
= n
->string
; '\0' != *p
; p
++) {
330 /* Only warn about this once... */
331 if (isalpha((unsigned char)*p
) &&
332 ! isupper((unsigned char)*p
)) {
333 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
335 n
->pos
+ (p
- n
->string
),
340 man
->meta
.title
= mandoc_strdup(n
->string
);
342 man
->meta
.title
= mandoc_strdup("");
343 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
344 nb
->line
, nb
->pos
, "TH");
347 /* TITLE ->MSEC<- DATE SOURCE VOL */
352 man
->meta
.msec
= mandoc_strdup(n
->string
);
354 man
->meta
.msec
= mandoc_strdup("");
355 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
356 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
359 /* TITLE MSEC ->DATE<- SOURCE VOL */
363 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
364 man
->meta
.date
= man
->quick
?
365 mandoc_strdup(n
->string
) :
366 mandoc_normdate(man
->parse
, n
->string
,
369 man
->meta
.date
= mandoc_strdup("");
370 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
371 n
? n
->line
: nb
->line
,
372 n
? n
->pos
: nb
->pos
, "TH");
375 /* TITLE MSEC DATE ->SOURCE<- VOL */
377 if (n
&& (n
= n
->next
))
378 man
->meta
.source
= mandoc_strdup(n
->string
);
379 else if (man
->defos
!= NULL
)
380 man
->meta
.source
= mandoc_strdup(man
->defos
);
382 /* TITLE MSEC DATE SOURCE ->VOL<- */
383 /* If missing, use the default VOL name for MSEC. */
385 if (n
&& (n
= n
->next
))
386 man
->meta
.vol
= mandoc_strdup(n
->string
);
387 else if ('\0' != man
->meta
.msec
[0] &&
388 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
389 man
->meta
.vol
= mandoc_strdup(p
);
392 * Remove the `TH' node after we've processed it for our
395 man_node_delete(man
, man
->last
);
402 if (man
->flags
& MAN_LITERAL
)
403 mandoc_msg(MANDOCERR_NF_SKIP
, man
->parse
,
404 n
->line
, n
->pos
, "nf");
406 man
->flags
|= MAN_LITERAL
;
413 if ( ! (MAN_LITERAL
& man
->flags
))
414 mandoc_msg(MANDOCERR_FI_SKIP
, man
->parse
,
415 n
->line
, n
->pos
, "fi");
417 man
->flags
&= ~MAN_LITERAL
;
423 static const char * const bsd_versions
[] = {
424 "3rd Berkeley Distribution",
425 "4th Berkeley Distribution",
426 "4.2 Berkeley Distribution",
427 "4.3 Berkeley Distribution",
428 "4.4 Berkeley Distribution",
435 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
439 if (0 == strcmp(s
, "3"))
441 else if (0 == strcmp(s
, "4"))
443 else if (0 == strcmp(s
, "5"))
445 else if (0 == strcmp(s
, "6"))
447 else if (0 == strcmp(s
, "7"))
453 free(man
->meta
.source
);
454 man
->meta
.source
= mandoc_strdup(p
);
460 static const char * const unix_versions
[] = {
464 "System V Release 2",
472 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
473 p
= unix_versions
[0];
476 if (0 == strcmp(s
, "3"))
477 p
= unix_versions
[0];
478 else if (0 == strcmp(s
, "4"))
479 p
= unix_versions
[1];
480 else if (0 == strcmp(s
, "5")) {
482 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
483 p
= unix_versions
[3];
485 p
= unix_versions
[2];
487 p
= unix_versions
[0];
490 free(man
->meta
.source
);
491 man
->meta
.source
= mandoc_strdup(p
);
498 if (n
->tok
== MAN_sp
)
504 switch (n
->parent
->tok
) {
508 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
509 "%s after %s", man_macronames
[n
->tok
],
510 man_macronames
[n
->parent
->tok
]);
514 * Don't warn about this because it occurs in pod2man
515 * and would cause considerable (unfixable) warnage.
517 man_node_delete(man
, n
);