Annotation of mandoc/man_validate.c, Revision 1.50
1.50 ! kristaps 1: /* $Id: man_validate.c,v 1.49 2010/08/20 01:02:07 schwarze Exp $ */
1.1 kristaps 2: /*
1.48 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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>
1.46 kristaps 29: #include <string.h>
1.50 ! kristaps 30: #include <time.h>
1.1 kristaps 31:
1.41 kristaps 32: #include "mandoc.h"
1.1 kristaps 33: #include "libman.h"
1.15 kristaps 34: #include "libmandoc.h"
1.1 kristaps 35:
1.43 kristaps 36: #define CHKARGS struct man *m, struct man_node *n
1.1 kristaps 37:
1.17 kristaps 38: typedef int (*v_check)(CHKARGS);
1.1 kristaps 39:
40: struct man_valid {
1.17 kristaps 41: v_check *pres;
42: v_check *posts;
1.1 kristaps 43: };
44:
1.17 kristaps 45: static int check_bline(CHKARGS);
46: static int check_eq0(CHKARGS);
1.25 kristaps 47: static int check_le1(CHKARGS);
1.17 kristaps 48: static int check_ge2(CHKARGS);
49: static int check_le5(CHKARGS);
50: static int check_par(CHKARGS);
1.23 kristaps 51: static int check_part(CHKARGS);
1.17 kristaps 52: static int check_root(CHKARGS);
53: static int check_sec(CHKARGS);
54: static int check_text(CHKARGS);
1.32 kristaps 55: static int check_title(CHKARGS);
1.17 kristaps 56:
57: static v_check posts_eq0[] = { check_eq0, NULL };
1.32 kristaps 58: static v_check posts_th[] = { check_ge2, check_le5, check_title, NULL };
1.17 kristaps 59: static v_check posts_par[] = { check_par, NULL };
1.23 kristaps 60: static v_check posts_part[] = { check_part, NULL };
1.17 kristaps 61: static v_check posts_sec[] = { check_sec, NULL };
1.30 kristaps 62: static v_check posts_le1[] = { check_le1, NULL };
1.17 kristaps 63: static v_check pres_bline[] = { check_bline, NULL };
1.1 kristaps 64:
65: static const struct man_valid man_valids[MAN_MAX] = {
1.29 kristaps 66: { NULL, posts_eq0 }, /* br */
1.32 kristaps 67: { pres_bline, posts_th }, /* TH */
1.17 kristaps 68: { pres_bline, posts_sec }, /* SH */
69: { pres_bline, posts_sec }, /* SS */
70: { pres_bline, posts_par }, /* TP */
71: { pres_bline, posts_par }, /* LP */
72: { pres_bline, posts_par }, /* PP */
73: { pres_bline, posts_par }, /* P */
74: { pres_bline, posts_par }, /* IP */
75: { pres_bline, posts_par }, /* HP */
1.22 kristaps 76: { NULL, NULL }, /* SM */
77: { NULL, NULL }, /* SB */
1.17 kristaps 78: { NULL, NULL }, /* BI */
79: { NULL, NULL }, /* IB */
80: { NULL, NULL }, /* BR */
81: { NULL, NULL }, /* RB */
1.22 kristaps 82: { NULL, NULL }, /* R */
83: { NULL, NULL }, /* B */
84: { NULL, NULL }, /* I */
1.17 kristaps 85: { NULL, NULL }, /* IR */
86: { NULL, NULL }, /* RI */
1.47 kristaps 87: { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
1.17 kristaps 88: { NULL, NULL }, /* i */
1.47 kristaps 89: { NULL, posts_le1 }, /* sp */ /* FIXME: should warn only. */
1.17 kristaps 90: { pres_bline, posts_eq0 }, /* nf */
91: { pres_bline, posts_eq0 }, /* fi */
92: { NULL, NULL }, /* r */
1.19 kristaps 93: { NULL, NULL }, /* RE */
1.23 kristaps 94: { NULL, posts_part }, /* RS */
1.21 kristaps 95: { NULL, NULL }, /* DT */
1.24 kristaps 96: { NULL, NULL }, /* UC */
1.26 kristaps 97: { NULL, NULL }, /* PD */
1.47 kristaps 98: { NULL, posts_le1 }, /* Sp */ /* FIXME: should warn only. */
99: { pres_bline, posts_le1 }, /* Vb */ /* FIXME: should warn only. */
1.30 kristaps 100: { pres_bline, posts_eq0 }, /* Ve */
1.40 joerg 101: { NULL, NULL }, /* AT */
1.47 kristaps 102: { NULL, NULL }, /* in */
1.1 kristaps 103: };
104:
105:
106: int
1.43 kristaps 107: man_valid_pre(struct man *m, struct man_node *n)
1.17 kristaps 108: {
109: v_check *cp;
110:
111: if (MAN_TEXT == n->type)
112: return(1);
113: if (MAN_ROOT == n->type)
114: return(1);
115:
116: if (NULL == (cp = man_valids[n->tok].pres))
117: return(1);
118: for ( ; *cp; cp++)
119: if ( ! (*cp)(m, n))
120: return(0);
121: return(1);
122: }
123:
124:
125: int
1.1 kristaps 126: man_valid_post(struct man *m)
127: {
1.17 kristaps 128: v_check *cp;
1.1 kristaps 129:
130: if (MAN_VALID & m->last->flags)
131: return(1);
132: m->last->flags |= MAN_VALID;
133:
134: switch (m->last->type) {
135: case (MAN_TEXT):
1.11 kristaps 136: return(check_text(m, m->last));
1.1 kristaps 137: case (MAN_ROOT):
1.14 kristaps 138: return(check_root(m, m->last));
1.1 kristaps 139: default:
140: break;
141: }
142:
143: if (NULL == (cp = man_valids[m->last->tok].posts))
144: return(1);
145: for ( ; *cp; cp++)
146: if ( ! (*cp)(m, m->last))
147: return(0);
148:
149: return(1);
150: }
151:
152:
1.11 kristaps 153: static int
1.17 kristaps 154: check_root(CHKARGS)
1.14 kristaps 155: {
1.17 kristaps 156:
157: if (MAN_BLINE & m->flags)
1.41 kristaps 158: return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
1.17 kristaps 159: if (MAN_ELINE & m->flags)
1.41 kristaps 160: return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
1.22 kristaps 161:
162: m->flags &= ~MAN_BLINE;
163: m->flags &= ~MAN_ELINE;
1.17 kristaps 164:
1.41 kristaps 165: if (NULL == m->first->child) {
166: man_nmsg(m, n, MANDOCERR_NODOCBODY);
167: return(0);
168: } else if (NULL == m->meta.title) {
169: if ( ! man_nmsg(m, n, MANDOCERR_NOTITLE))
1.34 kristaps 170: return(0);
171: /*
172: * If a title hasn't been set, do so now (by
173: * implication, date and section also aren't set).
174: *
175: * FIXME: this should be in man_action.c.
176: */
177: m->meta.title = mandoc_strdup("unknown");
178: m->meta.date = time(NULL);
1.37 kristaps 179: m->meta.msec = mandoc_strdup("1");
1.34 kristaps 180: }
1.32 kristaps 181:
182: return(1);
183: }
184:
185:
186: static int
187: check_title(CHKARGS)
188: {
189: const char *p;
190:
191: assert(n->child);
1.41 kristaps 192: /* FIXME: is this sufficient? */
193: if ('\0' == *n->child->string) {
194: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
195: return(0);
196: }
1.32 kristaps 197:
198: for (p = n->child->string; '\0' != *p; p++)
199: if (isalpha((u_char)*p) && ! isupper((u_char)*p))
1.41 kristaps 200: if ( ! man_nmsg(m, n, MANDOCERR_UPPERCASE))
1.32 kristaps 201: return(0);
1.14 kristaps 202:
203: return(1);
204: }
205:
206:
207: static int
1.17 kristaps 208: check_text(CHKARGS)
1.11 kristaps 209: {
1.43 kristaps 210: char *p;
1.15 kristaps 211: int pos, c;
1.46 kristaps 212: size_t sz;
1.11 kristaps 213:
1.46 kristaps 214: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
215: sz = strcspn(p, "\t\\");
216: p += (int)sz;
217:
218: if ('\0' == *p)
219: break;
220:
221: pos += (int)sz;
1.11 kristaps 222:
1.46 kristaps 223: if ('\t' == *p) {
224: if (MAN_LITERAL & m->flags)
225: continue;
226: if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
1.15 kristaps 227: continue;
1.46 kristaps 228: return(0);
1.15 kristaps 229: }
1.45 kristaps 230:
1.46 kristaps 231: /* Check the special character. */
1.15 kristaps 232:
1.46 kristaps 233: c = mandoc_special(p);
234: if (c) {
235: p += c - 1;
236: pos += c - 1;
1.49 schwarze 237: } else
238: man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
1.11 kristaps 239: }
240:
241: return(1);
1.1 kristaps 242: }
243:
244:
245: #define INEQ_DEFINE(x, ineq, name) \
246: static int \
1.17 kristaps 247: check_##name(CHKARGS) \
1.1 kristaps 248: { \
1.11 kristaps 249: if (n->nchild ineq (x)) \
1.1 kristaps 250: return(1); \
1.41 kristaps 251: man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
252: "line arguments %s %d (have %d)", \
253: #ineq, (x), n->nchild); \
254: return(0); \
1.1 kristaps 255: }
256:
257: INEQ_DEFINE(0, ==, eq0)
1.25 kristaps 258: INEQ_DEFINE(1, <=, le1)
1.1 kristaps 259: INEQ_DEFINE(2, >=, ge2)
260: INEQ_DEFINE(5, <=, le5)
261:
1.16 kristaps 262:
263: static int
1.17 kristaps 264: check_sec(CHKARGS)
265: {
1.16 kristaps 266:
1.41 kristaps 267: if (MAN_HEAD == n->type && 0 == n->nchild) {
268: man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
269: return(0);
270: } else if (MAN_BODY == n->type && 0 == n->nchild)
271: return(man_nmsg(m, n, MANDOCERR_NOBODY));
272:
1.16 kristaps 273: return(1);
274: }
1.17 kristaps 275:
276:
277: static int
1.23 kristaps 278: check_part(CHKARGS)
279: {
280:
281: if (MAN_BODY == n->type && 0 == n->nchild)
1.41 kristaps 282: return(man_nmsg(m, n, MANDOCERR_NOBODY));
1.23 kristaps 283: return(1);
284: }
285:
286:
287: static int
1.17 kristaps 288: check_par(CHKARGS)
289: {
290:
291: if (MAN_BODY == n->type)
292: switch (n->tok) {
293: case (MAN_IP):
294: /* FALLTHROUGH */
295: case (MAN_HP):
296: /* FALLTHROUGH */
297: case (MAN_TP):
298: /* Body-less lists are ok. */
299: break;
300: default:
301: if (n->nchild)
302: break;
1.41 kristaps 303: return(man_nmsg(m, n, MANDOCERR_NOBODY));
1.17 kristaps 304: }
305: if (MAN_HEAD == n->type)
306: switch (n->tok) {
307: case (MAN_PP):
308: /* FALLTHROUGH */
309: case (MAN_P):
310: /* FALLTHROUGH */
311: case (MAN_LP):
312: if (0 == n->nchild)
313: break;
1.41 kristaps 314: return(man_nmsg(m, n, MANDOCERR_ARGSLOST));
1.17 kristaps 315: default:
316: if (n->nchild)
317: break;
1.41 kristaps 318: return(man_nmsg(m, n, MANDOCERR_NOARGS));
1.17 kristaps 319: }
320:
321: return(1);
322: }
323:
324:
325: static int
326: check_bline(CHKARGS)
327: {
328:
1.22 kristaps 329: assert( ! (MAN_ELINE & m->flags));
1.41 kristaps 330: if (MAN_BLINE & m->flags) {
331: man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
332: return(0);
333: }
1.31 kristaps 334:
1.18 kristaps 335: return(1);
1.17 kristaps 336: }
337:
CVSweb