version 1.124, 2009/11/07 14:14:16 |
version 1.126, 2009/11/12 05:58:30 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
*/ |
*/ |
|
#include <sys/types.h> |
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
#include <stdio.h> |
#include <stdio.h> |
|
|
#include "mdoc.h" |
#include "mdoc.h" |
#include "main.h" |
#include "main.h" |
|
|
/* FIXME: accomodate non-breaking, non-collapsing white-space. */ |
|
/* FIXME: accomodate non-breaking, collapsing white-space. */ |
|
|
|
static struct termp *term_alloc(enum termenc); |
static struct termp *term_alloc(enum termenc); |
static void term_free(struct termp *); |
static void term_free(struct termp *); |
|
static void spec(struct termp *, const char *, size_t); |
|
static void res(struct termp *, const char *, size_t); |
|
static void buffera(struct termp *, const char *, size_t); |
|
static void bufferc(struct termp *, char); |
|
static void adjbuf(struct termp *p, size_t); |
|
static void encode(struct termp *, const char *, size_t); |
|
|
static void do_special(struct termp *, |
|
const char *, size_t); |
|
static void do_reserved(struct termp *, |
|
const char *, size_t); |
|
static void buffer(struct termp *, char); |
|
static void encode(struct termp *, char); |
|
|
|
|
|
void * |
void * |
ascii_alloc(void) |
ascii_alloc(void) |
{ |
{ |
Line 320 term_vspace(struct termp *p) |
|
Line 318 term_vspace(struct termp *p) |
|
|
|
|
|
static void |
static void |
do_special(struct termp *p, 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; |
int i; |
|
|
|
rhs = chars_a2ascii(p->symtab, word, len, &sz); |
rhs = chars_a2ascii(p->symtab, word, len, &sz); |
|
if (rhs) |
if (NULL == rhs) { |
encode(p, rhs, sz); |
#if 0 |
|
fputs("Unknown special character: ", stderr); |
|
for (i = 0; i < (int)len; i++) |
|
fputc(word[i], stderr); |
|
fputc('\n', stderr); |
|
#endif |
|
return; |
|
} |
|
for (i = 0; i < (int)sz; i++) |
|
encode(p, rhs[i]); |
|
} |
} |
|
|
|
|
static void |
static void |
do_reserved(struct termp *p, const char *word, size_t len) |
res(struct termp *p, const char *word, size_t len) |
{ |
{ |
const char *rhs; |
const char *rhs; |
size_t sz; |
size_t sz; |
int i; |
|
|
|
rhs = chars_a2res(p->symtab, word, len, &sz); |
rhs = chars_a2res(p->symtab, word, len, &sz); |
|
if (rhs) |
|
encode(p, rhs, sz); |
|
} |
|
|
if (NULL == rhs) { |
|
#if 0 |
void |
fputs("Unknown reserved word: ", stderr); |
term_fontlast(struct termp *p) |
for (i = 0; i < (int)len; i++) |
{ |
fputc(word[i], stderr); |
enum termfont f; |
fputc('\n', stderr); |
|
#endif |
f = p->fontl; |
return; |
p->fontl = p->fontq[p->fonti]; |
} |
p->fontq[p->fonti] = f; |
for (i = 0; i < (int)sz; i++) |
|
encode(p, rhs[i]); |
|
} |
} |
|
|
|
|
|
void |
|
term_fontrepl(struct termp *p, enum termfont f) |
|
{ |
|
|
|
p->fontl = p->fontq[p->fonti]; |
|
p->fontq[p->fonti] = f; |
|
} |
|
|
|
|
|
void |
|
term_fontpush(struct termp *p, enum termfont f) |
|
{ |
|
|
|
assert(p->fonti + 1 < 10); |
|
p->fontl = p->fontq[p->fonti]; |
|
p->fontq[++p->fonti] = f; |
|
} |
|
|
|
|
|
const void * |
|
term_fontq(struct termp *p) |
|
{ |
|
|
|
return(&p->fontq[p->fonti]); |
|
} |
|
|
|
|
|
enum termfont |
|
term_fonttop(struct termp *p) |
|
{ |
|
|
|
return(p->fontq[p->fonti]); |
|
} |
|
|
|
|
|
void |
|
term_fontpopq(struct termp *p, const void *key) |
|
{ |
|
|
|
while (p->fonti >= 0 && key != &p->fontq[p->fonti]) |
|
p->fonti--; |
|
assert(p->fonti >= 0); |
|
} |
|
|
|
|
|
void |
|
term_fontpop(struct termp *p) |
|
{ |
|
|
|
assert(p->fonti); |
|
p->fonti--; |
|
} |
|
|
|
|
/* |
/* |
* Handle pwords, partial words, which may be either a single word or a |
* 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 |
* phrase that cannot be broken down (such as a literal string). This |
|
|
term_word(struct termp *p, const char *word) |
term_word(struct termp *p, const char *word) |
{ |
{ |
const char *sv, *seq; |
const char *sv, *seq; |
int sz, meta; |
int sz; |
size_t ssz; |
size_t ssz; |
enum roffdeco deco; |
enum roffdeco deco; |
|
|
Line 407 term_word(struct termp *p, const char *word) |
|
Line 448 term_word(struct termp *p, const char *word) |
|
} |
} |
|
|
if ( ! (TERMP_NOSPACE & p->flags)) |
if ( ! (TERMP_NOSPACE & p->flags)) |
buffer(p, ' '); |
bufferc(p, ' '); |
|
|
if ( ! (p->flags & TERMP_NONOSPACE)) |
if ( ! (p->flags & TERMP_NONOSPACE)) |
p->flags &= ~TERMP_NOSPACE; |
p->flags &= ~TERMP_NOSPACE; |
|
|
/* |
/* FIXME: use strcspn. */ |
* FIXME: it's faster to put the metafont conditional here, |
|
* because most of the time we're not a metafont and can use |
|
* strcspn and fwrite. |
|
*/ |
|
|
|
while (*word) { |
while (*word) { |
if ('\\' != *word) { |
if ('\\' != *word) { |
encode(p, *word); |
encode(p, word, 1); |
word++; |
word++; |
continue; |
continue; |
} |
} |
Line 430 term_word(struct termp *p, const char *word) |
|
Line 467 term_word(struct termp *p, const char *word) |
|
|
|
switch (deco) { |
switch (deco) { |
case (DECO_RESERVED): |
case (DECO_RESERVED): |
do_reserved(p, seq, ssz); |
res(p, seq, ssz); |
break; |
break; |
case (DECO_SPECIAL): |
case (DECO_SPECIAL): |
do_special(p, seq, ssz); |
spec(p, seq, ssz); |
break; |
break; |
case (DECO_BOLD): |
case (DECO_BOLD): |
p->metamask = p->metafont; |
term_fontrepl(p, TERMFONT_BOLD); |
p->metafont |= METAF_BOLD; |
|
break; |
break; |
case (DECO_ITALIC): |
case (DECO_ITALIC): |
p->metamask = p->metafont; |
term_fontrepl(p, TERMFONT_UNDER); |
p->metafont |= METAF_UNDER; |
|
break; |
break; |
case (DECO_ROMAN): |
case (DECO_ROMAN): |
p->metamask = p->metafont; |
term_fontrepl(p, TERMFONT_NONE); |
p->metafont &= ~METAF_UNDER; |
|
p->metafont &= ~METAF_BOLD; |
|
break; |
break; |
case (DECO_PREVIOUS): |
case (DECO_PREVIOUS): |
meta = p->metamask; |
term_fontlast(p); |
p->metamask = p->metafont; |
|
p->metafont = meta; |
|
break; |
break; |
default: |
default: |
break; |
break; |
Line 474 term_word(struct termp *p, const char *word) |
|
Line 505 term_word(struct termp *p, const char *word) |
|
} |
} |
|
|
|
|
/* |
|
* Insert a single character into the line-buffer. If the buffer's |
|
* space is exceeded, then allocate more space by doubling the buffer |
|
* size. |
|
*/ |
|
static void |
static void |
buffer(struct termp *p, char c) |
adjbuf(struct termp *p, size_t sz) |
{ |
{ |
size_t s; |
|
|
|
if (p->col + 1 >= p->maxcols) { |
if (0 == p->maxcols) |
if (0 == p->maxcols) |
p->maxcols = 1024; |
p->maxcols = 256; |
while (sz >= p->maxcols) |
s = p->maxcols * 2; |
p->maxcols <<= 2; |
p->buf = realloc(p->buf, s); |
|
if (NULL == p->buf) { |
p->buf = realloc(p->buf, p->maxcols); |
perror(NULL); |
if (NULL == p->buf) { |
exit(EXIT_FAILURE); |
perror(NULL); |
} |
exit(EXIT_FAILURE); |
p->maxcols = s; |
|
} |
} |
p->buf[(int)(p->col)++] = c; |
|
} |
} |
|
|
|
|
static void |
static void |
encode(struct termp *p, char c) |
buffera(struct termp *p, const char *word, size_t sz) |
{ |
{ |
|
|
if (isgraph((u_char)c)) { |
if (p->col + sz >= p->maxcols) |
if (p->under || METAF_UNDER & p->metafont) { |
adjbuf(p, p->col + sz); |
buffer(p, '_'); |
|
buffer(p, 8); |
memcpy(&p->buf[(int)p->col], word, sz); |
|
p->col += sz; |
|
} |
|
|
|
|
|
static void |
|
bufferc(struct termp *p, char c) |
|
{ |
|
|
|
if (p->col + 1 >= p->maxcols) |
|
adjbuf(p, p->col + 1); |
|
|
|
p->buf[(int)p->col++] = c; |
|
} |
|
|
|
|
|
static void |
|
encode(struct termp *p, const char *word, size_t sz) |
|
{ |
|
enum termfont f; |
|
int i; |
|
|
|
/* |
|
* 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 == (f = term_fonttop(p))) { |
|
buffera(p, word, sz); |
|
return; |
|
} |
|
|
|
for (i = 0; i < (int)sz; i++) { |
|
if ( ! isgraph((u_char)word[i])) { |
|
bufferc(p, word[i]); |
|
continue; |
} |
} |
if (p->bold || METAF_BOLD & p->metafont) { |
|
buffer(p, c); |
if (TERMFONT_UNDER == f) |
buffer(p, 8); |
bufferc(p, '_'); |
} |
else |
|
bufferc(p, word[i]); |
|
|
|
bufferc(p, 8); |
|
bufferc(p, word[i]); |
} |
} |
buffer(p, c); |
|
} |
} |
|
|
|
|