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