1 /* $Id: roff_validate.c,v 1.15 2018/12/16 00:17:02 schwarze Exp $ */
3 * Copyright (c) 2010, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
17 #include <sys/types.h>
25 #include "libmandoc.h"
28 #define ROFF_VALID_ARGS struct roff_man *man, struct roff_node *n
30 typedef void (*roff_valid_fp
)(ROFF_VALID_ARGS
);
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
);
36 static const roff_valid_fp roff_valids
[ROFF_MAX
] = {
37 roff_valid_br
, /* br */
39 roff_valid_ft
, /* ft */
44 roff_valid_sp
, /* sp */
51 roff_validate(struct roff_man
*man
)
56 assert(n
->tok
< ROFF_MAX
);
57 if (roff_valids
[n
->tok
] != NULL
)
58 (*roff_valids
[n
->tok
])(man
, n
);
62 roff_valid_br(ROFF_VALID_ARGS
)
67 mandoc_msg(MANDOCERR_ARG_SKIP
,
68 n
->line
, n
->pos
, "br %s", n
->child
->string
);
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
);
78 if ((np
= n
->prev
) == NULL
)
85 mandoc_msg(MANDOCERR_PAR_SKIP
,
86 n
->line
, n
->pos
, "br after %s", roff_name
[np
->tok
]);
87 roff_node_delete(man
, n
);
95 roff_valid_ft(ROFF_VALID_ARGS
)
99 if (n
->child
== NULL
) {
100 man
->next
= ROFF_NEXT_CHILD
;
101 roff_word_alloc(man
, n
->line
, n
->pos
, "P");
106 cp
= n
->child
->string
;
107 if (mandoc_font(cp
, (int)strlen(cp
)) != ESCAPE_ERROR
)
109 mandoc_msg(MANDOCERR_FT_BAD
, n
->line
, n
->pos
, "ft %s", cp
);
110 roff_node_delete(man
, n
);
114 roff_valid_sp(ROFF_VALID_ARGS
)
116 struct roff_node
*np
;
118 if (n
->child
!= NULL
&& n
->child
->next
!= NULL
)
119 mandoc_msg(MANDOCERR_ARG_EXCESS
,
120 n
->child
->next
->line
, n
->child
->next
->pos
,
121 "sp ... %s", n
->child
->next
->string
);
123 if ((np
= n
->prev
) == NULL
)
128 mandoc_msg(MANDOCERR_PAR_SKIP
,
129 np
->line
, np
->pos
, "br before sp");
130 roff_node_delete(man
, np
);
133 mandoc_msg(MANDOCERR_PAR_SKIP
,
134 n
->line
, n
->pos
, "sp after Pp");
135 roff_node_delete(man
, n
);