+
+
+void
+print_tagq(struct html *h, const struct tag *until)
+{
+ struct tag *tag;
+
+ while ((tag = h->tags.head) != NULL) {
+ if (tag == h->metaf)
+ h->metaf = NULL;
+ print_ctag(h, tag->tag);
+ h->tags.head = tag->next;
+ free(tag);
+ if (until && tag == until)
+ return;
+ }
+}
+
+
+void
+print_stagq(struct html *h, const struct tag *suntil)
+{
+ struct tag *tag;
+
+ while ((tag = h->tags.head) != NULL) {
+ if (suntil && tag == suntil)
+ return;
+ if (tag == h->metaf)
+ h->metaf = NULL;
+ print_ctag(h, tag->tag);
+ h->tags.head = tag->next;
+ free(tag);
+ }
+}
+
+
+void
+bufinit(struct html *h)
+{
+
+ h->buf[0] = '\0';
+ h->buflen = 0;
+}
+
+
+void
+bufcat_style(struct html *h, const char *key, const char *val)
+{
+
+ bufcat(h, key);
+ bufncat(h, ":", 1);
+ bufcat(h, val);
+ bufncat(h, ";", 1);
+}
+
+
+void
+bufcat(struct html *h, const char *p)
+{
+
+ bufncat(h, p, strlen(p));
+}
+
+
+void
+buffmt(struct html *h, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ (void)vsnprintf(h->buf + (int)h->buflen,
+ BUFSIZ - h->buflen - 1, fmt, ap);
+ va_end(ap);
+ h->buflen = strlen(h->buf);
+}
+
+
+void
+bufncat(struct html *h, const char *p, size_t sz)
+{
+
+ if (h->buflen + sz > BUFSIZ - 1)
+ sz = BUFSIZ - 1 - h->buflen;
+
+ (void)strncat(h->buf, p, sz);
+ h->buflen += sz;
+}
+
+
+void
+buffmt_includes(struct html *h, const char *name)
+{
+ const char *p, *pp;
+
+ pp = h->base_includes;
+
+ while (NULL != (p = strchr(pp, '%'))) {
+ bufncat(h, pp, (size_t)(p - pp));
+ switch (*(p + 1)) {
+ case('I'):
+ bufcat(h, name);
+ break;
+ default:
+ bufncat(h, p, 2);
+ break;
+ }
+ pp = p + 2;
+ }
+ if (pp)
+ bufcat(h, pp);
+}
+
+
+void
+buffmt_man(struct html *h,
+ const char *name, const char *sec)
+{
+ const char *p, *pp;
+
+ pp = h->base_man;
+
+ /* LINTED */
+ while (NULL != (p = strchr(pp, '%'))) {
+ bufncat(h, pp, (size_t)(p - pp));
+ switch (*(p + 1)) {
+ case('S'):
+ bufcat(h, sec ? sec : "1");
+ break;
+ case('N'):
+ buffmt(h, name);
+ break;
+ default:
+ bufncat(h, p, 2);
+ break;
+ }
+ pp = p + 2;
+ }
+ if (pp)
+ bufcat(h, pp);
+}
+
+
+void
+bufcat_su(struct html *h, const char *p, const struct roffsu *su)
+{
+ double v;
+ const char *u;
+
+ v = su->scale;
+
+ switch (su->unit) {
+ case (SCALE_CM):
+ u = "cm";
+ break;
+ case (SCALE_IN):
+ u = "in";
+ break;
+ case (SCALE_PC):
+ u = "pc";
+ break;
+ case (SCALE_PT):
+ u = "pt";
+ break;
+ case (SCALE_EM):
+ u = "em";
+ break;
+ case (SCALE_MM):
+ if (0 == (v /= 100))
+ v = 1;
+ u = "em";
+ break;
+ case (SCALE_EN):
+ u = "ex";
+ break;
+ case (SCALE_BU):
+ u = "ex";
+ break;
+ case (SCALE_VS):
+ u = "em";
+ break;
+ default:
+ u = "ex";
+ break;
+ }
+
+ if (su->pt)
+ buffmt(h, "%s: %f%s;", p, v, u);
+ else
+ /* LINTED */
+ buffmt(h, "%s: %d%s;", p, (int)v, u);
+}
+
+
+void
+html_idcat(char *dst, const char *src, int sz)
+{
+ int ssz;
+
+ assert(sz);
+
+ /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
+
+ for ( ; *dst != '\0' && sz; dst++, sz--)
+ /* Jump to end. */ ;
+
+ assert(sz > 2);
+
+ /* 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;
+ }
+}