]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
d4c4009336fe8a8411458bf4c4c75c90b7682a86
1 /* $Id: man_validate.c,v 1.34 2010/04/03 14:12:48 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.
21 #include <sys/types.h>
31 #include "libmandoc.h"
33 #define CHKARGS struct man *m, const struct man_node *n
35 typedef int (*v_check
)(CHKARGS
);
42 static int check_bline(CHKARGS
);
43 static int check_eq0(CHKARGS
);
44 static int check_le1(CHKARGS
);
45 static int check_ge2(CHKARGS
);
46 static int check_le5(CHKARGS
);
47 static int check_par(CHKARGS
);
48 static int check_part(CHKARGS
);
49 static int check_roff(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
};
62 static v_check pres_roff
[] = { check_roff
, 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 */
87 { NULL
, NULL
}, /* i */
88 { NULL
, posts_le1
}, /* sp */
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_eq0
}, /* Sp */
98 { pres_bline
, posts_le1
}, /* Vb */
99 { pres_bline
, posts_eq0
}, /* Ve */
100 { pres_roff
, NULL
}, /* de */
101 { pres_roff
, NULL
}, /* dei */
102 { pres_roff
, NULL
}, /* am */
103 { pres_roff
, NULL
}, /* ami */
104 { pres_roff
, NULL
}, /* ig */
105 { NULL
, NULL
}, /* . */
110 man_valid_pre(struct man
*m
, const struct man_node
*n
)
114 if (MAN_TEXT
== n
->type
)
116 if (MAN_ROOT
== n
->type
)
119 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
129 man_valid_post(struct man
*m
)
133 if (MAN_VALID
& m
->last
->flags
)
135 m
->last
->flags
|= MAN_VALID
;
137 switch (m
->last
->type
) {
139 return(check_text(m
, m
->last
));
141 return(check_root(m
, m
->last
));
146 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
149 if ( ! (*cp
)(m
, m
->last
))
160 if (MAN_BLINE
& m
->flags
)
161 return(man_nwarn(m
, n
, WEXITSCOPE
));
162 if (MAN_ELINE
& m
->flags
)
163 return(man_nwarn(m
, n
, WEXITSCOPE
));
165 m
->flags
&= ~MAN_BLINE
;
166 m
->flags
&= ~MAN_ELINE
;
168 if (NULL
== m
->first
->child
)
169 return(man_nerr(m
, n
, WNODATA
));
170 if (NULL
== m
->meta
.title
) {
171 if ( ! man_nwarn(m
, n
, WNOTITLE
))
174 * If a title hasn't been set, do so now (by
175 * implication, date and section also aren't set).
177 * FIXME: this should be in man_action.c.
179 m
->meta
.title
= mandoc_strdup("unknown");
180 m
->meta
.date
= time(NULL
);
194 if ('\0' == *n
->child
->string
)
195 return(man_nerr(m
, n
, WNOTITLE
));
197 for (p
= n
->child
->string
; '\0' != *p
; p
++)
198 if (isalpha((u_char
)*p
) && ! isupper((u_char
)*p
))
199 if ( ! man_nwarn(m
, n
, WTITLECASE
))
214 for (p
= n
->string
, pos
= n
->pos
+ 1; *p
; p
++, pos
++) {
216 c
= mandoc_special(p
);
222 if ( ! (MAN_IGN_ESCAPE
& m
->pflags
))
223 return(man_perr(m
, n
->line
, pos
, WESCAPE
));
224 if ( ! man_pwarn(m
, n
->line
, pos
, WESCAPE
))
229 if ('\t' == *p
|| isprint((u_char
)*p
))
232 if (MAN_IGN_CHARS
& m
->pflags
)
233 return(man_pwarn(m
, n
->line
, pos
, WNPRINT
));
234 return(man_perr(m
, n
->line
, pos
, WNPRINT
));
241 #define INEQ_DEFINE(x, ineq, name) \
243 check_##name(CHKARGS) \
245 if (n->nchild ineq (x)) \
247 return(man_verr(m, n->line, n->pos, \
248 "expected line arguments %s %d, have %d", \
249 #ineq, (x), n->nchild)); \
252 INEQ_DEFINE(0, ==, eq0
)
253 INEQ_DEFINE(1, <=, le1
)
254 INEQ_DEFINE(2, >=, ge2
)
255 INEQ_DEFINE(5, <=, le5
)
262 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
263 return(man_nwarn(m
, n
, WBODYARGS
));
264 if (MAN_HEAD
== n
->type
&& 0 == n
->nchild
)
265 return(man_nerr(m
, n
, WHEADARGS
));
274 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
275 return(man_nwarn(m
, n
, WBODYARGS
));
284 if (MAN_BODY
== n
->type
)
291 /* Body-less lists are ok. */
296 return(man_nwarn(m
, n
, WBODYARGS
));
298 if (MAN_HEAD
== n
->type
)
307 return(man_nwarn(m
, n
, WNHEADARGS
));
311 return(man_nwarn(m
, n
, WHEADARGS
));
322 assert( ! (MAN_ELINE
& m
->flags
));
323 if (MAN_BLINE
& m
->flags
)
324 return(man_nerr(m
, n
, WLNSCOPE
));
334 if (MAN_BLOCK
!= n
->type
)
337 for (n
= n
->parent
; n
; n
= n
->parent
)
338 if (MAN_de
== n
->tok
|| MAN_dei
== n
->tok
||
342 return(man_nerr(m
, n
, WROFFNEST
));