=================================================================== RCS file: /cvs/mandoc/term.c,v retrieving revision 1.5 retrieving revision 1.8 diff -u -p -r1.5 -r1.8 --- mandoc/term.c 2009/02/21 19:05:28 1.5 +++ mandoc/term.c 2009/02/22 15:50:45 1.8 @@ -1,4 +1,4 @@ -/* $Id: term.c,v 1.5 2009/02/21 19:05:28 kristaps Exp $ */ +/* $Id: term.c,v 1.8 2009/02/22 15:50:45 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -24,8 +24,11 @@ #include #include +#ifdef __linux__ +#include +#endif + #include "term.h" -#include "private.h" /* XXX */ enum termstyle { STYLE_CLEAR, @@ -42,9 +45,15 @@ static void termprint_footer(struct termp *, const struct mdoc_meta *); static void pword(struct termp *, const char *, size_t); +static void pescape(struct termp *, + const char *, size_t *, size_t); static void chara(struct termp *, char); -static void escape(struct termp *, enum termstyle); +static void style(struct termp *, enum termstyle); +#ifdef __linux__ +extern size_t strlcat(char *, const char *, size_t); +extern size_t strlcpy(char *, const char *, size_t); +#endif void flushln(struct termp *p) @@ -73,6 +82,18 @@ flushln(struct termp *p) for (j = 0; j < p->offset; j++) putchar(' '); + /* + * If we're literal, print out verbatim. + */ + if (p->flags & TERMP_LITERAL) { + /* FIXME: count non-printing chars. */ + for (i = 0; i < p->col; i++) + putchar(p->buf[i]); + putchar('\n'); + p->col = 0; + return; + } + for (i = 0; i < p->col; i++) { /* * Count up visible word characters. Control sequences @@ -185,7 +206,7 @@ chara(struct termp *p, char c) static void -escape(struct termp *p, enum termstyle esc) +style(struct termp *p, enum termstyle esc) { if (p->col + 4 >= p->maxcols) @@ -212,30 +233,76 @@ escape(struct termp *p, enum termstyle esc) static void +pescape(struct termp *p, const char *word, size_t *i, size_t len) +{ + + (*i)++; + assert(*i < len); + + if ('(' == word[*i]) { + /* Two-character escapes. */ + (*i)++; + assert(*i + 1 < len); + + if ('r' == word[*i] && 'B' == word[*i + 1]) + chara(p, ']'); + else if ('l' == word[*i] && 'B' == word[*i + 1]) + chara(p, '['); + + (*i)++; + return; + + } else if ('[' != word[*i]) { + /* One-character escapes. */ + switch (word[*i]) { + case ('\\'): + /* FALLTHROUGH */ + case ('\''): + /* FALLTHROUGH */ + case ('`'): + /* FALLTHROUGH */ + case ('-'): + /* FALLTHROUGH */ + case ('.'): + chara(p, word[*i]); + default: + break; + } + return; + } + /* n-character escapes. */ +} + + +static void pword(struct termp *p, const char *word, size_t len) { size_t i; - assert(len > 0); + /*assert(len > 0);*/ /* Can be, if literal. */ - if ( ! (p->flags & TERMP_NOSPACE)) + if ( ! (p->flags & TERMP_NOSPACE) && + ! (p->flags & TERMP_LITERAL)) chara(p, ' '); p->flags &= ~TERMP_NOSPACE; if (p->flags & TERMP_BOLD) - escape(p, STYLE_BOLD); + style(p, STYLE_BOLD); if (p->flags & TERMP_UNDERLINE) - escape(p, STYLE_UNDERLINE); + style(p, STYLE_UNDERLINE); - /* TODO: escape patterns. */ - - for (i = 0; i < len; i++) + for (i = 0; i < len; i++) { + if ('\\' == word[i]) { + pescape(p, word, &i, len); + continue; + } chara(p, word[i]); + } if (p->flags & TERMP_BOLD || p->flags & TERMP_UNDERLINE) - escape(p, STYLE_CLEAR); + style(p, STYLE_CLEAR); } @@ -244,12 +311,17 @@ word(struct termp *p, const char *word) { size_t i, j, len; - if (mdoc_isdelim(word)) - p->flags |= TERMP_NOSPACE; + if (p->flags & TERMP_LITERAL) { + pword(p, word, strlen(word)); + return; + } len = strlen(word); assert(len > 0); + if (mdoc_isdelim(word)) + p->flags |= TERMP_NOSPACE; + /* LINTED */ for (j = i = 0; i < len; i++) { if ( ! isspace(word[i])) { @@ -293,11 +365,9 @@ termprint_r(struct termp *p, const struct mdoc_meta *m /* Post-processing. */ - if (MDOC_TEXT != node->type) { + if (MDOC_TEXT != node->type) if (termacts[node->tok].post) - if ( ! (*termacts[node->tok].post)(p, meta, node)) - return; - } + (*termacts[node->tok].post)(p, meta, node); /* Siblings. */ @@ -319,7 +389,12 @@ termprint_footer(struct termp *p, const struct mdoc_me err(1, "malloc"); tm = localtime(&meta->date); + +#ifdef __linux__ + if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm)) +#else if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm)) +#endif err(1, "strftime"); osz = strlcpy(os, meta->os, p->rmargin); @@ -351,7 +426,8 @@ termprint_footer(struct termp *p, const struct mdoc_me static void termprint_header(struct termp *p, const struct mdoc_meta *meta) { - char *msec, *buf, *title, *pp; + char *buf, *title; + const char *pp, *msec; size_t ssz, tsz, ttsz, i;; if (NULL == (buf = malloc(p->rmargin))) @@ -439,7 +515,7 @@ termprint_header(struct termp *p, const struct mdoc_me } -int +void termprint(const struct mdoc_node *node, const struct mdoc_meta *meta) { @@ -459,8 +535,6 @@ termprint(const struct mdoc_node *node, termprint_footer(&p, meta); free(p.buf); - - return(1); }