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

Annotation of mandoc/terminal.c, Revision 1.2

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

CVSweb