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

Annotation of mandoc/terminal.c, Revision 1.3

1.3     ! kristaps    1: /* $Id: terminal.c,v 1.2 2009/03/19 16:40:49 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;
                    225:                        } else if (vis + vsz > bp)
                    226:                                warnx("word breaks right margin");
                    227:
                    228:                        /* TODO: hyphenate. */
                    229:
                    230:                } else {
                    231:                        if (vis && vis + vsz > bp) {
                    232:                                putchar('\n');
                    233:                                for (j = 0; j < (int)p->rmargin; j++)
                    234:                                        putchar(' ');
                    235:                                vis = p->rmargin - p->offset;
                    236:                        } else if (vis + vsz > bp)
                    237:                                warnx("word breaks right margin");
                    238:
                    239:                        /* TODO: hyphenate. */
                    240:                }
                    241:
                    242:                /*
                    243:                 * Write out the word and a trailing space.  Omit the
                    244:                 * space if we're the last word in the line or beyond
                    245:                 * our breakpoint.
                    246:                 */
                    247:
                    248:                for ( ; i < (int)p->col; i++) {
                    249:                        if (' ' == p->buf[i])
                    250:                                break;
                    251:                        putchar(p->buf[i]);
                    252:                }
                    253:                vis += vsz;
                    254:                if (i < (int)p->col && vis <= bp) {
                    255:                        putchar(' ');
                    256:                        vis++;
                    257:                }
                    258:        }
                    259:
                    260:        /*
                    261:         * If we've overstepped our maximum visible no-break space, then
                    262:         * cause a newline and offset at the right margin.
                    263:         */
                    264:
                    265:        if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) {
                    266:                if ( ! (TERMP_NONOBREAK & p->flags)) {
                    267:                        putchar('\n');
                    268:                        for (i = 0; i < (int)p->rmargin; i++)
                    269:                                putchar(' ');
                    270:                }
                    271:                p->col = 0;
                    272:                return;
                    273:        }
                    274:
                    275:        /*
                    276:         * If we're not to right-marginalise it (newline), then instead
                    277:         * pad to the right margin and stay off.
                    278:         */
                    279:
                    280:        if (p->flags & TERMP_NOBREAK) {
                    281:                if ( ! (TERMP_NONOBREAK & p->flags))
                    282:                        for ( ; vis < maxvis; vis++)
                    283:                                putchar(' ');
                    284:        } else
                    285:                putchar('\n');
                    286:
                    287:        p->col = 0;
                    288: }
                    289:
                    290:
                    291: /*
                    292:  * A newline only breaks an existing line; it won't assert vertical
                    293:  * space.  All data in the output buffer is flushed prior to the newline
                    294:  * assertion.
                    295:  */
                    296: void
1.3     ! kristaps  297: term_newln(struct termp *p)
1.1       kristaps  298: {
                    299:
                    300:        p->flags |= TERMP_NOSPACE;
                    301:        if (0 == p->col) {
                    302:                p->flags &= ~TERMP_NOLPAD;
                    303:                return;
                    304:        }
1.3     ! kristaps  305:        term_flushln(p);
1.1       kristaps  306:        p->flags &= ~TERMP_NOLPAD;
                    307: }
                    308:
                    309:
                    310: /*
                    311:  * Asserts a vertical space (a full, empty line-break between lines).
                    312:  * Note that if used twice, this will cause two blank spaces and so on.
                    313:  * All data in the output buffer is flushed prior to the newline
                    314:  * assertion.
                    315:  */
                    316: void
1.3     ! kristaps  317: term_vspace(struct termp *p)
1.1       kristaps  318: {
                    319:
1.3     ! kristaps  320:        term_newln(p);
1.1       kristaps  321:        putchar('\n');
                    322: }
                    323:
                    324:
                    325: /*
                    326:  * Break apart a word into "pwords" (partial-words, usually from
                    327:  * breaking up a phrase into individual words) and, eventually, put them
                    328:  * into the output buffer.  If we're a literal word, then don't break up
                    329:  * the word and put it verbatim into the output buffer.
                    330:  */
                    331: void
