summaryrefslogtreecommitdiffstats
path: root/read.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2014-11-01 04:08:43 +0000
committerIngo Schwarze <schwarze@openbsd.org>2014-11-01 04:08:43 +0000
commite8ad8f372f71b24bdc4359cc0dd0d62b9fad70e7 (patch)
treef0847767e7776670bebf22af95bba3e5f59a5754 /read.c
parentd56e1e4fc2e21ba1b22e1ab3b24ea5bd71b8a659 (diff)
downloadmandoc-e8ad8f372f71b24bdc4359cc0dd0d62b9fad70e7.tar.gz
Refactor, no functional change: Remove the parse point from struct buf.
Some functions need multiple parse points, some none at all, and it varies whether any of them need to be passed around. So better pass them as a separate argument, and only when needed.
Diffstat (limited to 'read.c')
-rw-r--r--read.c68
1 files changed, 31 insertions, 37 deletions
diff --git a/read.c b/read.c
index 9ff997be..78e054d3 100644
--- a/read.c
+++ b/read.c
@@ -68,7 +68,7 @@ struct mparse {
static void choose_parser(struct mparse *);
static void resize_buf(struct buf *, size_t);
-static void mparse_buf_r(struct mparse *, struct buf, int);
+static void mparse_buf_r(struct mparse *, struct buf, size_t, int);
static int read_whole_file(struct mparse *, const char *, int,
struct buf *, int *);
static void mparse_end(struct mparse *);
@@ -303,27 +303,29 @@ choose_parser(struct mparse *curp)
}
/*
- * Main parse routine for an opened file. This is called for each
- * opened file and simply loops around the full input file, possibly
- * nesting (i.e., with `so').
+ * Main parse routine for a buffer.
+ * It assumes encoding and line numbering are already set up.
+ * It can recurse directly (for invocations of user-defined
+ * macros, inline equations, and input line traps)
+ * and indirectly (for .so file inclusion).
*/
static void
-mparse_buf_r(struct mparse *curp, struct buf blk, int start)
+mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
{
const struct tbl_span *span;
struct buf ln;
+ size_t pos; /* byte number in the ln buffer */
enum rofferr rr;
- int i, of, rc;
- int pos; /* byte number in the ln buffer */
+ int of, rc;
int lnn; /* line number in the real file */
unsigned char c;
- memset(&ln, 0, sizeof(struct buf));
+ memset(&ln, 0, sizeof(ln));
lnn = curp->line;
pos = 0;
- for (i = blk.offs; i < (int)blk.sz; ) {
+ while (i < blk.sz) {
if (0 == pos && '\0' == blk.buf[i])
break;
@@ -333,13 +335,11 @@ mparse_buf_r(struct mparse *curp, struct buf blk, int start)
if (lnn < 3 &&
curp->filenc & MPARSE_UTF8 &&
- curp->filenc & MPARSE_LATIN1) {
- blk.offs = i;
- curp->filenc = preconv_cue(&blk);
- }
+ curp->filenc & MPARSE_LATIN1)
+ curp->filenc = preconv_cue(&blk, i);
}
- while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
+ while (i < blk.sz && (start || blk.buf[i] != '\0')) {
/*
* When finding an unescaped newline character,
@@ -347,7 +347,7 @@ mparse_buf_r(struct mparse *curp, struct buf blk, int start)
* Skip a preceding carriage return, if any.
*/
- if ('\r' == blk.buf[i] && i + 1 < (int)blk.sz &&
+ if ('\r' == blk.buf[i] && i + 1 < blk.sz &&
'\n' == blk.buf[i + 1])
++i;
if ('\n' == blk.buf[i]) {
@@ -361,7 +361,7 @@ mparse_buf_r(struct mparse *curp, struct buf blk, int start)
* case of 11 bytes: "\\[u10ffff]\0"
*/
- if (pos + 11 > (int)ln.sz)
+ if (pos + 11 > ln.sz)
resize_buf(&ln, 256);
/*
@@ -370,13 +370,8 @@ mparse_buf_r(struct mparse *curp, struct buf blk, int start)
c = blk.buf[i];
if (c & 0x80) {
- blk.offs = i;
- ln.offs = pos;
- if (curp->filenc && preconv_encode(
- &blk, &ln, &curp->filenc)) {
- pos = ln.offs;
- i = blk.offs;
- } else {
+ if ( ! (curp->filenc && preconv_encode(
+ &blk, &i, &ln, &pos, &curp->filenc))) {
mandoc_vmsg(MANDOCERR_BADCHAR,
curp, curp->line, pos,
"0x%x", c);
@@ -400,7 +395,7 @@ mparse_buf_r(struct mparse *curp, struct buf blk, int start)
/* Trailing backslash = a plain char. */
- if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
+ if (blk.buf[i] != '\\' || i + 1 == blk.sz) {
ln.buf[pos++] = blk.buf[i++];
continue;
}
@@ -412,7 +407,7 @@ mparse_buf_r(struct mparse *curp, struct buf blk, int start)
* skip that one as well.
*/
- if ('\r' == blk.buf[i + 1] && i + 2 < (int)blk.sz &&
+ if ('\r' == blk.buf[i + 1] && i + 2 < blk.sz &&
'\n' == blk.buf[i + 2])
++i;
if ('\n' == blk.buf[i + 1]) {
@@ -424,7 +419,7 @@ mparse_buf_r(struct mparse *curp, struct buf blk, int start)
if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) {
i += 2;
/* Comment, skip to end of line */
- for (; i < (int)blk.sz; ++i) {
+ for (; i < blk.sz; ++i) {
if ('\n' == blk.buf[i]) {
++i;
++lnn;
@@ -461,7 +456,7 @@ mparse_buf_r(struct mparse *curp, struct buf blk, int start)
ln.buf[pos++] = blk.buf[i++];
}
- if (pos >= (int)ln.sz)
+ if (pos >= ln.sz)
resize_buf(&ln, 256);
ln.buf[pos] = '\0';
@@ -504,14 +499,14 @@ rerun:
switch (rr) {
case ROFF_REPARSE:
if (REPARSE_LIMIT >= ++curp->reparse_count)
- mparse_buf_r(curp, ln, 0);
+ mparse_buf_r(curp, ln, of, 0);
else
mandoc_msg(MANDOCERR_ROFFLOOP, curp,
curp->line, pos, NULL);
pos = 0;
continue;
case ROFF_APPEND:
- pos = (int)strlen(ln.buf);
+ pos = strlen(ln.buf);
continue;
case ROFF_RERUN:
goto rerun;
@@ -522,8 +517,8 @@ rerun:
assert(MANDOCLEVEL_FATAL <= curp->file_status);
break;
case ROFF_SO:
- if (0 == (MPARSE_SO & curp->options) &&
- (i >= (int)blk.sz || '\0' == blk.buf[i])) {
+ if ( ! (curp->options & MPARSE_SO) &&
+ (i >= blk.sz || blk.buf[i] == '\0')) {
curp->sodest = mandoc_strdup(ln.buf + of);
free(ln.buf);
return;
@@ -650,7 +645,6 @@ read_whole_file(struct mparse *curp, const char *file, int fd,
return(0);
}
*with_mmap = 1;
- fb->offs = 0;
fb->sz = (size_t)st.st_size;
fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
if (fb->buf != MAP_FAILED)
@@ -682,7 +676,6 @@ read_whole_file(struct mparse *curp, const char *file, int fd,
ssz = read(fd, fb->buf + (int)off, fb->sz - off);
if (ssz == 0) {
fb->sz = off;
- fb->offs = 0;
return(1);
}
if (ssz == -1) {
@@ -739,6 +732,7 @@ mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
{
struct buf *svprimary;
const char *svfile;
+ size_t offset;
static int recursion_depth;
if (64 < recursion_depth) {
@@ -759,11 +753,12 @@ mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
(unsigned char)blk.buf[0] == 0xef &&
(unsigned char)blk.buf[1] == 0xbb &&
(unsigned char)blk.buf[2] == 0xbf) {
- blk.offs = 3;
+ offset = 3;
curp->filenc &= ~MPARSE_LATIN1;
- }
+ } else
+ offset = 0;
- mparse_buf_r(curp, blk, 1);
+ mparse_buf_r(curp, blk, offset, 1);
if (0 == --recursion_depth && MANDOCLEVEL_FATAL > curp->file_status)
mparse_end(curp);
@@ -780,7 +775,6 @@ mparse_readmem(struct mparse *curp, const void *buf, size_t len,
blk.buf = UNCONST(buf);
blk.sz = len;
- blk.offs = 0;
mparse_parse_buffer(curp, blk, file);
return(curp->file_status);