]>
git.cameronkatri.com Git - mandoc.git/blob - libmdocml.c
1 /* $Id: libmdocml.c,v 1.3 2008/11/22 20:15:34 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
47 typedef int (*md_line
) (const struct md_args
*, struct md_mbuf
*,
48 const struct md_rbuf
*,
49 const char *, size_t);
50 typedef int (*md_init
) (const struct md_args
*, struct md_mbuf
*);
51 typedef int (*md_exit
) (const struct md_args
*, struct md_mbuf
*);
53 static int md_line_dummy(const struct md_args
*,
55 const struct md_rbuf
*,
56 const char *, size_t);
58 static int md_line_html4_strict(const struct md_args
*,
60 const struct md_rbuf
*,
61 const char *, size_t);
62 static int md_init_html4_strict(const struct md_args
*,
64 static int md_exit_html4_strict(const struct md_args
*,
67 static int md_run_enter(const struct md_args
*,
68 struct md_mbuf
*, struct md_rbuf
*);
69 static int md_run_leave(const struct md_args
*,
71 struct md_rbuf
*, int);
73 static ssize_t
md_buf_fill(struct md_rbuf
*);
74 static int md_buf_flush(struct md_mbuf
*);
75 static int md_buf_putchar(struct md_mbuf
*, char);
76 static int md_buf_puts(struct md_mbuf
*,
77 const char *, size_t);
81 md_buf_fill(struct md_rbuf
*in
)
87 assert(in
->bufsz
> 0);
90 if (-1 == (ssz
= read(in
->fd
, in
->buf
, in
->bufsz
)))
98 md_buf_flush(struct md_mbuf
*buf
)
109 sz
= write(buf
->fd
, buf
->buf
, buf
->pos
);
112 warn("%s", buf
->name
);
114 } else if ((size_t)sz
!= buf
->pos
) {
115 warnx("%s: short write", buf
->name
);
125 md_buf_putchar(struct md_mbuf
*buf
, char c
)
127 return(md_buf_puts(buf
, &c
, 1));
132 md_buf_puts(struct md_mbuf
*buf
, const char *p
, size_t sz
)
141 while (buf
->pos
+ sz
> buf
->bufsz
) {
142 ssz
= buf
->bufsz
- buf
->pos
;
143 (void)memcpy(/* LINTED */
144 buf
->buf
+ buf
->pos
, p
, ssz
);
149 if ( ! md_buf_flush(buf
))
153 (void)memcpy(/* LINTED */
154 buf
->buf
+ buf
->pos
, p
, sz
);
161 md_run_leave(const struct md_args
*args
,
162 struct md_mbuf
*mbuf
, struct md_rbuf
*rbuf
, int c
)
169 switch (args
->type
) {
170 case (MD_HTML4_STRICT
):
171 if ( ! md_exit_html4_strict(args
, mbuf
))
180 /* Make final flush of buffer. */
181 if ( ! md_buf_flush(mbuf
))
189 md_run_enter(const struct md_args
*args
,
190 struct md_mbuf
*mbuf
, struct md_rbuf
*rbuf
)
193 char line
[BUFFER_LINE
];
201 /* Function ptrs to line-parsers. */
202 switch (args
->type
) {
203 case (MD_HTML4_STRICT
):
204 fp
= md_line_html4_strict
;
215 if (-1 == (sz
= md_buf_fill(rbuf
)))
220 for (i
= 0; i
< sz
; i
++) {
221 if ('\n' == rbuf
->buf
[i
]) {
222 if ( ! (*fp
)(args
, mbuf
, rbuf
, line
, pos
))
229 if (pos
< BUFFER_LINE
) {
231 line
[pos
++] = rbuf
->buf
[i
];
235 warnx("%s: line %zu too long",
236 rbuf
->name
, rbuf
->line
);
241 if (0 != pos
&& ! (*fp
)(args
, mbuf
, rbuf
, line
, pos
))
244 return(md_run_leave(args
, mbuf
, rbuf
, 0));
249 md_run(const struct md_args
*args
,
250 const struct md_buf
*out
, const struct md_buf
*in
)
259 (void)memcpy(&mbuf
, out
, sizeof(struct md_buf
));
260 (void)memcpy(&rbuf
, in
, sizeof(struct md_buf
));
265 /* Run initialisers. */
266 switch (args
->type
) {
267 case (MD_HTML4_STRICT
):
268 if ( ! md_init_html4_strict(args
, &mbuf
))
277 /* Go into mainline. */
278 return(md_run_enter(args
, &mbuf
, &rbuf
));
283 md_line_dummy(const struct md_args
*args
, struct md_mbuf
*out
,
284 const struct md_rbuf
*in
, const char *buf
, size_t sz
)
292 if ( ! md_buf_puts(out
, buf
, sz
))
294 if ( ! md_buf_putchar(out
, '\n'))
302 md_exit_html4_strict(const struct md_args
*args
, struct md_mbuf
*p
)
312 md_init_html4_strict(const struct md_args
*args
, struct md_mbuf
*p
)
322 md_line_html4_strict(const struct md_args
*args
, struct md_mbuf
*out
,
323 const struct md_rbuf
*in
, const char *buf
, size_t sz
)