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

Annotation of mandoc/mdocterm.c, Revision 1.18

1.18    ! kristaps    1: /* $Id: mdocterm.c,v 1.17 2009/02/27 09:14:02 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
1.18    ! kristaps   19: #include <sys/utsname.h>
        !            20:
1.1       kristaps   21: #include <assert.h>
1.3       kristaps   22: #include <ctype.h>
1.1       kristaps   23: #include <err.h>
                     24: #include <getopt.h>
1.3       kristaps   25: #include <stdio.h>
1.1       kristaps   26: #include <stdlib.h>
1.3       kristaps   27: #include <string.h>
1.1       kristaps   28:
1.7       kristaps   29: #ifndef __OpenBSD__
1.5       kristaps   30: #include <time.h>
                     31: #endif
                     32:
1.2       kristaps   33: #include "mmain.h"
1.1       kristaps   34: #include "term.h"
                     35:
1.16      kristaps   36: #define        TERMSYM_RBRACK          "]"
                     37: #define        TERMSYM_LBRACK          "["
                     38: #define        TERMSYM_LARROW          "<-"
                     39: #define        TERMSYM_RARROW          "->"
                     40: #define        TERMSYM_UARROW          "^"
1.17      kristaps   41: #define        TERMSYM_DARROW          "v"
1.16      kristaps   42: #define        TERMSYM_LSQUOTE         "`"
                     43: #define        TERMSYM_RSQUOTE         "\'"
                     44: #define        TERMSYM_SQUOTE          "\'"
                     45: #define        TERMSYM_LDQUOTE         "``"
                     46: #define        TERMSYM_RDQUOTE         "\'\'"
                     47: #define        TERMSYM_DQUOTE          "\""
                     48: #define        TERMSYM_LT              "<"
                     49: #define        TERMSYM_GT              ">"
                     50: #define        TERMSYM_LE              "<="
                     51: #define        TERMSYM_GE              ">="
                     52: #define        TERMSYM_EQ              "=="
                     53: #define        TERMSYM_NEQ             "!="
                     54: #define        TERMSYM_ACUTE           "\'"
                     55: #define        TERMSYM_GRAVE           "`"
                     56: #define        TERMSYM_PI              "pi"
                     57: #define        TERMSYM_PLUSMINUS       "+="
1.17      kristaps   58: #define        TERMSYM_INF             "oo"
                     59: #define        TERMSYM_INF2            "infinity"
1.16      kristaps   60: #define        TERMSYM_NAN             "NaN"
                     61: #define        TERMSYM_BAR             "|"
                     62: #define        TERMSYM_BULLET          "o"
                     63:
1.7       kristaps   64: #ifdef __NetBSD__
                     65: #define xisspace(x) isspace((int)(x))
                     66: #else
                     67: #define        xisspace(x) isspace((x))
                     68: #endif
                     69:
1.3       kristaps   70: enum   termstyle {
                     71:        STYLE_CLEAR,
                     72:        STYLE_BOLD,
                     73:        STYLE_UNDERLINE
                     74: };
                     75:
                     76: static void              body(struct termp *,
1.12      kristaps   77:                                struct termpair *,
1.3       kristaps   78:                                const struct mdoc_meta *,
                     79:                                const struct mdoc_node *);
                     80: static void              header(struct termp *,
                     81:                                const struct mdoc_meta *);
                     82: static void              footer(struct termp *,
                     83:                                const struct mdoc_meta *);
                     84:
                     85: static void              pword(struct termp *, const char *, size_t);
                     86: static void              pescape(struct termp *,
                     87:                                const char *, size_t *, size_t);
1.12      kristaps   88: static void              nescape(struct termp *,
                     89:                                const char *, size_t);
1.3       kristaps   90: static void              chara(struct termp *, char);
1.4       kristaps   91: static void              stringa(struct termp *, const char *);
1.3       kristaps   92: static void              style(struct termp *, enum termstyle);
                     93:
                     94: #ifdef __linux__
                     95: extern size_t            strlcat(char *, const char *, size_t);
                     96: extern size_t            strlcpy(char *, const char *, size_t);
                     97: #endif
                     98:
                     99:
1.1       kristaps  100: int
                    101: main(int argc, char *argv[])
                    102: {
1.2       kristaps  103:        struct mmain    *p;
                    104:        const struct mdoc *mdoc;
1.3       kristaps  105:        struct termp     termp;
1.2       kristaps  106:
                    107:        p = mmain_alloc();
1.1       kristaps  108:
1.3       kristaps  109:        if ( ! mmain_getopt(p, argc, argv, NULL, NULL, NULL, NULL))
1.2       kristaps  110:                mmain_exit(p, 1);
1.1       kristaps  111:
1.3       kristaps  112:        if (NULL == (mdoc = mmain_mdoc(p)))
                    113:                mmain_exit(p, 1);
                    114:
                    115:        termp.maxrmargin = 80; /* XXX */
                    116:        termp.rmargin = termp.maxrmargin;
                    117:        termp.maxcols = 1024;
                    118:        termp.offset = termp.col = 0;
                    119:        termp.flags = TERMP_NOSPACE;
                    120:
                    121:        if (NULL == (termp.buf = malloc(termp.maxcols)))
                    122:                err(1, "malloc");
                    123:
1.15      kristaps  124:        header(&termp, mdoc_meta(mdoc));
1.12      kristaps  125:        body(&termp, NULL, mdoc_meta(mdoc), mdoc_node(mdoc));
1.3       kristaps  126:        footer(&termp, mdoc_meta(mdoc));
                    127:
                    128:        free(termp.buf);
                    129:
                    130:        mmain_exit(p, 0);
                    131:        /* NOTREACHED */
                    132: }
                    133:
                    134:
                    135: void
                    136: flushln(struct termp *p)
                    137: {
                    138:        size_t           i, j, vsz, vis, maxvis;
                    139:
                    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
                    144:         * small set of values.
                    145:         */
                    146:
                    147:        assert(p->offset < p->rmargin);
                    148:        maxvis = p->rmargin - p->offset;
                    149:        vis = 0;
                    150:
                    151:        /*
                    152:         * If in the standard case (left-justified), then begin with our
                    153:         * indentation, otherwise (columns, etc.) just start spitting
                    154:         * out text.
                    155:         */
                    156:
                    157:        if ( ! (p->flags & TERMP_NOLPAD))
                    158:                /* LINTED */
                    159:                for (j = 0; j < p->offset; j++)
                    160:                        putchar(' ');
                    161:
                    162:        /*
                    163:         * If we're literal, print out verbatim.
                    164:         */
                    165:        if (p->flags & TERMP_LITERAL) {
                    166:                for (i = 0; i < p->col; i++)
                    167:                        putchar(p->buf[i]);
                    168:                putchar('\n');
                    169:                p->col = 0;
                    170:                return;
                    171:        }
                    172:
                    173:        for (i = 0; i < p->col; i++) {
                    174:                /*
                    175:                 * Count up visible word characters.  Control sequences
                    176:                 * (starting with the CSI) aren't counted.
                    177:                 */
1.7       kristaps  178:                assert( ! xisspace(p->buf[i]));
1.3       kristaps  179:
                    180:                /* LINTED */
                    181:                for (j = i, vsz = 0; j < p->col; j++) {
1.7       kristaps  182:                        if (xisspace(p->buf[j]))
1.3       kristaps  183:                                break;
                    184:                        else if (27 == p->buf[j]) {
                    185:                                assert(j + 4 <= p->col);
                    186:                                j += 3;
                    187:                        } else
                    188:                                vsz++;
                    189:                }
                    190:                assert(vsz > 0);
                    191:
                    192:                /*
                    193:                 * If a word is too long and we're within a line, put it
                    194:                 * on the next line.  Puke if we're being asked to write
                    195:                 * something that will exceed the right margin (i.e.,
                    196:                 * from a fresh line or when we're not allowed to break
                    197:                 * the line with TERMP_NOBREAK).
                    198:                 */
                    199:
1.16      kristaps  200:                /* FIXME: allow selective right-margin breaking. */
                    201:
1.14      kristaps  202:                if (vis && vis + vsz > maxvis) {
1.3       kristaps  203:                        if (p->flags & TERMP_NOBREAK)
                    204:                                errx(1, "word breaks right margin");
                    205:                        putchar('\n');
                    206:                        for (j = 0; j < p->offset; j++)
                    207:                                putchar(' ');
                    208:                        vis = 0;
1.14      kristaps  209:                } else if (vis + vsz > maxvis)
1.3       kristaps  210:                        errx(1, "word breaks right margin");
                    211:
                    212:                /*
                    213:                 * Write out the word and a trailing space.  Omit the
                    214:                 * space if we're the last word in the line.
                    215:                 */
                    216:
                    217:                for ( ; i < p->col; i++) {
1.7       kristaps  218:                        if (xisspace(p->buf[i]))
1.3       kristaps  219:                                break;
                    220:                        putchar(p->buf[i]);
                    221:                }
                    222:                vis += vsz;
                    223:                if (i < p->col) {
                    224:                        putchar(' ');
                    225:                        vis++;
                    226:                }
                    227:        }
                    228:
                    229:        /*
                    230:         * If we're not to right-marginalise it (newline), then instead
                    231:         * pad to the right margin and stay off.
                    232:         */
                    233:
                    234:        if (p->flags & TERMP_NOBREAK) {
1.14      kristaps  235:                if ( ! (p->flags & TERMP_NORPAD))
                    236:                        for ( ; vis < maxvis; vis++)
                    237:                                putchar(' ');
1.3       kristaps  238:        } else
                    239:                putchar('\n');
                    240:
                    241:        p->col = 0;
                    242: }
                    243:
                    244:
                    245: void
                    246: newln(struct termp *p)
                    247: {
1.1       kristaps  248:
1.3       kristaps  249:        /*
                    250:         * A newline only breaks an existing line; it won't assert
                    251:         * vertical space.
                    252:         */
                    253:        p->flags |= TERMP_NOSPACE;
1.12      kristaps  254:        if (0 == p->col) {
                    255:                p->flags &= ~TERMP_NOLPAD;
1.3       kristaps  256:                return;
1.12      kristaps  257:        }
1.3       kristaps  258:        flushln(p);
1.11      kristaps  259:        p->flags &= ~TERMP_NOLPAD;
1.3       kristaps  260: }
                    261:
                    262:
                    263: void
                    264: vspace(struct termp *p)
                    265: {
                    266:
                    267:        /*
                    268:         * Asserts a vertical space (a full, empty line-break between
                    269:         * lines).
                    270:         */
                    271:        newln(p);
                    272:        putchar('\n');
                    273: }
                    274:
                    275:
                    276: static void
