[BACK]Return to terminal.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/terminal.c, Revision 1.4

1.4     ! kristaps    1: /* $Id: terminal.c,v 1.3 2009/03/20 15:14:01 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19: #include <assert.h>
                     20: #include <err.h>
                     21: #include <stdio.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
                     25: #include "term.h"
                     26:
1.2       kristaps   27: #ifdef __linux__
                     28: extern size_t            strlcpy(char *, const char *, size_t);
                     29: extern size_t            strlcat(char *, const char *, size_t);
                     30: #endif
                     31:
1.1       kristaps   32: static struct termp     *termp_alloc(enum termenc);
                     33: static void              termp_free(struct termp *);
                     34: static void              termp_body(struct termp *, struct termpair *,
                     35:                                const struct mdoc_meta *,
                     36:                                const struct mdoc_node *);
                     37: static void              termp_head(struct termp *,
                     38:                                const struct mdoc_meta *);
                     39: static void              termp_foot(struct termp *,
                     40:                                const struct mdoc_meta *);
                     41: static void              termp_pword(struct termp *, const char *, int);
                     42: static void              termp_pescape(struct termp *,
                     43:                                const char *, int *, int);
                     44: static void              termp_nescape(struct termp *,
                     45:                                const char *, size_t);
                     46: static void              termp_chara(struct termp *, char);
                     47: static void              termp_stringa(struct termp *,
                     48:                                const char *, size_t);
                     49: static void              sanity(const struct mdoc_node *); /* XXX */
                     50:
                     51:
                     52: void *
                     53: latin1_alloc(void)
                     54: {
                     55:
                     56:        return(termp_alloc(TERMENC_LATIN1));
                     57: }
                     58:
                     59:
                     60: void *
                     61: utf8_alloc(void)
                     62: {
                     63:
                     64:        return(termp_alloc(TERMENC_UTF8));
                     65: }
                     66:
                     67:
                     68: void *
                     69: ascii_alloc(void)
                     70: {
                     71:
                     72:        return(termp_alloc(TERMENC_ASCII));
                     73: }
                     74:
                     75:
                     76: int
                     77: terminal_run(void *arg, const struct mdoc *mdoc)
                     78: {
                     79:        struct termp    *p;
                     80:
                     81:        p = (struct termp *)arg;
                     82:
                     83:        if (NULL == p->symtab)
1.3       kristaps   84:                p->symtab = term_ascii2htab();
1.1       kristaps   85:
                     86:        termp_head(p, mdoc_meta(mdoc));
                     87:        termp_body(p, NULL, mdoc_meta(mdoc), mdoc_node(mdoc));
                     88:        termp_foot(p, mdoc_meta(mdoc));
                     89:
                     90:        return(1);
                     91: }
                     92:
                     93:
                     94: void
                     95: terminal_free(void *arg)
                     96: {
                     97:
                     98:        termp_free((struct termp *)arg);
                     99: }
                    100:
                    101:
                    102: static void
                    103: termp_free(struct termp *p)
                    104: {
                    105:
                    106:        if (p->buf)
                    107:                free(p->buf);
                    108:        if (TERMENC_ASCII == p->enc && p->symtab)
1.3       kristaps  109:                term_asciifree(p->symtab);
1.1       kristaps  110:
                    111:        free(p);
                    112: }
                    113:
                    114:
                    115: static struct termp *
                    116: termp_alloc(enum termenc enc)
                    117: {
                    118:        struct termp *p;
                    119:
                    120:        if (NULL == (p = malloc(sizeof(struct termp))))
                    121:                err(1, "malloc");
                    122:        bzero(p, sizeof(struct termp));
                    123:        p->maxrmargin = 78;
                    124:        p->enc = enc;
                    125:        return(p);
                    126: }
                    127:
                    128:
                    129: /*
                    130:  * Flush a line of text.  A "line" is loosely defined as being something
                    131:  * that should be followed by a newline, regardless of whether it's
                    132:  * broken apart by newlines getting there.  A line can also be a
                    133:  * fragment of a columnar list.
                    134:  *
                    135:  * Specifically, a line is whatever's in p->buf of length p->col, which
                    136:  * is zeroed after this function returns.
                    137:  *
                    138:  * The variables TERMP_NOLPAD, TERMP_LITERAL and TERMP_NOBREAK are of
                    139:  * critical importance here.  Their behaviour follows:
                    140:  *
                    141:  *  - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
                    142:  *    offset value.  This is useful when doing columnar lists where the
                    143:  *    prior column has right-padded.
                    144:  *
                    145:  *  - TERMP_NOBREAK: this is the most important and is used when making
                    146:  *    columns.  In short: don't print a newline and instead pad to the
                    147:  *    right margin.  Used in conjunction with TERMP_NOLPAD.
                    148:  *
                    149:  *  - TERMP_NONOBREAK: don't newline when TERMP_NOBREAK is specified.
                    150:  *
                    151:  *  In-line line breaking:
                    152:  *
                    153:  *  If TERMP_NOBREAK is specified and the line overruns the right
                    154:  *  margin, it will break and pad-right to the right margin after
                    155:  *  writing.  If maxrmargin is violated, it will break and continue
                    156:  *  writing from the right-margin, which will lead to the above
                    157:  *  scenario upon exit.
                    158:  *
                    159:  *  Otherwise, the line will break at the right margin.  Extremely long
                    160:  *  lines will cause the system to emit a warning (TODO: hyphenate, if
                    161:  *  possible).
                    162:  */
                    163: void
