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

Annotation of mandoc/term.c, Revision 1.77

1.77    ! kristaps    1: /*     $Id: term.c,v 1.76 2009/06/11 07:26:35 kristaps Exp $ */
1.1       kristaps    2: /*
1.75      kristaps    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.74      kristaps    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.74      kristaps    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>
1.23      kristaps   18: #include <err.h>
1.22      kristaps   19: #include <stdio.h>
1.1       kristaps   20: #include <stdlib.h>
                     21: #include <string.h>
                     22:
1.71      kristaps   23: #include "term.h"
                     24: #include "man.h"
                     25: #include "mdoc.h"
1.1       kristaps   26:
1.71      kristaps   27: extern int               man_run(struct termp *,
                     28:                                const struct man *);
                     29: extern int               mdoc_run(struct termp *,
                     30:                                const struct mdoc *);
1.1       kristaps   31:
1.71      kristaps   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);
1.1       kristaps   44:
                     45:
1.71      kristaps   46: void *
                     47: ascii_alloc(void)
1.10      kristaps   48: {
1.1       kristaps   49:
1.71      kristaps   50:        return(term_alloc(TERMENC_ASCII));
1.1       kristaps   51: }
                     52:
                     53:
1.71      kristaps   54: int
1.72      kristaps   55: terminal_man(void *arg, const struct man *man)
1.1       kristaps   56: {
1.71      kristaps   57:        struct termp    *p;
1.1       kristaps   58:
1.71      kristaps   59:        p = (struct termp *)arg;
                     60:        if (NULL == p->symtab)
                     61:                p->symtab = term_ascii2htab();
1.2       kristaps   62:
1.72      kristaps   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();
1.2       kristaps   75:
1.72      kristaps   76:        return(mdoc_run(p, mdoc));
1.1       kristaps   77: }
                     78:
                     79:
1.71      kristaps   80: void
                     81: terminal_free(void *arg)
1.11      kristaps   82: {
                     83:
1.71      kristaps   84:        term_free((struct termp *)arg);
1.11      kristaps   85: }
                     86:
                     87:
1.71      kristaps   88: static void
                     89: term_free(struct termp *p)
1.14      kristaps   90: {
                     91:
1.71      kristaps   92:        if (p->buf)
                     93:                free(p->buf);
                     94:        if (TERMENC_ASCII == p->enc && p->symtab)
                     95:                term_asciifree(p->symtab);
1.14      kristaps   96:
1.71      kristaps   97:        free(p);
1.14      kristaps   98: }
                     99:
                    100:
1.71      kristaps  101: static struct termp *
                    102: term_alloc(enum termenc enc)
1.14      kristaps  103: {
1.71      kristaps  104:        struct termp *p;
1.14      kristaps  105:
1.71      kristaps  106:        if (NULL == (p = malloc(sizeof(struct termp))))
                    107:                err(1, "malloc");
                    108:        bzero(p, sizeof(struct termp));
1.76      kristaps  109:        p->maxrmargin = 80;
1.71      kristaps  110:        p->enc = enc;
                    111:        return(p);
1.14      kristaps  112: }
                    113:
                    114:
                    115: static int
1.71      kristaps  116: term_isclosedelim(const char *p, int len)
1.14      kristaps  117: {
                    118:
1.71      kristaps  119:        if (1 != len)
                    120:                return(0);
1.14      kristaps  121:
1.71      kristaps  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:        }
1.14      kristaps  144:
1.71      kristaps  145:        return(0);
1.30      kristaps  146: }
                    147:
                    148:
1.14      kristaps  149: static int
1.71      kristaps  150: term_isopendelim(const char *p, int len)
1.14      kristaps  151: {
1.43      kristaps  152:
1.71      kristaps  153:        if (1 != len)
                    154:                return(0);
1.14      kristaps  155:
1.71      kristaps  156:        switch (*p) {
                    157:        case('('):
                    158:                /* FALLTHROUGH */
                    159:        case('['):
                    160:                /* FALLTHROUGH */
                    161:        case('{'):
                    162:                return(1);
                    163:        default:
                    164:                break;
                    165:        }
1.43      kristaps  166:
1.14      kristaps  167:        return(0);
                    168: }
1.15      kristaps  169:
                    170:
