term.c revision 698f87a48e2e945bfe5493ce168e0d0ae1cedd5c
/* $Id: term.c,v 1.214 2013/12/25 00:39:31 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010, 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mandoc.h"
#include "out.h"
#include "term.h"
#include "main.h"
void
{
if (p->buf)
if (p->symtab)
mchars_free(p->symtab);
free(p);
}
void
{
(*p->begin)(p);
}
void
{
(*p->end)(p);
}
/*
* Flush a line of text. A "line" is loosely defined as being something
* that should be followed by a newline, regardless of whether it's
* broken apart by newlines getting there. A line can also be a
* fragment of a columnar list (`Bl -tag' or `Bl -column'), which does
* not have a trailing newline.
*
* The following flags may be specified:
*
* - TERMP_NOBREAK: this is the most important and is used when making
* columns. In short: don't print a newline and instead expect the
* next call to do the padding up to the start of the next column.
* p->trailspace may be set to 0, 1, or 2, depending on how many
* space characters are required at the end of the column.
*
* - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
* the line is overrun, and don't pad-right if it's underrun.
*
* - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
* overrunning, instead save the position and continue at that point
* when the next invocation.
*
* In-line line breaking:
*
* If TERMP_NOBREAK is specified and the line overruns the right
* margin, it will break and pad-right to the right margin after
* writing. If maxrmargin is violated, it will break and continue
* writing from the right-margin, which will lead to the above scenario
* upon exit. Otherwise, the line will break at the right margin.
*/
void
term_flushln(struct termp *p)
{
size_t i; /* current input position in p->buf */
int ntab; /* number of tabs to prepend */
size_t j; /* temporary loop index for p->buf */
/*
* First, establish the maximum columns of "visible" content.
* This is usually the difference between the right-margin and
* an indentation, but can be, for tagged lists or columns, a
* small set of values.
*
* The following unsigned-signed subtractions look strange,
* but they are actually correct. If the int p->overstep
* is negative, it gets sign extended. Subtracting that
* very large size_t effectively adds a small number to dv.
*/
/*
* Calculate the required amount of padding.
*/
i = 0;
while (i < p->col) {
/*
* Handle literal tab characters: collapse all
* subsequent tabs into a single huge set of spaces.
*/
ntab = 0;
ntab++;
i++;
}
/*
* Count up visible word characters. Control sequences
* (starting with the CSI) aren't counted. A space
* generates a non-printing word, which is valid (the
* space is printed according to regular spacing rules).
*/
break;
/* Back over the the last printed character. */
if (8 == p->buf[j]) {
assert(j);
continue;
}
/* Regular word. */
/* Break at the hyphen point if we overrun. */
ASCII_HYPH == p->buf[j])
jhy = j;
}
/*
* Find out whether we would exceed the right margin.
* If so, break to the next line.
*/
(*p->endline)(p);
p->viscol = 0;
if (TERMP_NOBREAK & p->flags) {
} else
/* use pending tabs on the new line */
if (0 < ntab)
/*
* Remove the p->overstep width.
* Again, if p->overstep is negative,
* sign extension does the right thing.
*/
p->overstep = 0;
}
/* Write out the [remaining] word. */
for ( ; i < p->col; i++) {
break;
if ('\t' == p->buf[i])
break;
if (' ' == p->buf[i]) {
j = i;
while (' ' == p->buf[i])
i++;
break;
}
if (ASCII_NBRSP == p->buf[i]) {
continue;
}
/*
* Now we definitely know there will be
* printable characters to output,
* so write preceding white space now.
*/
if (vbl) {
vbl = 0;
}
if (ASCII_HYPH == p->buf[i]) {
(*p->letter)(p, '-');
continue;
}
if (8 == p->buf[i])
else
}
}
/*
* If there was trailing white space, it was not printed;
* so reset the cursor position accordingly.
*/
if (vis)
p->col = 0;
p->overstep = 0;
if ( ! (TERMP_NOBREAK & p->flags)) {
p->viscol = 0;
(*p->endline)(p);
return;
}
if (TERMP_HANG & p->flags) {
/*
* If we have overstepped the margin, temporarily move
* it to the right and flag the rest of the line to be
* shorter.
* If there is a request to keep the columns together,
* allow negative overstep when the column is not full.
*/
if (p->trailspace && p->overstep < 0)
p->overstep = 0;
return;
} else if (TERMP_DANGLE & p->flags)
return;
/* If the column was overrun, break the line. */
(*p->endline)(p);
p->viscol = 0;
}
}
/*
* A newline only breaks an existing line; it won't assert vertical
* space. All data in the output buffer is flushed prior to the newline
* assertion.
*/
void
term_newln(struct termp *p)
{
p->flags |= TERMP_NOSPACE;
term_flushln(p);
}
/*
* Asserts a vertical space (a full, empty line-break between lines).
* Note that if used twice, this will cause two blank spaces and so on.
* All data in the output buffer is flushed prior to the newline
* assertion.
*/
void
term_vspace(struct termp *p)
{
term_newln(p);
p->viscol = 0;
if (0 < p->skipvsp)
p->skipvsp--;
else
(*p->endline)(p);
}
void
term_fontlast(struct termp *p)
{
enum termfont f;
f = p->fontl;
}
void
{
}
void
{
}
const void *
term_fontq(struct termp *p)
{
}
enum termfont
term_fonttop(struct termp *p)
{
}
void
{
p->fonti--;
}
void
term_fontpop(struct termp *p)
{
p->fonti--;
}
/*
* Handle pwords, partial words, which may be either a single word or a
* phrase that cannot be broken down (such as a literal string). This
* handles word styling.
*/
void
{
char c;
enum mandoc_esc esc;
if ( ! (TERMP_NOSPACE & p->flags)) {
if ( ! (TERMP_KEEP & p->flags)) {
bufferc(p, ' ');
if (TERMP_SENTENCE & p->flags)
bufferc(p, ' ');
} else
bufferc(p, ASCII_NBRSP);
}
if (TERMP_PREKEEP & p->flags)
p->flags |= TERMP_KEEP;
if ( ! (p->flags & TERMP_NONOSPACE))
p->flags &= ~TERMP_NOSPACE;
else
p->flags |= TERMP_NOSPACE;
p->flags &= ~TERMP_SENTENCE;
while ('\0' != *word) {
if ('\\' != *word) {
if (TERMP_SKIPCHAR & p->flags) {
p->flags &= ~TERMP_SKIPCHAR;
word++;
continue;
}
if (TERMP_NBRWORD & p->flags) {
if (' ' == *word) {
word++;
continue;
}
} else
continue;
}
word++;
if (ESCAPE_ERROR == esc)
break;
if (TERMENC_ASCII != p->enc)
switch (esc) {
case (ESCAPE_UNICODE):
if ('\0' == uc)
break;
continue;
case (ESCAPE_SPECIAL):
if (uc <= 0)
break;
continue;
default:
break;
}
switch (esc) {
case (ESCAPE_UNICODE):
encode1(p, '?');
break;
case (ESCAPE_NUMBERED):
if ('\0' != c)
encode(p, &c, 1);
break;
case (ESCAPE_SPECIAL):
else if (1 == ssz)
break;
case (ESCAPE_FONTBOLD):
break;
case (ESCAPE_FONTITALIC):
break;
case (ESCAPE_FONTBI):
term_fontrepl(p, TERMFONT_BI);
break;
case (ESCAPE_FONT):
/* FALLTHROUGH */
case (ESCAPE_FONTROMAN):
break;
case (ESCAPE_FONTPREV):
term_fontlast(p);
break;
case (ESCAPE_NOSPACE):
if (TERMP_SKIPCHAR & p->flags)
p->flags &= ~TERMP_SKIPCHAR;
else if ('\0' == *word)
p->flags |= TERMP_NOSPACE;
break;
case (ESCAPE_SKIPCHAR):
p->flags |= TERMP_SKIPCHAR;
break;
default:
break;
}
}
p->flags &= ~TERMP_NBRWORD;
}
static void
{
if (0 == p->maxcols)
p->maxcols = 1024;
p->maxcols <<= 2;
}
static void
{
}
/*
* See encode().
* Do this for a single (probably unicode) value.
* Does not check for non-decorated glyphs.
*/
static void
{
enum termfont f;
if (TERMP_SKIPCHAR & p->flags) {
p->flags &= ~TERMP_SKIPCHAR;
return;
}
f = term_fonttop(p);
if (TERMFONT_UNDER == f || TERMFONT_BI == f) {
}
if (TERMFONT_BOLD == f || TERMFONT_BI == f) {
if (ASCII_HYPH == c)
else
}
}
static void
{
size_t i;
if (TERMP_SKIPCHAR & p->flags) {
p->flags &= ~TERMP_SKIPCHAR;
return;
}
/*
* Encode and buffer a string of characters. If the current
* font mode is unset, buffer directly, else encode then buffer
* character by character.
*/
if (TERMFONT_NONE == term_fonttop(p)) {
for (i = 0; i < sz; i++)
return;
}
/* Pre-buffer, assuming worst-case. */
for (i = 0; i < sz; i++) {
if (ASCII_HYPH == word[i] ||
else
}
}
{
}
static size_t
{
if (*skip) {
(*skip) = 0;
return(0);
} else
return((*p->width)(p, c));
}
{
enum mandoc_esc esc;
/*
* Account for escaped sequences within string length
* calculations. This follows the logic in term_word() as we
* must calculate the width of produced strings.
*/
sz = 0;
skip = 0;
while ('\0' != *cp) {
for (i = 0; i < rsz; i++)
c = 0;
switch (*cp) {
case ('\\'):
cp++;
if (ESCAPE_ERROR == esc)
return(sz);
if (TERMENC_ASCII != p->enc)
switch (esc) {
case (ESCAPE_UNICODE):
c = mchars_num2uc
if ('\0' == c)
break;
continue;
case (ESCAPE_SPECIAL):
c = mchars_spec2cp
if (c <= 0)
break;
continue;
default:
break;
}
switch (esc) {
case (ESCAPE_UNICODE):
break;
case (ESCAPE_NUMBERED):
if ('\0' != c)
break;
case (ESCAPE_SPECIAL):
break;
break;
case (ESCAPE_SKIPCHAR):
skip = 1;
break;
default:
break;
}
break;
if (skip) {
skip = 0;
break;
}
for (i = 0; i < rsz; i++)
break;
case (ASCII_NBRSP):
cp++;
break;
case (ASCII_HYPH):
cp++;
break;
default:
break;
}
}
return(sz);
}
/* ARGSUSED */
{
double r;
case (SCALE_CM):
break;
case (SCALE_IN):
break;
case (SCALE_PC):
break;
case (SCALE_PT):
break;
case (SCALE_MM):
break;
case (SCALE_VS):
break;
default:
break;
}
if (r < 0.0)
r = 0.0;
return(/* LINTED */(size_t)
r);
}
{
double v;
if (v < 0.0)
v = 0.0;
return((size_t) /* LINTED */
v);
}