version 1.2, 2008/11/22 16:55:02 |
version 1.3, 2008/11/22 17:14:32 |
|
|
|
|
#include "libmdocml.h" |
#include "libmdocml.h" |
|
|
struct md_file { |
#define BUFFER_IN_DEF BUFSIZ |
|
#define BUFFER_OUT_DEF BUFSIZ |
|
#define BUFFER_LINE BUFSIZ |
|
|
|
struct md_rbuf { |
int fd; |
int fd; |
const char *name; |
const char *name; |
}; |
|
|
|
struct md_buf { |
|
struct md_file *file; |
|
char *buf; |
char *buf; |
size_t bufsz; |
size_t bufsz; |
size_t line; |
size_t line; |
}; |
}; |
|
|
struct md_mbuf { |
struct md_mbuf { |
struct md_buf *buf; |
int fd; |
|
const char *name; |
|
char *buf; |
|
size_t bufsz; |
size_t pos; |
size_t pos; |
}; |
}; |
|
|
Line 51 static void usage(void); |
|
Line 54 static void usage(void); |
|
|
|
static int md_begin(const char *, const char *); |
static int md_begin(const char *, const char *); |
static int md_begin_io(const char *, const char *); |
static int md_begin_io(const char *, const char *); |
static int md_begin_bufs(struct md_file *, struct md_file *); |
static int md_begin_bufs(struct md_mbuf *, struct md_rbuf *); |
static int md_run(struct md_buf *, struct md_buf *); |
static int md_run(struct md_mbuf *, struct md_rbuf *); |
static int md_line(struct md_mbuf *, const struct md_buf *, |
static int md_line(struct md_mbuf *, const struct md_rbuf *, |
const char *, size_t); |
const char *, size_t); |
|
|
static ssize_t md_buf_fill(struct md_buf *); |
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); |
|
|
md_begin_io(const char *out, const char *in) |
md_begin_io(const char *out, const char *in) |
{ |
{ |
int c; |
int c; |
struct md_file fin, fout; |
struct md_rbuf fin; |
|
struct md_mbuf fout; |
|
|
assert(out); |
assert(out); |
assert(in); |
assert(in); |
Line 162 md_begin_io(const char *out, const char *in) |
|
Line 166 md_begin_io(const char *out, const char *in) |
|
|
|
|
|
static int |
static int |
md_begin_bufs(struct md_file *out, struct md_file *in) |
md_begin_bufs(struct md_mbuf *out, struct md_rbuf *in) |
{ |
{ |
struct stat stin, stout; |
struct stat stin, stout; |
struct md_buf inbuf, outbuf; |
|
int c; |
int c; |
|
|
assert(in); |
assert(in); |
assert(out); |
assert(out); |
|
|
if (-1 == fstat(in->fd, &stin)) { |
if (-1 == fstat(in->fd, &stin)) { |
warn("fstat: %s", in->name); |
warn("%s", in->name); |
return(1); |
return(1); |
} else if (-1 == fstat(out->fd, &stout)) { |
} else if (-1 == fstat(out->fd, &stout)) { |
warn("fstat: %s", out->name); |
warn("%s", out->name); |
return(1); |
return(1); |
} |
} |
|
|
inbuf.file = in; |
in->bufsz = MAX(stin.st_blksize, BUFFER_IN_DEF); |
inbuf.line = 1; |
|
/*inbuf.bufsz = MAX(stin.st_blksize, BUFSIZ);*/ |
|
inbuf.bufsz = 256; |
|
|
|
outbuf.file = out; |
out->bufsz = MAX(stout.st_blksize, BUFFER_OUT_DEF); |
outbuf.line = 1; |
|
/*outbuf.bufsz = MAX(stout.st_blksize, BUFSIZ);*/ |
|
outbuf.bufsz = 256; |
|
|
|
if (NULL == (inbuf.buf = malloc(inbuf.bufsz))) { |
if (NULL == (in->buf = malloc(in->bufsz))) { |
warn("malloc"); |
warn("malloc"); |
return(1); |
return(1); |
} else if (NULL == (outbuf.buf = malloc(outbuf.bufsz))) { |
} else if (NULL == (out->buf = malloc(out->bufsz))) { |
warn("malloc"); |
warn("malloc"); |
free(inbuf.buf); |
free(in->buf); |
return(1); |
return(1); |
} |
} |
|
|
c = md_run(&outbuf, &inbuf); |
c = md_run(out, in); |
|
|
free(inbuf.buf); |
free(in->buf); |
free(outbuf.buf); |
free(out->buf); |
|
|
return(c); |
return(c); |
} |
} |
|
|
|
|
static ssize_t |
static ssize_t |
md_buf_fill(struct md_buf *in) |
md_buf_fill(struct md_rbuf *in) |
{ |
{ |
ssize_t ssz; |
ssize_t ssz; |
|
|
assert(in); |
assert(in); |
assert(in->file); |
|
assert(in->buf); |
assert(in->buf); |
assert(in->bufsz > 0); |
assert(in->bufsz > 0); |
assert(in->file->name); |
assert(in->name); |
|
|
if (-1 == (ssz = read(in->file->fd, in->buf, in->bufsz))) |
if (-1 == (ssz = read(in->fd, in->buf, in->bufsz))) |
warn("%s", in->file->name); |
warn("%s", in->name); |
else |
|
(void)printf("%s: filled %zd bytes\n", |
|
in->file->name, ssz); |
|
|
|
return(ssz); |
return(ssz); |
} |
} |
|
|
|
|
static int |
static int |
md_run(struct md_buf *out, struct md_buf *in) |
md_run(struct md_mbuf *out, struct md_rbuf *in) |
{ |
{ |
struct md_mbuf mbuf; |
|
ssize_t sz, i; |
ssize_t sz, i; |
char line[BUFSIZ]; |
char line[BUFFER_LINE]; |
size_t pos; |
size_t pos; |
|
|
assert(in); |
assert(in); |
assert(out); |
assert(out); |
|
|
mbuf.buf = out; |
out->pos = 0; |
mbuf.pos = 0; |
in->line = 1; |
|
|
/* LINTED */ |
/* LINTED */ |
for (pos = 0; ; ) { |
for (pos = 0; ; ) { |
Line 251 md_run(struct md_buf *out, struct md_buf *in) |
|
Line 243 md_run(struct md_buf *out, struct md_buf *in) |
|
|
|
for (i = 0; i < sz; i++) { |
for (i = 0; i < sz; i++) { |
if ('\n' == in->buf[i]) { |
if ('\n' == in->buf[i]) { |
if (md_line(&mbuf, in, line, pos)) |
if (md_line(out, in, line, pos)) |
return(1); |
return(1); |
in->line++; |
in->line++; |
pos = 0; |
pos = 0; |
continue; |
continue; |
} |
} |
|
|
if (pos < BUFSIZ) { |
if (pos < BUFFER_LINE) { |
/* LINTED */ |
/* LINTED */ |
line[pos++] = in->buf[i]; |
line[pos++] = in->buf[i]; |
continue; |
continue; |
} |
} |
|
|
warnx("%s: line %zu too long", |
warnx("%s: line %zu too long", |
in->file->name, in->line); |
in->name, in->line); |
return(1); |
return(1); |
} |
} |
} |
} |
|
|
if (0 != pos && md_line(&mbuf, in, line, pos)) |
if (0 != pos && md_line(out, in, line, pos)) |
return(1); |
return(1); |
|
|
return(md_buf_flush(&mbuf) ? 0 : 1); |
return(md_buf_flush(out) ? 0 : 1); |
} |
} |
|
|
|
|
Line 284 md_buf_flush(struct md_mbuf *buf) |
|
Line 276 md_buf_flush(struct md_mbuf *buf) |
|
|
|
assert(buf); |
assert(buf); |
assert(buf->buf); |
assert(buf->buf); |
assert(buf->buf->file); |
assert(buf->name); |
assert(buf->buf->buf); |
|
assert(buf->buf->file->name); |
|
|
|
(void)printf("%s: flushing %zu bytes\n", |
|
buf->buf->file->name, buf->pos); |
|
|
|
if (0 == buf->pos) |
if (0 == buf->pos) |
return(1); |
return(1); |
|
|
sz = write(buf->buf->file->fd, buf->buf->buf, buf->pos); |
sz = write(buf->fd, buf->buf, buf->pos); |
|
|
if (-1 == sz) { |
if (-1 == sz) { |
warn("%s", buf->buf->file->name); |
warn("%s", buf->name); |
return(0); |
return(0); |
} else if ((size_t)sz != buf->pos) { |
} else if ((size_t)sz != buf->pos) { |
warnx("%s: short write", buf->buf->file->name); |
warnx("%s: short write", buf->name); |
return(0); |
return(0); |
} |
} |
|
|
Line 325 md_buf_puts(struct md_mbuf *buf, const char *p, size_t |
|
Line 312 md_buf_puts(struct md_mbuf *buf, const char *p, size_t |
|
assert(buf); |
assert(buf); |
assert(buf->buf); |
assert(buf->buf); |
|
|
while (buf->pos + sz > buf->buf->bufsz) { |
while (buf->pos + sz > buf->bufsz) { |
ssz = buf->buf->bufsz - buf->pos; |
ssz = buf->bufsz - buf->pos; |
(void)memcpy(buf->buf->buf + buf->pos, p, ssz); |
(void)memcpy(buf->buf + buf->pos, p, ssz); |
p += ssz; |
p += ssz; |
sz -= ssz; |
sz -= ssz; |
buf->pos += ssz; |
buf->pos += ssz; |
Line 336 md_buf_puts(struct md_mbuf *buf, const char *p, size_t |
|
Line 323 md_buf_puts(struct md_mbuf *buf, const char *p, size_t |
|
return(0); |
return(0); |
} |
} |
|
|
(void)memcpy(buf->buf->buf + buf->pos, p, sz); |
(void)memcpy(buf->buf + buf->pos, p, sz); |
buf->pos += sz; |
buf->pos += sz; |
return(1); |
return(1); |
} |
} |
|
|
|
|
static int |
static int |
md_line(struct md_mbuf *out, const struct md_buf *in, |
md_line(struct md_mbuf *out, const struct md_rbuf *in, |
const char *buf, size_t sz) |
const char *buf, size_t sz) |
{ |
{ |
|
|
|
/* FIXME: this is just a placeholder function. */ |
|
|
assert(buf); |
assert(buf); |
assert(out); |
assert(out); |