1.71      kristaps  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)
1.53      kristaps  207: {
1.71      kristaps  208:        int              i, j;
                    209:        size_t           vsz, vis, maxvis, mmax, bp;
1.53      kristaps  210:
1.71      kristaps  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:         */
1.53      kristaps  217:
1.71      kristaps  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;
1.53      kristaps  223:
1.71      kristaps  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:         */
1.53      kristaps  229:
1.71      kristaps  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:                }
1.53      kristaps  252:
1.71      kristaps  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:                }
1.53      kristaps  274:
1.71      kristaps  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:
1.77    ! kristaps  281:                if (0 < vis++)
        !           282:                        putchar(' ');
        !           283:
1.71      kristaps  284:                for ( ; i < (int)p->col; i++) {
                    285:                        if (' ' == p->buf[i])
                    286:                                break;
                    287:                        putchar(p->buf[i]);
                    288:                }
                    289:                vis += vsz;
                    290:        }
1.15      kristaps  291:
1.71      kristaps  292:        /*
                    293:         * If we've overstepped our maximum visible no-break space, then
                    294:         * cause a newline and offset at the right margin.
                    295:         */
1.15      kristaps  296:
1.71      kristaps  297:        if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) {
                    298:                if ( ! (TERMP_NONOBREAK & p->flags)) {
                    299:                        putchar('\n');
                    300:                        for (i = 0; i < (int)p->rmargin; i++)
                    301:                                putchar(' ');
                    302:                }
                    303:                p->col = 0;
1.15      kristaps  304:                return;
1.71      kristaps  305:        }
1.15      kristaps  306:
1.71      kristaps  307:        /*
                    308:         * If we're not to right-marginalise it (newline), then instead
                    309:         * pad to the right margin and stay off.
                    310:         */
1.15      kristaps  311:
1.71      kristaps  312:        if (p->flags & TERMP_NOBREAK) {
                    313:                if ( ! (TERMP_NONOBREAK & p->flags))
1.77    ! kristaps  314:                        for ( ; vis <= maxvis; vis++)
1.71      kristaps  315:                                putchar(' ');
                    316:        } else
                    317:                putchar('\n');
1.15      kristaps  318:
1.71      kristaps  319:        p->col = 0;
1.15      kristaps  320: }
                    321:
                    322:
1.71      kristaps  323: /*
                    324:  * A newline only breaks an existing line; it won't assert vertical
                    325:  * space.  All data in the output buffer is flushed prior to the newline
                    326:  * assertion.
                    327:  */
                    328: void
                    329: term_newln(struct termp *p)
1.15      kristaps  330: {
                    331:
1.71      kristaps  332:        p->flags |= TERMP_NOSPACE;
                    333:        if (0 == p->col) {
                    334:                p->flags &= ~TERMP_NOLPAD;
1.15      kristaps  335:                return;
1.16      kristaps  336:        }
1.71      kristaps  337:        term_flushln(p);
                    338:        p->flags &= ~TERMP_NOLPAD;
1.16      kristaps  339: }
                    340:
                    341:
1.71      kristaps  342: /*
                    343:  * Asserts a vertical space (a full, empty line-break between lines).
                    344:  * Note that if used twice, this will cause two blank spaces and so on.
                    345:  * All data in the output buffer is flushed prior to the newline
                    346:  * assertion.
                    347:  */
                    348: void
                    349: term_vspace(struct termp *p)
1.16      kristaps  350: {
                    351:
1.62      kristaps  352:        term_newln(p);
1.71      kristaps  353:        putchar('\n');
1.16      kristaps  354: }
                    355:
                    356:
1.71      kristaps  357: /*
                    358:  * Break apart a word into "pwords" (partial-words, usually from
                    359:  * breaking up a phrase into individual words) and, eventually, put them
                    360:  * into the output buffer.  If we're a literal word, then don't break up
                    361:  * the word and put it verbatim into the output buffer.
                    362:  */
                    363: void
                    364: term_word(struct termp *p, const char *word)
