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