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