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

Annotation of mandoc/term.c, Revision 1.123

1.123   ! kristaps    1: /*     $Id: term.c,v 1.122 2009/11/05 08:40:16 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.122     kristaps   18: #include <ctype.h>
1.22      kristaps   19: #include <stdio.h>
1.1       kristaps   20: #include <stdlib.h>
                     21: #include <string.h>
1.113     kristaps   22: #include <time.h>
1.1       kristaps   23:
1.101     kristaps   24: #include "chars.h"
1.107     kristaps   25: #include "out.h"
1.71      kristaps   26: #include "term.h"
                     27: #include "man.h"
                     28: #include "mdoc.h"
1.105     kristaps   29: #include "main.h"
1.1       kristaps   30:
1.103     kristaps   31: /* FIXME: accomodate non-breaking, non-collapsing white-space. */
                     32: /* FIXME: accomodate non-breaking, collapsing white-space. */
                     33:
1.71      kristaps   34: static struct termp     *term_alloc(enum termenc);
                     35: static void              term_free(struct termp *);
1.95      kristaps   36:
                     37: static void              do_escaped(struct termp *, const char **);
                     38: static void              do_special(struct termp *,
1.71      kristaps   39:                                const char *, size_t);
1.95      kristaps   40: static void              do_reserved(struct termp *,
1.94      kristaps   41:                                const char *, size_t);
1.95      kristaps   42: static void              buffer(struct termp *, char);
                     43: static void              encode(struct termp *, char);
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.99      kristaps   54: void
1.71      kristaps   55: terminal_free(void *arg)
1.11      kristaps   56: {
                     57:
1.71      kristaps   58:        term_free((struct termp *)arg);
1.11      kristaps   59: }
                     60:
                     61:
1.71      kristaps   62: static void
                     63: term_free(struct termp *p)
1.14      kristaps   64: {
                     65:
1.71      kristaps   66:        if (p->buf)
                     67:                free(p->buf);
1.102     kristaps   68:        if (p->symtab)
1.101     kristaps   69:                chars_free(p->symtab);
1.14      kristaps   70:
1.71      kristaps   71:        free(p);
1.14      kristaps   72: }
                     73:
                     74:
1.71      kristaps   75: static struct termp *
                     76: term_alloc(enum termenc enc)
1.14      kristaps   77: {
1.71      kristaps   78:        struct termp *p;
1.14      kristaps   79:
1.117     kristaps   80:        p = calloc(1, sizeof(struct termp));
                     81:        if (NULL == p) {
1.120     kristaps   82:                perror(NULL);
1.117     kristaps   83:                exit(EXIT_FAILURE);
                     84:        }
1.80      kristaps   85:        p->maxrmargin = 78;
1.71      kristaps   86:        p->enc = enc;
                     87:        return(p);
1.14      kristaps   88: }
                     89:
                     90:
1.71      kristaps   91: /*
                     92:  * Flush a line of text.  A "line" is loosely defined as being something
                     93:  * that should be followed by a newline, regardless of whether it's
                     94:  * broken apart by newlines getting there.  A line can also be a
                     95:  * fragment of a columnar list.
                     96:  *
                     97:  * Specifically, a line is whatever's in p->buf of length p->col, which
                     98:  * is zeroed after this function returns.
                     99:  *
1.84      kristaps  100:  * The usage of termp:flags is as follows:
1.71      kristaps  101:  *
                    102:  *  - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
                    103:  *    offset value.  This is useful when doing columnar lists where the
                    104:  *    prior column has right-padded.
                    105:  *
                    106:  *  - TERMP_NOBREAK: this is the most important and is used when making
                    107:  *    columns.  In short: don't print a newline and instead pad to the
                    108:  *    right margin.  Used in conjunction with TERMP_NOLPAD.
                    109:  *
1.91      kristaps  110:  *  - TERMP_TWOSPACE: when padding, make sure there are at least two
                    111:  *    space characters of padding.  Otherwise, rather break the line.
                    112:  *
1.84      kristaps  113:  *  - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
                    114:  *    the line is overrun, and don't pad-right if it's underrun.
                    115:  *
                    116:  *  - TERMP_HANG: like TERMP_DANGLE, but doesn't newline when
                    117:  *    overruning, instead save the position and continue at that point
                    118:  *    when the next invocation.
1.71      kristaps  119:  *
                    120:  *  In-line line breaking:
                    121:  *
                    122:  *  If TERMP_NOBREAK is specified and the line overruns the right
                    123:  *  margin, it will break and pad-right to the right margin after
                    124:  *  writing.  If maxrmargin is violated, it will break and continue
1.114     kristaps  125:  *  writing from the right-margin, which will lead to the above scenario
                    126:  *  upon exit.  Otherwise, the line will break at the right margin.
1.71      kristaps  127:  */
                    128: void
                    129: term_flushln(struct termp *p)
