]>
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
] = {
93 static const v_check
*man_valids
= __man_valids
- MAN_TH
;
97 man_node_validate(struct roff_man
*man
)
103 man
->last
= man
->last
->child
;
104 while (man
->last
!= NULL
) {
105 man_node_validate(man
);
107 man
->last
= man
->last
->child
;
109 man
->last
= man
->last
->next
;
113 man
->next
= ROFF_NEXT_SIBLING
;
125 if (n
->tok
< ROFF_MAX
) {
137 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
138 cp
= man_valids
+ n
->tok
;
151 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
153 if (NULL
== man
->first
->child
)
154 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
155 n
->line
, n
->pos
, NULL
);
157 man
->meta
.hasbody
= 1;
159 if (NULL
== man
->meta
.title
) {
160 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
161 n
->line
, n
->pos
, NULL
);
164 * If a title hasn't been set, do so now (by
165 * implication, date and section also aren't set).
168 man
->meta
.title
= mandoc_strdup("");
169 man
->meta
.msec
= mandoc_strdup("");
170 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
171 mandoc_normdate(man
, NULL
, n
->line
, n
->pos
);
174 if (man
->meta
.os_e
&&
175 (man
->meta
.rcsids
& (1 << man
->meta
.os_e
)) == 0)
176 mandoc_msg(MANDOCERR_RCS_MISSING
, man
->parse
, 0, 0,
177 man
->meta
.os_e
== MANDOC_OS_OPENBSD
?
178 "(OpenBSD)" : "(NetBSD)");
186 if (MAN_LITERAL
& man
->flags
)
190 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
191 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
192 n
->line
, n
->pos
+ (p
- cp
), NULL
);
199 if (n
->child
== NULL
)
200 mandoc_msg(MANDOCERR_OP_EMPTY
, man
->parse
,
201 n
->line
, n
->pos
, "OP");
202 else if (n
->child
->next
!= NULL
&& n
->child
->next
->next
!= NULL
) {
203 n
= n
->child
->next
->next
;
204 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
205 n
->line
, n
->pos
, "OP ... %s", n
->string
);
213 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
214 mandoc_vmsg(MANDOCERR_UR_NOHEAD
, man
->parse
,
215 n
->line
, n
->pos
, "UR");
223 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
224 mandoc_msg(MANDOCERR_BLK_EMPTY
, man
->parse
,
225 n
->line
, n
->pos
, roff_name
[n
->tok
]);
234 if (n
->body
->child
== NULL
)
235 roff_node_delete(man
, n
);
238 if (n
->child
== NULL
)
239 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
240 man
->parse
, n
->line
, n
->pos
,
241 "%s empty", roff_name
[n
->tok
]);
244 if (n
->child
!= NULL
)
245 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
246 man
->parse
, n
->line
, n
->pos
, "%s %s%s",
247 roff_name
[n
->tok
], n
->child
->string
,
248 n
->child
->next
!= NULL
? " ..." : "");
261 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
262 roff_node_delete(man
, n
);
265 if (n
->parent
->head
->child
== NULL
&& n
->child
== NULL
)
266 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
267 man
->parse
, n
->line
, n
->pos
,
268 "%s empty", roff_name
[n
->tok
]);
278 struct roff_node
*nb
;
281 free(man
->meta
.title
);
284 free(man
->meta
.msec
);
285 free(man
->meta
.date
);
287 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
288 man
->meta
.msec
= man
->meta
.os
= NULL
;
292 /* ->TITLE<- MSEC DATE OS VOL */
295 if (n
&& n
->string
) {
296 for (p
= n
->string
; '\0' != *p
; p
++) {
297 /* Only warn about this once... */
298 if (isalpha((unsigned char)*p
) &&
299 ! isupper((unsigned char)*p
)) {
300 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
302 n
->pos
+ (p
- n
->string
),
307 man
->meta
.title
= mandoc_strdup(n
->string
);
309 man
->meta
.title
= mandoc_strdup("");
310 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
311 nb
->line
, nb
->pos
, "TH");
314 /* TITLE ->MSEC<- DATE OS VOL */
319 man
->meta
.msec
= mandoc_strdup(n
->string
);
321 man
->meta
.msec
= mandoc_strdup("");
322 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
323 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
326 /* TITLE MSEC ->DATE<- OS VOL */
330 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
331 man
->meta
.date
= man
->quick
?
332 mandoc_strdup(n
->string
) :
333 mandoc_normdate(man
, n
->string
, n
->line
, n
->pos
);
335 man
->meta
.date
= mandoc_strdup("");
336 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
337 n
? n
->line
: nb
->line
,
338 n
? n
->pos
: nb
->pos
, "TH");
341 /* TITLE MSEC DATE ->OS<- VOL */
343 if (n
&& (n
= n
->next
))
344 man
->meta
.os
= mandoc_strdup(n
->string
);
345 else if (man
->os_s
!= NULL
)
346 man
->meta
.os
= mandoc_strdup(man
->os_s
);
347 if (man
->meta
.os_e
== MANDOC_OS_OTHER
&& man
->meta
.os
!= NULL
) {
348 if (strstr(man
->meta
.os
, "OpenBSD") != NULL
)
349 man
->meta
.os_e
= MANDOC_OS_OPENBSD
;
350 else if (strstr(man
->meta
.os
, "NetBSD") != NULL
)
351 man
->meta
.os_e
= MANDOC_OS_NETBSD
;
354 /* TITLE MSEC DATE OS ->VOL<- */
355 /* If missing, use the default VOL name for MSEC. */
357 if (n
&& (n
= n
->next
))
358 man
->meta
.vol
= mandoc_strdup(n
->string
);
359 else if ('\0' != man
->meta
.msec
[0] &&
360 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
361 man
->meta
.vol
= mandoc_strdup(p
);
363 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
364 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
365 n
->line
, n
->pos
, "TH ... %s", n
->string
);
368 * Remove the `TH' node after we've processed it for our
371 roff_node_delete(man
, man
->last
);
377 static const char * const bsd_versions
[] = {
378 "3rd Berkeley Distribution",
379 "4th Berkeley Distribution",
380 "4.2 Berkeley Distribution",
381 "4.3 Berkeley Distribution",
382 "4.4 Berkeley Distribution",
389 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
393 if (0 == strcmp(s
, "3"))
395 else if (0 == strcmp(s
, "4"))
397 else if (0 == strcmp(s
, "5"))
399 else if (0 == strcmp(s
, "6"))
401 else if (0 == strcmp(s
, "7"))
408 man
->meta
.os
= mandoc_strdup(p
);
414 static const char * const unix_versions
[] = {
418 "System V Release 2",
421 struct roff_node
*nn
;
426 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
427 p
= unix_versions
[0];
430 if (0 == strcmp(s
, "3"))
431 p
= unix_versions
[0];
432 else if (0 == strcmp(s
, "4"))
433 p
= unix_versions
[1];
434 else if (0 == strcmp(s
, "5")) {
437 nn
->type
== ROFFT_TEXT
&&
438 nn
->string
[0] != '\0')
439 p
= unix_versions
[3];
441 p
= unix_versions
[2];
443 p
= unix_versions
[0];
447 man
->meta
.os
= mandoc_strdup(p
);
455 if (n
->parent
->tok
!= MAN_TP
||
456 n
->parent
->type
!= ROFFT_HEAD
||
458 *n
->child
->string
== '+' ||
459 *n
->child
->string
== '-')
461 mandoc_asprintf(&s
, "+%s", n
->child
->string
);
462 free(n
->child
->string
);
463 n
->child
->string
= s
;
473 switch (n
->parent
->tok
) {
479 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
480 "%s after %s", roff_name
[n
->tok
],
481 roff_name
[n
->parent
->tok
]);
485 * Don't warn about this because it occurs in pod2man
486 * and would cause considerable (unfixable) warnage.
488 roff_node_delete(man
, n
);