1.3       kristaps  164: term_flushln(struct termp *p)
1.1       kristaps  165: {
                    166:        int              i, j;
                    167:        size_t           vsz, vis, maxvis, mmax, bp;
                    168:
                    169:        /*
                    170:         * First, establish the maximum columns of "visible" content.
                    171:         * This is usually the difference between the right-margin and
                    172:         * an indentation, but can be, for tagged lists or columns, a
                    173:         * small set of values.
                    174:         */
                    175:
                    176:        assert(p->offset < p->rmargin);
                    177:        maxvis = p->rmargin - p->offset;
                    178:        mmax = p->maxrmargin - p->offset;
                    179:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
                    180:        vis = 0;
                    181:
                    182:        /*
                    183:         * If in the standard case (left-justified), then begin with our
                    184:         * indentation, otherwise (columns, etc.) just start spitting
                    185:         * out text.
                    186:         */
                    187:
                    188:        if ( ! (p->flags & TERMP_NOLPAD))
                    189:                /* LINTED */
                    190:                for (j = 0; j < (int)p->offset; j++)
                    191:                        putchar(' ');
                    192:
                    193:        for (i = 0; i < (int)p->col; i++) {
                    194:                /*
                    195:                 * Count up visible word characters.  Control sequences
                    196:                 * (starting with the CSI) aren't counted.  A space
                    197:                 * generates a non-printing word, which is valid (the
                    198:                 * space is printed according to regular spacing rules).
                    199:                 */
                    200:
                    201:                /* LINTED */
                    202:                for (j = i, vsz = 0; j < (int)p->col; j++) {
                    203:                        if (' ' == p->buf[j])
                    204:                                break;
                    205:                        else if (8 == p->buf[j])
                    206:                                j += 1;
                    207:                        else
                    208:                                vsz++;
                    209:                }
                    210:
                    211:                /*
                    212:                 * Do line-breaking.  If we're greater than our
                    213:                 * break-point and already in-line, break to the next
                    214:                 * line and start writing.  If we're at the line start,
                    215:                 * then write out the word (TODO: hyphenate) and break
                    216:                 * in a subsequent loop invocation.
                    217:                 */
                    218:
                    219:                if ( ! (TERMP_NOBREAK & p->flags)) {
                    220:                        if (vis && vis + vsz > bp) {
                    221:                                putchar('\n');
                    222:                                for (j = 0; j < (int)p->offset; j++)
                    223:                                        putchar(' ');
                    224:                                vis = 0;
1.4     ! kristaps  225:                        }
        !           226:                } else if (vis && vis + vsz > bp) {
        !           227:                        putchar('\n');
        !           228:                        for (j = 0; j < (int)p->rmargin; j++)
        !           229:                                putchar(' ');
        !           230:                        vis = p->rmargin - p->offset;
1.1       kristaps  231:                }
                    232:
                    233:                /*
                    234:                 * Write out the word and a trailing space.  Omit the
                    235:                 * space if we're the last word in the line or beyond
                    236:                 * our breakpoint.
                    237:                 */
                    238:
                    239:                for ( ; i < (int)p->col; i++) {
                    240:                        if (' ' == p->buf[i])
                    241:                                break;
                    242:                        putchar(p->buf[i]);
                    243:                }
                    244:                vis += vsz;
                    245:                if (i < (int)p->col && vis <= bp) {
                    246:                        putchar(' ');
                    247:                        vis++;
                    248:                }
                    249:        }
                    250:
                    251:        /*
                    252:         * If we've overstepped our maximum visible no-break space, then
                    253:         * cause a newline and offset at the right margin.
                    254:         */
                    255:
                    256:        if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) {
                    257:                if ( ! (TERMP_NONOBREAK & p->flags)) {
                    258:                        putchar('\n');
                    259:                        for (i = 0; i < (int)p->rmargin; i++)
                    260:                                putchar(' ');
                    261:                }
                    262:                p->col = 0;
                    263:                return;
                    264:        }
                    265:
                    266:        /*
                    267:         * If we're not to right-marginalise it (newline), then instead
                    268:         * pad to the right margin and stay off.
                    269:         */
                    270:
                    271:        if (p->flags & TERMP_NOBREAK) {
                    272:                if ( ! (TERMP_NONOBREAK & p->flags))
                    273:                        for ( ; vis < maxvis; vis++)
                    274:                                putchar(' ');
                    275:        } else
                    276:                putchar('\n');
                    277:
                    278:        p->col = 0;
                    279: }
                    280:
                    281:
                    282: /*
                    283:  * A newline only breaks an existing line; it won't assert vertical
                    284:  * space.  All data in the output buffer is flushed prior to the newline
                    285:  * assertion.
                    286:  */
                    287: void
