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

Annotation of mandoc/mdocterm.c, Revision 1.33

1.33    ! kristaps    1: /* $Id: mdocterm.c,v 1.32 2009/03/04 14:41:40 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.7       kristaps   28: #ifndef __OpenBSD__
1.5       kristaps   29: #include <time.h>
                     30: #endif
                     31:
1.2       kristaps   32: #include "mmain.h"
1.1       kristaps   33: #include "term.h"
                     34:
1.27      kristaps   35: struct termenc {
                     36:        const char       *enc;
                     37:        int               sym;
                     38: };
                     39:
1.3       kristaps   40: static void              body(struct termp *,
1.12      kristaps   41:                                struct termpair *,
1.3       kristaps   42:                                const struct mdoc_meta *,
                     43:                                const struct mdoc_node *);
                     44: static void              header(struct termp *,
                     45:                                const struct mdoc_meta *);
                     46: static void              footer(struct termp *,
                     47:                                const struct mdoc_meta *);
                     48:
                     49: static void              pword(struct termp *, const char *, size_t);
1.25      kristaps   50: static void              pescape(struct termp *, const char *,
                     51:                                size_t *, size_t);
                     52: static void              nescape(struct termp *,
1.12      kristaps   53:                                const char *, size_t);
1.3       kristaps   54: static void              chara(struct termp *, char);
1.25      kristaps   55: static void              stringa(struct termp *,
                     56:                                const char *, size_t);
                     57: static void              symbola(struct termp *, enum tsym);
1.26      kristaps   58: static void              stylea(struct termp *, enum tstyle);
1.3       kristaps   59:
                     60: #ifdef __linux__
                     61: extern size_t            strlcat(char *, const char *, size_t);
                     62: extern size_t            strlcpy(char *, const char *, size_t);
                     63: #endif
                     64:
1.27      kristaps   65: static struct termenc    termenc1[] = {
                     66:        { "\\",           TERMSYM_SLASH },
                     67:        { "\'",           TERMSYM_RSQUOTE },
                     68:        { "`",            TERMSYM_LSQUOTE },
                     69:        { "-",            TERMSYM_HYPHEN },
                     70:        { " ",            TERMSYM_SPACE },
                     71:        { ".",            TERMSYM_PERIOD },
                     72:        { "&",            TERMSYM_BREAK },
                     73:        { "e",            TERMSYM_SLASH },
                     74:        { "q",            TERMSYM_DQUOTE },
                     75:        { NULL,           0 }
                     76: };
                     77:
                     78: static struct termenc    termenc2[] = {
                     79:        { "rB",           TERMSYM_RBRACK },
                     80:        { "lB",           TERMSYM_LBRACK },
1.31      kristaps   81:        { "ra",           TERMSYM_RANGLE },
                     82:        { "la",           TERMSYM_LANGLE },
1.27      kristaps   83:        { "Lq",           TERMSYM_LDQUOTE },
                     84:        { "lq",           TERMSYM_LDQUOTE },
                     85:        { "Rq",           TERMSYM_RDQUOTE },
                     86:        { "rq",           TERMSYM_RDQUOTE },
                     87:        { "oq",           TERMSYM_LSQUOTE },
                     88:        { "aq",           TERMSYM_RSQUOTE },
                     89:
                     90:        { "<-",           TERMSYM_LARROW },
                     91:        { "->",           TERMSYM_RARROW },
                     92:        { "ua",           TERMSYM_UARROW },
                     93:        { "da",           TERMSYM_DARROW },
                     94:
                     95:        { "bu",           TERMSYM_BULLET },
                     96:        { "Ba",           TERMSYM_BAR },
                     97:        { "ba",           TERMSYM_BAR },
                     98:        { "co",           TERMSYM_COPY },
                     99:        { "Am",           TERMSYM_AMP },
                    100:
                    101:        { "Le",           TERMSYM_LE },
                    102:        { "<=",           TERMSYM_LE },
                    103:        { "Ge",           TERMSYM_GE },
1.31      kristaps  104:        { ">=",           TERMSYM_GE },
1.27      kristaps  105:        { "==",           TERMSYM_EQ },
                    106:        { "Ne",           TERMSYM_NEQ },
                    107:        { "!=",           TERMSYM_NEQ },
                    108:        { "Pm",           TERMSYM_PLUSMINUS },
                    109:        { "+-",           TERMSYM_PLUSMINUS },
                    110:        { "If",           TERMSYM_INF2 },
                    111:        { "if",           TERMSYM_INF },
                    112:        { "Na",           TERMSYM_NAN },
                    113:        { "na",           TERMSYM_NAN },
                    114:        { "**",           TERMSYM_ASTERISK },
                    115:        { "Gt",           TERMSYM_GT },
                    116:        { "Lt",           TERMSYM_LT },
                    117:
                    118:        { "aa",           TERMSYM_ACUTE },
                    119:        { "ga",           TERMSYM_GRAVE },
                    120:
                    121:        { "en",           TERMSYM_EN },
                    122:        { "em",           TERMSYM_EM },
                    123:
                    124:        { "Pi",           TERMSYM_PI },
                    125:        { NULL,           0 }
                    126: };
                    127:
1.25      kristaps  128: static struct termsym    termsym_ansi[] = {
                    129:        { "]", 1 },             /* TERMSYM_RBRACK */
                    130:        { "[", 1 },             /* TERMSYM_LBRACK */
                    131:        { "<-", 2 },            /* TERMSYM_LARROW */
                    132:        { "->", 2 },            /* TERMSYM_RARROW */
                    133:        { "^", 1 },             /* TERMSYM_UARROW */
                    134:        { "v", 1 },             /* TERMSYM_DARROW */
                    135:        { "`", 1 },             /* TERMSYM_LSQUOTE */
                    136:        { "\'", 1 },            /* TERMSYM_RSQUOTE */
                    137:        { "\'", 1 },            /* TERMSYM_SQUOTE */
                    138:        { "``", 2 },            /* TERMSYM_LDQUOTE */
                    139:        { "\'\'", 2 },          /* TERMSYM_RDQUOTE */
                    140:        { "\"", 1 },            /* TERMSYM_DQUOTE */
                    141:        { "<", 1 },             /* TERMSYM_LT */
                    142:        { ">", 1 },             /* TERMSYM_GT */
                    143:        { "<=", 2 },            /* TERMSYM_LE */
                    144:        { ">=", 2 },            /* TERMSYM_GE */
                    145:        { "==", 2 },            /* TERMSYM_EQ */
                    146:        { "!=", 2 },            /* TERMSYM_NEQ */
                    147:        { "\'", 1 },            /* TERMSYM_ACUTE */
                    148:        { "`", 1 },             /* TERMSYM_GRAVE */
                    149:        { "pi", 2 },            /* TERMSYM_PI */
                    150:        { "+=", 2 },            /* TERMSYM_PLUSMINUS */
                    151:        { "oo", 2 },            /* TERMSYM_INF */
                    152:        { "infinity", 8 },      /* TERMSYM_INF2 */
                    153:        { "NaN", 3 },           /* TERMSYM_NAN */
                    154:        { "|", 1 },             /* TERMSYM_BAR */
                    155:        { "o", 1 },             /* TERMSYM_BULLET */