1.53      kristaps  130: {
1.114     kristaps  131:        int              i;     /* current input position in p->buf */
                    132:        size_t           vis;   /* current visual position on output */
                    133:        size_t           vbl;   /* number of blanks to prepend to output */
                    134:        size_t           vsz;   /* visual characters to write to output */
                    135:        size_t           bp;    /* visual right border position */
                    136:        int              j;     /* temporary loop index */
                    137:        size_t           maxvis, mmax;
1.91      kristaps  138:        static int       overstep = 0;
1.53      kristaps  139:
1.71      kristaps  140:        /*
                    141:         * First, establish the maximum columns of "visible" content.
                    142:         * This is usually the difference between the right-margin and
                    143:         * an indentation, but can be, for tagged lists or columns, a
1.115     kristaps  144:         * small set of values.
1.71      kristaps  145:         */
1.53      kristaps  146:
1.71      kristaps  147:        assert(p->offset < p->rmargin);
1.92      kristaps  148:
1.114     kristaps  149:        maxvis = (int)(p->rmargin - p->offset) - overstep < 0 ?
1.119     kristaps  150:                /* LINTED */
                    151:                0 : p->rmargin - p->offset - overstep;
1.114     kristaps  152:        mmax = (int)(p->maxrmargin - p->offset) - overstep < 0 ?
1.119     kristaps  153:                /* LINTED */
                    154:                0 : p->maxrmargin - p->offset - overstep;
1.92      kristaps  155:
1.71      kristaps  156:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.115     kristaps  157:
                    158:        /*
                    159:         * FIXME: if bp is zero, we still output the first word before
                    160:         * breaking the line.
                    161:         */
                    162:
1.71      kristaps  163:        vis = 0;
1.84      kristaps  164:
1.71      kristaps  165:        /*
                    166:         * If in the standard case (left-justified), then begin with our
                    167:         * indentation, otherwise (columns, etc.) just start spitting
                    168:         * out text.
                    169:         */
1.53      kristaps  170:
1.71      kristaps  171:        if ( ! (p->flags & TERMP_NOLPAD))
                    172:                /* LINTED */
                    173:                for (j = 0; j < (int)p->offset; j++)
                    174:                        putchar(' ');
                    175:
                    176:        for (i = 0; i < (int)p->col; i++) {
                    177:                /*
                    178:                 * Count up visible word characters.  Control sequences
                    179:                 * (starting with the CSI) aren't counted.  A space
                    180:                 * generates a non-printing word, which is valid (the
                    181:                 * space is printed according to regular spacing rules).
                    182:                 */
                    183:
                    184:                /* LINTED */
                    185:                for (j = i, vsz = 0; j < (int)p->col; j++) {
1.93      kristaps  186:                        if (j && ' ' == p->buf[j])
1.71      kristaps  187:                                break;
                    188:                        else if (8 == p->buf[j])
1.89      kristaps  189:                                vsz--;
1.71      kristaps  190:                        else
                    191:                                vsz++;
                    192:                }
1.53      kristaps  193:
1.71      kristaps  194:                /*
1.81      kristaps  195:                 * Choose the number of blanks to prepend: no blank at the
                    196:                 * beginning of a line, one between words -- but do not
                    197:                 * actually write them yet.
1.71      kristaps  198:                 */
1.81      kristaps  199:                vbl = (size_t)(0 == vis ? 0 : 1);
1.71      kristaps  200:
1.81      kristaps  201:                /*
                    202:                 * Find out whether we would exceed the right margin.
                    203:                 * If so, break to the next line.  (TODO: hyphenate)
                    204:                 * Otherwise, write the chosen number of blanks now.
                    205:                 */
                    206:                if (vis && vis + vbl + vsz > bp) {
                    207:                        putchar('\n');
                    208:                        if (TERMP_NOBREAK & p->flags) {
                    209:                                for (j = 0; j < (int)p->rmargin; j++)
                    210:                                        putchar(' ');
                    211:                                vis = p->rmargin - p->offset;
                    212:                        } else {
1.71      kristaps  213:                                for (j = 0; j < (int)p->offset; j++)
                    214:                                        putchar(' ');
                    215:                                vis = 0;
1.81      kristaps  216:                        }
1.104     kristaps  217:                        /* Remove the overstep width. */
1.112     kristaps  218:                        bp += (int)/* LINTED */
                    219:                                overstep;
1.110     kristaps  220:                        overstep = 0;
1.81      kristaps  221:                } else {
                    222:                        for (j = 0; j < (int)vbl; j++)
1.71      kristaps  223:                                putchar(' ');
1.81      kristaps  224:                        vis += vbl;
1.71      kristaps  225:                }
1.53      kristaps  226:
1.78      kristaps  227:                /*
1.81      kristaps  228:                 * Finally, write out the word.
1.71      kristaps  229:                 */
                    230:                for ( ; i < (int)p->col; i++) {
                    231:                        if (' ' == p->buf[i])
                    232:                                break;
1.121     kristaps  233:
                    234:                        /* The unit sep. is a non-breaking space. */
                    235:                        if (31 == p->buf[i])
                    236:                                putchar(' ');
                    237:                        else
                    238:                                putchar(p->buf[i]);
1.71      kristaps  239:                }
                    240:                vis += vsz;
                    241:        }
1.111     kristaps  242:
1.91      kristaps  243:        p->col = 0;
1.111     kristaps  244:        overstep = 0;
1.15      kristaps  245:
1.91      kristaps  246:        if ( ! (TERMP_NOBREAK & p->flags)) {
                    247:                putchar('\n');
1.15      kristaps  248:                return;
1.71      kristaps  249:        }
1.15      kristaps  250:
1.91      kristaps  251:        if (TERMP_HANG & p->flags) {
                    252:                /* We need one blank after the tag. */
1.92      kristaps  253:                overstep = /* LINTED */
                    254:                        vis - maxvis + 1;
1.91      kristaps  255:
                    256:                /*
                    257:                 * Behave exactly the same way as groff:
1.92      kristaps  258:                 * If we have overstepped the margin, temporarily move
                    259:                 * it to the right and flag the rest of the line to be
                    260:                 * shorter.
1.91      kristaps  261:                 * If we landed right at the margin, be happy.
1.92      kristaps  262:                 * If we are one step before the margin, temporarily
                    263:                 * move it one step LEFT and flag the rest of the line
                    264:                 * to be longer.
1.91      kristaps  265:                 */
1.92      kristaps  266:                if (overstep >= -1) {
                    267:                        assert((int)maxvis + overstep >= 0);
                    268:                        /* LINTED */
1.91      kristaps  269:                        maxvis += overstep;
1.92      kristaps  270:                } else
1.91      kristaps  271:                        overstep = 0;
                    272:
                    273:        } else if (TERMP_DANGLE & p->flags)
                    274:                return;
1.15      kristaps  275:
1.92      kristaps  276:        /* Right-pad. */
                    277:        if (maxvis > vis + /* LINTED */
                    278:                        ((TERMP_TWOSPACE & p->flags) ? 1 : 0))
1.91      kristaps  279:                for ( ; vis < maxvis; vis++)
                    280:                        putchar(' ');
1.92      kristaps  281:        else {  /* ...or newline break. */
1.71      kristaps  282:                putchar('\n');
1.91      kristaps  283:                for (i = 0; i < (int)p->rmargin; i++)
                    284:                        putchar(' ');
                    285:        }
1.15      kristaps  286: }
                    287:
                    288:
1.71      kristaps  289: /*
                    290:  * A newline only breaks an existing line; it won't assert vertical
                    291:  * space.  All data in the output buffer is flushed prior to the newline
                    292:  * assertion.
                    293:  */
                    294: void
                    295: term_newln(struct termp *p)
1.15      kristaps  296: {
                    297:
1.71      kristaps  298:        p->flags |= TERMP_NOSPACE;
                    299:        if (0 == p->col) {
                    300:                p->flags &= ~TERMP_NOLPAD;
1.15      kristaps  301:                return;
1.16      kristaps  302:        }
1.71      kristaps  303:        term_flushln(p);
                    304:        p->flags &= ~TERMP_NOLPAD;
1.16      kristaps  305: }
                    306:
                    307:
1.71      kristaps  308: /*
                    309:  * Asserts a vertical space (a full, empty line-break between lines).
                    310:  * Note that if used twice, this will cause two blank spaces and so on.
                    311:  * All data in the output buffer is flushed prior to the newline
                    312:  * assertion.
                    313:  */
                    314: void
                    315: term_vspace(struct termp *p)
1.16      kristaps  316: {
                    317:
1.62      kristaps  318:        term_newln(p);
1.71      kristaps  319:        putchar('\n');
1.16      kristaps  320: }
                    321:
                    322:
