]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.21 2009/08/20 11:51:07 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
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.
17 #include <sys/types.h>
27 #include "libmandoc.h"
29 #define CHKARGS struct man *m, const struct man_node *n
31 typedef int (*v_check
)(CHKARGS
);
38 static int check_bline(CHKARGS
);
39 static int check_eline(CHKARGS
);
40 static int check_eq0(CHKARGS
);
41 static int check_eq1(CHKARGS
);
42 static int check_ge2(CHKARGS
);
43 static int check_le5(CHKARGS
);
44 static int check_par(CHKARGS
);
45 static int check_root(CHKARGS
);
46 static int check_sec(CHKARGS
);
47 static int check_sp(CHKARGS
);
48 static int check_text(CHKARGS
);
50 static v_check posts_eq0
[] = { check_eq0
, NULL
};
51 static v_check posts_ge2_le5
[] = { check_ge2
, check_le5
, NULL
};
52 static v_check posts_par
[] = { check_par
, NULL
};
53 static v_check posts_sec
[] = { check_sec
, NULL
};
54 static v_check posts_sp
[] = { check_sp
, NULL
};
55 static v_check pres_eline
[] = { check_eline
, NULL
};
56 static v_check pres_bline
[] = { check_bline
, NULL
};
58 static const struct man_valid man_valids
[MAN_MAX
] = {
59 { pres_bline
, posts_eq0
}, /* br */
60 { pres_bline
, posts_ge2_le5
}, /* TH */
61 { pres_bline
, posts_sec
}, /* SH */
62 { pres_bline
, posts_sec
}, /* SS */
63 { pres_bline
, posts_par
}, /* TP */
64 { pres_bline
, posts_par
}, /* LP */
65 { pres_bline
, posts_par
}, /* PP */
66 { pres_bline
, posts_par
}, /* P */
67 { pres_bline
, posts_par
}, /* IP */
68 { pres_bline
, posts_par
}, /* HP */
69 { pres_eline
, NULL
}, /* SM */
70 { pres_eline
, NULL
}, /* SB */
71 { NULL
, NULL
}, /* BI */
72 { NULL
, NULL
}, /* IB */
73 { NULL
, NULL
}, /* BR */
74 { NULL
, NULL
}, /* RB */
75 { pres_eline
, NULL
}, /* R */
76 { pres_eline
, NULL
}, /* B */
77 { pres_eline
, NULL
}, /* I */
78 { NULL
, NULL
}, /* IR */
79 { NULL
, NULL
}, /* RI */
80 { pres_bline
, posts_eq0
}, /* na */
81 { NULL
, NULL
}, /* i */
82 { pres_bline
, posts_sp
}, /* sp */
83 { pres_bline
, posts_eq0
}, /* nf */
84 { pres_bline
, posts_eq0
}, /* fi */
85 { NULL
, NULL
}, /* r */
86 { NULL
, NULL
}, /* RE */
87 { NULL
, NULL
}, /* RS */ /* FIXME: warn if empty body. */
88 { NULL
, NULL
}, /* DT */
93 man_valid_pre(struct man
*m
, const struct man_node
*n
)
97 if (MAN_TEXT
== n
->type
)
99 if (MAN_ROOT
== n
->type
)
102 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
112 man_valid_post(struct man
*m
)
116 if (MAN_VALID
& m
->last
->flags
)
118 m
->last
->flags
|= MAN_VALID
;
120 switch (m
->last
->type
) {
122 return(check_text(m
, m
->last
));
124 return(check_root(m
, m
->last
));
129 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
132 if ( ! (*cp
)(m
, m
->last
))
143 /* XXX - make this into a warning? */
144 if (MAN_BLINE
& m
->flags
)
145 return(man_nerr(m
, n
, WEXITSCOPE
));
146 /* XXX - make this into a warning? */
147 if (MAN_ELINE
& m
->flags
)
148 return(man_nerr(m
, n
, WEXITSCOPE
));
150 if (NULL
== m
->first
->child
)
151 return(man_nerr(m
, n
, WNODATA
));
152 if (NULL
== m
->meta
.title
)
153 return(man_nerr(m
, n
, WNOTITLE
));
167 for (p
= n
->string
, pos
= n
->pos
+ 1; *p
; p
++, pos
++) {
169 c
= mandoc_special(p
);
175 if ( ! (MAN_IGN_ESCAPE
& m
->pflags
))
176 return(man_perr(m
, n
->line
, pos
, WESCAPE
));
177 if ( ! man_pwarn(m
, n
->line
, pos
, WESCAPE
))
182 if ('\t' == *p
|| isprint((u_char
)*p
))
185 if (MAN_IGN_CHARS
& m
->pflags
)
186 return(man_pwarn(m
, n
->line
, pos
, WNPRINT
));
187 return(man_perr(m
, n
->line
, pos
, WNPRINT
));
194 #define INEQ_DEFINE(x, ineq, name) \
196 check_##name(CHKARGS) \
198 if (n->nchild ineq (x)) \
200 return(man_verr(m, n->line, n->pos, \
201 "expected line arguments %s %d, have %d", \
202 #ineq, (x), n->nchild)); \
205 INEQ_DEFINE(0, ==, eq0
)
206 INEQ_DEFINE(1, ==, eq1
)
207 INEQ_DEFINE(2, >=, ge2
)
208 INEQ_DEFINE(5, <=, le5
)
217 if (NULL
== n
->child
)
219 else if ( ! check_eq1(m
, n
))
222 assert(MAN_TEXT
== n
->child
->type
);
223 buf
= n
->child
->string
;
226 /* From OpenBSD's strtol(3). */
229 lval
= strtol(buf
, &ep
, 10);
230 if (buf
[0] == '\0' || *ep
!= '\0')
231 return(man_nerr(m
, n
->child
, WNUMFMT
));
233 if ((errno
== ERANGE
&& (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
234 (lval
> INT_MAX
|| lval
< 0))
235 return(man_nerr(m
, n
->child
, WNUMFMT
));
245 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
246 return(man_nwarn(m
, n
, WBODYARGS
));
247 if (MAN_HEAD
== n
->type
&& 0 == n
->nchild
)
248 return(man_nerr(m
, n
, WHEADARGS
));
257 if (MAN_BODY
== n
->type
)
264 /* Body-less lists are ok. */
269 return(man_nwarn(m
, n
, WBODYARGS
));
271 if (MAN_HEAD
== n
->type
)
280 return(man_nwarn(m
, n
, WNHEADARGS
));
284 return(man_nwarn(m
, n
, WHEADARGS
));
295 if (MAN_ELINE
& m
->flags
)
296 return(man_nerr(m
, n
, WLNSCOPE
));
305 if (MAN_BLINE
& m
->flags
)
306 return(man_nerr(m
, n
, WLNSCOPE
));
307 if (MAN_ELINE
& m
->flags
)
308 return(man_nerr(m
, n
, WLNSCOPE
));