1.3       kristaps  288: term_newln(struct termp *p)
1.1       kristaps  289: {
                    290:
                    291:        p->flags |= TERMP_NOSPACE;
                    292:        if (0 == p->col) {
                    293:                p->flags &= ~TERMP_NOLPAD;
                    294:                return;
                    295:        }
1.3       kristaps  296:        term_flushln(p);
1.1       kristaps  297:        p->flags &= ~TERMP_NOLPAD;
                    298: }
                    299:
                    300:
                    301: /*
                    302:  * Asserts a vertical space (a full, empty line-break between lines).
                    303:  * Note that if used twice, this will cause two blank spaces and so on.
                    304:  * All data in the output buffer is flushed prior to the newline
                    305:  * assertion.
                    306:  */
                    307: void
1.3       kristaps  308: term_vspace(struct termp *p)
1.1       kristaps  309: {
                    310:
1.3       kristaps  311:        term_newln(p);
1.1       kristaps  312:        putchar('\n');
                    313: }
                    314:
                    315:
                    316: /*
                    317:  * Break apart a word into "pwords" (partial-words, usually from
                    318:  * breaking up a phrase into individual words) and, eventually, put them
                    319:  * into the output buffer.  If we're a literal word, then don't break up
                    320:  * the word and put it verbatim into the output buffer.
                    321:  */
                    322: void
1.3       kristaps  323: term_word(struct termp *p, const char *word)
1.1       kristaps  324: {
                    325:        int              i, j, len;
                    326:
1.3       kristaps  327:        len = (int)strlen(word);
                    328:
1.1       kristaps  329:        if (p->flags & TERMP_LITERAL) {
1.3       kristaps  330:                termp_pword(p, word, len);
1.1       kristaps  331:                return;
                    332:        }
                    333:
                    334:        if (mdoc_isdelim(word)) {
                    335:                if ( ! (p->flags & TERMP_IGNDELIM))
                    336:                        p->flags |= TERMP_NOSPACE;
                    337:                p->flags &= ~TERMP_IGNDELIM;
                    338:        }
                    339:
                    340:        /* LINTED */
                    341:        for (j = i = 0; i < len; i++) {
                    342:                if (' ' != word[i]) {
                    343:                        j++;
                    344:                        continue;
                    345:                }
                    346:
                    347:                /* Escaped spaces don't delimit... */
                    348:                if (i && ' ' == word[i] && '\\' == word[i - 1]) {
                    349:                        j++;
                    350:                        continue;
                    351:                }
                    352:
                    353:                if (0 == j)
                    354:                        continue;
                    355:                assert(i >= j);
                    356:                termp_pword(p, &word[i - j], j);
                    357:                j = 0;
                    358:        }
                    359:        if (j > 0) {
                    360:                assert(i >= j);
                    361:                termp_pword(p, &word[i - j], j);
                    362:        }
                    363: }
                    364:
                    365:
1.3       kristaps  366: static void
                    367: termp_body(struct termp *p, struct termpair *ppair,
                    368:                const struct mdoc_meta *meta,
                    369:                const struct mdoc_node *node)
                    370: {
                    371:
                    372:        term_node(p, ppair, meta, node);
                    373:        if (node->next)
                    374:                termp_body(p, ppair, meta, node->next);
                    375: }
                    376:
                    377:
1.1       kristaps  378: /*
                    379:  * This is the main function for printing out nodes.  It's constituted
                    380:  * of PRE and POST functions, which correspond to prefix and infix
                    381:  * processing.  The termpair structure allows data to persist between
                    382:  * prefix and postfix invocations.
                    383:  */
1.3       kristaps  384: void
                    385: term_node(struct termp *p, struct termpair *ppair,
1.1       kristaps  386:                const struct mdoc_meta *meta,
                    387:                const struct mdoc_node *node)
                    388: {
                    389:        int              dochild;
                    390:        struct termpair  pair;
                    391:
                    392:        /* Some quick sanity-checking. */
                    393:
                    394:        sanity(node);
                    395:
                    396:        /* Pre-processing. */
                    397:
                    398:        dochild = 1;
                    399:        pair.ppair = ppair;
                    400:        pair.type = 0;
                    401:        pair.offset = pair.rmargin = 0;
                    402:        pair.flag = 0;
                    403:        pair.count = 0;
                    404:
                    405:        if (MDOC_TEXT != node->type) {
                    406:                if (termacts[node->tok].pre)
                    407:                        if ( ! (*termacts[node->tok].pre)(p, &pair, meta, node))
                    408:                                dochild = 0;
                    409:        } else /* MDOC_TEXT == node->type */
1.3       kristaps  410:                term_word(p, node->string);
1.1       kristaps  411:
                    412:        /* Children. */
                    413:
                    414:        if (TERMPAIR_FLAG & pair.type)
                    415:                p->flags |= pair.flag;
                    416:
                    417:        if (dochild && node->child)
                    418:                termp_body(p, &pair, meta, node->child);
                    419:
                    420:        if (TERMPAIR_FLAG & pair.type)
                    421:                p->flags &= ~pair.flag;
                    422:
                    423:        /* Post-processing. */
                    424:
                    425:        if (MDOC_TEXT != node->type)
                    426:                if (termacts[node->tok].post)
                    427:                        (*termacts[node->tok].post)(p, &pair, meta, node);
                    428: }
                    429:
                    430:
                    431: static void
                    432: termp_foot(struct termp *p, const struct mdoc_meta *meta)
                    433: {
                    434:        struct tm       *tm;
                    435:        char            *buf, *os;
                    436:
                    437:        if (NULL == (buf = malloc(p->rmargin)))
                    438:                err(1, "malloc");
                    439:        if (NULL == (os = malloc(p->rmargin)))
                    440:                err(1, "malloc");
                    441:
                    442:        tm = localtime(&meta->date);
                    443:
                    444: #ifdef __OpenBSD__
                    445:        if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                    446: #else
                    447:        if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                    448: #endif
                    449:                err(1, "strftime");
                    450:
                    451:        (void)strlcpy(os, meta->os, p->rmargin);
                    452:
                    453:        /*
                    454:         * This is /slightly/ different from regular groff output
                    455:         * because we don't have page numbers.  Print the following:
                    456:         *
                    457:         * OS                                            MDOCDATE
                    458:         */
                    459:
1.3       kristaps  460:        term_vspace(p);
1.1       kristaps  461:
                    462:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
                    463:        p->rmargin = p->maxrmargin - strlen(buf);
                    464:        p->offset = 0;
                    465:
1.3       kristaps  466:        term_word(p, os);
                    467:        term_flushln(p);
1.1       kristaps  468:
                    469:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    470:        p->offset = p->rmargin;
                    471:        p->rmargin = p->maxrmargin;
                    472:        p->flags &= ~TERMP_NOBREAK;
                    473:
1.3       kristaps  474:        term_word(p, buf);
                    475:        term_flushln(p);
1.1       kristaps  476:
                    477:        free(buf);
                    478:        free(os);
                    479: }
                    480:
                    481:
                    482: static void
                    483: termp_head(struct termp *p, const struct mdoc_meta *meta)
                    484: {
                    485:        char            *buf, *title;
                    486:
                    487:        p->rmargin = p->maxrmargin;
                    488:        p->offset = 0;
                    489:
                    490:        if (NULL == (buf = malloc(p->rmargin)))
                    491:                err(1, "malloc");
                    492:        if (NULL == (title = malloc(p->rmargin)))
                    493:                err(1, "malloc");
                    494:
                    495:        /*
                    496:         * The header is strange.  It has three components, which are
                    497:         * really two with the first duplicated.  It goes like this:
                    498:         *
                    499:         * IDENTIFIER              TITLE                   IDENTIFIER
                    500:         *
                    501:         * The IDENTIFIER is NAME(SECTION), which is the command-name
                    502:         * (if given, or "unknown" if not) followed by the manual page
                    503:         * section.  These are given in `Dt'.  The TITLE is a free-form
                    504:         * string depending on the manual volume.  If not specified, it
                    505:         * switches on the manual section.
                    506:         */
                    507:
                    508:        assert(meta->vol);
                    509:        (void)strlcpy(buf, meta->vol, p->rmargin);
                    510:
                    511:        if (meta->arch) {
                    512:                (void)strlcat(buf, " (", p->rmargin);
                    513:                (void)strlcat(buf, meta->arch, p->rmargin);
                    514:                (void)strlcat(buf, ")", p->rmargin);
                    515:        }
                    516:
                    517:        (void)snprintf(title, p->rmargin, "%s(%d)",
                    518:                        meta->title, meta->msec);
                    519:
                    520:        p->offset = 0;
                    521:        p->rmargin = (p->maxrmargin - strlen(buf)) / 2;
                    522:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
                    523:
1.3       kristaps  524:        term_word(p, title);
                    525:        term_flushln(p);
1.1       kristaps  526:
                    527:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    528:        p->offset = p->rmargin;
                    529:        p->rmargin = p->maxrmargin - strlen(title);
                    530:
1.3       kristaps  531:        term_word(p, buf);
                    532:        term_flushln(p);
1.1       kristaps  533:
                    534:        p->offset = p->rmargin;
                    535:        p->rmargin = p->maxrmargin;
                    536:        p->flags &= ~TERMP_NOBREAK;
                    537:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    538:
1.3       kristaps  539:        term_word(p, title);
                    540:        term_flushln(p);
1.1       kristaps  541:
                    542:        p->rmargin = p->maxrmargin;
                    543:        p->offset = 0;
                    544:        p->flags &= ~TERMP_NOSPACE;
                    545:
                    546:        free(title);
                    547:        free(buf);
                    548: }
                    549:
                    550:
                    551: /*
                    552:  * Determine the symbol indicated by an escape sequences, that is, one
                    553:  * starting with a backslash.  Once done, we pass this value into the
                    554:  * output buffer by way of the symbol table.
                    555:  */
                    556: static void
                    557: termp_nescape(struct termp *p, const char *word, size_t len)
                    558: {
                    559:        const char      *rhs;
                    560:        size_t           sz;
                    561:
1.3       kristaps  562:        if (NULL == (rhs = term_a2ascii(p->symtab, word, len, &sz)))
1.1       kristaps  563:                return;
                    564:        termp_stringa(p, rhs, sz);
                    565: }
                    566:
                    567:
                    568: /*
                    569:  * Handle an escape sequence: determine its length and pass it to the
                    570:  * escape-symbol look table.  Note that we assume mdoc(3) has validated
                    571:  * the escape sequence (we assert upon badly-formed escape sequences).
                    572:  */
                    573: static void
                    574: termp_pescape(struct termp *p, const char *word, int *i, int len)
                    575: {
                    576:        int              j;
                    577:
                    578:        if (++(*i) >= len)
                    579:                return;
                    580:
                    581:        if ('(' == word[*i]) {
                    582:                (*i)++;
                    583:                if (*i + 1 >= len)
                    584:                        return;
                    585:
                    586:                termp_nescape(p, &word[*i], 2);
                    587:                (*i)++;
                    588:                return;
                    589:
                    590:        } else if ('*' == word[*i]) {
                    591:                (*i)++;
                    592:                if (*i >= len)
                    593:                        return;
                    594:
                    595:                switch (word[*i]) {
                    596:                case ('('):
                    597:                        (*i)++;
                    598:                        if (*i + 1 >= len)
                    599:                                return;
                    600:
                    601:                        termp_nescape(p, &word[*i], 2);
                    602:                        (*i)++;
                    603:                        return;
                    604:                case ('['):
                    605:                        break;
                    606:                default:
                    607:                        termp_nescape(p, &word[*i], 1);
                    608:                        return;
                    609:                }
                    610:
                    611:        } else if ('[' != word[*i]) {
                    612:                termp_nescape(p, &word[*i], 1);
                    613:                return;
                    614:        }
                    615:
                    616:        (*i)++;
                    617:        for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
                    618:                /* Loop... */ ;
                    619:
                    620:        if (0 == word[*i])
                    621:                return;
                    622:
                    623:        termp_nescape(p, &word[*i - j], (size_t)j);
                    624: }
                    625:
                    626:
                    627: /*
                    628:  * Handle pwords, partial words, which may be either a single word or a
                    629:  * phrase that cannot be broken down (such as a literal string).  This
                    630:  * handles word styling.
                    631:  */
                    632: static void
                    633: termp_pword(struct termp *p, const char *word, int len)
                    634: {
                    635:        int              i;
                    636:
1.3       kristaps  637:        if ( ! (TERMP_NOSPACE & p->flags))
1.1       kristaps  638:                termp_chara(p, ' ');
                    639:
                    640:        if ( ! (p->flags & TERMP_NONOSPACE))
                    641:                p->flags &= ~TERMP_NOSPACE;
                    642:
                    643:        /*
                    644:         * If ANSI (word-length styling), then apply our style now,
                    645:         * before the word.
                    646:         */
                    647:
                    648:        for (i = 0; i < len; i++) {
                    649:                if ('\\' == word[i]) {
                    650:                        termp_pescape(p, word, &i, len);
                    651:                        continue;
                    652:                }
                    653:
                    654:                if (TERMP_STYLE & p->flags) {
                    655:                        if (TERMP_BOLD & p->flags) {
                    656:                                termp_chara(p, word[i]);
                    657:                                termp_chara(p, 8);
                    658:                        }
                    659:                        if (TERMP_UNDER & p->flags) {
                    660:                                termp_chara(p, '_');
                    661:                                termp_chara(p, 8);
                    662:                        }
                    663:                }
                    664:
                    665:                termp_chara(p, word[i]);
                    666:        }
                    667: }
                    668:
                    669:
                    670: /*
                    671:  * Like termp_chara() but for arbitrary-length buffers.  Resize the
                    672:  * buffer by a factor of two (if the buffer is less than that) or the
                    673:  * buffer's size.
                    674:  */
                    675: static void
                    676: termp_stringa(struct termp *p, const char *c, size_t sz)
                    677: {
                    678:        size_t           s;
                    679:
                    680:        if (0 == sz)
                    681:                return;
                    682:
                    683:        assert(c);
                    684:        if (p->col + sz >= p->maxcols) {
                    685:                if (0 == p->maxcols)
                    686:                        p->maxcols = 256;
                    687:                s = sz > p->maxcols * 2 ? sz : p->maxcols * 2;
                    688:                p->buf = realloc(p->buf, s);
                    689:                if (NULL == p->buf)
                    690:                        err(1, "realloc");
                    691:                p->maxcols = s;
                    692:        }
                    693:
                    694:        (void)memcpy(&p->buf[(int)p->col], c, sz);
                    695:        p->col += sz;
                    696: }
                    697:
                    698:
                    699: /*
                    700:  * Insert a single character into the line-buffer.  If the buffer's
                    701:  * space is exceeded, then allocate more space by doubling the buffer
                    702:  * size.
                    703:  */
                    704: static void
                    705: termp_chara(struct termp *p, char c)
                    706: {
                    707:        size_t           s;
                    708:
                    709:        if (p->col + 1 >= p->maxcols) {
                    710:                if (0 == p->maxcols)
                    711:                        p->maxcols = 256;
                    712:                s = p->maxcols * 2;
                    713:                p->buf = realloc(p->buf, s);
                    714:                if (NULL == p->buf)
                    715:                        err(1, "realloc");
                    716:                p->maxcols = s;
                    717:        }
                    718:        p->buf[(int)(p->col)++] = c;
                    719: }
                    720:
                    721:
                    722: static void
                    723: sanity(const struct mdoc_node *n)
                    724: {
                    725:
                    726:        switch (n->type) {
                    727:        case (MDOC_TEXT):
                    728:                if (n->child)
                    729:                        errx(1, "regular form violated (1)");
                    730:                if (NULL == n->parent)
                    731:                        errx(1, "regular form violated (2)");
                    732:                if (NULL == n->string)
                    733:                        errx(1, "regular form violated (3)");
                    734:                switch (n->parent->type) {
                    735:                case (MDOC_TEXT):
                    736:                        /* FALLTHROUGH */
                    737:                case (MDOC_ROOT):
                    738:                        errx(1, "regular form violated (4)");
                    739:                        /* NOTREACHED */
                    740:                default:
                    741:                        break;
                    742:                }
                    743:                break;
                    744:        case (MDOC_ELEM):
                    745:                if (NULL == n->parent)
                    746:                        errx(1, "regular form violated (5)");
                    747:                switch (n->parent->type) {
                    748:                case (MDOC_TAIL):
                    749:                        /* FALLTHROUGH */
                    750:                case (MDOC_BODY):
                    751:                        /* FALLTHROUGH */
                    752:                case (MDOC_HEAD):
                    753:                        break;
                    754:                default:
                    755:                        errx(1, "regular form violated (6)");
                    756:                        /* NOTREACHED */
                    757:                }
                    758:                if (n->child) switch (n->child->type) {
                    759:                case (MDOC_TEXT):
                    760:                        break;
                    761:                default:
                    762:                        errx(1, "regular form violated (7(");
                    763:                        /* NOTREACHED */
                    764:                }
                    765:                break;
                    766:        case (MDOC_HEAD):
                    767:                /* FALLTHROUGH */
                    768:        case (MDOC_BODY):
                    769:                /* FALLTHROUGH */
                    770:        case (MDOC_TAIL):
                    771:                if (NULL == n->parent)
                    772:                        errx(1, "regular form violated (8)");
                    773:                if (MDOC_BLOCK != n->parent->type)
                    774:                        errx(1, "regular form violated (9)");
                    775:                if (n->child) switch (n->child->type) {
                    776:                case (MDOC_BLOCK):
                    777:                        /* FALLTHROUGH */
                    778:                case (MDOC_ELEM):
                    779:                        /* FALLTHROUGH */
                    780:                case (MDOC_TEXT):
                    781:                        break;
                    782:                default:
                    783:                        errx(1, "regular form violated (a)");
                    784:                        /* NOTREACHED */
                    785:                }
                    786:                break;
                    787:        case (MDOC_BLOCK):
                    788:                if (NULL == n->parent)
                    789:                        errx(1, "regular form violated (b)");
                    790:                if (NULL == n->child)
                    791:                        errx(1, "regular form violated (c)");
                    792:                switch (n->parent->type) {
                    793:                case (MDOC_ROOT):
                    794:                        /* FALLTHROUGH */
                    795:                case (MDOC_HEAD):
                    796:                        /* FALLTHROUGH */
                    797:                case (MDOC_BODY):
                    798:                        /* FALLTHROUGH */
                    799:                case (MDOC_TAIL):
                    800:                        break;
                    801:                default:
                    802:                        errx(1, "regular form violated (d)");
                    803:                        /* NOTREACHED */
                    804:                }
                    805:                switch (n->child->type) {
                    806:                case (MDOC_ROOT):
                    807:                        /* FALLTHROUGH */
                    808:                case (MDOC_ELEM):
                    809:                        errx(1, "regular form violated (e)");
                    810:                        /* NOTREACHED */
                    811:                default:
                    812:                        break;
                    813:                }
                    814:                break;
                    815:        case (MDOC_ROOT):
                    816:                if (n->parent)
                    817:                        errx(1, "regular form violated (f)");
                    818:                if (NULL == n->child)
                    819:                        errx(1, "regular form violated (10)");
                    820:                switch (n->child->type) {
                    821:                case (MDOC_BLOCK):
                    822:                        break;
                    823:                default:
                    824:                        errx(1, "regular form violated (11)");
                    825:                        /* NOTREACHED */
                    826:                }
                    827:                break;
                    828:        }
                    829: }

CVSweb