1.3     ! kristaps  332: term_word(struct termp *p, const char *word)
1.1       kristaps  333: {
                    334:        int              i, j, len;
                    335:
1.3     ! kristaps  336:        len = (int)strlen(word);
        !           337:
1.1       kristaps  338:        if (p->flags & TERMP_LITERAL) {
1.3     ! kristaps  339:                termp_pword(p, word, len);
1.1       kristaps  340:                return;
                    341:        }
                    342:
                    343:        if (mdoc_isdelim(word)) {
                    344:                if ( ! (p->flags & TERMP_IGNDELIM))
                    345:                        p->flags |= TERMP_NOSPACE;
                    346:                p->flags &= ~TERMP_IGNDELIM;
                    347:        }
                    348:
                    349:        /* LINTED */
                    350:        for (j = i = 0; i < len; i++) {
                    351:                if (' ' != word[i]) {
                    352:                        j++;
                    353:                        continue;
                    354:                }
                    355:
                    356:                /* Escaped spaces don't delimit... */
                    357:                if (i && ' ' == word[i] && '\\' == word[i - 1]) {
                    358:                        j++;
                    359:                        continue;
                    360:                }
                    361:
                    362:                if (0 == j)
                    363:                        continue;
                    364:                assert(i >= j);
                    365:                termp_pword(p, &word[i - j], j);
                    366:                j = 0;
                    367:        }
                    368:        if (j > 0) {
                    369:                assert(i >= j);
                    370:                termp_pword(p, &word[i - j], j);
                    371:        }
                    372: }
                    373:
                    374:
1.3     ! kristaps  375: static void
        !           376: termp_body(struct termp *p, struct termpair *ppair,
        !           377:                const struct mdoc_meta *meta,
        !           378:                const struct mdoc_node *node)
        !           379: {
        !           380:
        !           381:        term_node(p, ppair, meta, node);
        !           382:        if (node->next)
        !           383:                termp_body(p, ppair, meta, node->next);
        !           384: }
        !           385:
        !           386:
1.1       kristaps  387: /*
                    388:  * This is the main function for printing out nodes.  It's constituted
                    389:  * of PRE and POST functions, which correspond to prefix and infix
                    390:  * processing.  The termpair structure allows data to persist between
                    391:  * prefix and postfix invocations.
                    392:  */
