]> git.cameronkatri.com Git - mandoc.git/blobdiff - html.c
Using putchar for single-character prints (noted by Joerg Sonnenberger).
[mandoc.git] / html.c
diff --git a/html.c b/html.c
index c14d97d5eca48a742d9c7d2f798f415a4b570db5..47171298685a4e8dd3d5cdf567f3f709d45a0b19 100644 (file)
--- a/html.c
+++ b/html.c
@@ -1,4 +1,4 @@
-/*     $Id: html.c,v 1.70 2009/10/28 08:00:18 kristaps Exp $ */
+/*     $Id: html.c,v 1.78 2009/11/01 15:34:15 kristaps Exp $ */
 /*
  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
  *
@@ -18,9 +18,8 @@
 
 #include <assert.h>
 #include <ctype.h>
-#include <err.h>
-#include <stdio.h>
 #include <stdarg.h>
+#include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
@@ -100,16 +99,15 @@ 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;
-
-       if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
-               free(h);
-               return(NULL);
-       }
+       h->symtab = chars_init(CHARS_HTML);
 
        while (outopts && *outopts)
                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
@@ -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);
 }
 
 
@@ -322,26 +316,26 @@ 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 += sz;
+
                if ('\\' == *p) {
                        print_escape(h, &p);
                        continue;
-               }
-               switch (*p) {
-               case ('<'):
-                       printf("&lt;");
+               } else if ('\0' == *p)
                        break;
-               case ('>'):
+
+               if ('<' == *p)
+                       printf("&lt;");
+               else if ('>' == *p)
                        printf("&gt;");
-                       break;
-               case ('&'):
+               else if ('&' == *p)
                        printf("&amp;");
-                       break;
-               default:
-                       putchar(*p);
-                       break;
-               }
        }
 }
 
@@ -354,8 +348,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 +361,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 +388,11 @@ print_ctag(struct html *h, enum htmltag tag)
 {
        
        printf("</%s>", 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 +437,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 +668,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;
        }