Annotation of mandoc/man_validate.c, Revision 1.31
1.31 ! kristaps 1: /* $Id: man_validate.c,v 1.30 2010/03/23 11:30:48 kristaps Exp $ */
1.1 kristaps 2: /*
1.9 kristaps 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.8 kristaps 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.8 kristaps 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: */
1.28 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.1 kristaps 21: #include <sys/types.h>
22:
23: #include <assert.h>
24: #include <ctype.h>
1.16 kristaps 25: #include <errno.h>
26: #include <limits.h>
1.1 kristaps 27: #include <stdarg.h>
28: #include <stdlib.h>
29:
30: #include "libman.h"
1.15 kristaps 31: #include "libmandoc.h"
1.1 kristaps 32:
1.17 kristaps 33: #define CHKARGS struct man *m, const struct man_node *n
1.1 kristaps 34:
1.17 kristaps 35: typedef int (*v_check)(CHKARGS);
1.1 kristaps 36:
37: struct man_valid {
1.17 kristaps 38: v_check *pres;
39: v_check *posts;
1.1 kristaps 40: };
41:
1.17 kristaps 42: static int check_bline(CHKARGS);
43: static int check_eq0(CHKARGS);
1.25 kristaps 44: static int check_le1(CHKARGS);
1.17 kristaps 45: static int check_ge2(CHKARGS);
46: static int check_le5(CHKARGS);
47: static int check_par(CHKARGS);
1.23 kristaps 48: static int check_part(CHKARGS);
1.31 ! kristaps 49: static int check_roff(CHKARGS);
1.17 kristaps 50: static int check_root(CHKARGS);
51: static int check_sec(CHKARGS);
52: static int check_text(CHKARGS);
53:
54: static v_check posts_eq0[] = { check_eq0, NULL };
55: static v_check posts_ge2_le5[] = { check_ge2, check_le5, NULL };
56: static v_check posts_par[] = { check_par, NULL };
1.23 kristaps 57: static v_check posts_part[] = { check_part, NULL };
1.17 kristaps 58: static v_check posts_sec[] = { check_sec, NULL };
1.30 kristaps 59: static v_check posts_le1[] = { check_le1, NULL };
1.17 kristaps 60: static v_check pres_bline[] = { check_bline, NULL };
1.31 ! kristaps 61: static v_check pres_roff[] = { check_bline, check_roff, NULL };
1.1 kristaps 62:
63: static const struct man_valid man_valids[MAN_MAX] = {
1.29 kristaps 64: { NULL, posts_eq0 }, /* br */
1.27 kristaps 65: { pres_bline, posts_ge2_le5 }, /* TH */ /* FIXME: make sure capitalised. */
1.17 kristaps 66: { pres_bline, posts_sec }, /* SH */
67: { pres_bline, posts_sec }, /* SS */
68: { pres_bline, posts_par }, /* TP */
69: { pres_bline, posts_par }, /* LP */
70: { pres_bline, posts_par }, /* PP */
71: { pres_bline, posts_par }, /* P */
72: { pres_bline, posts_par }, /* IP */
73: { pres_bline, posts_par }, /* HP */
1.22 kristaps 74: { NULL, NULL }, /* SM */
75: { NULL, NULL }, /* SB */
1.17 kristaps 76: { NULL, NULL }, /* BI */
77: { NULL, NULL }, /* IB */
78: { NULL, NULL }, /* BR */
79: { NULL, NULL }, /* RB */
1.22 kristaps 80: { NULL, NULL }, /* R */
81: { NULL, NULL }, /* B */
82: { NULL, NULL }, /* I */
1.17 kristaps 83: { NULL, NULL }, /* IR */
84: { NULL, NULL }, /* RI */
1.29 kristaps 85: { NULL, posts_eq0 }, /* na */
1.17 kristaps 86: { NULL, NULL }, /* i */
1.30 kristaps 87: { NULL, posts_le1 }, /* sp */
1.17 kristaps 88: { pres_bline, posts_eq0 }, /* nf */
89: { pres_bline, posts_eq0 }, /* fi */
90: { NULL, NULL }, /* r */
1.19 kristaps 91: { NULL, NULL }, /* RE */
1.23 kristaps 92: { NULL, posts_part }, /* RS */
1.21 kristaps 93: { NULL, NULL }, /* DT */
1.24 kristaps 94: { NULL, NULL }, /* UC */
1.26 kristaps 95: { NULL, NULL }, /* PD */
1.30 kristaps 96: { NULL, posts_eq0 }, /* Sp */
97: { pres_bline, posts_le1 }, /* Vb */
98: { pres_bline, posts_eq0 }, /* Ve */
1.31 ! kristaps 99: { pres_roff, NULL }, /* de */
! 100: { pres_roff, NULL }, /* dei */
! 101: { pres_roff, NULL }, /* am */
! 102: { pres_roff, NULL }, /* ami */
! 103: { pres_roff, NULL }, /* ig */
! 104: { NULL, NULL }, /* . */
1.1 kristaps 105: };
106:
107:
108: int
1.17 kristaps 109: man_valid_pre(struct man *m, const struct man_node *n)
110: {
111: v_check *cp;
112:
113: if (MAN_TEXT == n->type)
114: return(1);
115: if (MAN_ROOT == n->type)
116: return(1);
117:
118: if (NULL == (cp = man_valids[n->tok].pres))
119: return(1);
120: for ( ; *cp; cp++)
121: if ( ! (*cp)(m, n))
122: return(0);
123: return(1);
124: }
125:
126:
127: int
1.1 kristaps 128: man_valid_post(struct man *m)
129: {
1.17 kristaps 130: v_check *cp;
1.1 kristaps 131:
132: if (MAN_VALID & m->last->flags)
133: return(1);
134: m->last->flags |= MAN_VALID;
135:
136: switch (m->last->type) {
137: case (MAN_TEXT):
1.11 kristaps 138: return(check_text(m, m->last));
1.1 kristaps 139: case (MAN_ROOT):
1.14 kristaps 140: return(check_root(m, m->last));
1.1 kristaps 141: default:
142: break;
143: }
144:
145: if (NULL == (cp = man_valids[m->last->tok].posts))
146: return(1);
147: for ( ; *cp; cp++)
148: if ( ! (*cp)(m, m->last))
149: return(0);
150:
151: return(1);
152: }
153:
154:
1.11 kristaps 155: static int
1.17 kristaps 156: check_root(CHKARGS)
1.14 kristaps 157: {
1.17 kristaps 158:
159: if (MAN_BLINE & m->flags)
1.22 kristaps 160: return(man_nwarn(m, n, WEXITSCOPE));
1.17 kristaps 161: if (MAN_ELINE & m->flags)
1.22 kristaps 162: return(man_nwarn(m, n, WEXITSCOPE));
163:
164: m->flags &= ~MAN_BLINE;
165: m->flags &= ~MAN_ELINE;
1.17 kristaps 166:
1.14 kristaps 167: if (NULL == m->first->child)
168: return(man_nerr(m, n, WNODATA));
169: if (NULL == m->meta.title)
170: return(man_nerr(m, n, WNOTITLE));
171:
172: return(1);
173: }
174:
175:
176: static int
1.17 kristaps 177: check_text(CHKARGS)
1.11 kristaps 178: {
179: const char *p;
1.15 kristaps 180: int pos, c;
1.11 kristaps 181:
182: assert(n->string);
183:
184: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
1.15 kristaps 185: if ('\\' == *p) {
186: c = mandoc_special(p);
187: if (c) {
188: p += c - 1;
189: pos += c - 1;
190: continue;
191: }
192: if ( ! (MAN_IGN_ESCAPE & m->pflags))
193: return(man_perr(m, n->line, pos, WESCAPE));
194: if ( ! man_pwarn(m, n->line, pos, WESCAPE))
195: return(0);
196: continue;
197: }
198:
199: if ('\t' == *p || isprint((u_char)*p))
1.11 kristaps 200: continue;
201:
202: if (MAN_IGN_CHARS & m->pflags)
1.12 kristaps 203: return(man_pwarn(m, n->line, pos, WNPRINT));
204: return(man_perr(m, n->line, pos, WNPRINT));
1.11 kristaps 205: }
206:
207: return(1);
1.1 kristaps 208: }
209:
210:
211: #define INEQ_DEFINE(x, ineq, name) \
212: static int \
1.17 kristaps 213: check_##name(CHKARGS) \
1.1 kristaps 214: { \
1.11 kristaps 215: if (n->nchild ineq (x)) \
1.1 kristaps 216: return(1); \
1.4 kristaps 217: return(man_verr(m, n->line, n->pos, \
1.1 kristaps 218: "expected line arguments %s %d, have %d", \
1.11 kristaps 219: #ineq, (x), n->nchild)); \
1.1 kristaps 220: }
221:
222: INEQ_DEFINE(0, ==, eq0)
1.25 kristaps 223: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 224: INEQ_DEFINE(2, >=, ge2)
225: INEQ_DEFINE(5, <=, le5)
226:
1.16 kristaps 227:
228: static int
1.17 kristaps 229: check_sec(CHKARGS)
230: {
1.16 kristaps 231:
1.17 kristaps 232: if (MAN_BODY == n->type && 0 == n->nchild)
233: return(man_nwarn(m, n, WBODYARGS));
234: if (MAN_HEAD == n->type && 0 == n->nchild)
235: return(man_nerr(m, n, WHEADARGS));
1.16 kristaps 236: return(1);
237: }
1.17 kristaps 238:
239:
240: static int
1.23 kristaps 241: check_part(CHKARGS)
242: {
243:
244: if (MAN_BODY == n->type && 0 == n->nchild)
245: return(man_nwarn(m, n, WBODYARGS));
246: return(1);
247: }
248:
249:
250: static int
1.17 kristaps 251: check_par(CHKARGS)
252: {
253:
254: if (MAN_BODY == n->type)
255: switch (n->tok) {
256: case (MAN_IP):
257: /* FALLTHROUGH */
258: case (MAN_HP):
259: /* FALLTHROUGH */
260: case (MAN_TP):
261: /* Body-less lists are ok. */
262: break;
263: default:
264: if (n->nchild)
265: break;
266: return(man_nwarn(m, n, WBODYARGS));
267: }
268: if (MAN_HEAD == n->type)
269: switch (n->tok) {
270: case (MAN_PP):
271: /* FALLTHROUGH */
272: case (MAN_P):
273: /* FALLTHROUGH */
274: case (MAN_LP):
275: if (0 == n->nchild)
276: break;
277: return(man_nwarn(m, n, WNHEADARGS));
278: default:
279: if (n->nchild)
280: break;
281: return(man_nwarn(m, n, WHEADARGS));
282: }
283:
284: return(1);
285: }
286:
287:
288: static int
289: check_bline(CHKARGS)
290: {
291:
1.22 kristaps 292: assert( ! (MAN_ELINE & m->flags));
1.18 kristaps 293: if (MAN_BLINE & m->flags)
294: return(man_nerr(m, n, WLNSCOPE));
1.31 ! kristaps 295:
1.18 kristaps 296: return(1);
1.17 kristaps 297: }
298:
1.31 ! kristaps 299:
! 300: static int
! 301: check_roff(CHKARGS)
! 302: {
! 303:
! 304: if (MAN_BLOCK != n->type)
! 305: return(1);
! 306:
! 307: for (n = n->parent; n; n = n->parent)
! 308: if (MAN_de == n->tok || MAN_dei == n->tok ||
! 309: MAN_am == n->tok ||
! 310: MAN_ami == n->tok ||
! 311: MAN_ig == n->tok)
! 312: return(man_nerr(m, n, WROFFNEST));
! 313:
! 314: return(1);
! 315: }
CVSweb