]>
git.cameronkatri.com Git - mandoc.git/blob - libmdocml.c
42e795841084f0e2aad0a419a751dba3007d2f5f
1 /* $Id: libmdocml.c,v 1.9 2008/11/24 14:24:55 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 /* Default line-buffer size. */
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 *);
37 static ssize_t
md_buf_fill(struct md_rbuf
*);
38 static int md_buf_flush(struct md_mbuf
*);
42 md_buf_fill(struct md_rbuf
*in
)
48 assert(in
->bufsz
> 0);
51 if (-1 == (ssz
= read(in
->fd
, in
->buf
, in
->bufsz
)))
59 md_buf_flush(struct md_mbuf
*buf
)
70 sz
= write(buf
->fd
, buf
->buf
, buf
->pos
);
73 warn("%s", buf
->name
);
75 } else if ((size_t)sz
!= buf
->pos
) {
76 warnx("%s: short write", buf
->name
);
86 md_buf_putchar(struct md_mbuf
*buf
, char c
)
88 return(md_buf_puts(buf
, &c
, 1));
93 md_buf_putstring(struct md_mbuf
*buf
, const char *p
)
95 return(md_buf_puts(buf
, p
, strlen(p
)));
100 md_buf_puts(struct md_mbuf
*buf
, const char *p
, size_t sz
)
109 while (buf
->pos
+ sz
> buf
->bufsz
) {
110 ssz
= buf
->bufsz
- buf
->pos
;
111 (void)memcpy(/* LINTED */
112 buf
->buf
+ buf
->pos
, p
, ssz
);
117 if ( ! md_buf_flush(buf
))
121 (void)memcpy(/* LINTED */
122 buf
->buf
+ buf
->pos
, p
, sz
);
129 md_run_leave(const struct md_args
*args
, struct md_mbuf
*mbuf
,
130 struct md_rbuf
*rbuf
, int c
, void *data
)
137 switch (args
->type
) {
138 case (MD_HTML4_STRICT
):
139 if ( ! md_exit_html4_strict(data
, -1 == c
? 0 : 1))
143 if ( ! md_exit_dummy(data
, -1 == c
? 0 : 1))
148 /* Make final flush of buffer. */
149 if ( ! md_buf_flush(mbuf
))
157 md_run_enter(const struct md_args
*args
, struct md_mbuf
*mbuf
,
158 struct md_rbuf
*rbuf
, void *p
)
161 char line
[BUFFER_LINE
];
169 /* Function ptrs to line-parsers. */
170 switch (args
->type
) {
171 case (MD_HTML4_STRICT
):
172 fp
= md_line_html4_strict
;
182 if (-1 == (sz
= md_buf_fill(rbuf
))) {
183 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
184 } else if (0 == sz
&& 0 != pos
) {
185 warnx("%s: no newline at end of file", rbuf
->name
);
186 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
188 return(md_run_leave(args
, mbuf
, rbuf
, 0, p
));
190 for (i
= 0; i
< sz
; i
++) {
191 if ('\n' != rbuf
->buf
[i
]) {
192 if (pos
< BUFFER_LINE
) {
194 line
[pos
++] = rbuf
->buf
[i
];
197 warnx("%s: line %zu too long",
198 rbuf
->name
, rbuf
->line
);
199 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
203 if ( ! (*fp
)(p
, line
, pos
))
204 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
215 md_run(const struct md_args
*args
,
216 const struct md_buf
*out
, const struct md_buf
*in
)
226 (void)memcpy(&mbuf
, out
, sizeof(struct md_buf
));
227 (void)memcpy(&rbuf
, in
, sizeof(struct md_buf
));
232 /* Run initialisers. */
233 switch (args
->type
) {
234 case (MD_HTML4_STRICT
):
235 data
= md_init_html4_strict
236 (args
, &mbuf
, &rbuf
);
240 (args
, &mbuf
, &rbuf
);
244 /* Go into mainline. */
245 return(md_run_enter(args
, &mbuf
, &rbuf
, data
));