]>
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_vs(CHKARGS
);
51 static void post_OP(CHKARGS
);
52 static void post_TH(CHKARGS
);
53 static void post_UC(CHKARGS
);
54 static void post_UR(CHKARGS
);
56 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
) {
136 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
137 cp
= man_valids
+ n
->tok
;
150 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
152 if (NULL
== man
->first
->child
)
153 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
154 n
->line
, n
->pos
, NULL
);
156 man
->meta
.hasbody
= 1;
158 if (NULL
== man
->meta
.title
) {
159 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
160 n
->line
, n
->pos
, NULL
);
163 * If a title hasn't been set, do so now (by
164 * implication, date and section also aren't set).
167 man
->meta
.title
= mandoc_strdup("");
168 man
->meta
.msec
= mandoc_strdup("");
169 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
170 mandoc_normdate(man
->parse
, NULL
, n
->line
, n
->pos
);
179 if (MAN_LITERAL
& man
->flags
)
183 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
184 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
185 n
->line
, n
->pos
+ (p
- cp
), NULL
);
192 if (n
->child
== NULL
)
193 mandoc_msg(MANDOCERR_OP_EMPTY
, man
->parse
,
194 n
->line
, n
->pos
, "OP");
195 else if (n
->child
->next
!= NULL
&& n
->child
->next
->next
!= NULL
) {
196 n
= n
->child
->next
->next
;
197 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
198 n
->line
, n
->pos
, "OP ... %s", n
->string
);
206 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
207 mandoc_vmsg(MANDOCERR_UR_NOHEAD
, man
->parse
,
208 n
->line
, n
->pos
, "UR");
216 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
217 mandoc_msg(MANDOCERR_BLK_EMPTY
, man
->parse
,
218 n
->line
, n
->pos
, roff_name
[n
->tok
]);
227 if (n
->body
->child
== NULL
)
228 roff_node_delete(man
, n
);
231 if (n
->child
== NULL
)
232 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
233 man
->parse
, n
->line
, n
->pos
,
234 "%s empty", roff_name
[n
->tok
]);
237 if (n
->child
!= NULL
)
238 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
239 man
->parse
, n
->line
, n
->pos
, "%s %s%s",
240 roff_name
[n
->tok
], n
->child
->string
,
241 n
->child
->next
!= NULL
? " ..." : "");
254 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
255 roff_node_delete(man
, n
);
258 if (n
->parent
->head
->child
== NULL
&& n
->child
== NULL
)
259 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
260 man
->parse
, n
->line
, n
->pos
,
261 "%s empty", roff_name
[n
->tok
]);
271 struct roff_node
*nb
;
274 free(man
->meta
.title
);
277 free(man
->meta
.msec
);
278 free(man
->meta
.date
);
280 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
281 man
->meta
.msec
= man
->meta
.os
= NULL
;
285 /* ->TITLE<- MSEC DATE OS VOL */
288 if (n
&& n
->string
) {
289 for (p
= n
->string
; '\0' != *p
; p
++) {
290 /* Only warn about this once... */
291 if (isalpha((unsigned char)*p
) &&
292 ! isupper((unsigned char)*p
)) {
293 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
295 n
->pos
+ (p
- n
->string
),
300 man
->meta
.title
= mandoc_strdup(n
->string
);
302 man
->meta
.title
= mandoc_strdup("");
303 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
304 nb
->line
, nb
->pos
, "TH");
307 /* TITLE ->MSEC<- DATE OS VOL */
312 man
->meta
.msec
= mandoc_strdup(n
->string
);
314 man
->meta
.msec
= mandoc_strdup("");
315 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
316 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
319 /* TITLE MSEC ->DATE<- OS VOL */
323 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
324 man
->meta
.date
= man
->quick
?
325 mandoc_strdup(n
->string
) :
326 mandoc_normdate(man
->parse
, n
->string
,
329 man
->meta
.date
= mandoc_strdup("");
330 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
331 n
? n
->line
: nb
->line
,
332 n
? n
->pos
: nb
->pos
, "TH");
335 /* TITLE MSEC DATE ->OS<- VOL */
337 if (n
&& (n
= n
->next
))
338 man
->meta
.os
= mandoc_strdup(n
->string
);
339 else if (man
->defos
!= NULL
)
340 man
->meta
.os
= mandoc_strdup(man
->defos
);
342 /* TITLE MSEC DATE OS ->VOL<- */
343 /* If missing, use the default VOL name for MSEC. */
345 if (n
&& (n
= n
->next
))
346 man
->meta
.vol
= mandoc_strdup(n
->string
);
347 else if ('\0' != man
->meta
.msec
[0] &&
348 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
349 man
->meta
.vol
= mandoc_strdup(p
);
351 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
352 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
353 n
->line
, n
->pos
, "TH ... %s", n
->string
);
356 * Remove the `TH' node after we've processed it for our
359 roff_node_delete(man
, man
->last
);
365 static const char * const bsd_versions
[] = {
366 "3rd Berkeley Distribution",
367 "4th Berkeley Distribution",
368 "4.2 Berkeley Distribution",
369 "4.3 Berkeley Distribution",
370 "4.4 Berkeley Distribution",
377 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
381 if (0 == strcmp(s
, "3"))
383 else if (0 == strcmp(s
, "4"))
385 else if (0 == strcmp(s
, "5"))
387 else if (0 == strcmp(s
, "6"))
389 else if (0 == strcmp(s
, "7"))
396 man
->meta
.os
= mandoc_strdup(p
);
402 static const char * const unix_versions
[] = {
406 "System V Release 2",
409 struct roff_node
*nn
;
414 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
415 p
= unix_versions
[0];
418 if (0 == strcmp(s
, "3"))
419 p
= unix_versions
[0];
420 else if (0 == strcmp(s
, "4"))
421 p
= unix_versions
[1];
422 else if (0 == strcmp(s
, "5")) {
425 nn
->type
== ROFFT_TEXT
&&
426 nn
->string
[0] != '\0')
427 p
= unix_versions
[3];
429 p
= unix_versions
[2];
431 p
= unix_versions
[0];
435 man
->meta
.os
= mandoc_strdup(p
);
445 switch (n
->parent
->tok
) {
448 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
449 "%s after %s", roff_name
[n
->tok
],
450 roff_name
[n
->parent
->tok
]);
454 * Don't warn about this because it occurs in pod2man
455 * and would cause considerable (unfixable) warnage.
457 roff_node_delete(man
, n
);