/* * Return a pointer to the vsb data. */ char * VSB_data(const struct vsb *s) { assert_VSB_integrity(s); assert_VSB_state(s, VSB_FINISHED); return (s->s_buf); }
/* * Format the given argument list and append the resulting string to an vsb. */ int VSB_vprintf(struct vsb *s, const char *fmt, va_list ap) { va_list ap_copy; int len; assert_VSB_integrity(s); assert_VSB_state(s, 0); KASSERT(fmt != NULL, ("%s called with a NULL format string", __func__)); if (s->s_error != 0) return (-1); _vsb_indent(s); /* * For the moment, there is no way to get vsnprintf(3) to hand * back a character at a time, to push everything into * VSB_putc_func() as was done for the kernel. * * In userspace, while drains are useful, there's generally * not a problem attempting to malloc(3) on out of space. So * expand a userland vsb if there is not enough room for the * data produced by VSB_[v]printf(3). */ do { va_copy(ap_copy, ap); len = vsnprintf(&s->s_buf[s->s_len], VSB_FREESPACE(s) + 1, fmt, ap_copy); va_end(ap_copy); } while (len > VSB_FREESPACE(s) && VSB_extend(s, len - VSB_FREESPACE(s)) == 0); /* * s->s_len is the length of the string, without the terminating nul. * When updating s->s_len, we must subtract 1 from the length that * we passed into vsnprintf() because that length includes the * terminating nul. * * vsnprintf() returns the amount that would have been copied, * given sufficient space, so don't over-increment s_len. */ if (VSB_FREESPACE(s) < len) len = VSB_FREESPACE(s); s->s_len += len; if (!VSB_HASROOM(s) && !VSB_CANEXTEND(s)) s->s_error = ENOMEM; KASSERT(s->s_len < s->s_size, ("wrote past end of vsb (%d >= %d)", s->s_len, s->s_size)); if (s->s_error != 0) return (-1); return (0); }
void VSB_indent(struct vsb * s, int i) { assert_VSB_integrity(s); if (s->s_indent + i < 0) s->s_error = EINVAL; else s->s_indent += i; }
/* * Copy a byte string into an vsb. */ int VSB_bcpy(struct vsb *s, const void *buf, size_t len) { assert_VSB_integrity(s); assert_VSB_state(s, 0); VSB_clear(s); return (VSB_bcat(s, buf, len)); }
/* * Copy a string into an vsb. */ int VSB_cpy(struct vsb *s, const char *str) { assert_VSB_integrity(s); assert_VSB_state(s, 0); VSB_clear(s); return (VSB_cat(s, str)); }
/* * Return the length of the vsb data. */ ssize_t VSB_len(const struct vsb *s) { assert_VSB_integrity(s); /* don't care if it's finished or not */ if (s->s_error != 0) return (-1); return (s->s_len); }
/* * Clear an vsb and reset its position. */ void VSB_clear(struct vsb *s) { assert_VSB_integrity(s); /* don't care if it's finished or not */ VSB_CLEARFLAG(s, VSB_FINISHED); s->s_error = 0; s->s_len = 0; }
/* * Finish off an vsb. */ int VSB_finish(struct vsb *s) { assert_VSB_integrity(s); assert_VSB_state(s, 0); s->s_buf[s->s_len] = '\0'; VSB_SETFLAG(s, VSB_FINISHED); errno = s->s_error; if (s->s_error) return (-1); return (0); }
/* * Clear an vsb, free its buffer if necessary. */ void VSB_delete(struct vsb *s) { int isdyn; assert_VSB_integrity(s); /* don't care if it's finished or not */ if (VSB_ISDYNAMIC(s)) SBFREE(s->s_buf); isdyn = VSB_ISDYNSTRUCT(s); memset(s, 0, sizeof(*s)); if (isdyn) SBFREE(s); }
/* * Trim whitespace characters from end of an vsb. */ int VSB_trim(struct vsb *s) { assert_VSB_integrity(s); assert_VSB_state(s, 0); if (s->s_error != 0) return (-1); while (s->s_len > 0 && isspace(s->s_buf[s->s_len-1])) --s->s_len; return (0); }
/* * Append a string to an vsb. */ int VSB_cat(struct vsb *s, const char *str) { assert_VSB_integrity(s); assert_VSB_state(s, 0); if (s->s_error != 0) return (-1); while (*str != '\0') { VSB_put_byte(s, *str++); if (s->s_error != 0) return (-1); } return (0); }
/* * Append a byte to an vsb. This is the core function for appending * to an vsb and is the main place that deals with extending the * buffer and marking overflow. */ static void VSB_put_byte(struct vsb *s, int c) { assert_VSB_integrity(s); assert_VSB_state(s, 0); if (s->s_error != 0) return; if (VSB_FREESPACE(s) <= 0) { if (VSB_extend(s, 1) < 0) s->s_error = ENOMEM; if (s->s_error != 0) return; } s->s_buf[s->s_len++] = (char)c; }
/* * Append a byte string to an vsb. */ int VSB_bcat(struct vsb *s, const void *buf, size_t len) { const char *str = buf; const char *end = str + len; assert_VSB_integrity(s); assert_VSB_state(s, 0); if (s->s_error != 0) return (-1); for (; str < end; str++) { VSB_put_byte(s, *str); if (s->s_error != 0) return (-1); } return (0); }
/* * Set the vsb's end position to an arbitrary value. * Effectively truncates the vsb at the new position. */ int VSB_setpos(struct vsb *s, ssize_t pos) { assert_VSB_integrity(s); assert_VSB_state(s, 0); KASSERT(pos >= 0, ("attempt to seek to a negative position (%jd)", (intmax_t)pos)); KASSERT(pos < s->s_size, ("attempt to seek past end of vsb (%jd >= %jd)", (intmax_t)pos, (intmax_t)s->s_size)); if (pos < 0 || pos > s->s_len) return (-1); s->s_len = pos; return (0); }
/* * Append a byte string to an vsb. */ int VSB_bcat(struct vsb *s, const void *buf, ssize_t len) { assert_VSB_integrity(s); assert_VSB_state(s, 0); assert(len >= 0); if (s->s_error != 0) return (-1); if (len == 0) return (0); _vsb_indent(s); if (len > VSB_FREESPACE(s)) { if (VSB_extend(s, len - VSB_FREESPACE(s)) < 0) s->s_error = ENOMEM; if (s->s_error != 0) return (-1); } memcpy(s->s_buf + s->s_len, buf, len); s->s_len += len; return (0); }