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

1.69    ! schwarze    1: /*     $OpenBSD: html.c,v 1.68 2017/01/19 15:27:26 schwarze Exp $ */
1.1       schwarze    2: /*
1.42      schwarze    3:  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.64      schwarze    4:  * Copyright (c) 2011-2015, 2017 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:  *
1.56      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       schwarze   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.56      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       schwarze   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.34      schwarze   30: #include "mandoc_aux.h"
1.1       schwarze   31: #include "out.h"
                     32: #include "html.h"
1.56      schwarze   33: #include "manconf.h"
1.1       schwarze   34: #include "main.h"
                     35:
                     36: struct htmldata {
                     37:        const char       *name;
                     38:        int               flags;
1.66      schwarze   39: #define        HTML_NOSTACK     (1 << 0)
                     40: #define        HTML_AUTOCLOSE   (1 << 1)
                     41: #define        HTML_NLBEFORE    (1 << 2)
                     42: #define        HTML_NLBEGIN     (1 << 3)
                     43: #define        HTML_NLEND       (1 << 4)
                     44: #define        HTML_NLAFTER     (1 << 5)
                     45: #define        HTML_NLAROUND    (HTML_NLBEFORE | HTML_NLAFTER)
                     46: #define        HTML_NLINSIDE    (HTML_NLBEGIN | HTML_NLEND)
                     47: #define        HTML_NLALL       (HTML_NLAROUND | HTML_NLINSIDE)
                     48: #define        HTML_INDENT      (1 << 6)
                     49: #define        HTML_NOINDENT    (1 << 7)
1.1       schwarze   50: };
                     51:
                     52: static const struct htmldata htmltags[TAG_MAX] = {
1.66      schwarze   53:        {"html",        HTML_NLALL},
                     54:        {"head",        HTML_NLALL | HTML_INDENT},
                     55:        {"body",        HTML_NLALL},
                     56:        {"meta",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     57:        {"title",       HTML_NLAROUND},
                     58:        {"div",         HTML_NLAROUND},
                     59:        {"h1",          HTML_NLAROUND},
                     60:        {"h2",          HTML_NLAROUND},
                     61:        {"span",        0},
                     62:        {"link",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     63:        {"br",          HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     64:        {"a",           0},
                     65:        {"table",       HTML_NLALL | HTML_INDENT},
                     66:        {"tbody",       HTML_NLALL | HTML_INDENT},
                     67:        {"col",         HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     68:        {"tr",          HTML_NLALL | HTML_INDENT},
                     69:        {"td",          HTML_NLAROUND},
                     70:        {"li",          HTML_NLAROUND | HTML_INDENT},
                     71:        {"ul",          HTML_NLALL | HTML_INDENT},
                     72:        {"ol",          HTML_NLALL | HTML_INDENT},
                     73:        {"dl",          HTML_NLALL | HTML_INDENT},
                     74:        {"dt",          HTML_NLAROUND},
                     75:        {"dd",          HTML_NLAROUND | HTML_INDENT},
                     76:        {"pre",         HTML_NLALL | HTML_NOINDENT},
                     77:        {"b",           0},
                     78:        {"i",           0},
                     79:        {"code",        0},
                     80:        {"small",       0},
                     81:        {"style",       HTML_NLALL | HTML_INDENT},
                     82:        {"math",        HTML_NLALL | HTML_INDENT},
                     83:        {"mrow",        0},
                     84:        {"mi",          0},
                     85:        {"mo",          0},
                     86:        {"msup",        0},
                     87:        {"msub",        0},
                     88:        {"msubsup",     0},
                     89:        {"mfrac",       0},
                     90:        {"msqrt",       0},
                     91:        {"mfenced",     0},
                     92:        {"mtable",      0},
                     93:        {"mtr",         0},
                     94:        {"mtd",         0},
                     95:        {"munderover",  0},
                     96:        {"munder",      0},
                     97:        {"mover",       0},
1.5       schwarze   98: };
                     99:
1.26      schwarze  100: static const char      *const roffscales[SCALE_MAX] = {
                    101:        "cm", /* SCALE_CM */
                    102:        "in", /* SCALE_IN */
                    103:        "pc", /* SCALE_PC */
                    104:        "pt", /* SCALE_PT */
                    105:        "em", /* SCALE_EM */
                    106:        "em", /* SCALE_MM */
                    107:        "ex", /* SCALE_EN */
                    108:        "ex", /* SCALE_BU */
                    109:        "em", /* SCALE_VS */
                    110:        "ex", /* SCALE_FS */
                    111: };
