]>
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-2017 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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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>
31 #include "mandoc_aux.h"
35 #include "libmandoc.h"
39 #define CHKARGS struct roff_man *man, struct roff_node *n
41 typedef void (*v_check
)(CHKARGS
);
43 static void check_par(CHKARGS
);
44 static void check_part(CHKARGS
);
45 static void check_root(CHKARGS
);
46 static void check_text(CHKARGS
);
48 static void post_AT(CHKARGS
);
49 static void post_IP(CHKARGS
);
50 static void post_OP(CHKARGS
);
51 static void post_TH(CHKARGS
);
52 static void post_UC(CHKARGS
);
53 static void post_UR(CHKARGS
);
54 static void post_in(CHKARGS
);
55 static void post_vs(CHKARGS
);
57 static const v_check __man_valids
[MAN_MAX
- MAN_TH
] = {
95 static const v_check
*man_valids
= __man_valids
- MAN_TH
;
99 man_node_validate(struct roff_man
*man
)
105 man
->last
= man
->last
->child
;
106 while (man
->last
!= NULL
) {
107 man_node_validate(man
);
109 man
->last
= man
->last
->child
;
111 man
->last
= man
->last
->next
;
115 man
->next
= ROFF_NEXT_SIBLING
;
127 if (n
->tok
< ROFF_MAX
) {
139 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
140 cp
= man_valids
+ n
->tok
;
153 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
155 if (NULL
== man
->first
->child
)
156 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
157 n
->line
, n
->pos
, NULL
);
159 man
->meta
.hasbody
= 1;
161 if (NULL
== man
->meta
.title
) {
162 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
163 n
->line
, n
->pos
, NULL
);
166 * If a title hasn't been set, do so now (by
167 * implication, date and section also aren't set).
170 man
->meta
.title
= mandoc_strdup("");
171 man
->meta
.msec
= mandoc_strdup("");
172 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
173 mandoc_normdate(man
, NULL
, n
->line
, n
->pos
);
176 if (man
->meta
.os_e
&&
177 (man
->meta
.rcsids
& (1 << man
->meta
.os_e
)) == 0)
178 mandoc_msg(MANDOCERR_RCS_MISSING
, man
->parse
, 0, 0,
179 man
->meta
.os_e
== MANDOC_OS_OPENBSD
?
180 "(OpenBSD)" : "(NetBSD)");
188 if (MAN_LITERAL
& man
->flags
)
192 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
193 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
194 n
->line
, n
->pos
+ (p
- cp
), NULL
);
201 if (n
->child
== NULL
)
202 mandoc_msg(MANDOCERR_OP_EMPTY
, man
->parse
,
203 n
->line
, n
->pos
, "OP");
204 else if (n
->child
->next
!= NULL
&& n
->child
->next
->next
!= NULL
) {
205 n
= n
->child
->next
->next
;
206 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
207 n
->line
, n
->pos
, "OP ... %s", n
->string
);
215 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
216 mandoc_vmsg(MANDOCERR_UR_NOHEAD
, man
->parse
,
217 n
->line
, n
->pos
, roff_name
[n
->tok
]);
225 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
226 mandoc_msg(MANDOCERR_BLK_EMPTY
, man
->parse
,
227 n
->line
, n
->pos
, roff_name
[n
->tok
]);
236 if (n
->body
->child
== NULL
)
237 roff_node_delete(man
, n
);
240 if (n
->child
== NULL
)
241 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
242 man
->parse
, n
->line
, n
->pos
,
243 "%s empty", roff_name
[n
->tok
]);
246 if (n
->child
!= NULL
)
247 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
248 man
->parse
, n
->line
, n
->pos
, "%s %s%s",
249 roff_name
[n
->tok
], n
->child
->string
,
250 n
->child
->next
!= NULL
? " ..." : "");
263 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
264 roff_node_delete(man
, n
);
267 if (n
->parent
->head
->child
== NULL
&& n
->child
== NULL
)
268 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
269 man
->parse
, n
->line
, n
->pos
,
270 "%s empty", roff_name
[n
->tok
]);
280 struct roff_node
*nb
;
283 free(man
->meta
.title
);
286 free(man
->meta
.msec
);
287 free(man
->meta
.date
);
289 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
290 man
->meta
.msec
= man
->meta
.os
= NULL
;
294 /* ->TITLE<- MSEC DATE OS VOL */
297 if (n
&& n
->string
) {
298 for (p
= n
->string
; '\0' != *p
; p
++) {
299 /* Only warn about this once... */
300 if (isalpha((unsigned char)*p
) &&
301 ! isupper((unsigned char)*p
)) {
302 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
304 n
->pos
+ (p
- n
->string
),
309 man
->meta
.title
= mandoc_strdup(n
->string
);
311 man
->meta
.title
= mandoc_strdup("");
312 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
313 nb
->line
, nb
->pos
, "TH");
316 /* TITLE ->MSEC<- DATE OS VOL */
321 man
->meta
.msec
= mandoc_strdup(n
->string
);
323 man
->meta
.msec
= mandoc_strdup("");
324 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
325 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
328 /* TITLE MSEC ->DATE<- OS VOL */
332 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
333 man
->meta
.date
= man
->quick
?
334 mandoc_strdup(n
->string
) :
335 mandoc_normdate(man
, n
->string
, n
->line
, n
->pos
);
337 man
->meta
.date
= mandoc_strdup("");
338 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
339 n
? n
->line
: nb
->line
,
340 n
? n
->pos
: nb
->pos
, "TH");
343 /* TITLE MSEC DATE ->OS<- VOL */
345 if (n
&& (n
= n
->next
))
346 man
->meta
.os
= mandoc_strdup(n
->string
);
347 else if (man
->os_s
!= NULL
)
348 man
->meta
.os
= mandoc_strdup(man
->os_s
);
349 if (man
->meta
.os_e
== MANDOC_OS_OTHER
&& man
->meta
.os
!= NULL
) {
350 if (strstr(man
->meta
.os
, "OpenBSD") != NULL
)
351 man
->meta
.os_e
= MANDOC_OS_OPENBSD
;
352 else if (strstr(man
->meta
.os
, "NetBSD") != NULL
)
353 man
->meta
.os_e
= MANDOC_OS_NETBSD
;
356 /* TITLE MSEC DATE OS ->VOL<- */
357 /* If missing, use the default VOL name for MSEC. */
359 if (n
&& (n
= n
->next
))
360 man
->meta
.vol
= mandoc_strdup(n
->string
);
361 else if ('\0' != man
->meta
.msec
[0] &&
362 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
363 man
->meta
.vol
= mandoc_strdup(p
);
365 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
366 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
367 n
->line
, n
->pos
, "TH ... %s", n
->string
);
370 * Remove the `TH' node after we've processed it for our
373 roff_node_delete(man
, man
->last
);
379 static const char * const bsd_versions
[] = {
380 "3rd Berkeley Distribution",
381 "4th Berkeley Distribution",
382 "4.2 Berkeley Distribution",
383 "4.3 Berkeley Distribution",
384 "4.4 Berkeley Distribution",
391 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
395 if (0 == strcmp(s
, "3"))
397 else if (0 == strcmp(s
, "4"))
399 else if (0 == strcmp(s
, "5"))
401 else if (0 == strcmp(s
, "6"))
403 else if (0 == strcmp(s
, "7"))
410 man
->meta
.os
= mandoc_strdup(p
);
416 static const char * const unix_versions
[] = {
420 "System V Release 2",
423 struct roff_node
*nn
;
428 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
429 p
= unix_versions
[0];
432 if (0 == strcmp(s
, "3"))
433 p
= unix_versions
[0];
434 else if (0 == strcmp(s
, "4"))
435 p
= unix_versions
[1];
436 else if (0 == strcmp(s
, "5")) {
439 nn
->type
== ROFFT_TEXT
&&
440 nn
->string
[0] != '\0')
441 p
= unix_versions
[3];
443 p
= unix_versions
[2];
445 p
= unix_versions
[0];
449 man
->meta
.os
= mandoc_strdup(p
);
457 if (n
->parent
->tok
!= MAN_TP
||
458 n
->parent
->type
!= ROFFT_HEAD
||
460 *n
->child
->string
== '+' ||
461 *n
->child
->string
== '-')
463 mandoc_asprintf(&s
, "+%s", n
->child
->string
);
464 free(n
->child
->string
);
465 n
->child
->string
= s
;
475 switch (n
->parent
->tok
) {
481 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
482 "%s after %s", roff_name
[n
->tok
],
483 roff_name
[n
->parent
->tok
]);
487 * Don't warn about this because it occurs in pod2man
488 * and would cause considerable (unfixable) warnage.
490 roff_node_delete(man
, n
);