]> git.cameronkatri.com Git - mandoc.git/blob - html.c
Using perror() instead of fprintf for failure from library functions.
[mandoc.git] / html.c
1 /* $Id: html.c,v 1.75 2009/10/31 06:10:57 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
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.
8 *
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.
16 */
17 #include <sys/types.h>
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "out.h"
29 #include "chars.h"
30 #include "html.h"
31 #include "main.h"
32
33 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
34
35 #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
36 #define DTD "http://www.w3.org/TR/html4/strict.dtd"
37
38 struct htmldata {
39 const char *name;
40 int flags;
41 #define HTML_CLRLINE (1 << 0)
42 #define HTML_NOSTACK (1 << 1)
43 };
44
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 */
67 };
68
69 static const char *const htmlattrs[ATTR_MAX] = {
70 "http-equiv",
71 "content",
72 "name",
73 "rel",
74 "href",
75 "type",
76 "media",
77 "class",
78 "style",
79 "width",
80 "valign",
81 "target",
82 "id",
83 "summary",
84 };
85
86 #ifdef __linux__
87 extern int getsubopt(char **, char * const *, char **);
88 #endif
89
90 void *
91 html_alloc(char *outopts)
92 {
93 struct html *h;
94 const char *toks[4];
95 char *v;
96
97 toks[0] = "style";
98 toks[1] = "man";
99 toks[2] = "includes";
100 toks[3] = NULL;
101
102 h = calloc(1, sizeof(struct html));
103 if (NULL == h) {
104 perror(NULL);
105 exit(EXIT_FAILURE);
106 }
107
108 h->tags.head = NULL;
109 h->ords.head = NULL;
110 h->symtab = chars_init(CHARS_HTML);
111
112 while (outopts && *outopts)
113 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
114 case (0):
115 h->style = v;
116 break;
117 case (1):
118 h->base_man = v;
119 break;
120 case (2):
121 h->base_includes = v;
122 break;
123 default:
124 break;
125 }
126
127 return(h);
128 }
129
130
131 void
132 html_free(void *p)
133 {
134 struct tag *tag;
135 struct ord *ord;
136 struct html *h;
137
138 h = (struct html *)p;
139
140 while ((ord = h->ords.head) != NULL) {
141 h->ords.head = ord->next;
142 free(ord);
143 }
144
145 while ((tag = h->tags.head) != NULL) {
146 h->tags.head = tag->next;
147 free(tag);
148 }
149
150 if (h->symtab)
151 chars_free(h->symtab);
152
153 free(h);
154 }
155
156
157 void
158 print_gen_head(struct html *h)
159 {
160 struct htmlpair tag[4];
161
162 tag[0].key = ATTR_HTTPEQUIV;
163 tag[0].val = "Content-Type";
164 tag[1].key = ATTR_CONTENT;
165 tag[1].val = "text/html; charset=utf-8";
166 print_otag(h, TAG_META, 2, tag);
167
168 tag[0].key = ATTR_NAME;
169 tag[0].val = "resource-type";
170 tag[1].key = ATTR_CONTENT;
171 tag[1].val = "document";
172 print_otag(h, TAG_META, 2, tag);
173
174 if (h->style) {
175 tag[0].key = ATTR_REL;
176 tag[0].val = "stylesheet";
177 tag[1].key = ATTR_HREF;
178 tag[1].val = h->style;
179 tag[2].key = ATTR_TYPE;
180 tag[2].val = "text/css";
181 tag[3].key = ATTR_MEDIA;
182 tag[3].val = "all";
183 print_otag(h, TAG_LINK, 4, tag);
184 }
185 }
186
187
188 static void
189 print_spec(struct html *h, const char *p, int len)
190 {
191 const char *rhs;
192 int i;
193 size_t sz;
194
195 rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
196
197 if (NULL == rhs)
198 return;
199 for (i = 0; i < (int)sz; i++)
200 putchar(rhs[i]);
201 }
202
203
204 static void
205 print_res(struct html *h, const char *p, int len)
206 {
207 const char *rhs;
208 int i;
209 size_t sz;
210
211 rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
212
213 if (NULL == rhs)
214 return;
215 for (i = 0; i < (int)sz; i++)
216 putchar(rhs[i]);
217 }
218
219
220 static void
221 print_escape(struct html *h, const char **p)
222 {
223 int j, type;
224 const char *wp;
225
226 wp = *p;
227 type = 1;
228
229 if (0 == *(++wp)) {
230 *p = wp;
231 return;
232 }
233
234 if ('(' == *wp) {
235 wp++;
236 if (0 == *wp || 0 == *(wp + 1)) {
237 *p = 0 == *wp ? wp : wp + 1;
238 return;
239 }
240
241 print_spec(h, wp, 2);
242 *p = ++wp;
243 return;
244
245 } else if ('*' == *wp) {
246 if (0 == *(++wp)) {
247 *p = wp;
248 return;
249 }
250
251 switch (*wp) {
252 case ('('):
253 wp++;
254 if (0 == *wp || 0 == *(wp + 1)) {
255 *p = 0 == *wp ? wp : wp + 1;
256 return;
257 }
258
259 print_res(h, wp, 2);
260 *p = ++wp;
261 return;
262 case ('['):
263 type = 0;
264 break;
265 default:
266 print_res(h, wp, 1);
267 *p = wp;
268 return;
269 }
270
271 } else if ('f' == *wp) {
272 if (0 == *(++wp)) {
273 *p = wp;
274 return;
275 }
276
277 switch (*wp) {
278 case ('B'):
279 /* TODO */
280 break;
281 case ('I'):
282 /* TODO */
283 break;
284 case ('P'):
285 /* FALLTHROUGH */
286 case ('R'):
287 /* TODO */
288 break;
289 default:
290 break;
291 }
292
293 *p = wp;
294 return;
295
296 } else if ('[' != *wp) {
297 print_spec(h, wp, 1);
298 *p = wp;
299 return;
300 }
301
302 wp++;
303 for (j = 0; *wp && ']' != *wp; wp++, j++)
304 /* Loop... */ ;
305
306 if (0 == *wp) {
307 *p = wp;
308 return;
309 }
310
311 if (type)
312 print_spec(h, wp - j, j);
313 else
314 print_res(h, wp - j, j);
315
316 *p = wp;
317 }
318
319
320 static void
321 print_encode(struct html *h, const char *p)
322 {
323
324 for (; *p; p++) {
325 if ('\\' == *p) {
326 print_escape(h, &p);
327 continue;
328 }
329 switch (*p) {
330 case ('<'):
331 printf("&lt;");
332 break;
333 case ('>'):
334 printf("&gt;");
335 break;
336 case ('&'):
337 printf("&amp;");
338 break;
339 default:
340 putchar(*p);
341 break;
342 }
343 }
344 }
345
346
347 struct tag *
348 print_otag(struct html *h, enum htmltag tag,
349 int sz, const struct htmlpair *p)
350 {
351 int i;
352 struct tag *t;
353
354 if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
355 t = malloc(sizeof(struct tag));
356 if (NULL == t) {
357 perror(NULL);
358 exit(EXIT_FAILURE);
359 }
360 t->tag = tag;
361 t->next = h->tags.head;
362 h->tags.head = t;
363 } else
364 t = NULL;
365
366 if ( ! (HTML_NOSPACE & h->flags))
367 if ( ! (HTML_CLRLINE & htmltags[tag].flags))
368 printf(" ");
369
370 printf("<%s", htmltags[tag].name);
371 for (i = 0; i < sz; i++) {
372 printf(" %s=\"", htmlattrs[p[i].key]);
373 assert(p->val);
374 print_encode(h, p[i].val);
375 printf("\"");
376 }
377 printf(">");
378
379 h->flags |= HTML_NOSPACE;
380 if (HTML_CLRLINE & htmltags[tag].flags)
381 h->flags |= HTML_NEWLINE;
382 else
383 h->flags &= ~HTML_NEWLINE;
384
385 return(t);
386 }
387
388
389 /* ARGSUSED */
390 static void
391 print_ctag(struct html *h, enum htmltag tag)
392 {
393
394 printf("</%s>", htmltags[tag].name);
395 if (HTML_CLRLINE & htmltags[tag].flags) {
396 h->flags |= HTML_NOSPACE;
397 h->flags |= HTML_NEWLINE;
398 printf("\n");
399 } else
400 h->flags &= ~HTML_NEWLINE;
401 }
402
403
404 /* ARGSUSED */
405 void
406 print_gen_doctype(struct html *h)
407 {
408
409 printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE, DTD);
410 }
411
412
413 void
414 print_text(struct html *h, const char *p)
415 {
416
417 if (*p && 0 == *(p + 1))
418 switch (*p) {
419 case('.'):
420 /* FALLTHROUGH */
421 case(','):
422 /* FALLTHROUGH */
423 case(';'):
424 /* FALLTHROUGH */
425 case(':'):
426 /* FALLTHROUGH */
427 case('?'):
428 /* FALLTHROUGH */
429 case('!'):
430 /* FALLTHROUGH */
431 case(')'):
432 /* FALLTHROUGH */
433 case(']'):
434 /* FALLTHROUGH */
435 case('}'):
436 if ( ! (HTML_IGNDELIM & h->flags))
437 h->flags |= HTML_NOSPACE;
438 break;
439 default:
440 break;
441 }
442
443 if ( ! (h->flags & HTML_NOSPACE))
444 printf(" ");
445
446 h->flags &= ~HTML_NOSPACE;
447 h->flags &= ~HTML_NEWLINE;
448
449 if (p)
450 print_encode(h, p);
451
452 if (*p && 0 == *(p + 1))
453 switch (*p) {
454 case('('):
455 /* FALLTHROUGH */
456 case('['):
457 /* FALLTHROUGH */
458 case('{'):
459 h->flags |= HTML_NOSPACE;
460 break;
461 default:
462 break;
463 }
464 }
465
466
467 void
468 print_tagq(struct html *h, const struct tag *until)
469 {
470 struct tag *tag;
471
472 while ((tag = h->tags.head) != NULL) {
473 print_ctag(h, tag->tag);
474 h->tags.head = tag->next;
475 free(tag);
476 if (until && tag == until)
477 return;
478 }
479 }
480
481
482 void
483 print_stagq(struct html *h, const struct tag *suntil)
484 {
485 struct tag *tag;
486
487 while ((tag = h->tags.head) != NULL) {
488 if (suntil && tag == suntil)
489 return;
490 print_ctag(h, tag->tag);
491 h->tags.head = tag->next;
492 free(tag);
493 }
494 }
495
496
497 void
498 bufinit(struct html *h)
499 {
500
501 h->buf[0] = '\0';
502 h->buflen = 0;
503 }
504
505
506 void
507 bufcat_style(struct html *h, const char *key, const char *val)
508 {
509
510 bufcat(h, key);
511 bufncat(h, ":", 1);
512 bufcat(h, val);
513 bufncat(h, ";", 1);
514 }
515
516
517 void
518 bufcat(struct html *h, const char *p)
519 {
520
521 bufncat(h, p, strlen(p));
522 }
523
524
525 void
526 buffmt(struct html *h, const char *fmt, ...)
527 {
528 va_list ap;
529
530 va_start(ap, fmt);
531 (void)vsnprintf(h->buf + (int)h->buflen,
532 BUFSIZ - h->buflen - 1, fmt, ap);
533 va_end(ap);
534 h->buflen = strlen(h->buf);
535 }
536
537
538 void
539 bufncat(struct html *h, const char *p, size_t sz)
540 {
541
542 if (h->buflen + sz > BUFSIZ - 1)
543 sz = BUFSIZ - 1 - h->buflen;
544
545 (void)strncat(h->buf, p, sz);
546 h->buflen += sz;
547 }
548
549
550 void
551 buffmt_includes(struct html *h, const char *name)
552 {
553 const char *p, *pp;
554
555 pp = h->base_includes;
556
557 while (NULL != (p = strchr(pp, '%'))) {
558 bufncat(h, pp, (size_t)(p - pp));
559 switch (*(p + 1)) {
560 case('I'):
561 bufcat(h, name);
562 break;
563 default:
564 bufncat(h, p, 2);
565 break;
566 }
567 pp = p + 2;
568 }
569 if (pp)
570 bufcat(h, pp);
571 }
572
573
574 void
575 buffmt_man(struct html *h,
576 const char *name, const char *sec)
577 {
578 const char *p, *pp;
579
580 pp = h->base_man;
581
582 /* LINTED */
583 while (NULL != (p = strchr(pp, '%'))) {
584 bufncat(h, pp, (size_t)(p - pp));
585 switch (*(p + 1)) {
586 case('S'):
587 bufcat(h, sec ? sec : "1");
588 break;
589 case('N'):
590 buffmt(h, name);
591 break;
592 default:
593 bufncat(h, p, 2);
594 break;
595 }
596 pp = p + 2;
597 }
598 if (pp)
599 bufcat(h, pp);
600 }
601
602
603 void
604 bufcat_su(struct html *h, const char *p, const struct roffsu *su)
605 {
606 double v;
607 const char *u;
608
609 v = su->scale;
610
611 switch (su->unit) {
612 case (SCALE_CM):
613 u = "cm";
614 break;
615 case (SCALE_IN):
616 u = "in";
617 break;
618 case (SCALE_PC):
619 u = "pc";
620 break;
621 case (SCALE_PT):
622 u = "pt";
623 break;
624 case (SCALE_EM):
625 u = "em";
626 break;
627 case (SCALE_MM):
628 if (0 == (v /= 100))
629 v = 1;
630 u = "em";
631 break;
632 case (SCALE_EN):
633 u = "ex";
634 break;
635 case (SCALE_BU):
636 u = "ex";
637 break;
638 case (SCALE_VS):
639 u = "em";
640 break;
641 default:
642 u = "ex";
643 break;
644 }
645
646 if (su->pt)
647 buffmt(h, "%s: %f%s;", p, v, u);
648 else
649 /* LINTED */
650 buffmt(h, "%s: %d%s;", p, (int)v, u);
651 }
652
653
654 void
655 html_idcat(char *dst, const char *src, int sz)
656 {
657 int ssz;
658
659 assert(sz);
660
661 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
662
663 for ( ; *dst != '\0' && sz; dst++, sz--)
664 /* Jump to end. */ ;
665
666 assert(sz > 2);
667
668 /* We can't start with a number (bah). */
669
670 *dst++ = 'x';
671 *dst = '\0';
672 sz--;
673
674 for ( ; *src != '\0' && sz > 1; src++) {
675 ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
676 sz -= ssz;
677 dst += ssz;
678 }
679 }