]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.84 2012/07/29 12:35:42 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012 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>
36 #include "libmandoc.h"
38 #define CHKARGS struct man *m, struct man_node *n
40 typedef int (*v_check
)(CHKARGS
);
47 static int check_eq0(CHKARGS
);
48 static int check_eq2(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 void check_text(CHKARGS
);
57 static int post_AT(CHKARGS
);
58 static int post_IP(CHKARGS
);
59 static int post_vs(CHKARGS
);
60 static int post_fi(CHKARGS
);
61 static int post_ft(CHKARGS
);
62 static int post_nf(CHKARGS
);
63 static int post_sec(CHKARGS
);
64 static int post_TH(CHKARGS
);
65 static int post_UC(CHKARGS
);
66 static int pre_sec(CHKARGS
);
68 static v_check posts_at
[] = { post_AT
, NULL
};
69 static v_check posts_br
[] = { post_vs
, check_eq0
, NULL
};
70 static v_check posts_eq0
[] = { check_eq0
, NULL
};
71 static v_check posts_eq2
[] = { check_eq2
, NULL
};
72 static v_check posts_fi
[] = { check_eq0
, post_fi
, NULL
};
73 static v_check posts_ft
[] = { post_ft
, NULL
};
74 static v_check posts_ip
[] = { post_IP
, NULL
};
75 static v_check posts_le1
[] = { check_le1
, NULL
};
76 static v_check posts_nf
[] = { check_eq0
, post_nf
, NULL
};
77 static v_check posts_par
[] = { check_par
, NULL
};
78 static v_check posts_part
[] = { check_part
, NULL
};
79 static v_check posts_sec
[] = { post_sec
, NULL
};
80 static v_check posts_sp
[] = { post_vs
, check_le1
, NULL
};
81 static v_check posts_th
[] = { check_ge2
, check_le5
, post_TH
, NULL
};
82 static v_check posts_uc
[] = { post_UC
, NULL
};
83 static v_check pres_sec
[] = { pre_sec
, NULL
};
85 static const struct man_valid man_valids
[MAN_MAX
] = {
86 { NULL
, posts_br
}, /* br */
87 { NULL
, posts_th
}, /* TH */
88 { pres_sec
, posts_sec
}, /* SH */
89 { pres_sec
, posts_sec
}, /* SS */
90 { NULL
, NULL
}, /* TP */
91 { NULL
, posts_par
}, /* LP */
92 { NULL
, posts_par
}, /* PP */
93 { NULL
, posts_par
}, /* P */
94 { NULL
, posts_ip
}, /* IP */
95 { NULL
, NULL
}, /* HP */
96 { NULL
, NULL
}, /* SM */
97 { NULL
, NULL
}, /* SB */
98 { NULL
, NULL
}, /* BI */
99 { NULL
, NULL
}, /* IB */
100 { NULL
, NULL
}, /* BR */
101 { NULL
, NULL
}, /* RB */
102 { NULL
, NULL
}, /* R */
103 { NULL
, NULL
}, /* B */
104 { NULL
, NULL
}, /* I */
105 { NULL
, NULL
}, /* IR */
106 { NULL
, NULL
}, /* RI */
107 { NULL
, posts_eq0
}, /* na */
108 { NULL
, posts_sp
}, /* sp */
109 { NULL
, posts_nf
}, /* nf */
110 { NULL
, posts_fi
}, /* fi */
111 { NULL
, NULL
}, /* RE */
112 { NULL
, posts_part
}, /* RS */
113 { NULL
, NULL
}, /* DT */
114 { NULL
, posts_uc
}, /* UC */
115 { NULL
, posts_le1
}, /* PD */
116 { NULL
, posts_at
}, /* AT */
117 { NULL
, NULL
}, /* in */
118 { NULL
, posts_ft
}, /* ft */
119 { NULL
, posts_eq2
}, /* OP */
120 { NULL
, posts_nf
}, /* EX */
121 { NULL
, posts_fi
}, /* EE */
126 man_valid_pre(struct man
*m
, struct man_node
*n
)
143 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
153 man_valid_post(struct man
*m
)
157 if (MAN_VALID
& m
->last
->flags
)
159 m
->last
->flags
|= MAN_VALID
;
161 switch (m
->last
->type
) {
163 check_text(m
, m
->last
);
166 return(check_root(m
, m
->last
));
175 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
178 if ( ! (*cp
)(m
, m
->last
))
189 if (MAN_BLINE
& m
->flags
)
190 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
191 else if (MAN_ELINE
& m
->flags
)
192 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
194 m
->flags
&= ~MAN_BLINE
;
195 m
->flags
&= ~MAN_ELINE
;
197 if (NULL
== m
->first
->child
) {
198 man_nmsg(m
, n
, MANDOCERR_NODOCBODY
);
200 } else if (NULL
== m
->meta
.title
) {
201 man_nmsg(m
, n
, MANDOCERR_NOTITLE
);
204 * If a title hasn't been set, do so now (by
205 * implication, date and section also aren't set).
208 m
->meta
.title
= mandoc_strdup("unknown");
209 m
->meta
.msec
= mandoc_strdup("1");
210 m
->meta
.date
= mandoc_normdate
211 (m
->parse
, NULL
, n
->line
, n
->pos
);
222 if (MAN_LITERAL
& m
->flags
)
226 for (p
= cp
; NULL
!= (p
= strchr(p
, '\t')); p
++)
227 man_pmsg(m
, n
->line
, (int)(p
- cp
), MANDOCERR_BADTAB
);
230 #define INEQ_DEFINE(x, ineq, name) \
232 check_##name(CHKARGS) \
234 if (n->nchild ineq (x)) \
236 mandoc_vmsg(MANDOCERR_ARGCOUNT, m->parse, n->line, n->pos, \
237 "line arguments %s %d (have %d)", \
238 #ineq, (x), n->nchild); \
242 INEQ_DEFINE(0, ==, eq0
)
243 INEQ_DEFINE(2, ==, eq2
)
244 INEQ_DEFINE(1, <=, le1
)
245 INEQ_DEFINE(2, >=, ge2
)
246 INEQ_DEFINE(5, <=, le5
)
258 cp
= n
->child
->string
;
277 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
281 if ('W' == cp
[1] && '\0' == cp
[2])
290 (MANDOCERR_BADFONT
, m
->parse
,
291 n
->line
, n
->pos
, "%s", cp
);
297 (MANDOCERR_ARGCOUNT
, m
->parse
, n
->line
,
298 n
->pos
, "want one child (have %d)",
308 if (MAN_BLOCK
== n
->type
)
309 m
->flags
&= ~MAN_LITERAL
;
317 if ( ! (MAN_HEAD
== n
->type
&& 0 == n
->nchild
))
320 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
328 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
329 mandoc_msg(MANDOCERR_ARGCWARN
, m
->parse
, n
->line
,
330 n
->pos
, "want children (have none)");
342 if (0 == n
->body
->nchild
)
343 man_node_delete(m
, n
);
347 man_nmsg(m
, n
, MANDOCERR_IGNPAR
);
351 man_nmsg(m
, n
, MANDOCERR_ARGSLOST
);
366 if (0 == n
->head
->nchild
&& 0 == n
->body
->nchild
)
367 man_node_delete(m
, n
);
370 if (0 == n
->parent
->head
->nchild
&& 0 == n
->nchild
)
371 man_nmsg(m
, n
, MANDOCERR_IGNPAR
);
390 free(m
->meta
.source
);
398 m
->meta
.title
= m
->meta
.vol
= m
->meta
.date
=
399 m
->meta
.msec
= m
->meta
.source
= NULL
;
401 /* ->TITLE<- MSEC DATE SOURCE VOL */
404 if (n
&& n
->string
) {
405 for (p
= n
->string
; '\0' != *p
; p
++) {
406 /* Only warn about this once... */
407 if (isalpha((unsigned char)*p
) &&
408 ! isupper((unsigned char)*p
)) {
409 man_nmsg(m
, n
, MANDOCERR_UPPERCASE
);
413 m
->meta
.title
= mandoc_strdup(n
->string
);
415 m
->meta
.title
= mandoc_strdup("");
417 /* TITLE ->MSEC<- DATE SOURCE VOL */
422 m
->meta
.msec
= mandoc_strdup(n
->string
);
424 m
->meta
.msec
= mandoc_strdup("");
426 /* TITLE MSEC ->DATE<- SOURCE VOL */
430 if (n
&& n
->string
&& '\0' != n
->string
[0]) {
432 m
->meta
.date
= mandoc_normdate
433 (m
->parse
, n
->string
, line
, pos
);
435 m
->meta
.date
= mandoc_strdup("");
437 /* TITLE MSEC DATE ->SOURCE<- VOL */
439 if (n
&& (n
= n
->next
))
440 m
->meta
.source
= mandoc_strdup(n
->string
);
442 /* TITLE MSEC DATE SOURCE ->VOL<- */
443 /* If missing, use the default VOL name for MSEC. */
445 if (n
&& (n
= n
->next
))
446 m
->meta
.vol
= mandoc_strdup(n
->string
);
447 else if ('\0' != m
->meta
.msec
[0] &&
448 (NULL
!= (p
= mandoc_a2msec(m
->meta
.msec
))))
449 m
->meta
.vol
= mandoc_strdup(p
);
452 * Remove the `TH' node after we've processed it for our
455 man_node_delete(m
, m
->last
);
463 if (MAN_LITERAL
& m
->flags
)
464 man_nmsg(m
, n
, MANDOCERR_SCOPEREP
);
466 m
->flags
|= MAN_LITERAL
;
474 if ( ! (MAN_LITERAL
& m
->flags
))
475 man_nmsg(m
, n
, MANDOCERR_WNOSCOPE
);
477 m
->flags
&= ~MAN_LITERAL
;
484 static const char * const bsd_versions
[] = {
485 "3rd Berkeley Distribution",
486 "4th Berkeley Distribution",
487 "4.2 Berkeley Distribution",
488 "4.3 Berkeley Distribution",
489 "4.4 Berkeley Distribution",
496 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
500 if (0 == strcmp(s
, "3"))
502 else if (0 == strcmp(s
, "4"))
504 else if (0 == strcmp(s
, "5"))
506 else if (0 == strcmp(s
, "6"))
508 else if (0 == strcmp(s
, "7"))
515 free(m
->meta
.source
);
517 m
->meta
.source
= mandoc_strdup(p
);
524 static const char * const unix_versions
[] = {
528 "System V Release 2",
536 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
537 p
= unix_versions
[0];
540 if (0 == strcmp(s
, "3"))
541 p
= unix_versions
[0];
542 else if (0 == strcmp(s
, "4"))
543 p
= unix_versions
[1];
544 else if (0 == strcmp(s
, "5")) {
546 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
547 p
= unix_versions
[3];
549 p
= unix_versions
[2];
551 p
= unix_versions
[0];
555 free(m
->meta
.source
);
557 m
->meta
.source
= mandoc_strdup(p
);
568 switch (n
->parent
->tok
) {
572 man_nmsg(m
, n
, MANDOCERR_IGNPAR
);
576 * Don't warn about this because it occurs in pod2man
577 * and would cause considerable (unfixable) warnage.
579 man_node_delete(m
, n
);