]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.56 2010/12/08 10:58:22 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 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>
34 #include "libmandoc.h"
36 #define CHKARGS struct man *m, struct man_node *n
38 typedef int (*v_check
)(CHKARGS
);
45 static int check_bline(CHKARGS
);
46 static int check_eq0(CHKARGS
);
47 static int check_ft(CHKARGS
);
48 static int check_le1(CHKARGS
);
49 static int check_ge2(CHKARGS
);
50 static int check_le5(CHKARGS
);
51 static int check_par(CHKARGS
);
52 static int check_part(CHKARGS
);
53 static int check_root(CHKARGS
);
54 static int check_sec(CHKARGS
);
55 static int check_text(CHKARGS
);
56 static int check_title(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
, check_title
, 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
, posts_par
}, /* TP */
84 { pres_bline
, posts_par
}, /* LP */
85 { pres_bline
, posts_par
}, /* PP */
86 { pres_bline
, posts_par
}, /* P */
87 { pres_bline
, posts_par
}, /* IP */
88 { pres_bline
, posts_par
}, /* 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
)
120 if (MAN_TEXT
== n
->type
)
122 if (MAN_ROOT
== n
->type
)
125 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
135 man_valid_post(struct man
*m
)
139 if (MAN_VALID
& m
->last
->flags
)
141 m
->last
->flags
|= MAN_VALID
;
143 switch (m
->last
->type
) {
145 return(check_text(m
, m
->last
));
147 return(check_root(m
, m
->last
));
152 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
155 if ( ! (*cp
)(m
, m
->last
))
166 if (MAN_BLINE
& m
->flags
)
167 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
168 else if (MAN_ELINE
& m
->flags
)
169 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
171 m
->flags
&= ~MAN_BLINE
;
172 m
->flags
&= ~MAN_ELINE
;
174 if (NULL
== m
->first
->child
) {
175 man_nmsg(m
, n
, MANDOCERR_NODOCBODY
);
177 } else if (NULL
== m
->meta
.title
) {
178 man_nmsg(m
, n
, MANDOCERR_NOTITLE
);
181 * If a title hasn't been set, do so now (by
182 * implication, date and section also aren't set).
185 m
->meta
.title
= mandoc_strdup("unknown");
186 m
->meta
.date
= time(NULL
);
187 m
->meta
.msec
= mandoc_strdup("1");
200 /* FIXME: is this sufficient? */
201 if ('\0' == *n
->child
->string
) {
202 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
206 for (p
= n
->child
->string
; '\0' != *p
; p
++)
207 /* Only warn about this once... */
208 if (isalpha((u_char
)*p
) && ! isupper((u_char
)*p
)) {
209 man_nmsg(m
, n
, MANDOCERR_UPPERCASE
);
224 for (p
= n
->string
, pos
= n
->pos
+ 1; *p
; p
++, pos
++) {
225 sz
= strcspn(p
, "\t\\");
234 if (MAN_LITERAL
& m
->flags
)
236 if (man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADTAB
))
241 /* Check the special character. */
243 c
= mandoc_special(p
);
248 man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADESCAPE
);
255 #define INEQ_DEFINE(x, ineq, name) \
257 check_##name(CHKARGS) \
259 if (n->nchild ineq (x)) \
261 man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
262 "line arguments %s %d (have %d)", \
263 #ineq, (x), n->nchild); \
267 INEQ_DEFINE(0, ==, eq0
)
268 INEQ_DEFINE(1, <=, le1
)
269 INEQ_DEFINE(2, >=, ge2
)
270 INEQ_DEFINE(5, <=, le5
)
282 cp
= n
->child
->string
;
301 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
305 if ('W' == cp
[1] && '\0' == cp
[2])
313 man_vmsg(m
, MANDOCERR_BADFONT
,
314 n
->line
, n
->pos
, "%s", cp
);
319 man_vmsg(m
, MANDOCERR_ARGCOUNT
, n
->line
, n
->pos
,
320 "want one child (have %d)", n
->nchild
);
329 if (MAN_HEAD
== n
->type
&& 0 == n
->nchild
) {
330 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
332 } else if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
333 man_nmsg(m
, n
, MANDOCERR_NOBODY
);
343 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
344 man_nmsg(m
, n
, MANDOCERR_NOBODY
);
354 if (MAN_BODY
== n
->type
)
361 /* Body-less lists are ok. */
365 man_nmsg(m
, n
, MANDOCERR_NOBODY
);
368 if (MAN_HEAD
== n
->type
)
376 man_nmsg(m
, n
, MANDOCERR_ARGSLOST
);
390 assert( ! (MAN_ELINE
& m
->flags
));
391 if (MAN_BLINE
& m
->flags
) {
392 man_nmsg(m
, n
, MANDOCERR_SYNTLINESCOPE
);
408 free(m
->meta
.source
);
412 free(m
->meta
.rawdate
);
414 m
->meta
.title
= m
->meta
.vol
= m
->meta
.rawdate
=
415 m
->meta
.msec
= m
->meta
.source
= NULL
;
418 /* ->TITLE<- MSEC DATE SOURCE VOL */
422 m
->meta
.title
= mandoc_strdup(n
->string
);
424 /* TITLE ->MSEC<- DATE SOURCE VOL */
428 m
->meta
.msec
= mandoc_strdup(n
->string
);
430 /* TITLE MSEC ->DATE<- SOURCE VOL */
433 * Try to parse the date. If this works, stash the epoch (this
434 * is optimal because we can reformat it in the canonical form).
435 * If it doesn't parse, isn't specified at all, or is an empty
436 * string, then use the current date.
440 if (n
&& n
->string
&& *n
->string
) {
441 m
->meta
.date
= mandoc_a2time
442 (MTIME_ISO_8601
, n
->string
);
443 if (0 == m
->meta
.date
) {
444 man_nmsg(m
, n
, MANDOCERR_BADDATE
);
445 m
->meta
.rawdate
= mandoc_strdup(n
->string
);
448 m
->meta
.date
= time(NULL
);
450 /* TITLE MSEC DATE ->SOURCE<- VOL */
452 if (n
&& (n
= n
->next
))
453 m
->meta
.source
= mandoc_strdup(n
->string
);
455 /* TITLE MSEC DATE SOURCE ->VOL<- */
457 if (n
&& (n
= n
->next
))
458 m
->meta
.vol
= mandoc_strdup(n
->string
);
461 * Remove the `TH' node after we've processed it for our
464 man_node_delete(m
, m
->last
);
472 if (MAN_LITERAL
& m
->flags
)
473 man_nmsg(m
, n
, MANDOCERR_SCOPEREP
);
475 m
->flags
|= MAN_LITERAL
;
483 if ( ! (MAN_LITERAL
& m
->flags
))
484 man_nmsg(m
, n
, MANDOCERR_NOSCOPE
);
486 m
->flags
&= ~MAN_LITERAL
;
493 static const char * const bsd_versions
[] = {
494 "3rd Berkeley Distribution",
495 "4th Berkeley Distribution",
496 "4.2 Berkeley Distribution",
497 "4.3 Berkeley Distribution",
498 "4.4 Berkeley Distribution",
506 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
510 if (0 == strcmp(s
, "3"))
512 else if (0 == strcmp(s
, "4"))
514 else if (0 == strcmp(s
, "5"))
516 else if (0 == strcmp(s
, "6"))
518 else if (0 == strcmp(s
, "7"))
525 free(m
->meta
.source
);
527 m
->meta
.source
= mandoc_strdup(p
);
534 static const char * const unix_versions
[] = {
538 "System V Release 2",
546 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
547 p
= unix_versions
[0];
550 if (0 == strcmp(s
, "3"))
551 p
= unix_versions
[0];
552 else if (0 == strcmp(s
, "4"))
553 p
= unix_versions
[1];
554 else if (0 == strcmp(s
, "5")) {
556 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
557 p
= unix_versions
[3];
559 p
= unix_versions
[2];
561 p
= unix_versions
[0];
565 free(m
->meta
.source
);
567 m
->meta
.source
= mandoc_strdup(p
);