1.4       kristaps  277: stringa(struct termp *p, const char *s)
                    278: {
                    279:
                    280:        /* XXX - speed up if not passing to chara. */
                    281:        for ( ; *s; s++)
                    282:                chara(p, *s);
                    283: }
                    284:
                    285:
                    286: static void
1.3       kristaps  287: chara(struct termp *p, char c)
                    288: {
                    289:
1.16      kristaps  290:        /*
                    291:         * Insert a single character into the line-buffer.  If the
                    292:         * buffer's space is exceeded, then allocate more space.
                    293:         */
                    294:        if (p->col + 1 >= p->maxcols) {
                    295:                p->buf = realloc(p->buf, p->maxcols * 2);
                    296:                if (NULL == p->buf)
                    297:                        err(1, "malloc");
                    298:                p->maxcols *= 2;
                    299:        }
1.3       kristaps  300:        p->buf[(p->col)++] = c;
                    301: }
                    302:
                    303:
                    304: static void
                    305: style(struct termp *p, enum termstyle esc)
                    306: {
                    307:
                    308:        if (p->col + 4 >= p->maxcols)
                    309:                errx(1, "line overrun");
                    310:
                    311:        p->buf[(p->col)++] = 27;
                    312:        p->buf[(p->col)++] = '[';
                    313:        switch (esc) {
                    314:        case (STYLE_CLEAR):
                    315:                p->buf[(p->col)++] = '0';
                    316:                break;
                    317:        case (STYLE_BOLD):
                    318:                p->buf[(p->col)++] = '1';
                    319:                break;
                    320:        case (STYLE_UNDERLINE):
                    321:                p->buf[(p->col)++] = '4';
                    322:                break;
                    323:        default:
                    324:                abort();
                    325:                /* NOTREACHED */
                    326:        }
                    327:        p->buf[(p->col)++] = 'm';
                    328: }
                    329:
                    330:
                    331: static void
