]>
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, NULL
);
184 if (MAN_LITERAL
& man
->flags
)
188 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
189 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
190 n
->line
, n
->pos
+ (p
- cp
), NULL
);
197 if (n
->child
== NULL
)
198 mandoc_msg(MANDOCERR_OP_EMPTY
, man
->parse
,
199 n
->line
, n
->pos
, "OP");
200 else if (n
->child
->next
!= NULL
&& n
->child
->next
->next
!= NULL
) {
201 n
= n
->child
->next
->next
;
202 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
203 n
->line
, n
->pos
, "OP ... %s", n
->string
);
211 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
212 mandoc_vmsg(MANDOCERR_UR_NOHEAD
, man
->parse
,
213 n
->line
, n
->pos
, "UR");
221 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
222 mandoc_msg(MANDOCERR_BLK_EMPTY
, man
->parse
,
223 n
->line
, n
->pos
, roff_name
[n
->tok
]);
232 if (n
->body
->child
== NULL
)
233 roff_node_delete(man
, n
);
236 if (n
->child
== NULL
)
237 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
238 man
->parse
, n
->line
, n
->pos
,
239 "%s empty", roff_name
[n
->tok
]);
242 if (n
->child
!= NULL
)
243 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
244 man
->parse
, n
->line
, n
->pos
, "%s %s%s",
245 roff_name
[n
->tok
], n
->child
->string
,
246 n
->child
->next
!= NULL
? " ..." : "");
259 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
260 roff_node_delete(man
, n
);
263 if (n
->parent
->head
->child
== NULL
&& n
->child
== NULL
)
264 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
265 man
->parse
, n
->line
, n
->pos
,
266 "%s empty", roff_name
[n
->tok
]);
276 struct roff_node
*nb
;
279 free(man
->meta
.title
);
282 free(man
->meta
.msec
);
283 free(man
->meta
.date
);
285 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
286 man
->meta
.msec
= man
->meta
.os
= NULL
;
290 /* ->TITLE<- MSEC DATE OS VOL */
293 if (n
&& n
->string
) {
294 for (p
= n
->string
; '\0' != *p
; p
++) {
295 /* Only warn about this once... */
296 if (isalpha((unsigned char)*p
) &&
297 ! isupper((unsigned char)*p
)) {
298 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
300 n
->pos
+ (p
- n
->string
),
305 man
->meta
.title
= mandoc_strdup(n
->string
);
307 man
->meta
.title
= mandoc_strdup("");
308 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
309 nb
->line
, nb
->pos
, "TH");
312 /* TITLE ->MSEC<- DATE OS VOL */
317 man
->meta
.msec
= mandoc_strdup(n
->string
);
319 man
->meta
.msec
= mandoc_strdup("");
320 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
321 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
324 /* TITLE MSEC ->DATE<- OS VOL */
328 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
329 man
->meta
.date
= man
->quick
?
330 mandoc_strdup(n
->string
) :
331 mandoc_normdate(man
, n
->string
, n
->line
, n
->pos
);
333 man
->meta
.date
= mandoc_strdup("");
334 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
335 n
? n
->line
: nb
->line
,
336 n
? n
->pos
: nb
->pos
, "TH");
339 /* TITLE MSEC DATE ->OS<- VOL */
341 if (n
&& (n
= n
->next
))
342 man
->meta
.os
= mandoc_strdup(n
->string
);
343 else if (man
->defos
!= NULL
)
344 man
->meta
.os
= mandoc_strdup(man
->defos
);
345 man
->meta
.os_e
= man
->meta
.os
== NULL
? MDOC_OS_OTHER
:
346 strstr(man
->meta
.os
, "OpenBSD") != NULL
? MDOC_OS_OPENBSD
:
347 strstr(man
->meta
.os
, "NetBSD") != NULL
? MDOC_OS_NETBSD
:
350 /* TITLE MSEC DATE OS ->VOL<- */
351 /* If missing, use the default VOL name for MSEC. */
353 if (n
&& (n
= n
->next
))
354 man
->meta
.vol
= mandoc_strdup(n
->string
);
355 else if ('\0' != man
->meta
.msec
[0] &&
356 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
357 man
->meta
.vol
= mandoc_strdup(p
);
359 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
360 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
361 n
->line
, n
->pos
, "TH ... %s", n
->string
);
364 * Remove the `TH' node after we've processed it for our
367 roff_node_delete(man
, man
->last
);
373 static const char * const bsd_versions
[] = {
374 "3rd Berkeley Distribution",
375 "4th Berkeley Distribution",
376 "4.2 Berkeley Distribution",
377 "4.3 Berkeley Distribution",
378 "4.4 Berkeley Distribution",
385 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
389 if (0 == strcmp(s
, "3"))
391 else if (0 == strcmp(s
, "4"))
393 else if (0 == strcmp(s
, "5"))
395 else if (0 == strcmp(s
, "6"))
397 else if (0 == strcmp(s
, "7"))
404 man
->meta
.os
= mandoc_strdup(p
);
410 static const char * const unix_versions
[] = {
414 "System V Release 2",
417 struct roff_node
*nn
;
422 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
423 p
= unix_versions
[0];
426 if (0 == strcmp(s
, "3"))
427 p
= unix_versions
[0];
428 else if (0 == strcmp(s
, "4"))
429 p
= unix_versions
[1];
430 else if (0 == strcmp(s
, "5")) {
433 nn
->type
== ROFFT_TEXT
&&
434 nn
->string
[0] != '\0')
435 p
= unix_versions
[3];
437 p
= unix_versions
[2];
439 p
= unix_versions
[0];
443 man
->meta
.os
= mandoc_strdup(p
);
451 if (n
->parent
->tok
!= MAN_TP
||
452 n
->parent
->type
!= ROFFT_HEAD
||
454 *n
->child
->string
== '+' ||
455 *n
->child
->string
== '-')
457 mandoc_asprintf(&s
, "+%s", n
->child
->string
);
458 free(n
->child
->string
);
459 n
->child
->string
= s
;
469 switch (n
->parent
->tok
) {
475 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
476 "%s after %s", roff_name
[n
->tok
],
477 roff_name
[n
->parent
->tok
]);
481 * Don't warn about this because it occurs in pod2man
482 * and would cause considerable (unfixable) warnage.
484 roff_node_delete(man
, n
);