=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/mandoc/read.c,v retrieving revision 1.70 retrieving revision 1.71 diff -c -r1.70 -r1.71 *** src/usr.bin/mandoc/read.c 2014/10/30 00:05:02 1.70 --- src/usr.bin/mandoc/read.c 2014/11/01 04:07:25 1.71 *************** *** 1,4 **** ! /* $OpenBSD: read.c,v 1.70 2014/10/30 00:05:02 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2014 Ingo Schwarze --- 1,4 ---- ! /* $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,68 **** 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 int read_whole_file(struct mparse *, const char *, int, struct buf *, int *); static void mparse_end(struct mparse *); --- 62,68 ---- static void choose_parser(struct mparse *); static void resize_buf(struct buf *, size_t); ! 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,323 **** } /* ! * 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'). */ static void ! mparse_buf_r(struct mparse *curp, struct buf blk, int start) { const struct tbl_span *span; struct buf ln; enum rofferr rr; ! int i, of, rc; ! int pos; /* byte number in the ln buffer */ int lnn; /* line number in the real file */ unsigned char c; ! memset(&ln, 0, sizeof(struct buf)); lnn = curp->line; pos = 0; ! for (i = blk.offs; i < (int)blk.sz; ) { if (0 == pos && '\0' == blk.buf[i]) break; --- 297,325 ---- } /* ! * 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, 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 of, rc; int lnn; /* line number in the real file */ unsigned char c; ! memset(&ln, 0, sizeof(ln)); lnn = curp->line; pos = 0; ! while (i < blk.sz) { if (0 == pos && '\0' == blk.buf[i]) break; *************** *** 327,339 **** if (lnn < 3 && curp->filenc & MPARSE_UTF8 && ! curp->filenc & MPARSE_LATIN1) { ! blk.offs = i; ! curp->filenc = preconv_cue(&blk); ! } } ! while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) { /* * When finding an unescaped newline character, --- 329,339 ---- if (lnn < 3 && curp->filenc & MPARSE_UTF8 && ! curp->filenc & MPARSE_LATIN1) ! curp->filenc = preconv_cue(&blk, i); } ! while (i < blk.sz && (start || blk.buf[i] != '\0')) { /* * When finding an unescaped newline character, *************** *** 341,347 **** * Skip a preceding carriage return, if any. */ ! if ('\r' == blk.buf[i] && i + 1 < (int)blk.sz && '\n' == blk.buf[i + 1]) ++i; if ('\n' == blk.buf[i]) { --- 341,347 ---- * Skip a preceding carriage return, if any. */ ! if ('\r' == blk.buf[i] && i + 1 < blk.sz && '\n' == blk.buf[i + 1]) ++i; if ('\n' == blk.buf[i]) { *************** *** 355,361 **** * case of 11 bytes: "\\[u10ffff]\0" */ ! if (pos + 11 > (int)ln.sz) resize_buf(&ln, 256); /* --- 355,361 ---- * case of 11 bytes: "\\[u10ffff]\0" */ ! if (pos + 11 > ln.sz) resize_buf(&ln, 256); /* *************** *** 364,376 **** 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 { mandoc_vmsg(MANDOCERR_BADCHAR, curp, curp->line, pos, "0x%x", c); --- 364,371 ---- c = blk.buf[i]; if (c & 0x80) { ! if ( ! (curp->filenc && preconv_encode( ! &blk, &i, &ln, &pos, &curp->filenc))) { mandoc_vmsg(MANDOCERR_BADCHAR, curp, curp->line, pos, "0x%x", c); *************** *** 394,400 **** /* Trailing backslash = a plain char. */ ! if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) { ln.buf[pos++] = blk.buf[i++]; continue; } --- 389,395 ---- /* Trailing backslash = a plain char. */ ! if (blk.buf[i] != '\\' || i + 1 == blk.sz) { ln.buf[pos++] = blk.buf[i++]; continue; } *************** *** 406,412 **** * skip that one as well. */ ! if ('\r' == blk.buf[i + 1] && i + 2 < (int)blk.sz && '\n' == blk.buf[i + 2]) ++i; if ('\n' == blk.buf[i + 1]) { --- 401,407 ---- * skip that one as well. */ ! if ('\r' == blk.buf[i + 1] && i + 2 < blk.sz && '\n' == blk.buf[i + 2]) ++i; if ('\n' == blk.buf[i + 1]) { *************** *** 418,424 **** if ('"' == blk.buf[i + 1] || '#' == 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; --- 413,419 ---- if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) { i += 2; /* Comment, skip to end of line */ ! for (; i < blk.sz; ++i) { if ('\n' == blk.buf[i]) { ++i; ++lnn; *************** *** 455,461 **** ln.buf[pos++] = blk.buf[i++]; } ! if (pos >= (int)ln.sz) resize_buf(&ln, 256); ln.buf[pos] = '\0'; --- 450,456 ---- ln.buf[pos++] = blk.buf[i++]; } ! if (pos >= ln.sz) resize_buf(&ln, 256); ln.buf[pos] = '\0'; *************** *** 498,511 **** switch (rr) { case ROFF_REPARSE: if (REPARSE_LIMIT >= ++curp->reparse_count) ! mparse_buf_r(curp, ln, 0); else mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, pos, NULL); pos = 0; continue; case ROFF_APPEND: ! pos = (int)strlen(ln.buf); continue; case ROFF_RERUN: goto rerun; --- 493,506 ---- switch (rr) { case ROFF_REPARSE: if (REPARSE_LIMIT >= ++curp->reparse_count) ! mparse_buf_r(curp, ln, of, 0); else mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, pos, NULL); pos = 0; continue; case ROFF_APPEND: ! pos = strlen(ln.buf); continue; case ROFF_RERUN: goto rerun; *************** *** 516,523 **** assert(MANDOCLEVEL_FATAL <= curp->file_status); break; case ROFF_SO: ! if (0 == (MPARSE_SO & curp->options) && ! (i >= (int)blk.sz || '\0' == blk.buf[i])) { curp->sodest = mandoc_strdup(ln.buf + of); free(ln.buf); return; --- 511,518 ---- assert(MANDOCLEVEL_FATAL <= curp->file_status); break; case ROFF_SO: ! if ( ! (curp->options & MPARSE_SO) && ! (i >= blk.sz || blk.buf[i] == '\0')) { curp->sodest = mandoc_strdup(ln.buf + of); free(ln.buf); return; *************** *** 643,649 **** 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) --- 638,643 ---- *************** *** 674,680 **** ssz = read(fd, fb->buf + (int)off, fb->sz - off); if (ssz == 0) { fb->sz = off; - fb->offs = 0; return(1); } if (ssz == -1) { --- 668,673 ---- *************** *** 731,736 **** --- 724,730 ---- { struct buf *svprimary; const char *svfile; + size_t offset; static int recursion_depth; if (64 < recursion_depth) { *************** *** 751,761 **** (unsigned char)blk.buf[0] == 0xef && (unsigned char)blk.buf[1] == 0xbb && (unsigned char)blk.buf[2] == 0xbf) { ! blk.offs = 3; curp->filenc &= ~MPARSE_LATIN1; ! } ! mparse_buf_r(curp, blk, 1); if (0 == --recursion_depth && MANDOCLEVEL_FATAL > curp->file_status) mparse_end(curp); --- 745,756 ---- (unsigned char)blk.buf[0] == 0xef && (unsigned char)blk.buf[1] == 0xbb && (unsigned char)blk.buf[2] == 0xbf) { ! offset = 3; curp->filenc &= ~MPARSE_LATIN1; ! } else ! offset = 0; ! mparse_buf_r(curp, blk, offset, 1); if (0 == --recursion_depth && MANDOCLEVEL_FATAL > curp->file_status) mparse_end(curp);