]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.77 2011/11/03 20:48:52 schwarze 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_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_fi
[] = { check_eq0
, post_fi
, NULL
};
71 static v_check posts_ft
[] = { post_ft
, NULL
};
72 static v_check posts_nf
[] = { check_eq0
, post_nf
, NULL
};
73 static v_check posts_par
[] = { check_par
, NULL
};
74 static v_check posts_part
[] = { check_part
, NULL
};
75 static v_check posts_sec
[] = { post_sec
, NULL
};
76 static v_check posts_sp
[] = { post_vs
, check_le1
, NULL
};
77 static v_check posts_th
[] = { check_ge2
, check_le5
, post_TH
, NULL
};
78 static v_check posts_uc
[] = { post_UC
, NULL
};
79 static v_check pres_bline
[] = { check_bline
, NULL
};
80 static v_check pres_sec
[] = { check_bline
, pre_sec
, NULL
};
82 static const struct man_valid man_valids
[MAN_MAX
] = {
83 { NULL
, posts_br
}, /* br */
84 { pres_bline
, posts_th
}, /* TH */
85 { pres_sec
, posts_sec
}, /* SH */
86 { pres_sec
, posts_sec
}, /* SS */
87 { pres_bline
, NULL
}, /* TP */
88 { pres_bline
, posts_par
}, /* LP */
89 { pres_bline
, posts_par
}, /* PP */
90 { pres_bline
, posts_par
}, /* P */
91 { pres_bline
, NULL
}, /* IP */
92 { pres_bline
, 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 */ /* FIXME: should warn only. */
105 { NULL
, posts_sp
}, /* sp */ /* FIXME: should warn only. */
106 { pres_bline
, posts_nf
}, /* nf */
107 { pres_bline
, 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 */
120 man_valid_pre(struct man
*m
, struct man_node
*n
)
137 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
147 man_valid_post(struct man
*m
)
151 if (MAN_VALID
& m
->last
->flags
)
153 m
->last
->flags
|= MAN_VALID
;
155 switch (m
->last
->type
) {
157 check_text(m
, m
->last
);
160 return(check_root(m
, m
->last
));
169 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
172 if ( ! (*cp
)(m
, m
->last
))
183 if (MAN_BLINE
& m
->flags
)
184 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
185 else if (MAN_ELINE
& m
->flags
)
186 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
188 m
->flags
&= ~MAN_BLINE
;
189 m
->flags
&= ~MAN_ELINE
;
191 if (NULL
== m
->first
->child
) {
192 man_nmsg(m
, n
, MANDOCERR_NODOCBODY
);
194 } else if (NULL
== m
->meta
.title
) {
195 man_nmsg(m
, n
, MANDOCERR_NOTITLE
);
198 * If a title hasn't been set, do so now (by
199 * implication, date and section also aren't set).
202 m
->meta
.title
= mandoc_strdup("unknown");
203 m
->meta
.msec
= mandoc_strdup("1");
204 m
->meta
.date
= mandoc_normdate
205 (m
->parse
, NULL
, n
->line
, n
->pos
);
216 if (MAN_LITERAL
& m
->flags
)
220 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
221 man_pmsg(m
, n
->line
, (int)(p
- cp
), MANDOCERR_BADTAB
);
224 #define INEQ_DEFINE(x, ineq, name) \
226 check_##name(CHKARGS) \
228 if (n->nchild ineq (x)) \
230 mandoc_vmsg(MANDOCERR_ARGCOUNT, m->parse, n->line, n->pos, \
231 "line arguments %s %d (have %d)", \
232 #ineq, (x), n->nchild); \
236 INEQ_DEFINE(0, ==, eq0
)
237 INEQ_DEFINE(1, <=, le1
)
238 INEQ_DEFINE(2, >=, ge2
)
239 INEQ_DEFINE(5, <=, le5
)
251 cp
= n
->child
->string
;
270 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
274 if ('W' == cp
[1] && '\0' == cp
[2])
283 (MANDOCERR_BADFONT
, m
->parse
,
284 n
->line
, n
->pos
, "%s", cp
);
290 (MANDOCERR_ARGCOUNT
, m
->parse
, n
->line
,
291 n
->pos
, "want one child (have %d)",
301 if (MAN_BLOCK
== n
->type
)
302 m
->flags
&= ~MAN_LITERAL
;
310 if ( ! (MAN_HEAD
== n
->type
&& 0 == n
->nchild
))
313 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
321 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
322 mandoc_msg(MANDOCERR_ARGCWARN
, m
->parse
, n
->line
,
323 n
->pos
, "want children (have none)");
335 if (0 == n
->body
->nchild
)
336 man_node_delete(m
, n
);
340 man_nmsg(m
, n
, MANDOCERR_IGNPAR
);
344 man_nmsg(m
, n
, MANDOCERR_ARGSLOST
);
358 assert( ! (MAN_ELINE
& m
->flags
));
359 if (MAN_BLINE
& m
->flags
) {
360 man_nmsg(m
, n
, MANDOCERR_SYNTLINESCOPE
);
378 free(m
->meta
.source
);
386 m
->meta
.title
= m
->meta
.vol
= m
->meta
.date
=
387 m
->meta
.msec
= m
->meta
.source
= NULL
;
389 /* ->TITLE<- MSEC DATE SOURCE VOL */
392 if (n
&& n
->string
) {
393 for (p
= n
->string
; '\0' != *p
; p
++) {
394 /* Only warn about this once... */
395 if (isalpha((unsigned char)*p
) &&
396 ! isupper((unsigned char)*p
)) {
397 man_nmsg(m
, n
, MANDOCERR_UPPERCASE
);
401 m
->meta
.title
= mandoc_strdup(n
->string
);
403 m
->meta
.title
= mandoc_strdup("");
405 /* TITLE ->MSEC<- DATE SOURCE VOL */
410 m
->meta
.msec
= mandoc_strdup(n
->string
);
412 m
->meta
.msec
= mandoc_strdup("");
414 /* TITLE MSEC ->DATE<- SOURCE VOL */
418 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
420 m
->meta
.date
= mandoc_normdate
421 (m
->parse
, n
->string
, line
, pos
);
423 m
->meta
.date
= mandoc_strdup("");
425 /* TITLE MSEC DATE ->SOURCE<- VOL */
427 if (n
&& (n
= n
->next
))
428 m
->meta
.source
= mandoc_strdup(n
->string
);
430 /* TITLE MSEC DATE SOURCE ->VOL<- */
432 if (n
&& (n
= n
->next
))
433 m
->meta
.vol
= mandoc_strdup(n
->string
);
436 * Remove the `TH' node after we've processed it for our
439 man_node_delete(m
, m
->last
);
447 if (MAN_LITERAL
& m
->flags
)
448 man_nmsg(m
, n
, MANDOCERR_SCOPEREP
);
450 m
->flags
|= MAN_LITERAL
;
458 if ( ! (MAN_LITERAL
& m
->flags
))
459 man_nmsg(m
, n
, MANDOCERR_WNOSCOPE
);
461 m
->flags
&= ~MAN_LITERAL
;
468 static const char * const bsd_versions
[] = {
469 "3rd Berkeley Distribution",
470 "4th Berkeley Distribution",
471 "4.2 Berkeley Distribution",
472 "4.3 Berkeley Distribution",
473 "4.4 Berkeley Distribution",
480 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
484 if (0 == strcmp(s
, "3"))
486 else if (0 == strcmp(s
, "4"))
488 else if (0 == strcmp(s
, "5"))
490 else if (0 == strcmp(s
, "6"))
492 else if (0 == strcmp(s
, "7"))
499 free(m
->meta
.source
);
501 m
->meta
.source
= mandoc_strdup(p
);
508 static const char * const unix_versions
[] = {
512 "System V Release 2",
520 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
521 p
= unix_versions
[0];
524 if (0 == strcmp(s
, "3"))
525 p
= unix_versions
[0];
526 else if (0 == strcmp(s
, "4"))
527 p
= unix_versions
[1];
528 else if (0 == strcmp(s
, "5")) {
530 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
531 p
= unix_versions
[3];
533 p
= unix_versions
[2];
535 p
= unix_versions
[0];
539 free(m
->meta
.source
);
541 m
->meta
.source
= mandoc_strdup(p
);
550 * Don't warn about this because it occurs in pod2man and would
551 * cause considerable (unfixable) warnage.
553 if (NULL
== n
->prev
&& MAN_ROOT
== n
->parent
->type
)
554 man_node_delete(m
, n
);