version 1.181, 2011/03/22 10:13:01 |
version 1.189, 2011/05/15 14:50:01 |
|
|
#include "term.h" |
#include "term.h" |
#include "main.h" |
#include "main.h" |
|
|
static void spec(struct termp *, enum roffdeco, |
static void spec(struct termp *, const char *, size_t); |
const char *, size_t); |
|
static void res(struct termp *, const char *, size_t); |
static void res(struct termp *, const char *, size_t); |
static void bufferc(struct termp *, char); |
static void bufferc(struct termp *, char); |
static void adjbuf(struct termp *p, size_t); |
static void adjbuf(struct termp *p, int); |
static void encode(struct termp *, const char *, size_t); |
static void encode(struct termp *, const char *, size_t); |
|
|
|
|
Line 48 term_free(struct termp *p) |
|
Line 47 term_free(struct termp *p) |
|
if (p->buf) |
if (p->buf) |
free(p->buf); |
free(p->buf); |
if (p->symtab) |
if (p->symtab) |
chars_free(p->symtab); |
mchars_free(p->symtab); |
|
|
free(p); |
free(p); |
} |
} |
Line 156 term_flushln(struct termp *p) |
|
Line 155 term_flushln(struct termp *p) |
|
vis = vend = 0; |
vis = vend = 0; |
i = 0; |
i = 0; |
|
|
while (i < (int)p->col) { |
while (i < p->col) { |
/* |
/* |
* Handle literal tab characters: collapse all |
* Handle literal tab characters: collapse all |
* subsequent tabs into a single huge set of spaces. |
* subsequent tabs into a single huge set of spaces. |
*/ |
*/ |
while (i < (int)p->col && '\t' == p->buf[i]) { |
while (i < p->col && '\t' == p->buf[i]) { |
vend = (vis / p->tabwidth + 1) * p->tabwidth; |
vend = (vis / p->tabwidth + 1) * p->tabwidth; |
vbl += vend - vis; |
vbl += vend - vis; |
vis = vend; |
vis = vend; |
Line 175 term_flushln(struct termp *p) |
|
Line 174 term_flushln(struct termp *p) |
|
* space is printed according to regular spacing rules). |
* space is printed according to regular spacing rules). |
*/ |
*/ |
|
|
for (j = i, jhy = 0; j < (int)p->col; j++) { |
for (j = i, jhy = 0; j < p->col; j++) { |
if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j]) |
if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j]) |
break; |
break; |
|
|
Line 218 term_flushln(struct termp *p) |
|
Line 217 term_flushln(struct termp *p) |
|
} |
} |
|
|
/* Write out the [remaining] word. */ |
/* Write out the [remaining] word. */ |
for ( ; i < (int)p->col; i++) { |
for ( ; i < p->col; i++) { |
if (vend > bp && jhy > 0 && i > jhy) |
if (vend > bp && jhy > 0 && i > jhy) |
break; |
break; |
if ('\t' == p->buf[i]) |
if ('\t' == p->buf[i]) |
Line 349 term_vspace(struct termp *p) |
|
Line 348 term_vspace(struct termp *p) |
|
static void |
static void |
numbered(struct termp *p, const char *word, size_t len) |
numbered(struct termp *p, const char *word, size_t len) |
{ |
{ |
const char *rhs; |
char c; |
|
|
rhs = chars_num2char(word, len); |
if ('\0' != (c = mchars_num2char(word, len))) |
if (rhs) |
encode(p, &c, 1); |
encode(p, rhs, 1); |
|
} |
} |
|
|
|
|
static void |
static void |
spec(struct termp *p, enum roffdeco d, const char *word, size_t len) |
spec(struct termp *p, const char *word, size_t len) |
{ |
{ |
const char *rhs; |
const char *rhs; |
size_t sz; |
size_t sz; |
|
|
rhs = chars_spec2str(p->symtab, word, len, &sz); |
rhs = mchars_spec2str(p->symtab, word, len, &sz); |
if (rhs) |
if (rhs) |
encode(p, rhs, sz); |
encode(p, rhs, sz); |
else if (DECO_SSPECIAL == d) |
else if (1 == len) |
encode(p, word, len); |
encode(p, word, len); |
} |
} |
|
|
Line 377 res(struct termp *p, const char *word, size_t len) |
|
Line 375 res(struct termp *p, const char *word, size_t len) |
|
const char *rhs; |
const char *rhs; |
size_t sz; |
size_t sz; |
|
|
rhs = chars_res2str(p->symtab, word, len, &sz); |
rhs = mchars_res2str(p->symtab, word, len, &sz); |
if (rhs) |
if (rhs) |
encode(p, rhs, sz); |
encode(p, rhs, sz); |
} |
} |
Line 456 term_fontpop(struct termp *p) |
|
Line 454 term_fontpop(struct termp *p) |
|
void |
void |
term_word(struct termp *p, const char *word) |
term_word(struct termp *p, const char *word) |
{ |
{ |
const char *sv, *seq; |
const char *seq; |
|
int sz; |
size_t ssz; |
size_t ssz; |
enum roffdeco deco; |
enum mandoc_esc esc; |
|
|
sv = word; |
|
|
|
if (DELIM_CLOSE == mandoc_isdelim(word)) |
|
if ( ! (TERMP_IGNDELIM & p->flags)) |
|
p->flags |= TERMP_NOSPACE; |
|
|
|
if ( ! (TERMP_NOSPACE & p->flags)) { |
if ( ! (TERMP_NOSPACE & p->flags)) { |
if ( ! (TERMP_KEEP & p->flags)) { |
if ( ! (TERMP_KEEP & p->flags)) { |
if (TERMP_PREKEEP & p->flags) |
if (TERMP_PREKEEP & p->flags) |
Line 484 term_word(struct termp *p, const char *word) |
|
Line 477 term_word(struct termp *p, const char *word) |
|
|
|
p->flags &= ~(TERMP_SENTENCE | TERMP_IGNDELIM); |
p->flags &= ~(TERMP_SENTENCE | TERMP_IGNDELIM); |
|
|
while (*word) { |
while ('\0' != *word) { |
if ((ssz = strcspn(word, "\\")) > 0) |
if ((ssz = strcspn(word, "\\")) > 0) |
encode(p, word, ssz); |
encode(p, word, ssz); |
|
|
Line 492 term_word(struct termp *p, const char *word) |
|
Line 485 term_word(struct termp *p, const char *word) |
|
if ('\\' != *word) |
if ('\\' != *word) |
continue; |
continue; |
|
|
seq = ++word; |
word++; |
word += a2roffdeco(&deco, &seq, &ssz); |
esc = mandoc_escape(&word, &seq, &sz); |
|
if (ESCAPE_ERROR == esc) |
|
break; |
|
|
switch (deco) { |
switch (esc) { |
case (DECO_NUMBERED): |
case (ESCAPE_NUMBERED): |
numbered(p, seq, ssz); |
numbered(p, seq, sz); |
break; |
break; |
case (DECO_RESERVED): |
case (ESCAPE_PREDEF): |
res(p, seq, ssz); |
res(p, seq, sz); |
break; |
break; |
case (DECO_SPECIAL): |
case (ESCAPE_SPECIAL): |
/* FALLTHROUGH */ |
spec(p, seq, sz); |
case (DECO_SSPECIAL): |
|
spec(p, deco, seq, ssz); |
|
break; |
break; |
case (DECO_BOLD): |
case (ESCAPE_FONTBOLD): |
term_fontrepl(p, TERMFONT_BOLD); |
term_fontrepl(p, TERMFONT_BOLD); |
break; |
break; |
case (DECO_ITALIC): |
case (ESCAPE_FONTITALIC): |
term_fontrepl(p, TERMFONT_UNDER); |
term_fontrepl(p, TERMFONT_UNDER); |
break; |
break; |
case (DECO_ROMAN): |
case (ESCAPE_FONTROMAN): |
term_fontrepl(p, TERMFONT_NONE); |
term_fontrepl(p, TERMFONT_NONE); |
break; |
break; |
case (DECO_PREVIOUS): |
case (ESCAPE_FONTPREV): |
term_fontlast(p); |
term_fontlast(p); |
break; |
break; |
|
case (ESCAPE_NOSPACE): |
|
if ('\0' == *word) |
|
p->flags |= TERMP_NOSPACE; |
|
break; |
default: |
default: |
break; |
break; |
} |
} |
|
|
if (DECO_NOSPACE == deco && '\0' == *word) |
|
p->flags |= TERMP_NOSPACE; |
|
} |
} |
|
|
if (DELIM_OPEN == mandoc_isdelim(sv)) |
|
p->flags |= TERMP_NOSPACE; |
|
} |
} |
|
|
|
|
static void |
static void |
adjbuf(struct termp *p, size_t sz) |
adjbuf(struct termp *p, int sz) |
{ |
{ |
|
|
if (0 == p->maxcols) |
if (0 == p->maxcols) |
Line 541 adjbuf(struct termp *p, size_t sz) |
|
Line 532 adjbuf(struct termp *p, size_t sz) |
|
while (sz >= p->maxcols) |
while (sz >= p->maxcols) |
p->maxcols <<= 2; |
p->maxcols <<= 2; |
|
|
p->buf = mandoc_realloc(p->buf, p->maxcols); |
p->buf = mandoc_realloc |
|
(p->buf, sizeof(int) * (size_t)p->maxcols); |
} |
} |
|
|
|
|
Line 552 bufferc(struct termp *p, char c) |
|
Line 544 bufferc(struct termp *p, char c) |
|
if (p->col + 1 >= p->maxcols) |
if (p->col + 1 >= p->maxcols) |
adjbuf(p, p->col + 1); |
adjbuf(p, p->col + 1); |
|
|
p->buf[(int)p->col++] = c; |
p->buf[p->col++] = c; |
} |
} |
|
|
|
|
|
|
encode(struct termp *p, const char *word, size_t sz) |
encode(struct termp *p, const char *word, size_t sz) |
{ |
{ |
enum termfont f; |
enum termfont f; |
int i; |
int i, len; |
|
|
|
/* LINTED */ |
|
len = sz; |
|
|
/* |
/* |
* Encode and buffer a string of characters. If the current |
* Encode and buffer a string of characters. If the current |
* font mode is unset, buffer directly, else encode then buffer |
* font mode is unset, buffer directly, else encode then buffer |
Line 569 encode(struct termp *p, const char *word, size_t sz) |
|
Line 564 encode(struct termp *p, const char *word, size_t sz) |
|
*/ |
*/ |
|
|
if (TERMFONT_NONE == (f = term_fonttop(p))) { |
if (TERMFONT_NONE == (f = term_fonttop(p))) { |
if (p->col + sz >= p->maxcols) |
if (p->col + len >= p->maxcols) |
adjbuf(p, p->col + sz); |
adjbuf(p, p->col + len); |
memcpy(&p->buf[(int)p->col], word, sz); |
for (i = 0; i < len; i++) |
p->col += sz; |
p->buf[p->col++] = word[i]; |
return; |
return; |
} |
} |
|
|
/* Pre-buffer, assuming worst-case. */ |
/* Pre-buffer, assuming worst-case. */ |
|
|
if (p->col + 1 + (sz * 3) >= p->maxcols) |
if (p->col + 1 + (len * 3) >= p->maxcols) |
adjbuf(p, p->col + 1 + (sz * 3)); |
adjbuf(p, p->col + 1 + (len * 3)); |
|
|
for (i = 0; i < (int)sz; i++) { |
for (i = 0; i < len; i++) { |
if ( ! isgraph((u_char)word[i])) { |
if ( ! isgraph((unsigned char)word[i])) { |
p->buf[(int)p->col++] = word[i]; |
p->buf[p->col++] = word[i]; |
continue; |
continue; |
} |
} |
|
|
if (TERMFONT_UNDER == f) |
if (TERMFONT_UNDER == f) |
p->buf[(int)p->col++] = '_'; |
p->buf[p->col++] = '_'; |
else |
else |
p->buf[(int)p->col++] = word[i]; |
p->buf[p->col++] = word[i]; |
|
|
p->buf[(int)p->col++] = 8; |
p->buf[p->col++] = 8; |
p->buf[(int)p->col++] = word[i]; |
p->buf[p->col++] = word[i]; |
} |
} |
} |
} |
|
|
Line 609 term_len(const struct termp *p, size_t sz) |
|
Line 604 term_len(const struct termp *p, size_t sz) |
|
size_t |
size_t |
term_strlen(const struct termp *p, const char *cp) |
term_strlen(const struct termp *p, const char *cp) |
{ |
{ |
size_t sz, ssz, rsz, i; |
size_t sz, rsz, i; |
enum roffdeco d; |
int ssz; |
const char *seq, *rhs; |
const char *seq, *rhs; |
|
static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' }; |
|
|
for (sz = 0; '\0' != *cp; ) |
/* |
/* |
* Account for escaped sequences within string length |
* Account for escaped sequences within string length |
* calculations. This follows the logic in term_word() as we |
* calculations. This follows the logic in term_word() |
* must calculate the width of produced strings. |
* as we must calculate the width of produced strings. |
*/ |
*/ |
|
if ('\\' == *cp) { |
|
seq = ++cp; |
|
cp += a2roffdeco(&d, &seq, &ssz); |
|
|
|
switch (d) { |
sz = 0; |
case (DECO_RESERVED): |
while ('\0' != *cp) { |
rhs = chars_res2str |
rsz = strcspn(cp, rej); |
|
for (i = 0; i < rsz; i++) |
|
sz += (*p->width)(p, *cp++); |
|
|
|
switch (*cp) { |
|
case ('\\'): |
|
cp++; |
|
switch (mandoc_escape(&cp, &seq, &ssz)) { |
|
case (ESCAPE_ERROR): |
|
return(sz); |
|
case (ESCAPE_PREDEF): |
|
rhs = mchars_res2str |
(p->symtab, seq, ssz, &rsz); |
(p->symtab, seq, ssz, &rsz); |
break; |
break; |
case (DECO_SPECIAL): |
case (ESCAPE_SPECIAL): |
/* FALLTHROUGH */ |
rhs = mchars_spec2str |
case (DECO_SSPECIAL): |
|
rhs = chars_spec2str |
|
(p->symtab, seq, ssz, &rsz); |
(p->symtab, seq, ssz, &rsz); |
|
|
/* Allow for one-char escapes. */ |
if (ssz != 1 || rhs) |
if (DECO_SSPECIAL != d || rhs) |
|
break; |
break; |
|
|
rhs = seq; |
rhs = seq; |
Line 646 term_strlen(const struct termp *p, const char *cp) |
|
Line 646 term_strlen(const struct termp *p, const char *cp) |
|
break; |
break; |
} |
} |
|
|
if (rhs) |
if (NULL == rhs) |
for (i = 0; i < rsz; i++) |
break; |
sz += (*p->width)(p, *rhs++); |
|
} else if (ASCII_NBRSP == *cp) { |
for (i = 0; i < rsz; i++) |
|
sz += (*p->width)(p, *rhs++); |
|
break; |
|
case (ASCII_NBRSP): |
sz += (*p->width)(p, ' '); |
sz += (*p->width)(p, ' '); |
cp++; |
cp++; |
} else if (ASCII_HYPH == *cp) { |
break; |
|
case (ASCII_HYPH): |
sz += (*p->width)(p, '-'); |
sz += (*p->width)(p, '-'); |
cp++; |
cp++; |
} else |
break; |
sz += (*p->width)(p, *cp++); |
default: |
|
break; |
|
} |
|
} |
|
|
return(sz); |
return(sz); |
} |
} |
|
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
size_t |
size_t |
term_vspan(const struct termp *p, const struct roffsu *su) |
term_vspan(const struct termp *p, const struct roffsu *su) |
Line 697 term_vspan(const struct termp *p, const struct roffsu |
|
Line 703 term_vspan(const struct termp *p, const struct roffsu |
|
return(/* LINTED */(size_t) |
return(/* LINTED */(size_t) |
r); |
r); |
} |
} |
|
|
|
|
size_t |
size_t |
term_hspan(const struct termp *p, const struct roffsu *su) |
term_hspan(const struct termp *p, const struct roffsu *su) |