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