[BACK]Return to term.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/term.c, Revision 1.2

1.2     ! schwarze    1: /*     $Id: term.c,v 1.78 2009/06/11 10:34:32 kristaps Exp $ */
1.1       kristaps    2: /*
1.2     ! schwarze    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.2     ! schwarze    6:  * purpose with or without fee is hereby granted, provided that the above
        !             7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.2     ! schwarze    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !            10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
                     17: #include <assert.h>
                     18: #include <err.h>
                     19: #include <stdio.h>
                     20: #include <stdlib.h>
                     21: #include <string.h>
                     22:
                     23: #include "term.h"
                     24: #include "man.h"
                     25: #include "mdoc.h"
                     26:
                     27: extern int               man_run(struct termp *,
                     28:                                const struct man *);
                     29: extern int               mdoc_run(struct termp *,
                     30:                                const struct mdoc *);
                     31:
                     32: static struct termp     *term_alloc(enum termenc);
                     33: static void              term_free(struct termp *);
                     34: static void              term_pword(struct termp *, const char *, int);
                     35: static void              term_pescape(struct termp *,
                     36:                                const char *, int *, int);
                     37: static void              term_nescape(struct termp *,
                     38:                                const char *, size_t);
                     39: static void              term_chara(struct termp *, char);
                     40: static void              term_stringa(struct termp *,
                     41:                                const char *, size_t);
                     42: static int               term_isopendelim(const char *, int);
                     43: static int               term_isclosedelim(const char *, int);
                     44:
                     45:
                     46: void *
                     47: ascii_alloc(void)
                     48: {
                     49:
                     50:        return(term_alloc(TERMENC_ASCII));
                     51: }
                     52:
                     53:
                     54: int
                     55: terminal_man(void *arg, const struct man *man)
                     56: {
                     57:        struct termp    *p;
                     58:
                     59:        p = (struct termp *)arg;
                     60:        if (NULL == p->symtab)
                     61:                p->symtab = term_ascii2htab();
                     62:
                     63:        return(man_run(p, man));
                     64: }
                     65:
                     66:
                     67: int
                     68: terminal_mdoc(void *arg, const struct mdoc *mdoc)
                     69: {
                     70:        struct termp    *p;
                     71:
                     72:        p = (struct termp *)arg;
                     73:        if (NULL == p->symtab)
                     74:                p->symtab = term_ascii2htab();
                     75:
                     76:        return(mdoc_run(p, mdoc));
                     77: }
                     78:
                     79:
                     80: void
                     81: terminal_free(void *arg)
                     82: {
                     83:
                     84:        term_free((struct termp *)arg);
                     85: }
                     86:
                     87:
                     88: static void
                     89: term_free(struct termp *p)
                     90: {
                     91:
                     92:        if (p->buf)
                     93:                free(p->buf);
                     94:        if (TERMENC_ASCII == p->enc && p->symtab)
                     95:                term_asciifree(p->symtab);
                     96:
                     97:        free(p);
                     98: }
                     99:
                    100:
                    101: static struct termp *
                    102: term_alloc(enum termenc enc)
                    103: {
                    104:        struct termp *p;
                    105:
                    106:        if (NULL == (p = malloc(sizeof(struct termp))))
                    107:                err(1, "malloc");
                    108:        bzero(p, sizeof(struct termp));
                    109:        p->maxrmargin = 78;
                    110:        p->enc = enc;
                    111:        return(p);
                    112: }
                    113:
                    114:
                    115: static int
                    116: term_isclosedelim(const char *p, int len)
                    117: {
                    118:
                    119:        if (1 != len)
                    120:                return(0);
                    121:
                    122:        switch (*p) {
                    123:        case('.'):
                    124:                /* FALLTHROUGH */
                    125:        case(','):
                    126:                /* FALLTHROUGH */
                    127:        case(';'):
                    128:                /* FALLTHROUGH */
                    129:        case(':'):
                    130:                /* FALLTHROUGH */
                    131:        case('?'):
                    132:                /* FALLTHROUGH */
                    133:        case('!'):
                    134:                /* FALLTHROUGH */
                    135:        case(')'):
                    136:                /* FALLTHROUGH */
                    137:        case(']'):
                    138:                /* FALLTHROUGH */
                    139:        case('}'):
                    140:                return(1);
                    141:        default:
                    142:                break;
                    143:        }
                    144:
                    145:        return(0);
                    146: }
                    147:
                    148:
                    149: static int
                    150: term_isopendelim(const char *p, int len)
                    151: {
                    152:
                    153:        if (1 != len)
                    154:                return(0);
                    155:
                    156:        switch (*p) {
                    157:        case('('):
                    158:                /* FALLTHROUGH */
                    159:        case('['):
                    160:                /* FALLTHROUGH */
                    161:        case('{'):
                    162:                return(1);
                    163:        default:
                    164:                break;
                    165:        }
                    166:
                    167:        return(0);
                    168: }
                    169:
                    170:
                    171: /*
                    172:  * Flush a line of text.  A "line" is loosely defined as being something
                    173:  * that should be followed by a newline, regardless of whether it's
                    174:  * broken apart by newlines getting there.  A line can also be a
                    175:  * fragment of a columnar list.
                    176:  *
                    177:  * Specifically, a line is whatever's in p->buf of length p->col, which
                    178:  * is zeroed after this function returns.
                    179:  *
                    180:  * The variables TERMP_NOLPAD, TERMP_LITERAL and TERMP_NOBREAK are of
                    181:  * critical importance here.  Their behaviour follows:
                    182:  *
                    183:  *  - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
                    184:  *    offset value.  This is useful when doing columnar lists where the
                    185:  *    prior column has right-padded.
                    186:  *
                    187:  *  - TERMP_NOBREAK: this is the most important and is used when making
                    188:  *    columns.  In short: don't print a newline and instead pad to the
                    189:  *    right margin.  Used in conjunction with TERMP_NOLPAD.
                    190:  *
                    191:  *  - TERMP_NONOBREAK: don't newline when TERMP_NOBREAK is specified.
                    192:  *
                    193:  *  In-line line breaking:
                    194:  *
                    195:  *  If TERMP_NOBREAK is specified and the line overruns the right
                    196:  *  margin, it will break and pad-right to the right margin after
                    197:  *  writing.  If maxrmargin is violated, it will break and continue
                    198:  *  writing from the right-margin, which will lead to the above
                    199:  *  scenario upon exit.
                    200:  *
                    201:  *  Otherwise, the line will break at the right margin.  Extremely long
                    202:  *  lines will cause the system to emit a warning (TODO: hyphenate, if
                    203:  *  possible).
                    204:  */
                    205: void
                    206: term_flushln(struct termp *p)
                    207: {
                    208:        int              i, j;
                    209:        size_t           vsz, vis, maxvis, mmax, bp;
                    210:
                    211:        /*
                    212:         * First, establish the maximum columns of "visible" content.
                    213:         * This is usually the difference between the right-margin and
                    214:         * an indentation, but can be, for tagged lists or columns, a
                    215:         * small set of values.
                    216:         */
                    217:
                    218:        assert(p->offset < p->rmargin);
                    219:        maxvis = p->rmargin - p->offset;
                    220:        mmax = p->maxrmargin - p->offset;
                    221:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
                    222:        vis = 0;
                    223:
                    224:        /*
                    225:         * If in the standard case (left-justified), then begin with our
                    226:         * indentation, otherwise (columns, etc.) just start spitting
                    227:         * out text.
                    228:         */
                    229:
                    230:        if ( ! (p->flags & TERMP_NOLPAD))
                    231:                /* LINTED */
                    232:                for (j = 0; j < (int)p->offset; j++)
                    233:                        putchar(' ');
                    234:
                    235:        for (i = 0; i < (int)p->col; i++) {
                    236:                /*
                    237:                 * Count up visible word characters.  Control sequences
                    238:                 * (starting with the CSI) aren't counted.  A space
                    239:                 * generates a non-printing word, which is valid (the
                    240:                 * space is printed according to regular spacing rules).
                    241:                 */
                    242:
                    243:                /* LINTED */
                    244:                for (j = i, vsz = 0; j < (int)p->col; j++) {
                    245:                        if (' ' == p->buf[j])
                    246:                                break;
                    247:                        else if (8 == p->buf[j])
                    248:                                j += 1;
                    249:                        else
                    250:                                vsz++;
                    251:                }
                    252:
                    253:                /*
                    254:                 * Do line-breaking.  If we're greater than our
                    255:                 * break-point and already in-line, break to the next
                    256:                 * line and start writing.  If we're at the line start,
                    257:                 * then write out the word (TODO: hyphenate) and break
                    258:                 * in a subsequent loop invocation.
                    259:                 */
                    260:
                    261:                if ( ! (TERMP_NOBREAK & p->flags)) {
                    262:                        if (vis && vis + vsz > bp) {
                    263:                                putchar('\n');
                    264:                                for (j = 0; j < (int)p->offset; j++)
                    265:                                        putchar(' ');
                    266:                                vis = 0;
                    267:                        }
                    268:                } else if (vis && vis + vsz > bp) {
                    269:                        putchar('\n');
                    270:                        for (j = 0; j < (int)p->rmargin; j++)
                    271:                                putchar(' ');
                    272:                        vis = p->rmargin - p->offset;
                    273:                }
                    274:
                    275:                /*
                    276:                 * Write out the word and a trailing space.  Omit the
                    277:                 * space if we're the last word in the line or beyond
                    278:                 * our breakpoint.
                    279:                 */
                    280:
                    281:                for ( ; i < (int)p->col; i++) {
                    282:                        if (' ' == p->buf[i])
                    283:                                break;
                    284:                        putchar(p->buf[i]);
                    285:                }
                    286:                vis += vsz;
                    287:                if (i < (int)p->col && vis <= bp) {
                    288:                        putchar(' ');
                    289:                        vis++;
                    290:                }
                    291:        }
                    292:
                    293:        /*
                    294:         * If we've overstepped our maximum visible no-break space, then
                    295:         * cause a newline and offset at the right margin.
                    296:         */
                    297:
                    298:        if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) {
                    299:                if ( ! (TERMP_NONOBREAK & p->flags)) {
                    300:                        putchar('\n');
                    301:                        for (i = 0; i < (int)p->rmargin; i++)
                    302:                                putchar(' ');
                    303:                }
                    304:                p->col = 0;
                    305:                return;
                    306:        }
                    307:
                    308:        /*
                    309:         * If we're not to right-marginalise it (newline), then instead
                    310:         * pad to the right margin and stay off.
                    311:         */
                    312:
                    313:        if (p->flags & TERMP_NOBREAK) {
                    314:                if ( ! (TERMP_NONOBREAK & p->flags))
                    315:                        for ( ; vis < maxvis; vis++)
                    316:                                putchar(' ');
                    317:        } else
                    318:                putchar('\n');
                    319:
                    320:        p->col = 0;
                    321: }
                    322:
                    323:
                    324: /*
                    325:  * A newline only breaks an existing line; it won't assert vertical
                    326:  * space.  All data in the output buffer is flushed prior to the newline
                    327:  * assertion.
                    328:  */
                    329: void
                    330: term_newln(struct termp *p)
                    331: {
                    332:
                    333:        p->flags |= TERMP_NOSPACE;
                    334:        if (0 == p->col) {
                    335:                p->flags &= ~TERMP_NOLPAD;
                    336:                return;
                    337:        }
                    338:        term_flushln(p);
                    339:        p->flags &= ~TERMP_NOLPAD;
                    340: }
                    341:
                    342:
                    343: /*
                    344:  * Asserts a vertical space (a full, empty line-break between lines).
                    345:  * Note that if used twice, this will cause two blank spaces and so on.
                    346:  * All data in the output buffer is flushed prior to the newline
                    347:  * assertion.
                    348:  */
                    349: void
                    350: term_vspace(struct termp *p)
                    351: {
                    352:
                    353:        term_newln(p);
                    354:        putchar('\n');
                    355: }
                    356:
                    357:
                    358: /*
                    359:  * Break apart a word into "pwords" (partial-words, usually from
                    360:  * breaking up a phrase into individual words) and, eventually, put them
                    361:  * into the output buffer.  If we're a literal word, then don't break up
                    362:  * the word and put it verbatim into the output buffer.
                    363:  */
                    364: void
                    365: term_word(struct termp *p, const char *word)
                    366: {
                    367:        int              i, j, len;
                    368:
                    369:        len = (int)strlen(word);
                    370:
                    371:        if (p->flags & TERMP_LITERAL) {
                    372:                term_pword(p, word, len);
                    373:                return;
                    374:        }
                    375:
                    376:        /* LINTED */
                    377:        for (j = i = 0; i < len; i++) {
                    378:                if (' ' != word[i]) {
                    379:                        j++;
                    380:                        continue;
                    381:                }
                    382:
                    383:                /* Escaped spaces don't delimit... */
                    384:                if (i && ' ' == word[i] && '\\' == word[i - 1]) {
                    385:                        j++;
                    386:                        continue;
                    387:                }
                    388:
                    389:                if (0 == j)
                    390:                        continue;
                    391:                assert(i >= j);
                    392:                term_pword(p, &word[i - j], j);
                    393:                j = 0;
                    394:        }
                    395:        if (j > 0) {
                    396:                assert(i >= j);
                    397:                term_pword(p, &word[i - j], j);
                    398:        }
                    399: }
                    400:
                    401:
                    402: /*
                    403:  * Determine the symbol indicated by an escape sequences, that is, one
                    404:  * starting with a backslash.  Once done, we pass this value into the
                    405:  * output buffer by way of the symbol table.
                    406:  */
                    407: static void
                    408: term_nescape(struct termp *p, const char *word, size_t len)
                    409: {
                    410:        const char      *rhs;
                    411:        size_t           sz;
                    412:
                    413:        if (NULL == (rhs = term_a2ascii(p->symtab, word, len, &sz)))
                    414:                return;
                    415:        term_stringa(p, rhs, sz);
                    416: }
                    417:
                    418:
                    419: /*
                    420:  * Handle an escape sequence: determine its length and pass it to the
                    421:  * escape-symbol look table.  Note that we assume mdoc(3) has validated
                    422:  * the escape sequence (we assert upon badly-formed escape sequences).
                    423:  */
                    424: static void
                    425: term_pescape(struct termp *p, const char *word, int *i, int len)
                    426: {
                    427:        int              j;
                    428:
                    429:        if (++(*i) >= len)
                    430:                return;
                    431:
                    432:        if ('(' == word[*i]) {
                    433:                (*i)++;
                    434:                if (*i + 1 >= len)
                    435:                        return;
                    436:
                    437:                term_nescape(p, &word[*i], 2);
                    438:                (*i)++;
                    439:                return;
                    440:
                    441:        } else if ('*' == word[*i]) {
                    442:                (*i)++;
                    443:                if (*i >= len)
                    444:                        return;
                    445:
                    446:                switch (word[*i]) {
                    447:                case ('('):
                    448:                        (*i)++;
                    449:                        if (*i + 1 >= len)
                    450:                                return;
                    451:
                    452:                        term_nescape(p, &word[*i], 2);
                    453:                        (*i)++;
                    454:                        return;
                    455:                case ('['):
                    456:                        break;
                    457:                default:
                    458:                        term_nescape(p, &word[*i], 1);
                    459:                        return;
                    460:                }
                    461:
                    462:        } else if ('f' == word[*i]) {
                    463:                (*i)++;
                    464:                if (*i >= len)
                    465:                        return;
                    466:                switch (word[*i]) {
                    467:                case ('B'):
                    468:                        p->flags |= TERMP_BOLD;
                    469:                        break;
                    470:                case ('I'):
                    471:                        p->flags |= TERMP_UNDER;
                    472:                        break;
                    473:                case ('P'):
                    474:                        /* FALLTHROUGH */
                    475:                case ('R'):
                    476:                        p->flags &= ~TERMP_STYLE;
                    477:                        break;
                    478:                default:
                    479:                        break;
                    480:                }
                    481:                return;
                    482:
                    483:        } else if ('[' != word[*i]) {
                    484:                term_nescape(p, &word[*i], 1);
                    485:                return;
                    486:        }
                    487:
                    488:        (*i)++;
                    489:        for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
                    490:                /* Loop... */ ;
                    491:
                    492:        if (0 == word[*i])
                    493:                return;
                    494:
                    495:        term_nescape(p, &word[*i - j], (size_t)j);
                    496: }
                    497:
                    498:
                    499: /*
                    500:  * Handle pwords, partial words, which may be either a single word or a
                    501:  * phrase that cannot be broken down (such as a literal string).  This
                    502:  * handles word styling.
                    503:  */
                    504: static void
                    505: term_pword(struct termp *p, const char *word, int len)
                    506: {
                    507:        int              i;
                    508:
                    509:        if (term_isclosedelim(word, len))
                    510:                if ( ! (TERMP_IGNDELIM & p->flags))
                    511:                        p->flags |= TERMP_NOSPACE;
                    512:
                    513:        if ( ! (TERMP_NOSPACE & p->flags))
                    514:                term_chara(p, ' ');
                    515:
                    516:        if ( ! (p->flags & TERMP_NONOSPACE))
                    517:                p->flags &= ~TERMP_NOSPACE;
                    518:
                    519:        /*
                    520:         * If ANSI (word-length styling), then apply our style now,
                    521:         * before the word.
                    522:         */
                    523:
                    524:        for (i = 0; i < len; i++) {
                    525:                if ('\\' == word[i]) {
                    526:                        term_pescape(p, word, &i, len);
                    527:                        continue;
                    528:                }
                    529:
                    530:                if (TERMP_STYLE & p->flags) {
                    531:                        if (TERMP_BOLD & p->flags) {
                    532:                                term_chara(p, word[i]);
                    533:                                term_chara(p, 8);
                    534:                        }
                    535:                        if (TERMP_UNDER & p->flags) {
                    536:                                term_chara(p, '_');
                    537:                                term_chara(p, 8);
                    538:                        }
                    539:                }
                    540:
                    541:                term_chara(p, word[i]);
                    542:        }
                    543:
                    544:        if (term_isopendelim(word, len))
                    545:                p->flags |= TERMP_NOSPACE;
                    546: }
                    547:
                    548:
                    549: /*
                    550:  * Like term_chara() but for arbitrary-length buffers.  Resize the
                    551:  * buffer by a factor of two (if the buffer is less than that) or the
                    552:  * buffer's size.
                    553:  */
                    554: static void
                    555: term_stringa(struct termp *p, const char *c, size_t sz)
                    556: {
                    557:        size_t           s;
                    558:
                    559:        if (0 == sz)
                    560:                return;
                    561:
                    562:        assert(c);
                    563:        if (p->col + sz >= p->maxcols) {
                    564:                if (0 == p->maxcols)
                    565:                        p->maxcols = 256;
                    566:                s = sz > p->maxcols * 2 ? sz : p->maxcols * 2;
                    567:                p->buf = realloc(p->buf, s);
                    568:                if (NULL == p->buf)
                    569:                        err(1, "realloc");
                    570:                p->maxcols = s;
                    571:        }
                    572:
                    573:        (void)memcpy(&p->buf[(int)p->col], c, sz);
                    574:        p->col += sz;
                    575: }
                    576:
                    577:
                    578: /*
                    579:  * Insert a single character into the line-buffer.  If the buffer's
                    580:  * space is exceeded, then allocate more space by doubling the buffer
                    581:  * size.
                    582:  */
                    583: static void
                    584: term_chara(struct termp *p, char c)
                    585: {
                    586:        size_t           s;
                    587:
                    588:        if (p->col + 1 >= p->maxcols) {
                    589:                if (0 == p->maxcols)
                    590:                        p->maxcols = 256;
                    591:                s = p->maxcols * 2;
                    592:                p->buf = realloc(p->buf, s);
                    593:                if (NULL == p->buf)
                    594:                        err(1, "realloc");
                    595:                p->maxcols = s;
                    596:        }
                    597:        p->buf[(int)(p->col)++] = c;
                    598: }
                    599: