]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
c038b887a2647d5be4fff0c4f27d2efc0310e1e3
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_ft(CHKARGS
);
52 static void post_OP(CHKARGS
);
53 static void post_TH(CHKARGS
);
54 static void post_UC(CHKARGS
);
55 static void post_UR(CHKARGS
);
57 static const v_check __man_valids
[MAN_MAX
- MAN_TH
] = {
96 static const v_check
*man_valids
= __man_valids
- MAN_TH
;
100 man_node_validate(struct roff_man
*man
)
106 man
->last
= man
->last
->child
;
107 while (man
->last
!= NULL
) {
108 man_node_validate(man
);
110 man
->last
= man
->last
->child
;
112 man
->last
= man
->last
->next
;
116 man
->next
= ROFF_NEXT_SIBLING
;
128 if (n
->tok
< ROFF_MAX
) {
138 assert(n
->tok
>= MAN_TH
&& n
->tok
< MAN_MAX
);
139 cp
= man_valids
+ n
->tok
;
152 assert((man
->flags
& (MAN_BLINE
| MAN_ELINE
)) == 0);
154 if (NULL
== man
->first
->child
)
155 mandoc_msg(MANDOCERR_DOC_EMPTY
, man
->parse
,
156 n
->line
, n
->pos
, NULL
);
158 man
->meta
.hasbody
= 1;
160 if (NULL
== man
->meta
.title
) {
161 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
162 n
->line
, n
->pos
, NULL
);
165 * If a title hasn't been set, do so now (by
166 * implication, date and section also aren't set).
169 man
->meta
.title
= mandoc_strdup("");
170 man
->meta
.msec
= mandoc_strdup("");
171 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
172 mandoc_normdate(man
->parse
, NULL
, n
->line
, n
->pos
);
181 if (MAN_LITERAL
& man
->flags
)
185 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
186 mandoc_msg(MANDOCERR_FI_TAB
, man
->parse
,
187 n
->line
, n
->pos
+ (p
- cp
), NULL
);
194 if (n
->child
== NULL
)
195 mandoc_msg(MANDOCERR_OP_EMPTY
, man
->parse
,
196 n
->line
, n
->pos
, "OP");
197 else if (n
->child
->next
!= NULL
&& n
->child
->next
->next
!= NULL
) {
198 n
= n
->child
->next
->next
;
199 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
200 n
->line
, n
->pos
, "OP ... %s", n
->string
);
208 if (n
->type
== ROFFT_HEAD
&& n
->child
== NULL
)
209 mandoc_vmsg(MANDOCERR_UR_NOHEAD
, man
->parse
,
210 n
->line
, n
->pos
, "UR");
220 if (n
->child
== NULL
)
224 cp
= n
->child
->string
;
237 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
241 if ('W' == cp
[1] && '\0' == cp
[2])
249 mandoc_vmsg(MANDOCERR_FT_BAD
, man
->parse
,
250 n
->line
, n
->pos
, "ft %s", cp
);
259 if (n
->type
== ROFFT_BODY
&& n
->child
== NULL
)
260 mandoc_msg(MANDOCERR_BLK_EMPTY
, man
->parse
,
261 n
->line
, n
->pos
, roff_name
[n
->tok
]);
270 if (n
->body
->child
== NULL
)
271 roff_node_delete(man
, n
);
274 if (n
->child
== NULL
)
275 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
276 man
->parse
, n
->line
, n
->pos
,
277 "%s empty", roff_name
[n
->tok
]);
280 if (n
->child
!= NULL
)
281 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
282 man
->parse
, n
->line
, n
->pos
, "%s %s%s",
283 roff_name
[n
->tok
], n
->child
->string
,
284 n
->child
->next
!= NULL
? " ..." : "");
297 if (n
->head
->child
== NULL
&& n
->body
->child
== NULL
)
298 roff_node_delete(man
, n
);
301 if (n
->parent
->head
->child
== NULL
&& n
->child
== NULL
)
302 mandoc_vmsg(MANDOCERR_PAR_SKIP
,
303 man
->parse
, n
->line
, n
->pos
,
304 "%s empty", roff_name
[n
->tok
]);
314 struct roff_node
*nb
;
317 free(man
->meta
.title
);
320 free(man
->meta
.msec
);
321 free(man
->meta
.date
);
323 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
324 man
->meta
.msec
= man
->meta
.os
= NULL
;
328 /* ->TITLE<- MSEC DATE OS VOL */
331 if (n
&& n
->string
) {
332 for (p
= n
->string
; '\0' != *p
; p
++) {
333 /* Only warn about this once... */
334 if (isalpha((unsigned char)*p
) &&
335 ! isupper((unsigned char)*p
)) {
336 mandoc_vmsg(MANDOCERR_TITLE_CASE
,
338 n
->pos
+ (p
- n
->string
),
343 man
->meta
.title
= mandoc_strdup(n
->string
);
345 man
->meta
.title
= mandoc_strdup("");
346 mandoc_msg(MANDOCERR_TH_NOTITLE
, man
->parse
,
347 nb
->line
, nb
->pos
, "TH");
350 /* TITLE ->MSEC<- DATE OS VOL */
355 man
->meta
.msec
= mandoc_strdup(n
->string
);
357 man
->meta
.msec
= mandoc_strdup("");
358 mandoc_vmsg(MANDOCERR_MSEC_MISSING
, man
->parse
,
359 nb
->line
, nb
->pos
, "TH %s", man
->meta
.title
);
362 /* TITLE MSEC ->DATE<- OS VOL */
366 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
367 man
->meta
.date
= man
->quick
?
368 mandoc_strdup(n
->string
) :
369 mandoc_normdate(man
->parse
, n
->string
,
372 man
->meta
.date
= mandoc_strdup("");
373 mandoc_msg(MANDOCERR_DATE_MISSING
, man
->parse
,
374 n
? n
->line
: nb
->line
,
375 n
? n
->pos
: nb
->pos
, "TH");
378 /* TITLE MSEC DATE ->OS<- VOL */
380 if (n
&& (n
= n
->next
))
381 man
->meta
.os
= mandoc_strdup(n
->string
);
382 else if (man
->defos
!= NULL
)
383 man
->meta
.os
= mandoc_strdup(man
->defos
);
385 /* TITLE MSEC DATE OS ->VOL<- */
386 /* If missing, use the default VOL name for MSEC. */
388 if (n
&& (n
= n
->next
))
389 man
->meta
.vol
= mandoc_strdup(n
->string
);
390 else if ('\0' != man
->meta
.msec
[0] &&
391 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
392 man
->meta
.vol
= mandoc_strdup(p
);
394 if (n
!= NULL
&& (n
= n
->next
) != NULL
)
395 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
396 n
->line
, n
->pos
, "TH ... %s", n
->string
);
399 * Remove the `TH' node after we've processed it for our
402 roff_node_delete(man
, man
->last
);
408 static const char * const bsd_versions
[] = {
409 "3rd Berkeley Distribution",
410 "4th Berkeley Distribution",
411 "4.2 Berkeley Distribution",
412 "4.3 Berkeley Distribution",
413 "4.4 Berkeley Distribution",
420 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
424 if (0 == strcmp(s
, "3"))
426 else if (0 == strcmp(s
, "4"))
428 else if (0 == strcmp(s
, "5"))
430 else if (0 == strcmp(s
, "6"))
432 else if (0 == strcmp(s
, "7"))
439 man
->meta
.os
= mandoc_strdup(p
);
445 static const char * const unix_versions
[] = {
449 "System V Release 2",
452 struct roff_node
*nn
;
457 if (n
== NULL
|| n
->type
!= ROFFT_TEXT
)
458 p
= unix_versions
[0];
461 if (0 == strcmp(s
, "3"))
462 p
= unix_versions
[0];
463 else if (0 == strcmp(s
, "4"))
464 p
= unix_versions
[1];
465 else if (0 == strcmp(s
, "5")) {
468 nn
->type
== ROFFT_TEXT
&&
469 nn
->string
[0] != '\0')
470 p
= unix_versions
[3];
472 p
= unix_versions
[2];
474 p
= unix_versions
[0];
478 man
->meta
.os
= mandoc_strdup(p
);
488 switch (n
->parent
->tok
) {
491 mandoc_vmsg(MANDOCERR_PAR_SKIP
, man
->parse
, n
->line
, n
->pos
,
492 "%s after %s", roff_name
[n
->tok
],
493 roff_name
[n
->parent
->tok
]);
497 * Don't warn about this because it occurs in pod2man
498 * and would cause considerable (unfixable) warnage.
500 roff_node_delete(man
, n
);