1.27      kristaps  156:        { "&", 1 },             /* TERMSYM_AMP */
                    157:        { "--", 2 },            /* TERMSYM_EM */
                    158:        { "-", 1 },             /* TERMSYM_EN */
                    159:        { "(C)", 3 },           /* TERMSYM_COPY */
                    160:        { "*", 1 },             /* TERMSYM_ASTERISK */
                    161:        { "\\", 1 },            /* TERMSYM_SLASH */
                    162:        { "-", 1 },             /* TERMSYM_HYPHEN */
                    163:        { " ", 1 },             /* TERMSYM_SPACE */
                    164:        { ".", 1 },             /* TERMSYM_PERIOD */
                    165:        { "", 0 },              /* TERMSYM_BREAK */
1.31      kristaps  166:        { "<", 1 },             /* TERMSYM_LANGLE */
                    167:        { ">", 1 },             /* TERMSYM_RANGLE */
1.25      kristaps  168: };
                    169:
                    170: static const char        ansi_clear[]  = { 27, '[', '0', 'm' };
                    171: static const char        ansi_bold[]  = { 27, '[', '1', 'm' };
                    172: static const char        ansi_under[]  = { 27, '[', '4', 'm' };
                    173:
                    174: static struct termsym    termstyle_ansi[] = {
                    175:        { ansi_clear, 4 },
                    176:        { ansi_bold, 4 },
                    177:        { ansi_under, 4 }
                    178: };
                    179:
