]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
0e0010dd87f1af19cea1788547188cc6ade1f57a
1 /* $Id: man_validate.c,v 1.47 2010/07/22 23:03:15 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
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.
21 #include <sys/types.h>
33 #include "libmandoc.h"
35 #define CHKARGS struct man *m, struct man_node *n
37 typedef int (*v_check
)(CHKARGS
);
44 static int check_bline(CHKARGS
);
45 static int check_eq0(CHKARGS
);
46 static int check_le1(CHKARGS
);
47 static int check_ge2(CHKARGS
);
48 static int check_le5(CHKARGS
);
49 static int check_par(CHKARGS
);
50 static int check_part(CHKARGS
);
51 static int check_root(CHKARGS
);
52 static int check_sec(CHKARGS
);
53 static int check_text(CHKARGS
);
54 static int check_title(CHKARGS
);
56 static v_check posts_eq0
[] = { check_eq0
, NULL
};
57 static v_check posts_th
[] = { check_ge2
, check_le5
, check_title
, NULL
};
58 static v_check posts_par
[] = { check_par
, NULL
};
59 static v_check posts_part
[] = { check_part
, NULL
};
60 static v_check posts_sec
[] = { check_sec
, NULL
};
61 static v_check posts_le1
[] = { check_le1
, NULL
};
62 static v_check pres_bline
[] = { check_bline
, NULL
};
64 static const struct man_valid man_valids
[MAN_MAX
] = {
65 { NULL
, posts_eq0
}, /* br */
66 { pres_bline
, posts_th
}, /* TH */
67 { pres_bline
, posts_sec
}, /* SH */
68 { pres_bline
, posts_sec
}, /* SS */
69 { pres_bline
, posts_par
}, /* TP */
70 { pres_bline
, posts_par
}, /* LP */
71 { pres_bline
, posts_par
}, /* PP */
72 { pres_bline
, posts_par
}, /* P */
73 { pres_bline
, posts_par
}, /* IP */
74 { pres_bline
, posts_par
}, /* HP */
75 { NULL
, NULL
}, /* SM */
76 { NULL
, NULL
}, /* SB */
77 { NULL
, NULL
}, /* BI */
78 { NULL
, NULL
}, /* IB */
79 { NULL
, NULL
}, /* BR */
80 { NULL
, NULL
}, /* RB */
81 { NULL
, NULL
}, /* R */
82 { NULL
, NULL
}, /* B */
83 { NULL
, NULL
}, /* I */
84 { NULL
, NULL
}, /* IR */
85 { NULL
, NULL
}, /* RI */
86 { NULL
, posts_eq0
}, /* na */ /* FIXME: should warn only. */
87 { NULL
, NULL
}, /* i */
88 { NULL
, posts_le1
}, /* sp */ /* FIXME: should warn only. */
89 { pres_bline
, posts_eq0
}, /* nf */
90 { pres_bline
, posts_eq0
}, /* fi */
91 { NULL
, NULL
}, /* r */
92 { NULL
, NULL
}, /* RE */
93 { NULL
, posts_part
}, /* RS */
94 { NULL
, NULL
}, /* DT */
95 { NULL
, NULL
}, /* UC */
96 { NULL
, NULL
}, /* PD */
97 { NULL
, posts_le1
}, /* Sp */ /* FIXME: should warn only. */
98 { pres_bline
, posts_le1
}, /* Vb */ /* FIXME: should warn only. */
99 { pres_bline
, posts_eq0
}, /* Ve */
100 { NULL
, NULL
}, /* AT */
101 { NULL
, NULL
}, /* in */
106 man_valid_pre(struct man
*m
, struct man_node
*n
)
110 if (MAN_TEXT
== n
->type
)
112 if (MAN_ROOT
== n
->type
)
115 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
125 man_valid_post(struct man
*m
)
129 if (MAN_VALID
& m
->last
->flags
)
131 m
->last
->flags
|= MAN_VALID
;
133 switch (m
->last
->type
) {
135 return(check_text(m
, m
->last
));
137 return(check_root(m
, m
->last
));
142 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
145 if ( ! (*cp
)(m
, m
->last
))
156 if (MAN_BLINE
& m
->flags
)
157 return(man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
));
158 if (MAN_ELINE
& m
->flags
)
159 return(man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
));
161 m
->flags
&= ~MAN_BLINE
;
162 m
->flags
&= ~MAN_ELINE
;
164 if (NULL
== m
->first
->child
) {
165 man_nmsg(m
, n
, MANDOCERR_NODOCBODY
);
167 } else if (NULL
== m
->meta
.title
) {
168 if ( ! man_nmsg(m
, n
, MANDOCERR_NOTITLE
))
171 * If a title hasn't been set, do so now (by
172 * implication, date and section also aren't set).
174 * FIXME: this should be in man_action.c.
176 m
->meta
.title
= mandoc_strdup("unknown");
177 m
->meta
.date
= time(NULL
);
178 m
->meta
.msec
= mandoc_strdup("1");
191 /* FIXME: is this sufficient? */
192 if ('\0' == *n
->child
->string
) {
193 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
197 for (p
= n
->child
->string
; '\0' != *p
; p
++)
198 if (isalpha((u_char
)*p
) && ! isupper((u_char
)*p
))
199 if ( ! man_nmsg(m
, n
, MANDOCERR_UPPERCASE
))
213 for (p
= n
->string
, pos
= n
->pos
+ 1; *p
; p
++, pos
++) {
214 sz
= strcspn(p
, "\t\\");
223 if (MAN_LITERAL
& m
->flags
)
225 if (man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADTAB
))
230 /* Check the special character. */
232 c
= mandoc_special(p
);
239 c
= man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADESCAPE
);
240 if ( ! (MAN_IGN_ESCAPE
& m
->pflags
) && ! c
)
248 #define INEQ_DEFINE(x, ineq, name) \
250 check_##name(CHKARGS) \
252 if (n->nchild ineq (x)) \
254 man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
255 "line arguments %s %d (have %d)", \
256 #ineq, (x), n->nchild); \
260 INEQ_DEFINE(0, ==, eq0
)
261 INEQ_DEFINE(1, <=, le1
)
262 INEQ_DEFINE(2, >=, ge2
)
263 INEQ_DEFINE(5, <=, le5
)
270 if (MAN_HEAD
== n
->type
&& 0 == n
->nchild
) {
271 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
273 } else if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
274 return(man_nmsg(m
, n
, MANDOCERR_NOBODY
));
284 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
285 return(man_nmsg(m
, n
, MANDOCERR_NOBODY
));
294 if (MAN_BODY
== n
->type
)
301 /* Body-less lists are ok. */
306 return(man_nmsg(m
, n
, MANDOCERR_NOBODY
));
308 if (MAN_HEAD
== n
->type
)
317 return(man_nmsg(m
, n
, MANDOCERR_ARGSLOST
));
321 return(man_nmsg(m
, n
, MANDOCERR_NOARGS
));
332 assert( ! (MAN_ELINE
& m
->flags
));
333 if (MAN_BLINE
& m
->flags
) {
334 man_nmsg(m
, n
, MANDOCERR_SYNTLINESCOPE
);