Annotation of src/usr.bin/mandoc/html.c, Revision 1.47
1.47 ! schwarze 1: /* $OpenBSD: html.c,v 1.46 2014/10/13 21:05:59 chl Exp $ */
1.1 schwarze 2: /*
1.42 schwarze 3: * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.33 schwarze 4: * Copyright (c) 2011, 2012, 2013, 2014 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.34 schwarze 30: #include "mandoc_aux.h"
1.26 schwarze 31: #include "libmandoc.h"
1.1 schwarze 32: #include "out.h"
33: #include "html.h"
34: #include "main.h"
35:
36: struct htmldata {
37: const char *name;
38: int flags;
39: #define HTML_CLRLINE (1 << 0)
40: #define HTML_NOSTACK (1 << 1)
1.6 schwarze 41: #define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */
1.1 schwarze 42: };
43:
44: static const struct htmldata htmltags[TAG_MAX] = {
45: {"html", HTML_CLRLINE}, /* TAG_HTML */
46: {"head", HTML_CLRLINE}, /* TAG_HEAD */
47: {"body", HTML_CLRLINE}, /* TAG_BODY */
1.6 schwarze 48: {"meta", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.1 schwarze 49: {"title", HTML_CLRLINE}, /* TAG_TITLE */
50: {"div", HTML_CLRLINE}, /* TAG_DIV */
51: {"h1", 0}, /* TAG_H1 */
52: {"h2", 0}, /* TAG_H2 */
53: {"span", 0}, /* TAG_SPAN */
1.8 schwarze 54: {"link", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.6 schwarze 55: {"br", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.1 schwarze 56: {"a", 0}, /* TAG_A */
57: {"table", HTML_CLRLINE}, /* TAG_TABLE */
1.18 schwarze 58: {"tbody", HTML_CLRLINE}, /* TAG_TBODY */
1.6 schwarze 59: {"col", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.1 schwarze 60: {"tr", HTML_CLRLINE}, /* TAG_TR */
61: {"td", HTML_CLRLINE}, /* TAG_TD */
62: {"li", HTML_CLRLINE}, /* TAG_LI */
63: {"ul", HTML_CLRLINE}, /* TAG_UL */
64: {"ol", HTML_CLRLINE}, /* TAG_OL */
1.18 schwarze 65: {"dl", HTML_CLRLINE}, /* TAG_DL */
66: {"dt", HTML_CLRLINE}, /* TAG_DT */
67: {"dd", HTML_CLRLINE}, /* TAG_DD */
68: {"blockquote", HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
69: {"pre", HTML_CLRLINE }, /* TAG_PRE */
1.19 schwarze 70: {"b", 0 }, /* TAG_B */
71: {"i", 0 }, /* TAG_I */
1.20 schwarze 72: {"code", 0 }, /* TAG_CODE */
73: {"small", 0 }, /* TAG_SMALL */
1.42 schwarze 74: {"style", HTML_CLRLINE}, /* TAG_STYLE */
1.43 schwarze 75: {"math", HTML_CLRLINE}, /* TAG_MATH */
76: {"mrow", 0}, /* TAG_MROW */
77: {"mi", 0}, /* TAG_MI */
78: {"mo", 0}, /* TAG_MO */
79: {"msup", 0}, /* TAG_MSUP */
80: {"msub", 0}, /* TAG_MSUB */
81: {"msubsup", 0}, /* TAG_MSUBSUP */
82: {"mfrac", 0}, /* TAG_MFRAC */
83: {"msqrt", 0}, /* TAG_MSQRT */
84: {"mfenced", 0}, /* TAG_MFENCED */
85: {"mtable", 0}, /* TAG_MTABLE */
86: {"mtr", 0}, /* TAG_MTR */
87: {"mtd", 0}, /* TAG_MTD */
1.44 schwarze 88: {"munderover", 0}, /* TAG_MUNDEROVER */
89: {"munder", 0}, /* TAG_MUNDER*/
90: {"mover", 0}, /* TAG_MOVER*/
1.5 schwarze 91: };
92:
93: static const char *const htmlattrs[ATTR_MAX] = {
1.19 schwarze 94: "name", /* ATTR_NAME */
95: "rel", /* ATTR_REL */
96: "href", /* ATTR_HREF */
97: "type", /* ATTR_TYPE */
98: "media", /* ATTR_MEDIA */
99: "class", /* ATTR_CLASS */
100: "style", /* ATTR_STYLE */
101: "id", /* ATTR_ID */
1.22 schwarze 102: "colspan", /* ATTR_COLSPAN */
1.42 schwarze 103: "charset", /* ATTR_CHARSET */
1.43 schwarze 104: "open", /* ATTR_OPEN */
105: "close", /* ATTR_CLOSE */
1.45 schwarze 106: "mathvariant", /* ATTR_MATHVARIANT */
1.1 schwarze 107: };
108:
1.26 schwarze 109: static const char *const roffscales[SCALE_MAX] = {
110: "cm", /* SCALE_CM */
111: "in", /* SCALE_IN */
112: "pc", /* SCALE_PC */
113: "pt", /* SCALE_PT */
114: "em", /* SCALE_EM */
115: "em", /* SCALE_MM */
116: "ex", /* SCALE_EN */
117: "ex", /* SCALE_BU */
118: "em", /* SCALE_VS */
119: "ex", /* SCALE_FS */
120: };
1.5 schwarze 121:
1.26 schwarze 122: static void bufncat(struct html *, const char *, size_t);
123: static void print_ctag(struct html *, enum htmltag);
1.38 schwarze 124: static int print_escape(char);
1.26 schwarze 125: static int print_encode(struct html *, const char *, int);
126: static void print_metaf(struct html *, enum mandoc_esc);
127: static void print_attr(struct html *, const char *, const char *);
1.42 schwarze 128: static void *ml_alloc(char *);
1.5 schwarze 129:
1.35 schwarze 130:
1.6 schwarze 131: static void *
1.42 schwarze 132: ml_alloc(char *outopts)
1.1 schwarze 133: {
134: struct html *h;
1.29 schwarze 135: const char *toks[5];
1.1 schwarze 136: char *v;
137:
138: toks[0] = "style";
139: toks[1] = "man";
140: toks[2] = "includes";
1.29 schwarze 141: toks[3] = "fragment";
142: toks[4] = NULL;
1.1 schwarze 143:
1.24 schwarze 144: h = mandoc_calloc(1, sizeof(struct html));
1.1 schwarze 145:
1.2 schwarze 146: h->tags.head = NULL;
1.26 schwarze 147: h->symtab = mchars_alloc();
1.1 schwarze 148:
149: while (outopts && *outopts)
150: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.35 schwarze 151: case 0:
1.1 schwarze 152: h->style = v;
153: break;
1.35 schwarze 154: case 1:
1.1 schwarze 155: h->base_man = v;
156: break;
1.35 schwarze 157: case 2:
1.1 schwarze 158: h->base_includes = v;
1.29 schwarze 159: break;
1.35 schwarze 160: case 3:
1.29 schwarze 161: h->oflags |= HTML_FRAGMENT;
1.1 schwarze 162: break;
163: default:
164: break;
165: }
166:
167: return(h);
168: }
169:
1.6 schwarze 170: void *
171: html_alloc(char *outopts)
172: {
173:
1.42 schwarze 174: return(ml_alloc(outopts));
1.6 schwarze 175: }
176:
177: void *
178: xhtml_alloc(char *outopts)
179: {
180:
1.42 schwarze 181: return(ml_alloc(outopts));
1.6 schwarze 182: }
183:
1.1 schwarze 184: void
185: html_free(void *p)
186: {
187: struct tag *tag;
188: struct html *h;
189:
190: h = (struct html *)p;
191:
1.2 schwarze 192: while ((tag = h->tags.head) != NULL) {
1.35 schwarze 193: h->tags.head = tag->next;
1.1 schwarze 194: free(tag);
195: }
1.35 schwarze 196:
1.1 schwarze 197: if (h->symtab)
1.26 schwarze 198: mchars_free(h->symtab);
1.1 schwarze 199:
200: free(h);
201: }
202:
203: void
204: print_gen_head(struct html *h)
205: {
206: struct htmlpair tag[4];
1.42 schwarze 207: struct tag *t;
208:
209: tag[0].key = ATTR_CHARSET;
210: tag[0].val = "utf-8";
211: print_otag(h, TAG_META, 1, tag);
1.1 schwarze 212:
1.42 schwarze 213: /*
214: * Print a default style-sheet.
215: */
216: t = print_otag(h, TAG_STYLE, 0, NULL);
217: print_text(h, "table.head, table.foot { width: 100%; }\n"
218: "td.head-rtitle, td.foot-os { text-align: right; }\n"
219: "td.head-vol { text-align: center; }\n"
220: "table.foot td { width: 50%; }\n"
221: "table.head td { width: 33%; }\n"
222: "div.spacer { margin: 1em 0; }\n");
223: print_tagq(h, t);
1.1 schwarze 224:
225: if (h->style) {
226: tag[0].key = ATTR_REL;
227: tag[0].val = "stylesheet";
228: tag[1].key = ATTR_HREF;
229: tag[1].val = h->style;
230: tag[2].key = ATTR_TYPE;
231: tag[2].val = "text/css";
232: tag[3].key = ATTR_MEDIA;
233: tag[3].val = "all";
234: print_otag(h, TAG_LINK, 4, tag);
235: }
236: }
237:
1.5 schwarze 238: static void
1.26 schwarze 239: print_metaf(struct html *h, enum mandoc_esc deco)
1.5 schwarze 240: {
241: enum htmlfont font;
1.1 schwarze 242:
1.5 schwarze 243: switch (deco) {
1.35 schwarze 244: case ESCAPE_FONTPREV:
1.5 schwarze 245: font = h->metal;
246: break;
1.35 schwarze 247: case ESCAPE_FONTITALIC:
1.5 schwarze 248: font = HTMLFONT_ITALIC;
249: break;
1.35 schwarze 250: case ESCAPE_FONTBOLD:
1.5 schwarze 251: font = HTMLFONT_BOLD;
252: break;
1.35 schwarze 253: case ESCAPE_FONTBI:
1.31 schwarze 254: font = HTMLFONT_BI;
255: break;
1.35 schwarze 256: case ESCAPE_FONT:
1.26 schwarze 257: /* FALLTHROUGH */
1.35 schwarze 258: case ESCAPE_FONTROMAN:
1.5 schwarze 259: font = HTMLFONT_NONE;
260: break;
261: default:
262: abort();
263: /* NOTREACHED */
1.1 schwarze 264: }
265:
1.20 schwarze 266: if (h->metaf) {
267: print_tagq(h, h->metaf);
268: h->metaf = NULL;
269: }
270:
271: h->metal = h->metac;
272: h->metac = font;
273:
1.31 schwarze 274: switch (font) {
1.35 schwarze 275: case HTMLFONT_ITALIC:
1.31 schwarze 276: h->metaf = print_otag(h, TAG_I, 0, NULL);
277: break;
1.35 schwarze 278: case HTMLFONT_BOLD:
1.31 schwarze 279: h->metaf = print_otag(h, TAG_B, 0, NULL);
280: break;
1.35 schwarze 281: case HTMLFONT_BI:
1.31 schwarze 282: h->metaf = print_otag(h, TAG_B, 0, NULL);
283: print_otag(h, TAG_I, 0, NULL);
284: break;
285: default:
286: break;
287: }
1.1 schwarze 288: }
289:
1.26 schwarze 290: int
291: html_strlen(const char *cp)
292: {
1.30 schwarze 293: size_t rsz;
294: int skip, sz;
1.26 schwarze 295:
296: /*
297: * Account for escaped sequences within string length
298: * calculations. This follows the logic in term_strlen() as we
299: * must calculate the width of produced strings.
300: * Assume that characters are always width of "1". This is
301: * hacky, but it gets the job done for approximation of widths.
302: */
303:
304: sz = 0;
1.30 schwarze 305: skip = 0;
306: while (1) {
307: rsz = strcspn(cp, "\\");
308: if (rsz) {
309: cp += rsz;
310: if (skip) {
311: skip = 0;
312: rsz--;
313: }
314: sz += rsz;
315: }
316: if ('\0' == *cp)
317: break;
318: cp++;
319: switch (mandoc_escape(&cp, NULL, NULL)) {
1.35 schwarze 320: case ESCAPE_ERROR:
1.26 schwarze 321: return(sz);
1.35 schwarze 322: case ESCAPE_UNICODE:
1.26 schwarze 323: /* FALLTHROUGH */
1.35 schwarze 324: case ESCAPE_NUMBERED:
1.26 schwarze 325: /* FALLTHROUGH */
1.35 schwarze 326: case ESCAPE_SPECIAL:
1.30 schwarze 327: if (skip)
328: skip = 0;
329: else
330: sz++;
331: break;
1.35 schwarze 332: case ESCAPE_SKIPCHAR:
1.30 schwarze 333: skip = 1;
1.26 schwarze 334: break;
335: default:
336: break;
337: }
338: }
1.30 schwarze 339: return(sz);
1.26 schwarze 340: }
1.1 schwarze 341:
1.5 schwarze 342: static int
1.38 schwarze 343: print_escape(char c)
344: {
345:
346: switch (c) {
347: case '<':
348: printf("<");
349: break;
350: case '>':
351: printf(">");
352: break;
353: case '&':
354: printf("&");
355: break;
356: case '"':
357: printf(""");
358: break;
359: case ASCII_NBRSP:
360: putchar('-');
361: break;
362: case ASCII_HYPH:
363: putchar('-');
364: /* FALLTHROUGH */
365: case ASCII_BREAK:
366: break;
367: default:
368: return(0);
369: }
370: return(1);
371: }
372:
373: static int
1.5 schwarze 374: print_encode(struct html *h, const char *p, int norecurse)
1.1 schwarze 375: {
1.4 schwarze 376: size_t sz;
1.26 schwarze 377: int c, len, nospace;
1.5 schwarze 378: const char *seq;
1.26 schwarze 379: enum mandoc_esc esc;
1.37 schwarze 380: static const char rejs[9] = { '\\', '<', '>', '&', '"',
1.33 schwarze 381: ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5 schwarze 382:
383: nospace = 0;
1.1 schwarze 384:
1.26 schwarze 385: while ('\0' != *p) {
1.30 schwarze 386: if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
387: h->flags &= ~HTML_SKIPCHAR;
388: p++;
389: continue;
390: }
391:
1.9 schwarze 392: sz = strcspn(p, rejs);
1.4 schwarze 393:
394: fwrite(p, 1, sz, stdout);
1.26 schwarze 395: p += (int)sz;
1.4 schwarze 396:
1.26 schwarze 397: if ('\0' == *p)
398: break;
399:
1.38 schwarze 400: if (print_escape(*p++))
1.33 schwarze 401: continue;
1.4 schwarze 402:
1.26 schwarze 403: esc = mandoc_escape(&p, &seq, &len);
404: if (ESCAPE_ERROR == esc)
405: break;
1.5 schwarze 406:
1.26 schwarze 407: switch (esc) {
1.35 schwarze 408: case ESCAPE_FONT:
1.30 schwarze 409: /* FALLTHROUGH */
1.35 schwarze 410: case ESCAPE_FONTPREV:
1.30 schwarze 411: /* FALLTHROUGH */
1.35 schwarze 412: case ESCAPE_FONTBOLD:
1.30 schwarze 413: /* FALLTHROUGH */
1.35 schwarze 414: case ESCAPE_FONTITALIC:
1.30 schwarze 415: /* FALLTHROUGH */
1.35 schwarze 416: case ESCAPE_FONTBI:
1.31 schwarze 417: /* FALLTHROUGH */
1.35 schwarze 418: case ESCAPE_FONTROMAN:
1.30 schwarze 419: if (0 == norecurse)
420: print_metaf(h, esc);
421: continue;
1.35 schwarze 422: case ESCAPE_SKIPCHAR:
1.30 schwarze 423: h->flags |= HTML_SKIPCHAR;
424: continue;
425: default:
426: break;
427: }
428:
429: if (h->flags & HTML_SKIPCHAR) {
430: h->flags &= ~HTML_SKIPCHAR;
431: continue;
432: }
433:
434: switch (esc) {
1.35 schwarze 435: case ESCAPE_UNICODE:
1.38 schwarze 436: /* Skip past "u" header. */
1.26 schwarze 437: c = mchars_num2uc(seq + 1, len - 1);
1.47 ! schwarze 438:
! 439: /*
! 440: * XXX Security warning:
! 441: * For now, forbid Unicode obfuscation of ASCII
! 442: * characters. An audit of the callers is
! 443: * required before this can be removed.
! 444: */
! 445:
! 446: if (c < 0x80)
! 447: c = 0xFFFD;
! 448:
! 449: printf("&#x%x;", c);
1.26 schwarze 450: break;
1.35 schwarze 451: case ESCAPE_NUMBERED:
1.26 schwarze 452: c = mchars_num2char(seq, len);
1.38 schwarze 453: if ( ! ('\0' == c || print_escape(c)))
1.26 schwarze 454: putchar(c);
455: break;
1.35 schwarze 456: case ESCAPE_SPECIAL:
1.26 schwarze 457: c = mchars_spec2cp(h->symtab, seq, len);
458: if (c > 0)
459: printf("&#%d;", c);
1.38 schwarze 460: else if (-1 == c && 1 == len &&
461: !print_escape(*seq))
1.26 schwarze 462: putchar((int)*seq);
463: break;
1.35 schwarze 464: case ESCAPE_NOSPACE:
1.26 schwarze 465: if ('\0' == *p)
466: nospace = 1;
1.5 schwarze 467: break;
468: default:
469: break;
470: }
1.1 schwarze 471: }
1.5 schwarze 472:
473: return(nospace);
1.1 schwarze 474: }
475:
1.6 schwarze 476: static void
477: print_attr(struct html *h, const char *key, const char *val)
478: {
479: printf(" %s=\"", key);
480: (void)print_encode(h, val, 1);
481: putchar('\"');
482: }
483:
1.1 schwarze 484: struct tag *
1.35 schwarze 485: print_otag(struct html *h, enum htmltag tag,
1.1 schwarze 486: int sz, const struct htmlpair *p)
487: {
488: int i;
489: struct tag *t;
490:
1.6 schwarze 491: /* Push this tags onto the stack of open scopes. */
492:
1.1 schwarze 493: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.24 schwarze 494: t = mandoc_malloc(sizeof(struct tag));
1.1 schwarze 495: t->tag = tag;
1.2 schwarze 496: t->next = h->tags.head;
497: h->tags.head = t;
1.1 schwarze 498: } else
499: t = NULL;
500:
501: if ( ! (HTML_NOSPACE & h->flags))
1.12 schwarze 502: if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
503: /* Manage keeps! */
504: if ( ! (HTML_KEEP & h->flags)) {
505: if (HTML_PREKEEP & h->flags)
506: h->flags |= HTML_KEEP;
507: putchar(' ');
508: } else
509: printf(" ");
510: }
1.1 schwarze 511:
1.13 schwarze 512: if ( ! (h->flags & HTML_NONOSPACE))
513: h->flags &= ~HTML_NOSPACE;
1.14 schwarze 514: else
515: h->flags |= HTML_NOSPACE;
1.13 schwarze 516:
1.6 schwarze 517: /* Print out the tag name and attributes. */
518:
1.1 schwarze 519: printf("<%s", htmltags[tag].name);
1.6 schwarze 520: for (i = 0; i < sz; i++)
521: print_attr(h, htmlattrs[p[i].key], p[i].val);
522:
1.42 schwarze 523: /* Accommodate for "well-formed" singleton escaping. */
1.6 schwarze 524:
525: if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.42 schwarze 526: putchar('/');
1.6 schwarze 527:
1.4 schwarze 528: putchar('>');
1.1 schwarze 529:
530: h->flags |= HTML_NOSPACE;
1.18 schwarze 531:
532: if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
533: putchar('\n');
534:
1.1 schwarze 535: return(t);
536: }
537:
538: static void
539: print_ctag(struct html *h, enum htmltag tag)
540: {
1.35 schwarze 541:
1.1 schwarze 542: printf("</%s>", htmltags[tag].name);
1.3 schwarze 543: if (HTML_CLRLINE & htmltags[tag].flags) {
1.1 schwarze 544: h->flags |= HTML_NOSPACE;
1.4 schwarze 545: putchar('\n');
1.35 schwarze 546: }
1.1 schwarze 547: }
548:
549: void
1.6 schwarze 550: print_gen_decls(struct html *h)
551: {
552:
1.42 schwarze 553: puts("<!DOCTYPE html>");
1.1 schwarze 554: }
555:
556: void
1.12 schwarze 557: print_text(struct html *h, const char *word)
1.1 schwarze 558: {
559:
1.12 schwarze 560: if ( ! (HTML_NOSPACE & h->flags)) {
561: /* Manage keeps! */
562: if ( ! (HTML_KEEP & h->flags)) {
563: if (HTML_PREKEEP & h->flags)
564: h->flags |= HTML_KEEP;
565: putchar(' ');
566: } else
567: printf(" ");
568: }
1.1 schwarze 569:
1.20 schwarze 570: assert(NULL == h->metaf);
1.31 schwarze 571: switch (h->metac) {
1.35 schwarze 572: case HTMLFONT_ITALIC:
1.31 schwarze 573: h->metaf = print_otag(h, TAG_I, 0, NULL);
574: break;
1.35 schwarze 575: case HTMLFONT_BOLD:
1.31 schwarze 576: h->metaf = print_otag(h, TAG_B, 0, NULL);
577: break;
1.35 schwarze 578: case HTMLFONT_BI:
1.31 schwarze 579: h->metaf = print_otag(h, TAG_B, 0, NULL);
580: print_otag(h, TAG_I, 0, NULL);
581: break;
582: default:
583: break;
584: }
1.20 schwarze 585:
1.12 schwarze 586: assert(word);
1.28 schwarze 587: if ( ! print_encode(h, word, 0)) {
1.13 schwarze 588: if ( ! (h->flags & HTML_NONOSPACE))
589: h->flags &= ~HTML_NOSPACE;
1.28 schwarze 590: } else
591: h->flags |= HTML_NOSPACE;
1.20 schwarze 592:
593: if (h->metaf) {
594: print_tagq(h, h->metaf);
595: h->metaf = NULL;
596: }
1.17 schwarze 597:
598: h->flags &= ~HTML_IGNDELIM;
1.1 schwarze 599: }
600:
601: void
602: print_tagq(struct html *h, const struct tag *until)
603: {
604: struct tag *tag;
605:
1.2 schwarze 606: while ((tag = h->tags.head) != NULL) {
1.35 schwarze 607: /*
1.22 schwarze 608: * Remember to close out and nullify the current
609: * meta-font and table, if applicable.
610: */
1.5 schwarze 611: if (tag == h->metaf)
612: h->metaf = NULL;
1.22 schwarze 613: if (tag == h->tblt)
614: h->tblt = NULL;
1.1 schwarze 615: print_ctag(h, tag->tag);
1.2 schwarze 616: h->tags.head = tag->next;
1.1 schwarze 617: free(tag);
618: if (until && tag == until)
619: return;
620: }
621: }
622:
623: void
624: print_stagq(struct html *h, const struct tag *suntil)
625: {
626: struct tag *tag;
627:
1.2 schwarze 628: while ((tag = h->tags.head) != NULL) {
1.1 schwarze 629: if (suntil && tag == suntil)
630: return;
1.35 schwarze 631: /*
1.22 schwarze 632: * Remember to close out and nullify the current
633: * meta-font and table, if applicable.
634: */
1.5 schwarze 635: if (tag == h->metaf)
636: h->metaf = NULL;
1.22 schwarze 637: if (tag == h->tblt)
638: h->tblt = NULL;
1.1 schwarze 639: print_ctag(h, tag->tag);
1.2 schwarze 640: h->tags.head = tag->next;
1.1 schwarze 641: free(tag);
642: }
643: }
1.42 schwarze 644:
645: void
646: print_paragraph(struct html *h)
647: {
648: struct tag *t;
649: struct htmlpair tag;
650:
651: PAIR_CLASS_INIT(&tag, "spacer");
652: t = print_otag(h, TAG_DIV, 1, &tag);
653: print_tagq(h, t);
654: }
655:
1.1 schwarze 656:
657: void
658: bufinit(struct html *h)
659: {
660:
661: h->buf[0] = '\0';
662: h->buflen = 0;
663: }
664:
665: void
666: bufcat_style(struct html *h, const char *key, const char *val)
667: {
668:
669: bufcat(h, key);
1.26 schwarze 670: bufcat(h, ":");
1.1 schwarze 671: bufcat(h, val);
1.26 schwarze 672: bufcat(h, ";");
1.1 schwarze 673: }
674:
675: void
676: bufcat(struct html *h, const char *p)
677: {
1.36 schwarze 678:
679: /*
680: * XXX This is broken and not easy to fix.
681: * When using the -Oincludes option, buffmt_includes()
682: * may pass in strings overrunning BUFSIZ, causing a crash.
683: */
1.1 schwarze 684:
1.26 schwarze 685: h->buflen = strlcat(h->buf, p, BUFSIZ);
686: assert(h->buflen < BUFSIZ);
1.1 schwarze 687: }
688:
689: void
1.26 schwarze 690: bufcat_fmt(struct html *h, const char *fmt, ...)
1.1 schwarze 691: {
692: va_list ap;
693:
694: va_start(ap, fmt);
1.35 schwarze 695: (void)vsnprintf(h->buf + (int)h->buflen,
696: BUFSIZ - h->buflen - 1, fmt, ap);
1.1 schwarze 697: va_end(ap);
698: h->buflen = strlen(h->buf);
699: }
700:
1.26 schwarze 701: static void
1.1 schwarze 702: bufncat(struct html *h, const char *p, size_t sz)
703: {
704:
1.26 schwarze 705: assert(h->buflen + sz + 1 < BUFSIZ);
706: strncat(h->buf, p, sz);
1.1 schwarze 707: h->buflen += sz;
708: }
709:
710: void
711: buffmt_includes(struct html *h, const char *name)
712: {
713: const char *p, *pp;
714:
715: pp = h->base_includes;
1.35 schwarze 716:
1.26 schwarze 717: bufinit(h);
1.1 schwarze 718: while (NULL != (p = strchr(pp, '%'))) {
719: bufncat(h, pp, (size_t)(p - pp));
720: switch (*(p + 1)) {
1.35 schwarze 721: case'I':
1.1 schwarze 722: bufcat(h, name);
723: break;
724: default:
725: bufncat(h, p, 2);
726: break;
727: }
728: pp = p + 2;
729: }
730: if (pp)
731: bufcat(h, pp);
732: }
733:
734: void
1.35 schwarze 735: buffmt_man(struct html *h, const char *name, const char *sec)
1.1 schwarze 736: {
737: const char *p, *pp;
738:
739: pp = h->base_man;
1.35 schwarze 740:
1.26 schwarze 741: bufinit(h);
1.1 schwarze 742: while (NULL != (p = strchr(pp, '%'))) {
743: bufncat(h, pp, (size_t)(p - pp));
744: switch (*(p + 1)) {
1.35 schwarze 745: case 'S':
1.1 schwarze 746: bufcat(h, sec ? sec : "1");
747: break;
1.35 schwarze 748: case 'N':
1.32 schwarze 749: bufcat_fmt(h, "%s", name);
1.1 schwarze 750: break;
751: default:
752: bufncat(h, p, 2);
753: break;
754: }
755: pp = p + 2;
756: }
757: if (pp)
758: bufcat(h, pp);
759: }
760:
761: void
762: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
763: {
764: double v;
765:
766: v = su->scale;
1.26 schwarze 767: if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
768: v = 1.0;
1.40 schwarze 769: else if (SCALE_BU == su->unit)
770: v /= 24.0;
1.1 schwarze 771:
1.26 schwarze 772: bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
1.1 schwarze 773: }
774:
1.3 schwarze 775: void
1.26 schwarze 776: bufcat_id(struct html *h, const char *src)
1.3 schwarze 777: {
778:
779: /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
780:
1.26 schwarze 781: while ('\0' != *src)
782: bufcat_fmt(h, "%.2x", *src++);
1.3 schwarze 783: }