term.c revision 371584c2eae4cf827fd406ba26c14f021adaaa70
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov/* $Id: term.c,v 1.257 2016/04/12 15:30:00 schwarze Exp $ */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore *
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * copyright notice and this permission notice appear in all copies.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore *
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include "config.h"
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include <sys/types.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include <assert.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include <ctype.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include <stdio.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include <stdlib.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include <string.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include "mandoc.h"
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov#include "mandoc_aux.h"
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include "out.h"
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include "term.h"
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include "main.h"
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amorestatic size_t cond_width(const struct termp *, int, int *);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amorestatic void adjbuf(struct termp *p, size_t);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void bufferc(struct termp *, char);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void encode(struct termp *, const char *, size_t);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void encode1(struct termp *, int);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_free(struct termp *p)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov free(p->buf);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov free(p->fontq);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore free(p);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovterm_begin(struct termp *p, term_margin head,
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov term_margin foot, const struct roff_meta *arg)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->headf = head;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->footf = foot;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->argf = arg;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (*p->begin)(p);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_end(struct termp *p)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (*p->end)(p);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/*
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Flush a chunk of text. By default, break the output line each time
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * the right margin is reached, and continue output on the next line
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * at the same offset as the chunk itself. By default, also break the
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * output line at the end of the chunk.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * The following flags may be specified:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore *
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * - TERMP_NOBREAK: Do not break the output line at the right margin,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * but only at the max right margin. Also, do not break the output
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * line at the end of the chunk, such that the next call can pad to
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * the next column. However, if less than p->trailspace blanks,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * which can be 0, 1, or 2, remain to the right margin, the line
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * will be broken.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * - TERMP_BRTRSP: Consider trailing whitespace significant
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * when deciding whether the chunk fits or not.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * - TERMP_BRIND: If the chunk does not fit and the output line has
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * to be broken, start the next line at the right margin instead
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * of at the offset. Used together with TERMP_NOBREAK for the tags
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * in various kinds of tagged lists.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * - TERMP_DANGLE: Do not break the output line at the right margin,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * append the next chunk after it even if this one is too long.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * To be used together with TERMP_NOBREAK.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * the next chunk if this column is not full.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_flushln(struct termp *p)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore size_t i; /* current input position in p->buf */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore int ntab; /* number of tabs to prepend */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t vis; /* current visual position on output */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t vbl; /* number of blanks to prepend to output */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t vend; /* end of word visual position on output */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t bp; /* visual right border position */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t dv; /* temporary for visual pos calculations */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore size_t j; /* temporary loop index for p->buf */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore size_t jhy; /* last hyph before overflow w/r/t j */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t maxvis; /* output position of visible boundary */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * First, establish the maximum columns of "visible" content.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * This is usually the difference between the right-margin and
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * an indentation, but can be, for tagged lists or columns, a
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * small set of values.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore *
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * The following unsigned-signed subtractions look strange,
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * but they are actually correct. If the int p->overstep
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * is negative, it gets sign extended. Subtracting that
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * very large size_t effectively adds a small number to dv.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov dv = p->rmargin > p->offset ? p->rmargin - p->offset : 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (p->flags & TERMP_NOBREAK) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov dv = p->maxrmargin > p->offset ?
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->maxrmargin - p->offset : 0;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov bp = (int)dv > p->overstep ?
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov dv - (size_t)p->overstep : 0;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov } else
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov bp = maxvis;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Calculate the required amount of padding.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vbl = p->offset + p->overstep > p->viscol ?
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->offset + p->overstep - p->viscol : 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vis = vend = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore i = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore while (i < p->col) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Handle literal tab characters: collapse all
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * subsequent tabs into a single huge set of spaces.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore ntab = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore while (i < p->col && '\t' == p->buf[i]) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vend = (vis / p->tabwidth + 1) * p->tabwidth;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vbl += vend - vis;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vis = vend;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore ntab++;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore i++;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Count up visible word characters. Control sequences
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * (starting with the CSI) aren't counted. A space
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * generates a non-printing word, which is valid (the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * space is printed according to regular spacing rules).
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (j = i, jhy = 0; j < p->col; j++) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (' ' == p->buf[j] || '\t' == p->buf[j])
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Back over the last printed character. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (8 == p->buf[j]) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore assert(j);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vend -= (*p->width)(p, p->buf[j - 1]);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore continue;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Regular word. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Break at the hyphen point if we overrun. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (vend > vis && vend < bp &&
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov (ASCII_HYPH == p->buf[j] ||
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov ASCII_BREAK == p->buf[j]))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore jhy = j;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /*
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Hyphenation now decided, put back a real
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * hyphen such that we get the correct width.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (ASCII_HYPH == p->buf[j])
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->buf[j] = '-';
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vend += (*p->width)(p, p->buf[j]);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Find out whether we would exceed the right margin.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If so, break to the next line.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (vend > bp && 0 == jhy && vis > 0) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vend -= vis;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (*p->endline)(p);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->viscol = 0;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (TERMP_BRIND & p->flags) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vbl = p->rmargin;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov vend += p->rmargin;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov vend -= p->offset;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore } else
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vbl = p->offset;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore /* use pending tabs on the new line */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (0 < ntab)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore vbl += ntab * p->tabwidth;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore /*
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * Remove the p->overstep width.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * Again, if p->overstep is negative,
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * sign extension does the right thing.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore bp += (size_t)p->overstep;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->overstep = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Write out the [remaining] word. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for ( ; i < p->col; i++) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (vend > bp && jhy > 0 && i > jhy)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ('\t' == p->buf[i])
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (' ' == p->buf[i]) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore j = i;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov while (i < p->col && ' ' == p->buf[i])
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore i++;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore dv = (i - j) * (*p->width)(p, ' ');
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vbl += dv;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vend += dv;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (ASCII_NBRSP == p->buf[i]) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vbl += (*p->width)(p, ' ');
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore continue;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (ASCII_BREAK == p->buf[i])
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Now we definitely know there will be
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * printable characters to output,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * so write preceding white space now.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (vbl) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (*p->advance)(p, vbl);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->viscol += vbl;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vbl = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (*p->letter)(p, p->buf[i]);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (8 == p->buf[i])
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->viscol -= (*p->width)(p, p->buf[i-1]);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov else
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->viscol += (*p->width)(p, p->buf[i]);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vis = vend;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If there was trailing white space, it was not printed;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * so reset the cursor position accordingly.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (vis > vbl)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vis -= vbl;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov else
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov vis = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->col = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->overstep = 0;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ( ! (TERMP_NOBREAK & p->flags)) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->viscol = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (*p->endline)(p);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore return;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (TERMP_HANG & p->flags) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->overstep += (int)(p->offset + vis - p->rmargin +
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->trailspace * (*p->width)(p, ' '));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If we have overstepped the margin, temporarily move
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * it to the right and flag the rest of the line to be
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * shorter.
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * If there is a request to keep the columns together,
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore * allow negative overstep when the column is not full.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (p->trailspace && p->overstep < 0)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->overstep = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore return;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore } else if (TERMP_DANGLE & p->flags)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore return;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Trailing whitespace is significant in some columns. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (vis && vbl && (TERMP_BRTRSP & p->flags))
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov vis += vbl;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* If the column was overrun, break the line. */
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (*p->endline)(p);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->viscol = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * A newline only breaks an existing line; it won't assert vertical
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * space. All data in the output buffer is flushed prior to the newline
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * assertion.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_newln(struct termp *p)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->flags |= TERMP_NOSPACE;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (p->col || p->viscol)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore term_flushln(p);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Asserts a vertical space (a full, empty line-break between lines).
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Note that if used twice, this will cause two blank spaces and so on.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * All data in the output buffer is flushed prior to the newline
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * assertion.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_vspace(struct termp *p)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore term_newln(p);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->viscol = 0;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (0 < p->skipvsp)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p->skipvsp--;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore else
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore (*p->endline)(p);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* Swap current and previous font; for \fP and .ft P */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_fontlast(struct termp *p)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore enum termfont f;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore f = p->fontl;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->fontl = p->fontq[p->fonti];
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->fontq[p->fonti] = f;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* Set font, save current, discard previous; for \f, .ft, .B etc. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_fontrepl(struct termp *p, enum termfont f)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->fontl = p->fontq[p->fonti];
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->fontq[p->fonti] = f;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* Set font, save previous. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_fontpush(struct termp *p, enum termfont f)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->fontl = p->fontq[p->fonti];
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (++p->fonti == p->fontsz) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->fontsz += 8;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->fontq = mandoc_reallocarray(p->fontq,
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->fontsz, sizeof(*p->fontq));
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->fontq[p->fonti] = f;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* Flush to make the saved pointer current again. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovterm_fontpopq(struct termp *p, int i)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov assert(i >= 0);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (p->fonti > i)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->fonti = i;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* Pop one font off the stack. */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_fontpop(struct termp *p)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore assert(p->fonti);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->fonti--;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Handle pwords, partial words, which may be either a single word or a
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * phrase that cannot be broken down (such as a literal string). This
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * handles word styling.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_word(struct termp *p, const char *word)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore const char nbrsp[2] = { ASCII_NBRSP, 0 };
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const char *seq, *cp;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore int sz, uc;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t ssz;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore enum mandoc_esc esc;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ( ! (TERMP_NOSPACE & p->flags)) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ( ! (TERMP_KEEP & p->flags)) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore bufferc(p, ' ');
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (TERMP_SENTENCE & p->flags)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore bufferc(p, ' ');
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore } else
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore bufferc(p, ASCII_NBRSP);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (TERMP_PREKEEP & p->flags)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p->flags |= TERMP_KEEP;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ( ! (p->flags & TERMP_NONOSPACE))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->flags &= ~TERMP_NOSPACE;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore else
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->flags |= TERMP_NOSPACE;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->flags &= ~(TERMP_SENTENCE | TERMP_NONEWLINE);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->skipvsp = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore while ('\0' != *word) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if ('\\' != *word) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (TERMP_NBRWORD & p->flags) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (' ' == *word) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore encode(p, nbrsp, 1);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore word++;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore continue;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore }
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore ssz = strcspn(word, "\\ ");
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore } else
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore ssz = strcspn(word, "\\");
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore encode(p, word, ssz);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore word += (int)ssz;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore continue;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore word++;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore esc = mandoc_escape(&word, &seq, &sz);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (ESCAPE_ERROR == esc)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (esc) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_UNICODE:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov uc = mchars_num2uc(seq + 1, sz - 1);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_NUMBERED:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov uc = mchars_num2char(seq, sz);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (uc < 0)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_SPECIAL:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (p->enc == TERMENC_ASCII) {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov cp = mchars_spec2str(seq, sz, &ssz);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (cp != NULL)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov encode(p, cp, ssz);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov } else {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov uc = mchars_spec2cp(seq, sz);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (uc > 0)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov encode1(p, uc);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_FONTBOLD:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore term_fontrepl(p, TERMFONT_BOLD);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_FONTITALIC:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore term_fontrepl(p, TERMFONT_UNDER);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_FONTBI:
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore term_fontrepl(p, TERMFONT_BI);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_FONT:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_FONTROMAN:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore term_fontrepl(p, TERMFONT_NONE);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_FONTPREV:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore term_fontlast(p);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_NOSPACE:
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (p->flags & TERMP_BACKAFTER)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->flags &= ~TERMP_BACKAFTER;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov else if (*word == '\0')
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_SKIPCHAR:
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->flags |= TERMP_BACKAFTER;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_OVERSTRIKE:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov cp = seq + sz;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov while (seq < cp) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (*seq == '\\') {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov mandoc_escape(&seq, NULL, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov encode1(p, *seq++);
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (seq < cp) {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (p->flags & TERMP_BACKBEFORE)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->flags |= TERMP_BACKAFTER;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov else
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->flags |= TERMP_BACKBEFORE;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov /* Trim trailing backspace/blank pair. */
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (p->col > 2 && p->buf[p->col - 1] == ' ')
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->col -= 2;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov continue;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore default:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /*
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Common handling for Unicode and numbered
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * character escape sequences.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (p->enc == TERMENC_ASCII) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov cp = ascii_uc2str(uc);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov encode(p, cp, strlen(cp));
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov } else {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if ((uc < 0x20 && uc != 0x09) ||
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov (uc > 0x7E && uc < 0xA0))
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov uc = 0xFFFD;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov encode1(p, uc);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p->flags &= ~TERMP_NBRWORD;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amoreadjbuf(struct termp *p, size_t sz)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (0 == p->maxcols)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->maxcols = 1024;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore while (sz >= p->maxcols)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->maxcols <<= 2;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov p->buf = mandoc_reallocarray(p->buf, p->maxcols, sizeof(int));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorebufferc(struct termp *p, char c)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (p->col + 1 >= p->maxcols)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore adjbuf(p, p->col + 1);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->buf[p->col++] = c;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * See encode().
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Do this for a single (probably unicode) value.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Does not check for non-decorated glyphs.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreencode1(struct termp *p, int c)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore enum termfont f;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (p->col + 7 >= p->maxcols)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov adjbuf(p, p->col + 7);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov f = (c == ASCII_HYPH || c > 127 || isgraph(c)) ?
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->fontq[p->fonti] : TERMFONT_NONE;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (p->flags & TERMP_BACKBEFORE) {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (p->buf[p->col - 1] == ' ')
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->col--;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov else
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->buf[p->col++] = 8;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->flags &= ~TERMP_BACKBEFORE;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov }
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->buf[p->col++] = '_';
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p->buf[p->col++] = 8;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore }
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (ASCII_HYPH == c)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p->buf[p->col++] = '-';
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore else
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p->buf[p->col++] = c;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore p->buf[p->col++] = 8;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->buf[p->col++] = c;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (p->flags & TERMP_BACKAFTER) {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->flags |= TERMP_BACKBEFORE;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov p->flags &= ~TERMP_BACKAFTER;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic void
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreencode(struct termp *p, const char *word, size_t sz)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore size_t i;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov if (p->col + 2 + (sz * 5) >= p->maxcols)
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov adjbuf(p, p->col + 2 + (sz * 5));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore for (i = 0; i < sz; i++) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (ASCII_HYPH == word[i] ||
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore isgraph((unsigned char)word[i]))
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore encode1(p, word[i]);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore else
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->buf[p->col++] = word[i];
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovvoid
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovterm_setwidth(struct termp *p, const char *wstr)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov{
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov struct roffsu su;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov int iop, width;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov iop = 0;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov width = 0;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL != wstr) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov switch (*wstr) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case '+':
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov iop = 1;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov wstr++;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case '-':
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov iop = -1;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov wstr++;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov default:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (a2roffsu(wstr, &su, SCALE_MAX))
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov width = term_hspan(p, &su);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov else
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov iop = 0;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov (*p->setwidth)(p, iop, width);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov}
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoresize_t
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_len(const struct termp *p, size_t sz)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov return (*p->width)(p, ' ') * sz;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amorestatic size_t
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amorecond_width(const struct termp *p, int c, int *skip)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore{
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (*skip) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore (*skip) = 0;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov return 0;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore } else
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov return (*p->width)(p, c);
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoresize_t
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_strlen(const struct termp *p, const char *cp)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore size_t sz, rsz, i;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov int ssz, skip, uc;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const char *seq, *rhs;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore enum mandoc_esc esc;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov static const char rej[] = { '\\', ASCII_NBRSP, ASCII_HYPH,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov ASCII_BREAK, '\0' };
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /*
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Account for escaped sequences within string length
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * calculations. This follows the logic in term_word() as we
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * must calculate the width of produced strings.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore sz = 0;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore skip = 0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore while ('\0' != *cp) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore rsz = strcspn(cp, rej);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (i = 0; i < rsz; i++)
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore sz += cond_width(p, *cp++, &skip);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (*cp) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case '\\':
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore cp++;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore esc = mandoc_escape(&cp, &seq, &ssz);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (ESCAPE_ERROR == esc)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore rhs = NULL;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (esc) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_UNICODE:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov uc = mchars_num2uc(seq + 1, ssz - 1);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_NUMBERED:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov uc = mchars_num2char(seq, ssz);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (uc < 0)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_SPECIAL:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (p->enc == TERMENC_ASCII) {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov rhs = mchars_spec2str(seq, ssz, &rsz);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (rhs != NULL)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov } else {
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov uc = mchars_spec2cp(seq, ssz);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (uc > 0)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov sz += cond_width(p, uc, &skip);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_SKIPCHAR:
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore skip = 1;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ESCAPE_OVERSTRIKE:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov rsz = 0;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov rhs = seq + ssz;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov while (seq < rhs) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (*seq == '\\') {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov mandoc_escape(&seq, NULL, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov i = (*p->width)(p, *seq++);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (rsz < i)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov rsz = i;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov sz += rsz;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore default:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /*
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Common handling for Unicode and numbered
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * character escape sequences.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (rhs == NULL) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (p->enc == TERMENC_ASCII) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov rhs = ascii_uc2str(uc);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov rsz = strlen(rhs);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov } else {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if ((uc < 0x20 && uc != 0x09) ||
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov (uc > 0x7E && uc < 0xA0))
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov uc = 0xFFFD;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov sz += cond_width(p, uc, &skip);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov continue;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore if (skip) {
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore skip = 0;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore break;
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore }
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /*
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Common handling for all escape sequences
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * printing more than one character.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (i = 0; i < rsz; i++)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore sz += (*p->width)(p, *rhs++);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ASCII_NBRSP:
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore sz += cond_width(p, ' ', &skip);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore cp++;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case ASCII_HYPH:
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore sz += cond_width(p, '-', &skip);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore cp++;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore default:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov return sz;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovint
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_vspan(const struct termp *p, const struct roffsu *su)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore double r;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov int ri;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (su->unit) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case SCALE_BU:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov r = su->scale / 40.0;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case SCALE_CM:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov r = su->scale * 6.0 / 2.54;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case SCALE_FS:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov r = su->scale * 65536.0 / 40.0;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case SCALE_IN:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov r = su->scale * 6.0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case SCALE_MM:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov r = su->scale * 0.006;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case SCALE_PC:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore r = su->scale;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case SCALE_PT:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov r = su->scale / 12.0;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case SCALE_EN:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case SCALE_EM:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov r = su->scale * 0.6;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case SCALE_VS:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore r = su->scale;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore break;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore default:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov abort();
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov ri = r > 0.0 ? r + 0.4995 : r - 0.4995;
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov return ri < 66 ? ri : 1;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov/*
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov * Convert a scaling width to basic units, rounding down.
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovint
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreterm_hspan(const struct termp *p, const struct roffsu *su)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov return (*p->hspan)(p, su);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}