roff_validate.c revision 1.1.1.2 1 1.1.1.2 christos /* Id: roff_validate.c,v 1.18 2018/12/31 09:02:37 schwarze Exp */
2 1.1 christos /*
3 1.1.1.2 christos * Copyright (c) 2010, 2017, 2018 Ingo Schwarze <schwarze (at) openbsd.org>
4 1.1 christos *
5 1.1 christos * Permission to use, copy, modify, and distribute this software for any
6 1.1 christos * purpose with or without fee is hereby granted, provided that the above
7 1.1 christos * copyright notice and this permission notice appear in all copies.
8 1.1 christos *
9 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 1.1 christos */
17 1.1 christos #include <sys/types.h>
18 1.1 christos
19 1.1 christos #include <assert.h>
20 1.1.1.2 christos #include <stdio.h>
21 1.1.1.2 christos #include <string.h>
22 1.1 christos
23 1.1 christos #include "mandoc.h"
24 1.1 christos #include "roff.h"
25 1.1 christos #include "libmandoc.h"
26 1.1 christos #include "roff_int.h"
27 1.1 christos
28 1.1 christos #define ROFF_VALID_ARGS struct roff_man *man, struct roff_node *n
29 1.1 christos
30 1.1 christos typedef void (*roff_valid_fp)(ROFF_VALID_ARGS);
31 1.1 christos
32 1.1.1.2 christos static void roff_valid_br(ROFF_VALID_ARGS);
33 1.1.1.2 christos static void roff_valid_fi(ROFF_VALID_ARGS);
34 1.1 christos static void roff_valid_ft(ROFF_VALID_ARGS);
35 1.1.1.2 christos static void roff_valid_nf(ROFF_VALID_ARGS);
36 1.1.1.2 christos static void roff_valid_sp(ROFF_VALID_ARGS);
37 1.1 christos
38 1.1 christos static const roff_valid_fp roff_valids[ROFF_MAX] = {
39 1.1.1.2 christos roff_valid_br, /* br */
40 1.1 christos NULL, /* ce */
41 1.1.1.2 christos roff_valid_fi, /* fi */
42 1.1 christos roff_valid_ft, /* ft */
43 1.1 christos NULL, /* ll */
44 1.1 christos NULL, /* mc */
45 1.1.1.2 christos roff_valid_nf, /* nf */
46 1.1 christos NULL, /* po */
47 1.1 christos NULL, /* rj */
48 1.1.1.2 christos roff_valid_sp, /* sp */
49 1.1 christos NULL, /* ta */
50 1.1 christos NULL, /* ti */
51 1.1 christos };
52 1.1 christos
53 1.1 christos
54 1.1 christos void
55 1.1 christos roff_validate(struct roff_man *man)
56 1.1 christos {
57 1.1 christos struct roff_node *n;
58 1.1 christos
59 1.1 christos n = man->last;
60 1.1 christos assert(n->tok < ROFF_MAX);
61 1.1 christos if (roff_valids[n->tok] != NULL)
62 1.1 christos (*roff_valids[n->tok])(man, n);
63 1.1 christos }
64 1.1 christos
65 1.1 christos static void
66 1.1.1.2 christos roff_valid_br(ROFF_VALID_ARGS)
67 1.1.1.2 christos {
68 1.1.1.2 christos struct roff_node *np;
69 1.1.1.2 christos
70 1.1.1.2 christos if (n->next != NULL && n->next->type == ROFFT_TEXT &&
71 1.1.1.2 christos *n->next->string == ' ') {
72 1.1.1.2 christos mandoc_msg(MANDOCERR_PAR_SKIP, n->line, n->pos,
73 1.1.1.2 christos "br before text line with leading blank");
74 1.1.1.2 christos roff_node_delete(man, n);
75 1.1.1.2 christos return;
76 1.1.1.2 christos }
77 1.1.1.2 christos
78 1.1.1.2 christos if ((np = n->prev) == NULL)
79 1.1.1.2 christos return;
80 1.1.1.2 christos
81 1.1.1.2 christos switch (np->tok) {
82 1.1.1.2 christos case ROFF_br:
83 1.1.1.2 christos case ROFF_sp:
84 1.1.1.2 christos case MDOC_Pp:
85 1.1.1.2 christos mandoc_msg(MANDOCERR_PAR_SKIP,
86 1.1.1.2 christos n->line, n->pos, "br after %s", roff_name[np->tok]);
87 1.1.1.2 christos roff_node_delete(man, n);
88 1.1.1.2 christos break;
89 1.1.1.2 christos default:
90 1.1.1.2 christos break;
91 1.1.1.2 christos }
92 1.1.1.2 christos }
93 1.1.1.2 christos
94 1.1.1.2 christos static void
95 1.1.1.2 christos roff_valid_fi(ROFF_VALID_ARGS)
96 1.1.1.2 christos {
97 1.1.1.2 christos if ((n->flags & NODE_NOFILL) == 0)
98 1.1.1.2 christos mandoc_msg(MANDOCERR_FI_SKIP, n->line, n->pos, "fi");
99 1.1.1.2 christos }
100 1.1.1.2 christos
101 1.1.1.2 christos static void
102 1.1 christos roff_valid_ft(ROFF_VALID_ARGS)
103 1.1 christos {
104 1.1.1.2 christos const char *cp;
105 1.1 christos
106 1.1 christos if (n->child == NULL) {
107 1.1 christos man->next = ROFF_NEXT_CHILD;
108 1.1 christos roff_word_alloc(man, n->line, n->pos, "P");
109 1.1 christos man->last = n;
110 1.1 christos return;
111 1.1 christos }
112 1.1 christos
113 1.1 christos cp = n->child->string;
114 1.1.1.2 christos if (mandoc_font(cp, (int)strlen(cp)) != ESCAPE_ERROR)
115 1.1.1.2 christos return;
116 1.1.1.2 christos mandoc_msg(MANDOCERR_FT_BAD, n->line, n->pos, "ft %s", cp);
117 1.1.1.2 christos roff_node_delete(man, n);
118 1.1.1.2 christos }
119 1.1.1.2 christos
120 1.1.1.2 christos static void
121 1.1.1.2 christos roff_valid_nf(ROFF_VALID_ARGS)
122 1.1.1.2 christos {
123 1.1.1.2 christos if (n->flags & NODE_NOFILL)
124 1.1.1.2 christos mandoc_msg(MANDOCERR_NF_SKIP, n->line, n->pos, "nf");
125 1.1.1.2 christos }
126 1.1.1.2 christos
127 1.1.1.2 christos static void
128 1.1.1.2 christos roff_valid_sp(ROFF_VALID_ARGS)
129 1.1.1.2 christos {
130 1.1.1.2 christos struct roff_node *np;
131 1.1.1.2 christos
132 1.1.1.2 christos if ((np = n->prev) == NULL)
133 1.1.1.2 christos return;
134 1.1.1.2 christos
135 1.1.1.2 christos switch (np->tok) {
136 1.1.1.2 christos case ROFF_br:
137 1.1.1.2 christos mandoc_msg(MANDOCERR_PAR_SKIP,
138 1.1.1.2 christos np->line, np->pos, "br before sp");
139 1.1.1.2 christos roff_node_delete(man, np);
140 1.1 christos break;
141 1.1.1.2 christos case MDOC_Pp:
142 1.1.1.2 christos mandoc_msg(MANDOCERR_PAR_SKIP,
143 1.1.1.2 christos n->line, n->pos, "sp after Pp");
144 1.1.1.2 christos roff_node_delete(man, n);
145 1.1 christos break;
146 1.1 christos default:
147 1.1 christos break;
148 1.1 christos }
149 1.1 christos }
150