1.5       schwarze  112:
1.64      schwarze  113: static void     a2width(const char *, struct roffsu *);
1.67      schwarze  114: static void     print_byte(struct html *, char);
                    115: static void     print_endline(struct html *);
                    116: static void     print_endword(struct html *);
                    117: static void     print_indent(struct html *);
                    118: static void     print_word(struct html *, const char *);
                    119:
1.54      schwarze  120: static void     print_ctag(struct html *, struct tag *);
1.67      schwarze  121: static int      print_escape(struct html *, char);
1.65      schwarze  122: static int      print_encode(struct html *, const char *, const char *, int);
                    123: static void     print_href(struct html *, const char *, const char *, int);
1.26      schwarze  124: static void     print_metaf(struct html *, enum mandoc_esc);
1.5       schwarze  125:
1.35      schwarze  126:
1.50      schwarze  127: void *
1.61      schwarze  128: html_alloc(const struct manoutput *outopts)
1.1       schwarze  129: {
                    130:        struct html     *h;
                    131:
1.24      schwarze  132:        h = mandoc_calloc(1, sizeof(struct html));
1.1       schwarze  133:
1.2       schwarze  134:        h->tags.head = NULL;
1.56      schwarze  135:        h->style = outopts->style;
                    136:        h->base_man = outopts->man;
                    137:        h->base_includes = outopts->includes;
                    138:        if (outopts->fragment)
                    139:                h->oflags |= HTML_FRAGMENT;
1.1       schwarze  140:
1.58      schwarze  141:        return h;
1.1       schwarze  142: }
                    143:
                    144: void
                    145: html_free(void *p)
                    146: {
                    147:        struct tag      *tag;
                    148:        struct html     *h;
                    149:
                    150:        h = (struct html *)p;
                    151:
1.2       schwarze  152:        while ((tag = h->tags.head) != NULL) {
1.35      schwarze  153:                h->tags.head = tag->next;
1.1       schwarze  154:                free(tag);
                    155:        }
                    156:
                    157:        free(h);
                    158: }
                    159:
                    160: void
                    161: print_gen_head(struct html *h)
                    162: {
1.42      schwarze  163:        struct tag      *t;
                    164:
1.64      schwarze  165:        print_otag(h, TAG_META, "?", "charset", "utf-8");
1.1       schwarze  166:
1.42      schwarze  167:        /*
                    168:         * Print a default style-sheet.
                    169:         */
1.66      schwarze  170:
1.64      schwarze  171:        t = print_otag(h, TAG_STYLE, "");
1.66      schwarze  172:        print_text(h, "table.head, table.foot { width: 100%; }");
1.67      schwarze  173:        print_endline(h);
1.66      schwarze  174:        print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.67      schwarze  175:        print_endline(h);
1.66      schwarze  176:        print_text(h, "td.head-vol { text-align: center; }");
1.67      schwarze  177:        print_endline(h);
1.66      schwarze  178:        print_text(h, "table.foot td { width: 50%; }");
1.67      schwarze  179:        print_endline(h);
1.66      schwarze  180:        print_text(h, "table.head td { width: 33%; }");
1.67      schwarze  181:        print_endline(h);
1.68      schwarze  182:        print_text(h, "div.Pp { margin: 1ex 0ex; }");
1.42      schwarze  183:        print_tagq(h, t);
1.1       schwarze  184:
1.64      schwarze  185:        if (h->style)
                    186:                print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
                    187:                    h->style, "type", "text/css", "media", "all");
1.1       schwarze  188: }
                    189:
1.5       schwarze  190: static void
1.26      schwarze  191: print_metaf(struct html *h, enum mandoc_esc deco)
1.5       schwarze  192: {
                    193:        enum htmlfont    font;
1.1       schwarze  194:
1.5       schwarze  195:        switch (deco) {
1.35      schwarze  196:        case ESCAPE_FONTPREV:
1.5       schwarze  197:                font = h->metal;
                    198:                break;
1.35      schwarze  199:        case ESCAPE_FONTITALIC:
1.5       schwarze  200:                font = HTMLFONT_ITALIC;
                    201:                break;
1.35      schwarze  202:        case ESCAPE_FONTBOLD:
1.5       schwarze  203:                font = HTMLFONT_BOLD;
                    204:                break;
1.35      schwarze  205:        case ESCAPE_FONTBI:
1.31      schwarze  206:                font = HTMLFONT_BI;
                    207:                break;
1.35      schwarze  208:        case ESCAPE_FONT:
                    209:        case ESCAPE_FONTROMAN:
1.5       schwarze  210:                font = HTMLFONT_NONE;
                    211:                break;
                    212:        default:
                    213:                abort();
1.1       schwarze  214:        }
                    215:
1.20      schwarze  216:        if (h->metaf) {
                    217:                print_tagq(h, h->metaf);
                    218:                h->metaf = NULL;
                    219:        }
                    220:
                    221:        h->metal = h->metac;
                    222:        h->metac = font;
                    223:
1.31      schwarze  224:        switch (font) {
1.35      schwarze  225:        case HTMLFONT_ITALIC:
1.64      schwarze  226:                h->metaf = print_otag(h, TAG_I, "");
1.31      schwarze  227:                break;
1.35      schwarze  228:        case HTMLFONT_BOLD:
1.64      schwarze  229:                h->metaf = print_otag(h, TAG_B, "");
1.31      schwarze  230:                break;
1.35      schwarze  231:        case HTMLFONT_BI:
1.64      schwarze  232:                h->metaf = print_otag(h, TAG_B, "");
                    233:                print_otag(h, TAG_I, "");
1.31      schwarze  234:                break;
                    235:        default:
                    236:                break;
                    237:        }
1.1       schwarze  238: }
                    239:
1.26      schwarze  240: int
                    241: html_strlen(const char *cp)
                    242: {
1.30      schwarze  243:        size_t           rsz;
                    244:        int              skip, sz;
1.26      schwarze  245:
                    246:        /*
                    247:         * Account for escaped sequences within string length
                    248:         * calculations.  This follows the logic in term_strlen() as we
                    249:         * must calculate the width of produced strings.
                    250:         * Assume that characters are always width of "1".  This is
                    251:         * hacky, but it gets the job done for approximation of widths.
                    252:         */
                    253:
                    254:        sz = 0;
1.30      schwarze  255:        skip = 0;
                    256:        while (1) {
                    257:                rsz = strcspn(cp, "\\");
                    258:                if (rsz) {
                    259:                        cp += rsz;
                    260:                        if (skip) {
                    261:                                skip = 0;
                    262:                                rsz--;
                    263:                        }
                    264:                        sz += rsz;
                    265:                }
                    266:                if ('\0' == *cp)
                    267:                        break;
                    268:                cp++;
                    269:                switch (mandoc_escape(&cp, NULL, NULL)) {
1.35      schwarze  270:                case ESCAPE_ERROR:
1.58      schwarze  271:                        return sz;
1.35      schwarze  272:                case ESCAPE_UNICODE:
                    273:                case ESCAPE_NUMBERED:
                    274:                case ESCAPE_SPECIAL:
1.55      schwarze  275:                case ESCAPE_OVERSTRIKE:
1.30      schwarze  276:                        if (skip)
                    277:                                skip = 0;
                    278:                        else
                    279:                                sz++;
                    280:                        break;
1.35      schwarze  281:                case ESCAPE_SKIPCHAR:
1.30      schwarze  282:                        skip = 1;
1.26      schwarze  283:                        break;
                    284:                default:
                    285:                        break;
                    286:                }
                    287:        }
1.58      schwarze  288:        return sz;
1.26      schwarze  289: }
1.1       schwarze  290:
1.5       schwarze  291: static int
1.67      schwarze  292: print_escape(struct html *h, char c)
1.38      schwarze  293: {
                    294:
                    295:        switch (c) {
                    296:        case '<':
1.67      schwarze  297:                print_word(h, "&lt;");
1.38      schwarze  298:                break;
                    299:        case '>':
1.67      schwarze  300:                print_word(h, "&gt;");
1.38      schwarze  301:                break;
                    302:        case '&':
1.67      schwarze  303:                print_word(h, "&amp;");
1.38      schwarze  304:                break;
                    305:        case '"':
1.67      schwarze  306:                print_word(h, "&quot;");
1.38      schwarze  307:                break;
                    308:        case ASCII_NBRSP:
1.67      schwarze  309:                print_word(h, "&nbsp;");
1.38      schwarze  310:                break;
                    311:        case ASCII_HYPH:
1.67      schwarze  312:                print_byte(h, '-');
1.59      schwarze  313:                break;
1.38      schwarze  314:        case ASCII_BREAK:
                    315:                break;
                    316:        default:
1.58      schwarze  317:                return 0;
1.38      schwarze  318:        }
1.58      schwarze  319:        return 1;
1.38      schwarze  320: }
                    321:
                    322: static int