1.71      kristaps  323: static void
1.95      kristaps  324: do_special(struct termp *p, const char *word, size_t len)
1.17      kristaps  325: {
1.71      kristaps  326:        const char      *rhs;
                    327:        size_t           sz;
1.79      kristaps  328:        int              i;
1.17      kristaps  329:
1.101     kristaps  330:        rhs = chars_a2ascii(p->symtab, word, len, &sz);
1.86      kristaps  331:
1.96      kristaps  332:        if (NULL == rhs) {
1.97      kristaps  333: #if 0
1.96      kristaps  334:                fputs("Unknown special character: ", stderr);
                    335:                for (i = 0; i < (int)len; i++)
                    336:                        fputc(word[i], stderr);
                    337:                fputc('\n', stderr);
                    338: #endif
1.94      kristaps  339:                return;
1.96      kristaps  340:        }
1.94      kristaps  341:        for (i = 0; i < (int)sz; i++)
1.95      kristaps  342:                encode(p, rhs[i]);
1.94      kristaps  343: }
                    344:
                    345:
                    346: static void
1.95      kristaps  347: do_reserved(struct termp *p, const char *word, size_t len)
1.94      kristaps  348: {
                    349:        const char      *rhs;
                    350:        size_t           sz;
                    351:        int              i;
                    352:
1.101     kristaps  353:        rhs = chars_a2res(p->symtab, word, len, &sz);
1.94      kristaps  354:
1.96      kristaps  355:        if (NULL == rhs) {
                    356: #if 0
                    357:                fputs("Unknown reserved word: ", stderr);
                    358:                for (i = 0; i < (int)len; i++)
                    359:                        fputc(word[i], stderr);
                    360:                fputc('\n', stderr);
                    361: #endif
1.94      kristaps  362:                return;
1.96      kristaps  363:        }
1.94      kristaps  364:        for (i = 0; i < (int)sz; i++)
1.95      kristaps  365:                encode(p, rhs[i]);
1.17      kristaps  366: }
                    367:
                    368:
