[BACK]Return to html.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/html.c, Revision 1.12

1.12    ! schwarze    1: /*     $Id: html.c,v 1.11 2010/06/27 20:28:56 schwarze Exp $ */
1.1       schwarze    2: /*
1.12    ! schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       schwarze    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 above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #include <sys/types.h>
                     18:
                     19: #include <assert.h>
1.3       schwarze   20: #include <ctype.h>
1.4       schwarze   21: #include <stdarg.h>
1.1       schwarze   22: #include <stdio.h>
                     23: #include <stdint.h>
                     24: #include <stdlib.h>
                     25: #include <string.h>
                     26: #include <unistd.h>
                     27:
1.9       schwarze   28: #include "mandoc.h"
1.1       schwarze   29: #include "out.h"
                     30: #include "chars.h"
                     31: #include "html.h"
                     32: #include "main.h"
                     33:
                     34: struct htmldata {
                     35:        const char       *name;
                     36:        int               flags;
                     37: #define        HTML_CLRLINE     (1 << 0)
                     38: #define        HTML_NOSTACK     (1 << 1)
1.6       schwarze   39: #define        HTML_AUTOCLOSE   (1 << 2) /* Tag has auto-closure. */
1.1       schwarze   40: };
                     41:
                     42: static const struct htmldata htmltags[TAG_MAX] = {
                     43:        {"html",        HTML_CLRLINE}, /* TAG_HTML */
                     44:        {"head",        HTML_CLRLINE}, /* TAG_HEAD */
                     45:        {"body",        HTML_CLRLINE}, /* TAG_BODY */
1.6       schwarze   46:        {"meta",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.1       schwarze   47:        {"title",       HTML_CLRLINE}, /* TAG_TITLE */
                     48:        {"div",         HTML_CLRLINE}, /* TAG_DIV */
                     49:        {"h1",          0}, /* TAG_H1 */
                     50:        {"h2",          0}, /* TAG_H2 */
                     51:        {"span",        0}, /* TAG_SPAN */
1.8       schwarze   52:        {"link",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.6       schwarze   53:        {"br",          HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.1       schwarze   54:        {"a",           0}, /* TAG_A */
                     55:        {"table",       HTML_CLRLINE}, /* TAG_TABLE */
1.6       schwarze   56:        {"col",         HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.1       schwarze   57:        {"tr",          HTML_CLRLINE}, /* TAG_TR */
                     58:        {"td",          HTML_CLRLINE}, /* TAG_TD */
                     59:        {"li",          HTML_CLRLINE}, /* TAG_LI */
                     60:        {"ul",          HTML_CLRLINE}, /* TAG_UL */
                     61:        {"ol",          HTML_CLRLINE}, /* TAG_OL */
                     62: };
                     63:
1.5       schwarze   64: static const char      *const htmlfonts[HTMLFONT_MAX] = {
                     65:        "roman",
                     66:        "bold",
                     67:        "italic"
                     68: };
                     69:
                     70: static const char      *const htmlattrs[ATTR_MAX] = {
1.1       schwarze   71:        "http-equiv",
                     72:        "content",
                     73:        "name",
                     74:        "rel",
                     75:        "href",
                     76:        "type",
                     77:        "media",
                     78:        "class",
                     79:        "style",
                     80:        "width",
                     81:        "valign",
                     82:        "target",
                     83:        "id",
1.3       schwarze   84:        "summary",
1.1       schwarze   85: };
                     86:
1.5       schwarze   87: static void              print_spec(struct html *, const char *, size_t);
                     88: static void              print_res(struct html *, const char *, size_t);
                     89: static void              print_ctag(struct html *, enum htmltag);
1.6       schwarze   90: static void              print_doctype(struct html *);
                     91: static void              print_xmltype(struct html *);
1.5       schwarze   92: static int               print_encode(struct html *, const char *, int);
                     93: static void              print_metaf(struct html *, enum roffdeco);
1.6       schwarze   94: static void              print_attr(struct html *,
                     95:                                const char *, const char *);
                     96: static void             *ml_alloc(char *, enum htmltype);
1.5       schwarze   97:
                     98:
1.6       schwarze   99: static void *
                    100: ml_alloc(char *outopts, enum htmltype type)
1.1       schwarze  101: {
                    102:        struct html     *h;
                    103:        const char      *toks[4];
                    104:        char            *v;
                    105:
                    106:        toks[0] = "style";
                    107:        toks[1] = "man";
                    108:        toks[2] = "includes";
                    109:        toks[3] = NULL;
                    110:
1.3       schwarze  111:        h = calloc(1, sizeof(struct html));
                    112:        if (NULL == h) {
                    113:                perror(NULL);
                    114:                exit(EXIT_FAILURE);
                    115:        }
1.1       schwarze  116:
1.6       schwarze  117:        h->type = type;
1.2       schwarze  118:        h->tags.head = NULL;
                    119:        h->ords.head = NULL;
1.3       schwarze  120:        h->symtab = chars_init(CHARS_HTML);
1.1       schwarze  121:
                    122:        while (outopts && *outopts)
                    123:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    124:                case (0):
                    125:                        h->style = v;
                    126:                        break;
                    127:                case (1):
                    128:                        h->base_man = v;
                    129:                        break;
                    130:                case (2):
                    131:                        h->base_includes = v;
                    132:                        break;
                    133:                default:
                    134:                        break;
                    135:                }
                    136:
                    137:        return(h);
                    138: }
                    139:
1.6       schwarze  140: void *
                    141: html_alloc(char *outopts)
                    142: {
                    143:
                    144:        return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
                    145: }
                    146:
                    147:
                    148: void *
                    149: xhtml_alloc(char *outopts)
                    150: {
                    151:
                    152:        return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
                    153: }
                    154:
1.1       schwarze  155:
                    156: void
                    157: html_free(void *p)
                    158: {
                    159:        struct tag      *tag;
                    160:        struct ord      *ord;
                    161:        struct html     *h;
                    162:
                    163:        h = (struct html *)p;
                    164:
1.2       schwarze  165:        while ((ord = h->ords.head) != NULL) {
                    166:                h->ords.head = ord->next;
1.1       schwarze  167:                free(ord);
                    168:        }
                    169:
1.2       schwarze  170:        while ((tag = h->tags.head) != NULL) {
                    171:                h->tags.head = tag->next;
1.1       schwarze  172:                free(tag);
                    173:        }
                    174:
                    175:        if (h->symtab)
                    176:                chars_free(h->symtab);
                    177:
                    178:        free(h);
                    179: }
                    180:
                    181:
                    182: void
                    183: print_gen_head(struct html *h)
                    184: {
                    185:        struct htmlpair  tag[4];
                    186:
                    187:        tag[0].key = ATTR_HTTPEQUIV;
                    188:        tag[0].val = "Content-Type";
                    189:        tag[1].key = ATTR_CONTENT;
                    190:        tag[1].val = "text/html; charset=utf-8";
                    191:        print_otag(h, TAG_META, 2, tag);
                    192:
                    193:        tag[0].key = ATTR_NAME;
                    194:        tag[0].val = "resource-type";
                    195:        tag[1].key = ATTR_CONTENT;
                    196:        tag[1].val = "document";
                    197:        print_otag(h, TAG_META, 2, tag);
                    198:
                    199:        if (h->style) {
                    200:                tag[0].key = ATTR_REL;
                    201:                tag[0].val = "stylesheet";
                    202:                tag[1].key = ATTR_HREF;
                    203:                tag[1].val = h->style;
                    204:                tag[2].key = ATTR_TYPE;
                    205:                tag[2].val = "text/css";
                    206:                tag[3].key = ATTR_MEDIA;
                    207:                tag[3].val = "all";
                    208:                print_otag(h, TAG_LINK, 4, tag);
                    209:        }
                    210: }
                    211:
                    212:
                    213: static void
1.5       schwarze  214: print_spec(struct html *h, const char *p, size_t len)
1.1       schwarze  215: {
                    216:        const char      *rhs;
                    217:        size_t           sz;
                    218:
1.5       schwarze  219:        rhs = chars_a2ascii(h->symtab, p, len, &sz);
1.1       schwarze  220:
                    221:        if (NULL == rhs)
                    222:                return;
1.4       schwarze  223:        fwrite(rhs, 1, sz, stdout);
1.1       schwarze  224: }
                    225:
                    226:
                    227: static void
1.5       schwarze  228: print_res(struct html *h, const char *p, size_t len)
1.1       schwarze  229: {
                    230:        const char      *rhs;
                    231:        size_t           sz;
                    232:
1.5       schwarze  233:        rhs = chars_a2res(h->symtab, p, len, &sz);
1.1       schwarze  234:
                    235:        if (NULL == rhs)
                    236:                return;
1.4       schwarze  237:        fwrite(rhs, 1, sz, stdout);
1.1       schwarze  238: }
                    239:
                    240:
1.5       schwarze  241: struct tag *
                    242: print_ofont(struct html *h, enum htmlfont font)
1.1       schwarze  243: {
1.5       schwarze  244:        struct htmlpair  tag;
1.1       schwarze  245:
1.5       schwarze  246:        h->metal = h->metac;
                    247:        h->metac = font;
1.1       schwarze  248:
1.5       schwarze  249:        /* FIXME: DECO_ROMAN should just close out preexisting. */
1.1       schwarze  250:
1.5       schwarze  251:        if (h->metaf && h->tags.head == h->metaf)
                    252:                print_tagq(h, h->metaf);
1.1       schwarze  253:
1.5       schwarze  254:        PAIR_CLASS_INIT(&tag, htmlfonts[font]);
                    255:        h->metaf = print_otag(h, TAG_SPAN, 1, &tag);
                    256:        return(h->metaf);
                    257: }
1.1       schwarze  258:
                    259:
1.5       schwarze  260: static void
                    261: print_metaf(struct html *h, enum roffdeco deco)
                    262: {
                    263:        enum htmlfont    font;
1.1       schwarze  264:
1.5       schwarze  265:        switch (deco) {
                    266:        case (DECO_PREVIOUS):
                    267:                font = h->metal;
                    268:                break;
                    269:        case (DECO_ITALIC):
                    270:                font = HTMLFONT_ITALIC;
                    271:                break;
                    272:        case (DECO_BOLD):
                    273:                font = HTMLFONT_BOLD;
                    274:                break;
                    275:        case (DECO_ROMAN):
                    276:                font = HTMLFONT_NONE;
                    277:                break;
                    278:        default:
                    279:                abort();
                    280:                /* NOTREACHED */
1.1       schwarze  281:        }
                    282:
1.5       schwarze  283:        (void)print_ofont(h, font);
1.1       schwarze  284: }
                    285:
                    286:
1.5       schwarze  287: static int
                    288: print_encode(struct html *h, const char *p, int norecurse)
1.1       schwarze  289: {
1.4       schwarze  290:        size_t           sz;
1.5       schwarze  291:        int              len, nospace;
                    292:        const char      *seq;
                    293:        enum roffdeco    deco;
1.9       schwarze  294:        static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
1.5       schwarze  295:
                    296:        nospace = 0;
1.1       schwarze  297:
                    298:        for (; *p; p++) {
1.9       schwarze  299:                sz = strcspn(p, rejs);
1.4       schwarze  300:
                    301:                fwrite(p, 1, sz, stdout);
                    302:                p += /* LINTED */
                    303:                        sz;
                    304:
1.5       schwarze  305:                if ('<' == *p) {
                    306:                        printf("&lt;");
                    307:                        continue;
                    308:                } else if ('>' == *p) {
                    309:                        printf("&gt;");
                    310:                        continue;
                    311:                } else if ('&' == *p) {
                    312:                        printf("&amp;");
1.1       schwarze  313:                        continue;
1.9       schwarze  314:                } else if (ASCII_HYPH == *p) {
                    315:                        /*
                    316:                         * Note: "soft hyphens" aren't graphically
                    317:                         * displayed when not breaking the text; we want
                    318:                         * them to be displayed.
                    319:                         */
                    320:                        /*printf("&#173;");*/
                    321:                        putchar('-');
                    322:                        continue;
1.4       schwarze  323:                } else if ('\0' == *p)
                    324:                        break;
                    325:
1.5       schwarze  326:                seq = ++p;
                    327:                len = a2roffdeco(&deco, &seq, &sz);
                    328:
                    329:                switch (deco) {
                    330:                case (DECO_RESERVED):
                    331:                        print_res(h, seq, sz);
                    332:                        break;
                    333:                case (DECO_SPECIAL):
                    334:                        print_spec(h, seq, sz);
                    335:                        break;
                    336:                case (DECO_PREVIOUS):
                    337:                        /* FALLTHROUGH */
                    338:                case (DECO_BOLD):
                    339:                        /* FALLTHROUGH */
                    340:                case (DECO_ITALIC):
                    341:                        /* FALLTHROUGH */
                    342:                case (DECO_ROMAN):
                    343:                        if (norecurse)
                    344:                                break;
                    345:                        print_metaf(h, deco);
                    346:                        break;
                    347:                default:
                    348:                        break;
                    349:                }
                    350:
                    351:                p += len - 1;
                    352:
                    353:                if (DECO_NOSPACE == deco && '\0' == *(p + 1))
                    354:                        nospace = 1;
1.1       schwarze  355:        }
1.5       schwarze  356:
                    357:        return(nospace);
1.1       schwarze  358: }
                    359:
                    360:
1.6       schwarze  361: static void
                    362: print_attr(struct html *h, const char *key, const char *val)
                    363: {
                    364:        printf(" %s=\"", key);
                    365:        (void)print_encode(h, val, 1);
                    366:        putchar('\"');
                    367: }
                    368:
                    369:
1.1       schwarze  370: struct tag *
                    371: print_otag(struct html *h, enum htmltag tag,
                    372:                int sz, const struct htmlpair *p)
                    373: {
                    374:        int              i;
                    375:        struct tag      *t;
                    376:
1.6       schwarze  377:        /* Push this tags onto the stack of open scopes. */
                    378:
1.1       schwarze  379:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.3       schwarze  380:                t = malloc(sizeof(struct tag));
                    381:                if (NULL == t) {
                    382:                        perror(NULL);
                    383:                        exit(EXIT_FAILURE);
                    384:                }
1.1       schwarze  385:                t->tag = tag;
1.2       schwarze  386:                t->next = h->tags.head;
                    387:                h->tags.head = t;
1.1       schwarze  388:        } else
                    389:                t = NULL;
                    390:
                    391:        if ( ! (HTML_NOSPACE & h->flags))
1.12    ! schwarze  392:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
        !           393:                        /* Manage keeps! */
        !           394:                        if ( ! (HTML_KEEP & h->flags)) {
        !           395:                                if (HTML_PREKEEP & h->flags)
        !           396:                                        h->flags |= HTML_KEEP;
        !           397:                                putchar(' ');
        !           398:                        } else
        !           399:                                printf("&#160;");
        !           400:                }
1.1       schwarze  401:
1.6       schwarze  402:        /* Print out the tag name and attributes. */
                    403:
1.1       schwarze  404:        printf("<%s", htmltags[tag].name);
1.6       schwarze  405:        for (i = 0; i < sz; i++)
                    406:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    407:
                    408:        /* Add non-overridable attributes. */
                    409:
                    410:        if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
                    411:                print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
                    412:                print_attr(h, "xml:lang", "en");
                    413:                print_attr(h, "lang", "en");
1.1       schwarze  414:        }
1.6       schwarze  415:
                    416:        /* Accomodate for XML "well-formed" singleton escaping. */
                    417:
                    418:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
                    419:                switch (h->type) {
                    420:                case (HTML_XHTML_1_0_STRICT):
                    421:                        putchar('/');
                    422:                        break;
                    423:                default:
                    424:                        break;
                    425:                }
                    426:
1.4       schwarze  427:        putchar('>');
1.1       schwarze  428:
                    429:        h->flags |= HTML_NOSPACE;
                    430:        return(t);
                    431: }
                    432:
                    433:
                    434: static void
                    435: print_ctag(struct html *h, enum htmltag tag)
                    436: {
                    437:
                    438:        printf("</%s>", htmltags[tag].name);
1.3       schwarze  439:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.1       schwarze  440:                h->flags |= HTML_NOSPACE;
1.4       schwarze  441:                putchar('\n');
1.5       schwarze  442:        }
1.1       schwarze  443: }
                    444:
                    445:
                    446: void