1.3       kristaps  180:
1.1       kristaps  181: int
                    182: main(int argc, char *argv[])
                    183: {
1.2       kristaps  184:        struct mmain    *p;
                    185:        const struct mdoc *mdoc;
1.3       kristaps  186:        struct termp     termp;
1.2       kristaps  187:
                    188:        p = mmain_alloc();
1.1       kristaps  189:
1.3       kristaps  190:        if ( ! mmain_getopt(p, argc, argv, NULL, NULL, NULL, NULL))
1.2       kristaps  191:                mmain_exit(p, 1);
1.1       kristaps  192:
1.3       kristaps  193:        if (NULL == (mdoc = mmain_mdoc(p)))
                    194:                mmain_exit(p, 1);
                    195:
1.29      kristaps  196:        termp.maxrmargin = termp.rmargin = 78; /* XXX */
1.3       kristaps  197:        termp.maxcols = 1024;
                    198:        termp.offset = termp.col = 0;
                    199:        termp.flags = TERMP_NOSPACE;
1.25      kristaps  200:        termp.symtab = termsym_ansi;
                    201:        termp.styletab = termstyle_ansi;
1.3       kristaps  202:
                    203:        if (NULL == (termp.buf = malloc(termp.maxcols)))
                    204:                err(1, "malloc");
                    205:
1.15      kristaps  206:        header(&termp, mdoc_meta(mdoc));
1.12      kristaps  207:        body(&termp, NULL, mdoc_meta(mdoc), mdoc_node(mdoc));
1.3       kristaps  208:        footer(&termp, mdoc_meta(mdoc));
                    209:
                    210:        free(termp.buf);
                    211:
                    212:        mmain_exit(p, 0);
                    213:        /* NOTREACHED */
                    214: }
                    215:
                    216:
1.25      kristaps  217: /*
                    218:  * Flush a line of text.  A "line" is loosely defined as being something
                    219:  * that should be followed by a newline, regardless of whether it's
                    220:  * broken apart by newlines getting there.  A line can also be a
                    221:  * fragment of a columnar list.
                    222:  *
                    223:  * Specifically, a line is whatever's in p->buf of length p->col, which
                    224:  * is zeroed after this function returns.
                    225:  *
                    226:  * The variables TERMP_NOLPAD, TERMP_LITERAL and TERMP_NOBREAK are of
                    227:  * critical importance here.  Their behaviour follows:
                    228:  *
                    229:  *  - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
                    230:  *    offset value.  This is useful when doing columnar lists where the
                    231:  *    prior column has right-padded.
                    232:  *
                    233:  *  - TERMP_NOBREAK: this is the most important and is used when making
                    234:  *    columns.  In short: don't print a newline and instead pad to the
                    235:  *    right margin.  Used in conjunction with TERMP_NOLPAD.
                    236:  *
                    237:  *  In-line line breaking:
                    238:  *
                    239:  *  If TERMP_NOBREAK is specified and the line overruns the right
                    240:  *  margin, it will break and pad-right to the right margin after
                    241:  *  writing.  If maxrmargin is violated, it will break and continue
                    242:  *  writing from the right-margin, which will lead to the above
                    243:  *  scenario upon exit.
                    244:  *
                    245:  *  Otherwise, the line will break at the right margin.  Extremely long
                    246:  *  lines will cause the system to emit a warning (TODO: hyphenate, if
                    247:  *  possible).
                    248:  */