1.65      schwarze  323: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.1       schwarze  324: {
1.67      schwarze  325:        char             numbuf[16];
1.4       schwarze  326:        size_t           sz;
1.26      schwarze  327:        int              c, len, nospace;
1.5       schwarze  328:        const char      *seq;
1.26      schwarze  329:        enum mandoc_esc  esc;
1.37      schwarze  330:        static const char rejs[9] = { '\\', '<', '>', '&', '"',
1.33      schwarze  331:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5       schwarze  332:
1.65      schwarze  333:        if (pend == NULL)
                    334:                pend = strchr(p, '\0');
                    335:
1.5       schwarze  336:        nospace = 0;
1.1       schwarze  337:
1.65      schwarze  338:        while (p < pend) {
1.30      schwarze  339:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    340:                        h->flags &= ~HTML_SKIPCHAR;
                    341:                        p++;
                    342:                        continue;
                    343:                }
                    344:
1.67      schwarze  345:                for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
                    346:                        if (*p == ' ')
                    347:                                print_endword(h);
                    348:                        else
                    349:                                print_byte(h, *p);
1.4       schwarze  350:
1.65      schwarze  351:                if (p >= pend)
1.26      schwarze  352:                        break;
                    353:
1.67      schwarze  354:                if (print_escape(h, *p++))
1.33      schwarze  355:                        continue;
1.4       schwarze  356:
1.26      schwarze  357:                esc = mandoc_escape(&p, &seq, &len);
                    358:                if (ESCAPE_ERROR == esc)
                    359:                        break;
1.5       schwarze  360:
1.26      schwarze  361:                switch (esc) {
1.35      schwarze  362:                case ESCAPE_FONT:
                    363:                case ESCAPE_FONTPREV:
                    364:                case ESCAPE_FONTBOLD:
                    365:                case ESCAPE_FONTITALIC:
                    366:                case ESCAPE_FONTBI:
                    367:                case ESCAPE_FONTROMAN:
1.30      schwarze  368:                        if (0 == norecurse)
                    369:                                print_metaf(h, esc);
                    370:                        continue;
1.35      schwarze  371:                case ESCAPE_SKIPCHAR:
1.30      schwarze  372:                        h->flags |= HTML_SKIPCHAR;
                    373:                        continue;
                    374:                default:
                    375:                        break;
                    376:                }
                    377:
                    378:                if (h->flags & HTML_SKIPCHAR) {
                    379:                        h->flags &= ~HTML_SKIPCHAR;
                    380:                        continue;
                    381:                }
                    382:
                    383:                switch (esc) {
1.35      schwarze  384:                case ESCAPE_UNICODE:
1.38      schwarze  385:                        /* Skip past "u" header. */
1.26      schwarze  386:                        c = mchars_num2uc(seq + 1, len - 1);
                    387:                        break;
1.35      schwarze  388:                case ESCAPE_NUMBERED:
1.26      schwarze  389:                        c = mchars_num2char(seq, len);
1.51      schwarze  390:                        if (c < 0)
                    391:                                continue;
1.26      schwarze  392:                        break;
1.35      schwarze  393:                case ESCAPE_SPECIAL:
1.61      schwarze  394:                        c = mchars_spec2cp(seq, len);
1.51      schwarze  395:                        if (c <= 0)
                    396:                                continue;
1.26      schwarze  397:                        break;
1.35      schwarze  398:                case ESCAPE_NOSPACE:
1.26      schwarze  399:                        if ('\0' == *p)
                    400:                                nospace = 1;
1.49      schwarze  401:                        continue;
1.55      schwarze  402:                case ESCAPE_OVERSTRIKE:
                    403:                        if (len == 0)
                    404:                                continue;
                    405:                        c = seq[len - 1];
                    406:                        break;
1.5       schwarze  407:                default:
1.49      schwarze  408:                        continue;
1.5       schwarze  409:                }
1.51      schwarze  410:                if ((c < 0x20 && c != 0x09) ||
                    411:                    (c > 0x7E && c < 0xA0))
1.49      schwarze  412:                        c = 0xFFFD;
1.67      schwarze  413:                if (c > 0x7E) {
                    414:                        (void)snprintf(numbuf, sizeof(numbuf), "&#%d;", c);
                    415:                        print_word(h, numbuf);
                    416:                } else if (print_escape(h, c) == 0)
                    417:                        print_byte(h, c);
1.1       schwarze  418:        }
1.5       schwarze  419:
1.58      schwarze  420:        return nospace;
1.1       schwarze  421: }
                    422:
1.6       schwarze  423: static void
1.65      schwarze  424: print_href(struct html *h, const char *name, const char *sec, int man)
1.6       schwarze  425: {
1.65      schwarze  426:        const char      *p, *pp;
                    427:
                    428:        pp = man ? h->base_man : h->base_includes;
                    429:        while ((p = strchr(pp, '%')) != NULL) {
                    430:                print_encode(h, pp, p, 1);
                    431:                if (man && p[1] == 'S') {
                    432:                        if (sec == NULL)
1.67      schwarze  433:                                print_byte(h, '1');
1.65      schwarze  434:                        else
                    435:                                print_encode(h, sec, NULL, 1);
                    436:                } else if ((man && p[1] == 'N') ||
                    437:                    (man == 0 && p[1] == 'I'))
                    438:                        print_encode(h, name, NULL, 1);
                    439:                else
                    440:                        print_encode(h, p, p + 2, 1);
                    441:                pp = p + 2;
                    442:        }
                    443:        if (*pp != '\0')
                    444:                print_encode(h, pp, NULL, 1);
1.6       schwarze  445: }
                    446:
1.1       schwarze  447: struct tag *
1.64      schwarze  448: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.1       schwarze  449: {
1.64      schwarze  450:        va_list          ap;
                    451:        struct roffsu    mysu, *su;
1.67      schwarze  452:        char             numbuf[16];
1.1       schwarze  453:        struct tag      *t;
1.65      schwarze  454:        const char      *attr;
1.64      schwarze  455:        char            *s;
1.65      schwarze  456:        double           v;
1.66      schwarze  457:        int              i, have_style, tflags;
                    458:
                    459:        tflags = htmltags[tag].flags;
1.1       schwarze  460:
1.6       schwarze  461:        /* Push this tags onto the stack of open scopes. */
                    462:
1.66      schwarze  463:        if ((tflags & HTML_NOSTACK) == 0) {
1.24      schwarze  464:                t = mandoc_malloc(sizeof(struct tag));
1.1       schwarze  465:                t->tag = tag;
1.2       schwarze  466:                t->next = h->tags.head;
                    467:                h->tags.head = t;
1.1       schwarze  468:        } else
                    469:                t = NULL;
                    470:
1.66      schwarze  471:        if (tflags & HTML_NLBEFORE)
1.67      schwarze  472:                print_endline(h);
                    473:        if (h->col == 0)
                    474:                print_indent(h);
1.66      schwarze  475:        else if ((h->flags & HTML_NOSPACE) == 0) {
                    476:                if (h->flags & HTML_KEEP)
1.67      schwarze  477:                        print_word(h, "&#160;");
1.66      schwarze  478:                else {
                    479:                        if (h->flags & HTML_PREKEEP)
                    480:                                h->flags |= HTML_KEEP;
1.67      schwarze  481:                        print_endword(h);
1.12      schwarze  482:                }
1.66      schwarze  483:        }
1.1       schwarze  484:
1.13      schwarze  485:        if ( ! (h->flags & HTML_NONOSPACE))
                    486:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  487:        else
                    488:                h->flags |= HTML_NOSPACE;
1.13      schwarze  489:
1.6       schwarze  490:        /* Print out the tag name and attributes. */
                    491:
1.67      schwarze  492:        print_byte(h, '<');
                    493:        print_word(h, htmltags[tag].name);
1.64      schwarze  494:
                    495:        va_start(ap, fmt);
                    496:
                    497:        have_style = 0;
                    498:        while (*fmt != '\0') {
                    499:                if (*fmt == 's') {
1.67      schwarze  500:                        print_word(h, " style=\"");
1.64      schwarze  501:                        have_style = 1;
                    502:                        fmt++;
                    503:                        break;
                    504:                }
                    505:                s = va_arg(ap, char *);
                    506:                switch (*fmt++) {
                    507:                case 'c':
1.65      schwarze  508:                        attr = "class";
1.64      schwarze  509:                        break;
                    510:                case 'h':
1.65      schwarze  511:                        attr = "href";
1.64      schwarze  512:                        break;
                    513:                case 'i':
1.65      schwarze  514:                        attr = "id";
1.64      schwarze  515:                        break;
                    516:                case '?':
1.65      schwarze  517:                        attr = s;
                    518:                        s = va_arg(ap, char *);
1.64      schwarze  519:                        break;
                    520:                default:
                    521:                        abort();
                    522:                }
1.67      schwarze  523:                print_byte(h, ' ');
                    524:                print_word(h, attr);
                    525:                print_byte(h, '=');
                    526:                print_byte(h, '"');
1.65      schwarze  527:                switch (*fmt) {
                    528:                case 'M':
                    529:                        print_href(h, s, va_arg(ap, char *), 1);
                    530:                        fmt++;
                    531:                        break;
                    532:                case 'I':
                    533:                        print_href(h, s, NULL, 0);
                    534:                        fmt++;
                    535:                        break;
                    536:                case 'R':
1.67      schwarze  537:                        print_byte(h, '#');
1.65      schwarze  538:                        fmt++;
                    539:                        /* FALLTHROUGH */
                    540:                default:
                    541:                        print_encode(h, s, NULL, 1);
                    542:                        break;
                    543:                }
1.67      schwarze  544:                print_byte(h, '"');
1.64      schwarze  545:        }
                    546:
                    547:        /* Print out styles. */
                    548:
                    549:        s = NULL;
                    550:        su = &mysu;
                    551:        while (*fmt != '\0') {
                    552:
                    553:                /* First letter: input argument type. */
                    554:
                    555:                switch (*fmt++) {
                    556:                case 'h':
                    557:                        i = va_arg(ap, int);
                    558:                        SCALE_HS_INIT(su, i);
                    559:                        break;
                    560:                case 's':
                    561:                        s = va_arg(ap, char *);
                    562:                        break;
                    563:                case 'u':
                    564:                        su = va_arg(ap, struct roffsu *);
                    565:                        break;
                    566:                case 'v':
                    567:                        i = va_arg(ap, int);
                    568:                        SCALE_VS_INIT(su, i);
                    569:                        break;
                    570:                case 'w':
                    571:                        s = va_arg(ap, char *);
                    572:                        a2width(s, su);
                    573:                        break;
                    574:                default:
                    575:                        abort();
                    576:                }
                    577:
                    578:                /* Second letter: style name. */
                    579:
                    580:                switch (*fmt++) {
                    581:                case 'b':
1.65      schwarze  582:                        attr = "margin-bottom";
1.64      schwarze  583:                        break;
                    584:                case 'h':
1.65      schwarze  585:                        attr = "height";
1.64      schwarze  586:                        break;
                    587:                case 'i':
1.65      schwarze  588:                        attr = "text-indent";
1.64      schwarze  589:                        break;
                    590:                case 'l':
1.65      schwarze  591:                        attr = "margin-left";
1.64      schwarze  592:                        break;
                    593:                case 't':
1.65      schwarze  594:                        attr = "margin-top";
1.64      schwarze  595:                        break;
                    596:                case 'w':
1.65      schwarze  597:                        attr = "width";
1.64      schwarze  598:                        break;
                    599:                case 'W':
1.65      schwarze  600:                        attr = "min-width";
1.64      schwarze  601:                        break;
                    602:                case '?':
1.67      schwarze  603:                        print_word(h, s);
                    604:                        print_byte(h, ':');
                    605:                        print_byte(h, ' ');
                    606:                        print_word(h, va_arg(ap, char *));
                    607:                        print_byte(h, ';');
                    608:                        if (*fmt != '\0')
                    609:                                print_byte(h, ' ');
1.65      schwarze  610:                        continue;
1.64      schwarze  611:                default:
                    612:                        abort();
                    613:                }
1.65      schwarze  614:                v = su->scale;
                    615:                if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
                    616:                        v = 1.0;
                    617:                else if (su->unit == SCALE_BU)
                    618:                        v /= 24.0;
1.67      schwarze  619:                print_word(h, attr);
                    620:                print_byte(h, ':');
                    621:                print_byte(h, ' ');
                    622:                (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
                    623:                print_word(h, numbuf);
                    624:                print_word(h, roffscales[su->unit]);
                    625:                print_byte(h, ';');
                    626:                if (*fmt != '\0')
                    627:                        print_byte(h, ' ');
1.64      schwarze  628:        }
                    629:        if (have_style)
1.67      schwarze  630:                print_byte(h, '"');
1.64      schwarze  631:
                    632:        va_end(ap);
1.6       schwarze  633:
1.42      schwarze  634:        /* Accommodate for "well-formed" singleton escaping. */
1.6       schwarze  635:
                    636:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.67      schwarze  637:                print_byte(h, '/');
1.6       schwarze  638:
1.67      schwarze  639:        print_byte(h, '>');
1.1       schwarze  640:
1.66      schwarze  641:        if (tflags & HTML_NLBEGIN)
1.67      schwarze  642:                print_endline(h);
1.66      schwarze  643:        else
                    644:                h->flags |= HTML_NOSPACE;
1.18      schwarze  645:
1.66      schwarze  646:        if (tflags & HTML_INDENT)
                    647:                h->indent++;
                    648:        if (tflags & HTML_NOINDENT)
                    649:                h->noindent++;
1.18      schwarze  650:
1.58      schwarze  651:        return t;
1.1       schwarze  652: }
                    653:
                    654: static void
1.54      schwarze  655: print_ctag(struct html *h, struct tag *tag)
1.1       schwarze  656: {
1.66      schwarze  657:        int      tflags;
1.35      schwarze  658:
1.54      schwarze  659:        /*
                    660:         * Remember to close out and nullify the current
                    661:         * meta-font and table, if applicable.
                    662:         */
                    663:        if (tag == h->metaf)
                    664:                h->metaf = NULL;
                    665:        if (tag == h->tblt)
                    666:                h->tblt = NULL;
                    667:
1.66      schwarze  668:        tflags = htmltags[tag->tag].flags;
                    669:
                    670:        if (tflags & HTML_INDENT)
                    671:                h->indent--;
                    672:        if (tflags & HTML_NOINDENT)
                    673:                h->noindent--;
                    674:        if (tflags & HTML_NLEND)
1.67      schwarze  675:                print_endline(h);
                    676:        print_indent(h);
                    677:        print_byte(h, '<');
                    678:        print_byte(h, '/');
                    679:        print_word(h, htmltags[tag->tag].name);
                    680:        print_byte(h, '>');
1.66      schwarze  681:        if (tflags & HTML_NLAFTER)
1.67      schwarze  682:                print_endline(h);
1.54      schwarze  683:
                    684:        h->tags.head = tag->next;
                    685:        free(tag);
1.1       schwarze  686: }
                    687:
                    688: void
1.6       schwarze  689: print_gen_decls(struct html *h)
                    690: {
1.67      schwarze  691:        print_word(h, "<!DOCTYPE html>");
                    692:        print_endline(h);
1.1       schwarze  693: }
                    694:
                    695: void
1.12      schwarze  696: print_text(struct html *h, const char *word)
1.1       schwarze  697: {
1.67      schwarze  698:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.12      schwarze  699:                if ( ! (HTML_KEEP & h->flags)) {
                    700:                        if (HTML_PREKEEP & h->flags)
                    701:                                h->flags |= HTML_KEEP;
1.67      schwarze  702:                        print_endword(h);
1.12      schwarze  703:                } else
1.67      schwarze  704:                        print_word(h, "&#160;");
1.12      schwarze  705:        }
1.1       schwarze  706:
1.20      schwarze  707:        assert(NULL == h->metaf);
1.31      schwarze  708:        switch (h->metac) {
1.35      schwarze  709:        case HTMLFONT_ITALIC:
1.64      schwarze  710:                h->metaf = print_otag(h, TAG_I, "");
1.31      schwarze  711:                break;
1.35      schwarze  712:        case HTMLFONT_BOLD:
1.64      schwarze  713:                h->metaf = print_otag(h, TAG_B, "");
1.31      schwarze  714:                break;
1.35      schwarze  715:        case HTMLFONT_BI:
1.64      schwarze  716:                h->metaf = print_otag(h, TAG_B, "");
                    717:                print_otag(h, TAG_I, "");
1.31      schwarze  718:                break;
                    719:        default:
1.67      schwarze  720:                print_indent(h);
1.31      schwarze  721:                break;
                    722:        }
1.20      schwarze  723:
1.12      schwarze  724:        assert(word);
1.65      schwarze  725:        if ( ! print_encode(h, word, NULL, 0)) {
1.13      schwarze  726:                if ( ! (h->flags & HTML_NONOSPACE))
                    727:                        h->flags &= ~HTML_NOSPACE;
1.53      schwarze  728:                h->flags &= ~HTML_NONEWLINE;
1.28      schwarze  729:        } else
1.53      schwarze  730:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.20      schwarze  731:
                    732:        if (h->metaf) {
                    733:                print_tagq(h, h->metaf);
                    734:                h->metaf = NULL;
                    735:        }
1.17      schwarze  736:
                    737:        h->flags &= ~HTML_IGNDELIM;
1.1       schwarze  738: }
                    739:
                    740: void
                    741: print_tagq(struct html *h, const struct tag *until)
                    742: {
                    743:        struct tag      *tag;
                    744:
1.2       schwarze  745:        while ((tag = h->tags.head) != NULL) {
1.54      schwarze  746:                print_ctag(h, tag);
1.1       schwarze  747:                if (until && tag == until)
                    748:                        return;
                    749:        }
                    750: }
                    751:
                    752: void
                    753: print_stagq(struct html *h, const struct tag *suntil)
                    754: {
                    755:        struct tag      *tag;
                    756:
1.2       schwarze  757:        while ((tag = h->tags.head) != NULL) {
1.1       schwarze  758:                if (suntil && tag == suntil)
                    759:                        return;
1.54      schwarze  760:                print_ctag(h, tag);
1.1       schwarze  761:        }
                    762: }
