1 /* $Id: man_validate.c,v 1.105 2014/08/06 15:09:05 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012, 2013, 2014 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>
35 #include "mandoc_aux.h"
37 #include "libmandoc.h"
39 #define CHKARGS struct man *man, struct man_node *n
41 typedef int (*v_check)(CHKARGS);
43 static int check_eq0(CHKARGS);
44 static int check_eq2(CHKARGS);
45 static int check_le1(CHKARGS);
46 static int check_le5(CHKARGS);
47 static int check_par(CHKARGS);
48 static int check_part(CHKARGS);
49 static int check_root(CHKARGS);
50 static int check_text(CHKARGS);
52 static int post_AT(CHKARGS);
53 static int post_IP(CHKARGS);
54 static int post_vs(CHKARGS);
55 static int post_fi(CHKARGS);
56 static int post_ft(CHKARGS);
57 static int post_nf(CHKARGS);
58 static int post_TH(CHKARGS);
59 static int post_UC(CHKARGS);
60 static int post_UR(CHKARGS);
62 static v_check man_valids[MAN_MAX] = {
106 man_valid_post(struct man *man)
112 if (n->flags & MAN_VALID)
114 n->flags |= MAN_VALID;
118 return(check_text(man, n));
120 return(check_root(man, n));
126 cp = man_valids + n->tok;
127 return(*cp ? (*cp)(man, n) : 1);
135 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
137 if (NULL == man->first->child)
138 mandoc_msg(MANDOCERR_DOC_EMPTY, man->parse,
139 n->line, n->pos, NULL);
141 man->meta.hasbody = 1;
143 if (NULL == man->meta.title) {
144 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
145 n->line, n->pos, NULL);
148 * If a title hasn't been set, do so now (by
149 * implication, date and section also aren't set).
152 man->meta.title = mandoc_strdup("");
153 man->meta.msec = mandoc_strdup("");
154 man->meta.date = man->quick ? mandoc_strdup("") :
155 mandoc_normdate(man->parse, NULL, n->line, n->pos);
166 if (MAN_LITERAL & man->flags)
170 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
171 mandoc_msg(MANDOCERR_FI_TAB, man->parse,
172 n->line, n->pos + (p - cp), NULL);
176 #define INEQ_DEFINE(x, ineq, name) \
178 check_##name(CHKARGS) \
180 if (n->nchild ineq (x)) \
182 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
183 "line arguments %s %d (have %d)", \
184 #ineq, (x), n->nchild); \
188 INEQ_DEFINE(0, ==, eq0)
189 INEQ_DEFINE(2, ==, eq2)
190 INEQ_DEFINE(1, <=, le1)
191 INEQ_DEFINE(5, <=, le5)
197 if (MAN_HEAD == n->type && 1 != n->nchild)
198 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
199 n->pos, "line arguments eq 1 (have %d)", n->nchild);
201 return(check_part(man, n));
214 cp = n->child->string;
233 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
237 if ('W' == cp[1] && '\0' == cp[2])
245 mandoc_vmsg(MANDOCERR_FT_BAD, man->parse,
246 n->line, n->pos, "ft %s", cp);
251 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
252 n->pos, "want one child (have %d)", n->nchild);
261 if (MAN_BODY == n->type && 0 == n->nchild)
262 mandoc_msg(MANDOCERR_ARGCWARN, man->parse, n->line,
263 n->pos, "want children (have none)");
274 if (0 == n->body->nchild)
275 man_node_delete(man, n);
279 mandoc_vmsg(MANDOCERR_PAR_SKIP,
280 man->parse, n->line, n->pos,
281 "%s empty", man_macronames[n->tok]);
285 mandoc_vmsg(MANDOCERR_ARG_SKIP,
286 man->parse, n->line, n->pos,
287 "%s %s%s", man_macronames[n->tok],
289 n->nchild > 1 ? " ..." : "");
304 if (0 == n->head->nchild && 0 == n->body->nchild)
305 man_node_delete(man, n);
308 if (0 == n->parent->head->nchild && 0 == n->nchild)
309 mandoc_vmsg(MANDOCERR_PAR_SKIP,
310 man->parse, n->line, n->pos,
311 "%s empty", man_macronames[n->tok]);
327 free(man->meta.title);
329 free(man->meta.source);
330 free(man->meta.msec);
331 free(man->meta.date);
333 man->meta.title = man->meta.vol = man->meta.date =
334 man->meta.msec = man->meta.source = NULL;
338 /* ->TITLE<- MSEC DATE SOURCE VOL */
341 if (n && n->string) {
342 for (p = n->string; '\0' != *p; p++) {
343 /* Only warn about this once... */
344 if (isalpha((unsigned char)*p) &&
345 ! isupper((unsigned char)*p)) {
346 mandoc_vmsg(MANDOCERR_TITLE_CASE,
348 n->pos + (p - n->string),
353 man->meta.title = mandoc_strdup(n->string);
355 man->meta.title = mandoc_strdup("");
356 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
357 nb->line, nb->pos, "TH");
360 /* TITLE ->MSEC<- DATE SOURCE VOL */
365 man->meta.msec = mandoc_strdup(n->string);
367 man->meta.msec = mandoc_strdup("");
368 mandoc_vmsg(MANDOCERR_MSEC_MISSING, man->parse,
369 nb->line, nb->pos, "TH %s", man->meta.title);
372 /* TITLE MSEC ->DATE<- SOURCE VOL */
376 if (n && n->string && '\0' != n->string[0]) {
377 man->meta.date = man->quick ?
378 mandoc_strdup(n->string) :
379 mandoc_normdate(man->parse, n->string,
382 man->meta.date = mandoc_strdup("");
383 mandoc_msg(MANDOCERR_DATE_MISSING, man->parse,
384 n ? n->line : nb->line,
385 n ? n->pos : nb->pos, "TH");
388 /* TITLE MSEC DATE ->SOURCE<- VOL */
390 if (n && (n = n->next))
391 man->meta.source = mandoc_strdup(n->string);
393 /* TITLE MSEC DATE SOURCE ->VOL<- */
394 /* If missing, use the default VOL name for MSEC. */
396 if (n && (n = n->next))
397 man->meta.vol = mandoc_strdup(n->string);
398 else if ('\0' != man->meta.msec[0] &&
399 (NULL != (p = mandoc_a2msec(man->meta.msec))))
400 man->meta.vol = mandoc_strdup(p);
403 * Remove the `TH' node after we've processed it for our
406 man_node_delete(man, man->last);
416 if (MAN_LITERAL & man->flags)
417 mandoc_msg(MANDOCERR_NF_SKIP, man->parse,
418 n->line, n->pos, "nf");
420 man->flags |= MAN_LITERAL;
430 if ( ! (MAN_LITERAL & man->flags))
431 mandoc_msg(MANDOCERR_FI_SKIP, man->parse,
432 n->line, n->pos, "fi");
434 man->flags &= ~MAN_LITERAL;
441 static const char * const bsd_versions[] = {
442 "3rd Berkeley Distribution",
443 "4th Berkeley Distribution",
444 "4.2 Berkeley Distribution",
445 "4.3 Berkeley Distribution",
446 "4.4 Berkeley Distribution",
453 if (NULL == n || MAN_TEXT != n->type)
457 if (0 == strcmp(s, "3"))
459 else if (0 == strcmp(s, "4"))
461 else if (0 == strcmp(s, "5"))
463 else if (0 == strcmp(s, "6"))
465 else if (0 == strcmp(s, "7"))
471 free(man->meta.source);
472 man->meta.source = mandoc_strdup(p);
479 static const char * const unix_versions[] = {
483 "System V Release 2",
491 if (NULL == n || MAN_TEXT != n->type)
492 p = unix_versions[0];
495 if (0 == strcmp(s, "3"))
496 p = unix_versions[0];
497 else if (0 == strcmp(s, "4"))
498 p = unix_versions[1];
499 else if (0 == strcmp(s, "5")) {
501 if (nn && MAN_TEXT == nn->type && nn->string[0])
502 p = unix_versions[3];
504 p = unix_versions[2];
506 p = unix_versions[0];
509 free(man->meta.source);
510 man->meta.source = mandoc_strdup(p);
518 if (n->tok == MAN_br)
526 switch (n->parent->tok) {
530 mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos,
531 "%s after %s", man_macronames[n->tok],
532 man_macronames[n->parent->tok]);
536 * Don't warn about this because it occurs in pod2man
537 * and would cause considerable (unfixable) warnage.
539 man_node_delete(man, n);