1.12      kristaps  332: nescape(struct termp *p, const char *word, size_t len)
                    333: {
                    334:
                    335:        switch (len) {
1.16      kristaps  336:        case (1):
                    337:                if ('q' == word[0])
                    338:                        stringa(p, TERMSYM_DQUOTE);
                    339:                break;
1.12      kristaps  340:        case (2):
                    341:                if ('r' == word[0] && 'B' == word[1])
1.16      kristaps  342:                        stringa(p, TERMSYM_RBRACK);
1.12      kristaps  343:                else if ('l' == word[0] && 'B' == word[1])
1.16      kristaps  344:                        stringa(p, TERMSYM_LBRACK);
1.17      kristaps  345:                else if ('l' == word[0] && 'q' == word[1])
                    346:                        stringa(p, TERMSYM_LDQUOTE);
                    347:                else if ('r' == word[0] && 'q' == word[1])
                    348:                        stringa(p, TERMSYM_RDQUOTE);
                    349:                else if ('o' == word[0] && 'q' == word[1])
                    350:                        stringa(p, TERMSYM_LSQUOTE);
                    351:                else if ('a' == word[0] && 'q' == word[1])
                    352:                        stringa(p, TERMSYM_RSQUOTE);
1.12      kristaps  353:                else if ('<' == word[0] && '-' == word[1])
1.16      kristaps  354:                        stringa(p, TERMSYM_LARROW);
1.12      kristaps  355:                else if ('-' == word[0] && '>' == word[1])
1.16      kristaps  356:                        stringa(p, TERMSYM_RARROW);
1.12      kristaps  357:                else if ('b' == word[0] && 'u' == word[1])
1.16      kristaps  358:                        stringa(p, TERMSYM_BULLET);
                    359:                else if ('<' == word[0] && '=' == word[1])
                    360:                        stringa(p, TERMSYM_LE);
                    361:                else if ('>' == word[0] && '=' == word[1])
                    362:                        stringa(p, TERMSYM_GE);
1.17      kristaps  363:                else if ('=' == word[0] && '=' == word[1])
                    364:                        stringa(p, TERMSYM_EQ);
                    365:                else if ('+' == word[0] && '-' == word[1])
                    366:                        stringa(p, TERMSYM_PLUSMINUS);
1.16      kristaps  367:                else if ('u' == word[0] && 'a' == word[1])
                    368:                        stringa(p, TERMSYM_UARROW);
1.17      kristaps  369:                else if ('d' == word[0] && 'a' == word[1])
                    370:                        stringa(p, TERMSYM_DARROW);
1.16      kristaps  371:                else if ('a' == word[0] && 'a' == word[1])
                    372:                        stringa(p, TERMSYM_ACUTE);
                    373:                else if ('g' == word[0] && 'a' == word[1])
                    374:                        stringa(p, TERMSYM_GRAVE);
1.17      kristaps  375:                else if ('!' == word[0] && '=' == word[1])
1.16      kristaps  376:                        stringa(p, TERMSYM_NEQ);
1.17      kristaps  377:                else if ('i' == word[0] && 'f' == word[1])
                    378:                        stringa(p, TERMSYM_INF);
                    379:                else if ('n' == word[0] && 'a' == word[1])
                    380:                        stringa(p, TERMSYM_NAN);
                    381:                else if ('b' == word[0] && 'a' == word[1])
                    382:                        stringa(p, TERMSYM_BAR);
                    383:
                    384:                /* Deprecated forms. */
                    385:                else if ('B' == word[0] && 'a' == word[1])
                    386:                        stringa(p, TERMSYM_BAR);
                    387:                else if ('I' == word[0] && 'f' == word[1])
                    388:                        stringa(p, TERMSYM_INF2);
                    389:                else if ('G' == word[0] && 'e' == word[1])
                    390:                        stringa(p, TERMSYM_GE);
                    391:                else if ('G' == word[0] && 't' == word[1])
                    392:                        stringa(p, TERMSYM_GT);
                    393:                else if ('L' == word[0] && 'e' == word[1])
                    394:                        stringa(p, TERMSYM_LE);
                    395:                else if ('L' == word[0] && 'q' == word[1])
                    396:                        stringa(p, TERMSYM_LDQUOTE);
1.16      kristaps  397:                else if ('L' == word[0] && 't' == word[1])
                    398:                        stringa(p, TERMSYM_LT);
1.17      kristaps  399:                else if ('N' == word[0] && 'a' == word[1])
                    400:                        stringa(p, TERMSYM_NAN);
                    401:                else if ('N' == word[0] && 'e' == word[1])
                    402:                        stringa(p, TERMSYM_NEQ);
                    403:                else if ('P' == word[0] && 'i' == word[1])
                    404:                        stringa(p, TERMSYM_PI);
1.16      kristaps  405:                else if ('P' == word[0] && 'm' == word[1])
                    406:                        stringa(p, TERMSYM_PLUSMINUS);
1.17      kristaps  407:                else if ('R' == word[0] && 'q' == word[1])
                    408:                        stringa(p, TERMSYM_RDQUOTE);
1.12      kristaps  409:                break;
                    410:        default:
                    411:                break;
                    412:        }
                    413: }
                    414:
                    415:
                    416: static void
