Annotation of mandoc/html.c, Revision 1.55
1.55 ! kristaps 1: /* $Id: html.c,v 1.54 2009/10/03 15:26:26 kristaps Exp $ */
1.1 kristaps 2: /*
1.29 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.29 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.29 kristaps 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.
1.1 kristaps 16: */
1.41 kristaps 17: #include <sys/types.h>
1.30 kristaps 18: #include <sys/queue.h>
19:
1.1 kristaps 20: #include <assert.h>
1.4 kristaps 21: #include <err.h>
1.29 kristaps 22: #include <stdio.h>
1.55 ! kristaps 23: #include <stdarg.h>
1.1 kristaps 24: #include <stdlib.h>
1.33 kristaps 25: #include <string.h>
1.45 kristaps 26: #include <unistd.h>
1.1 kristaps 27:
1.32 kristaps 28: #include "chars.h"
1.51 kristaps 29: #include "html.h"
1.2 kristaps 30:
1.29 kristaps 31: #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
32: #define DTD "http://www.w3.org/TR/html4/strict.dtd"
1.8 kristaps 33:
1.29 kristaps 34: struct htmldata {
35: char *name;
36: int flags;
1.30 kristaps 37: #define HTML_CLRLINE (1 << 0)
38: #define HTML_NOSTACK (1 << 1)
1.29 kristaps 39: };
1.7 kristaps 40:
1.29 kristaps 41: static const struct htmldata htmltags[TAG_MAX] = {
1.30 kristaps 42: {"html", HTML_CLRLINE}, /* TAG_HTML */
43: {"head", HTML_CLRLINE}, /* TAG_HEAD */
44: {"body", HTML_CLRLINE}, /* TAG_BODY */
45: {"meta", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_META */
1.33 kristaps 46: {"title", HTML_CLRLINE}, /* TAG_TITLE */
1.30 kristaps 47: {"div", HTML_CLRLINE}, /* TAG_DIV */
1.29 kristaps 48: {"h1", 0}, /* TAG_H1 */
49: {"h2", 0}, /* TAG_H2 */
1.30 kristaps 50: {"p", HTML_CLRLINE}, /* TAG_P */
1.29 kristaps 51: {"span", 0}, /* TAG_SPAN */
1.30 kristaps 52: {"link", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
53: {"br", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
54: {"a", 0}, /* TAG_A */
1.33 kristaps 55: {"table", HTML_CLRLINE}, /* TAG_TABLE */
56: {"col", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_COL */
57: {"tr", HTML_CLRLINE}, /* TAG_TR */
58: {"td", HTML_CLRLINE}, /* TAG_TD */
1.34 kristaps 59: {"li", HTML_CLRLINE}, /* TAG_LI */
60: {"ul", HTML_CLRLINE}, /* TAG_UL */
61: {"ol", HTML_CLRLINE}, /* TAG_OL */
1.41 kristaps 62: {"base", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_BASE */
1.29 kristaps 63: };
1.10 kristaps 64:
1.29 kristaps 65: static const char *const htmlattrs[ATTR_MAX] = {
66: "http-equiv",
67: "content",
68: "name",
69: "rel",
70: "href",
71: "type",
72: "media",
1.33 kristaps 73: "class",
74: "style",
75: "width",
76: "valign",
1.54 kristaps 77: "target",
1.29 kristaps 78: };
1.10 kristaps 79:
1.33 kristaps 80: #ifdef __linux__
1.43 kristaps 81: extern int getsubopt(char **, char * const *, char **);
1.33 kristaps 82: #endif
1.29 kristaps 83:
84: void *
1.43 kristaps 85: html_alloc(char *outopts)
1.10 kristaps 86: {
1.30 kristaps 87: struct html *h;
1.53 kristaps 88: char *toks[4], *v;
1.43 kristaps 89:
90: toks[0] = "style";
1.53 kristaps 91: toks[1] = "man";
1.54 kristaps 92: toks[2] = "includes";
93: toks[3] = NULL;
1.30 kristaps 94:
95: if (NULL == (h = calloc(1, sizeof(struct html))))
96: return(NULL);
1.10 kristaps 97:
1.37 kristaps 98: SLIST_INIT(&h->tags);
99: SLIST_INIT(&h->ords);
100:
1.32 kristaps 101: if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
102: free(h);
103: return(NULL);
104: }
1.41 kristaps 105:
1.47 kristaps 106: while (outopts && *outopts)
1.43 kristaps 107: switch (getsubopt(&outopts, toks, &v)) {
108: case (0):
109: h->style = v;
110: break;
111: case (1):
1.53 kristaps 112: h->base_man = v;
1.43 kristaps 113: break;
1.54 kristaps 114: case (2):
115: h->base_includes = v;
116: break;
1.43 kristaps 117: default:
118: break;
119: }
120:
1.30 kristaps 121: return(h);
1.29 kristaps 122: }
1.10 kristaps 123:
1.33 kristaps 124:
1.29 kristaps 125: void
126: html_free(void *p)
127: {
1.30 kristaps 128: struct tag *tag;
1.37 kristaps 129: struct ord *ord;
1.30 kristaps 130: struct html *h;
131:
132: h = (struct html *)p;
1.10 kristaps 133:
1.37 kristaps 134: while ( ! SLIST_EMPTY(&h->ords)) {
135: ord = SLIST_FIRST(&h->ords);
136: SLIST_REMOVE_HEAD(&h->ords, entry);
137: free(ord);
138: }
139:
140: while ( ! SLIST_EMPTY(&h->tags)) {
141: tag = SLIST_FIRST(&h->tags);
142: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 143: free(tag);
144: }
1.36 kristaps 145:
146: if (h->symtab)
147: chars_free(h->symtab);
1.53 kristaps 148:
1.30 kristaps 149: free(h);
1.10 kristaps 150: }
1.2 kristaps 151:
1.33 kristaps 152:
1.51 kristaps 153: void
1.29 kristaps 154: print_gen_head(struct html *h)
155: {
1.41 kristaps 156: struct htmlpair tag[4];
157:
158: tag[0].key = ATTR_HTTPEQUIV;
159: tag[0].val = "Content-Type";
160: tag[1].key = ATTR_CONTENT;
161: tag[1].val = "text/html; charset=utf-8";
162: print_otag(h, TAG_META, 2, tag);
163:
164: tag[0].key = ATTR_NAME;
165: tag[0].val = "resource-type";
166: tag[1].key = ATTR_CONTENT;
167: tag[1].val = "document";
168: print_otag(h, TAG_META, 2, tag);
169:
170: if (h->style) {
171: tag[0].key = ATTR_REL;
172: tag[0].val = "stylesheet";
173: tag[1].key = ATTR_HREF;
174: tag[1].val = h->style;
175: tag[2].key = ATTR_TYPE;
176: tag[2].val = "text/css";
177: tag[3].key = ATTR_MEDIA;
178: tag[3].val = "all";
179: print_otag(h, TAG_LINK, 4, tag);
180: }
1.4 kristaps 181: }
182:
1.33 kristaps 183:
1.29 kristaps 184: static void
1.32 kristaps 185: print_spec(struct html *h, const char *p, int len)
186: {
187: const char *rhs;
188: int i;
189: size_t sz;
190:
191: rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
192:
193: if (NULL == rhs)
194: return;
195: for (i = 0; i < (int)sz; i++)
196: putchar(rhs[i]);
197: }
198:
1.33 kristaps 199:
1.32 kristaps 200: static void
201: print_res(struct html *h, const char *p, int len)
202: {
203: const char *rhs;
204: int i;
205: size_t sz;
206:
207: rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
208:
209: if (NULL == rhs)
210: return;
211: for (i = 0; i < (int)sz; i++)
212: putchar(rhs[i]);
213: }
214:
1.33 kristaps 215:
1.32 kristaps 216: static void
217: print_escape(struct html *h, const char **p)
218: {
219: int j, type;
220: const char *wp;
221:
222: wp = *p;
223: type = 1;
224:
225: if (0 == *(++wp)) {
226: *p = wp;
227: return;
228: }
229:
230: if ('(' == *wp) {
231: wp++;
232: if (0 == *wp || 0 == *(wp + 1)) {
233: *p = 0 == *wp ? wp : wp + 1;
234: return;
235: }
236:
237: print_spec(h, wp, 2);
238: *p = ++wp;
239: return;
240:
241: } else if ('*' == *wp) {
242: if (0 == *(++wp)) {
243: *p = wp;
244: return;
245: }
246:
247: switch (*wp) {
248: case ('('):
249: wp++;
250: if (0 == *wp || 0 == *(wp + 1)) {
251: *p = 0 == *wp ? wp : wp + 1;
252: return;
253: }
254:
255: print_res(h, wp, 2);
256: *p = ++wp;
257: return;
258: case ('['):
259: type = 0;
260: break;
261: default:
262: print_res(h, wp, 1);
263: *p = wp;
264: return;
265: }
266:
267: } else if ('f' == *wp) {
268: if (0 == *(++wp)) {
269: *p = wp;
270: return;
271: }
272:
273: switch (*wp) {
274: case ('B'):
275: /* TODO */
276: break;
277: case ('I'):
278: /* TODO */
279: break;
280: case ('P'):
281: /* FALLTHROUGH */
282: case ('R'):
283: /* TODO */
284: break;
285: default:
286: break;
287: }
288:
289: *p = wp;
290: return;
291:
292: } else if ('[' != *wp) {
293: print_spec(h, wp, 1);
294: *p = wp;
295: return;
296: }
297:
298: wp++;
299: for (j = 0; *wp && ']' != *wp; wp++, j++)
300: /* Loop... */ ;
301:
302: if (0 == *wp) {
303: *p = wp;
304: return;
305: }
306:
307: if (type)
308: print_spec(h, wp - j, j);
309: else
310: print_res(h, wp - j, j);
311:
312: *p = wp;
313: }
314:
1.9 kristaps 315:
1.29 kristaps 316: static void
1.32 kristaps 317: print_encode(struct html *h, const char *p)
1.29 kristaps 318: {
1.14 kristaps 319:
1.32 kristaps 320: for (; *p; p++) {
1.34 kristaps 321: if ('\\' == *p) {
322: print_escape(h, &p);
323: continue;
324: }
325: switch (*p) {
326: case ('<'):
327: printf("<");
328: break;
329: case ('>'):
330: printf(">");
331: break;
332: case ('&'):
333: printf("&");
334: break;
335: default:
1.32 kristaps 336: putchar(*p);
1.34 kristaps 337: break;
1.32 kristaps 338: }
339: }
1.14 kristaps 340: }
341:
342:
1.51 kristaps 343: struct tag *
1.29 kristaps 344: print_otag(struct html *h, enum htmltag tag,
345: int sz, const struct htmlpair *p)
1.14 kristaps 346: {
1.29 kristaps 347: int i;
1.30 kristaps 348: struct tag *t;
349:
350: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
351: if (NULL == (t = malloc(sizeof(struct tag))))
352: err(EXIT_FAILURE, "malloc");
353: t->tag = tag;
1.37 kristaps 354: SLIST_INSERT_HEAD(&h->tags, t, entry);
1.30 kristaps 355: } else
356: t = NULL;
1.29 kristaps 357:
358: if ( ! (HTML_NOSPACE & h->flags))
1.30 kristaps 359: if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.29 kristaps 360: printf(" ");
361:
362: printf("<%s", htmltags[tag].name);
363: for (i = 0; i < sz; i++) {
364: printf(" %s=\"", htmlattrs[p[i].key]);
365: assert(p->val);
1.32 kristaps 366: print_encode(h, p[i].val);
1.29 kristaps 367: printf("\"");
368: }
369: printf(">");
1.14 kristaps 370:
1.29 kristaps 371: h->flags |= HTML_NOSPACE;
1.30 kristaps 372: if (HTML_CLRLINE & htmltags[tag].flags)
373: h->flags |= HTML_NEWLINE;
374: else
375: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 376:
1.30 kristaps 377: return(t);
1.14 kristaps 378: }
379:
380:
381: /* ARGSUSED */
1.29 kristaps 382: static void
383: print_ctag(struct html *h, enum htmltag tag)
1.14 kristaps 384: {
385:
1.29 kristaps 386: printf("</%s>", htmltags[tag].name);
1.30 kristaps 387: if (HTML_CLRLINE & htmltags[tag].flags)
1.29 kristaps 388: h->flags |= HTML_NOSPACE;
1.30 kristaps 389: if (HTML_CLRLINE & htmltags[tag].flags)
390: h->flags |= HTML_NEWLINE;
391: else
392: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 393: }
394:
395:
1.29 kristaps 396: /* ARGSUSED */
1.51 kristaps 397: void
1.29 kristaps 398: print_gen_doctype(struct html *h)
1.1 kristaps 399: {
1.29 kristaps 400:
1.46 kristaps 401: printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE, DTD);
1.1 kristaps 402: }
403:
404:
1.51 kristaps 405: void
1.29 kristaps 406: print_text(struct html *h, const char *p)
1.1 kristaps 407: {
408:
1.29 kristaps 409: if (*p && 0 == *(p + 1))
410: switch (*p) {
411: case('.'):
412: /* FALLTHROUGH */
413: case(','):
414: /* FALLTHROUGH */
415: case(';'):
416: /* FALLTHROUGH */
417: case(':'):
418: /* FALLTHROUGH */
419: case('?'):
420: /* FALLTHROUGH */
421: case('!'):
422: /* FALLTHROUGH */
423: case(')'):
424: /* FALLTHROUGH */
425: case(']'):
426: /* FALLTHROUGH */
427: case('}'):
1.52 kristaps 428: if ( ! (HTML_IGNDELIM & h->flags))
429: h->flags |= HTML_NOSPACE;
1.30 kristaps 430: break;
1.29 kristaps 431: default:
432: break;
433: }
1.1 kristaps 434:
1.29 kristaps 435: if ( ! (h->flags & HTML_NOSPACE))
436: printf(" ");
1.30 kristaps 437:
1.29 kristaps 438: h->flags &= ~HTML_NOSPACE;
1.30 kristaps 439: h->flags &= ~HTML_NEWLINE;
1.1 kristaps 440:
1.29 kristaps 441: if (p)
1.32 kristaps 442: print_encode(h, p);
1.8 kristaps 443:
1.29 kristaps 444: if (*p && 0 == *(p + 1))
445: switch (*p) {
446: case('('):
447: /* FALLTHROUGH */
448: case('['):
449: /* FALLTHROUGH */
450: case('{'):
451: h->flags |= HTML_NOSPACE;
1.30 kristaps 452: break;
1.29 kristaps 453: default:
454: break;
455: }
1.1 kristaps 456: }
1.30 kristaps 457:
458:
1.51 kristaps 459: void
1.30 kristaps 460: print_tagq(struct html *h, const struct tag *until)
461: {
462: struct tag *tag;
463:
1.37 kristaps 464: while ( ! SLIST_EMPTY(&h->tags)) {
465: tag = SLIST_FIRST(&h->tags);
1.30 kristaps 466: print_ctag(h, tag->tag);
1.37 kristaps 467: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 468: free(tag);
469: if (until && tag == until)
470: return;
471: }
472: }
473:
474:
1.51 kristaps 475: void
1.30 kristaps 476: print_stagq(struct html *h, const struct tag *suntil)
477: {
478: struct tag *tag;
479:
1.37 kristaps 480: while ( ! SLIST_EMPTY(&h->tags)) {
481: tag = SLIST_FIRST(&h->tags);
1.30 kristaps 482: if (suntil && tag == suntil)
483: return;
484: print_ctag(h, tag->tag);
1.37 kristaps 485: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 486: free(tag);
487: }
488: }
1.55 ! kristaps 489:
! 490:
! 491: void
! 492: bufinit(struct html *h)
! 493: {
! 494:
! 495: h->buf[0] = '\0';
! 496: h->buflen = 0;
! 497: }
! 498:
! 499:
! 500: void
! 501: bufcat(struct html *h, const char *p)
! 502: {
! 503:
! 504: bufncat(h, p, strlen(p));
! 505: }
! 506:
! 507:
! 508: void
! 509: buffmt(struct html *h, const char *fmt, ...)
! 510: {
! 511: va_list ap;
! 512:
! 513: va_start(ap, fmt);
! 514: (void)vsnprintf(h->buf + h->buflen,
! 515: BUFSIZ - h->buflen - 1, fmt, ap);
! 516: va_end(ap);
! 517: h->buflen = strlen(h->buf);
! 518: assert('\0' == h->buf[h->buflen]);
! 519: }
! 520:
! 521:
! 522: void
! 523: bufncat(struct html *h, const char *p, size_t sz)
! 524: {
! 525:
! 526: if (h->buflen + sz > BUFSIZ - 1)
! 527: sz = BUFSIZ - 1 - h->buflen;
! 528:
! 529: (void)strncat(h->buf, p, sz);
! 530: h->buflen += sz;
! 531: assert('\0' == h->buf[h->buflen]);
! 532: }
! 533:
! 534:
! 535: void
! 536: buffmt_includes(struct html *h, const char *name)
! 537: {
! 538: const char *p, *pp;
! 539:
! 540: pp = h->base_includes;
! 541: while ((p = strchr(pp, '%'))) {
! 542: bufncat(h, pp, p - pp);
! 543: switch (*(p + 1)) {
! 544: case('I'):
! 545: bufcat(h, name);
! 546: break;
! 547: default:
! 548: bufncat(h, p, 2);
! 549: break;
! 550: }
! 551: pp = p + 2;
! 552: }
! 553: if (pp)
! 554: bufcat(h, pp);
! 555: }
! 556:
! 557:
! 558: void
! 559: buffmt_man(struct html *h,
! 560: const char *name, const char *sec)
! 561: {
! 562: const char *p, *pp;
! 563:
! 564: pp = h->base_man;
! 565: while ((p = strchr(pp, '%'))) {
! 566: bufncat(h, pp, p - pp);
! 567: switch (*(p + 1)) {
! 568: case('S'):
! 569: bufcat(h, sec);
! 570: break;
! 571: case('N'):
! 572: buffmt(h, name ? name : "1");
! 573: break;
! 574: default:
! 575: bufncat(h, p, 2);
! 576: break;
! 577: }
! 578: pp = p + 2;
! 579: }
! 580: if (pp)
! 581: bufcat(h, pp);
! 582: }
CVSweb