1.3     ! kristaps  393: void
        !           394: term_node(struct termp *p, struct termpair *ppair,
1.1       kristaps  395:                const struct mdoc_meta *meta,
                    396:                const struct mdoc_node *node)
                    397: {
                    398:        int              dochild;
                    399:        struct termpair  pair;
                    400:
                    401:        /* Some quick sanity-checking. */
                    402:
                    403:        sanity(node);
                    404:
                    405:        /* Pre-processing. */
                    406:
                    407:        dochild = 1;
                    408:        pair.ppair = ppair;
                    409:        pair.type = 0;
                    410:        pair.offset = pair.rmargin = 0;
                    411:        pair.flag = 0;
                    412:        pair.count = 0;
                    413:
                    414:        if (MDOC_TEXT != node->type) {
                    415:                if (termacts[node->tok].pre)
                    416:                        if ( ! (*termacts[node->tok].pre)(p, &pair, meta, node))
                    417:                                dochild = 0;
                    418:        } else /* MDOC_TEXT == node->type */
1.3     ! kristaps  419:                term_word(p, node->string);
1.1       kristaps  420:
                    421:        /* Children. */
                    422:
                    423:        if (TERMPAIR_FLAG & pair.type)
                    424:                p->flags |= pair.flag;
                    425:
                    426:        if (dochild && node->child)
                    427:                termp_body(p, &pair, meta, node->child);
                    428:
                    429:        if (TERMPAIR_FLAG & pair.type)
                    430:                p->flags &= ~pair.flag;
                    431:
                    432:        /* Post-processing. */
                    433:
                    434:        if (MDOC_TEXT != node->type)
                    435:                if (termacts[node->tok].post)
                    436:                        (*termacts[node->tok].post)(p, &pair, meta, node);
                    437: }
                    438:
                    439:
                    440: static void
                    441: termp_foot(struct termp *p, const struct mdoc_meta *meta)
                    442: {
                    443:        struct tm       *tm;
                    444:        char            *buf, *os;
                    445:
                    446:        if (NULL == (buf = malloc(p->rmargin)))
                    447:                err(1, "malloc");
                    448:        if (NULL == (os = malloc(p->rmargin)))
                    449:                err(1, "malloc");
                    450:
                    451:        tm = localtime(&meta->date);
                    452:
                    453: #ifdef __OpenBSD__
                    454:        if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                    455: #else
                    456:        if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                    457: #endif
                    458:                err(1, "strftime");
                    459:
                    460:        (void)strlcpy(os, meta->os, p->rmargin);
                    461:
                    462:        /*
                    463:         * This is /slightly/ different from regular groff output
                    464:         * because we don't have page numbers.  Print the following:
                    465:         *
                    466:         * OS                                            MDOCDATE
                    467:         */
                    468:
1.3     ! kristaps  469:        term_vspace(p);
1.1       kristaps  470:
                    471:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
                    472:        p->rmargin = p->maxrmargin - strlen(buf);
                    473:        p->offset = 0;
                    474:
1.3     ! kristaps  475:        term_word(p, os);
        !           476:        term_flushln(p);
1.1       kristaps  477:
                    478:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    479:        p->offset = p->rmargin;
                    480:        p->rmargin = p->maxrmargin;
                    481:        p->flags &= ~TERMP_NOBREAK;
                    482:
1.3     ! kristaps  483:        term_word(p, buf);
        !           484:        term_flushln(p);
1.1       kristaps  485:
                    486:        free(buf);
                    487:        free(os);
                    488: }
                    489:
                    490:
                    491: static void
                    492: termp_head(struct termp *p, const struct mdoc_meta *meta)
                    493: {
                    494:        char            *buf, *title;
                    495:
                    496:        p->rmargin = p->maxrmargin;
                    497:        p->offset = 0;
                    498:
                    499:        if (NULL == (buf = malloc(p->rmargin)))
                    500:                err(1, "malloc");
                    501:        if (NULL == (title = malloc(p->rmargin)))
                    502:                err(1, "malloc");
                    503:
                    504:        /*
                    505:         * The header is strange.  It has three components, which are
                    506:         * really two with the first duplicated.  It goes like this:
                    507:         *
                    508:         * IDENTIFIER              TITLE                   IDENTIFIER
                    509:         *
                    510:         * The IDENTIFIER is NAME(SECTION), which is the command-name
                    511:         * (if given, or "unknown" if not) followed by the manual page
                    512:         * section.  These are given in `Dt'.  The TITLE is a free-form
                    513:         * string depending on the manual volume.  If not specified, it
                    514:         * switches on the manual section.
                    515:         */
                    516:
                    517:        assert(meta->vol);
                    518:        (void)strlcpy(buf, meta->vol, p->rmargin);
                    519:
                    520:        if (meta->arch) {
                    521:                (void)strlcat(buf, " (", p->rmargin);
                    522:                (void)strlcat(buf, meta->arch, p->rmargin);
                    523:                (void)strlcat(buf, ")", p->rmargin);
                    524:        }
                    525:
                    526:        (void)snprintf(title, p->rmargin, "%s(%d)",
                    527:                        meta->title, meta->msec);
                    528:
                    529:        p->offset = 0;
                    530:        p->rmargin = (p->maxrmargin - strlen(buf)) / 2;
                    531:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
                    532:
1.3     ! kristaps  533:        term_word(p, title);
        !           534:        term_flushln(p);
1.1       kristaps  535:
                    536:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    537:        p->offset = p->rmargin;
                    538:        p->rmargin = p->maxrmargin - strlen(title);
                    539:
1.3     ! kristaps  540:        term_word(p, buf);
        !           541:        term_flushln(p);
1.1       kristaps  542:
                    543:        p->offset = p->rmargin;
                    544:        p->rmargin = p->maxrmargin;
                    545:        p->flags &= ~TERMP_NOBREAK;
                    546:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    547:
1.3     ! kristaps  548:        term_word(p, title);
        !           549:        term_flushln(p);
1.1       kristaps  550:
                    551:        p->rmargin = p->maxrmargin;
                    552:        p->offset = 0;
                    553:        p->flags &= ~TERMP_NOSPACE;
                    554:
                    555:        free(title);
                    556:        free(buf);
                    557: }
                    558:
                    559:
                    560: /*
                    561:  * Determine the symbol indicated by an escape sequences, that is, one
                    562:  * starting with a backslash.  Once done, we pass this value into the
                    563:  * output buffer by way of the symbol table.
                    564:  */
                    565: static void
                    566: termp_nescape(struct termp *p, const char *word, size_t len)
                    567: {
                    568:        const char      *rhs;
                    569:        size_t           sz;
                    570:
1.3     ! kristaps  571:        if (NULL == (rhs = term_a2ascii(p->symtab, word, len, &sz)))
1.1       kristaps  572:                return;
                    573:        termp_stringa(p, rhs, sz);
                    574: }
                    575:
                    576:
                    577: /*
                    578:  * Handle an escape sequence: determine its length and pass it to the
                    579:  * escape-symbol look table.  Note that we assume mdoc(3) has validated
                    580:  * the escape sequence (we assert upon badly-formed escape sequences).
                    581:  */
                    582: static void
                    583: termp_pescape(struct termp *p, const char *word, int *i, int len)
                    584: {
                    585:        int              j;
                    586:
                    587:        if (++(*i) >= len)
                    588:                return;
                    589:
                    590:        if ('(' == word[*i]) {
                    591:                (*i)++;
                    592:                if (*i + 1 >= len)
                    593:                        return;
                    594:
                    595:                termp_nescape(p, &word[*i], 2);
                    596:                (*i)++;
                    597:                return;
                    598:
                    599:        } else if ('*' == word[*i]) {
                    600:                (*i)++;
                    601:                if (*i >= len)
                    602:                        return;
                    603:
                    604:                switch (word[*i]) {
                    605:                case ('('):
                    606:                        (*i)++;
                    607:                        if (*i + 1 >= len)
                    608:                                return;
                    609:
                    610:                        termp_nescape(p, &word[*i], 2);
                    611:                        (*i)++;
                    612:                        return;
                    613:                case ('['):
                    614:                        break;
                    615:                default:
                    616:                        termp_nescape(p, &word[*i], 1);
                    617:                        return;
                    618:                }
                    619:
                    620:        } else if ('[' != word[*i]) {
                    621:                termp_nescape(p, &word[*i], 1);
                    622:                return;
                    623:        }
                    624:
                    625:        (*i)++;
                    626:        for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
                    627:                /* Loop... */ ;
                    628:
                    629:        if (0 == word[*i])
                    630:                return;
                    631:
                    632:        termp_nescape(p, &word[*i - j], (size_t)j);
                    633: }
                    634:
                    635:
                    636: /*
                    637:  * Handle pwords, partial words, which may be either a single word or a
                    638:  * phrase that cannot be broken down (such as a literal string).  This
                    639:  * handles word styling.
                    640:  */
                    641: static void
                    642: termp_pword(struct termp *p, const char *word, int len)
                    643: {
                    644:        int              i;
                    645:
1.3     ! kristaps  646:        if ( ! (TERMP_NOSPACE & p->flags))
1.1       kristaps  647:                termp_chara(p, ' ');
                    648:
                    649:        if ( ! (p->flags & TERMP_NONOSPACE))
                    650:                p->flags &= ~TERMP_NOSPACE;
                    651:
                    652:        /*
                    653:         * If ANSI (word-length styling), then apply our style now,
                    654:         * before the word.
                    655:         */
                    656:
                    657:        for (i = 0; i < len; i++) {
                    658:                if ('\\' == word[i]) {
                    659:                        termp_pescape(p, word, &i, len);
                    660:                        continue;
                    661:                }
                    662:
                    663:                if (TERMP_STYLE & p->flags) {
                    664:                        if (TERMP_BOLD & p->flags) {
                    665:                                termp_chara(p, word[i]);
                    666:                                termp_chara(p, 8);
                    667:                        }
                    668:                        if (TERMP_UNDER & p->flags) {
                    669:                                termp_chara(p, '_');
                    670:                                termp_chara(p, 8);
                    671:                        }
                    672:                }
                    673:
                    674:                termp_chara(p, word[i]);
                    675:        }
                    676: }
                    677:
                    678:
                    679: /*
                    680:  * Like termp_chara() but for arbitrary-length buffers.  Resize the
                    681:  * buffer by a factor of two (if the buffer is less than that) or the
                    682:  * buffer's size.
                    683:  */
                    684: static void
                    685: termp_stringa(struct termp *p, const char *c, size_t sz)
                    686: {
                    687:        size_t           s;
                    688:
                    689:        if (0 == sz)
                    690:                return;
                    691:
                    692:        assert(c);
                    693:        if (p->col + sz >= p->maxcols) {
                    694:                if (0 == p->maxcols)
                    695:                        p->maxcols = 256;
                    696:                s = sz > p->maxcols * 2 ? sz : p->maxcols * 2;
                    697:                p->buf = realloc(p->buf, s);
                    698:                if (NULL == p->buf)
                    699:                        err(1, "realloc");
                    700:                p->maxcols = s;
                    701:        }
                    702:
                    703:        (void)memcpy(&p->buf[(int)p->col], c, sz);
                    704:        p->col += sz;
                    705: }
                    706:
                    707:
                    708: /*
                    709:  * Insert a single character into the line-buffer.  If the buffer's
                    710:  * space is exceeded, then allocate more space by doubling the buffer
                    711:  * size.
                    712:  */
                    713: static void
                    714: termp_chara(struct termp *p, char c)
                    715: {
                    716:        size_t           s;
                    717:
                    718:        if (p->col + 1 >= p->maxcols) {
                    719:                if (0 == p->maxcols)
                    720:                        p->maxcols = 256;
                    721:                s = p->maxcols * 2;
                    722:                p->buf = realloc(p->buf, s);
                    723:                if (NULL == p->buf)
                    724:                        err(1, "realloc");
                    725:                p->maxcols = s;
                    726:        }
                    727:        p->buf[(int)(p->col)++] = c;
                    728: }
                    729:
                    730:
                    731: static void
                    732: sanity(const struct mdoc_node *n)
                    733: {
                    734:
                    735:        switch (n->type) {
                    736:        case (MDOC_TEXT):
                    737:                if (n->child)
                    738:                        errx(1, "regular form violated (1)");
                    739:                if (NULL == n->parent)
                    740:                        errx(1, "regular form violated (2)");
                    741:                if (NULL == n->string)
                    742:                        errx(1, "regular form violated (3)");
                    743:                switch (n->parent->type) {
                    744:                case (MDOC_TEXT):
                    745:                        /* FALLTHROUGH */
                    746:                case (MDOC_ROOT):
                    747:                        errx(1, "regular form violated (4)");
                    748:                        /* NOTREACHED */
                    749:                default:
                    750:                        break;
                    751:                }
                    752:                break;
                    753:        case (MDOC_ELEM):
                    754:                if (NULL == n->parent)
                    755:                        errx(1, "regular form violated (5)");
                    756:                switch (n->parent->type) {
                    757:                case (MDOC_TAIL):
                    758:                        /* FALLTHROUGH */
                    759:                case (MDOC_BODY):
                    760:                        /* FALLTHROUGH */
                    761:                case (MDOC_HEAD):
                    762:                        break;
                    763:                default:
                    764:                        errx(1, "regular form violated (6)");
                    765:                        /* NOTREACHED */
                    766:                }
                    767:                if (n->child) switch (n->child->type) {
                    768:                case (MDOC_TEXT):
                    769:                        break;
                    770:                default:
                    771:                        errx(1, "regular form violated (7(");
                    772:                        /* NOTREACHED */
                    773:                }
                    774:                break;
                    775:        case (MDOC_HEAD):
                    776:                /* FALLTHROUGH */
                    777:        case (MDOC_BODY):
                    778:                /* FALLTHROUGH */
                    779:        case (MDOC_TAIL):
                    780:                if (NULL == n->parent)
                    781:                        errx(1, "regular form violated (8)");
                    782:                if (MDOC_BLOCK != n->parent->type)
                    783:                        errx(1, "regular form violated (9)");
                    784:                if (n->child) switch (n->child->type) {
                    785:                case (MDOC_BLOCK):
                    786:                        /* FALLTHROUGH */
                    787:                case (MDOC_ELEM):
                    788:                        /* FALLTHROUGH */
                    789:                case (MDOC_TEXT):
                    790:                        break;
                    791:                default:
                    792:                        errx(1, "regular form violated (a)");
                    793:                        /* NOTREACHED */
                    794:                }
                    795:                break;
                    796:        case (MDOC_BLOCK):
                    797:                if (NULL == n->parent)
                    798:                        errx(1, "regular form violated (b)");
                    799:                if (NULL == n->child)
                    800:                        errx(1, "regular form violated (c)");
                    801:                switch (n->parent->type) {
                    802:                case (MDOC_ROOT):
                    803:                        /* FALLTHROUGH */
                    804:                case (MDOC_HEAD):
                    805:                        /* FALLTHROUGH */
                    806:                case (MDOC_BODY):
                    807:                        /* FALLTHROUGH */
                    808:                case (MDOC_TAIL):
                    809:                        break;
                    810:                default:
                    811:                        errx(1, "regular form violated (d)");
                    812:                        /* NOTREACHED */
                    813:                }
                    814:                switch (n->child->type) {
                    815:                case (MDOC_ROOT):
                    816:                        /* FALLTHROUGH */
                    817:                case (MDOC_ELEM):
                    818:                        errx(1, "regular form violated (e)");
                    819:                        /* NOTREACHED */
                    820:                default:
                    821:                        break;
                    822:                }
                    823:                break;
                    824:        case (MDOC_ROOT):
                    825:                if (n->parent)
                    826:                        errx(1, "regular form violated (f)");
                    827:                if (NULL == n->child)
                    828:                        errx(1, "regular form violated (10)");
                    829:                switch (n->child->type) {
                    830:                case (MDOC_BLOCK):
                    831:                        break;
                    832:                default:
                    833:                        errx(1, "regular form violated (11)");
                    834:                        /* NOTREACHED */
                    835:                }
                    836:                break;
                    837:        }
                    838: }

CVSweb