1.17      kristaps  365: {
1.71      kristaps  366:        int              i, j, len;
1.17      kristaps  367:
1.71      kristaps  368:        len = (int)strlen(word);
1.17      kristaps  369:
1.71      kristaps  370:        if (p->flags & TERMP_LITERAL) {
                    371:                term_pword(p, word, len);
                    372:                return;
                    373:        }
1.17      kristaps  374:
1.71      kristaps  375:        /* LINTED */
                    376:        for (j = i = 0; i < len; i++) {
                    377:                if (' ' != word[i]) {
                    378:                        j++;
                    379:                        continue;
                    380:                }
                    381:
                    382:                /* Escaped spaces don't delimit... */
                    383:                if (i && ' ' == word[i] && '\\' == word[i - 1]) {
                    384:                        j++;
                    385:                        continue;
                    386:                }
1.17      kristaps  387:
1.71      kristaps  388:                if (0 == j)
                    389:                        continue;
                    390:                assert(i >= j);
                    391:                term_pword(p, &word[i - j], j);
                    392:                j = 0;
                    393:        }
                    394:        if (j > 0) {
                    395:                assert(i >= j);
                    396:                term_pword(p, &word[i - j], j);
                    397:        }
1.17      kristaps  398: }
                    399:
                    400:
1.71      kristaps  401: /*
                    402:  * Determine the symbol indicated by an escape sequences, that is, one
                    403:  * starting with a backslash.  Once done, we pass this value into the
                    404:  * output buffer by way of the symbol table.
                    405:  */
                    406: static void
                    407: term_nescape(struct termp *p, const char *word, size_t len)
1.17      kristaps  408: {
1.71      kristaps  409:        const char      *rhs;
                    410:        size_t           sz;
1.17      kristaps  411:
1.71      kristaps  412:        if (NULL == (rhs = term_a2ascii(p->symtab, word, len, &sz)))
                    413:                return;
                    414:        term_stringa(p, rhs, sz);
1.17      kristaps  415: }
                    416:
                    417:
1.71      kristaps  418: /*
                    419:  * Handle an escape sequence: determine its length and pass it to the
                    420:  * escape-symbol look table.  Note that we assume mdoc(3) has validated
                    421:  * the escape sequence (we assert upon badly-formed escape sequences).
                    422:  */
                    423: static void
                    424: term_pescape(struct termp *p, const char *word, int *i, int len)
1.17      kristaps  425: {
1.71      kristaps  426:        int              j;
1.17      kristaps  427:
1.71      kristaps  428:        if (++(*i) >= len)
                    429:                return;
1.17      kristaps  430:
1.71      kristaps  431:        if ('(' == word[*i]) {
                    432:                (*i)++;
                    433:                if (*i + 1 >= len)
                    434:                        return;
1.22      kristaps  435:
1.71      kristaps  436:                term_nescape(p, &word[*i], 2);
                    437:                (*i)++;
                    438:                return;
1.22      kristaps  439:
1.71      kristaps  440:        } else if ('*' == word[*i]) {
                    441:                (*i)++;
                    442:                if (*i >= len)
                    443:                        return;
1.22      kristaps  444:
1.71      kristaps  445:                switch (word[*i]) {
                    446:                case ('('):
                    447:                        (*i)++;
                    448:                        if (*i + 1 >= len)
                    449:                                return;
1.65      kristaps  450:
1.71      kristaps  451:                        term_nescape(p, &word[*i], 2);
                    452:                        (*i)++;
                    453:                        return;
                    454:                case ('['):
                    455:                        break;
                    456:                default:
                    457:                        term_nescape(p, &word[*i], 1);
                    458:                        return;
                    459:                }
                    460:
                    461:        } else if ('f' == word[*i]) {
                    462:                (*i)++;
                    463:                if (*i >= len)
                    464:                        return;
                    465:                switch (word[*i]) {
                    466:                case ('B'):
                    467:                        p->flags |= TERMP_BOLD;
                    468:                        break;
                    469:                case ('I'):
                    470:                        p->flags |= TERMP_UNDER;
                    471:                        break;
                    472:                case ('P'):
                    473:                        /* FALLTHROUGH */
                    474:                case ('R'):
                    475:                        p->flags &= ~TERMP_STYLE;
                    476:                        break;
                    477:                default:
                    478:                        break;
                    479:                }
                    480:                return;
1.22      kristaps  481:
1.71      kristaps  482:        } else if ('[' != word[*i]) {
                    483:                term_nescape(p, &word[*i], 1);
                    484:                return;
                    485:        }
1.28      kristaps  486:
1.71      kristaps  487:        (*i)++;
                    488:        for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
                    489:                /* Loop... */ ;
1.28      kristaps  490:
1.71      kristaps  491:        if (0 == word[*i])
                    492:                return;
1.48      kristaps  493:
1.71      kristaps  494:        term_nescape(p, &word[*i - j], (size_t)j);
1.48      kristaps  495: }
                    496:
                    497:
