3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2015 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.
20 #include <sys/types.h>
33 #include "mandoc_aux.h"
35 #include "libmandoc.h"
37 #define CHKARGS struct man *man, struct man_node *n
39 typedef void (*v_check)(CHKARGS);
41 static void check_par(CHKARGS);
42 static void check_part(CHKARGS);
43 static void check_root(CHKARGS);
44 static void check_text(CHKARGS);
46 static void post_AT(CHKARGS);
47 static void post_IP(CHKARGS);
48 static void post_vs(CHKARGS);
49 static void post_fi(CHKARGS);
50 static void post_ft(CHKARGS);
51 static void post_nf(CHKARGS);
52 static void post_OP(CHKARGS);
53 static void post_TH(CHKARGS);
54 static void post_UC(CHKARGS);
55 static void post_UR(CHKARGS);
57 static v_check man_valids[MAN_MAX] = {
100 man_valid_post(struct man *man)
106 if (n->flags & MAN_VALID)
108 n->flags |= MAN_VALID;
122 cp = man_valids + n->tok;
133 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
135 if (NULL == man->first->child)
136 mandoc_msg(MANDOCERR_DOC_EMPTY, man->parse,
137 n->line, n->pos, NULL);
139 man->meta.hasbody = 1;
141 if (NULL == man->meta.title) {
142 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
143 n->line, n->pos, NULL);
146 * If a title hasn't been set, do so now (by
147 * implication, date and section also aren't set).
150 man->meta.title = mandoc_strdup("");
151 man->meta.msec = mandoc_strdup("");
152 man->meta.date = man->quick ? mandoc_strdup("") :
153 mandoc_normdate(man->parse, NULL, n->line, n->pos);
162 if (MAN_LITERAL & man->flags)
166 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
167 mandoc_msg(MANDOCERR_FI_TAB, man->parse,
168 n->line, n->pos + (p - cp), NULL);
176 mandoc_msg(MANDOCERR_OP_EMPTY, man->parse,
177 n->line, n->pos, "OP");
178 else if (n->nchild > 2) {
179 n = n->child->next->next;
180 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
181 n->line, n->pos, "OP ... %s", n->string);
189 if (n->type == MAN_HEAD && n->child == NULL)
190 mandoc_vmsg(MANDOCERR_UR_NOHEAD, man->parse,
191 n->line, n->pos, "UR");
205 cp = n->child->string;
224 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
228 if ('W' == cp[1] && '\0' == cp[2])
236 mandoc_vmsg(MANDOCERR_FT_BAD, man->parse,
237 n->line, n->pos, "ft %s", cp);
246 if (n->type == MAN_BODY && n->child == NULL)
247 mandoc_msg(MANDOCERR_BLK_EMPTY, man->parse,
248 n->line, n->pos, man_macronames[n->tok]);
257 if (0 == n->body->nchild)
258 man_node_delete(man, n);
262 mandoc_vmsg(MANDOCERR_PAR_SKIP,
263 man->parse, n->line, n->pos,
264 "%s empty", man_macronames[n->tok]);
268 mandoc_vmsg(MANDOCERR_ARG_SKIP,
269 man->parse, n->line, n->pos,
270 "%s %s%s", man_macronames[n->tok],
272 n->nchild > 1 ? " ..." : "");
285 if (0 == n->head->nchild && 0 == n->body->nchild)
286 man_node_delete(man, n);
289 if (0 == n->parent->head->nchild && 0 == n->nchild)
290 mandoc_vmsg(MANDOCERR_PAR_SKIP,
291 man->parse, n->line, n->pos,
292 "%s empty", man_macronames[n->tok]);
305 free(man->meta.title);
307 free(man->meta.source);
308 free(man->meta.msec);
309 free(man->meta.date);
311 man->meta.title = man->meta.vol = man->meta.date =
312 man->meta.msec = man->meta.source = NULL;
316 /* ->TITLE<- MSEC DATE SOURCE VOL */
319 if (n && n->string) {
320 for (p = n->string; '\0' != *p; p++) {
321 /* Only warn about this once... */
322 if (isalpha((unsigned char)*p) &&
323 ! isupper((unsigned char)*p)) {
324 mandoc_vmsg(MANDOCERR_TITLE_CASE,
326 n->pos + (p - n->string),
331 man->meta.title = mandoc_strdup(n->string);
333 man->meta.title = mandoc_strdup("");
334 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
335 nb->line, nb->pos, "TH");
338 /* TITLE ->MSEC<- DATE SOURCE VOL */
343 man->meta.msec = mandoc_strdup(n->string);
345 man->meta.msec = mandoc_strdup("");
346 mandoc_vmsg(MANDOCERR_MSEC_MISSING, man->parse,
347 nb->line, nb->pos, "TH %s", man->meta.title);
350 /* TITLE MSEC ->DATE<- SOURCE VOL */
354 if (n && n->string && '\0' != n->string[0]) {
355 man->meta.date = man->quick ?
356 mandoc_strdup(n->string) :
357 mandoc_normdate(man->parse, n->string,
360 man->meta.date = mandoc_strdup("");
361 mandoc_msg(MANDOCERR_DATE_MISSING, man->parse,
362 n ? n->line : nb->line,
363 n ? n->pos : nb->pos, "TH");
366 /* TITLE MSEC DATE ->SOURCE<- VOL */
368 if (n && (n = n->next))
369 man->meta.source = mandoc_strdup(n->string);
370 else if (man->defos != NULL)
371 man->meta.source = mandoc_strdup(man->defos);
373 /* TITLE MSEC DATE SOURCE ->VOL<- */
374 /* If missing, use the default VOL name for MSEC. */
376 if (n && (n = n->next))
377 man->meta.vol = mandoc_strdup(n->string);
378 else if ('\0' != man->meta.msec[0] &&
379 (NULL != (p = mandoc_a2msec(man->meta.msec))))
380 man->meta.vol = mandoc_strdup(p);
382 if (n != NULL && (n = n->next) != NULL)
383 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
384 n->line, n->pos, "TH ... %s", n->string);
387 * Remove the `TH' node after we've processed it for our
390 man_node_delete(man, man->last);
397 if (man->flags & MAN_LITERAL)
398 mandoc_msg(MANDOCERR_NF_SKIP, man->parse,
399 n->line, n->pos, "nf");
401 man->flags |= MAN_LITERAL;
408 if ( ! (MAN_LITERAL & man->flags))
409 mandoc_msg(MANDOCERR_FI_SKIP, man->parse,
410 n->line, n->pos, "fi");
412 man->flags &= ~MAN_LITERAL;
418 static const char * const bsd_versions[] = {
419 "3rd Berkeley Distribution",
420 "4th Berkeley Distribution",
421 "4.2 Berkeley Distribution",
422 "4.3 Berkeley Distribution",
423 "4.4 Berkeley Distribution",
430 if (NULL == n || MAN_TEXT != n->type)
434 if (0 == strcmp(s, "3"))
436 else if (0 == strcmp(s, "4"))
438 else if (0 == strcmp(s, "5"))
440 else if (0 == strcmp(s, "6"))
442 else if (0 == strcmp(s, "7"))
448 free(man->meta.source);
449 man->meta.source = mandoc_strdup(p);
455 static const char * const unix_versions[] = {
459 "System V Release 2",
467 if (NULL == n || MAN_TEXT != n->type)
468 p = unix_versions[0];
471 if (0 == strcmp(s, "3"))
472 p = unix_versions[0];
473 else if (0 == strcmp(s, "4"))
474 p = unix_versions[1];
475 else if (0 == strcmp(s, "5")) {
477 if (nn && MAN_TEXT == nn->type && nn->string[0])
478 p = unix_versions[3];
480 p = unix_versions[2];
482 p = unix_versions[0];
485 free(man->meta.source);
486 man->meta.source = mandoc_strdup(p);
496 switch (n->parent->tok) {
500 mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos,
501 "%s after %s", man_macronames[n->tok],
502 man_macronames[n->parent->tok]);
506 * Don't warn about this because it occurs in pod2man
507 * and would cause considerable (unfixable) warnage.
509 man_node_delete(man, n);