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