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