]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.62 2011/02/09 09:18:15 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
);
57 static int post_AT(CHKARGS
);
58 static int post_fi(CHKARGS
);
59 static int post_nf(CHKARGS
);
60 static int post_TH(CHKARGS
);
61 static int post_UC(CHKARGS
);
63 static v_check posts_at
[] = { post_AT
, NULL
};
64 static v_check posts_eq0
[] = { check_eq0
, NULL
};
65 static v_check posts_fi
[] = { check_eq0
, post_fi
, NULL
};
66 static v_check posts_le1
[] = { check_le1
, NULL
};
67 static v_check posts_ft
[] = { check_ft
, NULL
};
68 static v_check posts_nf
[] = { check_eq0
, post_nf
, NULL
};
69 static v_check posts_par
[] = { check_par
, NULL
};
70 static v_check posts_part
[] = { check_part
, NULL
};
71 static v_check posts_sec
[] = { check_sec
, NULL
};
72 static v_check posts_th
[] = { check_ge2
, check_le5
, post_TH
, NULL
};
73 static v_check posts_uc
[] = { post_UC
, NULL
};
74 static v_check pres_bline
[] = { check_bline
, NULL
};
77 static const struct man_valid man_valids
[MAN_MAX
] = {
78 { NULL
, posts_eq0
}, /* br */
79 { pres_bline
, posts_th
}, /* TH */
80 { pres_bline
, posts_sec
}, /* SH */
81 { pres_bline
, posts_sec
}, /* SS */
82 { pres_bline
, NULL
}, /* TP */
83 { pres_bline
, posts_par
}, /* LP */
84 { pres_bline
, posts_par
}, /* PP */
85 { pres_bline
, posts_par
}, /* P */
86 { pres_bline
, NULL
}, /* IP */
87 { pres_bline
, NULL
}, /* HP */
88 { NULL
, NULL
}, /* SM */
89 { NULL
, NULL
}, /* SB */
90 { NULL
, NULL
}, /* BI */
91 { NULL
, NULL
}, /* IB */
92 { NULL
, NULL
}, /* BR */
93 { NULL
, NULL
}, /* RB */
94 { NULL
, NULL
}, /* R */
95 { NULL
, NULL
}, /* B */
96 { NULL
, NULL
}, /* I */
97 { NULL
, NULL
}, /* IR */
98 { NULL
, NULL
}, /* RI */
99 { NULL
, posts_eq0
}, /* na */ /* FIXME: should warn only. */
100 { NULL
, posts_le1
}, /* sp */ /* FIXME: should warn only. */
101 { pres_bline
, posts_nf
}, /* nf */
102 { pres_bline
, posts_fi
}, /* fi */
103 { NULL
, NULL
}, /* RE */
104 { NULL
, posts_part
}, /* RS */
105 { NULL
, NULL
}, /* DT */
106 { NULL
, posts_uc
}, /* UC */
107 { NULL
, NULL
}, /* PD */
108 { NULL
, posts_at
}, /* AT */
109 { NULL
, NULL
}, /* in */
110 { NULL
, posts_ft
}, /* ft */
115 man_valid_pre(struct man
*m
, struct man_node
*n
)
132 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
142 man_valid_post(struct man
*m
)
146 if (MAN_VALID
& m
->last
->flags
)
148 m
->last
->flags
|= MAN_VALID
;
150 switch (m
->last
->type
) {
152 return(check_text(m
, m
->last
));
154 return(check_root(m
, m
->last
));
163 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
166 if ( ! (*cp
)(m
, m
->last
))
177 if (MAN_BLINE
& m
->flags
)
178 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
179 else if (MAN_ELINE
& m
->flags
)
180 man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
);
182 m
->flags
&= ~MAN_BLINE
;
183 m
->flags
&= ~MAN_ELINE
;
185 if (NULL
== m
->first
->child
) {
186 man_nmsg(m
, n
, MANDOCERR_NODOCBODY
);
188 } else if (NULL
== m
->meta
.title
) {
189 man_nmsg(m
, n
, MANDOCERR_NOTITLE
);
192 * If a title hasn't been set, do so now (by
193 * implication, date and section also aren't set).
196 m
->meta
.title
= mandoc_strdup("unknown");
197 m
->meta
.date
= time(NULL
);
198 m
->meta
.msec
= mandoc_strdup("1");
212 for (p
= n
->string
, pos
= n
->pos
+ 1; *p
; p
++, pos
++) {
213 sz
= strcspn(p
, "\t\\");
222 if (MAN_LITERAL
& m
->flags
)
224 if (man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADTAB
))
229 /* Check the special character. */
231 c
= mandoc_special(p
);
236 man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADESCAPE
);
243 #define INEQ_DEFINE(x, ineq, name) \
245 check_##name(CHKARGS) \
247 if (n->nchild ineq (x)) \
249 man_vmsg(m, MANDOCERR_ARGCOUNT, n->line, n->pos, \
250 "line arguments %s %d (have %d)", \
251 #ineq, (x), n->nchild); \
255 INEQ_DEFINE(0, ==, eq0
)
256 INEQ_DEFINE(1, <=, le1
)
257 INEQ_DEFINE(2, >=, ge2
)
258 INEQ_DEFINE(5, <=, le5
)
270 cp
= n
->child
->string
;
289 if ('\0' == cp
[1] || ('I' == cp
[1] && '\0' == cp
[2]))
293 if ('W' == cp
[1] && '\0' == cp
[2])
301 man_vmsg(m
, MANDOCERR_BADFONT
,
302 n
->line
, n
->pos
, "%s", cp
);
307 man_vmsg(m
, MANDOCERR_ARGCOUNT
, n
->line
, n
->pos
,
308 "want one child (have %d)", n
->nchild
);
317 if (MAN_HEAD
== n
->type
&& 0 == n
->nchild
) {
318 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
320 } else if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
321 man_nmsg(m
, n
, MANDOCERR_NOBODY
);
331 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
332 man_nmsg(m
, n
, MANDOCERR_NOBODY
);
344 if (0 == n
->body
->nchild
)
345 man_node_delete(m
, n
);
349 man_nmsg(m
, n
, MANDOCERR_IGNPAR
);
353 man_nmsg(m
, n
, MANDOCERR_ARGSLOST
);
367 assert( ! (MAN_ELINE
& m
->flags
));
368 if (MAN_BLINE
& m
->flags
) {
369 man_nmsg(m
, n
, MANDOCERR_SYNTLINESCOPE
);
386 free(m
->meta
.source
);
390 free(m
->meta
.rawdate
);
392 m
->meta
.title
= m
->meta
.vol
= m
->meta
.rawdate
=
393 m
->meta
.msec
= m
->meta
.source
= NULL
;
396 /* ->TITLE<- MSEC DATE SOURCE VOL */
399 if (n
&& n
->string
) {
400 for (p
= n
->string
; '\0' != *p
; p
++) {
401 /* Only warn about this once... */
402 if (isalpha((u_char
)*p
) && ! isupper((u_char
)*p
)) {
403 man_nmsg(m
, n
, MANDOCERR_UPPERCASE
);
407 m
->meta
.title
= mandoc_strdup(n
->string
);
409 m
->meta
.title
= mandoc_strdup("");
411 /* TITLE ->MSEC<- DATE SOURCE VOL */
416 m
->meta
.msec
= mandoc_strdup(n
->string
);
418 m
->meta
.msec
= mandoc_strdup("");
420 /* TITLE MSEC ->DATE<- SOURCE VOL */
423 * Try to parse the date. If this works, stash the epoch (this
424 * is optimal because we can reformat it in the canonical form).
425 * If it doesn't parse, isn't specified at all, or is an empty
426 * string, then use the current date.
431 if (n
&& n
->string
&& *n
->string
) {
432 m
->meta
.date
= mandoc_a2time
433 (MTIME_ISO_8601
, n
->string
);
434 if (0 == m
->meta
.date
) {
435 man_nmsg(m
, n
, MANDOCERR_BADDATE
);
436 m
->meta
.rawdate
= mandoc_strdup(n
->string
);
439 m
->meta
.date
= time(NULL
);
441 /* TITLE MSEC DATE ->SOURCE<- VOL */
443 if (n
&& (n
= n
->next
))
444 m
->meta
.source
= mandoc_strdup(n
->string
);
446 /* TITLE MSEC DATE SOURCE ->VOL<- */
448 if (n
&& (n
= n
->next
))
449 m
->meta
.vol
= mandoc_strdup(n
->string
);
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",
497 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
501 if (0 == strcmp(s
, "3"))
503 else if (0 == strcmp(s
, "4"))
505 else if (0 == strcmp(s
, "5"))
507 else if (0 == strcmp(s
, "6"))
509 else if (0 == strcmp(s
, "7"))
516 free(m
->meta
.source
);
518 m
->meta
.source
= mandoc_strdup(p
);
525 static const char * const unix_versions
[] = {
529 "System V Release 2",
537 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
538 p
= unix_versions
[0];
541 if (0 == strcmp(s
, "3"))
542 p
= unix_versions
[0];
543 else if (0 == strcmp(s
, "4"))
544 p
= unix_versions
[1];
545 else if (0 == strcmp(s
, "5")) {
547 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
548 p
= unix_versions
[3];
550 p
= unix_versions
[2];
552 p
= unix_versions
[0];
556 free(m
->meta
.source
);
558 m
->meta
.source
= mandoc_strdup(p
);