]> git.cameronkatri.com Git - mandoc.git/blob - roff_validate.c
222f2339337ae0c05c42381ba1818e2dd97a528e
[mandoc.git] / roff_validate.c
1 /* $Id: roff_validate.c,v 1.12 2018/12/04 03:28:58 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 <stddef.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_ft(ROFF_VALID_ARGS);
34 static void roff_valid_sp(ROFF_VALID_ARGS);
35
36 static const roff_valid_fp roff_valids[ROFF_MAX] = {
37 roff_valid_br, /* br */
38 NULL, /* ce */
39 roff_valid_ft, /* ft */
40 NULL, /* ll */
41 NULL, /* mc */
42 NULL, /* po */
43 NULL, /* rj */
44 roff_valid_sp, /* sp */
45 NULL, /* ta */
46 NULL, /* ti */
47 };
48
49
50 void
51 roff_validate(struct roff_man *man)
52 {
53 struct roff_node *n;
54
55 n = man->last;
56 assert(n->tok < ROFF_MAX);
57 if (roff_valids[n->tok] != NULL)
58 (*roff_valids[n->tok])(man, n);
59 }
60
61 static void
62 roff_valid_br(ROFF_VALID_ARGS)
63 {
64 struct roff_node *np;
65
66 if (n->child != NULL)
67 mandoc_vmsg(MANDOCERR_ARG_SKIP, man->parse,
68 n->line, n->pos, "br %s", n->child->string);
69
70 if (n->next != NULL && n->next->type == ROFFT_TEXT &&
71 *n->next->string == ' ') {
72 mandoc_msg(MANDOCERR_PAR_SKIP, man->parse, 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_vmsg(MANDOCERR_PAR_SKIP, man->parse,
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_ft(ROFF_VALID_ARGS)
96 {
97 const char *cp;
98
99 if (n->child == NULL) {
100 man->next = ROFF_NEXT_CHILD;
101 roff_word_alloc(man, n->line, n->pos, "P");
102 man->last = n;
103 return;
104 }
105
106 cp = n->child->string;
107 switch (*cp) {
108 case '1':
109 case '2':
110 case '3':
111 case '4':
112 case 'I':
113 case 'P':
114 case 'R':
115 if (cp[1] == '\0')
116 return;
117 break;
118 case 'B':
119 if (cp[1] == '\0' || (cp[1] == 'I' && cp[2] == '\0'))
120 return;
121 break;
122 case 'C':
123 if (cp[1] != '\0' && cp[2] == '\0' &&
124 strchr("BIRW", cp[1]) != NULL)
125 return;
126 break;
127 default:
128 break;
129 }
130
131 mandoc_vmsg(MANDOCERR_FT_BAD, man->parse,
132 n->line, n->pos, "ft %s", cp);
133 roff_node_delete(man, n);
134 }
135
136 static void
137 roff_valid_sp(ROFF_VALID_ARGS)
138 {
139 struct roff_node *np;
140
141 if (n->child != NULL && n->child->next != NULL)
142 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
143 n->child->next->line, n->child->next->pos,
144 "sp ... %s", n->child->next->string);
145
146 if ((np = n->prev) == NULL)
147 return;
148
149 switch (np->tok) {
150 case ROFF_br:
151 mandoc_msg(MANDOCERR_PAR_SKIP, man->parse,
152 np->line, np->pos, "br before sp");
153 roff_node_delete(man, np);
154 break;
155 case MDOC_Pp:
156 mandoc_msg(MANDOCERR_PAR_SKIP, man->parse,
157 n->line, n->pos, "sp after Pp");
158 roff_node_delete(man, n);
159 break;
160 default:
161 break;
162 }
163 }