]>
git.cameronkatri.com Git - mandoc.git/blob - libmdocml.c
1 /* $Id: libmdocml.c,v 1.6 2008/11/23 22:30:53 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.
27 #include "libmdocml.h"
30 #define BUFFER_LINE BUFSIZ
32 static int md_run_enter(const struct md_args
*,
33 struct md_mbuf
*, struct md_rbuf
*, void *);
34 static int md_run_leave(const struct md_args
*, struct md_mbuf
*,
35 struct md_rbuf
*, int, void *);
36 static ssize_t
md_buf_fill(struct md_rbuf
*);
37 static int md_buf_flush(struct md_mbuf
*);
41 md_buf_fill(struct md_rbuf
*in
)
47 assert(in
->bufsz
> 0);
50 if (-1 == (ssz
= read(in
->fd
, in
->buf
, in
->bufsz
)))
58 md_buf_flush(struct md_mbuf
*buf
)
69 sz
= write(buf
->fd
, buf
->buf
, buf
->pos
);
72 warn("%s", buf
->name
);
74 } else if ((size_t)sz
!= buf
->pos
) {
75 warnx("%s: short write", buf
->name
);
85 md_buf_putchar(struct md_mbuf
*buf
, char c
)
87 return(md_buf_puts(buf
, &c
, 1));
92 md_buf_putstring(struct md_mbuf
*buf
, const char *p
)
94 return(md_buf_puts(buf
, p
, strlen(p
)));
99 md_buf_puts(struct md_mbuf
*buf
, const char *p
, size_t sz
)
108 while (buf
->pos
+ sz
> buf
->bufsz
) {
109 ssz
= buf
->bufsz
- buf
->pos
;
110 (void)memcpy(/* LINTED */
111 buf
->buf
+ buf
->pos
, p
, ssz
);
116 if ( ! md_buf_flush(buf
))
120 (void)memcpy(/* LINTED */
121 buf
->buf
+ buf
->pos
, p
, sz
);
128 md_run_leave(const struct md_args
*args
, struct md_mbuf
*mbuf
,
129 struct md_rbuf
*rbuf
, int c
, void *data
)
136 switch (args
->type
) {
137 case (MD_HTML4_STRICT
):
138 if ( ! md_exit_html4_strict(args
, mbuf
, rbuf
, c
, data
))
147 /* Make final flush of buffer. */
148 if ( ! md_buf_flush(mbuf
))
156 md_run_enter(const struct md_args
*args
, struct md_mbuf
*mbuf
,
157 struct md_rbuf
*rbuf
, void *p
)
160 char line
[BUFFER_LINE
];
168 /* Function ptrs to line-parsers. */
169 switch (args
->type
) {
170 case (MD_HTML4_STRICT
):
171 fp
= md_line_html4_strict
;
182 if (-1 == (sz
= md_buf_fill(rbuf
)))
183 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
187 for (i
= 0; i
< sz
; i
++) {
188 if ('\n' == rbuf
->buf
[i
]) {
189 if ( ! (*fp
)(args
, mbuf
, rbuf
, line
, pos
, p
))
190 return(md_run_leave(args
, mbuf
, rbuf
,
197 if (pos
< BUFFER_LINE
) {
199 line
[pos
++] = rbuf
->buf
[i
];
203 warnx("%s: line %zu too long",
204 rbuf
->name
, rbuf
->line
);
205 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
209 if (0 != pos
&& ! (*fp
)(args
, mbuf
, rbuf
, line
, pos
, p
))
210 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
212 return(md_run_leave(args
, mbuf
, rbuf
, 0, p
));
217 md_run(const struct md_args
*args
,
218 const struct md_buf
*out
, const struct md_buf
*in
)
228 (void)memcpy(&mbuf
, out
, sizeof(struct md_buf
));
229 (void)memcpy(&rbuf
, in
, sizeof(struct md_buf
));
235 /* Run initialisers. */
236 switch (args
->type
) {
237 case (MD_HTML4_STRICT
):
238 if ( ! md_init_html4_strict(args
, &mbuf
, &rbuf
, &data
))
247 /* Go into mainline. */
248 return(md_run_enter(args
, &mbuf
, &rbuf
, data
));