]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.74 2011/09/04 09:58:40 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>
36 #include "libmandoc.h"
38 #define CHKARGS struct man *m, struct man_node *n
40 typedef int (*v_check
)(CHKARGS
);
47 static int check_bline(CHKARGS
);
48 static int check_eq0(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
);
56 static int post_AT(CHKARGS
);
57 static int post_vs(CHKARGS
);
58 static int post_fi(CHKARGS
);
59 static int post_ft(CHKARGS
);
60 static int post_nf(CHKARGS
);
61 static int post_sec(CHKARGS
);
62 static int post_TH(CHKARGS
);
63 static int post_UC(CHKARGS
);
64 static int pre_sec(CHKARGS
);
66 static v_check posts_at
[] = { post_AT
, NULL
};
67 static v_check posts_br
[] = { post_vs
, check_eq0
, NULL
};
68 static v_check posts_eq0
[] = { check_eq0
, NULL
};
69 static v_check posts_fi
[] = { check_eq0
, post_fi
, NULL
};
70 static v_check posts_ft
[] = { post_ft
, NULL
};
71 static v_check posts_nf
[] = { check_eq0
, post_nf
, NULL
};
72 static v_check posts_par
[] = { check_par
, NULL
};
73 static v_check posts_part
[] = { check_part
, NULL
};
74 static v_check posts_sec
[] = { post_sec
, NULL
};
75 static v_check posts_sp
[] = { post_vs
, check_le1
, NULL
};
76 static v_check posts_th
[] = { check_ge2
, check_le5
, post_TH
, NULL
};
77 static v_check posts_uc
[] = { post_UC
, NULL
};
78 static v_check pres_bline
[] = { check_bline
, NULL
};
79 static v_check pres_sec
[] = { check_bline
, pre_sec
, NULL
};
81 static const struct man_valid man_valids
[MAN_MAX
] = {
82 { NULL
, posts_br
}, /* br */
83 { pres_bline
, posts_th
}, /* TH */
84 { pres_sec
, posts_sec
}, /* SH */
85 { pres_sec
, posts_sec
}, /* SS */
86 { pres_bline
, NULL
}, /* TP */
87 { pres_bline
, posts_par
}, /* LP */
88 { pres_bline
, posts_par
}, /* PP */
89 { pres_bline
, posts_par
}, /* P */
90 { pres_bline
, NULL
}, /* IP */
91 { pres_bline
, NULL
}, /* HP */
92 { NULL
, NULL
}, /* SM */
93 { NULL
, NULL
}, /* SB */
94 { NULL
, NULL
}, /* BI */
95 { NULL
, NULL
}, /* IB */
96 { NULL
, NULL
}, /* BR */
97 { NULL
, NULL
}, /* RB */
98 { NULL
, NULL
}, /* R */
99 { NULL
, NULL
}, /* B */
100 { NULL
, NULL
}, /* I */
101 { NULL
, NULL
}, /* IR */
102 { NULL
, NULL
}, /* RI */
103 { NULL
, posts_eq0
}, /* na */ /* FIXME: should warn only. */
104 { NULL
, posts_sp
}, /* sp */ /* FIXME: should warn only. */
105 { pres_bline
, posts_nf
}, /* nf */
106 { pres_bline
, posts_fi
}, /* fi */
107 { NULL
, NULL
}, /* RE */
108 { NULL
, posts_part
}, /* RS */
109 { NULL
, NULL
}, /* DT */
110 { NULL
, posts_uc
}, /* UC */
111 { NULL
, NULL
}, /* PD */
112 { NULL
, posts_at
}, /* AT */
113 { NULL
, NULL
}, /* in */
114 { NULL
, posts_ft
}, /* ft */
119 man_valid_pre(struct man
*m
, struct man_node
*n
)
136 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
146 man_valid_post(struct man
*m
)
150 if (MAN_VALID
& m
->last
->flags
)
152 m
->last
->flags
|= MAN_VALID
;
154 switch (m
->last
->type
) {
156 return(check_root(m
, m
->last
));
167 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
170 if ( ! (*cp
)(m
, m
->last
))
181 if (MAN_BLINE
& m
->flags
)
182 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
183 else if (MAN_ELINE
& m
->flags
)
184 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
186 m
->flags
&= ~MAN_BLINE
;
187 m
->flags
&= ~MAN_ELINE
;
189 if (NULL
== m
->first
->child
) {
190 man_nmsg(m
, n
, MANDOCERR_NODOCBODY
);
192 } else if (NULL
== m
->meta
.title
) {
193 man_nmsg(m
, n
, MANDOCERR_NOTITLE
);
196 * If a title hasn't been set, do so now (by
197 * implication, date and section also aren't set).
200 m
->meta
.title
= mandoc_strdup("unknown");
201 m
->meta
.msec
= mandoc_strdup("1");
202 m
->meta
.date
= mandoc_normdate
203 (m
->parse
, NULL
, n
->line
, n
->pos
);
209 #define INEQ_DEFINE(x, ineq, name) \
211 check_##name(CHKARGS) \
213 if (n->nchild ineq (x)) \
215 mandoc_vmsg(MANDOCERR_ARGCOUNT, m->parse, n->line, n->pos, \
216 "line arguments %s %d (have %d)", \
217 #ineq, (x), n->nchild); \
221 INEQ_DEFINE(0, ==, eq0
)
222 INEQ_DEFINE(1, <=, le1
)
223 INEQ_DEFINE(2, >=, ge2
)
224 INEQ_DEFINE(5, <=, le5
)
236 cp
= n
->child
->string
;
255 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
259 if ('W' == cp
[1] && '\0' == cp
[2])
268 (MANDOCERR_BADFONT
, m
->parse
,
269 n
->line
, n
->pos
, "%s", cp
);
275 (MANDOCERR_ARGCOUNT
, m
->parse
, n
->line
,
276 n
->pos
, "want one child (have %d)",
286 if (MAN_BLOCK
== n
->type
)
287 m
->flags
&= ~MAN_LITERAL
;
295 if ( ! (MAN_HEAD
== n
->type
&& 0 == n
->nchild
))
298 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
306 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
307 mandoc_msg(MANDOCERR_ARGCWARN
, m
->parse
, n
->line
,
308 n
->pos
, "want children (have none)");
320 if (0 == n
->body
->nchild
)
321 man_node_delete(m
, n
);
325 man_nmsg(m
, n
, MANDOCERR_IGNPAR
);
329 man_nmsg(m
, n
, MANDOCERR_ARGSLOST
);
343 assert( ! (MAN_ELINE
& m
->flags
));
344 if (MAN_BLINE
& m
->flags
) {
345 man_nmsg(m
, n
, MANDOCERR_SYNTLINESCOPE
);
363 free(m
->meta
.source
);
371 m
->meta
.title
= m
->meta
.vol
= m
->meta
.date
=
372 m
->meta
.msec
= m
->meta
.source
= NULL
;
374 /* ->TITLE<- MSEC DATE SOURCE VOL */
377 if (n
&& n
->string
) {
378 for (p
= n
->string
; '\0' != *p
; p
++) {
379 /* Only warn about this once... */
380 if (isalpha((unsigned char)*p
) &&
381 ! isupper((unsigned char)*p
)) {
382 man_nmsg(m
, n
, MANDOCERR_UPPERCASE
);
386 m
->meta
.title
= mandoc_strdup(n
->string
);
388 m
->meta
.title
= mandoc_strdup("");
390 /* TITLE ->MSEC<- DATE SOURCE VOL */
395 m
->meta
.msec
= mandoc_strdup(n
->string
);
397 m
->meta
.msec
= mandoc_strdup("");
399 /* TITLE MSEC ->DATE<- SOURCE VOL */
405 m
->meta
.date
= mandoc_normdate
406 (m
->parse
, n
? n
->string
: NULL
, line
, pos
);
408 /* TITLE MSEC DATE ->SOURCE<- VOL */
410 if (n
&& (n
= n
->next
))
411 m
->meta
.source
= mandoc_strdup(n
->string
);
413 /* TITLE MSEC DATE SOURCE ->VOL<- */
415 if (n
&& (n
= n
->next
))
416 m
->meta
.vol
= mandoc_strdup(n
->string
);
419 * Remove the `TH' node after we've processed it for our
422 man_node_delete(m
, m
->last
);
430 if (MAN_LITERAL
& m
->flags
)
431 man_nmsg(m
, n
, MANDOCERR_SCOPEREP
);
433 m
->flags
|= MAN_LITERAL
;
441 if ( ! (MAN_LITERAL
& m
->flags
))
442 man_nmsg(m
, n
, MANDOCERR_WNOSCOPE
);
444 m
->flags
&= ~MAN_LITERAL
;
451 static const char * const bsd_versions
[] = {
452 "3rd Berkeley Distribution",
453 "4th Berkeley Distribution",
454 "4.2 Berkeley Distribution",
455 "4.3 Berkeley Distribution",
456 "4.4 Berkeley Distribution",
464 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
468 if (0 == strcmp(s
, "3"))
470 else if (0 == strcmp(s
, "4"))
472 else if (0 == strcmp(s
, "5"))
474 else if (0 == strcmp(s
, "6"))
476 else if (0 == strcmp(s
, "7"))
483 free(m
->meta
.source
);
485 m
->meta
.source
= mandoc_strdup(p
);
492 static const char * const unix_versions
[] = {
496 "System V Release 2",
504 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
505 p
= unix_versions
[0];
508 if (0 == strcmp(s
, "3"))
509 p
= unix_versions
[0];
510 else if (0 == strcmp(s
, "4"))
511 p
= unix_versions
[1];
512 else if (0 == strcmp(s
, "5")) {
514 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
515 p
= unix_versions
[3];
517 p
= unix_versions
[2];
519 p
= unix_versions
[0];
523 free(m
->meta
.source
);
525 m
->meta
.source
= mandoc_strdup(p
);
534 * Don't warn about this because it occurs in pod2man and would
535 * cause considerable (unfixable) warnage.
537 if (NULL
== n
->prev
&& MAN_ROOT
== n
->parent
->type
)
538 man_node_delete(m
, n
);