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

Annotation of mandoc/term.c, Revision 1.105

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

CVSweb