]>
git.cameronkatri.com Git - mandoc.git/blob - validate.c
0f03b2334c3efa344697688a21b6a014b8b834cf
1 /* $Id: validate.c,v 1.1 2008/11/29 14:14:21 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/param.h>
28 #include "libmdocml.h"
31 #ifdef __linux__ /* FIXME */
32 #define strlcat strncat
36 const struct md_args
*args
;
37 const struct md_rbuf
*rbuf
;
39 struct rofftree
*tree
;
45 #define MD_LITERAL (1 << 0)
48 static void roffmsg(void *arg
, enum roffmsg
,
49 const char *, const char *, char *);
50 static int roffhead(void *);
51 static int rofftail(void *);
52 static int roffin(void *, int, int *, char **);
53 static int roffdata(void *, char *);
54 static int roffout(void *, int);
55 static int roffblkin(void *, int);
56 static int roffblkout(void *, int);
57 static int roffspecial(void *, int);
59 static int mbuf_newline(struct md_valid
*);
60 static int mbuf_indent(struct md_valid
*);
61 static int mbuf_data(struct md_valid
*, char *);
65 mbuf_indent(struct md_valid
*p
)
71 for (i
= 0; i
< MIN(p
->indent
, 4); i
++)
72 if ( ! md_buf_putstring(p
->mbuf
, " "))
81 mbuf_atnewline(struct md_valid
*p
)
84 return(p
->pos
== MIN(4, p
->indent
));
89 mbuf_newline(struct md_valid
*p
)
92 if (mbuf_atnewline(p
))
94 if ( ! md_buf_putchar(p
->mbuf
, '\n'))
98 return(mbuf_indent(p
));
103 mbuf_data(struct md_valid
*p
, char *buf
)
109 assert(0 != p
->indent
);
111 if (MD_LITERAL
& p
->flags
)
112 return(md_buf_putstring(p
->mbuf
, buf
));
118 * Indent if we're at the beginning of a line. Don't indent
119 * more than 16 or so characters.
123 while (*buf
&& isspace(*buf
))
130 while (*buf
&& ! isspace(*buf
))
140 if (sz
+ p
->pos
< 72) {
141 if ( ! md_buf_putstring(p
->mbuf
, bufp
))
144 /* FIXME: check punctuation. */
146 if ( ! md_buf_putchar(p
->mbuf
, ' '))
152 if ( ! mbuf_newline(p
))
155 if ( ! md_buf_putstring(p
->mbuf
, bufp
))
158 /* FIXME: check punctuation. */
160 if ( ! md_buf_putchar(p
->mbuf
, ' '))
170 md_line_valid(void *arg
, char *buf
)
174 p
= (struct md_valid
*)arg
;
175 return(roff_engine(p
->tree
, buf
));
180 md_exit_valid(void *data
, int flush
)
185 p
= (struct md_valid
*)data
;
186 c
= roff_free(p
->tree
, flush
);
194 md_init_valid(const struct md_args
*args
,
195 struct md_mbuf
*mbuf
, const struct md_rbuf
*rbuf
)
200 cb
.roffhead
= roffhead
;
201 cb
.rofftail
= rofftail
;
203 cb
.roffout
= roffout
;
204 cb
.roffblkin
= roffblkin
;
205 cb
.roffblkout
= roffblkout
;
206 cb
.roffspecial
= roffspecial
;
207 cb
.roffmsg
= roffmsg
;
208 cb
.roffdata
= roffdata
;
210 if (NULL
== (p
= calloc(1, sizeof(struct md_valid
))))
219 if (NULL
== (p
->tree
= roff_alloc(&cb
, p
))) {
243 p
= (struct md_valid
*)arg
;
245 if (mbuf_atnewline(p
))
248 return(md_buf_putchar(p
->mbuf
, '\n'));
253 roffspecial(void *arg
, int tok
)
261 roffblkin(void *arg
, int tok
)
266 p
= (struct md_valid
*)arg
;
268 if ( ! mbuf_atnewline(p
)) {
269 if ( ! md_buf_putchar(p
->mbuf
, '\n'))
272 if ( ! mbuf_indent(p
))
276 if ( ! md_buf_putstring(p
->mbuf
, toknames
[tok
]))
279 if ( ! md_buf_putchar(p
->mbuf
, '\n'))
285 return(mbuf_indent(p
));
290 roffblkout(void *arg
, int tok
)
295 p
= (struct md_valid
*)arg
;
297 if ( ! md_buf_putchar(p
->mbuf
, '\n'))
303 return(mbuf_indent(p
));
308 roffin(void *arg
, int tok
, int *argcp
, char **argvp
)
316 roffout(void *arg
, int tok
)
325 roffmsg(void *arg
, enum roffmsg lvl
,
326 const char *buf
, const char *pos
, char *msg
)
332 p
= (struct md_valid
*)arg
;
336 if ( ! (MD_WARN_ALL
& p
->args
->warnings
))
348 (void)fprintf(stderr
, "%s:%zu: %s: %s\n",
349 p
->rbuf
->name
, p
->rbuf
->line
, level
, msg
);
351 (void)fprintf(stderr
, "%s: %s: %s\n",
352 p
->rbuf
->name
, level
, msg
);
358 roffdata(void *arg
, char *buf
)
363 p
= (struct md_valid
*)arg
;
364 return(mbuf_data(p
, buf
));