1.3       kristaps  417: pescape(struct termp *p, const char *word, size_t *i, size_t len)
                    418: {
1.12      kristaps  419:        size_t           j;
1.3       kristaps  420:
                    421:        (*i)++;
                    422:        assert(*i < len);
                    423:
1.16      kristaps  424:        /*
                    425:         * Handle an escape sequence.  This must manage both groff-style
                    426:         * escapes and mdoc-style escapes.
                    427:         */
                    428:
1.3       kristaps  429:        if ('(' == word[*i]) {
                    430:                /* Two-character escapes. */
                    431:                (*i)++;
                    432:                assert(*i + 1 < len);
1.12      kristaps  433:                nescape(p, &word[*i], 2);
1.3       kristaps  434:                (*i)++;
                    435:                return;
                    436:
1.16      kristaps  437:        } else if ('*' == word[*i]) {
                    438:                (*i)++;
                    439:                assert(*i < len);
                    440:                switch (word[*i]) {
                    441:                case ('('):
                    442:                        (*i)++;
                    443:                        assert(*i + 1 < len);
                    444:                        nescape(p, &word[*i], 2);
                    445:                        (*i)++;
                    446:                        return;
                    447:                default:
                    448:                        break;
                    449:                }
                    450:                nescape(p, &word[*i], 1);
                    451:                return;
                    452:
1.3       kristaps  453:        } else if ('[' != word[*i]) {
                    454:                /* One-character escapes. */
                    455:                switch (word[*i]) {
                    456:                case ('\\'):
                    457:                        /* FALLTHROUGH */
                    458:                case ('\''):
                    459:                        /* FALLTHROUGH */
                    460:                case ('`'):
                    461:                        /* FALLTHROUGH */
                    462:                case ('-'):
                    463:                        /* FALLTHROUGH */
1.13      kristaps  464:                case (' '):
                    465:                        /* FALLTHROUGH */
1.3       kristaps  466:                case ('.'):
                    467:                        chara(p, word[*i]);
1.1       kristaps  468:                default:
1.3       kristaps  469:                        break;
                    470:                }
                    471:                return;
                    472:        }
1.12      kristaps  473:
                    474:        (*i)++;
                    475:        for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
                    476:                /* Loop... */ ;
                    477:
                    478:        nescape(p, &word[*i - j], j);
1.3       kristaps  479: }
                    480:
                    481:
                    482: static void
                    483: pword(struct termp *p, const char *word, size_t len)
                    484: {
                    485:        size_t           i;
                    486:
                    487:        /*assert(len > 0);*/ /* Can be, if literal. */
                    488:
1.16      kristaps  489:        /*
                    490:         * Handle pwords, partial words, which may be either a single
                    491:         * word or a phrase that cannot be broken down (such as a
                    492:         * literal string).  This handles word styling.
                    493:         */
                    494:
1.3       kristaps  495:        if ( ! (p->flags & TERMP_NOSPACE) &&
                    496:                        ! (p->flags & TERMP_LITERAL))
                    497:                chara(p, ' ');
                    498:
1.13      kristaps  499:        if ( ! (p->flags & TERMP_NONOSPACE))
                    500:                p->flags &= ~TERMP_NOSPACE;
1.3       kristaps  501:
1.16      kristaps  502:        /*
                    503:         * XXX - if literal and underlining, this will underline the
                    504:         * spaces between literal words.
                    505:         */
                    506:
1.3       kristaps  507:        if (p->flags & TERMP_BOLD)
                    508:                style(p, STYLE_BOLD);
                    509:        if (p->flags & TERMP_UNDERLINE)
                    510:                style(p, STYLE_UNDERLINE);
                    511:
                    512:        for (i = 0; i < len; i++) {
                    513:                if ('\\' == word[i]) {
                    514:                        pescape(p, word, &i, len);
                    515:                        continue;
                    516:                }
                    517:                chara(p, word[i]);
                    518:        }
                    519:
                    520:        if (p->flags & TERMP_BOLD ||
                    521:                        p->flags & TERMP_UNDERLINE)
                    522:                style(p, STYLE_CLEAR);
                    523: }
                    524:
                    525:
                    526: void
                    527: word(struct termp *p, const char *word)
                    528: {
                    529:        size_t           i, j, len;
                    530:
1.16      kristaps  531:        /*
                    532:         * Break apart a word into tokens.  If we're a literal word,
                    533:         * then don't.  This doesn't handle zero-length words (there
                    534:         * should be none) and makes sure that pword doesn't get spaces
                    535:         * or nil words unless literal.
                    536:         */
                    537:
1.3       kristaps  538:        if (p->flags & TERMP_LITERAL) {
                    539:                pword(p, word, strlen(word));
                    540:                return;
                    541:        }
                    542:
                    543:        len = strlen(word);
                    544:        assert(len > 0);
                    545:
                    546:        if (mdoc_isdelim(word)) {
                    547:                if ( ! (p->flags & TERMP_IGNDELIM))
                    548:                        p->flags |= TERMP_NOSPACE;
                    549:                p->flags &= ~TERMP_IGNDELIM;
                    550:        }
                    551:
                    552:        /* LINTED */
                    553:        for (j = i = 0; i < len; i++) {
1.7       kristaps  554:                if ( ! xisspace(word[i])) {
1.3       kristaps  555:                        j++;
                    556:                        continue;
1.1       kristaps  557:                }
1.3       kristaps  558:                if (0 == j)
                    559:                        continue;
                    560:                assert(i >= j);
                    561:                pword(p, &word[i - j], j);
                    562:                j = 0;
                    563:        }
                    564:        if (j > 0) {
                    565:                assert(i >= j);
                    566:                pword(p, &word[i - j], j);
                    567:        }
                    568: }
                    569:
                    570:
                    571: static void