1.71      kristaps  369: /*
                    370:  * Handle an escape sequence: determine its length and pass it to the
                    371:  * escape-symbol look table.  Note that we assume mdoc(3) has validated
                    372:  * the escape sequence (we assert upon badly-formed escape sequences).
                    373:  */
                    374: static void
1.95      kristaps  375: do_escaped(struct termp *p, const char **word)
1.17      kristaps  376: {
1.123   ! kristaps  377:        int              j, type, sv, t, lim;
1.86      kristaps  378:        const char      *wp;
                    379:
                    380:        wp = *word;
1.97      kristaps  381:        type = 1;
1.17      kristaps  382:
1.123   ! kristaps  383:        if ('\0' == *(++wp)) {
1.86      kristaps  384:                *word = wp;
1.71      kristaps  385:                return;
1.86      kristaps  386:        }
1.17      kristaps  387:
1.86      kristaps  388:        if ('(' == *wp) {
                    389:                wp++;
1.123   ! kristaps  390:                if ('\0' == *wp || '\0' == *(wp + 1)) {
        !           391:                        *word = '\0' == *wp ? wp : wp + 1;
1.71      kristaps  392:                        return;
1.86      kristaps  393:                }
1.22      kristaps  394:
1.95      kristaps  395:                do_special(p, wp, 2);
1.86      kristaps  396:                *word = ++wp;
1.71      kristaps  397:                return;
1.22      kristaps  398:
1.86      kristaps  399:        } else if ('*' == *wp) {
1.123   ! kristaps  400:                if ('\0' == *(++wp)) {
1.86      kristaps  401:                        *word = wp;
1.71      kristaps  402:                        return;
1.86      kristaps  403:                }
1.22      kristaps  404:
1.86      kristaps  405:                switch (*wp) {
1.71      kristaps  406:                case ('('):
1.86      kristaps  407:                        wp++;
1.123   ! kristaps  408:                        if ('\0' == *wp || '\0' == *(wp + 1)) {
        !           409:                                *word = '\0' == *wp ? wp : wp + 1;
1.71      kristaps  410:                                return;
1.86      kristaps  411:                        }
1.65      kristaps  412:
1.95      kristaps  413:                        do_reserved(p, wp, 2);
1.86      kristaps  414:                        *word = ++wp;
1.71      kristaps  415:                        return;
                    416:                case ('['):
1.97      kristaps  417:                        type = 0;
1.71      kristaps  418:                        break;
                    419:                default:
1.95      kristaps  420:                        do_reserved(p, wp, 1);
1.86      kristaps  421:                        *word = wp;
1.71      kristaps  422:                        return;
                    423:                }
1.123   ! kristaps  424:
        !           425:        } else if ('s' == *wp) {
        !           426:                /* This closely follows mandoc_special(). */
        !           427:                if ('\0' == *(++wp)) {
        !           428:                        *word = wp;
        !           429:                        return;
        !           430:                }
        !           431:
        !           432:                t = 0;
        !           433:                lim = 1;
        !           434:
        !           435:                if (*wp == '\'') {
        !           436:                        lim = 0;
        !           437:                        t = 1;
        !           438:                        ++wp;
        !           439:                } else if (*wp == '[') {
        !           440:                        lim = 0;
        !           441:                        t = 2;
        !           442:                        ++wp;
        !           443:                } else if (*wp == '(') {
        !           444:                        lim = 2;
        !           445:                        t = 3;
        !           446:                        ++wp;
        !           447:                }
        !           448:
        !           449:                if (*wp == '+' || *wp == '-')
        !           450:                        ++wp;
        !           451:
        !           452:                if (*wp == '\'') {
        !           453:                        if (t) {
        !           454:                                *word = wp;
        !           455:                                return;
        !           456:                        }
        !           457:                        lim = 0;
        !           458:                        t = 1;
        !           459:                        ++wp;
        !           460:                } else if (*wp == '[') {
        !           461:                        if (t) {
        !           462:                                *word = wp;
        !           463:                                return;
        !           464:                        }
        !           465:                        lim = 0;
        !           466:                        t = 2;
        !           467:                        ++wp;
        !           468:                } else if (*wp == '(') {
        !           469:                        if (t) {
        !           470:                                *word = wp;
        !           471:                                return;
        !           472:                        }
        !           473:                        lim = 2;
        !           474:                        t = 3;
        !           475:                        ++wp;
        !           476:                }
        !           477:
        !           478:                if ( ! isdigit((u_char)*wp)) {
        !           479:                        *word = --wp;
        !           480:                        return;
        !           481:                }
        !           482:
        !           483:                for (j = 0; isdigit((u_char)*wp); j++) {
        !           484:                        if (lim && j >= lim)
        !           485:                                break;
        !           486:                        ++wp;
        !           487:                }
        !           488:
        !           489:                if (t && t < 3) {
        !           490:                        if (1 == t && *wp != '\'') {
        !           491:                                *word = --wp;
        !           492:                                return;
        !           493:                        }
        !           494:                        if (2 == t && *wp != ']') {
        !           495:                                *word = --wp;
        !           496:                                return;
        !           497:                        }
        !           498:                        ++wp;
        !           499:                }
        !           500:                *word = --wp;
        !           501:                return;
        !           502:
1.86      kristaps  503:        } else if ('f' == *wp) {
1.123   ! kristaps  504:                if ('\0' == *(++wp)) {
1.86      kristaps  505:                        *word = wp;
1.71      kristaps  506:                        return;
1.86      kristaps  507:                }
                    508:
                    509:                switch (*wp) {
1.122     kristaps  510:                case ('3'):
                    511:                        /* FALLTHROUGH */
1.71      kristaps  512:                case ('B'):
1.122     kristaps  513:                        p->metamask = p->metafont;
                    514:                        p->metafont |= METAF_BOLD;
1.71      kristaps  515:                        break;
1.122     kristaps  516:                case ('2'):
                    517:                        /* FALLTHROUGH */
1.71      kristaps  518:                case ('I'):
1.122     kristaps  519:                        p->metamask = p->metafont;
                    520:                        p->metafont |= METAF_UNDER;
1.71      kristaps  521:                        break;
                    522:                case ('P'):
1.122     kristaps  523:                        sv = p->metamask;
                    524:                        p->metamask = p->metafont;
                    525:                        p->metafont = sv;
                    526:                        break;
                    527:                case ('1'):
1.71      kristaps  528:                        /* FALLTHROUGH */
                    529:                case ('R'):
1.122     kristaps  530:                        p->metamask = p->metafont;
                    531:                        p->metafont &= ~METAF_UNDER;
                    532:                        p->metafont &= ~METAF_BOLD;
1.71      kristaps  533:                        break;
                    534:                default:
                    535:                        break;
                    536:                }
1.86      kristaps  537:
                    538:                *word = wp;
1.71      kristaps  539:                return;
1.22      kristaps  540:
1.86      kristaps  541:        } else if ('[' != *wp) {
1.95      kristaps  542:                do_special(p, wp, 1);
1.86      kristaps  543:                *word = wp;
1.71      kristaps  544:                return;
                    545:        }
1.28      kristaps  546:
1.86      kristaps  547:        wp++;
                    548:        for (j = 0; *wp && ']' != *wp; wp++, j++)
1.71      kristaps  549:                /* Loop... */ ;
1.28      kristaps  550:
1.123   ! kristaps  551:        if ('\0' == *wp) {
1.86      kristaps  552:                *word = wp;
1.71      kristaps  553:                return;
1.86      kristaps  554:        }
1.48      kristaps  555:
1.97      kristaps  556:        if (type)
                    557:                do_special(p, wp - j, (size_t)j);
                    558:        else
                    559:                do_reserved(p, wp - j, (size_t)j);
1.86      kristaps  560:        *word = wp;
1.48      kristaps  561: }
                    562:
                    563:
