Annotation of mandoc/man_validate.c, Revision 1.16
1.16 ! kristaps 1: /* $Id: man_validate.c,v 1.15 2009/07/04 09:01:55 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.11 kristaps 29: #define POSTARGS struct man *m, const struct man_node *n
1.1 kristaps 30:
31: typedef int (*v_post)(POSTARGS);
32:
33: struct man_valid {
34: v_post *posts;
35: };
36:
37: static int check_eq0(POSTARGS);
1.16 ! kristaps 38: static int check_eq1(POSTARGS);
1.1 kristaps 39: static int check_ge1(POSTARGS);
40: static int check_ge2(POSTARGS);
41: static int check_le1(POSTARGS);
42: static int check_le2(POSTARGS);
43: static int check_le5(POSTARGS);
1.16 ! kristaps 44: static int check_root(POSTARGS);
! 45: static int check_sp(POSTARGS);
1.11 kristaps 46: static int check_text(POSTARGS);
1.1 kristaps 47:
1.16 ! kristaps 48: static v_post posts_eq0[] = { check_eq0, NULL };
! 49: static v_post posts_ge1[] = { check_ge1, NULL };
! 50: static v_post posts_ge2_le5[] = { check_ge2, check_le5, NULL };
1.1 kristaps 51: static v_post posts_le1[] = { check_le1, NULL };
52: static v_post posts_le2[] = { check_le2, NULL };
1.16 ! kristaps 53: static v_post posts_sp[] = { check_sp, NULL };
1.1 kristaps 54:
55: static const struct man_valid man_valids[MAN_MAX] = {
1.10 kristaps 56: { posts_eq0 }, /* br */
1.1 kristaps 57: { posts_ge2_le5 }, /* TH */
58: { posts_ge1 }, /* SH */
59: { posts_ge1 }, /* SS */
1.4 kristaps 60: { NULL }, /* TP */
1.1 kristaps 61: { posts_eq0 }, /* LP */
62: { posts_eq0 }, /* PP */
63: { posts_eq0 }, /* P */
64: { posts_le2 }, /* IP */
65: { posts_le1 }, /* HP */
66: { NULL }, /* SM */
67: { NULL }, /* SB */
68: { NULL }, /* BI */
69: { NULL }, /* IB */
70: { NULL }, /* BR */
71: { NULL }, /* RB */
72: { NULL }, /* R */
73: { NULL }, /* B */
74: { NULL }, /* I */
75: { NULL }, /* IR */
1.4 kristaps 76: { NULL }, /* RI */
1.6 kristaps 77: { posts_eq0 }, /* na */
1.7 kristaps 78: { NULL }, /* i */
1.16 ! kristaps 79: { posts_sp }, /* sp */
1.1 kristaps 80: };
81:
82:
83: int
84: man_valid_post(struct man *m)
85: {
86: v_post *cp;
87:
88: if (MAN_VALID & m->last->flags)
89: return(1);
90: m->last->flags |= MAN_VALID;
91:
92: switch (m->last->type) {
93: case (MAN_TEXT):
1.11 kristaps 94: return(check_text(m, m->last));
1.1 kristaps 95: case (MAN_ROOT):
1.14 kristaps 96: return(check_root(m, m->last));
1.1 kristaps 97: default:
98: break;
99: }
100:
101: if (NULL == (cp = man_valids[m->last->tok].posts))
102: return(1);
103: for ( ; *cp; cp++)
104: if ( ! (*cp)(m, m->last))
105: return(0);
106:
107: return(1);
108: }
109:
110:
1.11 kristaps 111: static int
1.14 kristaps 112: check_root(POSTARGS)
113: {
114:
115: if (NULL == m->first->child)
116: return(man_nerr(m, n, WNODATA));
117: if (NULL == m->meta.title)
118: return(man_nerr(m, n, WNOTITLE));
119:
120: return(1);
121: }
122:
123:
124: static int
1.11 kristaps 125: check_text(POSTARGS)
126: {
127: const char *p;
1.15 kristaps 128: int pos, c;
1.11 kristaps 129:
130: assert(n->string);
131:
132: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
1.15 kristaps 133: if ('\\' == *p) {
134: c = mandoc_special(p);
135: if (c) {
136: p += c - 1;
137: pos += c - 1;
138: continue;
139: }
140: if ( ! (MAN_IGN_ESCAPE & m->pflags))
141: return(man_perr(m, n->line, pos, WESCAPE));
142: if ( ! man_pwarn(m, n->line, pos, WESCAPE))
143: return(0);
144: continue;
145: }
146:
147: if ('\t' == *p || isprint((u_char)*p))
1.11 kristaps 148: continue;
149:
150: if (MAN_IGN_CHARS & m->pflags)
1.12 kristaps 151: return(man_pwarn(m, n->line, pos, WNPRINT));
152: return(man_perr(m, n->line, pos, WNPRINT));
1.11 kristaps 153: }
154:
155: return(1);
1.1 kristaps 156: }
157:
158:
159: #define INEQ_DEFINE(x, ineq, name) \
160: static int \
161: check_##name(POSTARGS) \
162: { \
1.11 kristaps 163: if (n->nchild ineq (x)) \
1.1 kristaps 164: return(1); \
1.4 kristaps 165: return(man_verr(m, n->line, n->pos, \
1.1 kristaps 166: "expected line arguments %s %d, have %d", \
1.11 kristaps 167: #ineq, (x), n->nchild)); \
1.1 kristaps 168: }
169:
170: INEQ_DEFINE(0, ==, eq0)
1.16 ! kristaps 171: INEQ_DEFINE(1, ==, eq1)
1.1 kristaps 172: INEQ_DEFINE(1, >=, ge1)
173: INEQ_DEFINE(2, >=, ge2)
174: INEQ_DEFINE(1, <=, le1)
175: INEQ_DEFINE(2, <=, le2)
176: INEQ_DEFINE(5, <=, le5)
177:
1.16 ! kristaps 178:
! 179: static int
! 180: check_sp(POSTARGS)
! 181: {
! 182: long lval;
! 183: char *ep, *buf;
! 184:
! 185: if (NULL == m->last->child)
! 186: return(1);
! 187: else if ( ! check_eq1(m, n))
! 188: return(0);
! 189:
! 190: assert(MAN_TEXT == m->last->child->type);
! 191: buf = m->last->child->string;
! 192: assert(buf);
! 193:
! 194: /* From OpenBSD's strtol(3). */
! 195: errno = 0;
! 196: lval = strtol(buf, &ep, 10);
! 197: if (buf[0] == '\0' || *ep != '\0')
! 198: return(man_nerr(m, m->last->child, WNUMFMT));
! 199:
! 200: if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
! 201: (lval > INT_MAX || lval < 0))
! 202: return(man_nerr(m, m->last->child, WNUMFMT));
! 203:
! 204: return(1);
! 205: }
CVSweb