[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.4

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