]>
git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
1 /* $Id: man_validate.c,v 1.51 2010/11/30 15:24:27 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_le1(CHKARGS
);
48 static int check_ge2(CHKARGS
);
49 static int check_le5(CHKARGS
);
50 static int check_par(CHKARGS
);
51 static int check_part(CHKARGS
);
52 static int check_root(CHKARGS
);
53 static int check_sec(CHKARGS
);
54 static int check_text(CHKARGS
);
55 static int check_title(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_nf
[] = { check_eq0
, post_nf
, NULL
};
68 static v_check posts_par
[] = { check_par
, NULL
};
69 static v_check posts_part
[] = { check_part
, NULL
};
70 static v_check posts_sec
[] = { check_sec
, NULL
};
71 static v_check posts_th
[] = { check_ge2
, check_le5
, check_title
, post_TH
, NULL
};
72 static v_check posts_uc
[] = { post_UC
, NULL
};
73 static v_check posts_vb
[] = { check_le1
, post_nf
, 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
, posts_par
}, /* TP */
83 { pres_bline
, posts_par
}, /* LP */
84 { pres_bline
, posts_par
}, /* PP */
85 { pres_bline
, posts_par
}, /* P */
86 { pres_bline
, posts_par
}, /* IP */
87 { pres_bline
, posts_par
}, /* 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
, NULL
}, /* i */
101 { NULL
, posts_le1
}, /* sp */ /* FIXME: should warn only. */
102 { pres_bline
, posts_nf
}, /* nf */
103 { pres_bline
, posts_fi
}, /* fi */
104 { NULL
, NULL
}, /* r */
105 { NULL
, NULL
}, /* RE */
106 { NULL
, posts_part
}, /* RS */
107 { NULL
, NULL
}, /* DT */
108 { NULL
, posts_uc
}, /* UC */
109 { NULL
, NULL
}, /* PD */
110 { NULL
, posts_le1
}, /* Sp */ /* FIXME: should warn only. */
111 { pres_bline
, posts_vb
}, /* Vb */ /* FIXME: should warn only. */
112 { pres_bline
, posts_fi
}, /* Ve */
113 { NULL
, posts_at
}, /* AT */
114 { NULL
, NULL
}, /* in */
119 man_valid_pre(struct man
*m
, struct man_node
*n
)
123 if (MAN_TEXT
== n
->type
)
125 if (MAN_ROOT
== n
->type
)
128 if (NULL
== (cp
= man_valids
[n
->tok
].pres
))
138 man_valid_post(struct man
*m
)
142 if (MAN_VALID
& m
->last
->flags
)
144 m
->last
->flags
|= MAN_VALID
;
146 switch (m
->last
->type
) {
148 return(check_text(m
, m
->last
));
150 return(check_root(m
, m
->last
));
155 if (NULL
== (cp
= man_valids
[m
->last
->tok
].posts
))
158 if ( ! (*cp
)(m
, m
->last
))
169 if (MAN_BLINE
& m
->flags
)
170 return(man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
));
171 if (MAN_ELINE
& m
->flags
)
172 return(man_nmsg(m
, n
, MANDOCERR_SCOPEEXIT
));
174 m
->flags
&= ~MAN_BLINE
;
175 m
->flags
&= ~MAN_ELINE
;
177 if (NULL
== m
->first
->child
) {
178 man_nmsg(m
, n
, MANDOCERR_NODOCBODY
);
180 } else if (NULL
== m
->meta
.title
) {
181 if ( ! man_nmsg(m
, n
, MANDOCERR_NOTITLE
))
184 * If a title hasn't been set, do so now (by
185 * implication, date and section also aren't set).
187 * FIXME: this should be in man_action.c.
189 m
->meta
.title
= mandoc_strdup("unknown");
190 m
->meta
.date
= time(NULL
);
191 m
->meta
.msec
= mandoc_strdup("1");
204 /* FIXME: is this sufficient? */
205 if ('\0' == *n
->child
->string
) {
206 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
210 for (p
= n
->child
->string
; '\0' != *p
; p
++)
211 if (isalpha((u_char
)*p
) && ! isupper((u_char
)*p
))
212 if ( ! man_nmsg(m
, n
, MANDOCERR_UPPERCASE
))
226 for (p
= n
->string
, pos
= n
->pos
+ 1; *p
; p
++, pos
++) {
227 sz
= strcspn(p
, "\t\\");
236 if (MAN_LITERAL
& m
->flags
)
238 if (man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADTAB
))
243 /* Check the special character. */
245 c
= mandoc_special(p
);
250 man_pmsg(m
, n
->line
, pos
, MANDOCERR_BADESCAPE
);
257 #define INEQ_DEFINE(x, ineq, name) \
259 check_##name(CHKARGS) \
261 if (n->nchild ineq (x)) \
263 man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
264 "line arguments %s %d (have %d)", \
265 #ineq, (x), n->nchild); \
269 INEQ_DEFINE(0, ==, eq0
)
270 INEQ_DEFINE(1, <=, le1
)
271 INEQ_DEFINE(2, >=, ge2
)
272 INEQ_DEFINE(5, <=, le5
)
279 if (MAN_HEAD
== n
->type
&& 0 == n
->nchild
) {
280 man_nmsg(m
, n
, MANDOCERR_SYNTARGCOUNT
);
282 } else if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
283 return(man_nmsg(m
, n
, MANDOCERR_NOBODY
));
293 if (MAN_BODY
== n
->type
&& 0 == n
->nchild
)
294 return(man_nmsg(m
, n
, MANDOCERR_NOBODY
));
303 if (MAN_BODY
== n
->type
)
310 /* Body-less lists are ok. */
315 return(man_nmsg(m
, n
, MANDOCERR_NOBODY
));
317 if (MAN_HEAD
== n
->type
)
326 return(man_nmsg(m
, n
, MANDOCERR_ARGSLOST
));
330 return(man_nmsg(m
, n
, MANDOCERR_NOARGS
));
341 assert( ! (MAN_ELINE
& m
->flags
));
342 if (MAN_BLINE
& m
->flags
) {
343 man_nmsg(m
, n
, MANDOCERR_SYNTLINESCOPE
);
359 free(m
->meta
.source
);
363 free(m
->meta
.rawdate
);
365 m
->meta
.title
= m
->meta
.vol
= m
->meta
.rawdate
=
366 m
->meta
.msec
= m
->meta
.source
= NULL
;
369 /* ->TITLE<- MSEC DATE SOURCE VOL */
373 m
->meta
.title
= mandoc_strdup(n
->string
);
375 /* TITLE ->MSEC<- DATE SOURCE VOL */
379 m
->meta
.msec
= mandoc_strdup(n
->string
);
381 /* TITLE MSEC ->DATE<- SOURCE VOL */
384 * Try to parse the date. If this works, stash the epoch (this
385 * is optimal because we can reformat it in the canonical form).
386 * If it doesn't parse, isn't specified at all, or is an empty
387 * string, then use the current date.
391 if (n
&& n
->string
&& *n
->string
) {
392 m
->meta
.date
= mandoc_a2time
393 (MTIME_ISO_8601
, n
->string
);
394 if (0 == m
->meta
.date
) {
395 man_nmsg(m
, n
, MANDOCERR_BADDATE
);
396 m
->meta
.rawdate
= mandoc_strdup(n
->string
);
399 m
->meta
.date
= time(NULL
);
401 /* TITLE MSEC DATE ->SOURCE<- VOL */
403 if (n
&& (n
= n
->next
))
404 m
->meta
.source
= mandoc_strdup(n
->string
);
406 /* TITLE MSEC DATE SOURCE ->VOL<- */
408 if (n
&& (n
= n
->next
))
409 m
->meta
.vol
= mandoc_strdup(n
->string
);
412 * Remove the `TH' node after we've processed it for our
415 man_node_delete(m
, m
->last
);
423 if (MAN_LITERAL
& m
->flags
)
424 man_nmsg(m
, n
, MANDOCERR_SCOPEREP
);
426 m
->flags
|= MAN_LITERAL
;
434 if ( ! (MAN_LITERAL
& m
->flags
))
435 man_nmsg(m
, n
, MANDOCERR_NOSCOPE
);
437 m
->flags
&= ~MAN_LITERAL
;
444 static const char * const bsd_versions
[] = {
445 "3rd Berkeley Distribution",
446 "4th Berkeley Distribution",
447 "4.2 Berkeley Distribution",
448 "4.3 Berkeley Distribution",
449 "4.4 Berkeley Distribution",
457 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
461 if (0 == strcmp(s
, "3"))
463 else if (0 == strcmp(s
, "4"))
465 else if (0 == strcmp(s
, "5"))
467 else if (0 == strcmp(s
, "6"))
469 else if (0 == strcmp(s
, "7"))
476 free(m
->meta
.source
);
478 m
->meta
.source
= mandoc_strdup(p
);
485 static const char * const unix_versions
[] = {
489 "System V Release 2",
497 if (NULL
== n
|| MAN_TEXT
!= n
->type
)
498 p
= unix_versions
[0];
501 if (0 == strcmp(s
, "3"))
502 p
= unix_versions
[0];
503 else if (0 == strcmp(s
, "4"))
504 p
= unix_versions
[1];
505 else if (0 == strcmp(s
, "5")) {
507 if (nn
&& MAN_TEXT
== nn
->type
&& nn
->string
[0])
508 p
= unix_versions
[3];
510 p
= unix_versions
[2];
512 p
= unix_versions
[0];
516 free(m
->meta
.source
);
518 m
->meta
.source
= mandoc_strdup(p
);