version 1.3, 2008/11/22 20:15:34 |
version 1.7, 2008/11/23 23:12:47 |
|
|
#include <unistd.h> |
#include <unistd.h> |
|
|
#include "libmdocml.h" |
#include "libmdocml.h" |
|
#include "private.h" |
|
|
#define BUFFER_LINE BUFSIZ |
#define BUFFER_LINE BUFSIZ /* Default line-buffer size. */ |
|
|
struct md_rbuf { |
static int md_run_enter(const struct md_args *, |
int fd; |
struct md_mbuf *, struct md_rbuf *, void *); |
char *name; |
static int md_run_leave(const struct md_args *, struct md_mbuf *, |
char *buf; |
struct md_rbuf *, int, void *); |
size_t bufsz; |
|
size_t line; |
|
}; |
|
|
|
struct md_mbuf { |
static ssize_t md_buf_fill(struct md_rbuf *); |
int fd; |
static int md_buf_flush(struct md_mbuf *); |
char *name; |
|
char *buf; |
|
size_t bufsz; |
|
size_t pos; |
|
}; |
|
|
|
typedef int (*md_line) (const struct md_args *, struct md_mbuf *, |
|
const struct md_rbuf *, |
|
const char *, size_t); |
|
typedef int (*md_init) (const struct md_args *, struct md_mbuf *); |
|
typedef int (*md_exit) (const struct md_args *, struct md_mbuf *); |
|
|
|
static int md_line_dummy(const struct md_args *, |
|
struct md_mbuf *, |
|
const struct md_rbuf *, |
|
const char *, size_t); |
|
|
|
static int md_line_html4_strict(const struct md_args *, |
|
struct md_mbuf *, |
|
const struct md_rbuf *, |
|
const char *, size_t); |
|
static int md_init_html4_strict(const struct md_args *, |
|
struct md_mbuf *); |
|
static int md_exit_html4_strict(const struct md_args *, |
|
struct md_mbuf *); |
|
|
|
static int md_run_enter(const struct md_args *, |
|
struct md_mbuf *, struct md_rbuf *); |
|
static int md_run_leave(const struct md_args *, |
|
struct md_mbuf *, |
|
struct md_rbuf *, int); |
|
|
|
static ssize_t md_buf_fill(struct md_rbuf *); |
|
static int md_buf_flush(struct md_mbuf *); |
|
static int md_buf_putchar(struct md_mbuf *, char); |
|
static int md_buf_puts(struct md_mbuf *, |
|
const char *, size_t); |
|
|
|
|
|
static ssize_t |
static ssize_t |
md_buf_fill(struct md_rbuf *in) |
md_buf_fill(struct md_rbuf *in) |
{ |
{ |
Line 121 md_buf_flush(struct md_mbuf *buf) |
|
Line 82 md_buf_flush(struct md_mbuf *buf) |
|
} |
} |
|
|
|
|
static int |
int |
md_buf_putchar(struct md_mbuf *buf, char c) |
md_buf_putchar(struct md_mbuf *buf, char c) |
{ |
{ |
return(md_buf_puts(buf, &c, 1)); |
return(md_buf_puts(buf, &c, 1)); |
} |
} |
|
|
|
|
static int |
int |
|
md_buf_putstring(struct md_mbuf *buf, const char *p) |
|
{ |
|
return(md_buf_puts(buf, p, strlen(p))); |
|
} |
|
|
|
|
|
int |
md_buf_puts(struct md_mbuf *buf, const char *p, size_t sz) |
md_buf_puts(struct md_mbuf *buf, const char *p, size_t sz) |
{ |
{ |
size_t ssz; |
size_t ssz; |
Line 158 md_buf_puts(struct md_mbuf *buf, const char *p, size_t |
|
Line 126 md_buf_puts(struct md_mbuf *buf, const char *p, size_t |
|
|
|
|
|
static int |
static int |
md_run_leave(const struct md_args *args, |
md_run_leave(const struct md_args *args, struct md_mbuf *mbuf, |
struct md_mbuf *mbuf, struct md_rbuf *rbuf, int c) |
struct md_rbuf *rbuf, int c, void *data) |
{ |
{ |
assert(args); |
assert(args); |
assert(mbuf); |
assert(mbuf); |
Line 168 md_run_leave(const struct md_args *args, |
|
Line 136 md_run_leave(const struct md_args *args, |
|
/* Run exiters. */ |
/* Run exiters. */ |
switch (args->type) { |
switch (args->type) { |
case (MD_HTML4_STRICT): |
case (MD_HTML4_STRICT): |
if ( ! md_exit_html4_strict(args, mbuf)) |
if ( ! md_exit_html4_strict(args, mbuf, rbuf, c, data)) |
return(-1); |
return(-1); |
break; |
break; |
case (MD_DUMMY): |
case (MD_DUMMY): |
Line 186 md_run_leave(const struct md_args *args, |
|
Line 154 md_run_leave(const struct md_args *args, |
|
|
|
|
|
static int |
static int |
md_run_enter(const struct md_args *args, |
md_run_enter(const struct md_args *args, struct md_mbuf *mbuf, |
struct md_mbuf *mbuf, struct md_rbuf *rbuf) |
struct md_rbuf *rbuf, void *p) |
{ |
{ |
ssize_t sz, i; |
ssize_t sz, i; |
char line[BUFFER_LINE]; |
char line[BUFFER_LINE]; |
Line 203 md_run_enter(const struct md_args *args, |
|
Line 171 md_run_enter(const struct md_args *args, |
|
case (MD_HTML4_STRICT): |
case (MD_HTML4_STRICT): |
fp = md_line_html4_strict; |
fp = md_line_html4_strict; |
break; |
break; |
case (MD_DUMMY): |
default: |
fp = md_line_dummy; |
fp = md_line_dummy; |
break; |
break; |
default: |
|
abort(); |
|
} |
} |
|
|
/* LINTED */ |
pos = 0; |
for (pos = 0; ; ) { |
|
if (-1 == (sz = md_buf_fill(rbuf))) |
|
return(-1); |
|
else if (0 == sz) |
|
break; |
|
|
|
for (i = 0; i < sz; i++) { |
again: |
if ('\n' == rbuf->buf[i]) { |
if (-1 == (sz = md_buf_fill(rbuf))) { |
if ( ! (*fp)(args, mbuf, rbuf, line, pos)) |
return(md_run_leave(args, mbuf, rbuf, -1, p)); |
return(-1); |
} else if (0 == sz && 0 != pos) { |
rbuf->line++; |
warnx("%s: no newline at end of file", rbuf->name); |
pos = 0; |
return(md_run_leave(args, mbuf, rbuf, -1, p)); |
continue; |
} else if (0 == sz) |
} |
return(md_run_leave(args, mbuf, rbuf, 0, p)); |
|
|
|
for (i = 0; i < sz; i++) { |
|
if ('\n' != rbuf->buf[i]) { |
if (pos < BUFFER_LINE) { |
if (pos < BUFFER_LINE) { |
/* LINTED */ |
/* LINTED */ |
line[pos++] = rbuf->buf[i]; |
line[pos++] = rbuf->buf[i]; |
continue; |
continue; |
} |
} |
|
|
warnx("%s: line %zu too long", |
warnx("%s: line %zu too long", |
rbuf->name, rbuf->line); |
rbuf->name, rbuf->line); |
return(-1); |
return(md_run_leave(args, mbuf, rbuf, -1, p)); |
} |
} |
|
|
|
if ( ! (*fp)(args, mbuf, rbuf, line, pos, p)) |
|
return(md_run_leave(args, mbuf, rbuf, -1, p)); |
|
rbuf->line++; |
|
pos = 0; |
} |
} |
|
|
if (0 != pos && ! (*fp)(args, mbuf, rbuf, line, pos)) |
goto again; |
return(-1); |
/* NOTREACHED */ |
|
|
return(md_run_leave(args, mbuf, rbuf, 0)); |
|
} |
} |
|
|
|
|
Line 251 md_run(const struct md_args *args, |
|
Line 216 md_run(const struct md_args *args, |
|
{ |
{ |
struct md_mbuf mbuf; |
struct md_mbuf mbuf; |
struct md_rbuf rbuf; |
struct md_rbuf rbuf; |
|
void *data; |
|
|
assert(args); |
assert(args); |
assert(in); |
assert(in); |
Line 261 md_run(const struct md_args *args, |
|
Line 227 md_run(const struct md_args *args, |
|
|
|
mbuf.pos = 0; |
mbuf.pos = 0; |
rbuf.line = 1; |
rbuf.line = 1; |
|
data = NULL; |
|
|
/* Run initialisers. */ |
/* Run initialisers. */ |
switch (args->type) { |
switch (args->type) { |
case (MD_HTML4_STRICT): |
case (MD_HTML4_STRICT): |
if ( ! md_init_html4_strict(args, &mbuf)) |
if ( ! md_init_html4_strict(args, &mbuf, &rbuf, &data)) |
return(-1); |
return(-1); |
break; |
break; |
case (MD_DUMMY): |
case (MD_DUMMY): |
Line 275 md_run(const struct md_args *args, |
|
Line 242 md_run(const struct md_args *args, |
|
} |
} |
|
|
/* Go into mainline. */ |
/* Go into mainline. */ |
return(md_run_enter(args, &mbuf, &rbuf)); |
return(md_run_enter(args, &mbuf, &rbuf, data)); |
} |
|
|
|
|
|
static int |
|
md_line_dummy(const struct md_args *args, struct md_mbuf *out, |
|
const struct md_rbuf *in, const char *buf, size_t sz) |
|
{ |
|
|
|
assert(buf); |
|
assert(out); |
|
assert(in); |
|
assert(args); |
|
|
|
if ( ! md_buf_puts(out, buf, sz)) |
|
return(0); |
|
if ( ! md_buf_putchar(out, '\n')) |
|
return(0); |
|
|
|
return(1); |
|
} |
|
|
|
|
|
static int |
|
md_exit_html4_strict(const struct md_args *args, struct md_mbuf *p) |
|
{ |
|
|
|
assert(p); |
|
assert(args); |
|
return(1); |
|
} |
|
|
|
|
|
static int |
|
md_init_html4_strict(const struct md_args *args, struct md_mbuf *p) |
|
{ |
|
|
|
assert(p); |
|
assert(args); |
|
return(1); |
|
} |
|
|
|
|
|
static int |
|
md_line_html4_strict(const struct md_args *args, struct md_mbuf *out, |
|
const struct md_rbuf *in, const char *buf, size_t sz) |
|
{ |
|
|
|
assert(args); |
|
assert(buf); |
|
assert(out); |
|
assert(in); |
|
(void)sz; |
|
|
|
return(1); |
|
} |
} |