Annotation of mandoc/man_validate.c, Revision 1.10
1.10 ! kristaps 1: /* $Id: man_validate.c,v 1.9 2009/06/10 20:18:43 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>
21: #include <stdarg.h>
22: #include <stdlib.h>
23:
24: #include "libman.h"
25:
26: /* FIXME: validate text. */
27:
28: #define POSTARGS struct man *m, const struct man_node *n
29:
30: typedef int (*v_post)(POSTARGS);
31:
32: struct man_valid {
33: v_post *posts;
34: };
35:
1.3 kristaps 36: static int count(const struct man_node *);
1.1 kristaps 37: static int check_eq0(POSTARGS);
38: static int check_ge1(POSTARGS);
39: static int check_ge2(POSTARGS);
40: static int check_le1(POSTARGS);
41: static int check_le2(POSTARGS);
42: static int check_le5(POSTARGS);
43:
44: static v_post posts_le1[] = { check_le1, NULL };
45: static v_post posts_le2[] = { check_le2, NULL };
46: static v_post posts_ge1[] = { check_ge1, NULL };
47: static v_post posts_eq0[] = { check_eq0, NULL };
48: static v_post posts_ge2_le5[] = { check_ge2, check_le5, NULL };
49:
50: static const struct man_valid man_valids[MAN_MAX] = {
1.10 ! kristaps 51: { posts_eq0 }, /* br */
1.1 kristaps 52: { posts_ge2_le5 }, /* TH */
53: { posts_ge1 }, /* SH */
54: { posts_ge1 }, /* SS */
1.4 kristaps 55: { NULL }, /* TP */
1.1 kristaps 56: { posts_eq0 }, /* LP */
57: { posts_eq0 }, /* PP */
58: { posts_eq0 }, /* P */
59: { posts_le2 }, /* IP */
60: { posts_le1 }, /* HP */
61: { NULL }, /* SM */
62: { NULL }, /* SB */
63: { NULL }, /* BI */
64: { NULL }, /* IB */
65: { NULL }, /* BR */
66: { NULL }, /* RB */
67: { NULL }, /* R */
68: { NULL }, /* B */
69: { NULL }, /* I */
70: { NULL }, /* IR */
1.4 kristaps 71: { NULL }, /* RI */
1.6 kristaps 72: { posts_eq0 }, /* na */
1.7 kristaps 73: { NULL }, /* i */
1.1 kristaps 74: };
75:
76:
77: int
78: man_valid_post(struct man *m)
79: {
80: v_post *cp;
81:
82: if (MAN_VALID & m->last->flags)
83: return(1);
84: m->last->flags |= MAN_VALID;
85:
86: switch (m->last->type) {
87: case (MAN_TEXT):
88: /* FALLTHROUGH */
89: case (MAN_ROOT):
90: return(1);
91: default:
92: break;
93: }
94:
95: if (NULL == (cp = man_valids[m->last->tok].posts))
96: return(1);
97: for ( ; *cp; cp++)
98: if ( ! (*cp)(m, m->last))
99: return(0);
100:
101: return(1);
102: }
103:
104:
105: static inline int
1.3 kristaps 106: count(const struct man_node *n)
1.1 kristaps 107: {
108: int i;
109:
110: for (i = 0; n; n = n->next, i++)
111: /* Loop. */ ;
112: return(i);
113: }
114:
115:
116: #define INEQ_DEFINE(x, ineq, name) \
117: static int \
118: check_##name(POSTARGS) \
119: { \
120: int c; \
1.3 kristaps 121: if ((c = count(n->child)) ineq (x)) \
1.1 kristaps 122: return(1); \
1.4 kristaps 123: return(man_verr(m, n->line, n->pos, \
1.1 kristaps 124: "expected line arguments %s %d, have %d", \
125: #ineq, (x), c)); \
126: }
127:
128: INEQ_DEFINE(0, ==, eq0)
129: INEQ_DEFINE(1, >=, ge1)
130: INEQ_DEFINE(2, >=, ge2)
131: INEQ_DEFINE(1, <=, le1)
132: INEQ_DEFINE(2, <=, le2)
133: INEQ_DEFINE(5, <=, le5)
134:
CVSweb