]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.44 2010/06/19 20:46:28 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>
32 #include "libmandoc.h"
34 #define CHKARGS struct man *m, struct man_node *n
36 typedef int (*v_check
)(CHKARGS
);
43 static int check_bline(CHKARGS
);
44 static int check_eq0(CHKARGS
);
45 static int check_le1(CHKARGS
);
46 static int check_ge2(CHKARGS
);
47 static int check_le5(CHKARGS
);
48 static int check_par(CHKARGS
);
49 static int check_part(CHKARGS
);
50 static int check_root(CHKARGS
);
51 static int check_sec(CHKARGS
);
52 static int check_text(CHKARGS
);
53 static int check_title(CHKARGS
);
55 static v_check posts_eq0
[] = { check_eq0
, NULL
};
56 static v_check posts_th
[] = { check_ge2
, check_le5
, check_title
, NULL
};
57 static v_check posts_par
[] = { check_par
, NULL
};
58 static v_check posts_part
[] = { check_part
, NULL
};
59 static v_check posts_sec
[] = { check_sec
, NULL
};
60 static v_check posts_le1
[] = { check_le1
, NULL
};
61 static v_check pres_bline
[] = { check_bline
, NULL
};
63 static const struct man_valid man_valids
[MAN_MAX
] = {
64 { NULL
, posts_eq0
}, /* br */
65 { pres_bline
, posts_th
}, /* TH */
66 { pres_bline
, posts_sec
}, /* SH */
67 { pres_bline
, posts_sec
}, /* SS */
68 { pres_bline
, posts_par
}, /* TP */
69 { pres_bline
, posts_par
}, /* LP */
70 { pres_bline
, posts_par
}, /* PP */
71 { pres_bline
, posts_par
}, /* P */
72 { pres_bline
, posts_par
}, /* IP */
73 { pres_bline
, posts_par
}, /* HP */
74 { NULL
, NULL
}, /* SM */
75 { NULL
, NULL
}, /* SB */
76 { NULL
, NULL
}, /* BI */
77 { NULL
, NULL
}, /* IB */
78 { NULL
, NULL
}, /* BR */
79 { NULL
, NULL
}, /* RB */
80 { NULL
, NULL
}, /* R */
81 { NULL
, NULL
}, /* B */
82 { NULL
, NULL
}, /* I */
83 { NULL
, NULL
}, /* IR */
84 { NULL
, NULL
}, /* RI */
85 { NULL
, posts_eq0
}, /* na */
86 { NULL
, NULL
}, /* i */
87 { NULL
, posts_le1
}, /* sp */
88 { pres_bline
, posts_eq0
}, /* nf */
89 { pres_bline
, posts_eq0
}, /* fi */
90 { NULL
, NULL
}, /* r */
91 { NULL
, NULL
}, /* RE */
92 { NULL
, posts_part
}, /* RS */
93 { NULL
, NULL
}, /* DT */
94 { NULL
, NULL
}, /* UC */
95 { NULL
, NULL
}, /* PD */
96 { NULL
, posts_le1
}, /* Sp */
97 { pres_bline
, posts_le1
}, /* Vb */
98 { pres_bline
, posts_eq0
}, /* Ve */
99 { NULL
, NULL
}, /* AT */
104 man_valid_pre(struct man
*m
, struct man_node
*n
)
108 if (MAN_TEXT
== n
->type
)
110 if (MAN_ROOT
== n
->type
)
113 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
123 man_valid_post(struct man
*m
)
127 if (MAN_VALID
& m
->last
->flags
)
129 m
->last
->flags
|= MAN_VALID
;
131 switch (m
->last
->type
) {
133 return(check_text(m
, m
->last
));
135 return(check_root(m
, m
->last
));
140 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
143 if ( ! (*cp
)(m
, m
->last
))
154 if (MAN_BLINE
& m
->flags
)
155 return(man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
));
156 if (MAN_ELINE
& m
->flags
)
157 return(man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
));
159 m
->flags
&= ~MAN_BLINE
;
160 m
->flags
&= ~MAN_ELINE
;
162 if (NULL
== m
->first
->child
) {
163 man_nmsg(m
, n
, MANDOCERR_NODOCBODY
);
165 } else if (NULL
== m
->meta
.title
) {
166 if ( ! man_nmsg(m
, n
, MANDOCERR_NOTITLE
))
169 * If a title hasn't been set, do so now (by
170 * implication, date and section also aren't set).
172 * FIXME: this should be in man_action.c.
174 m
->meta
.title
= mandoc_strdup("unknown");
175 m
->meta
.date
= time(NULL
);
176 m
->meta
.msec
= mandoc_strdup("1");
189 /* FIXME: is this sufficient? */
190 if ('\0' == *n
->child
->string
) {
191 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
195 for (p
= n
->child
->string
; '\0' != *p
; p
++)
196 if (isalpha((u_char
)*p
) && ! isupper((u_char
)*p
))
197 if ( ! man_nmsg(m
, n
, MANDOCERR_UPPERCASE
))
212 for (p
= n
->string
, pos
= n
->pos
+ 1; *p
; p
++, pos
++) {
214 c
= mandoc_special(p
);
221 c
= man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADESCAPE
);
222 if ( ! (MAN_IGN_ESCAPE
& m
->pflags
) && ! c
)
226 if ('\t' == *p
|| isprint((u_char
)*p
) || ASCII_HYPH
== *p
)
228 if ( ! man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADCHAR
))
236 #define INEQ_DEFINE(x, ineq, name) \
238 check_##name(CHKARGS) \
240 if (n->nchild ineq (x)) \
242 man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
243 "line arguments %s %d (have %d)", \
244 #ineq, (x), n->nchild); \
248 INEQ_DEFINE(0, ==, eq0
)
249 INEQ_DEFINE(1, <=, le1
)
250 INEQ_DEFINE(2, >=, ge2
)
251 INEQ_DEFINE(5, <=, le5
)
258 if (MAN_HEAD
== n
->type
&& 0 == n
->nchild
) {
259 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
261 } else if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
262 return(man_nmsg(m
, n
, MANDOCERR_NOBODY
));
272 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
273 return(man_nmsg(m
, n
, MANDOCERR_NOBODY
));
282 if (MAN_BODY
== n
->type
)
289 /* Body-less lists are ok. */
294 return(man_nmsg(m
, n
, MANDOCERR_NOBODY
));
296 if (MAN_HEAD
== n
->type
)
305 return(man_nmsg(m
, n
, MANDOCERR_ARGSLOST
));
309 return(man_nmsg(m
, n
, MANDOCERR_NOARGS
));
320 assert( ! (MAN_ELINE
& m
->flags
));
321 if (MAN_BLINE
& m
->flags
) {
322 man_nmsg(m
, n
, MANDOCERR_SYNTLINESCOPE
);