kernel - Video - Add suppor for Intel IGD chipsets (netbook / N450 etc)
[dragonfly.git] / usr.bin / mandoc / html.c
blob4e6641560084a845fb48e0f51b9533ee95906ae5
1 /* $Id: html.c,v 1.91 2009/11/16 08:46:58 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "out.h"
29 #include "chars.h"
30 #include "html.h"
31 #include "main.h"
33 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
35 #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
36 #define DTD "http://www.w3.org/TR/html4/strict.dtd"
38 struct htmldata {
39 const char *name;
40 int flags;
41 #define HTML_CLRLINE (1 << 0)
42 #define HTML_NOSTACK (1 << 1)
45 static const struct htmldata htmltags[TAG_MAX] = {
46 {"html", HTML_CLRLINE}, /* TAG_HTML */
47 {"head", HTML_CLRLINE}, /* TAG_HEAD */
48 {"body", HTML_CLRLINE}, /* TAG_BODY */
49 {"meta", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_META */
50 {"title", HTML_CLRLINE}, /* TAG_TITLE */
51 {"div", HTML_CLRLINE}, /* TAG_DIV */
52 {"h1", 0}, /* TAG_H1 */
53 {"h2", 0}, /* TAG_H2 */
54 {"p", HTML_CLRLINE}, /* TAG_P */
55 {"span", 0}, /* TAG_SPAN */
56 {"link", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
57 {"br", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
58 {"a", 0}, /* TAG_A */
59 {"table", HTML_CLRLINE}, /* TAG_TABLE */
60 {"col", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_COL */
61 {"tr", HTML_CLRLINE}, /* TAG_TR */
62 {"td", HTML_CLRLINE}, /* TAG_TD */
63 {"li", HTML_CLRLINE}, /* TAG_LI */
64 {"ul", HTML_CLRLINE}, /* TAG_UL */
65 {"ol", HTML_CLRLINE}, /* TAG_OL */
66 {"base", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_BASE */
69 static const char *const htmlfonts[HTMLFONT_MAX] = {
70 "roman",
71 "bold",
72 "italic"
75 static const char *const htmlattrs[ATTR_MAX] = {
76 "http-equiv",
77 "content",
78 "name",
79 "rel",
80 "href",
81 "type",
82 "media",
83 "class",
84 "style",
85 "width",
86 "valign",
87 "target",
88 "id",
89 "summary",
92 #ifdef __linux__
93 extern int getsubopt(char **, char * const *, char **);
94 #endif
97 static void print_spec(struct html *, const char *, size_t);
98 static void print_res(struct html *, const char *, size_t);
99 static void print_ctag(struct html *, enum htmltag);
100 static int print_encode(struct html *, const char *, int);
101 static void print_metaf(struct html *, enum roffdeco);
104 void *
105 html_alloc(char *outopts)
107 struct html *h;
108 const char *toks[4];
109 char *v;
111 toks[0] = "style";
112 toks[1] = "man";
113 toks[2] = "includes";
114 toks[3] = NULL;
116 h = calloc(1, sizeof(struct html));
117 if (NULL == h) {
118 perror(NULL);
119 exit(EXIT_FAILURE);
122 h->tags.head = NULL;
123 h->ords.head = NULL;
124 h->symtab = chars_init(CHARS_HTML);
126 while (outopts && *outopts)
127 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
128 case (0):
129 h->style = v;
130 break;
131 case (1):
132 h->base_man = v;
133 break;
134 case (2):
135 h->base_includes = v;
136 break;
137 default:
138 break;
141 return(h);
145 void
146 html_free(void *p)
148 struct tag *tag;
149 struct ord *ord;
150 struct html *h;
152 h = (struct html *)p;
154 while ((ord = h->ords.head) != NULL) {
155 h->ords.head = ord->next;
156 free(ord);
159 while ((tag = h->tags.head) != NULL) {
160 h->tags.head = tag->next;
161 free(tag);
164 if (h->symtab)
165 chars_free(h->symtab);
167 free(h);
171 void
172 print_gen_head(struct html *h)
174 struct htmlpair tag[4];
176 tag[0].key = ATTR_HTTPEQUIV;
177 tag[0].val = "Content-Type";
178 tag[1].key = ATTR_CONTENT;
179 tag[1].val = "text/html; charset=utf-8";
180 print_otag(h, TAG_META, 2, tag);
182 tag[0].key = ATTR_NAME;
183 tag[0].val = "resource-type";
184 tag[1].key = ATTR_CONTENT;
185 tag[1].val = "document";
186 print_otag(h, TAG_META, 2, tag);
188 if (h->style) {
189 tag[0].key = ATTR_REL;
190 tag[0].val = "stylesheet";
191 tag[1].key = ATTR_HREF;
192 tag[1].val = h->style;
193 tag[2].key = ATTR_TYPE;
194 tag[2].val = "text/css";
195 tag[3].key = ATTR_MEDIA;
196 tag[3].val = "all";
197 print_otag(h, TAG_LINK, 4, tag);
202 static void
203 print_spec(struct html *h, const char *p, size_t len)
205 const char *rhs;
206 size_t sz;
208 rhs = chars_a2ascii(h->symtab, p, len, &sz);
210 if (NULL == rhs)
211 return;
212 fwrite(rhs, 1, sz, stdout);
216 static void
217 print_res(struct html *h, const char *p, size_t len)
219 const char *rhs;
220 size_t sz;
222 rhs = chars_a2res(h->symtab, p, len, &sz);
224 if (NULL == rhs)
225 return;
226 fwrite(rhs, 1, sz, stdout);
230 struct tag *
231 print_ofont(struct html *h, enum htmlfont font)
233 struct htmlpair tag;
235 h->metal = h->metac;
236 h->metac = font;
238 /* FIXME: DECO_ROMAN should just close out preexisting. */
240 if (h->metaf && h->tags.head == h->metaf)
241 print_tagq(h, h->metaf);
243 PAIR_CLASS_INIT(&tag, htmlfonts[font]);
244 h->metaf = print_otag(h, TAG_SPAN, 1, &tag);
245 return(h->metaf);
249 static void
250 print_metaf(struct html *h, enum roffdeco deco)
252 enum htmlfont font;
254 switch (deco) {
255 case (DECO_PREVIOUS):
256 font = h->metal;
257 break;
258 case (DECO_ITALIC):
259 font = HTMLFONT_ITALIC;
260 break;
261 case (DECO_BOLD):
262 font = HTMLFONT_BOLD;
263 break;
264 case (DECO_ROMAN):
265 font = HTMLFONT_NONE;
266 break;
267 default:
268 abort();
269 /* NOTREACHED */
272 (void)print_ofont(h, font);
276 static int
277 print_encode(struct html *h, const char *p, int norecurse)
279 size_t sz;
280 int len, nospace;
281 const char *seq;
282 enum roffdeco deco;
284 nospace = 0;
286 for (; *p; p++) {
287 sz = strcspn(p, "\\<>&");
289 fwrite(p, 1, sz, stdout);
290 p += /* LINTED */
293 if ('<' == *p) {
294 printf("&lt;");
295 continue;
296 } else if ('>' == *p) {
297 printf("&gt;");
298 continue;
299 } else if ('&' == *p) {
300 printf("&amp;");
301 continue;
302 } else if ('\0' == *p)
303 break;
305 seq = ++p;
306 len = a2roffdeco(&deco, &seq, &sz);
308 switch (deco) {
309 case (DECO_RESERVED):
310 print_res(h, seq, sz);
311 break;
312 case (DECO_SPECIAL):
313 print_spec(h, seq, sz);
314 break;
315 case (DECO_PREVIOUS):
316 /* FALLTHROUGH */
317 case (DECO_BOLD):
318 /* FALLTHROUGH */
319 case (DECO_ITALIC):
320 /* FALLTHROUGH */
321 case (DECO_ROMAN):
322 if (norecurse)
323 break;
324 print_metaf(h, deco);
325 break;
326 default:
327 break;
330 p += len - 1;
332 if (DECO_NOSPACE == deco && '\0' == *(p + 1))
333 nospace = 1;
336 return(nospace);
340 struct tag *
341 print_otag(struct html *h, enum htmltag tag,
342 int sz, const struct htmlpair *p)
344 int i;
345 struct tag *t;
347 if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
348 t = malloc(sizeof(struct tag));
349 if (NULL == t) {
350 perror(NULL);
351 exit(EXIT_FAILURE);
353 t->tag = tag;
354 t->next = h->tags.head;
355 h->tags.head = t;
356 } else
357 t = NULL;
359 if ( ! (HTML_NOSPACE & h->flags))
360 if ( ! (HTML_CLRLINE & htmltags[tag].flags))
361 putchar(' ');
363 printf("<%s", htmltags[tag].name);
364 for (i = 0; i < sz; i++) {
365 printf(" %s=\"", htmlattrs[p[i].key]);
366 assert(p->val);
367 (void)print_encode(h, p[i].val, 1);
368 putchar('\"');
370 putchar('>');
372 h->flags |= HTML_NOSPACE;
373 return(t);
377 static void
378 print_ctag(struct html *h, enum htmltag tag)
381 printf("</%s>", htmltags[tag].name);
382 if (HTML_CLRLINE & htmltags[tag].flags) {
383 h->flags |= HTML_NOSPACE;
384 putchar('\n');
389 /* ARGSUSED */
390 void
391 print_gen_doctype(struct html *h)
394 printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE, DTD);
398 void
399 print_text(struct html *h, const char *p)
402 if (*p && 0 == *(p + 1))
403 switch (*p) {
404 case('.'):
405 /* FALLTHROUGH */
406 case(','):
407 /* FALLTHROUGH */
408 case(';'):
409 /* FALLTHROUGH */
410 case(':'):
411 /* FALLTHROUGH */
412 case('?'):
413 /* FALLTHROUGH */
414 case('!'):
415 /* FALLTHROUGH */
416 case(')'):
417 /* FALLTHROUGH */
418 case(']'):
419 /* FALLTHROUGH */
420 case('}'):
421 if ( ! (HTML_IGNDELIM & h->flags))
422 h->flags |= HTML_NOSPACE;
423 break;
424 default:
425 break;
428 if ( ! (h->flags & HTML_NOSPACE))
429 putchar(' ');
431 assert(p);
432 if ( ! print_encode(h, p, 0))
433 h->flags &= ~HTML_NOSPACE;
435 if (*p && 0 == *(p + 1))
436 switch (*p) {
437 case('('):
438 /* FALLTHROUGH */
439 case('['):
440 /* FALLTHROUGH */
441 case('{'):
442 h->flags |= HTML_NOSPACE;
443 break;
444 default:
445 break;
450 void
451 print_tagq(struct html *h, const struct tag *until)
453 struct tag *tag;
455 while ((tag = h->tags.head) != NULL) {
456 if (tag == h->metaf)
457 h->metaf = NULL;
458 print_ctag(h, tag->tag);
459 h->tags.head = tag->next;
460 free(tag);
461 if (until && tag == until)
462 return;
467 void
468 print_stagq(struct html *h, const struct tag *suntil)
470 struct tag *tag;
472 while ((tag = h->tags.head) != NULL) {
473 if (suntil && tag == suntil)
474 return;
475 if (tag == h->metaf)
476 h->metaf = NULL;
477 print_ctag(h, tag->tag);
478 h->tags.head = tag->next;
479 free(tag);
484 void
485 bufinit(struct html *h)
488 h->buf[0] = '\0';
489 h->buflen = 0;
493 void
494 bufcat_style(struct html *h, const char *key, const char *val)
497 bufcat(h, key);
498 bufncat(h, ":", 1);
499 bufcat(h, val);
500 bufncat(h, ";", 1);
504 void
505 bufcat(struct html *h, const char *p)
508 bufncat(h, p, strlen(p));
512 void
513 buffmt(struct html *h, const char *fmt, ...)
515 va_list ap;
517 va_start(ap, fmt);
518 (void)vsnprintf(h->buf + (int)h->buflen,
519 BUFSIZ - h->buflen - 1, fmt, ap);
520 va_end(ap);
521 h->buflen = strlen(h->buf);
525 void
526 bufncat(struct html *h, const char *p, size_t sz)
529 if (h->buflen + sz > BUFSIZ - 1)
530 sz = BUFSIZ - 1 - h->buflen;
532 (void)strncat(h->buf, p, sz);
533 h->buflen += sz;
537 void
538 buffmt_includes(struct html *h, const char *name)
540 const char *p, *pp;
542 pp = h->base_includes;
544 while (NULL != (p = strchr(pp, '%'))) {
545 bufncat(h, pp, (size_t)(p - pp));
546 switch (*(p + 1)) {
547 case('I'):
548 bufcat(h, name);
549 break;
550 default:
551 bufncat(h, p, 2);
552 break;
554 pp = p + 2;
556 if (pp)
557 bufcat(h, pp);
561 void
562 buffmt_man(struct html *h,
563 const char *name, const char *sec)
565 const char *p, *pp;
567 pp = h->base_man;
569 /* LINTED */
570 while (NULL != (p = strchr(pp, '%'))) {
571 bufncat(h, pp, (size_t)(p - pp));
572 switch (*(p + 1)) {
573 case('S'):
574 bufcat(h, sec ? sec : "1");
575 break;
576 case('N'):
577 buffmt(h, name);
578 break;
579 default:
580 bufncat(h, p, 2);
581 break;
583 pp = p + 2;
585 if (pp)
586 bufcat(h, pp);
590 void
591 bufcat_su(struct html *h, const char *p, const struct roffsu *su)
593 double v;
594 const char *u;
596 v = su->scale;
598 switch (su->unit) {
599 case (SCALE_CM):
600 u = "cm";
601 break;
602 case (SCALE_IN):
603 u = "in";
604 break;
605 case (SCALE_PC):
606 u = "pc";
607 break;
608 case (SCALE_PT):
609 u = "pt";
610 break;
611 case (SCALE_EM):
612 u = "em";
613 break;
614 case (SCALE_MM):
615 if (0 == (v /= 100))
616 v = 1;
617 u = "em";
618 break;
619 case (SCALE_EN):
620 u = "ex";
621 break;
622 case (SCALE_BU):
623 u = "ex";
624 break;
625 case (SCALE_VS):
626 u = "em";
627 break;
628 default:
629 u = "ex";
630 break;
633 if (su->pt)
634 buffmt(h, "%s: %f%s;", p, v, u);
635 else
636 /* LINTED */
637 buffmt(h, "%s: %d%s;", p, (int)v, u);
641 void
642 html_idcat(char *dst, const char *src, int sz)
644 int ssz;
646 assert(sz);
648 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
650 for ( ; *dst != '\0' && sz; dst++, sz--)
651 /* Jump to end. */ ;
653 assert(sz > 2);
655 /* We can't start with a number (bah). */
657 *dst++ = 'x';
658 *dst = '\0';
659 sz--;
661 for ( ; *src != '\0' && sz > 1; src++) {
662 ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
663 sz -= ssz;
664 dst += ssz;