[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.17

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