xref: /illumos-gate/usr/src/cmd/mandoc/roff_validate.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
1 /*	$Id: roff_validate.c,v 1.18 2018/12/31 09:02:37 schwarze Exp $ */
2 /*
3  * Copyright (c) 2010, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
4  *
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.
8  *
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.
16  */
17 #include <sys/types.h>
18 
19 #include <assert.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 #include "mandoc.h"
24 #include "roff.h"
25 #include "libmandoc.h"
26 #include "roff_int.h"
27 
28 #define	ROFF_VALID_ARGS struct roff_man *man, struct roff_node *n
29 
30 typedef	void	(*roff_valid_fp)(ROFF_VALID_ARGS);
31 
32 static	void	  roff_valid_br(ROFF_VALID_ARGS);
33 static	void	  roff_valid_fi(ROFF_VALID_ARGS);
34 static	void	  roff_valid_ft(ROFF_VALID_ARGS);
35 static	void	  roff_valid_nf(ROFF_VALID_ARGS);
36 static	void	  roff_valid_sp(ROFF_VALID_ARGS);
37 
38 static	const roff_valid_fp roff_valids[ROFF_MAX] = {
39 	roff_valid_br,  /* br */
40 	NULL,  /* ce */
41 	roff_valid_fi,  /* fi */
42 	roff_valid_ft,  /* ft */
43 	NULL,  /* ll */
44 	NULL,  /* mc */
45 	roff_valid_nf,  /* nf */
46 	NULL,  /* po */
47 	NULL,  /* rj */
48 	roff_valid_sp,  /* sp */
49 	NULL,  /* ta */
50 	NULL,  /* ti */
51 };
52 
53 
54 void
55 roff_validate(struct roff_man *man)
56 {
57 	struct roff_node	*n;
58 
59 	n = man->last;
60 	assert(n->tok < ROFF_MAX);
61 	if (roff_valids[n->tok] != NULL)
62 		(*roff_valids[n->tok])(man, n);
63 }
64 
65 static void
66 roff_valid_br(ROFF_VALID_ARGS)
67 {
68 	struct roff_node	*np;
69 
70 	if (n->next != NULL && n->next->type == ROFFT_TEXT &&
71 	    *n->next->string == ' ') {
72 		mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos,
73 		    "br before text line with leading blank");
74 		roff_node_delete(man, n);
75 		return;
76 	}
77 
78 	if ((np = n->prev) == NULL)
79 		return;
80 
81 	switch (np->tok) {
82 	case ROFF_br:
83 	case ROFF_sp:
84 	case MDOC_Pp:
85 		mandoc_msg(MANDOCERR_PAR_SKIP,
86 		    n->line, n->pos, "br after %s", roff_name[np->tok]);
87 		roff_node_delete(man, n);
88 		break;
89 	default:
90 		break;
91 	}
92 }
93 
94 static void
95 roff_valid_fi(ROFF_VALID_ARGS)
96 {
97 	if ((n->flags & NODE_NOFILL) == 0)
98 		mandoc_msg(MANDOCERR_FI_SKIP, n->line, n->pos, "fi");
99 }
100 
101 static void
102 roff_valid_ft(ROFF_VALID_ARGS)
103 {
104 	const char		*cp;
105 
106 	if (n->child == NULL) {
107 		man->next = ROFF_NEXT_CHILD;
108 		roff_word_alloc(man, n->line, n->pos, "P");
109 		man->last = n;
110 		return;
111 	}
112 
113 	cp = n->child->string;
114 	if (mandoc_font(cp, (int)strlen(cp)) != ESCAPE_ERROR)
115 		return;
116 	mandoc_msg(MANDOCERR_FT_BAD, n->line, n->pos, "ft %s", cp);
117 	roff_node_delete(man, n);
118 }
119 
120 static void
121 roff_valid_nf(ROFF_VALID_ARGS)
122 {
123 	if (n->flags & NODE_NOFILL)
124 		mandoc_msg(MANDOCERR_NF_SKIP, n->line, n->pos, "nf");
125 }
126 
127 static void
128 roff_valid_sp(ROFF_VALID_ARGS)
129 {
130 	struct roff_node	*np;
131 
132 	if ((np = n->prev) == NULL)
133 		return;
134 
135 	switch (np->tok) {
136 	case ROFF_br:
137 		mandoc_msg(MANDOCERR_PAR_SKIP,
138 		    np->line, np->pos, "br before sp");
139 		roff_node_delete(man, np);
140 		break;
141 	case MDOC_Pp:
142 		mandoc_msg(MANDOCERR_PAR_SKIP,
143 		    n->line, n->pos, "sp after Pp");
144 		roff_node_delete(man, n);
145 		break;
146 	default:
147 		break;
148 	}
149 }
150