Bring in OpenBSD's mandoc(1) tool for formatting manual pages.
[dragonfly.git] / usr.bin / mandoc / man_validate.c
1 /*      $Id: man_validate.c,v 1.10 2009/10/21 19:13:50 schwarze 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 <errno.h>
22 #include <limits.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25
26 #include "libman.h"
27 #include "libmandoc.h"
28
29 #define CHKARGS   struct man *m, const struct man_node *n
30
31 typedef int     (*v_check)(CHKARGS);
32
33 struct  man_valid {
34         v_check  *pres;
35         v_check  *posts;
36 };
37
38 static  int       check_bline(CHKARGS);
39 static  int       check_eq0(CHKARGS);
40 static  int       check_le1(CHKARGS);
41 static  int       check_ge2(CHKARGS);
42 static  int       check_le5(CHKARGS);
43 static  int       check_par(CHKARGS);
44 static  int       check_part(CHKARGS);
45 static  int       check_root(CHKARGS);
46 static  int       check_sec(CHKARGS);
47 static  int       check_text(CHKARGS);
48
49 static  v_check   posts_eq0[] = { check_eq0, NULL };
50 static  v_check   posts_ge2_le5[] = { check_ge2, check_le5, NULL };
51 static  v_check   posts_par[] = { check_par, NULL };
52 static  v_check   posts_part[] = { check_part, NULL };
53 static  v_check   posts_sec[] = { check_sec, NULL };
54 static  v_check   posts_sp[] = { check_le1, NULL };
55 static  v_check   pres_bline[] = { check_bline, NULL };
56
57 static  const struct man_valid man_valids[MAN_MAX] = {
58         { pres_bline, posts_eq0 }, /* br */
59         { pres_bline, posts_ge2_le5 }, /* TH */
60         { pres_bline, posts_sec }, /* SH */
61         { pres_bline, posts_sec }, /* SS */
62         { pres_bline, posts_par }, /* TP */
63         { pres_bline, posts_par }, /* LP */
64         { pres_bline, posts_par }, /* PP */
65         { pres_bline, posts_par }, /* P */
66         { pres_bline, posts_par }, /* IP */
67         { pres_bline, posts_par }, /* HP */
68         { NULL, NULL }, /* SM */
69         { NULL, NULL }, /* SB */
70         { NULL, NULL }, /* BI */
71         { NULL, NULL }, /* IB */
72         { NULL, NULL }, /* BR */
73         { NULL, NULL }, /* RB */
74         { NULL, NULL }, /* R */
75         { NULL, NULL }, /* B */
76         { NULL, NULL }, /* I */
77         { NULL, NULL }, /* IR */
78         { NULL, NULL }, /* RI */
79         { pres_bline, posts_eq0 }, /* na */
80         { NULL, NULL }, /* i */
81         { pres_bline, posts_sp }, /* sp */
82         { pres_bline, posts_eq0 }, /* nf */
83         { pres_bline, posts_eq0 }, /* fi */
84         { NULL, NULL }, /* r */
85         { NULL, NULL }, /* RE */
86         { NULL, posts_part }, /* RS */
87         { NULL, NULL }, /* DT */
88         { NULL, NULL }, /* UC */
89 };
90
91
92 int
93 man_valid_pre(struct man *m, const struct man_node *n)
94 {
95         v_check         *cp;
96
97         if (MAN_TEXT == n->type)
98                 return(1);
99         if (MAN_ROOT == n->type)
100                 return(1);
101
102         if (NULL == (cp = man_valids[n->tok].pres))
103                 return(1);
104         for ( ; *cp; cp++)
105                 if ( ! (*cp)(m, n))
106                         return(0);
107         return(1);
108 }
109
110
111 int
112 man_valid_post(struct man *m)
113 {
114         v_check         *cp;
115
116         if (MAN_VALID & m->last->flags)
117                 return(1);
118         m->last->flags |= MAN_VALID;
119
120         switch (m->last->type) {
121         case (MAN_TEXT):
122                 return(check_text(m, m->last));
123         case (MAN_ROOT):
124                 return(check_root(m, m->last));
125         default:
126                 break;
127         }
128
129         if (NULL == (cp = man_valids[m->last->tok].posts))
130                 return(1);
131         for ( ; *cp; cp++)
132                 if ( ! (*cp)(m, m->last))
133                         return(0);
134
135         return(1);
136 }
137
138
139 static int
140 check_root(CHKARGS)
141 {
142
143         if (MAN_BLINE & m->flags)
144                 return(man_nwarn(m, n, WEXITSCOPE));
145         if (MAN_ELINE & m->flags)
146                 return(man_nwarn(m, n, WEXITSCOPE));
147
148         m->flags &= ~MAN_BLINE;
149         m->flags &= ~MAN_ELINE;
150
151         if (NULL == m->first->child)
152                 return(man_nerr(m, n, WNODATA));
153         if (NULL == m->meta.title)
154                 return(man_nerr(m, n, WNOTITLE));
155
156         return(1);
157 }
158
159
160 static int
161 check_text(CHKARGS)
162 {
163         const char      *p;
164         int              pos, c;
165
166         assert(n->string);
167
168         for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
169                 if ('\\' == *p) {
170                         c = mandoc_special(p);
171                         if (c) {
172                                 p += c - 1;
173                                 pos += c - 1;
174                                 continue;
175                         }
176                         if ( ! (MAN_IGN_ESCAPE & m->pflags))
177                                 return(man_perr(m, n->line, pos, WESCAPE));
178                         if ( ! man_pwarn(m, n->line, pos, WESCAPE))
179                                 return(0);
180                         continue;
181                 }
182
183                 if ('\t' == *p || isprint((u_char)*p))
184                         continue;
185
186                 if (MAN_IGN_CHARS & m->pflags)
187                         return(man_pwarn(m, n->line, pos, WNPRINT));
188                 return(man_perr(m, n->line, pos, WNPRINT));
189         }
190
191         return(1);
192 }
193
194
195 #define INEQ_DEFINE(x, ineq, name) \
196 static int \
197 check_##name(CHKARGS) \
198 { \
199         if (n->nchild ineq (x)) \
200                 return(1); \
201         return(man_verr(m, n->line, n->pos, \
202                         "expected line arguments %s %d, have %d", \
203                         #ineq, (x), n->nchild)); \
204 }
205
206 INEQ_DEFINE(0, ==, eq0)
207 INEQ_DEFINE(1, <=, le1)
208 INEQ_DEFINE(2, >=, ge2)
209 INEQ_DEFINE(5, <=, le5)
210
211
212 static int
213 check_sec(CHKARGS)
214 {
215
216         if (MAN_BODY == n->type && 0 == n->nchild)
217                 return(man_nwarn(m, n, WBODYARGS));
218         if (MAN_HEAD == n->type && 0 == n->nchild)
219                 return(man_nerr(m, n, WHEADARGS));
220         return(1);
221 }
222
223
224 static int
225 check_part(CHKARGS)
226 {
227
228         if (MAN_BODY == n->type && 0 == n->nchild)
229                 return(man_nwarn(m, n, WBODYARGS));
230         return(1);
231 }
232
233
234 static int
235 check_par(CHKARGS)
236 {
237
238         if (MAN_BODY == n->type)
239                 switch (n->tok) {
240                 case (MAN_IP):
241                         /* FALLTHROUGH */
242                 case (MAN_HP):
243                         /* FALLTHROUGH */
244                 case (MAN_TP):
245                         /* Body-less lists are ok. */
246                         break;
247                 default:
248                         if (n->nchild)
249                                 break;
250                         return(man_nwarn(m, n, WBODYARGS));
251                 }
252         if (MAN_HEAD == n->type)
253                 switch (n->tok) {
254                 case (MAN_PP):
255                         /* FALLTHROUGH */
256                 case (MAN_P):
257                         /* FALLTHROUGH */
258                 case (MAN_LP):
259                         if (0 == n->nchild)
260                                 break;
261                         return(man_nwarn(m, n, WNHEADARGS));
262                 default:
263                         if (n->nchild)
264                                 break;
265                         return(man_nwarn(m, n, WHEADARGS));
266                 }
267
268         return(1);
269 }
270
271
272 static int
273 check_bline(CHKARGS)
274 {
275
276         assert( ! (MAN_ELINE & m->flags));
277         if (MAN_BLINE & m->flags)
278                 return(man_nerr(m, n, WLNSCOPE));
279         return(1);
280 }