1.71      kristaps  564: /*
                    565:  * Handle pwords, partial words, which may be either a single word or a
                    566:  * phrase that cannot be broken down (such as a literal string).  This
                    567:  * handles word styling.
                    568:  */
1.86      kristaps  569: void
                    570: term_word(struct termp *p, const char *word)
1.65      kristaps  571: {
1.88      kristaps  572:        const char       *sv;
1.71      kristaps  573:
1.100     kristaps  574:        sv = word;
                    575:
1.123   ! kristaps  576:        if (word[0] && '\0' == word[1])
1.100     kristaps  577:                switch (word[0]) {
                    578:                case('.'):
                    579:                        /* FALLTHROUGH */
                    580:                case(','):
                    581:                        /* FALLTHROUGH */
                    582:                case(';'):
                    583:                        /* FALLTHROUGH */
                    584:                case(':'):
                    585:                        /* FALLTHROUGH */
                    586:                case('?'):
                    587:                        /* FALLTHROUGH */
                    588:                case('!'):
                    589:                        /* FALLTHROUGH */
                    590:                case(')'):
                    591:                        /* FALLTHROUGH */
                    592:                case(']'):
                    593:                        /* FALLTHROUGH */
                    594:                case('}'):
                    595:                        if ( ! (TERMP_IGNDELIM & p->flags))
                    596:                                p->flags |= TERMP_NOSPACE;
                    597:                        break;
                    598:                default:
                    599:                        break;
                    600:                }
1.65      kristaps  601:
1.71      kristaps  602:        if ( ! (TERMP_NOSPACE & p->flags))
1.95      kristaps  603:                buffer(p, ' ');
1.65      kristaps  604:
1.71      kristaps  605:        if ( ! (p->flags & TERMP_NONOSPACE))
                    606:                p->flags &= ~TERMP_NOSPACE;
1.65      kristaps  607:
1.100     kristaps  608:        for ( ; *word; word++)
1.86      kristaps  609:                if ('\\' != *word)
1.95      kristaps  610:                        encode(p, *word);
1.79      kristaps  611:                else
1.95      kristaps  612:                        do_escaped(p, &word);
1.65      kristaps  613:
1.100     kristaps  614:        if (sv[0] && 0 == sv[1])
                    615:                switch (sv[0]) {
                    616:                case('('):
                    617:                        /* FALLTHROUGH */
                    618:                case('['):
                    619:                        /* FALLTHROUGH */
                    620:                case('{'):
                    621:                        p->flags |= TERMP_NOSPACE;
                    622:                        break;
                    623:                default:
                    624:                        break;
                    625:                }
1.65      kristaps  626: }
                    627:
                    628:
1.71      kristaps  629: /*
                    630:  * Insert a single character into the line-buffer.  If the buffer's
                    631:  * space is exceeded, then allocate more space by doubling the buffer
                    632:  * size.
                    633:  */
                    634: static void
1.95      kristaps  635: buffer(struct termp *p, char c)
1.51      kristaps  636: {
1.71      kristaps  637:        size_t           s;
1.51      kristaps  638:
1.71      kristaps  639:        if (p->col + 1 >= p->maxcols) {
                    640:                if (0 == p->maxcols)
                    641:                        p->maxcols = 256;
                    642:                s = p->maxcols * 2;
                    643:                p->buf = realloc(p->buf, s);
1.118     kristaps  644:                if (NULL == p->buf) {
1.120     kristaps  645:                        perror(NULL);
1.118     kristaps  646:                        exit(EXIT_FAILURE);
                    647:                }
1.71      kristaps  648:                p->maxcols = s;
                    649:        }
                    650:        p->buf[(int)(p->col)++] = c;
1.51      kristaps  651: }
                    652:
1.79      kristaps  653:
                    654: static void
1.95      kristaps  655: encode(struct termp *p, char c)
1.79      kristaps  656: {
1.89      kristaps  657:
1.122     kristaps  658:        if (isgraph((u_char)c)) {
                    659:                if (p->under || METAF_UNDER & p->metafont) {
1.109     kristaps  660:                        buffer(p, '_');
                    661:                        buffer(p, 8);
                    662:                }
1.122     kristaps  663:                if (p->bold || METAF_BOLD & p->metafont) {
1.95      kristaps  664:                        buffer(p, c);
                    665:                        buffer(p, 8);
1.79      kristaps  666:                }
                    667:        }
1.95      kristaps  668:        buffer(p, c);
1.79      kristaps  669: }
1.106     kristaps  670:
                    671:
1.107     kristaps  672: size_t
                    673: term_vspan(const struct roffsu *su)
1.106     kristaps  674: {
                    675:        double           r;
                    676:
1.107     kristaps  677:        switch (su->unit) {
1.106     kristaps  678:        case (SCALE_CM):
1.107     kristaps  679:                r = su->scale * 2;
1.106     kristaps  680:                break;
                    681:        case (SCALE_IN):
1.107     kristaps  682:                r = su->scale * 6;
1.106     kristaps  683:                break;
                    684:        case (SCALE_PC):
1.107     kristaps  685:                r = su->scale;
1.106     kristaps  686:                break;
                    687:        case (SCALE_PT):
1.107     kristaps  688:                r = su->scale / 8;
1.106     kristaps  689:                break;
                    690:        case (SCALE_MM):
1.107     kristaps  691:                r = su->scale / 1000;
1.106     kristaps  692:                break;
                    693:        case (SCALE_VS):
1.107     kristaps  694:                r = su->scale;
1.106     kristaps  695:                break;
                    696:        default:
1.107     kristaps  697:                r = su->scale - 1;
1.106     kristaps  698:                break;
                    699:        }
                    700:
                    701:        if (r < 0.0)
                    702:                r = 0.0;
1.107     kristaps  703:        return(/* LINTED */(size_t)
1.106     kristaps  704:                        r);
                    705: }
                    706:
                    707:
1.107     kristaps  708: size_t
                    709: term_hspan(const struct roffsu *su)
1.106     kristaps  710: {
                    711:        double           r;
                    712:
1.108     kristaps  713:        /* XXX: CM, IN, and PT are approximations. */
                    714:
1.107     kristaps  715:        switch (su->unit) {
1.106     kristaps  716:        case (SCALE_CM):
1.108     kristaps  717:                r = 4 * su->scale;
1.106     kristaps  718:                break;
                    719:        case (SCALE_IN):
1.108     kristaps  720:                /* XXX: this is an approximation. */
                    721:                r = 10 * su->scale;
1.106     kristaps  722:                break;
                    723:        case (SCALE_PC):
1.108     kristaps  724:                r = (10 * su->scale) / 6;
1.106     kristaps  725:                break;
                    726:        case (SCALE_PT):
1.108     kristaps  727:                r = (10 * su->scale) / 72;
1.106     kristaps  728:                break;
                    729:        case (SCALE_MM):
1.107     kristaps  730:                r = su->scale / 1000; /* FIXME: double-check. */
1.106     kristaps  731:                break;
                    732:        case (SCALE_VS):
1.107     kristaps  733:                r = su->scale * 2 - 1; /* FIXME: double-check. */
1.106     kristaps  734:                break;
                    735:        default:
1.107     kristaps  736:                r = su->scale;
1.106     kristaps  737:                break;
                    738:        }
                    739:
                    740:        if (r < 0.0)
                    741:                r = 0.0;
1.107     kristaps  742:        return((size_t)/* LINTED */
1.106     kristaps  743:                        r);
                    744: }
                    745:
                    746:

CVSweb