version 1.1, 2008/11/22 14:53:29 |
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; |
|
char *buf; |
|
size_t bufsz; |
|
size_t line; |
}; |
}; |
|
|
struct md_buf { |
struct md_mbuf { |
struct md_file *file; |
int fd; |
|
const char *name; |
char *buf; |
char *buf; |
size_t bufsz; |
size_t bufsz; |
size_t line; |
size_t pos; |
}; |
}; |
|
|
static void usage(void); |
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_buf *, 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_putchar(struct md_mbuf *, char); |
|
static int md_buf_puts(struct md_mbuf *, |
|
const char *, size_t); |
|
|
|
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
|
|
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); |
|
|
/* XXX: put an output file in TMPDIR and switch it out when the |
/* TODO: accept "-" as both input and output. */ |
* true file is ready to be written. |
|
*/ |
|
|
|
fin.name = in; |
fin.name = in; |
|
|
Line 130 md_begin_io(const char *out, const char *in) |
|
Line 142 md_begin_io(const char *out, const char *in) |
|
|
|
fout.name = out; |
fout.name = out; |
|
|
fout.fd = open(fout.name, O_WRONLY | O_CREAT | O_EXCL, 0644); |
fout.fd = open(fout.name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
if (-1 == fout.fd) { |
if (-1 == fout.fd) { |
warn("%s", fout.name); |
warn("%s", fout.name); |
if (-1 == close(fin.fd)) |
if (-1 == close(fin.fd)) |
Line 154 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); |
|
|
|
outbuf.file = out; |
out->bufsz = MAX(stout.st_blksize, BUFFER_OUT_DEF); |
outbuf.line = 1; |
|
outbuf.bufsz = MAX(stout.st_blksize, BUFSIZ); |
|
|
|
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); |
|
|
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) |
{ |
{ |
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); |
|
|
|
out->pos = 0; |
|
in->line = 1; |
|
|
/* LINTED */ |
/* LINTED */ |
for (pos = 0; ; ) { |
for (pos = 0; ; ) { |
if (-1 == (sz = md_buf_fill(in))) |
if (-1 == (sz = md_buf_fill(in))) |
Line 241 md_run(struct md_buf *out, struct md_buf *in) |
|
Line 250 md_run(struct md_buf *out, struct md_buf *in) |
|
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) |
if (0 != pos && md_line(out, in, line, pos)) |
return(md_line(out, in, line, pos)); |
return(1); |
|
|
return(0); |
return(md_buf_flush(out) ? 0 : 1); |
} |
} |
|
|
|
|
static int |
static int |
md_line(struct md_buf *out, const struct md_buf *in, |
md_buf_flush(struct md_mbuf *buf) |
|
{ |
|
ssize_t sz; |
|
|
|
assert(buf); |
|
assert(buf->buf); |
|
assert(buf->name); |
|
|
|
if (0 == buf->pos) |
|
return(1); |
|
|
|
sz = write(buf->fd, buf->buf, buf->pos); |
|
|
|
if (-1 == sz) { |
|
warn("%s", buf->name); |
|
return(0); |
|
} else if ((size_t)sz != buf->pos) { |
|
warnx("%s: short write", buf->name); |
|
return(0); |
|
} |
|
|
|
buf->pos = 0; |
|
return(1); |
|
} |
|
|
|
|
|
static int |
|
md_buf_putchar(struct md_mbuf *buf, char c) |
|
{ |
|
return(md_buf_puts(buf, &c, 1)); |
|
} |
|
|
|
|
|
static int |
|
md_buf_puts(struct md_mbuf *buf, const char *p, size_t sz) |
|
{ |
|
size_t ssz; |
|
|
|
assert(p); |
|
assert(buf); |
|
assert(buf->buf); |
|
|
|
while (buf->pos + sz > buf->bufsz) { |
|
ssz = buf->bufsz - buf->pos; |
|
(void)memcpy(buf->buf + buf->pos, p, ssz); |
|
p += ssz; |
|
sz -= ssz; |
|
buf->pos += ssz; |
|
|
|
if ( ! md_buf_flush(buf)) |
|
return(0); |
|
} |
|
|
|
(void)memcpy(buf->buf + buf->pos, p, sz); |
|
buf->pos += sz; |
|
return(1); |
|
} |
|
|
|
|
|
static int |
|
md_line(struct md_mbuf *out, const struct md_rbuf *in, |
const char *buf, size_t sz) |
const char *buf, size_t sz) |
{ |
{ |
size_t i; |
|
|
|
|
/* FIXME: this is just a placeholder function. */ |
|
|
assert(buf); |
assert(buf); |
assert(out); |
assert(out); |
assert(in); |
assert(in); |
|
|
for (i = 0; i < sz; i++) |
if ( ! md_buf_puts(out, buf, sz)) |
(void)putchar(buf[i]); |
return(1); |
|
if ( ! md_buf_putchar(out, '\n')) |
|
return(1); |
|
|
(void)putchar('\n'); |
|
return(0); |
return(0); |
} |
} |
|
|