]>
git.cameronkatri.com Git - mandoc.git/blob - libmdocml.c
1 /* $Id: libmdocml.c,v 1.2 2008/11/22 18:34:06 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"
29 #define BUFFER_LINE BUFSIZ
31 typedef int (*md_line
) (struct md_mbuf
*, const struct md_rbuf
*,
32 const char *, size_t);
34 static int md_line_dummy(struct md_mbuf
*,
35 const struct md_rbuf
*,
36 const char *, size_t);
37 static ssize_t
md_buf_fill(struct md_rbuf
*);
38 static int md_buf_flush(struct md_mbuf
*);
39 static int md_buf_putchar(struct md_mbuf
*, char);
40 static int md_buf_puts(struct md_mbuf
*,
41 const char *, size_t);
45 md_buf_fill(struct md_rbuf
*in
)
51 assert(in
->bufsz
> 0);
54 if (-1 == (ssz
= read(in
->fd
, in
->buf
, in
->bufsz
)))
62 md_buf_flush(struct md_mbuf
*buf
)
73 sz
= write(buf
->fd
, buf
->buf
, buf
->pos
);
76 warn("%s", buf
->name
);
78 } else if ((size_t)sz
!= buf
->pos
) {
79 warnx("%s: short write", buf
->name
);
89 md_buf_putchar(struct md_mbuf
*buf
, char c
)
91 return(md_buf_puts(buf
, &c
, 1));
96 md_buf_puts(struct md_mbuf
*buf
, const char *p
, size_t sz
)
105 while (buf
->pos
+ sz
> buf
->bufsz
) {
106 ssz
= buf
->bufsz
- buf
->pos
;
107 (void)memcpy(/* LINTED */
108 buf
->buf
+ buf
->pos
, p
, ssz
);
113 if ( ! md_buf_flush(buf
))
117 (void)memcpy(/* LINTED */
118 buf
->buf
+ buf
->pos
, p
, sz
);
125 md_run(enum md_type type
, struct md_mbuf
*out
, struct md_rbuf
*in
)
128 char line
[BUFFER_LINE
];
138 assert(MD_DUMMY
== type
);
139 func
= md_line_dummy
;
143 if (-1 == (sz
= md_buf_fill(in
)))
148 for (i
= 0; i
< sz
; i
++) {
149 if ('\n' == in
->buf
[i
]) {
150 if ((*func
)(out
, in
, line
, pos
))
157 if (pos
< BUFFER_LINE
) {
159 line
[pos
++] = in
->buf
[i
];
163 warnx("%s: line %zu too long",
169 if (0 != pos
&& (*func
)(out
, in
, line
, pos
))
172 return(md_buf_flush(out
) ? 0 : 1);
177 md_line_dummy(struct md_mbuf
*out
, const struct md_rbuf
*in
,
178 const char *buf
, size_t sz
)
185 if ( ! md_buf_puts(out
, buf
, sz
))
187 if ( ! md_buf_putchar(out
, '\n'))