=================================================================== RCS file: /cvs/mandoc/term.c,v retrieving revision 1.95 retrieving revision 1.103 diff -u -p -r1.95 -r1.103 --- mandoc/term.c 2009/07/27 12:35:54 1.95 +++ mandoc/term.c 2009/09/23 11:02:21 1.103 @@ -1,4 +1,4 @@ -/* $Id: term.c,v 1.95 2009/07/27 12:35:54 kristaps Exp $ */ +/* $Id: term.c,v 1.103 2009/09/23 11:02:21 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -20,13 +20,17 @@ #include #include +#include "chars.h" #include "term.h" #include "man.h" #include "mdoc.h" -extern int man_run(struct termp *, +/* FIXME: accomodate non-breaking, non-collapsing white-space. */ +/* FIXME: accomodate non-breaking, collapsing white-space. */ + +extern void man_run(struct termp *, const struct man *); -extern int mdoc_run(struct termp *, +extern void mdoc_run(struct termp *, const struct mdoc *); static struct termp *term_alloc(enum termenc); @@ -39,8 +43,6 @@ static void do_reserved(struct termp *, const char *, size_t); static void buffer(struct termp *, char); static void encode(struct termp *, char); -static int isopendelim(const char *); -static int isclosedelim(const char *); void * @@ -51,29 +53,29 @@ ascii_alloc(void) } -int +void terminal_man(void *arg, const struct man *man) { struct termp *p; p = (struct termp *)arg; if (NULL == p->symtab) - p->symtab = term_ascii2htab(); + p->symtab = chars_init(CHARS_ASCII); - return(man_run(p, man)); + man_run(p, man); } -int +void terminal_mdoc(void *arg, const struct mdoc *mdoc) { struct termp *p; p = (struct termp *)arg; if (NULL == p->symtab) - p->symtab = term_ascii2htab(); + p->symtab = chars_init(CHARS_ASCII); - return(mdoc_run(p, mdoc)); + mdoc_run(p, mdoc); } @@ -91,8 +93,8 @@ term_free(struct termp *p) if (p->buf) free(p->buf); - if (TERMENC_ASCII == p->enc && p->symtab) - term_asciifree(p->symtab); + if (p->symtab) + chars_free(p->symtab); free(p); } @@ -104,7 +106,7 @@ term_alloc(enum termenc enc) struct termp *p; if (NULL == (p = malloc(sizeof(struct termp)))) - err(1, "malloc"); + return(NULL); bzero(p, sizeof(struct termp)); p->maxrmargin = 78; p->enc = enc; @@ -112,62 +114,6 @@ term_alloc(enum termenc enc) } -static int -isclosedelim(const char *p) -{ - - if ( ! (*p && 0 == *(p + 1))) - return(0); - - switch (*p) { - case('.'): - /* FALLTHROUGH */ - case(','): - /* FALLTHROUGH */ - case(';'): - /* FALLTHROUGH */ - case(':'): - /* FALLTHROUGH */ - case('?'): - /* FALLTHROUGH */ - case('!'): - /* FALLTHROUGH */ - case(')'): - /* FALLTHROUGH */ - case(']'): - /* FALLTHROUGH */ - case('}'): - return(1); - default: - break; - } - - return(0); -} - - -static int -isopendelim(const char *p) -{ - - if ( ! (*p && 0 == *(p + 1))) - return(0); - - switch (*p) { - case('('): - /* FALLTHROUGH */ - case('['): - /* FALLTHROUGH */ - case('{'): - return(1); - default: - break; - } - - return(0); -} - - /* * 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 @@ -389,10 +335,17 @@ do_special(struct termp *p, const char *word, size_t l size_t sz; int i; - rhs = term_a2ascii(p->symtab, word, len, &sz); + rhs = chars_a2ascii(p->symtab, word, len, &sz); - if (NULL == rhs) + if (NULL == rhs) { +#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]); } @@ -405,10 +358,17 @@ do_reserved(struct termp *p, const char *word, size_t size_t sz; int i; - rhs = term_a2res(p->symtab, word, len, &sz); + rhs = chars_a2res(p->symtab, word, len, &sz); - if (NULL == rhs) + if (NULL == rhs) { +#if 0 + fputs("Unknown reserved word: ", 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]); } @@ -422,10 +382,11 @@ do_reserved(struct termp *p, const char *word, size_t static void do_escaped(struct termp *p, const char **word) { - int j; + int j, type; const char *wp; wp = *word; + type = 1; if (0 == *(++wp)) { *word = wp; @@ -461,6 +422,7 @@ do_escaped(struct termp *p, const char **word) *word = ++wp; return; case ('['): + type = 0; break; default: do_reserved(p, wp, 1); @@ -476,15 +438,15 @@ do_escaped(struct termp *p, const char **word) switch (*wp) { case ('B'): - p->flags |= TERMP_BOLD; + p->bold++; break; case ('I'): - p->flags |= TERMP_UNDER; + p->under++; break; case ('P'): /* FALLTHROUGH */ case ('R'): - p->flags &= ~TERMP_STYLE; + p->bold = p->under = 0; break; default: break; @@ -508,7 +470,10 @@ do_escaped(struct termp *p, const char **word) return; } - do_special(p, wp - j, (size_t)j); + if (type) + do_special(p, wp - j, (size_t)j); + else + do_reserved(p, wp - j, (size_t)j); *word = wp; } @@ -523,24 +488,58 @@ term_word(struct termp *p, const char *word) { const char *sv; - if (isclosedelim(word)) - if ( ! (TERMP_IGNDELIM & p->flags)) - p->flags |= TERMP_NOSPACE; + sv = word; + if (word[0] && 0 == word[1]) + switch (word[0]) { + case('.'): + /* FALLTHROUGH */ + case(','): + /* FALLTHROUGH */ + case(';'): + /* FALLTHROUGH */ + case(':'): + /* FALLTHROUGH */ + case('?'): + /* FALLTHROUGH */ + case('!'): + /* FALLTHROUGH */ + case(')'): + /* FALLTHROUGH */ + case(']'): + /* FALLTHROUGH */ + case('}'): + if ( ! (TERMP_IGNDELIM & p->flags)) + p->flags |= TERMP_NOSPACE; + break; + default: + break; + } + if ( ! (TERMP_NOSPACE & p->flags)) buffer(p, ' '); if ( ! (p->flags & TERMP_NONOSPACE)) p->flags &= ~TERMP_NOSPACE; - for (sv = word; *word; word++) + for ( ; *word; word++) if ('\\' != *word) encode(p, *word); else do_escaped(p, &word); - if (isopendelim(sv)) - p->flags |= TERMP_NOSPACE; + if (sv[0] && 0 == sv[1]) + switch (sv[0]) { + case('('): + /* FALLTHROUGH */ + case('['): + /* FALLTHROUGH */ + case('{'): + p->flags |= TERMP_NOSPACE; + break; + default: + break; + } } @@ -560,7 +559,7 @@ buffer(struct termp *p, char c) s = p->maxcols * 2; p->buf = realloc(p->buf, s); if (NULL == p->buf) - err(1, "realloc"); + err(1, "realloc"); /* FIXME: shouldn't be here! */ p->maxcols = s; } p->buf[(int)(p->col)++] = c; @@ -571,12 +570,12 @@ static void encode(struct termp *p, char c) { - if (' ' != c && TERMP_STYLE & p->flags) { - if (TERMP_BOLD & p->flags) { + if (' ' != c) { + if (p->bold) { buffer(p, c); buffer(p, 8); } - if (TERMP_UNDER & p->flags) { + if (p->under) { buffer(p, '_'); buffer(p, 8); }