version 1.1, 2008/11/22 14:53:29 |
version 1.2, 2008/11/22 16:55:02 |
|
|
size_t line; |
size_t line; |
}; |
}; |
|
|
|
struct md_mbuf { |
|
struct md_buf *buf; |
|
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_file *, struct md_file *); |
static int md_run(struct md_buf *, struct md_buf *); |
static int md_run(struct md_buf *, struct md_buf *); |
static int md_line(struct md_buf *, const struct md_buf *, |
static int md_line(struct md_mbuf *, const struct md_buf *, |
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_buf *); |
|
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[]) |
{ |
{ |
Line 117 md_begin_io(const char *out, const char *in) |
|
Line 127 md_begin_io(const char *out, const char *in) |
|
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 138 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 173 md_begin_bufs(struct md_file *out, struct md_file *in) |
|
Line 181 md_begin_bufs(struct md_file *out, struct md_file *in) |
|
|
|
inbuf.file = in; |
inbuf.file = in; |
inbuf.line = 1; |
inbuf.line = 1; |
inbuf.bufsz = MAX(stin.st_blksize, BUFSIZ); |
/*inbuf.bufsz = MAX(stin.st_blksize, BUFSIZ);*/ |
|
inbuf.bufsz = 256; |
|
|
outbuf.file = out; |
outbuf.file = out; |
outbuf.line = 1; |
outbuf.line = 1; |
outbuf.bufsz = MAX(stout.st_blksize, BUFSIZ); |
/*outbuf.bufsz = MAX(stout.st_blksize, BUFSIZ);*/ |
|
outbuf.bufsz = 256; |
|
|
if (NULL == (inbuf.buf = malloc(inbuf.bufsz))) { |
if (NULL == (inbuf.buf = malloc(inbuf.bufsz))) { |
warn("malloc"); |
warn("malloc"); |
Line 210 md_buf_fill(struct md_buf *in) |
|
Line 220 md_buf_fill(struct md_buf *in) |
|
|
|
if (-1 == (ssz = read(in->file->fd, in->buf, in->bufsz))) |
if (-1 == (ssz = read(in->file->fd, in->buf, in->bufsz))) |
warn("%s", in->file->name); |
warn("%s", in->file->name); |
|
else |
|
(void)printf("%s: filled %zd bytes\n", |
|
in->file->name, ssz); |
|
|
return(ssz); |
return(ssz); |
} |
} |
Line 218 md_buf_fill(struct md_buf *in) |
|
Line 231 md_buf_fill(struct md_buf *in) |
|
static int |
static int |
md_run(struct md_buf *out, struct md_buf *in) |
md_run(struct md_buf *out, struct md_buf *in) |
{ |
{ |
|
struct md_mbuf mbuf; |
ssize_t sz, i; |
ssize_t sz, i; |
char line[BUFSIZ]; |
char line[BUFSIZ]; |
size_t pos; |
size_t pos; |
Line 225 md_run(struct md_buf *out, struct md_buf *in) |
|
Line 239 md_run(struct md_buf *out, struct md_buf *in) |
|
assert(in); |
assert(in); |
assert(out); |
assert(out); |
|
|
|
mbuf.buf = out; |
|
mbuf.pos = 0; |
|
|
/* LINTED */ |
/* LINTED */ |
for (pos = 0; ; ) { |
for (pos = 0; ; ) { |
if (-1 == (sz = md_buf_fill(in))) |
if (-1 == (sz = md_buf_fill(in))) |
Line 234 md_run(struct md_buf *out, struct md_buf *in) |
|
Line 251 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(out, in, line, pos)) |
if (md_line(&mbuf, in, line, pos)) |
return(1); |
return(1); |
in->line++; |
in->line++; |
pos = 0; |
pos = 0; |
Line 253 md_run(struct md_buf *out, struct md_buf *in) |
|
Line 270 md_run(struct md_buf *out, struct md_buf *in) |
|
} |
} |
} |
} |
|
|
if (0 != pos) |
if (0 != pos && md_line(&mbuf, in, line, pos)) |
return(md_line(out, in, line, pos)); |
return(1); |
|
|
return(0); |
return(md_buf_flush(&mbuf) ? 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->buf->file); |
|
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) |
|
return(1); |
|
|
|
sz = write(buf->buf->file->fd, buf->buf->buf, buf->pos); |
|
|
|
if (-1 == sz) { |
|
warn("%s", buf->buf->file->name); |
|
return(0); |
|
} else if ((size_t)sz != buf->pos) { |
|
warnx("%s: short write", buf->buf->file->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->buf->bufsz) { |
|
ssz = buf->buf->bufsz - buf->pos; |
|
(void)memcpy(buf->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 + buf->pos, p, sz); |
|
buf->pos += sz; |
|
return(1); |
|
} |
|
|
|
|
|
static int |
|
md_line(struct md_mbuf *out, const struct md_buf *in, |
const char *buf, size_t sz) |
const char *buf, size_t sz) |
{ |
{ |
size_t i; |
|
|
|
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); |
} |
} |
|
|