1.12      kristaps  572: body(struct termp *p, struct termpair *ppair,
                    573:                const struct mdoc_meta *meta,
1.3       kristaps  574:                const struct mdoc_node *node)
                    575: {
                    576:        int              dochild;
1.9       kristaps  577:        struct termpair  pair;
1.3       kristaps  578:
1.16      kristaps  579:        /*
                    580:         * This is the main function for printing out nodes.  It's
                    581:         * constituted of PRE and POST functions, which correspond to
                    582:         * prefix and infix processing.
                    583:         */
                    584:
1.3       kristaps  585:        /* Pre-processing. */
                    586:
                    587:        dochild = 1;
1.12      kristaps  588:        pair.ppair = ppair;
1.9       kristaps  589:        pair.type = 0;
1.11      kristaps  590:        pair.offset = pair.rmargin = 0;
1.10      kristaps  591:        pair.flag = 0;
1.12      kristaps  592:        pair.count = 0;
1.3       kristaps  593:
                    594:        if (MDOC_TEXT != node->type) {
                    595:                if (termacts[node->tok].pre)
1.9       kristaps  596:                        if ( ! (*termacts[node->tok].pre)(p, &pair, meta, node))
1.3       kristaps  597:                                dochild = 0;
                    598:        } else /* MDOC_TEXT == node->type */
                    599:                word(p, node->data.text.string);
                    600:
                    601:        /* Children. */
                    602:
1.10      kristaps  603:        if (TERMPAIR_FLAG & pair.type)
                    604:                p->flags |= pair.flag;
1.9       kristaps  605:
1.3       kristaps  606:        if (dochild && node->child)
1.12      kristaps  607:                body(p, &pair, meta, node->child);
1.3       kristaps  608:
1.10      kristaps  609:        if (TERMPAIR_FLAG & pair.type)
                    610:                p->flags &= ~pair.flag;
1.9       kristaps  611:
1.3       kristaps  612:        /* Post-processing. */
                    613:
                    614:        if (MDOC_TEXT != node->type)
                    615:                if (termacts[node->tok].post)
1.9       kristaps  616:                        (*termacts[node->tok].post)(p, &pair, meta, node);
1.3       kristaps  617:
                    618:        /* Siblings. */
1.1       kristaps  619:
1.3       kristaps  620:        if (node->next)
1.12      kristaps  621:                body(p, ppair, meta, node->next);
1.3       kristaps  622: }
                    623:
                    624:
                    625: static void
                    626: footer(struct termp *p, const struct mdoc_meta *meta)
                    627: {
                    628:        struct tm       *tm;
                    629:        char            *buf, *os;
                    630:
                    631:        if (NULL == (buf = malloc(p->rmargin)))
                    632:                err(1, "malloc");
                    633:        if (NULL == (os = malloc(p->rmargin)))
                    634:                err(1, "malloc");
                    635:
                    636:        tm = localtime(&meta->date);
                    637:
1.7       kristaps  638: #ifdef __OpenBSD__
                    639:        if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                    640: #else
1.3       kristaps  641:        if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                    642: #endif
                    643:                err(1, "strftime");
                    644:
1.15      kristaps  645:        (void)strlcpy(os, meta->os, p->rmargin);
1.3       kristaps  646:
1.16      kristaps  647:        /*
                    648:         * This is /slightly/ different from regular groff output
                    649:         * because we don't have page numbers.  Print the following:
                    650:         *
                    651:         * OS                                            MDOCDATE
                    652:         */
                    653:
1.15      kristaps  654:        vspace(p);
1.3       kristaps  655:
1.15      kristaps  656:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
                    657:        p->rmargin = p->maxrmargin - strlen(buf);
                    658:        p->offset = 0;
1.3       kristaps  659:
1.15      kristaps  660:        word(p, os);
                    661:        flushln(p);
1.3       kristaps  662:
1.15      kristaps  663:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    664:        p->offset = p->rmargin;
                    665:        p->rmargin = p->maxrmargin;
                    666:        p->flags &= ~TERMP_NOBREAK;
                    667:
                    668:        word(p, buf);
                    669:        flushln(p);
1.1       kristaps  670:
1.3       kristaps  671:        free(buf);
                    672:        free(os);
1.1       kristaps  673: }
                    674:
                    675:
