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

1.25    ! schwarze    1: /*     $Id: html.c,v 1.24 2011/04/21 22:59:54 schwarze Exp $ */
1.1       schwarze    2: /*
1.22      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.23      schwarze    4:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18: #include <sys/types.h>
                     19:
                     20: #include <assert.h>
1.3       schwarze   21: #include <ctype.h>
1.4       schwarze   22: #include <stdarg.h>
1.1       schwarze   23: #include <stdio.h>
                     24: #include <stdint.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28:
1.9       schwarze   29: #include "mandoc.h"
1.1       schwarze   30: #include "out.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.18      schwarze   56:        {"tbody",       HTML_CLRLINE}, /* TAG_TBODY */
1.6       schwarze   57:        {"col",         HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.1       schwarze   58:        {"tr",          HTML_CLRLINE}, /* TAG_TR */
                     59:        {"td",          HTML_CLRLINE}, /* TAG_TD */
                     60:        {"li",          HTML_CLRLINE}, /* TAG_LI */
                     61:        {"ul",          HTML_CLRLINE}, /* TAG_UL */
                     62:        {"ol",          HTML_CLRLINE}, /* TAG_OL */
1.18      schwarze   63:        {"dl",          HTML_CLRLINE}, /* TAG_DL */
                     64:        {"dt",          HTML_CLRLINE}, /* TAG_DT */
                     65:        {"dd",          HTML_CLRLINE}, /* TAG_DD */
                     66:        {"blockquote",  HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
                     67:        {"p",           HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_P */
                     68:        {"pre",         HTML_CLRLINE }, /* TAG_PRE */
1.19      schwarze   69:        {"b",           0 }, /* TAG_B */
                     70:        {"i",           0 }, /* TAG_I */
1.20      schwarze   71:        {"code",        0 }, /* TAG_CODE */
                     72:        {"small",       0 }, /* TAG_SMALL */
1.5       schwarze   73: };
                     74:
                     75: static const char      *const htmlattrs[ATTR_MAX] = {
1.19      schwarze   76:        "http-equiv", /* ATTR_HTTPEQUIV */
                     77:        "content", /* ATTR_CONTENT */
                     78:        "name", /* ATTR_NAME */
                     79:        "rel", /* ATTR_REL */
                     80:        "href", /* ATTR_HREF */
                     81:        "type", /* ATTR_TYPE */
                     82:        "media", /* ATTR_MEDIA */
                     83:        "class", /* ATTR_CLASS */
                     84:        "style", /* ATTR_STYLE */
                     85:        "width", /* ATTR_WIDTH */
                     86:        "id", /* ATTR_ID */
                     87:        "summary", /* ATTR_SUMMARY */
                     88:        "align", /* ATTR_ALIGN */
1.22      schwarze   89:        "colspan", /* ATTR_COLSPAN */
1.1       schwarze   90: };
                     91:
1.23      schwarze   92: static void              print_num(struct html *, const char *, size_t);
1.13      schwarze   93: static void              print_spec(struct html *, enum roffdeco,
                     94:                                const char *, size_t);
1.5       schwarze   95: static void              print_res(struct html *, const char *, size_t);
                     96: static void              print_ctag(struct html *, enum htmltag);
1.6       schwarze   97: static void              print_doctype(struct html *);
                     98: static void              print_xmltype(struct html *);
1.5       schwarze   99: static int               print_encode(struct html *, const char *, int);
                    100: static void              print_metaf(struct html *, enum roffdeco);
1.6       schwarze  101: static void              print_attr(struct html *,
                    102:                                const char *, const char *);
                    103: static void             *ml_alloc(char *, enum htmltype);
1.5       schwarze  104:
                    105:
1.6       schwarze  106: static void *
                    107: ml_alloc(char *outopts, enum htmltype type)
1.1       schwarze  108: {
                    109:        struct html     *h;
                    110:        const char      *toks[4];
                    111:        char            *v;
                    112:
                    113:        toks[0] = "style";
                    114:        toks[1] = "man";
                    115:        toks[2] = "includes";
                    116:        toks[3] = NULL;
                    117:
1.24      schwarze  118:        h = mandoc_calloc(1, sizeof(struct html));
1.1       schwarze  119:
1.6       schwarze  120:        h->type = type;
1.2       schwarze  121:        h->tags.head = NULL;
1.3       schwarze  122:        h->symtab = chars_init(CHARS_HTML);
1.1       schwarze  123:
                    124:        while (outopts && *outopts)
                    125:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    126:                case (0):
                    127:                        h->style = v;
                    128:                        break;
                    129:                case (1):
                    130:                        h->base_man = v;
                    131:                        break;
                    132:                case (2):
                    133:                        h->base_includes = v;
                    134:                        break;
                    135:                default:
                    136:                        break;
                    137:                }
                    138:
                    139:        return(h);
                    140: }
                    141:
1.6       schwarze  142: void *
                    143: html_alloc(char *outopts)
                    144: {
                    145:
                    146:        return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
                    147: }
                    148:
                    149:
                    150: void *
                    151: xhtml_alloc(char *outopts)
                    152: {
                    153:
                    154:        return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
                    155: }
                    156:
1.1       schwarze  157:
                    158: void
                    159: html_free(void *p)
                    160: {
                    161:        struct tag      *tag;
                    162:        struct html     *h;
                    163:
                    164:        h = (struct html *)p;
                    165:
1.2       schwarze  166:        while ((tag = h->tags.head) != NULL) {
                    167:                h->tags.head = tag->next;
1.1       schwarze  168:                free(tag);
                    169:        }
                    170:
                    171:        if (h->symtab)
                    172:                chars_free(h->symtab);
                    173:
                    174:        free(h);
                    175: }
                    176:
                    177:
                    178: void
                    179: print_gen_head(struct html *h)
                    180: {
                    181:        struct htmlpair  tag[4];
                    182:
                    183:        tag[0].key = ATTR_HTTPEQUIV;
                    184:        tag[0].val = "Content-Type";
                    185:        tag[1].key = ATTR_CONTENT;
                    186:        tag[1].val = "text/html; charset=utf-8";
                    187:        print_otag(h, TAG_META, 2, tag);
                    188:
                    189:        tag[0].key = ATTR_NAME;
                    190:        tag[0].val = "resource-type";
                    191:        tag[1].key = ATTR_CONTENT;
                    192:        tag[1].val = "document";
                    193:        print_otag(h, TAG_META, 2, tag);
                    194:
                    195:        if (h->style) {
                    196:                tag[0].key = ATTR_REL;
                    197:                tag[0].val = "stylesheet";
                    198:                tag[1].key = ATTR_HREF;
                    199:                tag[1].val = h->style;
                    200:                tag[2].key = ATTR_TYPE;
                    201:                tag[2].val = "text/css";
                    202:                tag[3].key = ATTR_MEDIA;
                    203:                tag[3].val = "all";
                    204:                print_otag(h, TAG_LINK, 4, tag);
                    205:        }
                    206: }
                    207:
1.24      schwarze  208: /* ARGSUSED */
1.1       schwarze  209: static void
1.23      schwarze  210: print_num(struct html *h, const char *p, size_t len)
                    211: {
                    212:        const char      *rhs;
                    213:
                    214:        rhs = chars_num2char(p, len);
                    215:        if (rhs)
                    216:                putchar((int)*rhs);
                    217: }
                    218:
                    219: static void
1.13      schwarze  220: print_spec(struct html *h, enum roffdeco d, const char *p, size_t len)
1.1       schwarze  221: {
1.13      schwarze  222:        int              cp;
1.1       schwarze  223:        const char      *rhs;
                    224:        size_t           sz;
                    225:
1.13      schwarze  226:        if ((cp = chars_spec2cp(h->symtab, p, len)) > 0) {
                    227:                printf("&#%d;", cp);
                    228:                return;
                    229:        } else if (-1 == cp && DECO_SSPECIAL == d) {
                    230:                fwrite(p, 1, len, stdout);
                    231:                return;
                    232:        } else if (-1 == cp)
                    233:                return;
1.1       schwarze  234:
1.13      schwarze  235:        if (NULL != (rhs = chars_spec2str(h->symtab, p, len, &sz)))
                    236:                fwrite(rhs, 1, sz, stdout);
1.1       schwarze  237: }
                    238:
                    239:
                    240: static void
1.5       schwarze  241: print_res(struct html *h, const char *p, size_t len)
1.1       schwarze  242: {
1.13      schwarze  243:        int              cp;
1.1       schwarze  244:        const char      *rhs;
                    245:        size_t           sz;
                    246:
1.13      schwarze  247:        if ((cp = chars_res2cp(h->symtab, p, len)) > 0) {
                    248:                printf("&#%d;", cp);
                    249:                return;
                    250:        } else if (-1 == cp)
                    251:                return;
1.1       schwarze  252:
1.13      schwarze  253:        if (NULL != (rhs = chars_res2str(h->symtab, p, len, &sz)))
                    254:                fwrite(rhs, 1, sz, stdout);
1.1       schwarze  255: }
                    256:
                    257:
1.5       schwarze  258: static void
                    259: print_metaf(struct html *h, enum roffdeco deco)
                    260: {
                    261:        enum htmlfont    font;
1.1       schwarze  262:
1.5       schwarze  263:        switch (deco) {
                    264:        case (DECO_PREVIOUS):
                    265:                font = h->metal;
                    266:                break;
                    267:        case (DECO_ITALIC):
                    268:                font = HTMLFONT_ITALIC;
                    269:                break;
                    270:        case (DECO_BOLD):
                    271:                font = HTMLFONT_BOLD;
                    272:                break;
                    273:        case (DECO_ROMAN):
                    274:                font = HTMLFONT_NONE;
                    275:                break;
                    276:        default:
                    277:                abort();
                    278:                /* NOTREACHED */
1.1       schwarze  279:        }
                    280:
1.20      schwarze  281:        if (h->metaf) {
                    282:                print_tagq(h, h->metaf);
                    283:                h->metaf = NULL;
                    284:        }
                    285:
                    286:        h->metal = h->metac;
                    287:        h->metac = font;
                    288:
                    289:        if (HTMLFONT_NONE != font)
                    290:                h->metaf = HTMLFONT_BOLD == font ?
                    291:                        print_otag(h, TAG_B, 0, NULL) :
                    292:                        print_otag(h, TAG_I, 0, NULL);
1.1       schwarze  293: }
                    294:
                    295:
1.5       schwarze  296: static int
                    297: print_encode(struct html *h, const char *p, int norecurse)
1.1       schwarze  298: {
1.4       schwarze  299:        size_t           sz;
1.5       schwarze  300:        int              len, nospace;
                    301:        const char      *seq;
                    302:        enum roffdeco    deco;
1.9       schwarze  303:        static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
1.5       schwarze  304:
                    305:        nospace = 0;
1.1       schwarze  306:
                    307:        for (; *p; p++) {
1.9       schwarze  308:                sz = strcspn(p, rejs);
1.4       schwarze  309:
                    310:                fwrite(p, 1, sz, stdout);
                    311:                p += /* LINTED */
                    312:                        sz;
                    313:
1.5       schwarze  314:                if ('<' == *p) {
                    315:                        printf("&lt;");
                    316:                        continue;
                    317:                } else if ('>' == *p) {
                    318:                        printf("&gt;");
                    319:                        continue;
                    320:                } else if ('&' == *p) {
                    321:                        printf("&amp;");
1.1       schwarze  322:                        continue;
1.9       schwarze  323:                } else if (ASCII_HYPH == *p) {
                    324:                        /*
                    325:                         * Note: "soft hyphens" aren't graphically
                    326:                         * displayed when not breaking the text; we want
                    327:                         * them to be displayed.
                    328:                         */
                    329:                        /*printf("&#173;");*/
                    330:                        putchar('-');
                    331:                        continue;
1.4       schwarze  332:                } else if ('\0' == *p)
                    333:                        break;
                    334:
1.5       schwarze  335:                seq = ++p;
                    336:                len = a2roffdeco(&deco, &seq, &sz);
                    337:
                    338:                switch (deco) {
1.23      schwarze  339:                case (DECO_NUMBERED):
                    340:                        print_num(h, seq, sz);
                    341:                        break;
1.5       schwarze  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.24      schwarze  394:                t = mandoc_malloc(sizeof(struct tag));
1.1       schwarze  395:                t->tag = tag;
1.2       schwarze  396:                t->next = h->tags.head;
                    397:                h->tags.head = t;
1.1       schwarze  398:        } else
                    399:                t = NULL;
                    400:
                    401:        if ( ! (HTML_NOSPACE & h->flags))
1.12      schwarze  402:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                    403:                        /* Manage keeps! */
                    404:                        if ( ! (HTML_KEEP & h->flags)) {
                    405:                                if (HTML_PREKEEP & h->flags)
                    406:                                        h->flags |= HTML_KEEP;
                    407:                                putchar(' ');
                    408:                        } else
                    409:                                printf("&#160;");
                    410:                }
1.1       schwarze  411:
1.13      schwarze  412:        if ( ! (h->flags & HTML_NONOSPACE))
                    413:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  414:        else
                    415:                h->flags |= HTML_NOSPACE;
1.13      schwarze  416:
1.6       schwarze  417:        /* Print out the tag name and attributes. */
                    418:
1.1       schwarze  419:        printf("<%s", htmltags[tag].name);
1.6       schwarze  420:        for (i = 0; i < sz; i++)
                    421:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    422:
                    423:        /* Add non-overridable attributes. */
                    424:
                    425:        if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
                    426:                print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
                    427:                print_attr(h, "xml:lang", "en");
                    428:                print_attr(h, "lang", "en");
1.1       schwarze  429:        }
1.6       schwarze  430:
                    431:        /* Accomodate for XML "well-formed" singleton escaping. */
                    432:
                    433:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
                    434:                switch (h->type) {
                    435:                case (HTML_XHTML_1_0_STRICT):
                    436:                        putchar('/');
                    437:                        break;
                    438:                default:
                    439:                        break;
                    440:                }
                    441:
1.4       schwarze  442:        putchar('>');
1.1       schwarze  443:
                    444:        h->flags |= HTML_NOSPACE;
1.18      schwarze  445:
                    446:        if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
                    447:                putchar('\n');
                    448:
1.1       schwarze  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)
1.20      schwarze  479:                puts("<?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: void
1.12      schwarze  508: print_text(struct html *h, const char *word)
1.1       schwarze  509: {
                    510:
1.12      schwarze  511:        if ( ! (HTML_NOSPACE & h->flags)) {
                    512:                /* Manage keeps! */
                    513:                if ( ! (HTML_KEEP & h->flags)) {
                    514:                        if (HTML_PREKEEP & h->flags)
                    515:                                h->flags |= HTML_KEEP;
                    516:                        putchar(' ');
                    517:                } else
                    518:                        printf("&#160;");
                    519:        }
1.1       schwarze  520:
1.20      schwarze  521:        assert(NULL == h->metaf);
                    522:        if (HTMLFONT_NONE != h->metac)
                    523:                h->metaf = HTMLFONT_BOLD == h->metac ?
                    524:                        print_otag(h, TAG_B, 0, NULL) :
                    525:                        print_otag(h, TAG_I, 0, NULL);
                    526:
1.12      schwarze  527:        assert(word);
                    528:        if ( ! print_encode(h, word, 0))
1.13      schwarze  529:                if ( ! (h->flags & HTML_NONOSPACE))
                    530:                        h->flags &= ~HTML_NOSPACE;
1.20      schwarze  531:
                    532:        if (h->metaf) {
                    533:                print_tagq(h, h->metaf);
                    534:                h->metaf = NULL;
                    535:        }
1.17      schwarze  536:
                    537:        h->flags &= ~HTML_IGNDELIM;
1.1       schwarze  538: }
                    539:
                    540:
                    541: void
                    542: print_tagq(struct html *h, const struct tag *until)
                    543: {
                    544:        struct tag      *tag;
                    545:
1.2       schwarze  546:        while ((tag = h->tags.head) != NULL) {
1.22      schwarze  547:                /*
                    548:                 * Remember to close out and nullify the current
                    549:                 * meta-font and table, if applicable.
                    550:                 */
1.5       schwarze  551:                if (tag == h->metaf)
                    552:                        h->metaf = NULL;
1.22      schwarze  553:                if (tag == h->tblt)
                    554:                        h->tblt = NULL;
1.1       schwarze  555:                print_ctag(h, tag->tag);
1.2       schwarze  556:                h->tags.head = tag->next;
1.1       schwarze  557:                free(tag);
                    558:                if (until && tag == until)
                    559:                        return;
                    560:        }
                    561: }
                    562:
                    563:
                    564: void
                    565: print_stagq(struct html *h, const struct tag *suntil)
                    566: {
                    567:        struct tag      *tag;
                    568:
1.2       schwarze  569:        while ((tag = h->tags.head) != NULL) {
1.1       schwarze  570:                if (suntil && tag == suntil)
                    571:                        return;
1.22      schwarze  572:                /*
                    573:                 * Remember to close out and nullify the current
                    574:                 * meta-font and table, if applicable.
                    575:                 */
1.5       schwarze  576:                if (tag == h->metaf)
                    577:                        h->metaf = NULL;
1.22      schwarze  578:                if (tag == h->tblt)
                    579:                        h->tblt = NULL;
1.1       schwarze  580:                print_ctag(h, tag->tag);
1.2       schwarze  581:                h->tags.head = tag->next;
1.1       schwarze  582:                free(tag);
                    583:        }
                    584: }
                    585:
                    586:
                    587: void
                    588: bufinit(struct html *h)
                    589: {
                    590:
                    591:        h->buf[0] = '\0';
                    592:        h->buflen = 0;
                    593: }
                    594:
                    595:
                    596: void
                    597: bufcat_style(struct html *h, const char *key, const char *val)
                    598: {
                    599:
                    600:        bufcat(h, key);
                    601:        bufncat(h, ":", 1);
                    602:        bufcat(h, val);
                    603:        bufncat(h, ";", 1);
                    604: }
                    605:
                    606:
                    607: void
                    608: bufcat(struct html *h, const char *p)
                    609: {
                    610:
                    611:        bufncat(h, p, strlen(p));
                    612: }
                    613:
                    614:
                    615: void
                    616: buffmt(struct html *h, const char *fmt, ...)
                    617: {
                    618:        va_list          ap;
                    619:
                    620:        va_start(ap, fmt);
                    621:        (void)vsnprintf(h->buf + (int)h->buflen,
                    622:                        BUFSIZ - h->buflen - 1, fmt, ap);
                    623:        va_end(ap);
                    624:        h->buflen = strlen(h->buf);
                    625: }
                    626:
                    627:
                    628: void
                    629: bufncat(struct html *h, const char *p, size_t sz)
                    630: {
                    631:
                    632:        if (h->buflen + sz > BUFSIZ - 1)
                    633:                sz = BUFSIZ - 1 - h->buflen;
                    634:
                    635:        (void)strncat(h->buf, p, sz);
                    636:        h->buflen += sz;
                    637: }
                    638:
                    639:
                    640: void
                    641: buffmt_includes(struct html *h, const char *name)
                    642: {
                    643:        const char      *p, *pp;
                    644:
                    645:        pp = h->base_includes;
                    646:
                    647:        while (NULL != (p = strchr(pp, '%'))) {
                    648:                bufncat(h, pp, (size_t)(p - pp));
                    649:                switch (*(p + 1)) {
                    650:                case('I'):
                    651:                        bufcat(h, name);
                    652:                        break;
                    653:                default:
                    654:                        bufncat(h, p, 2);
                    655:                        break;
                    656:                }
                    657:                pp = p + 2;
                    658:        }
                    659:        if (pp)
                    660:                bufcat(h, pp);
                    661: }
                    662:
                    663:
                    664: void
                    665: buffmt_man(struct html *h,
                    666:                const char *name, const char *sec)
                    667: {
                    668:        const char      *p, *pp;
                    669:
                    670:        pp = h->base_man;
                    671:
                    672:        /* LINTED */
                    673:        while (NULL != (p = strchr(pp, '%'))) {
                    674:                bufncat(h, pp, (size_t)(p - pp));
                    675:                switch (*(p + 1)) {
                    676:                case('S'):
                    677:                        bufcat(h, sec ? sec : "1");
                    678:                        break;
                    679:                case('N'):
                    680:                        buffmt(h, name);
                    681:                        break;
                    682:                default:
                    683:                        bufncat(h, p, 2);
                    684:                        break;
                    685:                }
                    686:                pp = p + 2;
                    687:        }
                    688:        if (pp)
                    689:                bufcat(h, pp);
                    690: }
                    691:
                    692:
                    693: void
                    694: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    695: {
                    696:        double           v;
                    697:        const char      *u;
                    698:
                    699:        v = su->scale;
                    700:
                    701:        switch (su->unit) {
                    702:        case (SCALE_CM):
                    703:                u = "cm";
                    704:                break;
                    705:        case (SCALE_IN):
                    706:                u = "in";
                    707:                break;
                    708:        case (SCALE_PC):
                    709:                u = "pc";
                    710:                break;
                    711:        case (SCALE_PT):
                    712:                u = "pt";
                    713:                break;
                    714:        case (SCALE_EM):
                    715:                u = "em";
                    716:                break;
                    717:        case (SCALE_MM):
                    718:                if (0 == (v /= 100))
                    719:                        v = 1;
                    720:                u = "em";
                    721:                break;
                    722:        case (SCALE_EN):
                    723:                u = "ex";
                    724:                break;
                    725:        case (SCALE_BU):
                    726:                u = "ex";
                    727:                break;
                    728:        case (SCALE_VS):
                    729:                u = "em";
                    730:                break;
                    731:        default:
                    732:                u = "ex";
                    733:                break;
                    734:        }
                    735:
1.11      schwarze  736:        /*
                    737:         * XXX: the CSS spec isn't clear as to which types accept
                    738:         * integer or real numbers, so we just make them all decimals.
                    739:         */
                    740:        buffmt(h, "%s: %.2f%s;", p, v, u);
1.1       schwarze  741: }
                    742:
1.3       schwarze  743:
                    744: void
                    745: html_idcat(char *dst, const char *src, int sz)
                    746: {
                    747:        int              ssz;
                    748:
1.21      schwarze  749:        assert(sz > 2);
1.3       schwarze  750:
                    751:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    752:
1.21      schwarze  753:        /* We can't start with a number (bah). */
                    754:
                    755:        if ('#' == *dst) {
                    756:                dst++;
                    757:                sz--;
                    758:        }
                    759:        if ('\0' == *dst) {
                    760:                *dst++ = 'x';
                    761:                *dst = '\0';
                    762:                sz--;
                    763:        }
                    764:
1.3       schwarze  765:        for ( ; *dst != '\0' && sz; dst++, sz--)
                    766:                /* Jump to end. */ ;
                    767:
                    768:        for ( ; *src != '\0' && sz > 1; src++) {
                    769:                ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
                    770:                sz -= ssz;
                    771:                dst += ssz;
                    772:        }
                    773: }