]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
libman documents require `TH' and at least one node.
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.14 2009/06/22 13:09:17 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
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.
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 #define POSTARGS struct man *m, const struct man_node *n
27
28 typedef int (*v_post)(POSTARGS);
29
30 struct man_valid {
31 v_post *posts;
32 };
33
34 static int check_eq0(POSTARGS);
35 static int check_ge1(POSTARGS);
36 static int check_ge2(POSTARGS);
37 static int check_le1(POSTARGS);
38 static int check_le2(POSTARGS);
39 static int check_le5(POSTARGS);
40 static int check_text(POSTARGS);
41 static int check_root(POSTARGS);
42
43 static v_post posts_le1[] = { check_le1, NULL };
44 static v_post posts_le2[] = { check_le2, NULL };
45 static v_post posts_ge1[] = { check_ge1, NULL };
46 static v_post posts_eq0[] = { check_eq0, NULL };
47 static v_post posts_ge2_le5[] = { check_ge2, check_le5, NULL };
48
49 static const struct man_valid man_valids[MAN_MAX] = {
50 { posts_eq0 }, /* br */
51 { posts_ge2_le5 }, /* TH */
52 { posts_ge1 }, /* SH */
53 { posts_ge1 }, /* SS */
54 { NULL }, /* TP */
55 { posts_eq0 }, /* LP */
56 { posts_eq0 }, /* PP */
57 { posts_eq0 }, /* P */
58 { posts_le2 }, /* IP */
59 { posts_le1 }, /* HP */
60 { NULL }, /* SM */
61 { NULL }, /* SB */
62 { NULL }, /* BI */
63 { NULL }, /* IB */
64 { NULL }, /* BR */
65 { NULL }, /* RB */
66 { NULL }, /* R */
67 { NULL }, /* B */
68 { NULL }, /* I */
69 { NULL }, /* IR */
70 { NULL }, /* RI */
71 { posts_eq0 }, /* na */
72 { NULL }, /* i */
73 };
74
75
76 int
77 man_valid_post(struct man *m)
78 {
79 v_post *cp;
80
81 if (MAN_VALID & m->last->flags)
82 return(1);
83 m->last->flags |= MAN_VALID;
84
85 switch (m->last->type) {
86 case (MAN_TEXT):
87 return(check_text(m, m->last));
88 case (MAN_ROOT):
89 return(check_root(m, m->last));
90 default:
91 break;
92 }
93
94 if (NULL == (cp = man_valids[m->last->tok].posts))
95 return(1);
96 for ( ; *cp; cp++)
97 if ( ! (*cp)(m, m->last))
98 return(0);
99
100 return(1);
101 }
102
103
104 static int
105 check_root(POSTARGS)
106 {
107
108 if (NULL == m->first->child)
109 return(man_nerr(m, n, WNODATA));
110 if (NULL == m->meta.title)
111 return(man_nerr(m, n, WNOTITLE));
112
113 return(1);
114 }
115
116
117 static int
118 check_text(POSTARGS)
119 {
120 const char *p;
121 int pos;
122
123 assert(n->string);
124
125 for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
126 if ('\t' == *p || isprint((u_char)*p))
127 continue;
128
129 if (MAN_IGN_CHARS & m->pflags)
130 return(man_pwarn(m, n->line, pos, WNPRINT));
131 return(man_perr(m, n->line, pos, WNPRINT));
132 }
133
134 return(1);
135 }
136
137
138 #define INEQ_DEFINE(x, ineq, name) \
139 static int \
140 check_##name(POSTARGS) \
141 { \
142 if (n->nchild ineq (x)) \
143 return(1); \
144 return(man_verr(m, n->line, n->pos, \
145 "expected line arguments %s %d, have %d", \
146 #ineq, (x), n->nchild)); \
147 }
148
149 INEQ_DEFINE(0, ==, eq0)
150 INEQ_DEFINE(1, >=, ge1)
151 INEQ_DEFINE(2, >=, ge2)
152 INEQ_DEFINE(1, <=, le1)
153 INEQ_DEFINE(2, <=, le2)
154 INEQ_DEFINE(5, <=, le5)
155