1.6       schwarze  447: print_gen_decls(struct html *h)
                    448: {
                    449:
                    450:        print_xmltype(h);
                    451:        print_doctype(h);
                    452: }
                    453:
                    454:
                    455: static void
                    456: print_xmltype(struct html *h)
                    457: {
                    458:
1.9       schwarze  459:        if (HTML_XHTML_1_0_STRICT == h->type)
                    460:                printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1.6       schwarze  461: }
                    462:
                    463:
                    464: static void
                    465: print_doctype(struct html *h)
1.1       schwarze  466: {
1.6       schwarze  467:        const char      *doctype;
                    468:        const char      *dtd;
                    469:        const char      *name;
                    470:
                    471:        switch (h->type) {
                    472:        case (HTML_HTML_4_01_STRICT):
                    473:                name = "HTML";
                    474:                doctype = "-//W3C//DTD HTML 4.01//EN";
                    475:                dtd = "http://www.w3.org/TR/html4/strict.dtd";
                    476:                break;
                    477:        default:
                    478:                name = "html";
                    479:                doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
                    480:                dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
                    481:                break;
                    482:        }
                    483:
                    484:        printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
                    485:                        name, doctype, dtd);
1.1       schwarze  486: }
                    487:
                    488:
                    489: void
1.12    ! schwarze  490: print_text(struct html *h, const char *word)
1.1       schwarze  491: {
                    492:
1.12    ! schwarze  493:        if (word[0] && '\0' == word[1])
        !           494:                switch (word[0]) {
1.1       schwarze  495:                case('.'):
                    496:                        /* FALLTHROUGH */
                    497:                case(','):
                    498:                        /* FALLTHROUGH */
                    499:                case(';'):
                    500:                        /* FALLTHROUGH */
                    501:                case(':'):
                    502:                        /* FALLTHROUGH */
                    503:                case('?'):
                    504:                        /* FALLTHROUGH */
                    505:                case('!'):
                    506:                        /* FALLTHROUGH */
                    507:                case(')'):
                    508:                        /* FALLTHROUGH */
                    509:                case(']'):
                    510:                        if ( ! (HTML_IGNDELIM & h->flags))
                    511:                                h->flags |= HTML_NOSPACE;
                    512:                        break;
                    513:                default:
                    514:                        break;
                    515:                }
                    516:
1.12    ! schwarze  517:        if ( ! (HTML_NOSPACE & h->flags)) {
        !           518:                /* Manage keeps! */
        !           519:                if ( ! (HTML_KEEP & h->flags)) {
        !           520:                        if (HTML_PREKEEP & h->flags)
        !           521:                                h->flags |= HTML_KEEP;
        !           522:                        putchar(' ');
        !           523:                } else
        !           524:                        printf("&#160;");
        !           525:        }
1.1       schwarze  526:
1.12    ! schwarze  527:        assert(word);
        !           528:        if ( ! print_encode(h, word, 0))
1.5       schwarze  529:                h->flags &= ~HTML_NOSPACE;
1.1       schwarze  530:
1.8       schwarze  531:        /*
                    532:         * Note that we don't process the pipe: the parser sees it as
                    533:         * punctuation, but we don't in terms of typography.
                    534:         */
1.12    ! schwarze  535:        if (word[0] && '\0' == word[1])
        !           536:                switch (word[0]) {
1.1       schwarze  537:                case('('):
                    538:                        /* FALLTHROUGH */
                    539:                case('['):
                    540:                        h->flags |= HTML_NOSPACE;
                    541:                        break;
                    542:                default:
                    543:                        break;
                    544:                }
                    545: }
                    546:
                    547:
                    548: void
                    549: print_tagq(struct html *h, const struct tag *until)
                    550: {
                    551:        struct tag      *tag;
                    552:
1.2       schwarze  553:        while ((tag = h->tags.head) != NULL) {
1.5       schwarze  554:                if (tag == h->metaf)
                    555:                        h->metaf = NULL;
1.1       schwarze  556:                print_ctag(h, tag->tag);
1.2       schwarze  557:                h->tags.head = tag->next;
1.1       schwarze  558:                free(tag);
                    559:                if (until && tag == until)
                    560:                        return;
                    561:        }
                    562: }
                    563:
                    564:
                    565: void
                    566: print_stagq(struct html *h, const struct tag *suntil)
                    567: {
                    568:        struct tag      *tag;
                    569:
1.2       schwarze  570:        while ((tag = h->tags.head) != NULL) {
1.1       schwarze  571:                if (suntil && tag == suntil)
                    572:                        return;
1.5       schwarze  573:                if (tag == h->metaf)
                    574:                        h->metaf = NULL;
1.1       schwarze  575:                print_ctag(h, tag->tag);
1.2       schwarze  576:                h->tags.head = tag->next;
1.1       schwarze  577:                free(tag);
                    578:        }
                    579: }
                    580:
                    581:
                    582: void
                    583: bufinit(struct html *h)
                    584: {
                    585:
                    586:        h->buf[0] = '\0';
                    587:        h->buflen = 0;
                    588: }
                    589:
                    590:
                    591: void
                    592: bufcat_style(struct html *h, const char *key, const char *val)
                    593: {
                    594:
                    595:        bufcat(h, key);
                    596:        bufncat(h, ":", 1);
                    597:        bufcat(h, val);
                    598:        bufncat(h, ";", 1);
                    599: }
                    600:
                    601:
                    602: void
                    603: bufcat(struct html *h, const char *p)
                    604: {
                    605:
                    606:        bufncat(h, p, strlen(p));
                    607: }
                    608:
                    609:
                    610: void
                    611: buffmt(struct html *h, const char *fmt, ...)
                    612: {
                    613:        va_list          ap;
                    614:
                    615:        va_start(ap, fmt);
                    616:        (void)vsnprintf(h->buf + (int)h->buflen,
                    617:                        BUFSIZ - h->buflen - 1, fmt, ap);
                    618:        va_end(ap);
                    619:        h->buflen = strlen(h->buf);
                    620: }
                    621:
                    622:
                    623: void
                    624: bufncat(struct html *h, const char *p, size_t sz)
                    625: {
                    626:
                    627:        if (h->buflen + sz > BUFSIZ - 1)
                    628:                sz = BUFSIZ - 1 - h->buflen;
                    629:
                    630:        (void)strncat(h->buf, p, sz);
                    631:        h->buflen += sz;
                    632: }
                    633:
                    634:
                    635: void
                    636: buffmt_includes(struct html *h, const char *name)
                    637: {
                    638:        const char      *p, *pp;
                    639:
                    640:        pp = h->base_includes;
                    641:
                    642:        while (NULL != (p = strchr(pp, '%'))) {
                    643:                bufncat(h, pp, (size_t)(p - pp));
                    644:                switch (*(p + 1)) {
                    645:                case('I'):
                    646:                        bufcat(h, name);
                    647:                        break;
                    648:                default:
                    649:                        bufncat(h, p, 2);
                    650:                        break;
                    651:                }
                    652:                pp = p + 2;
                    653:        }
                    654:        if (pp)
                    655:                bufcat(h, pp);
                    656: }
                    657:
                    658:
                    659: void
                    660: buffmt_man(struct html *h,
                    661:                const char *name, const char *sec)
                    662: {
                    663:        const char      *p, *pp;
                    664:
                    665:        pp = h->base_man;
                    666:
                    667:        /* LINTED */
                    668:        while (NULL != (p = strchr(pp, '%'))) {
                    669:                bufncat(h, pp, (size_t)(p - pp));
                    670:                switch (*(p + 1)) {
                    671:                case('S'):
                    672:                        bufcat(h, sec ? sec : "1");
                    673:                        break;
                    674:                case('N'):
                    675:                        buffmt(h, name);
                    676:                        break;
                    677:                default:
                    678:                        bufncat(h, p, 2);
                    679:                        break;
                    680:                }
                    681:                pp = p + 2;
                    682:        }
                    683:        if (pp)
                    684:                bufcat(h, pp);
                    685: }
                    686:
                    687:
                    688: void
                    689: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    690: {
                    691:        double           v;
                    692:        const char      *u;
                    693:
                    694:        v = su->scale;
                    695:
                    696:        switch (su->unit) {
                    697:        case (SCALE_CM):
                    698:                u = "cm";
                    699:                break;
                    700:        case (SCALE_IN):
                    701:                u = "in";
                    702:                break;
                    703:        case (SCALE_PC):
                    704:                u = "pc";
                    705:                break;
                    706:        case (SCALE_PT):
                    707:                u = "pt";
                    708:                break;
                    709:        case (SCALE_EM):
                    710:                u = "em";
                    711:                break;
                    712:        case (SCALE_MM):
                    713:                if (0 == (v /= 100))
                    714:                        v = 1;
                    715:                u = "em";
                    716:                break;
                    717:        case (SCALE_EN):
                    718:                u = "ex";
                    719:                break;
                    720:        case (SCALE_BU):
                    721:                u = "ex";
                    722:                break;
                    723:        case (SCALE_VS):
                    724:                u = "em";
                    725:                break;
                    726:        default:
                    727:                u = "ex";
                    728:                break;
                    729:        }
                    730:
1.11      schwarze  731:        /*
                    732:         * XXX: the CSS spec isn't clear as to which types accept
                    733:         * integer or real numbers, so we just make them all decimals.
                    734:         */
                    735:        buffmt(h, "%s: %.2f%s;", p, v, u);
1.1       schwarze  736: }
                    737:
1.3       schwarze  738:
                    739: void
                    740: html_idcat(char *dst, const char *src, int sz)
                    741: {
                    742:        int              ssz;
                    743:
                    744:        assert(sz);
                    745:
                    746:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    747:
                    748:        for ( ; *dst != '\0' && sz; dst++, sz--)
                    749:                /* Jump to end. */ ;
                    750:
                    751:        assert(sz > 2);
                    752:
                    753:        /* We can't start with a number (bah). */
                    754:
                    755:        *dst++ = 'x';
                    756:        *dst = '\0';
                    757:        sz--;
                    758:
                    759:        for ( ; *src != '\0' && sz > 1; src++) {
                    760:                ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
                    761:                sz -= ssz;
                    762:                dst += ssz;
                    763:        }
                    764: }