]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.64 2011/03/17 11:56:17 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
35 #include "libmandoc.h"
37 #define CHKARGS struct man *m, struct man_node *n
39 typedef int (*v_check
)(CHKARGS
);
46 static int check_bline(CHKARGS
);
47 static int check_eq0(CHKARGS
);
48 static int check_ft(CHKARGS
);
49 static int check_le1(CHKARGS
);
50 static int check_ge2(CHKARGS
);
51 static int check_le5(CHKARGS
);
52 static int check_par(CHKARGS
);
53 static int check_part(CHKARGS
);
54 static int check_root(CHKARGS
);
55 static int check_sec(CHKARGS
);
56 static int check_text(CHKARGS
);
58 static int post_AT(CHKARGS
);
59 static int post_fi(CHKARGS
);
60 static int post_nf(CHKARGS
);
61 static int post_TH(CHKARGS
);
62 static int post_UC(CHKARGS
);
64 static v_check posts_at
[] = { post_AT
, NULL
};
65 static v_check posts_eq0
[] = { check_eq0
, NULL
};
66 static v_check posts_fi
[] = { check_eq0
, post_fi
, NULL
};
67 static v_check posts_le1
[] = { check_le1
, NULL
};
68 static v_check posts_ft
[] = { check_ft
, NULL
};
69 static v_check posts_nf
[] = { check_eq0
, post_nf
, NULL
};
70 static v_check posts_par
[] = { check_par
, NULL
};
71 static v_check posts_part
[] = { check_part
, NULL
};
72 static v_check posts_sec
[] = { check_sec
, NULL
};
73 static v_check posts_th
[] = { check_ge2
, check_le5
, post_TH
, NULL
};
74 static v_check posts_uc
[] = { post_UC
, NULL
};
75 static v_check pres_bline
[] = { check_bline
, NULL
};
78 static const struct man_valid man_valids
[MAN_MAX
] = {
79 { NULL
, posts_eq0
}, /* br */
80 { pres_bline
, posts_th
}, /* TH */
81 { pres_bline
, posts_sec
}, /* SH */
82 { pres_bline
, posts_sec
}, /* SS */
83 { pres_bline
, NULL
}, /* TP */
84 { pres_bline
, posts_par
}, /* LP */
85 { pres_bline
, posts_par
}, /* PP */
86 { pres_bline
, posts_par
}, /* P */
87 { pres_bline
, NULL
}, /* IP */
88 { pres_bline
, NULL
}, /* HP */
89 { NULL
, NULL
}, /* SM */
90 { NULL
, NULL
}, /* SB */
91 { NULL
, NULL
}, /* BI */
92 { NULL
, NULL
}, /* IB */
93 { NULL
, NULL
}, /* BR */
94 { NULL
, NULL
}, /* RB */
95 { NULL
, NULL
}, /* R */
96 { NULL
, NULL
}, /* B */
97 { NULL
, NULL
}, /* I */
98 { NULL
, NULL
}, /* IR */
99 { NULL
, NULL
}, /* RI */
100 { NULL
, posts_eq0
}, /* na */ /* FIXME: should warn only. */
101 { NULL
, posts_le1
}, /* sp */ /* FIXME: should warn only. */
102 { pres_bline
, posts_nf
}, /* nf */
103 { pres_bline
, posts_fi
}, /* fi */
104 { NULL
, NULL
}, /* RE */
105 { NULL
, posts_part
}, /* RS */
106 { NULL
, NULL
}, /* DT */
107 { NULL
, posts_uc
}, /* UC */
108 { NULL
, NULL
}, /* PD */
109 { NULL
, posts_at
}, /* AT */
110 { NULL
, NULL
}, /* in */
111 { NULL
, posts_ft
}, /* ft */
116 man_valid_pre(struct man
*m
, struct man_node
*n
)
133 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
143 man_valid_post(struct man
*m
)
147 if (MAN_VALID
& m
->last
->flags
)
149 m
->last
->flags
|= MAN_VALID
;
151 switch (m
->last
->type
) {
153 return(check_text(m
, m
->last
));
155 return(check_root(m
, m
->last
));
164 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
167 if ( ! (*cp
)(m
, m
->last
))
178 if (MAN_BLINE
& m
->flags
)
179 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
180 else if (MAN_ELINE
& m
->flags
)
181 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
183 m
->flags
&= ~MAN_BLINE
;
184 m
->flags
&= ~MAN_ELINE
;
186 if (NULL
== m
->first
->child
) {
187 man_nmsg(m
, n
, MANDOCERR_NODOCBODY
);
189 } else if (NULL
== m
->meta
.title
) {
190 man_nmsg(m
, n
, MANDOCERR_NOTITLE
);
193 * If a title hasn't been set, do so now (by
194 * implication, date and section also aren't set).
197 m
->meta
.title
= mandoc_strdup("unknown");
198 m
->meta
.msec
= mandoc_strdup("1");
199 m
->meta
.date
= mandoc_normdate(NULL
,
200 m
->msg
, m
->data
, n
->line
, n
->pos
);
214 for (p
= n
->string
, pos
= n
->pos
+ 1; *p
; p
++, pos
++) {
215 sz
= strcspn(p
, "\t\\");
224 if (MAN_LITERAL
& m
->flags
)
226 man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADTAB
);
230 /* Check the special character. */
232 c
= mandoc_special(p
);
237 man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADESCAPE
);
244 #define INEQ_DEFINE(x, ineq, name) \
246 check_##name(CHKARGS) \
248 if (n->nchild ineq (x)) \
250 man_vmsg(m, MANDOCERR_ARGCOUNT, n->line, n->pos, \
251 "line arguments %s %d (have %d)", \
252 #ineq, (x), n->nchild); \
256 INEQ_DEFINE(0, ==, eq0
)
257 INEQ_DEFINE(1, <=, le1
)
258 INEQ_DEFINE(2, >=, ge2
)
259 INEQ_DEFINE(5, <=, le5
)
271 cp
= n
->child
->string
;
290 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
294 if ('W' == cp
[1] && '\0' == cp
[2])
302 man_vmsg(m
, MANDOCERR_BADFONT
,
303 n
->line
, n
->pos
, "%s", cp
);
308 man_vmsg(m
, MANDOCERR_ARGCOUNT
, n
->line
, n
->pos
,
309 "want one child (have %d)", n
->nchild
);
318 if (MAN_HEAD
== n
->type
&& 0 == n
->nchild
) {
319 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
321 } else if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
322 man_nmsg(m
, n
, MANDOCERR_NOBODY
);
332 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
333 man_nmsg(m
, n
, MANDOCERR_NOBODY
);
345 if (0 == n
->body
->nchild
)
346 man_node_delete(m
, n
);
350 man_nmsg(m
, n
, MANDOCERR_IGNPAR
);
354 man_nmsg(m
, n
, MANDOCERR_ARGSLOST
);
368 assert( ! (MAN_ELINE
& m
->flags
));
369 if (MAN_BLINE
& m
->flags
) {
370 man_nmsg(m
, n
, MANDOCERR_SYNTLINESCOPE
);
388 free(m
->meta
.source
);
396 m
->meta
.title
= m
->meta
.vol
= m
->meta
.date
=
397 m
->meta
.msec
= m
->meta
.source
= NULL
;
399 /* ->TITLE<- MSEC DATE SOURCE VOL */
402 if (n
&& n
->string
) {
403 for (p
= n
->string
; '\0' != *p
; p
++) {
404 /* Only warn about this once... */
405 if (isalpha((u_char
)*p
) && ! isupper((u_char
)*p
)) {
406 man_nmsg(m
, n
, MANDOCERR_UPPERCASE
);
410 m
->meta
.title
= mandoc_strdup(n
->string
);
412 m
->meta
.title
= mandoc_strdup("");
414 /* TITLE ->MSEC<- DATE SOURCE VOL */
419 m
->meta
.msec
= mandoc_strdup(n
->string
);
421 m
->meta
.msec
= mandoc_strdup("");
423 /* TITLE MSEC ->DATE<- SOURCE VOL */
429 m
->meta
.date
= mandoc_normdate(n
? n
->string
: NULL
,
430 m
->msg
, m
->data
, line
, pos
);
432 /* TITLE MSEC DATE ->SOURCE<- VOL */
434 if (n
&& (n
= n
->next
))
435 m
->meta
.source
= mandoc_strdup(n
->string
);
437 /* TITLE MSEC DATE SOURCE ->VOL<- */
439 if (n
&& (n
= n
->next
))
440 m
->meta
.vol
= mandoc_strdup(n
->string
);
443 * Remove the `TH' node after we've processed it for our
446 man_node_delete(m
, m
->last
);
454 if (MAN_LITERAL
& m
->flags
)
455 man_nmsg(m
, n
, MANDOCERR_SCOPEREP
);
457 m
->flags
|= MAN_LITERAL
;
465 if ( ! (MAN_LITERAL
& m
->flags
))
466 man_nmsg(m
, n
, MANDOCERR_WNOSCOPE
);
468 m
->flags
&= ~MAN_LITERAL
;
475 static const char * const bsd_versions
[] = {
476 "3rd Berkeley Distribution",
477 "4th Berkeley Distribution",
478 "4.2 Berkeley Distribution",
479 "4.3 Berkeley Distribution",
480 "4.4 Berkeley Distribution",
488 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
492 if (0 == strcmp(s
, "3"))
494 else if (0 == strcmp(s
, "4"))
496 else if (0 == strcmp(s
, "5"))
498 else if (0 == strcmp(s
, "6"))
500 else if (0 == strcmp(s
, "7"))
507 free(m
->meta
.source
);
509 m
->meta
.source
= mandoc_strdup(p
);
516 static const char * const unix_versions
[] = {
520 "System V Release 2",
528 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
529 p
= unix_versions
[0];
532 if (0 == strcmp(s
, "3"))
533 p
= unix_versions
[0];
534 else if (0 == strcmp(s
, "4"))
535 p
= unix_versions
[1];
536 else if (0 == strcmp(s
, "5")) {
538 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
539 p
= unix_versions
[3];
541 p
= unix_versions
[2];
543 p
= unix_versions
[0];
547 free(m
->meta
.source
);
549 m
->meta
.source
= mandoc_strdup(p
);