1.3       kristaps  249: void
                    250: flushln(struct termp *p)
                    251: {
1.25      kristaps  252:        size_t           i, j, vsz, vis, maxvis, mmax, bp;
1.3       kristaps  253:
                    254:        /*
                    255:         * First, establish the maximum columns of "visible" content.
                    256:         * This is usually the difference between the right-margin and
                    257:         * an indentation, but can be, for tagged lists or columns, a
                    258:         * small set of values.
                    259:         */
                    260:
                    261:        assert(p->offset < p->rmargin);
                    262:        maxvis = p->rmargin - p->offset;
1.25      kristaps  263:        mmax = p->maxrmargin - p->offset;
                    264:        bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
1.3       kristaps  265:        vis = 0;
                    266:
                    267:        /*
                    268:         * If in the standard case (left-justified), then begin with our
                    269:         * indentation, otherwise (columns, etc.) just start spitting
                    270:         * out text.
                    271:         */
                    272:
                    273:        if ( ! (p->flags & TERMP_NOLPAD))
                    274:                /* LINTED */
                    275:                for (j = 0; j < p->offset; j++)
                    276:                        putchar(' ');
                    277:
                    278:        for (i = 0; i < p->col; i++) {
                    279:                /*
                    280:                 * Count up visible word characters.  Control sequences
1.23      kristaps  281:                 * (starting with the CSI) aren't counted.  A space
                    282:                 * generates a non-printing word, which is valid (the
                    283:                 * space is printed according to regular spacing rules).
1.3       kristaps  284:                 */
                    285:
1.25      kristaps  286:                /* FIXME: make non-ANSI friendly. */
                    287:
1.3       kristaps  288:                /* LINTED */
                    289:                for (j = i, vsz = 0; j < p->col; j++) {
1.33    ! kristaps  290:                        if (isspace((u_char)p->buf[j]))
1.3       kristaps  291:                                break;
                    292:                        else if (27 == p->buf[j]) {
                    293:                                assert(j + 4 <= p->col);
                    294:                                j += 3;
                    295:                        } else
                    296:                                vsz++;
                    297:                }
                    298:
                    299:                /*
1.25      kristaps  300:                 * Do line-breaking.  If we're greater than our
                    301:                 * break-point and already in-line, break to the next
                    302:                 * line and start writing.  If we're at the line start,
                    303:                 * then write out the word (TODO: hyphenate) and break
                    304:                 * in a subsequent loop invocation.
1.3       kristaps  305:                 */
                    306:
1.22      kristaps  307:                if ( ! (TERMP_NOBREAK & p->flags)) {
1.25      kristaps  308:                        if (vis && vis + vsz > bp) {
1.22      kristaps  309:                                putchar('\n');
                    310:                                for (j = 0; j < p->offset; j++)
                    311:                                        putchar(' ');
                    312:                                vis = 0;
1.25      kristaps  313:                        } else if (vis + vsz > bp)
                    314:                                warnx("word breaks right margin");
                    315:
                    316:                        /* TODO: hyphenate. */
                    317:
                    318:                } else {
                    319:                        if (vis && vis + vsz > bp) {
                    320:                                putchar('\n');
                    321:                                for (j = 0; j < p->rmargin; j++)
                    322:                                        putchar(' ');
1.30      kristaps  323:                                vis = p->rmargin - p->offset;
1.25      kristaps  324:                        } else if (vis + vsz > bp)
                    325:                                warnx("word breaks right margin");
                    326:
                    327:                        /* TODO: hyphenate. */
1.24      kristaps  328:                }
1.3       kristaps  329:
                    330:                /*
                    331:                 * Write out the word and a trailing space.  Omit the
1.25      kristaps  332:                 * space if we're the last word in the line or beyond
                    333:                 * our breakpoint.
1.3       kristaps  334:                 */
                    335:
                    336:                for ( ; i < p->col; i++) {
1.33    ! kristaps  337:                        if (isspace((u_char)p->buf[i]))
1.3       kristaps  338:                                break;
                    339:                        putchar(p->buf[i]);
                    340:                }
                    341:                vis += vsz;
1.25      kristaps  342:                if (i < p->col && vis <= bp) {
1.3       kristaps  343:                        putchar(' ');
                    344:                        vis++;
                    345:                }
                    346:        }
                    347:
1.25      kristaps  348:        /*
                    349:         * If we've overstepped our maximum visible no-break space, then
                    350:         * cause a newline and offset at the right margin.
                    351:         */
                    352:
1.22      kristaps  353:        if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) {
1.32      kristaps  354:                if ( ! (TERMP_NONOBREAK & p->flags)) {
                    355:                        putchar('\n');
                    356:                        for (i = 0; i < p->rmargin; i++)
                    357:                                putchar(' ');
                    358:                }
1.22      kristaps  359:                p->col = 0;
                    360:                return;
                    361:        }
                    362:
1.3       kristaps  363:        /*
                    364:         * If we're not to right-marginalise it (newline), then instead
                    365:         * pad to the right margin and stay off.
                    366:         */
                    367:
1.32      kristaps  368:        if (p->flags & TERMP_NOBREAK) {
                    369:                if ( ! (TERMP_NONOBREAK & p->flags))
                    370:                        for ( ; vis < maxvis; vis++)
                    371:                                putchar(' ');
                    372:        } else
1.3       kristaps  373:                putchar('\n');
                    374:
                    375:        p->col = 0;
                    376: }
                    377:
                    378:
1.25      kristaps  379: /*
                    380:  * A newline only breaks an existing line; it won't assert vertical
                    381:  * space.  All data in the output buffer is flushed prior to the newline
                    382:  * assertion.
                    383:  */
1.3       kristaps  384: void
                    385: newln(struct termp *p)
                    386: {
1.1       kristaps  387:
1.3       kristaps  388:        p->flags |= TERMP_NOSPACE;
1.12      kristaps  389:        if (0 == p->col) {
                    390:                p->flags &= ~TERMP_NOLPAD;
1.3       kristaps  391:                return;
1.12      kristaps  392:        }
1.3       kristaps  393:        flushln(p);
1.11      kristaps  394:        p->flags &= ~TERMP_NOLPAD;
1.3       kristaps  395: }
                    396:
                    397:
1.25      kristaps  398: /*
                    399:  * Asserts a vertical space (a full, empty line-break between lines).
                    400:  * Note that if used twice, this will cause two blank spaces and so on.
                    401:  * All data in the output buffer is flushed prior to the newline
                    402:  * assertion.
                    403:  */
1.3       kristaps  404: void
                    405: vspace(struct termp *p)
                    406: {
                    407:
                    408:        newln(p);
                    409:        putchar('\n');
                    410: }
                    411:
                    412:
1.25      kristaps  413: /*
                    414:  * Break apart a word into "pwords" (partial-words, usually from
                    415:  * breaking up a phrase into individual words) and, eventually, put them
                    416:  * into the output buffer.  If we're a literal word, then don't break up
                    417:  * the word and put it verbatim into the output buffer.
                    418:  */
1.3       kristaps  419: void
                    420: word(struct termp *p, const char *word)
                    421: {
                    422:        size_t           i, j, len;
                    423:
                    424:        if (p->flags & TERMP_LITERAL) {
                    425:                pword(p, word, strlen(word));
                    426:                return;
                    427:        }
                    428:
                    429:        len = strlen(word);
                    430:        assert(len > 0);
                    431:
                    432:        if (mdoc_isdelim(word)) {
                    433:                if ( ! (p->flags & TERMP_IGNDELIM))
                    434:                        p->flags |= TERMP_NOSPACE;
                    435:                p->flags &= ~TERMP_IGNDELIM;
                    436:        }
                    437:
                    438:        /* LINTED */
                    439:        for (j = i = 0; i < len; i++) {
1.33    ! kristaps  440:                if ( ! isspace((u_char)word[i])) {
1.3       kristaps  441:                        j++;
                    442:                        continue;
1.20      kristaps  443:                }
                    444:
                    445:                /* Escaped spaces don't delimit... */
1.33    ! kristaps  446:                if (i > 0 && isspace((u_char)word[i]) &&
1.25      kristaps  447:                                '\\' == word[i - 1]) {
1.20      kristaps  448:                        j++;
                    449:                        continue;
1.1       kristaps  450:                }
1.20      kristaps  451:
1.3       kristaps  452:                if (0 == j)
                    453:                        continue;
                    454:                assert(i >= j);
                    455:                pword(p, &word[i - j], j);
                    456:                j = 0;
                    457:        }
                    458:        if (j > 0) {
                    459:                assert(i >= j);
                    460:                pword(p, &word[i - j], j);
                    461:        }
                    462: }
                    463:
                    464:
1.25      kristaps  465: /*
                    466:  * This is the main function for printing out nodes.  It's constituted
                    467:  * of PRE and POST functions, which correspond to prefix and infix
                    468:  * processing.  The termpair structure allows data to persist between
                    469:  * prefix and postfix invocations.
                    470:  */
1.3       kristaps  471: static void
1.12      kristaps  472: body(struct termp *p, struct termpair *ppair,
                    473:                const struct mdoc_meta *meta,
1.3       kristaps  474:                const struct mdoc_node *node)
                    475: {
                    476:        int              dochild;
1.9       kristaps  477:        struct termpair  pair;
1.3       kristaps  478:
                    479:        /* Pre-processing. */
                    480:
                    481:        dochild = 1;
1.12      kristaps  482:        pair.ppair = ppair;
1.9       kristaps  483:        pair.type = 0;
1.11      kristaps  484:        pair.offset = pair.rmargin = 0;
1.10      kristaps  485:        pair.flag = 0;
1.12      kristaps  486:        pair.count = 0;
1.3       kristaps  487:
                    488:        if (MDOC_TEXT != node->type) {
                    489:                if (termacts[node->tok].pre)
1.9       kristaps  490:                        if ( ! (*termacts[node->tok].pre)(p, &pair, meta, node))
1.3       kristaps  491:                                dochild = 0;
                    492:        } else /* MDOC_TEXT == node->type */
                    493:                word(p, node->data.text.string);
                    494:
                    495:        /* Children. */
                    496:
1.10      kristaps  497:        if (TERMPAIR_FLAG & pair.type)
                    498:                p->flags |= pair.flag;
1.9       kristaps  499:
1.3       kristaps  500:        if (dochild && node->child)
1.12      kristaps  501:                body(p, &pair, meta, node->child);
1.3       kristaps  502:
1.10      kristaps  503:        if (TERMPAIR_FLAG & pair.type)
                    504:                p->flags &= ~pair.flag;
1.9       kristaps  505:
1.3       kristaps  506:        /* Post-processing. */
                    507:
                    508:        if (MDOC_TEXT != node->type)
                    509:                if (termacts[node->tok].post)
1.9       kristaps  510:                        (*termacts[node->tok].post)(p, &pair, meta, node);
1.3       kristaps  511:
                    512:        /* Siblings. */
1.1       kristaps  513:
1.3       kristaps  514:        if (node->next)
1.12      kristaps  515:                body(p, ppair, meta, node->next);
1.3       kristaps  516: }
                    517:
                    518:
                    519: static void
                    520: footer(struct termp *p, const struct mdoc_meta *meta)
                    521: {
                    522:        struct tm       *tm;
                    523:        char            *buf, *os;
                    524:
                    525:        if (NULL == (buf = malloc(p->rmargin)))
                    526:                err(1, "malloc");
                    527:        if (NULL == (os = malloc(p->rmargin)))
                    528:                err(1, "malloc");
                    529:
                    530:        tm = localtime(&meta->date);
                    531:
1.7       kristaps  532: #ifdef __OpenBSD__
                    533:        if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                    534: #else
1.3       kristaps  535:        if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                    536: #endif
                    537:                err(1, "strftime");
                    538:
1.15      kristaps  539:        (void)strlcpy(os, meta->os, p->rmargin);
1.3       kristaps  540:
1.16      kristaps  541:        /*
                    542:         * This is /slightly/ different from regular groff output
                    543:         * because we don't have page numbers.  Print the following:
                    544:         *
                    545:         * OS                                            MDOCDATE
                    546:         */
                    547:
1.15      kristaps  548:        vspace(p);
1.3       kristaps  549:
1.15      kristaps  550:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
                    551:        p->rmargin = p->maxrmargin - strlen(buf);
                    552:        p->offset = 0;
1.3       kristaps  553:
1.15      kristaps  554:        word(p, os);
                    555:        flushln(p);
1.3       kristaps  556:
1.15      kristaps  557:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    558:        p->offset = p->rmargin;
                    559:        p->rmargin = p->maxrmargin;
                    560:        p->flags &= ~TERMP_NOBREAK;
                    561:
                    562:        word(p, buf);
                    563:        flushln(p);
1.1       kristaps  564:
1.3       kristaps  565:        free(buf);
                    566:        free(os);
1.1       kristaps  567: }
                    568:
                    569:
