=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/mandoc/term.c,v retrieving revision 1.125 retrieving revision 1.126 diff -c -r1.125 -r1.126 *** src/usr.bin/mandoc/term.c 2017/06/07 02:13:52 1.125 --- src/usr.bin/mandoc/term.c 2017/06/07 17:38:08 1.126 *************** *** 1,4 **** ! /* $OpenBSD: term.c,v 1.125 2017/06/07 02:13:52 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2017 Ingo Schwarze --- 1,4 ---- ! /* $OpenBSD: term.c,v 1.126 2017/06/07 17:38:08 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2017 Ingo Schwarze *************** *** 30,36 **** #include "main.h" static size_t cond_width(const struct termp *, int, int *); ! static void adjbuf(struct termp *p, size_t); static void bufferc(struct termp *, char); static void encode(struct termp *, const char *, size_t); static void encode1(struct termp *, int); --- 30,36 ---- #include "main.h" static size_t cond_width(const struct termp *, int, int *); ! static void adjbuf(struct termp_col *, size_t); static void bufferc(struct termp *, char); static void encode(struct termp *, const char *, size_t); static void encode1(struct termp *, int); *************** *** 40,47 **** void term_free(struct termp *p) { ! ! free(p->buf); free(p->fontq); free(p); } --- 40,48 ---- void term_free(struct termp *p) { ! for (p->tcol = p->tcols; p->tcol < p->tcols + p->maxtcol; p->tcol++) ! free(p->tcol->buf); ! free(p->tcols); free(p->fontq); free(p); } *************** *** 92,114 **** void term_flushln(struct termp *p) { ! size_t i; /* current input position in p->buf */ ! int ntab; /* number of tabs to prepend */ size_t vis; /* current visual position on output */ size_t vbl; /* number of blanks to prepend to output */ size_t vend; /* end of word visual position on output */ size_t bp; /* visual right border position */ size_t dv; /* temporary for visual pos calculations */ ! size_t j; /* temporary loop index for p->buf */ size_t jhy; /* last hyph before overflow w/r/t j */ size_t maxvis; /* output position of visible boundary */ ! vbl = (p->flags & TERMP_NOPAD) || p->offset < p->viscol ? 0 : ! p->offset - p->viscol; if (p->minbl && vbl < p->minbl) vbl = p->minbl; ! maxvis = p->rmargin > p->viscol + vbl ? ! p->rmargin - p->viscol - vbl : 0; bp = !(p->flags & TERMP_NOBREAK) ? maxvis : p->maxrmargin > p->viscol + vbl ? p->maxrmargin - p->viscol - vbl : 0; --- 93,115 ---- void term_flushln(struct termp *p) { ! size_t i; /* current input position in p->tcol->buf */ size_t vis; /* current visual position on output */ size_t vbl; /* number of blanks to prepend to output */ size_t vend; /* end of word visual position on output */ size_t bp; /* visual right border position */ size_t dv; /* temporary for visual pos calculations */ ! size_t j; /* temporary loop index for p->tcol->buf */ size_t jhy; /* last hyph before overflow w/r/t j */ size_t maxvis; /* output position of visible boundary */ + int ntab; /* number of tabs to prepend */ ! vbl = (p->flags & TERMP_NOPAD) || p->tcol->offset < p->viscol ? ! 0 : p->tcol->offset - p->viscol; if (p->minbl && vbl < p->minbl) vbl = p->minbl; ! maxvis = p->tcol->rmargin > p->viscol + vbl ? ! p->tcol->rmargin - p->viscol - vbl : 0; bp = !(p->flags & TERMP_NOBREAK) ? maxvis : p->maxrmargin > p->viscol + vbl ? p->maxrmargin - p->viscol - vbl : 0; *************** *** 121,127 **** * subsequent tabs into a single huge set of spaces. */ ntab = 0; ! while (i < p->lastcol && p->buf[i] == '\t') { vend = term_tab_next(vis); vbl += vend - vis; vis = vend; --- 122,128 ---- * subsequent tabs into a single huge set of spaces. */ ntab = 0; ! while (i < p->lastcol && p->tcol->buf[i] == '\t') { vend = term_tab_next(vis); vbl += vend - vis; vis = vend; *************** *** 137,167 **** */ for (j = i, jhy = 0; j < p->lastcol; j++) { ! if (' ' == p->buf[j] || '\t' == p->buf[j]) break; /* Back over the last printed character. */ ! if (8 == p->buf[j]) { assert(j); ! vend -= (*p->width)(p, p->buf[j - 1]); continue; } /* Regular word. */ /* Break at the hyphen point if we overrun. */ if (vend > vis && vend < bp && ! (ASCII_HYPH == p->buf[j] || ! ASCII_BREAK == p->buf[j])) jhy = j; /* * Hyphenation now decided, put back a real * hyphen such that we get the correct width. */ ! if (ASCII_HYPH == p->buf[j]) ! p->buf[j] = '-'; ! vend += (*p->width)(p, p->buf[j]); } /* --- 138,168 ---- */ for (j = i, jhy = 0; j < p->lastcol; j++) { ! if (p->tcol->buf[j] == ' ' || p->tcol->buf[j] == '\t') break; /* Back over the last printed character. */ ! if (p->tcol->buf[j] == '\b') { assert(j); ! vend -= (*p->width)(p, p->tcol->buf[j - 1]); continue; } /* Regular word. */ /* Break at the hyphen point if we overrun. */ if (vend > vis && vend < bp && ! (p->tcol->buf[j] == ASCII_HYPH|| ! p->tcol->buf[j] == ASCII_BREAK)) jhy = j; /* * Hyphenation now decided, put back a real * hyphen such that we get the correct width. */ ! if (p->tcol->buf[j] == ASCII_HYPH) ! p->tcol->buf[j] = '-'; ! vend += (*p->width)(p, p->tcol->buf[j]); } /* *************** *** 182,191 **** /* Re-establish indentation. */ if (p->flags & TERMP_BRIND) ! vbl += p->rmargin; else ! vbl += p->offset; ! maxvis = p->rmargin > vbl ? p->rmargin - vbl : 0; bp = !(p->flags & TERMP_NOBREAK) ? maxvis : p->maxrmargin > vbl ? p->maxrmargin - vbl : 0; } --- 183,193 ---- /* Re-establish indentation. */ if (p->flags & TERMP_BRIND) ! vbl += p->tcol->rmargin; else ! vbl += p->tcol->offset; ! maxvis = p->tcol->rmargin > vbl ? ! p->tcol->rmargin - vbl : 0; bp = !(p->flags & TERMP_NOBREAK) ? maxvis : p->maxrmargin > vbl ? p->maxrmargin - vbl : 0; } *************** *** 194,215 **** for ( ; i < p->lastcol; i++) { if (vend > bp && jhy > 0 && i > jhy) break; ! if ('\t' == p->buf[i]) break; ! if (' ' == p->buf[i]) { j = i; ! while (i < p->lastcol && ' ' == p->buf[i]) i++; dv = (i - j) * (*p->width)(p, ' '); vbl += dv; vend += dv; break; } ! if (ASCII_NBRSP == p->buf[i]) { vbl += (*p->width)(p, ' '); continue; } ! if (ASCII_BREAK == p->buf[i]) continue; /* --- 196,218 ---- for ( ; i < p->lastcol; i++) { if (vend > bp && jhy > 0 && i > jhy) break; ! if (p->tcol->buf[i] == '\t') break; ! if (p->tcol->buf[i] == ' ') { j = i; ! while (i < p->lastcol && ! p->tcol->buf[i] == ' ') i++; dv = (i - j) * (*p->width)(p, ' '); vbl += dv; vend += dv; break; } ! if (p->tcol->buf[i] == ASCII_NBRSP) { vbl += (*p->width)(p, ' '); continue; } ! if (p->tcol->buf[i] == ASCII_BREAK) continue; /* *************** *** 223,233 **** vbl = 0; } ! (*p->letter)(p, p->buf[i]); ! if (8 == p->buf[i]) ! p->viscol -= (*p->width)(p, p->buf[i-1]); else ! p->viscol += (*p->width)(p, p->buf[i]); } vis = vend; } --- 226,236 ---- vbl = 0; } ! (*p->letter)(p, p->tcol->buf[i]); ! if (p->tcol->buf[i] == '\b') ! p->viscol -= (*p->width)(p, p->tcol->buf[i-1]); else ! p->viscol += (*p->width)(p, p->tcol->buf[i]); } vis = vend; } *************** *** 470,481 **** else { uc += p->col; p->col = 0; ! if (p->offset > (size_t)(-uc)) { p->ti += uc; ! p->offset += uc; } else { ! p->ti -= p->offset; ! p->offset = 0; } } continue; --- 473,484 ---- else { uc += p->col; p->col = 0; ! if (p->tcol->offset > (size_t)(-uc)) { p->ti += uc; ! p->tcol->offset += uc; } else { ! p->ti -= p->tcol->offset; ! p->tcol->offset = 0; } } continue; *************** *** 484,492 **** continue; uc = term_hspan(p, &su) / 24; if (uc <= 0) { ! if (p->rmargin <= p->offset) continue; ! lsz = p->rmargin - p->offset; } else lsz = uc; while (sz && --- 487,495 ---- continue; uc = term_hspan(p, &su) / 24; if (uc <= 0) { ! if (p->tcol->rmargin <= p->tcol->offset) continue; ! lsz = p->tcol->rmargin - p->tcol->offset; } else lsz = uc; while (sz && *************** *** 555,562 **** } /* Trim trailing backspace/blank pair. */ if (p->lastcol > 2 && ! (p->buf[p->lastcol - 1] == ' ' || ! p->buf[p->lastcol - 1] == '\t')) p->lastcol -= 2; if (p->col > p->lastcol) p->col = p->lastcol; --- 558,565 ---- } /* Trim trailing backspace/blank pair. */ if (p->lastcol > 2 && ! (p->tcol->buf[p->lastcol - 1] == ' ' || ! p->tcol->buf[p->lastcol - 1] == '\t')) p->lastcol -= 2; if (p->col > p->lastcol) p->col = p->lastcol; *************** *** 584,598 **** } static void ! adjbuf(struct termp *p, size_t sz) { ! ! if (0 == p->maxcols) ! p->maxcols = 1024; ! while (sz >= p->maxcols) ! p->maxcols <<= 2; ! ! p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int)); } static void --- 587,599 ---- } static void ! adjbuf(struct termp_col *c, size_t sz) { ! if (c->maxcols == 0) ! c->maxcols = 1024; ! while (c->maxcols <= sz) ! c->maxcols <<= 2; ! c->buf = mandoc_reallocarray(c->buf, c->maxcols, sizeof(*c->buf)); } static void *************** *** 602,611 **** (*p->letter)(p, c); return; } ! if (p->col + 1 >= p->maxcols) ! adjbuf(p, p->col + 1); if (p->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP)) ! p->buf[p->col] = c; if (p->lastcol < ++p->col) p->lastcol = p->col; } --- 603,612 ---- (*p->letter)(p, c); return; } ! if (p->col + 1 >= p->tcol->maxcols) ! adjbuf(p->tcol, p->col + 1); if (p->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP)) ! p->tcol->buf[p->col] = c; if (p->lastcol < ++p->col) p->lastcol = p->col; } *************** *** 625,656 **** return; } ! if (p->col + 7 >= p->maxcols) ! adjbuf(p, p->col + 7); f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ? p->fontq[p->fonti] : TERMFONT_NONE; if (p->flags & TERMP_BACKBEFORE) { ! if (p->buf[p->col - 1] == ' ' || p->buf[p->col - 1] == '\t') p->col--; else ! p->buf[p->col++] = 8; p->flags &= ~TERMP_BACKBEFORE; } ! if (TERMFONT_UNDER == f || TERMFONT_BI == f) { ! p->buf[p->col++] = '_'; ! p->buf[p->col++] = 8; } ! if (TERMFONT_BOLD == f || TERMFONT_BI == f) { ! if (ASCII_HYPH == c) ! p->buf[p->col++] = '-'; else ! p->buf[p->col++] = c; ! p->buf[p->col++] = 8; } if (p->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP)) ! p->buf[p->col] = c; if (p->lastcol < ++p->col) p->lastcol = p->col; if (p->flags & TERMP_BACKAFTER) { --- 626,658 ---- return; } ! if (p->col + 7 >= p->tcol->maxcols) ! adjbuf(p->tcol, p->col + 7); f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ? p->fontq[p->fonti] : TERMFONT_NONE; if (p->flags & TERMP_BACKBEFORE) { ! if (p->tcol->buf[p->col - 1] == ' ' || ! p->tcol->buf[p->col - 1] == '\t') p->col--; else ! p->tcol->buf[p->col++] = '\b'; p->flags &= ~TERMP_BACKBEFORE; } ! if (f == TERMFONT_UNDER || f == TERMFONT_BI) { ! p->tcol->buf[p->col++] = '_'; ! p->tcol->buf[p->col++] = '\b'; } ! if (f == TERMFONT_BOLD || f == TERMFONT_BI) { ! if (c == ASCII_HYPH) ! p->tcol->buf[p->col++] = '-'; else ! p->tcol->buf[p->col++] = c; ! p->tcol->buf[p->col++] = '\b'; } if (p->lastcol <= p->col || (c != ' ' && c != ASCII_NBRSP)) ! p->tcol->buf[p->col] = c; if (p->lastcol < ++p->col) p->lastcol = p->col; if (p->flags & TERMP_BACKAFTER) { *************** *** 670,677 **** return; } ! if (p->col + 2 + (sz * 5) >= p->maxcols) ! adjbuf(p, p->col + 2 + (sz * 5)); for (i = 0; i < sz; i++) { if (ASCII_HYPH == word[i] || --- 672,679 ---- return; } ! if (p->col + 2 + (sz * 5) >= p->tcol->maxcols) ! adjbuf(p->tcol, p->col + 2 + (sz * 5)); for (i = 0; i < sz; i++) { if (ASCII_HYPH == word[i] || *************** *** 680,686 **** else { if (p->lastcol <= p->col || (word[i] != ' ' && word[i] != ASCII_NBRSP)) ! p->buf[p->col] = word[i]; p->col++; /* --- 682,688 ---- else { if (p->lastcol <= p->col || (word[i] != ' ' && word[i] != ASCII_NBRSP)) ! p->tcol->buf[p->col] = word[i]; p->col++; /*