]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.22 2009/08/21 12:12:12 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_eq0(CHKARGS
);
40 static int check_eq1(CHKARGS
);
41 static int check_ge2(CHKARGS
);
42 static int check_le5(CHKARGS
);
43 static int check_par(CHKARGS
);
44 static int check_root(CHKARGS
);
45 static int check_sec(CHKARGS
);
46 static int check_sp(CHKARGS
);
47 static int check_text(CHKARGS
);
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_sec
[] = { check_sec
, NULL
};
53 static v_check posts_sp
[] = { check_sp
, NULL
};
54 static v_check pres_bline
[] = { check_bline
, NULL
};
56 static const struct man_valid man_valids
[MAN_MAX
] = {
57 { pres_bline
, posts_eq0
}, /* br */
58 { pres_bline
, posts_ge2_le5
}, /* TH */
59 { pres_bline
, posts_sec
}, /* SH */
60 { pres_bline
, posts_sec
}, /* SS */
61 { pres_bline
, posts_par
}, /* TP */
62 { pres_bline
, posts_par
}, /* LP */
63 { pres_bline
, posts_par
}, /* PP */
64 { pres_bline
, posts_par
}, /* P */
65 { pres_bline
, posts_par
}, /* IP */
66 { pres_bline
, posts_par
}, /* HP */
67 { NULL
, NULL
}, /* SM */
68 { NULL
, NULL
}, /* SB */
69 { NULL
, NULL
}, /* BI */
70 { NULL
, NULL
}, /* IB */
71 { NULL
, NULL
}, /* BR */
72 { NULL
, NULL
}, /* RB */
73 { NULL
, NULL
}, /* R */
74 { NULL
, NULL
}, /* B */
75 { NULL
, NULL
}, /* I */
76 { NULL
, NULL
}, /* IR */
77 { NULL
, NULL
}, /* RI */
78 { pres_bline
, posts_eq0
}, /* na */
79 { NULL
, NULL
}, /* i */
80 { pres_bline
, posts_sp
}, /* sp */
81 { pres_bline
, posts_eq0
}, /* nf */
82 { pres_bline
, posts_eq0
}, /* fi */
83 { NULL
, NULL
}, /* r */
84 { NULL
, NULL
}, /* RE */
85 { NULL
, NULL
}, /* RS */ /* FIXME: warn if empty body. */
86 { NULL
, NULL
}, /* DT */
91 man_valid_pre(struct man
*m
, const struct man_node
*n
)
95 if (MAN_TEXT
== n
->type
)
97 if (MAN_ROOT
== n
->type
)
100 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
110 man_valid_post(struct man
*m
)
114 if (MAN_VALID
& m
->last
->flags
)
116 m
->last
->flags
|= MAN_VALID
;
118 switch (m
->last
->type
) {
120 return(check_text(m
, m
->last
));
122 return(check_root(m
, m
->last
));
127 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
130 if ( ! (*cp
)(m
, m
->last
))
141 if (MAN_BLINE
& m
->flags
)
142 return(man_nwarn(m
, n
, WEXITSCOPE
));
143 if (MAN_ELINE
& m
->flags
)
144 return(man_nwarn(m
, n
, WEXITSCOPE
));
146 m
->flags
&= ~MAN_BLINE
;
147 m
->flags
&= ~MAN_ELINE
;
149 if (NULL
== m
->first
->child
)
150 return(man_nerr(m
, n
, WNODATA
));
151 if (NULL
== m
->meta
.title
)
152 return(man_nerr(m
, n
, WNOTITLE
));
166 for (p
= n
->string
, pos
= n
->pos
+ 1; *p
; p
++, pos
++) {
168 c
= mandoc_special(p
);
174 if ( ! (MAN_IGN_ESCAPE
& m
->pflags
))
175 return(man_perr(m
, n
->line
, pos
, WESCAPE
));
176 if ( ! man_pwarn(m
, n
->line
, pos
, WESCAPE
))
181 if ('\t' == *p
|| isprint((u_char
)*p
))
184 if (MAN_IGN_CHARS
& m
->pflags
)
185 return(man_pwarn(m
, n
->line
, pos
, WNPRINT
));
186 return(man_perr(m
, n
->line
, pos
, WNPRINT
));
193 #define INEQ_DEFINE(x, ineq, name) \
195 check_##name(CHKARGS) \
197 if (n->nchild ineq (x)) \
199 return(man_verr(m, n->line, n->pos, \
200 "expected line arguments %s %d, have %d", \
201 #ineq, (x), n->nchild)); \
204 INEQ_DEFINE(0, ==, eq0
)
205 INEQ_DEFINE(1, ==, eq1
)
206 INEQ_DEFINE(2, >=, ge2
)
207 INEQ_DEFINE(5, <=, le5
)
216 if (NULL
== n
->child
)
218 else if ( ! check_eq1(m
, n
))
221 assert(MAN_TEXT
== n
->child
->type
);
222 buf
= n
->child
->string
;
225 /* From OpenBSD's strtol(3). */
228 lval
= strtol(buf
, &ep
, 10);
229 if (buf
[0] == '\0' || *ep
!= '\0')
230 return(man_nerr(m
, n
->child
, WNUMFMT
));
232 if ((errno
== ERANGE
&& (lval
== LONG_MAX
|| lval
== LONG_MIN
)) ||
233 (lval
> INT_MAX
|| lval
< 0))
234 return(man_nerr(m
, n
->child
, WNUMFMT
));
244 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
245 return(man_nwarn(m
, n
, WBODYARGS
));
246 if (MAN_HEAD
== n
->type
&& 0 == n
->nchild
)
247 return(man_nerr(m
, n
, WHEADARGS
));
256 if (MAN_BODY
== n
->type
)
263 /* Body-less lists are ok. */
268 return(man_nwarn(m
, n
, WBODYARGS
));
270 if (MAN_HEAD
== n
->type
)
279 return(man_nwarn(m
, n
, WNHEADARGS
));
283 return(man_nwarn(m
, n
, WHEADARGS
));
294 assert( ! (MAN_ELINE
& m
->flags
));
295 if (MAN_BLINE
& m
->flags
)
296 return(man_nerr(m
, n
, WLNSCOPE
));