1.3       kristaps  570: static void
                    571: header(struct termp *p, const struct mdoc_meta *meta)
                    572: {
1.18      kristaps  573:        char            *buf, *title, *bufp, *vbuf;
1.13      kristaps  574:        const char      *pp;
1.18      kristaps  575:        struct utsname   uts;
                    576:
                    577:        p->rmargin = p->maxrmargin;
                    578:        p->offset = 0;
1.3       kristaps  579:
                    580:        if (NULL == (buf = malloc(p->rmargin)))
                    581:                err(1, "malloc");
                    582:        if (NULL == (title = malloc(p->rmargin)))
                    583:                err(1, "malloc");
1.18      kristaps  584:        if (NULL == (vbuf = malloc(p->rmargin)))
                    585:                err(1, "malloc");
1.3       kristaps  586:
1.18      kristaps  587:        if (NULL == (pp = mdoc_vol2a(meta->vol))) {
1.3       kristaps  588:                switch (meta->msec) {
                    589:                case (MSEC_1):
                    590:                        /* FALLTHROUGH */
                    591:                case (MSEC_6):
                    592:                        /* FALLTHROUGH */
                    593:                case (MSEC_7):
                    594:                        pp = mdoc_vol2a(VOL_URM);
                    595:                        break;
                    596:                case (MSEC_8):
                    597:                        pp = mdoc_vol2a(VOL_SMM);
                    598:                        break;
                    599:                case (MSEC_2):
                    600:                        /* FALLTHROUGH */
                    601:                case (MSEC_3):
                    602:                        /* FALLTHROUGH */
                    603:                case (MSEC_4):
                    604:                        /* FALLTHROUGH */
                    605:                case (MSEC_5):
                    606:                        pp = mdoc_vol2a(VOL_PRM);
                    607:                        break;
                    608:                case (MSEC_9):
                    609:                        pp = mdoc_vol2a(VOL_KM);
                    610:                        break;
                    611:                default:
                    612:                        break;
                    613:                }
1.18      kristaps  614:        }
                    615:        vbuf[0] = 0;
                    616:
                    617:        if (pp) {
                    618:                if (-1 == uname(&uts))
                    619:                        err(1, "uname");
                    620:                (void)strlcat(vbuf, uts.sysname, p->rmargin);
                    621:                (void)strlcat(vbuf, " ", p->rmargin);
                    622:        } else if (NULL == (pp = mdoc_msec2a(meta->msec)))
                    623:                pp = mdoc_msec2a(MSEC_local);
                    624:
                    625:        (void)strlcat(vbuf, pp, p->rmargin);
1.3       kristaps  626:
1.16      kristaps  627:        /*
                    628:         * The header is strange.  It has three components, which are
                    629:         * really two with the first duplicated.  It goes like this:
                    630:         *
                    631:         * IDENTIFIER              TITLE                   IDENTIFIER
                    632:         *
                    633:         * The IDENTIFIER is NAME(SECTION), which is the command-name
                    634:         * (if given, or "unknown" if not) followed by the manual page
                    635:         * section.  These are given in `Dt'.  The TITLE is a free-form
                    636:         * string depending on the manual volume.  If not specified, it
                    637:         * switches on the manual section.
                    638:         */
                    639:
1.13      kristaps  640:        if (mdoc_arch2a(meta->arch))
1.16      kristaps  641:                (void)snprintf(buf, p->rmargin, "%s (%s)",
1.18      kristaps  642:                                vbuf, mdoc_arch2a(meta->arch));
1.13      kristaps  643:        else
1.18      kristaps  644:                (void)strlcpy(buf, vbuf, p->rmargin);
1.13      kristaps  645:
                    646:        pp = mdoc_msec2a(meta->msec);
                    647:
                    648:        (void)snprintf(title, p->rmargin, "%s(%s)",
                    649:                        meta->title, pp ? pp : "");
                    650:
1.16      kristaps  651:        for (bufp = title; *bufp; bufp++)
1.33    ! kristaps  652:                *bufp = toupper((u_char)*bufp);
1.16      kristaps  653:
1.13      kristaps  654:        p->offset = 0;
                    655:        p->rmargin = (p->maxrmargin - strlen(buf)) / 2;
1.15      kristaps  656:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.3       kristaps  657:
1.13      kristaps  658:        word(p, title);
                    659:        flushln(p);
1.3       kristaps  660:
1.15      kristaps  661:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1.13      kristaps  662:        p->offset = p->rmargin;
1.15      kristaps  663:        p->rmargin = p->maxrmargin - strlen(title);
1.3       kristaps  664:
1.13      kristaps  665:        word(p, buf);
                    666:        flushln(p);
1.3       kristaps  667:
1.13      kristaps  668:        p->offset = p->rmargin;
                    669:        p->rmargin = p->maxrmargin;
                    670:        p->flags &= ~TERMP_NOBREAK;
1.15      kristaps  671:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1.3       kristaps  672:
1.13      kristaps  673:        word(p, title);
                    674:        flushln(p);
1.3       kristaps  675:
1.13      kristaps  676:        p->rmargin = p->maxrmargin;
                    677:        p->offset = 0;
                    678:        p->flags &= ~TERMP_NOSPACE;
1.3       kristaps  679:
                    680:        free(title);
1.18      kristaps  681:        free(vbuf);
1.3       kristaps  682:        free(buf);
                    683: }
