version 1.2, 2008/11/22 18:34:06 |
version 1.3, 2008/11/22 20:15:34 |
|
|
|
|
#define BUFFER_LINE BUFSIZ |
#define BUFFER_LINE BUFSIZ |
|
|
typedef int (*md_line) (struct md_mbuf *, const struct md_rbuf *, |
struct md_rbuf { |
|
int fd; |
|
char *name; |
|
char *buf; |
|
size_t bufsz; |
|
size_t line; |
|
}; |
|
|
|
struct md_mbuf { |
|
int fd; |
|
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); |
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(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 struct md_rbuf *, |
const char *, size_t); |
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 ssize_t md_buf_fill(struct md_rbuf *); |
static int md_buf_flush(struct md_mbuf *); |
static int md_buf_flush(struct md_mbuf *); |
static int md_buf_putchar(struct md_mbuf *, char); |
static int md_buf_putchar(struct md_mbuf *, char); |
Line 41 static int md_buf_puts(struct md_mbuf *, |
|
Line 77 static int md_buf_puts(struct md_mbuf *, |
|
const char *, size_t); |
const char *, size_t); |
|
|
|
|
ssize_t |
static ssize_t |
md_buf_fill(struct md_rbuf *in) |
md_buf_fill(struct md_rbuf *in) |
{ |
{ |
ssize_t ssz; |
ssize_t ssz; |
Line 58 md_buf_fill(struct md_rbuf *in) |
|
Line 94 md_buf_fill(struct md_rbuf *in) |
|
} |
} |
|
|
|
|
int |
static int |
md_buf_flush(struct md_mbuf *buf) |
md_buf_flush(struct md_mbuf *buf) |
{ |
{ |
ssize_t sz; |
ssize_t sz; |
Line 85 md_buf_flush(struct md_mbuf *buf) |
|
Line 121 md_buf_flush(struct md_mbuf *buf) |
|
} |
} |
|
|
|
|
int |
static 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)); |
} |
} |
|
|
|
|
int |
static 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 121 md_buf_puts(struct md_mbuf *buf, const char *p, size_t |
|
Line 157 md_buf_puts(struct md_mbuf *buf, const char *p, size_t |
|
} |
} |
|
|
|
|
int |
static int |
md_run(enum md_type type, struct md_mbuf *out, struct md_rbuf *in) |
md_run_leave(const struct md_args *args, |
|
struct md_mbuf *mbuf, struct md_rbuf *rbuf, int c) |
{ |
{ |
|
assert(args); |
|
assert(mbuf); |
|
assert(rbuf); |
|
|
|
/* Run exiters. */ |
|
switch (args->type) { |
|
case (MD_HTML4_STRICT): |
|
if ( ! md_exit_html4_strict(args, mbuf)) |
|
return(-1); |
|
break; |
|
case (MD_DUMMY): |
|
break; |
|
default: |
|
abort(); |
|
} |
|
|
|
/* Make final flush of buffer. */ |
|
if ( ! md_buf_flush(mbuf)) |
|
return(-1); |
|
|
|
return(c); |
|
} |
|
|
|
|
|
static int |
|
md_run_enter(const struct md_args *args, |
|
struct md_mbuf *mbuf, struct md_rbuf *rbuf) |
|
{ |
ssize_t sz, i; |
ssize_t sz, i; |
char line[BUFFER_LINE]; |
char line[BUFFER_LINE]; |
size_t pos; |
size_t pos; |
md_line func; |
md_line fp; |
|
|
assert(in); |
assert(args); |
assert(out); |
assert(mbuf); |
|
assert(rbuf); |
|
|
out->pos = 0; |
/* Function ptrs to line-parsers. */ |
in->line = 1; |
switch (args->type) { |
|
case (MD_HTML4_STRICT): |
|
fp = md_line_html4_strict; |
|
break; |
|
case (MD_DUMMY): |
|
fp = md_line_dummy; |
|
break; |
|
default: |
|
abort(); |
|
} |
|
|
assert(MD_DUMMY == type); |
|
func = md_line_dummy; |
|
|
|
/* LINTED */ |
/* LINTED */ |
for (pos = 0; ; ) { |
for (pos = 0; ; ) { |
if (-1 == (sz = md_buf_fill(in))) |
if (-1 == (sz = md_buf_fill(rbuf))) |
return(1); |
return(-1); |
else if (0 == sz) |
else if (0 == sz) |
break; |
break; |
|
|
for (i = 0; i < sz; i++) { |
for (i = 0; i < sz; i++) { |
if ('\n' == in->buf[i]) { |
if ('\n' == rbuf->buf[i]) { |
if ((*func)(out, in, line, pos)) |
if ( ! (*fp)(args, mbuf, rbuf, line, pos)) |
return(1); |
return(-1); |
in->line++; |
rbuf->line++; |
pos = 0; |
pos = 0; |
continue; |
continue; |
} |
} |
|
|
if (pos < BUFFER_LINE) { |
if (pos < BUFFER_LINE) { |
/* LINTED */ |
/* LINTED */ |
line[pos++] = in->buf[i]; |
line[pos++] = rbuf->buf[i]; |
continue; |
continue; |
} |
} |
|
|
warnx("%s: line %zu too long", |
warnx("%s: line %zu too long", |
in->name, in->line); |
rbuf->name, rbuf->line); |
return(1); |
return(-1); |
} |
} |
} |
} |
|
|
if (0 != pos && (*func)(out, in, line, pos)) |
if (0 != pos && ! (*fp)(args, mbuf, rbuf, line, pos)) |
return(1); |
return(-1); |
|
|
return(md_buf_flush(out) ? 0 : 1); |
return(md_run_leave(args, mbuf, rbuf, 0)); |
} |
} |
|
|
|
|
|
int |
|
md_run(const struct md_args *args, |
|
const struct md_buf *out, const struct md_buf *in) |
|
{ |
|
struct md_mbuf mbuf; |
|
struct md_rbuf rbuf; |
|
|
|
assert(args); |
|
assert(in); |
|
assert(out); |
|
|
|
(void)memcpy(&mbuf, out, sizeof(struct md_buf)); |
|
(void)memcpy(&rbuf, in, sizeof(struct md_buf)); |
|
|
|
mbuf.pos = 0; |
|
rbuf.line = 1; |
|
|
|
/* Run initialisers. */ |
|
switch (args->type) { |
|
case (MD_HTML4_STRICT): |
|
if ( ! md_init_html4_strict(args, &mbuf)) |
|
return(-1); |
|
break; |
|
case (MD_DUMMY): |
|
break; |
|
default: |
|
abort(); |
|
} |
|
|
|
/* Go into mainline. */ |
|
return(md_run_enter(args, &mbuf, &rbuf)); |
|
} |
|
|
|
|
static int |
static int |
md_line_dummy(struct md_mbuf *out, const struct md_rbuf *in, |
md_line_dummy(const struct md_args *args, struct md_mbuf *out, |
const char *buf, size_t sz) |
const struct md_rbuf *in, const char *buf, size_t sz) |
{ |
{ |
|
|
assert(buf); |
assert(buf); |
assert(out); |
assert(out); |
assert(in); |
assert(in); |
|
assert(args); |
|
|
if ( ! md_buf_puts(out, buf, sz)) |
if ( ! md_buf_puts(out, buf, sz)) |
return(1); |
return(0); |
if ( ! md_buf_putchar(out, '\n')) |
if ( ! md_buf_putchar(out, '\n')) |
return(1); |
return(0); |
|
|
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); |
|
} |