1.71      kristaps  498: /*
                    499:  * Handle pwords, partial words, which may be either a single word or a
                    500:  * phrase that cannot be broken down (such as a literal string).  This
                    501:  * handles word styling.
                    502:  */
                    503: static void
                    504: term_pword(struct termp *p, const char *word, int len)
1.65      kristaps  505: {
1.71      kristaps  506:        int              i;
                    507:
                    508:        if (term_isclosedelim(word, len))
                    509:                if ( ! (TERMP_IGNDELIM & p->flags))
                    510:                        p->flags |= TERMP_NOSPACE;
1.65      kristaps  511:
1.71      kristaps  512:        if ( ! (TERMP_NOSPACE & p->flags))
                    513:                term_chara(p, ' ');
1.65      kristaps  514:
1.71      kristaps  515:        if ( ! (p->flags & TERMP_NONOSPACE))
                    516:                p->flags &= ~TERMP_NOSPACE;
1.65      kristaps  517:
1.71      kristaps  518:        /*
                    519:         * If ANSI (word-length styling), then apply our style now,
                    520:         * before the word.
                    521:         */
1.28      kristaps  522:
1.71      kristaps  523:        for (i = 0; i < len; i++) {
                    524:                if ('\\' == word[i]) {
                    525:                        term_pescape(p, word, &i, len);
                    526:                        continue;
                    527:                }
1.28      kristaps  528:
1.71      kristaps  529:                if (TERMP_STYLE & p->flags) {
                    530:                        if (TERMP_BOLD & p->flags) {
                    531:                                term_chara(p, word[i]);
                    532:                                term_chara(p, 8);
                    533:                        }
                    534:                        if (TERMP_UNDER & p->flags) {
                    535:                                term_chara(p, '_');
                    536:                                term_chara(p, 8);
                    537:                        }
                    538:                }
1.28      kristaps  539:
1.71      kristaps  540:                term_chara(p, word[i]);
                    541:        }
1.65      kristaps  542:
1.71      kristaps  543:        if (term_isopendelim(word, len))
                    544:                p->flags |= TERMP_NOSPACE;
1.65      kristaps  545: }
                    546:
                    547:
1.71      kristaps  548: /*
                    549:  * Like term_chara() but for arbitrary-length buffers.  Resize the
                    550:  * buffer by a factor of two (if the buffer is less than that) or the
                    551:  * buffer's size.
                    552:  */
1.65      kristaps  553: static void
1.71      kristaps  554: term_stringa(struct termp *p, const char *c, size_t sz)
1.28      kristaps  555: {
1.71      kristaps  556:        size_t           s;
1.28      kristaps  557:
1.71      kristaps  558:        if (0 == sz)
                    559:                return;
1.51      kristaps  560:
1.71      kristaps  561:        assert(c);
                    562:        if (p->col + sz >= p->maxcols) {
                    563:                if (0 == p->maxcols)
                    564:                        p->maxcols = 256;
                    565:                s = sz > p->maxcols * 2 ? sz : p->maxcols * 2;
                    566:                p->buf = realloc(p->buf, s);
                    567:                if (NULL == p->buf)
                    568:                        err(1, "realloc");
                    569:                p->maxcols = s;
1.51      kristaps  570:        }
                    571:
1.71      kristaps  572:        (void)memcpy(&p->buf[(int)p->col], c, sz);
                    573:        p->col += sz;
1.51      kristaps  574: }
                    575:
                    576:
1.71      kristaps  577: /*
                    578:  * Insert a single character into the line-buffer.  If the buffer's
                    579:  * space is exceeded, then allocate more space by doubling the buffer
                    580:  * size.
                    581:  */
                    582: static void
                    583: term_chara(struct termp *p, char c)
1.51      kristaps  584: {
1.71      kristaps  585:        size_t           s;
1.51      kristaps  586:
1.71      kristaps  587:        if (p->col + 1 >= p->maxcols) {
                    588:                if (0 == p->maxcols)
                    589:                        p->maxcols = 256;
                    590:                s = p->maxcols * 2;
                    591:                p->buf = realloc(p->buf, s);
                    592:                if (NULL == p->buf)
                    593:                        err(1, "realloc");
                    594:                p->maxcols = s;
                    595:        }
                    596:        p->buf[(int)(p->col)++] = c;
1.51      kristaps  597: }
                    598:

CVSweb