Annotation of src/usr.bin/mandoc/man_validate.c, Revision 1.17
1.17 ! schwarze 1: /* $Id: man_validate.c,v 1.16 2010/04/02 11:37:07 schwarze Exp $ */
1.1 kristaps 2: /*
1.2 schwarze 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.2 schwarze 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 16: */
17: #include <sys/types.h>
18:
19: #include <assert.h>
20: #include <ctype.h>
1.6 schwarze 21: #include <errno.h>
22: #include <limits.h>
1.1 kristaps 23: #include <stdarg.h>
24: #include <stdlib.h>
25:
26: #include "libman.h"
1.5 schwarze 27: #include "libmandoc.h"
1.1 kristaps 28:
1.7 schwarze 29: #define CHKARGS struct man *m, const struct man_node *n
1.1 kristaps 30:
1.7 schwarze 31: typedef int (*v_check)(CHKARGS);
1.1 kristaps 32:
33: struct man_valid {
1.7 schwarze 34: v_check *pres;
35: v_check *posts;
1.1 kristaps 36: };
37:
1.7 schwarze 38: static int check_bline(CHKARGS);
39: static int check_eq0(CHKARGS);
1.10 schwarze 40: static int check_le1(CHKARGS);
1.7 schwarze 41: static int check_ge2(CHKARGS);
42: static int check_le5(CHKARGS);
43: static int check_par(CHKARGS);
1.8 schwarze 44: static int check_part(CHKARGS);
1.14 schwarze 45: static int check_roff(CHKARGS);
1.7 schwarze 46: static int check_root(CHKARGS);
47: static int check_sec(CHKARGS);
48: static int check_text(CHKARGS);
1.15 schwarze 49: static int check_title(CHKARGS);
1.7 schwarze 50:
51: static v_check posts_eq0[] = { check_eq0, NULL };
1.15 schwarze 52: static v_check posts_th[] = { check_ge2, check_le5, check_title, NULL };
1.7 schwarze 53: static v_check posts_par[] = { check_par, NULL };
1.8 schwarze 54: static v_check posts_part[] = { check_part, NULL };
1.7 schwarze 55: static v_check posts_sec[] = { check_sec, NULL };
1.13 schwarze 56: static v_check posts_le1[] = { check_le1, NULL };
1.7 schwarze 57: static v_check pres_bline[] = { check_bline, NULL };
1.16 schwarze 58: static v_check pres_roff[] = { check_roff, NULL };
1.1 kristaps 59:
60: static const struct man_valid man_valids[MAN_MAX] = {
1.12 schwarze 61: { NULL, posts_eq0 }, /* br */
1.15 schwarze 62: { pres_bline, posts_th }, /* TH */
1.7 schwarze 63: { pres_bline, posts_sec }, /* SH */
64: { pres_bline, posts_sec }, /* SS */
65: { pres_bline, posts_par }, /* TP */
66: { pres_bline, posts_par }, /* LP */
67: { pres_bline, posts_par }, /* PP */
68: { pres_bline, posts_par }, /* P */
69: { pres_bline, posts_par }, /* IP */
70: { pres_bline, posts_par }, /* HP */
1.8 schwarze 71: { NULL, NULL }, /* SM */
72: { NULL, NULL }, /* SB */
1.7 schwarze 73: { NULL, NULL }, /* BI */
74: { NULL, NULL }, /* IB */
75: { NULL, NULL }, /* BR */
76: { NULL, NULL }, /* RB */
1.8 schwarze 77: { NULL, NULL }, /* R */
78: { NULL, NULL }, /* B */
79: { NULL, NULL }, /* I */
1.7 schwarze 80: { NULL, NULL }, /* IR */
81: { NULL, NULL }, /* RI */
1.12 schwarze 82: { NULL, posts_eq0 }, /* na */
1.7 schwarze 83: { NULL, NULL }, /* i */
1.13 schwarze 84: { NULL, posts_le1 }, /* sp */
1.7 schwarze 85: { pres_bline, posts_eq0 }, /* nf */
86: { pres_bline, posts_eq0 }, /* fi */
87: { NULL, NULL }, /* r */
1.8 schwarze 88: { NULL, NULL }, /* RE */
89: { NULL, posts_part }, /* RS */
90: { NULL, NULL }, /* DT */
1.9 schwarze 91: { NULL, NULL }, /* UC */
1.11 schwarze 92: { NULL, NULL }, /* PD */
1.13 schwarze 93: { NULL, posts_le1 }, /* Sp */
94: { pres_bline, posts_le1 }, /* Vb */
95: { pres_bline, posts_eq0 }, /* Ve */
1.14 schwarze 96: { pres_roff, NULL }, /* de */
97: { pres_roff, NULL }, /* dei */
98: { pres_roff, NULL }, /* am */
99: { pres_roff, NULL }, /* ami */
100: { pres_roff, NULL }, /* ig */
101: { NULL, NULL }, /* . */
1.1 kristaps 102: };
103:
104:
105: int
1.7 schwarze 106: man_valid_pre(struct man *m, const struct man_node *n)
107: {
108: v_check *cp;
109:
110: if (MAN_TEXT == n->type)
111: return(1);
112: if (MAN_ROOT == n->type)
113: return(1);
114:
115: if (NULL == (cp = man_valids[n->tok].pres))
116: return(1);
117: for ( ; *cp; cp++)
118: if ( ! (*cp)(m, n))
119: return(0);
120: return(1);
121: }
122:
123:
124: int
1.1 kristaps 125: man_valid_post(struct man *m)
126: {
1.7 schwarze 127: v_check *cp;
1.1 kristaps 128:
129: if (MAN_VALID & m->last->flags)
130: return(1);
131: m->last->flags |= MAN_VALID;
132:
133: switch (m->last->type) {
134: case (MAN_TEXT):
1.4 schwarze 135: return(check_text(m, m->last));
1.1 kristaps 136: case (MAN_ROOT):
1.4 schwarze 137: return(check_root(m, m->last));
1.1 kristaps 138: default:
139: break;
140: }
141:
142: if (NULL == (cp = man_valids[m->last->tok].posts))
143: return(1);
144: for ( ; *cp; cp++)
145: if ( ! (*cp)(m, m->last))
146: return(0);
147:
148: return(1);
149: }
150:
151:
1.4 schwarze 152: static int
1.7 schwarze 153: check_root(CHKARGS)
1.4 schwarze 154: {
1.7 schwarze 155:
156: if (MAN_BLINE & m->flags)
1.8 schwarze 157: return(man_nwarn(m, n, WEXITSCOPE));
1.7 schwarze 158: if (MAN_ELINE & m->flags)
1.8 schwarze 159: return(man_nwarn(m, n, WEXITSCOPE));
160:
161: m->flags &= ~MAN_BLINE;
162: m->flags &= ~MAN_ELINE;
1.7 schwarze 163:
1.4 schwarze 164: if (NULL == m->first->child)
165: return(man_nerr(m, n, WNODATA));
1.17 ! schwarze 166: if (NULL == m->meta.title) {
! 167: if ( ! man_nwarn(m, n, WNOTITLE))
! 168: return(0);
! 169: m->meta.title = mandoc_strdup("unknown");
! 170: }
1.15 schwarze 171:
172: return(1);
173: }
174:
175:
176: static int
177: check_title(CHKARGS)
178: {
179: const char *p;
180:
181: assert(n->child);
182: if ('\0' == *n->child->string)
183: return(man_nerr(m, n, WNOTITLE));
184:
185: for (p = n->child->string; '\0' != *p; p++)
186: if (isalpha((u_char)*p) && ! isupper((u_char)*p))
187: if ( ! man_nwarn(m, n, WTITLECASE))
188: return(0);
1.4 schwarze 189:
190: return(1);
191: }
192:
193:
194: static int
1.7 schwarze 195: check_text(CHKARGS)
1.4 schwarze 196: {
197: const char *p;
1.5 schwarze 198: int pos, c;
1.4 schwarze 199:
200: assert(n->string);
201:
202: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
1.5 schwarze 203: if ('\\' == *p) {
204: c = mandoc_special(p);
205: if (c) {
206: p += c - 1;
207: pos += c - 1;
208: continue;
209: }
210: if ( ! (MAN_IGN_ESCAPE & m->pflags))
211: return(man_perr(m, n->line, pos, WESCAPE));
212: if ( ! man_pwarn(m, n->line, pos, WESCAPE))
213: return(0);
214: continue;
215: }
216:
217: if ('\t' == *p || isprint((u_char)*p))
1.4 schwarze 218: continue;
219:
220: if (MAN_IGN_CHARS & m->pflags)
221: return(man_pwarn(m, n->line, pos, WNPRINT));
222: return(man_perr(m, n->line, pos, WNPRINT));
223: }
224:
225: return(1);
1.1 kristaps 226: }
227:
228:
229: #define INEQ_DEFINE(x, ineq, name) \
230: static int \
1.7 schwarze 231: check_##name(CHKARGS) \
1.1 kristaps 232: { \
1.4 schwarze 233: if (n->nchild ineq (x)) \
1.1 kristaps 234: return(1); \
235: return(man_verr(m, n->line, n->pos, \
236: "expected line arguments %s %d, have %d", \
1.4 schwarze 237: #ineq, (x), n->nchild)); \
1.1 kristaps 238: }
239:
240: INEQ_DEFINE(0, ==, eq0)
1.10 schwarze 241: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 242: INEQ_DEFINE(2, >=, ge2)
243: INEQ_DEFINE(5, <=, le5)
1.7 schwarze 244:
245:
246: static int
247: check_sec(CHKARGS)
248: {
1.6 schwarze 249:
1.7 schwarze 250: if (MAN_BODY == n->type && 0 == n->nchild)
251: return(man_nwarn(m, n, WBODYARGS));
252: if (MAN_HEAD == n->type && 0 == n->nchild)
253: return(man_nerr(m, n, WHEADARGS));
1.6 schwarze 254: return(1);
255: }
1.7 schwarze 256:
257:
258: static int
1.8 schwarze 259: check_part(CHKARGS)
260: {
261:
262: if (MAN_BODY == n->type && 0 == n->nchild)
263: return(man_nwarn(m, n, WBODYARGS));
264: return(1);
265: }
266:
267:
268: static int
1.7 schwarze 269: check_par(CHKARGS)
270: {
271:
272: if (MAN_BODY == n->type)
273: switch (n->tok) {
274: case (MAN_IP):
275: /* FALLTHROUGH */
276: case (MAN_HP):
277: /* FALLTHROUGH */
278: case (MAN_TP):
279: /* Body-less lists are ok. */
280: break;
281: default:
282: if (n->nchild)
283: break;
284: return(man_nwarn(m, n, WBODYARGS));
285: }
286: if (MAN_HEAD == n->type)
287: switch (n->tok) {
288: case (MAN_PP):
289: /* FALLTHROUGH */
290: case (MAN_P):
291: /* FALLTHROUGH */
292: case (MAN_LP):
293: if (0 == n->nchild)
294: break;
295: return(man_nwarn(m, n, WNHEADARGS));
296: default:
297: if (n->nchild)
298: break;
299: return(man_nwarn(m, n, WHEADARGS));
300: }
301:
302: return(1);
303: }
304:
305:
306: static int
307: check_bline(CHKARGS)
308: {
309:
1.8 schwarze 310: assert( ! (MAN_ELINE & m->flags));
1.7 schwarze 311: if (MAN_BLINE & m->flags)
312: return(man_nerr(m, n, WLNSCOPE));
1.14 schwarze 313:
1.7 schwarze 314: return(1);
315: }
316:
1.14 schwarze 317:
318: static int
319: check_roff(CHKARGS)
320: {
321:
322: if (MAN_BLOCK != n->type)
323: return(1);
324:
325: for (n = n->parent; n; n = n->parent)
326: if (MAN_de == n->tok || MAN_dei == n->tok ||
327: MAN_am == n->tok ||
328: MAN_ami == n->tok ||
329: MAN_ig == n->tok)
330: return(man_nerr(m, n, WROFFNEST));
331:
332: return(1);
333: }