1 /* $Id: man_validate.c,v 1.80 2012/01/03 15:16:24 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 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>
36 #include "libmandoc.h"
38 #define CHKARGS struct man *m, struct man_node *n
40 typedef int (*v_check
)(CHKARGS
);
47 static int check_eq0(CHKARGS
);
48 static int check_eq2(CHKARGS
);
49 static int check_le1(CHKARGS
);
50 static int check_ge2(CHKARGS
);
51 static int check_le5(CHKARGS
);
52 static int check_par(CHKARGS
);
53 static int check_part(CHKARGS
);
54 static int check_root(CHKARGS
);
55 static void check_text(CHKARGS
);
57 static int post_AT(CHKARGS
);
58 static int post_vs(CHKARGS
);
59 static int post_fi(CHKARGS
);
60 static int post_ft(CHKARGS
);
61 static int post_nf(CHKARGS
);
62 static int post_sec(CHKARGS
);
63 static int post_TH(CHKARGS
);
64 static int post_UC(CHKARGS
);
65 static int pre_sec(CHKARGS
);
67 static v_check posts_at
[] = { post_AT
, NULL
};
68 static v_check posts_br
[] = { post_vs
, check_eq0
, NULL
};
69 static v_check posts_eq0
[] = { check_eq0
, NULL
};
70 static v_check posts_eq2
[] = { check_eq2
, NULL
};
71 static v_check posts_fi
[] = { check_eq0
, post_fi
, NULL
};
72 static v_check posts_ft
[] = { post_ft
, NULL
};
73 static v_check posts_nf
[] = { check_eq0
, post_nf
, NULL
};
74 static v_check posts_par
[] = { check_par
, NULL
};
75 static v_check posts_part
[] = { check_part
, NULL
};
76 static v_check posts_sec
[] = { post_sec
, NULL
};
77 static v_check posts_sp
[] = { post_vs
, check_le1
, NULL
};
78 static v_check posts_th
[] = { check_ge2
, check_le5
, post_TH
, NULL
};
79 static v_check posts_uc
[] = { post_UC
, NULL
};
80 static v_check pres_sec
[] = { pre_sec
, NULL
};
82 static const struct man_valid man_valids
[MAN_MAX
] = {
83 { NULL
, posts_br
}, /* br */
84 { NULL
, posts_th
}, /* TH */
85 { pres_sec
, posts_sec
}, /* SH */
86 { pres_sec
, posts_sec
}, /* SS */
87 { NULL
, NULL
}, /* TP */
88 { NULL
, posts_par
}, /* LP */
89 { NULL
, posts_par
}, /* PP */
90 { NULL
, posts_par
}, /* P */
91 { NULL
, NULL
}, /* IP */
92 { NULL
, NULL
}, /* HP */
93 { NULL
, NULL
}, /* SM */
94 { NULL
, NULL
}, /* SB */
95 { NULL
, NULL
}, /* BI */
96 { NULL
, NULL
}, /* IB */
97 { NULL
, NULL
}, /* BR */
98 { NULL
, NULL
}, /* RB */
99 { NULL
, NULL
}, /* R */
100 { NULL
, NULL
}, /* B */
101 { NULL
, NULL
}, /* I */
102 { NULL
, NULL
}, /* IR */
103 { NULL
, NULL
}, /* RI */
104 { NULL
, posts_eq0
}, /* na */
105 { NULL
, posts_sp
}, /* sp */
106 { NULL
, posts_nf
}, /* nf */
107 { NULL
, posts_fi
}, /* fi */
108 { NULL
, NULL
}, /* RE */
109 { NULL
, posts_part
}, /* RS */
110 { NULL
, NULL
}, /* DT */
111 { NULL
, posts_uc
}, /* UC */
112 { NULL
, NULL
}, /* PD */
113 { NULL
, posts_at
}, /* AT */
114 { NULL
, NULL
}, /* in */
115 { NULL
, posts_ft
}, /* ft */
116 { NULL
, posts_eq2
}, /* OP */
121 man_valid_pre(struct man
*m
, struct man_node
*n
)
138 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
148 man_valid_post(struct man
*m
)
152 if (MAN_VALID
& m
->last
->flags
)
154 m
->last
->flags
|= MAN_VALID
;
156 switch (m
->last
->type
) {
158 check_text(m
, m
->last
);
161 return(check_root(m
, m
->last
));
170 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
173 if ( ! (*cp
)(m
, m
->last
))
184 if (MAN_BLINE
& m
->flags
)
185 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
186 else if (MAN_ELINE
& m
->flags
)
187 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
189 m
->flags
&= ~MAN_BLINE
;
190 m
->flags
&= ~MAN_ELINE
;
192 if (NULL
== m
->first
->child
) {
193 man_nmsg(m
, n
, MANDOCERR_NODOCBODY
);
195 } else if (NULL
== m
->meta
.title
) {
196 man_nmsg(m
, n
, MANDOCERR_NOTITLE
);
199 * If a title hasn't been set, do so now (by
200 * implication, date and section also aren't set).
203 m
->meta
.title
= mandoc_strdup("unknown");
204 m
->meta
.msec
= mandoc_strdup("1");
205 m
->meta
.date
= mandoc_normdate
206 (m
->parse
, NULL
, n
->line
, n
->pos
);
217 if (MAN_LITERAL
& m
->flags
)
221 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
222 man_pmsg(m
, n
->line
, (int)(p
- cp
), MANDOCERR_BADTAB
);
225 #define INEQ_DEFINE(x, ineq, name) \
227 check_##name(CHKARGS) \
229 if (n->nchild ineq (x)) \
231 mandoc_vmsg(MANDOCERR_ARGCOUNT, m->parse, n->line, n->pos, \
232 "line arguments %s %d (have %d)", \
233 #ineq, (x), n->nchild); \
237 INEQ_DEFINE(0, ==, eq0
)
238 INEQ_DEFINE(2, ==, eq2
)
239 INEQ_DEFINE(1, <=, le1
)
240 INEQ_DEFINE(2, >=, ge2
)
241 INEQ_DEFINE(5, <=, le5
)
253 cp
= n
->child
->string
;
272 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
276 if ('W' == cp
[1] && '\0' == cp
[2])
285 (MANDOCERR_BADFONT
, m
->parse
,
286 n
->line
, n
->pos
, "%s", cp
);
292 (MANDOCERR_ARGCOUNT
, m
->parse
, n
->line
,
293 n
->pos
, "want one child (have %d)",
303 if (MAN_BLOCK
== n
->type
)
304 m
->flags
&= ~MAN_LITERAL
;
312 if ( ! (MAN_HEAD
== n
->type
&& 0 == n
->nchild
))
315 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
323 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
324 mandoc_msg(MANDOCERR_ARGCWARN
, m
->parse
, n
->line
,
325 n
->pos
, "want children (have none)");
337 if (0 == n
->body
->nchild
)
338 man_node_delete(m
, n
);
342 man_nmsg(m
, n
, MANDOCERR_IGNPAR
);
346 man_nmsg(m
, n
, MANDOCERR_ARGSLOST
);
367 free(m
->meta
.source
);
375 m
->meta
.title
= m
->meta
.vol
= m
->meta
.date
=
376 m
->meta
.msec
= m
->meta
.source
= NULL
;
378 /* ->TITLE<- MSEC DATE SOURCE VOL */
381 if (n
&& n
->string
) {
382 for (p
= n
->string
; '\0' != *p
; p
++) {
383 /* Only warn about this once... */
384 if (isalpha((unsigned char)*p
) &&
385 ! isupper((unsigned char)*p
)) {
386 man_nmsg(m
, n
, MANDOCERR_UPPERCASE
);
390 m
->meta
.title
= mandoc_strdup(n
->string
);
392 m
->meta
.title
= mandoc_strdup("");
394 /* TITLE ->MSEC<- DATE SOURCE VOL */
399 m
->meta
.msec
= mandoc_strdup(n
->string
);
401 m
->meta
.msec
= mandoc_strdup("");
403 /* TITLE MSEC ->DATE<- SOURCE VOL */
407 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
409 m
->meta
.date
= mandoc_normdate
410 (m
->parse
, n
->string
, line
, pos
);
412 m
->meta
.date
= mandoc_strdup("");
414 /* TITLE MSEC DATE ->SOURCE<- VOL */
416 if (n
&& (n
= n
->next
))
417 m
->meta
.source
= mandoc_strdup(n
->string
);
419 /* TITLE MSEC DATE SOURCE ->VOL<- */
420 /* If missing, use the default VOL name for MSEC. */
422 if (n
&& (n
= n
->next
))
423 m
->meta
.vol
= mandoc_strdup(n
->string
);
424 else if ('\0' != m
->meta
.msec
[0] &&
425 (NULL
!= (p
= mandoc_a2msec(m
->meta
.msec
))))
426 m
->meta
.vol
= mandoc_strdup(p
);
429 * Remove the `TH' node after we've processed it for our
432 man_node_delete(m
, m
->last
);
440 if (MAN_LITERAL
& m
->flags
)
441 man_nmsg(m
, n
, MANDOCERR_SCOPEREP
);
443 m
->flags
|= MAN_LITERAL
;
451 if ( ! (MAN_LITERAL
& m
->flags
))
452 man_nmsg(m
, n
, MANDOCERR_WNOSCOPE
);
454 m
->flags
&= ~MAN_LITERAL
;
461 static const char * const bsd_versions
[] = {
462 "3rd Berkeley Distribution",
463 "4th Berkeley Distribution",
464 "4.2 Berkeley Distribution",
465 "4.3 Berkeley Distribution",
466 "4.4 Berkeley Distribution",
473 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
477 if (0 == strcmp(s
, "3"))
479 else if (0 == strcmp(s
, "4"))
481 else if (0 == strcmp(s
, "5"))
483 else if (0 == strcmp(s
, "6"))
485 else if (0 == strcmp(s
, "7"))
492 free(m
->meta
.source
);
494 m
->meta
.source
= mandoc_strdup(p
);
501 static const char * const unix_versions
[] = {
505 "System V Release 2",
513 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
514 p
= unix_versions
[0];
517 if (0 == strcmp(s
, "3"))
518 p
= unix_versions
[0];
519 else if (0 == strcmp(s
, "4"))
520 p
= unix_versions
[1];
521 else if (0 == strcmp(s
, "5")) {
523 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
524 p
= unix_versions
[3];
526 p
= unix_versions
[2];
528 p
= unix_versions
[0];
532 free(m
->meta
.source
);
534 m
->meta
.source
= mandoc_strdup(p
);
543 * Don't warn about this because it occurs in pod2man and would
544 * cause considerable (unfixable) warnage.
546 if (NULL
== n
->prev
&& MAN_ROOT
== n
->parent
->type
)
547 man_node_delete(m
, n
);