]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.90 2014/03/30 19:47:48 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012, 2013, 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
22 #include <sys/types.h>
35 #include "mandoc_aux.h"
37 #include "libmandoc.h"
39 #define CHKARGS struct man *man, struct man_node *n
41 typedef int (*v_check
)(CHKARGS
);
48 static int check_eq0(CHKARGS
);
49 static int check_eq2(CHKARGS
);
50 static int check_le1(CHKARGS
);
51 static int check_ge2(CHKARGS
);
52 static int check_le5(CHKARGS
);
53 static int check_head1(CHKARGS
);
54 static int check_par(CHKARGS
);
55 static int check_part(CHKARGS
);
56 static int check_root(CHKARGS
);
57 static void check_text(CHKARGS
);
59 static int post_AT(CHKARGS
);
60 static int post_IP(CHKARGS
);
61 static int post_vs(CHKARGS
);
62 static int post_fi(CHKARGS
);
63 static int post_ft(CHKARGS
);
64 static int post_nf(CHKARGS
);
65 static int post_sec(CHKARGS
);
66 static int post_TH(CHKARGS
);
67 static int post_UC(CHKARGS
);
68 static int pre_sec(CHKARGS
);
70 static v_check posts_at
[] = { post_AT
, NULL
};
71 static v_check posts_br
[] = { post_vs
, check_eq0
, NULL
};
72 static v_check posts_eq0
[] = { check_eq0
, NULL
};
73 static v_check posts_eq2
[] = { check_eq2
, NULL
};
74 static v_check posts_fi
[] = { check_eq0
, post_fi
, NULL
};
75 static v_check posts_ft
[] = { post_ft
, NULL
};
76 static v_check posts_ip
[] = { post_IP
, NULL
};
77 static v_check posts_le1
[] = { check_le1
, NULL
};
78 static v_check posts_nf
[] = { check_eq0
, post_nf
, NULL
};
79 static v_check posts_par
[] = { check_par
, NULL
};
80 static v_check posts_part
[] = { check_part
, NULL
};
81 static v_check posts_sec
[] = { post_sec
, NULL
};
82 static v_check posts_sp
[] = { post_vs
, check_le1
, NULL
};
83 static v_check posts_th
[] = { check_ge2
, check_le5
, post_TH
, NULL
};
84 static v_check posts_uc
[] = { post_UC
, NULL
};
85 static v_check posts_ur
[] = { check_head1
, check_part
, NULL
};
86 static v_check pres_sec
[] = { pre_sec
, NULL
};
88 static const struct man_valid man_valids
[MAN_MAX
] = {
89 { NULL
, posts_br
}, /* br */
90 { NULL
, posts_th
}, /* TH */
91 { pres_sec
, posts_sec
}, /* SH */
92 { pres_sec
, posts_sec
}, /* SS */
93 { NULL
, NULL
}, /* TP */
94 { NULL
, posts_par
}, /* LP */
95 { NULL
, posts_par
}, /* PP */
96 { NULL
, posts_par
}, /* P */
97 { NULL
, posts_ip
}, /* IP */
98 { NULL
, NULL
}, /* HP */
99 { NULL
, NULL
}, /* SM */
100 { NULL
, NULL
}, /* SB */
101 { NULL
, NULL
}, /* BI */
102 { NULL
, NULL
}, /* IB */
103 { NULL
, NULL
}, /* BR */
104 { NULL
, NULL
}, /* RB */
105 { NULL
, NULL
}, /* R */
106 { NULL
, NULL
}, /* B */
107 { NULL
, NULL
}, /* I */
108 { NULL
, NULL
}, /* IR */
109 { NULL
, NULL
}, /* RI */
110 { NULL
, posts_eq0
}, /* na */
111 { NULL
, posts_sp
}, /* sp */
112 { NULL
, posts_nf
}, /* nf */
113 { NULL
, posts_fi
}, /* fi */
114 { NULL
, NULL
}, /* RE */
115 { NULL
, posts_part
}, /* RS */
116 { NULL
, NULL
}, /* DT */
117 { NULL
, posts_uc
}, /* UC */
118 { NULL
, posts_le1
}, /* PD */
119 { NULL
, posts_at
}, /* AT */
120 { NULL
, NULL
}, /* in */
121 { NULL
, posts_ft
}, /* ft */
122 { NULL
, posts_eq2
}, /* OP */
123 { NULL
, posts_nf
}, /* EX */
124 { NULL
, posts_fi
}, /* EE */
125 { NULL
, posts_ur
}, /* UR */
126 { NULL
, NULL
}, /* UE */
127 { NULL
, NULL
}, /* ll */
132 man_valid_pre(struct man
*man
, struct man_node
*n
)
149 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
152 if ( ! (*cp
)(man
, n
))
159 man_valid_post(struct man
*man
)
163 if (MAN_VALID
& man
->last
->flags
)
165 man
->last
->flags
|= MAN_VALID
;
167 switch (man
->last
->type
) {
169 check_text(man
, man
->last
);
172 return(check_root(man
, man
->last
));
181 if (NULL
== (cp
= man_valids
[man
->last
->tok
].posts
))
184 if ( ! (*cp
)(man
, man
->last
))
195 if (MAN_BLINE
& man
->flags
)
196 man_nmsg(man
, n
, MANDOCERR_SCOPEEXIT
);
197 else if (MAN_ELINE
& man
->flags
)
198 man_nmsg(man
, n
, MANDOCERR_SCOPEEXIT
);
200 man
->flags
&= ~MAN_BLINE
;
201 man
->flags
&= ~MAN_ELINE
;
203 if (NULL
== man
->first
->child
) {
204 man_nmsg(man
, n
, MANDOCERR_NODOCBODY
);
206 } else if (NULL
== man
->meta
.title
) {
207 man_nmsg(man
, n
, MANDOCERR_NOTITLE
);
210 * If a title hasn't been set, do so now (by
211 * implication, date and section also aren't set).
214 man
->meta
.title
= mandoc_strdup("unknown");
215 man
->meta
.msec
= mandoc_strdup("1");
216 man
->meta
.date
= man
->quick
? mandoc_strdup("") :
217 mandoc_normdate(man
->parse
, NULL
, n
->line
, n
->pos
);
228 if (MAN_LITERAL
& man
->flags
)
232 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
233 man_pmsg(man
, n
->line
, (int)(p
- cp
), MANDOCERR_BADTAB
);
236 #define INEQ_DEFINE(x, ineq, name) \
238 check_##name(CHKARGS) \
240 if (n->nchild ineq (x)) \
242 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
243 "line arguments %s %d (have %d)", \
244 #ineq, (x), n->nchild); \
248 INEQ_DEFINE(0, ==, eq0
)
249 INEQ_DEFINE(2, ==, eq2
)
250 INEQ_DEFINE(1, <=, le1
)
251 INEQ_DEFINE(2, >=, ge2
)
252 INEQ_DEFINE(5, <=, le5
)
258 if (MAN_HEAD
== n
->type
&& 1 != n
->nchild
)
259 mandoc_vmsg(MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
260 n
->pos
, "line arguments eq 1 (have %d)", n
->nchild
);
275 cp
= n
->child
->string
;
294 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
298 if ('W' == cp
[1] && '\0' == cp
[2])
307 (MANDOCERR_BADFONT
, man
->parse
,
308 n
->line
, n
->pos
, "%s", cp
);
314 (MANDOCERR_ARGCOUNT
, man
->parse
, n
->line
,
315 n
->pos
, "want one child (have %d)",
325 if (MAN_BLOCK
== n
->type
)
326 man
->flags
&= ~MAN_LITERAL
;
334 if ( ! (MAN_HEAD
== n
->type
&& 0 == n
->nchild
))
337 man_nmsg(man
, n
, MANDOCERR_SYNTARGCOUNT
);
345 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
346 mandoc_msg(MANDOCERR_ARGCWARN
, man
->parse
, n
->line
,
347 n
->pos
, "want children (have none)");
359 if (0 == n
->body
->nchild
)
360 man_node_delete(man
, n
);
364 man_nmsg(man
, n
, MANDOCERR_IGNPAR
);
368 man_nmsg(man
, n
, MANDOCERR_ARGSLOST
);
383 if (0 == n
->head
->nchild
&& 0 == n
->body
->nchild
)
384 man_node_delete(man
, n
);
387 if (0 == n
->parent
->head
->nchild
&& 0 == n
->nchild
)
388 man_nmsg(man
, n
, MANDOCERR_IGNPAR
);
401 free(man
->meta
.title
);
403 free(man
->meta
.source
);
404 free(man
->meta
.msec
);
405 free(man
->meta
.date
);
407 man
->meta
.title
= man
->meta
.vol
= man
->meta
.date
=
408 man
->meta
.msec
= man
->meta
.source
= NULL
;
410 /* ->TITLE<- MSEC DATE SOURCE VOL */
413 if (n
&& n
->string
) {
414 for (p
= n
->string
; '\0' != *p
; p
++) {
415 /* Only warn about this once... */
416 if (isalpha((unsigned char)*p
) &&
417 ! isupper((unsigned char)*p
)) {
418 man_nmsg(man
, n
, MANDOCERR_UPPERCASE
);
422 man
->meta
.title
= mandoc_strdup(n
->string
);
424 man
->meta
.title
= mandoc_strdup("");
426 /* TITLE ->MSEC<- DATE SOURCE VOL */
431 man
->meta
.msec
= mandoc_strdup(n
->string
);
433 man
->meta
.msec
= mandoc_strdup("");
435 /* TITLE MSEC ->DATE<- SOURCE VOL */
439 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
440 man
->meta
.date
= man
->quick
?
441 mandoc_strdup(n
->string
) :
442 mandoc_normdate(man
->parse
, n
->string
,
445 man
->meta
.date
= mandoc_strdup("");
447 /* TITLE MSEC DATE ->SOURCE<- VOL */
449 if (n
&& (n
= n
->next
))
450 man
->meta
.source
= mandoc_strdup(n
->string
);
452 /* TITLE MSEC DATE SOURCE ->VOL<- */
453 /* If missing, use the default VOL name for MSEC. */
455 if (n
&& (n
= n
->next
))
456 man
->meta
.vol
= mandoc_strdup(n
->string
);
457 else if ('\0' != man
->meta
.msec
[0] &&
458 (NULL
!= (p
= mandoc_a2msec(man
->meta
.msec
))))
459 man
->meta
.vol
= mandoc_strdup(p
);
462 * Remove the `TH' node after we've processed it for our
465 man_node_delete(man
, man
->last
);
473 if (MAN_LITERAL
& man
->flags
)
474 man_nmsg(man
, n
, MANDOCERR_SCOPEREP
);
476 man
->flags
|= MAN_LITERAL
;
484 if ( ! (MAN_LITERAL
& man
->flags
))
485 man_nmsg(man
, n
, MANDOCERR_WNOSCOPE
);
487 man
->flags
&= ~MAN_LITERAL
;
494 static const char * const bsd_versions
[] = {
495 "3rd Berkeley Distribution",
496 "4th Berkeley Distribution",
497 "4.2 Berkeley Distribution",
498 "4.3 Berkeley Distribution",
499 "4.4 Berkeley Distribution",
506 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
510 if (0 == strcmp(s
, "3"))
512 else if (0 == strcmp(s
, "4"))
514 else if (0 == strcmp(s
, "5"))
516 else if (0 == strcmp(s
, "6"))
518 else if (0 == strcmp(s
, "7"))
524 free(man
->meta
.source
);
525 man
->meta
.source
= mandoc_strdup(p
);
532 static const char * const unix_versions
[] = {
536 "System V Release 2",
544 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
545 p
= unix_versions
[0];
548 if (0 == strcmp(s
, "3"))
549 p
= unix_versions
[0];
550 else if (0 == strcmp(s
, "4"))
551 p
= unix_versions
[1];
552 else if (0 == strcmp(s
, "5")) {
554 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
555 p
= unix_versions
[3];
557 p
= unix_versions
[2];
559 p
= unix_versions
[0];
562 free(man
->meta
.source
);
563 man
->meta
.source
= mandoc_strdup(p
);
574 switch (n
->parent
->tok
) {
578 man_nmsg(man
, n
, MANDOCERR_IGNPAR
);
582 * Don't warn about this because it occurs in pod2man
583 * and would cause considerable (unfixable) warnage.
585 man_node_delete(man
, n
);