1.25      kristaps  684:
                    685:
                    686: /*
                    687:  * Determine the symbol indicated by an escape sequences, that is, one
                    688:  * starting with a backslash.  Once done, we pass this value into the
                    689:  * output buffer by way of the symbol table.
                    690:  */
                    691: static void
                    692: nescape(struct termp *p, const char *word, size_t len)
                    693: {
1.27      kristaps  694:        struct termenc  *enc;
1.25      kristaps  695:
                    696:        switch (len) {
                    697:        case (1):
1.27      kristaps  698:                enc = termenc1;
1.25      kristaps  699:                break;
                    700:        case (2):
1.27      kristaps  701:                enc = termenc2;
1.25      kristaps  702:                break;
                    703:        default:
1.27      kristaps  704:                warnx("unsupported %zu-byte escape sequence", len);
                    705:                return;
1.25      kristaps  706:        }
1.27      kristaps  707:
                    708:        for ( ; enc->enc; enc++)
                    709:                if (0 == memcmp(enc->enc, word, len)) {
                    710:                        symbola(p, enc->sym);
                    711:                        return;
                    712:                }
                    713:
                    714:        warnx("unsupported %zu-byte escape sequence", len);
1.25      kristaps  715: }
                    716:
                    717:
                    718: /*
                    719:  * Handle an escape sequence: determine its length and pass it to the
                    720:  * escape-symbol look table.  Note that we assume mdoc(3) has validated
                    721:  * the escape sequence (we assert upon badly-formed escape sequences).
                    722:  */
                    723: static void
                    724: pescape(struct termp *p, const char *word, size_t *i, size_t len)
                    725: {
                    726:        size_t           j;
                    727:
                    728:        (*i)++;
                    729:        assert(*i < len);
                    730:
                    731:        if ('(' == word[*i]) {
                    732:                (*i)++;
                    733:                assert(*i + 1 < len);
                    734:                nescape(p, &word[*i], 2);
                    735:                (*i)++;
                    736:                return;
                    737:
                    738:        } else if ('*' == word[*i]) {
                    739:                /* XXX - deprecated! */
                    740:                (*i)++;
                    741:                assert(*i < len);
                    742:                switch (word[*i]) {
                    743:                case ('('):
                    744:                        (*i)++;
                    745:                        assert(*i + 1 < len);
                    746:                        nescape(p, &word[*i], 2);
                    747:                        (*i)++;
                    748:                        return;
                    749:                case ('['):
                    750:                        break;
                    751:                default:
                    752:                        nescape(p, &word[*i], 1);
                    753:                        return;
                    754:                }
                    755:
                    756:        } else if ('[' != word[*i]) {
                    757:                nescape(p, &word[*i], 1);
                    758:                return;
                    759:        }
                    760:
                    761:        (*i)++;
                    762:        for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
                    763:                /* Loop... */ ;
                    764:
                    765:        assert(word[*i]);
                    766:        nescape(p, &word[*i - j], j);
                    767: }
                    768:
                    769:
                    770: /*
                    771:  * Handle pwords, partial words, which may be either a single word or a
                    772:  * phrase that cannot be broken down (such as a literal string).  This
                    773:  * handles word styling.
                    774:  */
                    775: static void
                    776: pword(struct termp *p, const char *word, size_t len)
                    777: {
                    778:        size_t           i;
                    779:
                    780:        if ( ! (TERMP_NOSPACE & p->flags) &&
                    781:                        ! (TERMP_LITERAL & p->flags))
                    782:                chara(p, ' ');
                    783:
                    784:        if ( ! (p->flags & TERMP_NONOSPACE))
                    785:                p->flags &= ~TERMP_NOSPACE;
                    786:
                    787:        /*
                    788:         * XXX - if literal and underlining, this will underline the
                    789:         * spaces between literal words.
                    790:         */
                    791:
                    792:        if (p->flags & TERMP_BOLD)
1.26      kristaps  793:                stylea(p, TERMSTYLE_BOLD);
1.25      kristaps  794:        if (p->flags & TERMP_UNDERLINE)
1.26      kristaps  795:                stylea(p, TERMSTYLE_UNDER);
1.25      kristaps  796:
                    797:        for (i = 0; i < len; i++) {
                    798:                if ('\\' == word[i]) {
                    799:                        pescape(p, word, &i, len);
                    800:                        continue;
                    801:                }
                    802:                chara(p, word[i]);
                    803:        }
                    804:
                    805:        if (p->flags & TERMP_BOLD ||
                    806:                        p->flags & TERMP_UNDERLINE)
1.26      kristaps  807:                stylea(p, TERMSTYLE_CLEAR);
1.25      kristaps  808: }
                    809:
                    810:
                    811: /*
                    812:  * Add a symbol to the output line buffer.
                    813:  */
                    814: static void
                    815: symbola(struct termp *p, enum tsym sym)
                    816: {
                    817:
                    818:        assert(p->symtab[sym].sym);
                    819:        stringa(p, p->symtab[sym].sym, p->symtab[sym].sz);
                    820: }
                    821:
                    822:
                    823: /*
1.26      kristaps  824:  * Add a style to the output line buffer.
                    825:  */
                    826: static void
                    827: stylea(struct termp *p, enum tstyle style)
                    828: {
                    829:
                    830:        assert(p->styletab[style].sym);
                    831:        stringa(p, p->styletab[style].sym, p->styletab[style].sz);
                    832: }
                    833:
                    834:
                    835: /*
1.25      kristaps  836:  * Like chara() but for arbitrary-length buffers.  Resize the buffer by
                    837:  * a factor of two (if the buffer is less than that) or the buffer's
                    838:  * size.
                    839:  */
                    840: static void
                    841: stringa(struct termp *p, const char *c, size_t sz)
                    842: {
                    843:        size_t           s;
                    844:
1.27      kristaps  845:        if (0 == sz)
                    846:                return;
                    847:
1.25      kristaps  848:        s = sz > p->maxcols * 2 ? sz : p->maxcols * 2;
                    849:
                    850:        assert(c);
                    851:        if (p->col + sz >= p->maxcols) {
                    852:                p->buf = realloc(p->buf, s);
                    853:                if (NULL == p->buf)
                    854:                        err(1, "realloc");
                    855:                p->maxcols = s;
                    856:        }
                    857:
                    858:        (void)memcpy(&p->buf[p->col], c, sz);
                    859:        p->col += sz;
                    860: }
                    861:
                    862:
                    863: /*
                    864:  * Insert a single character into the line-buffer.  If the buffer's
                    865:  * space is exceeded, then allocate more space by doubling the buffer
                    866:  * size.
                    867:  */
                    868: static void
                    869: chara(struct termp *p, char c)
                    870: {
                    871:
                    872:        if (p->col + 1 >= p->maxcols) {
                    873:                p->buf = realloc(p->buf, p->maxcols * 2);
                    874:                if (NULL == p->buf)
                    875:                        err(1, "malloc");
                    876:                p->maxcols *= 2;
                    877:        }
                    878:        p->buf[(p->col)++] = c;
                    879: }

CVSweb