=================================================================== RCS file: /cvs/mandoc/main.c,v retrieving revision 1.67 retrieving revision 1.70 diff -u -p -r1.67 -r1.70 --- mandoc/main.c 2010/05/15 05:50:19 1.67 +++ mandoc/main.c 2010/05/15 16:20:12 1.70 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.67 2010/05/15 05:50:19 joerg Exp $ */ +/* $Id: main.c,v 1.70 2010/05/15 16:20:12 joerg Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -268,11 +268,31 @@ ffile(const char *file, struct curparse *curp) static int +resize_buf(struct buf *buf, size_t initial) +{ + void *tmp; + size_t sz; + + if (buf->sz == 0) + sz = initial; + else + sz = 2 * buf->sz; + tmp = realloc(buf->buf, sz); + if (NULL == tmp) { + perror(NULL); + return(0); + } + buf->buf = tmp; + buf->sz = sz; + return(1); +} + + +static int read_whole_file(struct curparse *curp, struct buf *fb, int *with_mmap) { struct stat st; - char *buf; - size_t sz, off; + size_t off; ssize_t ssz; if (-1 == fstat(curp->fd, &st)) { @@ -319,17 +339,8 @@ read_whole_file(struct curparse *curp, struct buf *fb, curp->file); break; } - if (fb->sz == 0) - sz = 65536; - else - sz = 2 * fb->sz; - buf = realloc(fb->buf, sz); - if (NULL == buf) { - perror(NULL); + if (! resize_buf(fb, 65536)) break; - } - fb->buf = buf; - fb->sz = sz; } ssz = read(curp->fd, fb->buf + off, fb->sz - off); if (ssz == 0) { @@ -353,89 +364,85 @@ read_whole_file(struct curparse *curp, struct buf *fb, static void fdesc(struct curparse *curp) { - size_t sz; struct buf ln, blk; - int j, i, pos, lnn, comment, with_mmap; + int i, pos, lnn, lnn_start, with_mmap; struct man *man; struct mdoc *mdoc; - sz = BUFSIZ; man = NULL; mdoc = NULL; memset(&ln, 0, sizeof(struct buf)); /* - * Two buffers: ln and buf. buf is the input buffer optimised - * here for each file's block size. ln is a line buffer. Both - * growable, hence passed in by ptr-ptr. + * Two buffers: ln and buf. buf is the input file and may be + * memory mapped. ln is a line buffer and grows on-demand. */ if (!read_whole_file(curp, &blk, &with_mmap)) return; - /* Fill buf with file blocksize. */ - - for (i = lnn = pos = comment = 0; i < (int)blk.sz; ++i) { - if (pos >= (int)ln.sz) { - ln.sz += 256; /* Step-size. */ - ln.buf = realloc(ln.buf, ln.sz); - if (NULL == ln.buf) { - perror(NULL); - goto bailout; + for (i = 0, lnn = 1; i < (int)blk.sz;) { + pos = 0; + lnn_start = lnn; + while (i < (int)blk.sz) { + if ('\n' == blk.buf[i]) { + ++i; + ++lnn; + break; } - } - - if ('\n' != blk.buf[i]) { - if (comment) + /* Trailing backslash is like a plain character. */ + if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) { + if (pos >= (int)ln.sz) + if (! resize_buf(&ln, 256)) + goto bailout; + ln.buf[pos++] = blk.buf[i++]; continue; - ln.buf[pos++] = blk.buf[i]; - - /* Handle in-line `\"' comments. */ - - if (1 == pos || '\"' != ln.buf[pos - 1]) - continue; - - for (j = pos - 2; j >= 0; j--) - if ('\\' != ln.buf[j]) - break; - - if ( ! ((pos - 2 - j) % 2)) - continue; - - comment = 1; - pos -= 2; - for (; pos > 0; --pos) { - if (ln.buf[pos - 1] != ' ') - break; - if (pos > 2 && ln.buf[pos - 2] == '\\') - break; } - continue; - } - - /* Handle escaped `\\n' newlines. */ - - if (pos > 0 && 0 == comment && '\\' == ln.buf[pos - 1]) { - for (j = pos - 1; j >= 0; j--) - if ('\\' != ln.buf[j]) - break; - if ( ! ((pos - j) % 2)) { - pos--; - lnn++; + /* Found an escape and at least one other character. */ + if ('\n' == blk.buf[i + 1]) { + /* Escaped newlines are skipped over */ + i += 2; + ++lnn; continue; } + if ('"' == blk.buf[i + 1]) { + i += 2; + /* Comment, skip to end of line */ + for (; i < (int)blk.sz; ++i) { + if ('\n' == blk.buf[i]) { + ++i; + ++lnn; + break; + } + } + /* Backout trailing whitespaces */ + for (; pos > 0; --pos) { + if (ln.buf[pos - 1] != ' ') + break; + if (pos > 2 && ln.buf[pos - 2] == '\\') + break; + } + break; + } + /* Some other escape sequence, copy and continue. */ + if (pos + 1 >= (int)ln.sz) + if (! resize_buf(&ln, 256)) + goto bailout; + + ln.buf[pos++] = blk.buf[i++]; + ln.buf[pos++] = blk.buf[i++]; } + if (pos >= (int)ln.sz) + if (! resize_buf(&ln, 256)) + goto bailout; ln.buf[pos] = 0; - lnn++; /* If unset, assign parser in pset(). */ if ( ! (man || mdoc) && ! pset(ln.buf, pos, curp, &man, &mdoc)) goto bailout; - pos = comment = 0; - /* Pass down into parsers. */ if (man && ! man_parseln(man, lnn, ln.buf)) @@ -479,7 +486,7 @@ fdesc(struct curparse *curp) case (OUTT_LINT): break; default: - curp->outdata = ascii_alloc(); + curp->outdata = ascii_alloc(80); curp->outman = terminal_man; curp->outmdoc = terminal_mdoc; curp->outfree = terminal_free;