Annotation of mandoc/man_validate.c, Revision 1.11
1.11 ! kristaps 1: /* $Id: man_validate.c,v 1.10 2009/06/16 19:55:28 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:
1.11 ! kristaps 26: #define POSTARGS struct man *m, const struct man_node *n
1.1 kristaps 27:
1.11 ! kristaps 28: enum merr {
! 29: WPRINT
! 30: };
1.1 kristaps 31:
32: typedef int (*v_post)(POSTARGS);
33:
34: struct man_valid {
35: v_post *posts;
36: };
37:
38: static int check_eq0(POSTARGS);
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.11 ! kristaps 44: static int check_text(POSTARGS);
! 45: static int perr(struct man *, int, int, int, enum merr);
1.1 kristaps 46:
47: static v_post posts_le1[] = { check_le1, NULL };
48: static v_post posts_le2[] = { check_le2, NULL };
49: static v_post posts_ge1[] = { check_ge1, NULL };
50: static v_post posts_eq0[] = { check_eq0, NULL };
51: static v_post posts_ge2_le5[] = { check_ge2, check_le5, NULL };
52:
53: static const struct man_valid man_valids[MAN_MAX] = {
1.10 kristaps 54: { posts_eq0 }, /* br */
1.1 kristaps 55: { posts_ge2_le5 }, /* TH */
56: { posts_ge1 }, /* SH */
57: { posts_ge1 }, /* SS */
1.4 kristaps 58: { NULL }, /* TP */
1.1 kristaps 59: { posts_eq0 }, /* LP */
60: { posts_eq0 }, /* PP */
61: { posts_eq0 }, /* P */
62: { posts_le2 }, /* IP */
63: { posts_le1 }, /* HP */
64: { NULL }, /* SM */
65: { NULL }, /* SB */
66: { NULL }, /* BI */
67: { NULL }, /* IB */
68: { NULL }, /* BR */
69: { NULL }, /* RB */
70: { NULL }, /* R */
71: { NULL }, /* B */
72: { NULL }, /* I */
73: { NULL }, /* IR */
1.4 kristaps 74: { NULL }, /* RI */
1.6 kristaps 75: { posts_eq0 }, /* na */
1.7 kristaps 76: { NULL }, /* i */
1.1 kristaps 77: };
78:
79:
80: int
81: man_valid_post(struct man *m)
82: {
83: v_post *cp;
84:
85: if (MAN_VALID & m->last->flags)
86: return(1);
87: m->last->flags |= MAN_VALID;
88:
89: switch (m->last->type) {
90: case (MAN_TEXT):
1.11 ! kristaps 91: return(check_text(m, m->last));
1.1 kristaps 92: case (MAN_ROOT):
93: return(1);
94: default:
95: break;
96: }
97:
98: if (NULL == (cp = man_valids[m->last->tok].posts))
99: return(1);
100: for ( ; *cp; cp++)
101: if ( ! (*cp)(m, m->last))
102: return(0);
103:
104: return(1);
105: }
106:
107:
1.11 ! kristaps 108: static int
! 109: perr(struct man *m, int line, int pos,
! 110: int iserr, enum merr type)
! 111: {
! 112: const char *p;
! 113:
! 114: p = NULL;
! 115: switch (type) {
! 116: case (WPRINT):
! 117: p = "invalid character";
! 118: break;
! 119: }
! 120: assert(p);
! 121:
! 122: if (iserr)
! 123: return(man_verr(m, line, pos, p));
! 124:
! 125: return(man_vwarn(m, line, pos, p));
! 126: }
! 127:
! 128:
! 129: static int
! 130: check_text(POSTARGS)
! 131: {
! 132: const char *p;
! 133: int pos;
! 134:
! 135: assert(n->string);
! 136:
! 137: for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
! 138: if ('\t' == *p || isprint((u_char)*p))
! 139: continue;
! 140:
! 141: if (MAN_IGN_CHARS & m->pflags)
! 142: return(perr(m, n->line, pos, 0, WPRINT));
! 143: return(perr(m, n->line, pos, 1, WPRINT));
! 144: }
! 145:
! 146: return(1);
1.1 kristaps 147: }
148:
149:
150: #define INEQ_DEFINE(x, ineq, name) \
151: static int \
152: check_##name(POSTARGS) \
153: { \
1.11 ! kristaps 154: if (n->nchild ineq (x)) \
1.1 kristaps 155: return(1); \
1.4 kristaps 156: return(man_verr(m, n->line, n->pos, \
1.1 kristaps 157: "expected line arguments %s %d, have %d", \
1.11 ! kristaps 158: #ineq, (x), n->nchild)); \
1.1 kristaps 159: }
160:
161: INEQ_DEFINE(0, ==, eq0)
162: INEQ_DEFINE(1, >=, ge1)
163: INEQ_DEFINE(2, >=, ge2)
164: INEQ_DEFINE(1, <=, le1)
165: INEQ_DEFINE(2, <=, le2)
166: INEQ_DEFINE(5, <=, le5)
167:
CVSweb