Annotation of src/usr.bin/mandoc/html.c, Revision 1.54
1.54 ! schwarze 1: /* $OpenBSD: html.c,v 1.53 2014/12/02 10:07:17 schwarze 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.1 schwarze 31: #include "out.h"
32: #include "html.h"
33: #include "main.h"
34:
35: struct htmldata {
36: const char *name;
37: int flags;
38: #define HTML_CLRLINE (1 << 0)
39: #define HTML_NOSTACK (1 << 1)
1.6 schwarze 40: #define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */
1.1 schwarze 41: };
42:
43: static const struct htmldata htmltags[TAG_MAX] = {
44: {"html", HTML_CLRLINE}, /* TAG_HTML */
45: {"head", HTML_CLRLINE}, /* TAG_HEAD */
46: {"body", HTML_CLRLINE}, /* TAG_BODY */
1.6 schwarze 47: {"meta", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.1 schwarze 48: {"title", HTML_CLRLINE}, /* TAG_TITLE */
49: {"div", HTML_CLRLINE}, /* TAG_DIV */
50: {"h1", 0}, /* TAG_H1 */
51: {"h2", 0}, /* TAG_H2 */
52: {"span", 0}, /* TAG_SPAN */
1.8 schwarze 53: {"link", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.6 schwarze 54: {"br", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.1 schwarze 55: {"a", 0}, /* TAG_A */
56: {"table", HTML_CLRLINE}, /* TAG_TABLE */
1.18 schwarze 57: {"tbody", HTML_CLRLINE}, /* TAG_TBODY */
1.6 schwarze 58: {"col", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.1 schwarze 59: {"tr", HTML_CLRLINE}, /* TAG_TR */
60: {"td", HTML_CLRLINE}, /* TAG_TD */
61: {"li", HTML_CLRLINE}, /* TAG_LI */
62: {"ul", HTML_CLRLINE}, /* TAG_UL */
63: {"ol", HTML_CLRLINE}, /* TAG_OL */
1.18 schwarze 64: {"dl", HTML_CLRLINE}, /* TAG_DL */
65: {"dt", HTML_CLRLINE}, /* TAG_DT */
66: {"dd", HTML_CLRLINE}, /* TAG_DD */
67: {"blockquote", HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
68: {"pre", HTML_CLRLINE }, /* TAG_PRE */
1.19 schwarze 69: {"b", 0 }, /* TAG_B */
70: {"i", 0 }, /* TAG_I */
1.20 schwarze 71: {"code", 0 }, /* TAG_CODE */
72: {"small", 0 }, /* TAG_SMALL */
1.42 schwarze 73: {"style", HTML_CLRLINE}, /* TAG_STYLE */
1.43 schwarze 74: {"math", HTML_CLRLINE}, /* TAG_MATH */
75: {"mrow", 0}, /* TAG_MROW */
76: {"mi", 0}, /* TAG_MI */
77: {"mo", 0}, /* TAG_MO */
78: {"msup", 0}, /* TAG_MSUP */
79: {"msub", 0}, /* TAG_MSUB */
80: {"msubsup", 0}, /* TAG_MSUBSUP */
81: {"mfrac", 0}, /* TAG_MFRAC */
82: {"msqrt", 0}, /* TAG_MSQRT */
83: {"mfenced", 0}, /* TAG_MFENCED */
84: {"mtable", 0}, /* TAG_MTABLE */
85: {"mtr", 0}, /* TAG_MTR */
86: {"mtd", 0}, /* TAG_MTD */
1.44 schwarze 87: {"munderover", 0}, /* TAG_MUNDEROVER */
88: {"munder", 0}, /* TAG_MUNDER*/
89: {"mover", 0}, /* TAG_MOVER*/
1.5 schwarze 90: };
91:
92: static const char *const htmlattrs[ATTR_MAX] = {
1.19 schwarze 93: "name", /* ATTR_NAME */
94: "rel", /* ATTR_REL */
95: "href", /* ATTR_HREF */
96: "type", /* ATTR_TYPE */
97: "media", /* ATTR_MEDIA */
98: "class", /* ATTR_CLASS */
99: "style", /* ATTR_STYLE */
100: "id", /* ATTR_ID */
1.22 schwarze 101: "colspan", /* ATTR_COLSPAN */
1.42 schwarze 102: "charset", /* ATTR_CHARSET */
1.43 schwarze 103: "open", /* ATTR_OPEN */
104: "close", /* ATTR_CLOSE */
1.45 schwarze 105: "mathvariant", /* ATTR_MATHVARIANT */
1.1 schwarze 106: };
107:
1.26 schwarze 108: static const char *const roffscales[SCALE_MAX] = {
109: "cm", /* SCALE_CM */
110: "in", /* SCALE_IN */
111: "pc", /* SCALE_PC */
112: "pt", /* SCALE_PT */
113: "em", /* SCALE_EM */
114: "em", /* SCALE_MM */
115: "ex", /* SCALE_EN */
116: "ex", /* SCALE_BU */
117: "em", /* SCALE_VS */
118: "ex", /* SCALE_FS */
119: };
1.5 schwarze 120:
1.26 schwarze 121: static void bufncat(struct html *, const char *, size_t);
1.54 ! schwarze 122: static void print_ctag(struct html *, struct tag *);
1.38 schwarze 123: static int print_escape(char);
1.26 schwarze 124: static int print_encode(struct html *, const char *, int);
125: static void print_metaf(struct html *, enum mandoc_esc);
126: static void print_attr(struct html *, const char *, const char *);
1.5 schwarze 127:
1.35 schwarze 128:
1.50 schwarze 129: void *
130: html_alloc(const struct mchars *mchars, char *outopts)
1.1 schwarze 131: {
132: struct html *h;
1.29 schwarze 133: const char *toks[5];
1.1 schwarze 134: char *v;
135:
136: toks[0] = "style";
137: toks[1] = "man";
138: toks[2] = "includes";
1.29 schwarze 139: toks[3] = "fragment";
140: toks[4] = NULL;
1.1 schwarze 141:
1.24 schwarze 142: h = mandoc_calloc(1, sizeof(struct html));
1.1 schwarze 143:
1.2 schwarze 144: h->tags.head = NULL;
1.50 schwarze 145: h->symtab = mchars;
1.1 schwarze 146:
147: while (outopts && *outopts)
148: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.35 schwarze 149: case 0:
1.1 schwarze 150: h->style = v;
151: break;
1.35 schwarze 152: case 1:
1.1 schwarze 153: h->base_man = v;
154: break;
1.35 schwarze 155: case 2:
1.1 schwarze 156: h->base_includes = v;
1.29 schwarze 157: break;
1.35 schwarze 158: case 3:
1.29 schwarze 159: h->oflags |= HTML_FRAGMENT;
1.1 schwarze 160: break;
161: default:
162: break;
163: }
164:
165: return(h);
166: }
167:
168: void
169: html_free(void *p)
170: {
171: struct tag *tag;
172: struct html *h;
173:
174: h = (struct html *)p;
175:
1.2 schwarze 176: while ((tag = h->tags.head) != NULL) {
1.35 schwarze 177: h->tags.head = tag->next;
1.1 schwarze 178: free(tag);
179: }
180:
181: free(h);
182: }
183:
184: void
185: print_gen_head(struct html *h)
186: {
187: struct htmlpair tag[4];
1.42 schwarze 188: struct tag *t;
189:
190: tag[0].key = ATTR_CHARSET;
191: tag[0].val = "utf-8";
192: print_otag(h, TAG_META, 1, tag);
1.1 schwarze 193:
1.42 schwarze 194: /*
195: * Print a default style-sheet.
196: */
197: t = print_otag(h, TAG_STYLE, 0, NULL);
198: print_text(h, "table.head, table.foot { width: 100%; }\n"
199: "td.head-rtitle, td.foot-os { text-align: right; }\n"
200: "td.head-vol { text-align: center; }\n"
201: "table.foot td { width: 50%; }\n"
202: "table.head td { width: 33%; }\n"
203: "div.spacer { margin: 1em 0; }\n");
204: print_tagq(h, t);
1.1 schwarze 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: break;
1.35 schwarze 420: case ESCAPE_NUMBERED:
1.26 schwarze 421: c = mchars_num2char(seq, len);
1.51 schwarze 422: if (c < 0)
423: continue;
1.26 schwarze 424: break;
1.35 schwarze 425: case ESCAPE_SPECIAL:
1.26 schwarze 426: c = mchars_spec2cp(h->symtab, seq, len);
1.51 schwarze 427: if (c <= 0)
428: continue;
1.26 schwarze 429: break;
1.35 schwarze 430: case ESCAPE_NOSPACE:
1.26 schwarze 431: if ('\0' == *p)
432: nospace = 1;
1.49 schwarze 433: continue;
1.5 schwarze 434: default:
1.49 schwarze 435: continue;
1.5 schwarze 436: }
1.51 schwarze 437: if ((c < 0x20 && c != 0x09) ||
438: (c > 0x7E && c < 0xA0))
1.49 schwarze 439: c = 0xFFFD;
440: if (c > 0x7E)
441: printf("&#%d;", c);
442: else if ( ! print_escape(c))
443: putchar(c);
1.1 schwarze 444: }
1.5 schwarze 445:
446: return(nospace);
1.1 schwarze 447: }
448:
1.6 schwarze 449: static void
450: print_attr(struct html *h, const char *key, const char *val)
451: {
452: printf(" %s=\"", key);
453: (void)print_encode(h, val, 1);
454: putchar('\"');
455: }
456:
1.1 schwarze 457: struct tag *
1.35 schwarze 458: print_otag(struct html *h, enum htmltag tag,
1.1 schwarze 459: int sz, const struct htmlpair *p)
460: {
461: int i;
462: struct tag *t;
463:
1.6 schwarze 464: /* Push this tags onto the stack of open scopes. */
465:
1.1 schwarze 466: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.24 schwarze 467: t = mandoc_malloc(sizeof(struct tag));
1.1 schwarze 468: t->tag = tag;
1.2 schwarze 469: t->next = h->tags.head;
470: h->tags.head = t;
1.1 schwarze 471: } else
472: t = NULL;
473:
474: if ( ! (HTML_NOSPACE & h->flags))
1.12 schwarze 475: if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
476: /* Manage keeps! */
477: if ( ! (HTML_KEEP & h->flags)) {
478: if (HTML_PREKEEP & h->flags)
479: h->flags |= HTML_KEEP;
480: putchar(' ');
481: } else
482: printf(" ");
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.1 schwarze 492: printf("<%s", htmltags[tag].name);
1.6 schwarze 493: for (i = 0; i < sz; i++)
494: print_attr(h, htmlattrs[p[i].key], p[i].val);
495:
1.42 schwarze 496: /* Accommodate for "well-formed" singleton escaping. */
1.6 schwarze 497:
498: if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.42 schwarze 499: putchar('/');
1.6 schwarze 500:
1.4 schwarze 501: putchar('>');
1.1 schwarze 502:
503: h->flags |= HTML_NOSPACE;
1.18 schwarze 504:
505: if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
506: putchar('\n');
507:
1.1 schwarze 508: return(t);
509: }
510:
511: static void
1.54 ! schwarze 512: print_ctag(struct html *h, struct tag *tag)
1.1 schwarze 513: {
1.35 schwarze 514:
1.54 ! schwarze 515: /*
! 516: * Remember to close out and nullify the current
! 517: * meta-font and table, if applicable.
! 518: */
! 519: if (tag == h->metaf)
! 520: h->metaf = NULL;
! 521: if (tag == h->tblt)
! 522: h->tblt = NULL;
! 523:
! 524: printf("</%s>", htmltags[tag->tag].name);
! 525: if (HTML_CLRLINE & htmltags[tag->tag].flags) {
1.1 schwarze 526: h->flags |= HTML_NOSPACE;
1.4 schwarze 527: putchar('\n');
1.35 schwarze 528: }
1.54 ! schwarze 529:
! 530: h->tags.head = tag->next;
! 531: free(tag);
1.1 schwarze 532: }
533:
534: void
1.6 schwarze 535: print_gen_decls(struct html *h)
536: {
537:
1.42 schwarze 538: puts("<!DOCTYPE html>");
1.1 schwarze 539: }
540:
541: void
1.12 schwarze 542: print_text(struct html *h, const char *word)
1.1 schwarze 543: {
544:
1.12 schwarze 545: if ( ! (HTML_NOSPACE & h->flags)) {
546: /* Manage keeps! */
547: if ( ! (HTML_KEEP & h->flags)) {
548: if (HTML_PREKEEP & h->flags)
549: h->flags |= HTML_KEEP;
550: putchar(' ');
551: } else
552: printf(" ");
553: }
1.1 schwarze 554:
1.20 schwarze 555: assert(NULL == h->metaf);
1.31 schwarze 556: switch (h->metac) {
1.35 schwarze 557: case HTMLFONT_ITALIC:
1.31 schwarze 558: h->metaf = print_otag(h, TAG_I, 0, NULL);
559: break;
1.35 schwarze 560: case HTMLFONT_BOLD:
1.31 schwarze 561: h->metaf = print_otag(h, TAG_B, 0, NULL);
562: break;
1.35 schwarze 563: case HTMLFONT_BI:
1.31 schwarze 564: h->metaf = print_otag(h, TAG_B, 0, NULL);
565: print_otag(h, TAG_I, 0, NULL);
566: break;
567: default:
568: break;
569: }
1.20 schwarze 570:
1.12 schwarze 571: assert(word);
1.28 schwarze 572: if ( ! print_encode(h, word, 0)) {
1.13 schwarze 573: if ( ! (h->flags & HTML_NONOSPACE))
574: h->flags &= ~HTML_NOSPACE;
1.53 schwarze 575: h->flags &= ~HTML_NONEWLINE;
1.28 schwarze 576: } else
1.53 schwarze 577: h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.20 schwarze 578:
579: if (h->metaf) {
580: print_tagq(h, h->metaf);
581: h->metaf = NULL;
582: }
1.17 schwarze 583:
584: h->flags &= ~HTML_IGNDELIM;
1.1 schwarze 585: }
586:
587: void
588: print_tagq(struct html *h, const struct tag *until)
589: {
590: struct tag *tag;
591:
1.2 schwarze 592: while ((tag = h->tags.head) != NULL) {
1.54 ! schwarze 593: print_ctag(h, tag);
1.1 schwarze 594: if (until && tag == until)
595: return;
596: }
597: }
598:
599: void
600: print_stagq(struct html *h, const struct tag *suntil)
601: {
602: struct tag *tag;
603:
1.2 schwarze 604: while ((tag = h->tags.head) != NULL) {
1.1 schwarze 605: if (suntil && tag == suntil)
606: return;
1.54 ! schwarze 607: print_ctag(h, tag);
1.1 schwarze 608: }
609: }
1.42 schwarze 610:
611: void
612: print_paragraph(struct html *h)
613: {
614: struct tag *t;
615: struct htmlpair tag;
616:
617: PAIR_CLASS_INIT(&tag, "spacer");
618: t = print_otag(h, TAG_DIV, 1, &tag);
619: print_tagq(h, t);
620: }
621:
1.1 schwarze 622:
623: void
624: bufinit(struct html *h)
625: {
626:
627: h->buf[0] = '\0';
628: h->buflen = 0;
629: }
630:
631: void
632: bufcat_style(struct html *h, const char *key, const char *val)
633: {
634:
635: bufcat(h, key);
1.26 schwarze 636: bufcat(h, ":");
1.1 schwarze 637: bufcat(h, val);
1.26 schwarze 638: bufcat(h, ";");
1.1 schwarze 639: }
640:
641: void
642: bufcat(struct html *h, const char *p)
643: {
1.36 schwarze 644:
645: /*
646: * XXX This is broken and not easy to fix.
647: * When using the -Oincludes option, buffmt_includes()
648: * may pass in strings overrunning BUFSIZ, causing a crash.
649: */
1.1 schwarze 650:
1.26 schwarze 651: h->buflen = strlcat(h->buf, p, BUFSIZ);
652: assert(h->buflen < BUFSIZ);
1.1 schwarze 653: }
654:
655: void
1.26 schwarze 656: bufcat_fmt(struct html *h, const char *fmt, ...)
1.1 schwarze 657: {
658: va_list ap;
659:
660: va_start(ap, fmt);
1.35 schwarze 661: (void)vsnprintf(h->buf + (int)h->buflen,
662: BUFSIZ - h->buflen - 1, fmt, ap);
1.1 schwarze 663: va_end(ap);
664: h->buflen = strlen(h->buf);
665: }
666:
1.26 schwarze 667: static void
1.1 schwarze 668: bufncat(struct html *h, const char *p, size_t sz)
669: {
670:
1.26 schwarze 671: assert(h->buflen + sz + 1 < BUFSIZ);
672: strncat(h->buf, p, sz);
1.1 schwarze 673: h->buflen += sz;
674: }
675:
676: void
677: buffmt_includes(struct html *h, const char *name)
678: {
679: const char *p, *pp;
680:
681: pp = h->base_includes;
1.35 schwarze 682:
1.26 schwarze 683: bufinit(h);
1.1 schwarze 684: while (NULL != (p = strchr(pp, '%'))) {
685: bufncat(h, pp, (size_t)(p - pp));
686: switch (*(p + 1)) {
1.35 schwarze 687: case'I':
1.1 schwarze 688: bufcat(h, name);
689: break;
690: default:
691: bufncat(h, p, 2);
692: break;
693: }
694: pp = p + 2;
695: }
696: if (pp)
697: bufcat(h, pp);
698: }
699:
700: void
1.35 schwarze 701: buffmt_man(struct html *h, const char *name, const char *sec)
1.1 schwarze 702: {
703: const char *p, *pp;
704:
705: pp = h->base_man;
1.35 schwarze 706:
1.26 schwarze 707: bufinit(h);
1.1 schwarze 708: while (NULL != (p = strchr(pp, '%'))) {
709: bufncat(h, pp, (size_t)(p - pp));
710: switch (*(p + 1)) {
1.35 schwarze 711: case 'S':
1.1 schwarze 712: bufcat(h, sec ? sec : "1");
713: break;
1.35 schwarze 714: case 'N':
1.32 schwarze 715: bufcat_fmt(h, "%s", name);
1.1 schwarze 716: break;
717: default:
718: bufncat(h, p, 2);
719: break;
720: }
721: pp = p + 2;
722: }
723: if (pp)
724: bufcat(h, pp);
725: }
726:
727: void
728: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
729: {
730: double v;
731:
732: v = su->scale;
1.26 schwarze 733: if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
734: v = 1.0;
1.40 schwarze 735: else if (SCALE_BU == su->unit)
736: v /= 24.0;
1.1 schwarze 737:
1.26 schwarze 738: bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
1.1 schwarze 739: }
740:
1.3 schwarze 741: void
1.26 schwarze 742: bufcat_id(struct html *h, const char *src)
1.3 schwarze 743: {
744:
745: /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
746:
1.26 schwarze 747: while ('\0' != *src)
748: bufcat_fmt(h, "%.2x", *src++);
1.3 schwarze 749: }