3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2017 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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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>
31 #include "mandoc_aux.h"
35 #include "libmandoc.h"
39 #define CHKARGS struct roff_man *man, struct roff_node *n
41 typedef void (*v_check)(CHKARGS);
43 static void check_par(CHKARGS);
44 static void check_part(CHKARGS);
45 static void check_root(CHKARGS);
46 static void check_text(CHKARGS);
48 static void post_AT(CHKARGS);
49 static void post_IP(CHKARGS);
50 static void post_OP(CHKARGS);
51 static void post_TH(CHKARGS);
52 static void post_UC(CHKARGS);
53 static void post_UR(CHKARGS);
54 static void post_in(CHKARGS);
55 static void post_vs(CHKARGS);
57 static const v_check __man_valids[MAN_MAX - MAN_TH] = {
95 static const v_check *man_valids = __man_valids - MAN_TH;
99 man_node_validate(struct roff_man *man)
105 man->last = man->last->child;
106 while (man->last != NULL) {
107 man_node_validate(man);
109 man->last = man->last->child;
111 man->last = man->last->next;
115 man->next = ROFF_NEXT_SIBLING;
127 if (n->tok < ROFF_MAX) {
139 assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
140 cp = man_valids + n->tok;
153 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
155 if (NULL == man->first->child)
156 mandoc_msg(MANDOCERR_DOC_EMPTY, man->parse,
157 n->line, n->pos, NULL);
159 man->meta.hasbody = 1;
161 if (NULL == man->meta.title) {
162 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
163 n->line, n->pos, NULL);
166 * If a title hasn't been set, do so now (by
167 * implication, date and section also aren't set).
170 man->meta.title = mandoc_strdup("");
171 man->meta.msec = mandoc_strdup("");
172 man->meta.date = man->quick ? mandoc_strdup("") :
173 mandoc_normdate(man, NULL, n->line, n->pos);
176 if (man->meta.os_e &&
177 (man->meta.rcsids & (1 << man->meta.os_e)) == 0)
178 mandoc_msg(MANDOCERR_RCS_MISSING, man->parse, 0, 0,
179 man->meta.os_e == MANDOC_OS_OPENBSD ?
180 "(OpenBSD)" : "(NetBSD)");
188 if (MAN_LITERAL & man->flags)
192 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
193 mandoc_msg(MANDOCERR_FI_TAB, man->parse,
194 n->line, n->pos + (p - cp), NULL);
201 if (n->child == NULL)
202 mandoc_msg(MANDOCERR_OP_EMPTY, man->parse,
203 n->line, n->pos, "OP");
204 else if (n->child->next != NULL && n->child->next->next != NULL) {
205 n = n->child->next->next;
206 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
207 n->line, n->pos, "OP ... %s", n->string);
214 if (n->type == ROFFT_HEAD && n->child == NULL)
215 mandoc_msg(MANDOCERR_UR_NOHEAD, man->parse,
216 n->line, n->pos, roff_name[n->tok]);
224 if (n->type == ROFFT_BODY && n->child == NULL)
225 mandoc_msg(MANDOCERR_BLK_EMPTY, man->parse,
226 n->line, n->pos, roff_name[n->tok]);
235 if (n->body->child == NULL)
236 roff_node_delete(man, n);
239 if (n->child == NULL)
240 mandoc_vmsg(MANDOCERR_PAR_SKIP,
241 man->parse, n->line, n->pos,
242 "%s empty", roff_name[n->tok]);
245 if (n->child != NULL)
246 mandoc_vmsg(MANDOCERR_ARG_SKIP,
247 man->parse, n->line, n->pos, "%s %s%s",
248 roff_name[n->tok], n->child->string,
249 n->child->next != NULL ? " ..." : "");
262 if (n->head->child == NULL && n->body->child == NULL)
263 roff_node_delete(man, n);
266 if (n->parent->head->child == NULL && n->child == NULL)
267 mandoc_vmsg(MANDOCERR_PAR_SKIP,
268 man->parse, n->line, n->pos,
269 "%s empty", roff_name[n->tok]);
279 struct roff_node *nb;
282 free(man->meta.title);
285 free(man->meta.msec);
286 free(man->meta.date);
288 man->meta.title = man->meta.vol = man->meta.date =
289 man->meta.msec = man->meta.os = NULL;
293 /* ->TITLE<- MSEC DATE OS VOL */
296 if (n && n->string) {
297 for (p = n->string; '\0' != *p; p++) {
298 /* Only warn about this once... */
299 if (isalpha((unsigned char)*p) &&
300 ! isupper((unsigned char)*p)) {
301 mandoc_vmsg(MANDOCERR_TITLE_CASE,
303 n->pos + (p - n->string),
308 man->meta.title = mandoc_strdup(n->string);
310 man->meta.title = mandoc_strdup("");
311 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
312 nb->line, nb->pos, "TH");
315 /* TITLE ->MSEC<- DATE OS VOL */
320 man->meta.msec = mandoc_strdup(n->string);
322 man->meta.msec = mandoc_strdup("");
323 mandoc_vmsg(MANDOCERR_MSEC_MISSING, man->parse,
324 nb->line, nb->pos, "TH %s", man->meta.title);
327 /* TITLE MSEC ->DATE<- OS VOL */
331 if (n && n->string && '\0' != n->string[0]) {
332 man->meta.date = man->quick ?
333 mandoc_strdup(n->string) :
334 mandoc_normdate(man, n->string, n->line, n->pos);
336 man->meta.date = mandoc_strdup("");
337 mandoc_msg(MANDOCERR_DATE_MISSING, man->parse,
338 n ? n->line : nb->line,
339 n ? n->pos : nb->pos, "TH");
342 /* TITLE MSEC DATE ->OS<- VOL */
344 if (n && (n = n->next))
345 man->meta.os = mandoc_strdup(n->string);
346 else if (man->os_s != NULL)
347 man->meta.os = mandoc_strdup(man->os_s);
348 if (man->meta.os_e == MANDOC_OS_OTHER && man->meta.os != NULL) {
349 if (strstr(man->meta.os, "OpenBSD") != NULL)
350 man->meta.os_e = MANDOC_OS_OPENBSD;
351 else if (strstr(man->meta.os, "NetBSD") != NULL)
352 man->meta.os_e = MANDOC_OS_NETBSD;
355 /* TITLE MSEC DATE OS ->VOL<- */
356 /* If missing, use the default VOL name for MSEC. */
358 if (n && (n = n->next))
359 man->meta.vol = mandoc_strdup(n->string);
360 else if ('\0' != man->meta.msec[0] &&
361 (NULL != (p = mandoc_a2msec(man->meta.msec))))
362 man->meta.vol = mandoc_strdup(p);
364 if (n != NULL && (n = n->next) != NULL)
365 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
366 n->line, n->pos, "TH ... %s", n->string);
369 * Remove the `TH' node after we've processed it for our
372 roff_node_delete(man, man->last);
378 static const char * const bsd_versions[] = {
379 "3rd Berkeley Distribution",
380 "4th Berkeley Distribution",
381 "4.2 Berkeley Distribution",
382 "4.3 Berkeley Distribution",
383 "4.4 Berkeley Distribution",
390 if (n == NULL || n->type != ROFFT_TEXT)
394 if (0 == strcmp(s, "3"))
396 else if (0 == strcmp(s, "4"))
398 else if (0 == strcmp(s, "5"))
400 else if (0 == strcmp(s, "6"))
402 else if (0 == strcmp(s, "7"))
409 man->meta.os = mandoc_strdup(p);
415 static const char * const unix_versions[] = {
419 "System V Release 2",
422 struct roff_node *nn;
427 if (n == NULL || n->type != ROFFT_TEXT)
428 p = unix_versions[0];
431 if (0 == strcmp(s, "3"))
432 p = unix_versions[0];
433 else if (0 == strcmp(s, "4"))
434 p = unix_versions[1];
435 else if (0 == strcmp(s, "5")) {
438 nn->type == ROFFT_TEXT &&
439 nn->string[0] != '\0')
440 p = unix_versions[3];
442 p = unix_versions[2];
444 p = unix_versions[0];
448 man->meta.os = mandoc_strdup(p);
456 if (n->parent->tok != MAN_TP ||
457 n->parent->type != ROFFT_HEAD ||
459 *n->child->string == '+' ||
460 *n->child->string == '-')
462 mandoc_asprintf(&s, "+%s", n->child->string);
463 free(n->child->string);
464 n->child->string = s;
474 switch (n->parent->tok) {
480 mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos,
481 "%s after %s", roff_name[n->tok],
482 roff_name[n->parent->tok]);
486 * Don't warn about this because it occurs in pod2man
487 * and would cause considerable (unfixable) warnage.
489 roff_node_delete(man, n);