From 03b0868e6a25dfc999da64a87b9ecef97cd55bdd Mon Sep 17 00:00:00 2001 From: Ingo Schwarze Date: Sat, 1 Nov 2014 04:07:26 +0000 Subject: 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. --- usr.bin/mandoc/libmandoc.h | 8 +++--- usr.bin/mandoc/preconv.c | 23 ++++++++-------- usr.bin/mandoc/read.c | 69 +++++++++++++++++++++------------------------- 3 files changed, 48 insertions(+), 52 deletions(-) (limited to 'usr.bin/mandoc') diff --git a/usr.bin/mandoc/libmandoc.h b/usr.bin/mandoc/libmandoc.h index ccd4597b1d7..a5df63a6eec 100644 --- a/usr.bin/mandoc/libmandoc.h +++ b/usr.bin/mandoc/libmandoc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: libmandoc.h,v 1.32 2014/10/30 00:05:02 schwarze Exp $ */ +/* $OpenBSD: libmandoc.h,v 1.33 2014/11/01 04:07:25 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011, 2012 Kristaps Dzonsons * Copyright (c) 2013, 2014 Ingo Schwarze @@ -33,7 +33,6 @@ enum rofferr { struct buf { char *buf; size_t sz; - size_t offs; }; __BEGIN_DECLS @@ -69,8 +68,9 @@ int man_endparse(struct man *); int man_addspan(struct man *, const struct tbl_span *); int man_addeqn(struct man *, const struct eqn *); -int preconv_cue(const struct buf *); -int preconv_encode(struct buf *, struct buf *, int *); +int preconv_cue(const struct buf *, size_t); +int preconv_encode(struct buf *, size_t *, + struct buf *, size_t *, int *); void roff_free(struct roff *); struct roff *roff_alloc(struct mparse *, const struct mchars *, int); diff --git a/usr.bin/mandoc/preconv.c b/usr.bin/mandoc/preconv.c index 6ed72f1bccf..aa709191c88 100644 --- a/usr.bin/mandoc/preconv.c +++ b/usr.bin/mandoc/preconv.c @@ -1,4 +1,4 @@ -/* $OpenBSD: preconv.c,v 1.1 2014/10/30 00:05:02 schwarze Exp $ */ +/* $OpenBSD: preconv.c,v 1.2 2014/11/01 04:07:25 schwarze Exp $ */ /* * Copyright (c) 2011 Kristaps Dzonsons * Copyright (c) 2014 Ingo Schwarze @@ -24,7 +24,8 @@ #include "libmandoc.h" int -preconv_encode(struct buf *ib, struct buf *ob, int *filenc) +preconv_encode(struct buf *ib, size_t *ii, struct buf *ob, size_t *oi, + int *filenc) { size_t i; const long one = 1L; @@ -44,7 +45,7 @@ preconv_encode(struct buf *ib, struct buf *ob, int *filenc) if ( ! (*((const char *)(&one)))) be = 1; - for (i = ib->offs; i < ib->sz; i++) { + for (i = *ii; i < ib->sz; i++) { cu = ib->buf[i]; if (state) { if ( ! (cu & 128) || (cu & 64)) { @@ -78,11 +79,11 @@ preconv_encode(struct buf *ib, struct buf *ob, int *filenc) (accum << 24); if (accum < 0x80) - ob->buf[ob->offs++] = accum; + ob->buf[(*oi)++] = accum; else - ob->offs += snprintf(ob->buf + ob->offs, + *oi += snprintf(ob->buf + *oi, 11, "\\[u%.4X]", accum); - ib->offs = i + 1; + *ii = i + 1; *filenc &= ~MPARSE_LATIN1; return(1); } else { @@ -133,21 +134,21 @@ latin: if ( ! (*filenc & MPARSE_LATIN1)) return(0); - ob->offs += snprintf(ob->buf + ob->offs, 11, - "\\[u%.4X]", (unsigned char)ib->buf[ib->offs++]); + *oi += snprintf(ob->buf + *oi, 11, + "\\[u%.4X]", (unsigned char)ib->buf[(*ii)++]); *filenc &= ~MPARSE_UTF8; return(1); } int -preconv_cue(const struct buf *b) +preconv_cue(const struct buf *b, size_t offset) { const char *ln, *eoln, *eoph; size_t sz, phsz; - ln = b->buf + b->offs; - sz = b->sz - b->offs; + ln = b->buf + offset; + sz = b->sz - offset; /* Look for the end-of-line. */ diff --git a/usr.bin/mandoc/read.c b/usr.bin/mandoc/read.c index 8cec99488ac..6aa74c22405 100644 --- a/usr.bin/mandoc/read.c +++ b/usr.bin/mandoc/read.c @@ -1,4 +1,4 @@ -/* $OpenBSD: read.c,v 1.70 2014/10/30 00:05:02 schwarze Exp $ */ +/* $OpenBSD: read.c,v 1.71 2014/11/01 04:07:25 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2014 Ingo Schwarze @@ -62,7 +62,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 *); @@ -297,27 +297,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; @@ -327,13 +329,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, @@ -341,7 +341,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]) { @@ -355,7 +355,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); /* @@ -364,13 +364,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); @@ -394,7 +389,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; } @@ -406,7 +401,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]) { @@ -418,7 +413,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; @@ -455,7 +450,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'; @@ -498,14 +493,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; @@ -516,8 +511,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; @@ -643,7 +638,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) @@ -674,7 +668,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) { @@ -731,6 +724,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) { @@ -751,11 +745,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); -- cgit v1.2.3