1.42      schwarze  763:
                    764: void
                    765: print_paragraph(struct html *h)
                    766: {
                    767:        struct tag      *t;
                    768:
1.68      schwarze  769:        t = print_otag(h, TAG_DIV, "c", "Pp");
1.42      schwarze  770:        print_tagq(h, t);
                    771: }
                    772:
1.67      schwarze  773:
                    774: /***********************************************************************
                    775:  * Low level output functions.
                    776:  * They implement line breaking using a short static buffer.
                    777:  ***********************************************************************/
                    778:
                    779: /*
                    780:  * Buffer one HTML output byte.
                    781:  * If the buffer is full, flush and deactivate it and start a new line.
                    782:  * If the buffer is inactive, print directly.
                    783:  */
                    784: static void
                    785: print_byte(struct html *h, char c)
                    786: {
                    787:        if ((h->flags & HTML_BUFFER) == 0) {
                    788:                putchar(c);
                    789:                h->col++;
                    790:                return;
                    791:        }
                    792:
                    793:        if (h->col + h->bufcol < sizeof(h->buf)) {
                    794:                h->buf[h->bufcol++] = c;
                    795:                return;
                    796:        }
                    797:
                    798:        putchar('\n');
                    799:        h->col = 0;
                    800:        print_indent(h);
                    801:        putchar(' ');
                    802:        putchar(' ');
                    803:        fwrite(h->buf, h->bufcol, 1, stdout);
                    804:        putchar(c);
                    805:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                    806:        h->bufcol = 0;
                    807:        h->flags &= ~HTML_BUFFER;
                    808: }
                    809:
