-/* $Id: html.c,v 1.68 2009/10/28 05:08:17 kristaps Exp $ */
+/* $Id: html.c,v 1.80 2009/11/02 06:22:44 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
#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>
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)) {
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);
}
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);
}
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;
- }
}
}
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;
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)
{
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;
}
}
if ( ! (h->flags & HTML_NOSPACE))
- printf(" ");
+ putchar(' ');
h->flags &= ~HTML_NOSPACE;
h->flags &= ~HTML_NEWLINE;
void
-html_idcpy(char *dst, const char *src, int sz)
+html_idcat(char *dst, const char *src, int sz)
{
+ int ssz;
assert(sz);
- dst[0] = '\0';
- html_idcat(dst, src, sz);
-}
-
-
-void
-html_idcat(char *dst, const char *src, int sz)
-{
- int i;
/* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
- for (i = 0; *dst != '\0' && i < sz - 1; dst++, i++)
+ for ( ; *dst != '\0' && sz; dst++, sz--)
/* Jump to end. */ ;
- for ( ; *src != '\0' && i < sz - 1; src++, i++) {
- if (isalnum((u_char)*src)) {
- *dst++ = *src;
- continue;
- }
+ assert(sz > 2);
- switch (*src) {
- case (';'):
- *dst++ = ';';
- break;
- case ('-'):
- *dst++ = '-';
- break;
- case (':'):
- *dst++ = ':';
- break;
- case ('_'):
- /* FALLTHROUGH */
- default:
- *dst++ = '_';
- break;
- }
- }
+ /* We can't start with a number (bah). */
+ *dst++ = 'x';
*dst = '\0';
+ sz--;
+
+ for ( ; *src != '\0' && sz > 1; src++) {
+ ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
+ sz -= ssz;
+ dst += ssz;
+ }
}