]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.16 2009/07/24 20:22:24 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 POSTARGS struct man *m, const struct man_node *n
31 typedef int (*v_post
)(POSTARGS
);
37 static int check_eq0(POSTARGS
);
38 static int check_eq1(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
);
44 static int check_root(POSTARGS
);
45 static int check_sp(POSTARGS
);
46 static int check_text(POSTARGS
);
48 static v_post posts_eq0
[] = { check_eq0
, NULL
};
49 static v_post posts_ge1
[] = { check_ge1
, NULL
};
50 static v_post posts_ge2_le5
[] = { check_ge2
, check_le5
, NULL
};
51 static v_post posts_le1
[] = { check_le1
, NULL
};
52 static v_post posts_le2
[] = { check_le2
, NULL
};
53 static v_post posts_sp
[] = { check_sp
, NULL
};
55 static const struct man_valid man_valids
[MAN_MAX
] = {
56 { posts_eq0
}, /* br */
57 { posts_ge2_le5
}, /* TH */
58 { posts_ge1
}, /* SH */
59 { posts_ge1
}, /* SS */
61 { posts_eq0
}, /* LP */
62 { posts_eq0
}, /* PP */
63 { posts_eq0
}, /* P */
64 { posts_le2
}, /* IP */
65 { posts_le1
}, /* HP */
77 { posts_eq0
}, /* na */
79 { posts_sp
}, /* sp */
84 man_valid_post(struct man
*m
)
88 if (MAN_VALID
& m
->last
->flags
)
90 m
->last
->flags
|= MAN_VALID
;
92 switch (m
->last
->type
) {
94 return(check_text(m
, m
->last
));
96 return(check_root(m
, m
->last
));
101 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
104 if ( ! (*cp
)(m
, m
->last
))
115 if (NULL
== m
->first
->child
)
116 return(man_nerr(m
, n
, WNODATA
));
117 if (NULL
== m
->meta
.title
)
118 return(man_nerr(m
, n
, WNOTITLE
));
132 for (p
= n
->string
, pos
= n
->pos
+ 1; *p
; p
++, pos
++) {
134 c
= mandoc_special(p
);
140 if ( ! (MAN_IGN_ESCAPE
& m
->pflags
))
141 return(man_perr(m
, n
->line
, pos
, WESCAPE
));
142 if ( ! man_pwarn(m
, n
->line
, pos
, WESCAPE
))
147 if ('\t' == *p
|| isprint((u_char
)*p
))
150 if (MAN_IGN_CHARS
& m
->pflags
)
151 return(man_pwarn(m
, n
->line
, pos
, WNPRINT
));
152 return(man_perr(m
, n
->line
, pos
, WNPRINT
));
159 #define INEQ_DEFINE(x, ineq, name) \
161 check_##name(POSTARGS) \
163 if (n->nchild ineq (x)) \
165 return(man_verr(m, n->line, n->pos, \
166 "expected line arguments %s %d, have %d", \
167 #ineq, (x), n->nchild)); \
170 INEQ_DEFINE(0, ==, eq0
)
171 INEQ_DEFINE(1, ==, eq1
)
172 INEQ_DEFINE(1, >=, ge1
)
173 INEQ_DEFINE(2, >=, ge2
)
174 INEQ_DEFINE(1, <=, le1
)
175 INEQ_DEFINE(2, <=, le2
)
176 INEQ_DEFINE(5, <=, le5
)
185 if (NULL
== m
->last
->child
)
187 else if ( ! check_eq1(m
, n
))
190 assert(MAN_TEXT
== m
->last
->child
->type
);
191 buf
= m
->last
->child
->string
;
194 /* From OpenBSD's strtol(3). */
196 lval
= strtol(buf
, &ep
, 10);
197 if (buf
[0] == '\0' || *ep
!= '\0')
198 return(man_nerr(m
, m
->last
->child
, WNUMFMT
));
200 if ((errno
== ERANGE
&& (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
201 (lval
> INT_MAX
|| lval
< 0))
202 return(man_nerr(m
, m
->last
->child
, WNUMFMT
));