1.3       kristaps  676: static void
                    677: header(struct termp *p, const struct mdoc_meta *meta)
                    678: {
1.18    ! kristaps  679:        char            *buf, *title, *bufp, *vbuf;
1.13      kristaps  680:        const char      *pp;
1.18    ! kristaps  681:        struct utsname   uts;
        !           682:
        !           683:        p->rmargin = p->maxrmargin;
        !           684:        p->offset = 0;
1.3       kristaps  685:
                    686:        if (NULL == (buf = malloc(p->rmargin)))
                    687:                err(1, "malloc");
                    688:        if (NULL == (title = malloc(p->rmargin)))
                    689:                err(1, "malloc");
1.18    ! kristaps  690:        if (NULL == (vbuf = malloc(p->rmargin)))
        !           691:                err(1, "malloc");
1.3       kristaps  692:
1.18    ! kristaps  693:        if (NULL == (pp = mdoc_vol2a(meta->vol))) {
1.3       kristaps  694:                switch (meta->msec) {
                    695:                case (MSEC_1):
                    696:                        /* FALLTHROUGH */
                    697:                case (MSEC_6):
                    698:                        /* FALLTHROUGH */
                    699:                case (MSEC_7):
                    700:                        pp = mdoc_vol2a(VOL_URM);
                    701:                        break;
                    702:                case (MSEC_8):
                    703:                        pp = mdoc_vol2a(VOL_SMM);
                    704:                        break;
                    705:                case (MSEC_2):
                    706:                        /* FALLTHROUGH */
                    707:                case (MSEC_3):
                    708:                        /* FALLTHROUGH */
                    709:                case (MSEC_4):
                    710:                        /* FALLTHROUGH */
                    711:                case (MSEC_5):
                    712:                        pp = mdoc_vol2a(VOL_PRM);
                    713:                        break;
                    714:                case (MSEC_9):
                    715:                        pp = mdoc_vol2a(VOL_KM);
                    716:                        break;
                    717:                default:
                    718:                        break;
                    719:                }
1.18    ! kristaps  720:        }
        !           721:        vbuf[0] = 0;
        !           722:
        !           723:        if (pp) {
        !           724:                if (-1 == uname(&uts))
        !           725:                        err(1, "uname");
        !           726:                (void)strlcat(vbuf, uts.sysname, p->rmargin);
        !           727:                (void)strlcat(vbuf, " ", p->rmargin);
        !           728:        } else if (NULL == (pp = mdoc_msec2a(meta->msec)))
        !           729:                pp = mdoc_msec2a(MSEC_local);
        !           730:
        !           731:        (void)strlcat(vbuf, pp, p->rmargin);
1.3       kristaps  732:
1.16      kristaps  733:        /*
                    734:         * The header is strange.  It has three components, which are
                    735:         * really two with the first duplicated.  It goes like this:
                    736:         *
                    737:         * IDENTIFIER              TITLE                   IDENTIFIER
                    738:         *
                    739:         * The IDENTIFIER is NAME(SECTION), which is the command-name
                    740:         * (if given, or "unknown" if not) followed by the manual page
                    741:         * section.  These are given in `Dt'.  The TITLE is a free-form
                    742:         * string depending on the manual volume.  If not specified, it
                    743:         * switches on the manual section.
                    744:         */
                    745:
1.13      kristaps  746:        if (mdoc_arch2a(meta->arch))
1.16      kristaps  747:                (void)snprintf(buf, p->rmargin, "%s (%s)",
1.18    ! kristaps  748:                                vbuf, mdoc_arch2a(meta->arch));
1.13      kristaps  749:        else
1.18    ! kristaps  750:                (void)strlcpy(buf, vbuf, p->rmargin);
1.13      kristaps  751:
                    752:        pp = mdoc_msec2a(meta->msec);
                    753:
                    754:        (void)snprintf(title, p->rmargin, "%s(%s)",
                    755:                        meta->title, pp ? pp : "");
                    756:
1.16      kristaps  757:        for (bufp = title; *bufp; bufp++)
                    758:                *bufp = toupper(*bufp);
                    759:
1.13      kristaps  760:        p->offset = 0;
                    761:        p->rmargin = (p->maxrmargin - strlen(buf)) / 2;
1.15      kristaps  762:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.3       kristaps  763:
1.13      kristaps  764:        word(p, title);
                    765:        flushln(p);
1.3       kristaps  766:
1.15      kristaps  767:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1.13      kristaps  768:        p->offset = p->rmargin;
1.15      kristaps  769:        p->rmargin = p->maxrmargin - strlen(title);
1.3       kristaps  770:
1.13      kristaps  771:        word(p, buf);
                    772:        flushln(p);
1.3       kristaps  773:
1.13      kristaps  774:        p->offset = p->rmargin;
                    775:        p->rmargin = p->maxrmargin;
                    776:        p->flags &= ~TERMP_NOBREAK;
1.15      kristaps  777:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1.3       kristaps  778:
1.13      kristaps  779:        word(p, title);
                    780:        flushln(p);
1.3       kristaps  781:
1.13      kristaps  782:        p->rmargin = p->maxrmargin;
                    783:        p->offset = 0;
                    784:        p->flags &= ~TERMP_NOSPACE;
1.3       kristaps  785:
                    786:        free(title);
1.18    ! kristaps  787:        free(vbuf);
1.3       kristaps  788:        free(buf);
                    789: }

CVSweb