1 /* $Id: man_validate.c,v 1.67 2011/03/22 15:30:30 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_bline(CHKARGS);
48 static int check_eq0(CHKARGS);
49 static int check_ft(CHKARGS);
50 static int check_le1(CHKARGS);
51 static int check_ge2(CHKARGS);
52 static int check_le5(CHKARGS);
53 static int check_par(CHKARGS);
54 static int check_part(CHKARGS);
55 static int check_root(CHKARGS);
56 static int check_sec(CHKARGS);
57 static int check_text(CHKARGS);
59 static int post_AT(CHKARGS);
60 static int post_fi(CHKARGS);
61 static int post_nf(CHKARGS);
62 static int post_TH(CHKARGS);
63 static int post_UC(CHKARGS);
65 static v_check posts_at[] = { post_AT, NULL };
66 static v_check posts_eq0[] = { check_eq0, NULL };
67 static v_check posts_fi[] = { check_eq0, post_fi, NULL };
68 static v_check posts_le1[] = { check_le1, NULL };
69 static v_check posts_ft[] = { check_ft, NULL };
70 static v_check posts_nf[] = { check_eq0, post_nf, NULL };
71 static v_check posts_par[] = { check_par, NULL };
72 static v_check posts_part[] = { check_part, NULL };
73 static v_check posts_sec[] = { check_sec, NULL };
74 static v_check posts_th[] = { check_ge2, check_le5, post_TH, NULL };
75 static v_check posts_uc[] = { post_UC, NULL };
76 static v_check pres_bline[] = { check_bline, NULL };
79 static const struct man_valid man_valids[MAN_MAX] = {
80 { NULL, posts_eq0 }, /* br */
81 { pres_bline, posts_th }, /* TH */
82 { pres_bline, posts_sec }, /* SH */
83 { pres_bline, posts_sec }, /* SS */
84 { pres_bline, NULL }, /* TP */
85 { pres_bline, posts_par }, /* LP */
86 { pres_bline, posts_par }, /* PP */
87 { pres_bline, posts_par }, /* P */
88 { pres_bline, NULL }, /* IP */
89 { pres_bline, NULL }, /* HP */
90 { NULL, NULL }, /* SM */
91 { NULL, NULL }, /* SB */
92 { NULL, NULL }, /* BI */
93 { NULL, NULL }, /* IB */
94 { NULL, NULL }, /* BR */
95 { NULL, NULL }, /* RB */
96 { NULL, NULL }, /* R */
97 { NULL, NULL }, /* B */
98 { NULL, NULL }, /* I */
99 { NULL, NULL }, /* IR */
100 { NULL, NULL }, /* RI */
101 { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
102 { NULL, posts_le1 }, /* sp */ /* FIXME: should warn only. */
103 { pres_bline, posts_nf }, /* nf */
104 { pres_bline, posts_fi }, /* fi */
105 { NULL, NULL }, /* RE */
106 { NULL, posts_part }, /* RS */
107 { NULL, NULL }, /* DT */
108 { NULL, posts_uc }, /* UC */
109 { NULL, NULL }, /* PD */
110 { NULL, posts_at }, /* AT */
111 { NULL, NULL }, /* in */
112 { NULL, posts_ft }, /* ft */
117 man_valid_pre(struct man *m, struct man_node *n)
134 if (NULL == (cp = man_valids[n->tok].pres))
144 man_valid_post(struct man *m)
148 if (MAN_VALID & m->last->flags)
150 m->last->flags |= MAN_VALID;
152 switch (m->last->type) {
154 return(check_text(m, m->last));
156 return(check_root(m, m->last));
165 if (NULL == (cp = man_valids[m->last->tok].posts))
168 if ( ! (*cp)(m, m->last))
179 if (MAN_BLINE & m->flags)
180 man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
181 else if (MAN_ELINE & m->flags)
182 man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
184 m->flags &= ~MAN_BLINE;
185 m->flags &= ~MAN_ELINE;
187 if (NULL == m->first->child) {
188 man_nmsg(m, n, MANDOCERR_NODOCBODY);
190 } else if (NULL == m->meta.title) {
191 man_nmsg(m, n, MANDOCERR_NOTITLE);
194 * If a title hasn't been set, do so now (by
195 * implication, date and section also aren't set).
198 m->meta.title = mandoc_strdup("unknown");
199 m->meta.msec = mandoc_strdup("1");
200 m->meta.date = mandoc_normdate
201 (m->parse, NULL, n->line, n->pos);
215 for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
216 sz = strcspn(p, "\t\\");
225 if (MAN_LITERAL & m->flags)
227 man_pmsg(m, n->line, pos, MANDOCERR_BADTAB);
231 /* Check the special character. */
233 c = mandoc_special(p);
238 man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
245 #define INEQ_DEFINE(x, ineq, name) \
247 check_##name(CHKARGS) \
249 if (n->nchild ineq (x)) \
251 mandoc_vmsg(MANDOCERR_ARGCOUNT, m->parse, n->line, n->pos, \
252 "line arguments %s %d (have %d)", \
253 #ineq, (x), n->nchild); \
257 INEQ_DEFINE(0, ==, eq0)
258 INEQ_DEFINE(1, <=, le1)
259 INEQ_DEFINE(2, >=, ge2)
260 INEQ_DEFINE(5, <=, le5)
272 cp = n->child->string;
291 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
295 if ('W' == cp[1] && '\0' == cp[2])
304 (MANDOCERR_BADFONT, m->parse,
305 n->line, n->pos, "%s", cp);
311 (MANDOCERR_ARGCOUNT, m->parse, n->line,
312 n->pos, "want one child (have %d)",
322 if (MAN_HEAD == n->type && 0 == n->nchild) {
323 man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
325 } else if (MAN_BODY == n->type && 0 == n->nchild)
326 mandoc_msg(MANDOCERR_ARGCWARN, m->parse, n->line,
327 n->pos, "want children (have none)");
337 if (MAN_BODY == n->type && 0 == n->nchild)
338 mandoc_msg(MANDOCERR_ARGCWARN, m->parse, n->line,
339 n->pos, "want children (have none)");
351 if (0 == n->body->nchild)
352 man_node_delete(m, n);
356 man_nmsg(m, n, MANDOCERR_IGNPAR);
360 man_nmsg(m, n, MANDOCERR_ARGSLOST);
374 assert( ! (MAN_ELINE & m->flags));
375 if (MAN_BLINE & m->flags) {
376 man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
394 free(m->meta.source);
402 m->meta.title = m->meta.vol = m->meta.date =
403 m->meta.msec = m->meta.source = NULL;
405 /* ->TITLE<- MSEC DATE SOURCE VOL */
408 if (n && n->string) {
409 for (p = n->string; '\0' != *p; p++) {
410 /* Only warn about this once... */
411 if (isalpha((u_char)*p) && ! isupper((u_char)*p)) {
412 man_nmsg(m, n, MANDOCERR_UPPERCASE);
416 m->meta.title = mandoc_strdup(n->string);
418 m->meta.title = mandoc_strdup("");
420 /* TITLE ->MSEC<- DATE SOURCE VOL */
425 m->meta.msec = mandoc_strdup(n->string);
427 m->meta.msec = mandoc_strdup("");
429 /* TITLE MSEC ->DATE<- SOURCE VOL */
435 m->meta.date = mandoc_normdate
436 (m->parse, n ? n->string : NULL, line, pos);
438 /* TITLE MSEC DATE ->SOURCE<- VOL */
440 if (n && (n = n->next))
441 m->meta.source = mandoc_strdup(n->string);
443 /* TITLE MSEC DATE SOURCE ->VOL<- */
445 if (n && (n = n->next))
446 m->meta.vol = mandoc_strdup(n->string);
449 * Remove the `TH' node after we've processed it for our
452 man_node_delete(m, m->last);
460 if (MAN_LITERAL & m->flags)
461 man_nmsg(m, n, MANDOCERR_SCOPEREP);
463 m->flags |= MAN_LITERAL;
471 if ( ! (MAN_LITERAL & m->flags))
472 man_nmsg(m, n, MANDOCERR_WNOSCOPE);
474 m->flags &= ~MAN_LITERAL;
481 static const char * const bsd_versions[] = {
482 "3rd Berkeley Distribution",
483 "4th Berkeley Distribution",
484 "4.2 Berkeley Distribution",
485 "4.3 Berkeley Distribution",
486 "4.4 Berkeley Distribution",
494 if (NULL == n || MAN_TEXT != n->type)
498 if (0 == strcmp(s, "3"))
500 else if (0 == strcmp(s, "4"))
502 else if (0 == strcmp(s, "5"))
504 else if (0 == strcmp(s, "6"))
506 else if (0 == strcmp(s, "7"))
513 free(m->meta.source);
515 m->meta.source = mandoc_strdup(p);
522 static const char * const unix_versions[] = {
526 "System V Release 2",
534 if (NULL == n || MAN_TEXT != n->type)
535 p = unix_versions[0];
538 if (0 == strcmp(s, "3"))
539 p = unix_versions[0];
540 else if (0 == strcmp(s, "4"))
541 p = unix_versions[1];
542 else if (0 == strcmp(s, "5")) {
544 if (nn && MAN_TEXT == nn->type && nn->string[0])
545 p = unix_versions[3];
547 p = unix_versions[2];
549 p = unix_versions[0];
553 free(m->meta.source);
555 m->meta.source = mandoc_strdup(p);