1.66      schwarze  810: /*
                    811:  * If something was printed on the current output line, end it.
1.67      schwarze  812:  * Not to be called right after print_indent().
1.66      schwarze  813:  */
                    814: static void
1.67      schwarze  815: print_endline(struct html *h)
1.66      schwarze  816: {
1.67      schwarze  817:        if (h->col == 0)
1.66      schwarze  818:                return;
                    819:
1.67      schwarze  820:        if (h->bufcol) {
                    821:                putchar(' ');
                    822:                fwrite(h->buf, h->bufcol, 1, stdout);
                    823:                h->bufcol = 0;
                    824:        }
1.66      schwarze  825:        putchar('\n');
1.67      schwarze  826:        h->col = 0;
                    827:        h->flags |= HTML_NOSPACE;
                    828:        h->flags &= ~HTML_BUFFER;
                    829: }
                    830:
                    831: /*
                    832:  * Flush the HTML output buffer.
                    833:  * If it is inactive, activate it.
                    834:  */
                    835: static void
                    836: print_endword(struct html *h)
                    837: {
                    838:        if (h->noindent) {
                    839:                print_byte(h, ' ');
                    840:                return;
                    841:        }
                    842:
                    843:        if ((h->flags & HTML_BUFFER) == 0) {
                    844:                h->col++;
                    845:                h->flags |= HTML_BUFFER;
                    846:        } else if (h->bufcol) {
                    847:                putchar(' ');
                    848:                fwrite(h->buf, h->bufcol, 1, stdout);
                    849:                h->col += h->bufcol + 1;
                    850:        }
                    851:        h->bufcol = 0;
1.66      schwarze  852: }
                    853:
                    854: /*
                    855:  * If at the beginning of a new output line,
                    856:  * perform indentation and mark the line as containing output.
                    857:  * Make sure to really produce some output right afterwards,
                    858:  * but do not use print_otag() for producing it.
                    859:  */
                    860: static void
