=================================================================== RCS file: /cvs/mandoc/html.c,v retrieving revision 1.70 retrieving revision 1.81 diff -u -p -r1.70 -r1.81 --- mandoc/html.c 2009/10/28 08:00:18 1.70 +++ mandoc/html.c 2009/11/05 10:16:01 1.81 @@ -1,4 +1,4 @@ -/* $Id: html.c,v 1.70 2009/10/28 08:00:18 kristaps Exp $ */ +/* $Id: html.c,v 1.81 2009/11/05 10:16:01 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -18,9 +18,8 @@ #include #include -#include -#include #include +#include #include #include #include @@ -100,17 +99,16 @@ html_alloc(char *outopts) toks[2] = "includes"; toks[3] = NULL; - if (NULL == (h = calloc(1, sizeof(struct html)))) - return(NULL); + h = calloc(1, sizeof(struct html)); + if (NULL == h) { + perror(NULL); + exit(EXIT_FAILURE); + } h->tags.head = NULL; h->ords.head = NULL; + h->symtab = chars_init(CHARS_HTML); - if (NULL == (h->symtab = chars_init(CHARS_HTML))) { - free(h); - return(NULL); - } - while (outopts && *outopts) switch (getsubopt(&outopts, UNCONST(toks), &v)) { case (0): @@ -191,15 +189,13 @@ static void print_spec(struct html *h, const char *p, int len) { const char *rhs; - int i; size_t sz; rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz); if (NULL == rhs) return; - for (i = 0; i < (int)sz; i++) - putchar(rhs[i]); + fwrite(rhs, 1, sz, stdout); } @@ -207,15 +203,13 @@ static void print_res(struct html *h, const char *p, int len) { const char *rhs; - int i; size_t sz; rhs = chars_a2res(h->symtab, p, (size_t)len, &sz); if (NULL == rhs) return; - for (i = 0; i < (int)sz; i++) - putchar(rhs[i]); + fwrite(rhs, 1, sz, stdout); } @@ -276,21 +270,19 @@ print_escape(struct html *h, const char **p) return; } - switch (*wp) { - case ('B'): - /* TODO */ - break; - case ('I'): - /* TODO */ - break; - case ('P'): - /* FALLTHROUGH */ - case ('R'): - /* TODO */ - break; - default: - break; - } + /* + * These aren't supported, as they're symmetry-breaking + * constructs that don't play well with hierarchical + * mark-up. Consider: + * + * \fBHello. + * .PP + * World. + * + * The style started before "Hello" wouldn't be able to + * propogate into the next `PP' because we'd exit the + * current paragraph's scope. + */ *p = wp; return; @@ -322,26 +314,27 @@ print_escape(struct html *h, const char **p) static void print_encode(struct html *h, const char *p) { + size_t sz; for (; *p; p++) { + sz = strcspn(p, "\\<>&"); + + fwrite(p, 1, sz, stdout); + p += /* LINTED */ + sz; + if ('\\' == *p) { print_escape(h, &p); continue; - } - switch (*p) { - case ('<'): - printf("<"); + } else if ('\0' == *p) break; - case ('>'): + + if ('<' == *p) + printf("<"); + else if ('>' == *p) printf(">"); - break; - case ('&'): + else if ('&' == *p) printf("&"); - break; - default: - putchar(*p); - break; - } } } @@ -354,8 +347,11 @@ print_otag(struct html *h, enum htmltag tag, struct tag *t; if ( ! (HTML_NOSTACK & htmltags[tag].flags)) { - if (NULL == (t = malloc(sizeof(struct tag)))) - err(EXIT_FAILURE, "malloc"); + t = malloc(sizeof(struct tag)); + if (NULL == t) { + perror(NULL); + exit(EXIT_FAILURE); + } t->tag = tag; t->next = h->tags.head; h->tags.head = t; @@ -364,16 +360,16 @@ print_otag(struct html *h, enum htmltag tag, if ( ! (HTML_NOSPACE & h->flags)) if ( ! (HTML_CLRLINE & htmltags[tag].flags)) - printf(" "); + putchar(' '); printf("<%s", htmltags[tag].name); for (i = 0; i < sz; i++) { printf(" %s=\"", htmlattrs[p[i].key]); assert(p->val); print_encode(h, p[i].val); - printf("\""); + putchar('\"'); } - printf(">"); + putchar('>'); h->flags |= HTML_NOSPACE; if (HTML_CLRLINE & htmltags[tag].flags) @@ -391,11 +387,11 @@ print_ctag(struct html *h, enum htmltag tag) { printf("", htmltags[tag].name); - if (HTML_CLRLINE & htmltags[tag].flags) + if (HTML_CLRLINE & htmltags[tag].flags) { h->flags |= HTML_NOSPACE; - if (HTML_CLRLINE & htmltags[tag].flags) h->flags |= HTML_NEWLINE; - else + putchar('\n'); + } else h->flags &= ~HTML_NEWLINE; } @@ -440,7 +436,7 @@ print_text(struct html *h, const char *p) } if ( ! (h->flags & HTML_NOSPACE)) - printf(" "); + putchar(' '); h->flags &= ~HTML_NOSPACE; h->flags &= ~HTML_NEWLINE; @@ -671,7 +667,7 @@ html_idcat(char *dst, const char *src, int sz) sz--; for ( ; *src != '\0' && sz > 1; src++) { - ssz = snprintf(dst, sz, "%.2x", *src); + ssz = snprintf(dst, (size_t)sz, "%.2x", *src); sz -= ssz; dst += ssz; }