=================================================================== RCS file: /cvs/mandoc/Attic/mdocterm.c,v retrieving revision 1.4 retrieving revision 1.12 diff -u -p -r1.4 -r1.12 --- mandoc/Attic/mdocterm.c 2009/02/23 09:33:34 1.4 +++ mandoc/Attic/mdocterm.c 2009/02/25 15:12:26 1.12 @@ -1,4 +1,4 @@ - /* $Id: mdocterm.c,v 1.4 2009/02/23 09:33:34 kristaps Exp $ */ +/* $Id: mdocterm.c,v 1.12 2009/02/25 15:12:26 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -24,9 +24,19 @@ #include #include +#ifndef __OpenBSD__ +#include +#endif + #include "mmain.h" #include "term.h" +#ifdef __NetBSD__ +#define xisspace(x) isspace((int)(x)) +#else +#define xisspace(x) isspace((x)) +#endif + enum termstyle { STYLE_CLEAR, STYLE_BOLD, @@ -34,6 +44,7 @@ enum termstyle { }; static void body(struct termp *, + struct termpair *, const struct mdoc_meta *, const struct mdoc_node *); static void header(struct termp *, @@ -44,6 +55,8 @@ static void footer(struct termp *, static void pword(struct termp *, const char *, size_t); static void pescape(struct termp *, const char *, size_t *, size_t); +static void nescape(struct termp *, + const char *, size_t); static void chara(struct termp *, char); static void stringa(struct termp *, const char *); static void style(struct termp *, enum termstyle); @@ -61,9 +74,6 @@ main(int argc, char *argv[]) const struct mdoc *mdoc; struct termp termp; - extern int optreset; - extern int optind; - p = mmain_alloc(); if ( ! mmain_getopt(p, argc, argv, NULL, NULL, NULL, NULL)) @@ -82,7 +92,7 @@ main(int argc, char *argv[]) err(1, "malloc"); header(&termp, mdoc_meta(mdoc)); - body(&termp, mdoc_meta(mdoc), mdoc_node(mdoc)); + body(&termp, NULL, mdoc_meta(mdoc), mdoc_node(mdoc)); footer(&termp, mdoc_meta(mdoc)); free(termp.buf); @@ -136,11 +146,11 @@ flushln(struct termp *p) * Count up visible word characters. Control sequences * (starting with the CSI) aren't counted. */ - assert( ! isspace(p->buf[i])); + assert( ! xisspace(p->buf[i])); /* LINTED */ for (j = i, vsz = 0; j < p->col; j++) { - if (isspace(p->buf[j])) + if (xisspace(p->buf[j])) break; else if (27 == p->buf[j]) { assert(j + 4 <= p->col); @@ -166,10 +176,9 @@ flushln(struct termp *p) for (j = 0; j < p->offset; j++) putchar(' '); vis = 0; - } else if (vis + vsz >= maxvis) { + } else if (vis + vsz >= maxvis) /* FIXME */ errx(1, "word breaks right margin"); - } /* * Write out the word and a trailing space. Omit the @@ -177,7 +186,7 @@ flushln(struct termp *p) */ for ( ; i < p->col; i++) { - if (isspace(p->buf[i])) + if (xisspace(p->buf[i])) break; putchar(p->buf[i]); } @@ -194,7 +203,7 @@ flushln(struct termp *p) */ if (p->flags & TERMP_NOBREAK) { - for ( ; vis <= maxvis; vis++) + for ( ; vis < maxvis; vis++) putchar(' '); } else putchar('\n'); @@ -212,9 +221,12 @@ newln(struct termp *p) * vertical space. */ p->flags |= TERMP_NOSPACE; - if (0 == p->col) + if (0 == p->col) { + p->flags &= ~TERMP_NOLPAD; return; + } flushln(p); + p->flags &= ~TERMP_NOLPAD; } @@ -280,8 +292,36 @@ style(struct termp *p, enum termstyle esc) static void +nescape(struct termp *p, const char *word, size_t len) +{ + + switch (len) { + case (2): + if ('r' == word[0] && 'B' == word[1]) + chara(p, ']'); + else if ('l' == word[0] && 'B' == word[1]) + chara(p, '['); + else if ('<' == word[0] && '-' == word[1]) + stringa(p, "<-"); + else if ('-' == word[0] && '>' == word[1]) + stringa(p, "->"); + else if ('l' == word[0] && 'q' == word[1]) + chara(p, '\"'); + else if ('r' == word[0] && 'q' == word[1]) + chara(p, '\"'); + else if ('b' == word[0] && 'u' == word[1]) + chara(p, 'o'); + break; + default: + break; + } +} + + +static void pescape(struct termp *p, const char *word, size_t *i, size_t len) { + size_t j; (*i)++; assert(*i < len); @@ -290,16 +330,7 @@ pescape(struct termp *p, const char *word, size_t *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, '['); - else if ('<' == word[*i] && '-' == word[*i + 1]) - stringa(p, "<-"); - else if ('-' == word[*i] && '>' == word[*i + 1]) - stringa(p, "->"); - + nescape(p, &word[*i], 2); (*i)++; return; @@ -321,7 +352,12 @@ pescape(struct termp *p, const char *word, size_t *i, } return; } - /* n-character escapes. */ + + (*i)++; + for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++) + /* Loop... */ ; + + nescape(p, &word[*i - j], j); } @@ -378,7 +414,7 @@ word(struct termp *p, const char *word) /* LINTED */ for (j = i = 0; i < len; i++) { - if ( ! isspace(word[i])) { + if ( ! xisspace(word[i])) { j++; continue; } @@ -396,37 +432,50 @@ word(struct termp *p, const char *word) static void -body(struct termp *p, const struct mdoc_meta *meta, +body(struct termp *p, struct termpair *ppair, + const struct mdoc_meta *meta, const struct mdoc_node *node) { int dochild; + struct termpair pair; /* Pre-processing. */ dochild = 1; + pair.ppair = ppair; + pair.type = 0; + pair.offset = pair.rmargin = 0; + pair.flag = 0; + pair.count = 0; if (MDOC_TEXT != node->type) { if (termacts[node->tok].pre) - if ( ! (*termacts[node->tok].pre)(p, meta, node)) + if ( ! (*termacts[node->tok].pre)(p, &pair, meta, node)) dochild = 0; } else /* MDOC_TEXT == node->type */ word(p, node->data.text.string); /* Children. */ + if (TERMPAIR_FLAG & pair.type) + p->flags |= pair.flag; + if (dochild && node->child) - body(p, meta, node->child); + body(p, &pair, meta, node->child); + if (TERMPAIR_FLAG & pair.type) + p->flags &= ~pair.flag; + /* Post-processing. */ if (MDOC_TEXT != node->type) if (termacts[node->tok].post) - (*termacts[node->tok].post)(p, meta, node); + (*termacts[node->tok].post)(p, &pair, meta, node); /* Siblings. */ if (node->next) - body(p, meta, node->next); + body(p, ppair, meta, node->next); } @@ -444,10 +493,10 @@ footer(struct termp *p, const struct mdoc_meta *meta) tm = localtime(&meta->date); -#ifdef __linux__ - if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm)) -#else +#ifdef __OpenBSD__ if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm)) +#else + if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm)) #endif err(1, "strftime");