1.67      schwarze  861: print_indent(struct html *h)
1.66      schwarze  862: {
1.67      schwarze  863:        size_t   i;
1.66      schwarze  864:
1.67      schwarze  865:        if (h->col)
1.66      schwarze  866:                return;
                    867:
1.67      schwarze  868:        if (h->noindent == 0) {
                    869:                h->col = h->indent * 2;
                    870:                for (i = 0; i < h->col; i++)
1.66      schwarze  871:                        putchar(' ');
1.67      schwarze  872:        }
                    873:        h->flags &= ~HTML_NOSPACE;
                    874: }
                    875:
                    876: /*
                    877:  * Print or buffer some characters
                    878:  * depending on the current HTML output buffer state.
                    879:  */
                    880: static void
                    881: print_word(struct html *h, const char *cp)
                    882: {
                    883:        while (*cp != '\0')
                    884:                print_byte(h, *cp++);
1.66      schwarze  885: }
1.64      schwarze  886:
                    887: /*
                    888:  * Calculate the scaling unit passed in a `-width' argument.  This uses
                    889:  * either a native scaling unit (e.g., 1i, 2m) or the string length of
                    890:  * the value.
                    891:  */
                    892: static void
                    893: a2width(const char *p, struct roffsu *su)
                    894: {
                    895:        if (a2roffsu(p, su, SCALE_MAX) < 2) {
                    896:                su->unit = SCALE_EN;
                    897:                su->scale = html_strlen(p);
                    898:        } else if (su->scale < 0.0)
                    899:                su->scale = 0.0;
1.3       schwarze  900: }