]>
git.cameronkatri.com Git - mandoc.git/blob - libmdocml.c
1 /* $Id: libmdocml.c,v 1.11 2008/11/27 16:54:58 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.
28 #include "libmdocml.h"
31 #define BUFFER_LINE BUFSIZ /* Default line-buffer size. */
33 static int md_run_enter(const struct md_args
*,
34 struct md_mbuf
*, struct md_rbuf
*, void *);
35 static int md_run_leave(const struct md_args
*, struct md_mbuf
*,
36 struct md_rbuf
*, int, void *);
38 static ssize_t
md_buf_fill(struct md_rbuf
*);
39 static int md_buf_flush(struct md_mbuf
*);
43 md_buf_fill(struct md_rbuf
*in
)
49 assert(in
->bufsz
> 0);
52 if (-1 == (ssz
= read(in
->fd
, in
->buf
, in
->bufsz
)))
60 md_buf_flush(struct md_mbuf
*buf
)
71 sz
= write(buf
->fd
, buf
->buf
, buf
->pos
);
74 warn("%s", buf
->name
);
76 } else if ((size_t)sz
!= buf
->pos
) {
77 warnx("%s: short write", buf
->name
);
87 md_buf_putchar(struct md_mbuf
*buf
, char c
)
89 return(md_buf_puts(buf
, &c
, 1));
94 md_buf_putstring(struct md_mbuf
*buf
, const char *p
)
96 return(md_buf_puts(buf
, p
, strlen(p
)));
101 md_buf_puts(struct md_mbuf
*buf
, const char *p
, size_t sz
)
110 while (buf
->pos
+ sz
> buf
->bufsz
) {
111 ssz
= buf
->bufsz
- buf
->pos
;
112 (void)memcpy(/* LINTED */
113 buf
->buf
+ buf
->pos
, p
, ssz
);
118 if ( ! md_buf_flush(buf
))
122 (void)memcpy(/* LINTED */
123 buf
->buf
+ buf
->pos
, p
, sz
);
130 md_run_leave(const struct md_args
*args
, struct md_mbuf
*mbuf
,
131 struct md_rbuf
*rbuf
, int c
, void *data
)
138 switch (args
->type
) {
139 case (MD_HTML4_STRICT
):
140 if ( ! md_exit_html4_strict(data
, -1 == c
? 0 : 1))
144 if ( ! md_exit_dummy(data
, -1 == c
? 0 : 1))
149 /* Make final flush of buffer. */
150 if ( ! md_buf_flush(mbuf
))
158 md_run_enter(const struct md_args
*args
, struct md_mbuf
*mbuf
,
159 struct md_rbuf
*rbuf
, void *p
)
162 char line
[BUFFER_LINE
];
170 /* Function ptrs to line-parsers. */
171 switch (args
->type
) {
172 case (MD_HTML4_STRICT
):
173 fp
= md_line_html4_strict
;
183 if (-1 == (sz
= md_buf_fill(rbuf
))) {
184 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
185 } else if (0 == sz
&& 0 != pos
) {
186 warnx("%s: no newline at end of file", rbuf
->name
);
187 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
189 return(md_run_leave(args
, mbuf
, rbuf
, 0, p
));
191 for (i
= 0; i
< sz
; i
++) {
193 if ( ! isascii(rbuf->buf[i])) {
194 warnx("%s: non-ascii char (line %zu, col %zu)",
195 rbuf->name, rbuf->line, pos);
196 return(md_run_leave(args, mbuf, rbuf, -1, p));
199 if ('\n' != rbuf
->buf
[i
]) {
200 if (pos
< BUFFER_LINE
) {
202 line
[pos
++] = rbuf
->buf
[i
];
205 warnx("%s: line %zu too long",
206 rbuf
->name
, rbuf
->line
);
207 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
211 if ( ! (*fp
)(p
, line
, pos
))
212 return(md_run_leave(args
, mbuf
, rbuf
, -1, p
));
223 md_run(const struct md_args
*args
,
224 const struct md_buf
*out
, const struct md_buf
*in
)
234 (void)memcpy(&mbuf
, out
, sizeof(struct md_buf
));
235 (void)memcpy(&rbuf
, in
, sizeof(struct md_buf
));
240 /* Run initialisers. */
241 switch (args
->type
) {
242 case (MD_HTML4_STRICT
):
243 data
= md_init_html4_strict
244 (args
, &mbuf
, &rbuf
);
248 (args
, &mbuf
, &rbuf
);
252 /* Go into mainline. */
253 return(md_run_enter(args
, &mbuf
, &rbuf
, data
));