varnishd_LDADD = \
$(top_builddir)/lib/libcompat/libcompat.a \
- $(top_builddir)/lib/libsbuf/libsbuf.a \
$(top_builddir)/lib/libvarnish/libvarnish.la \
$(top_builddir)/lib/libvcl/libvcl.la \
-lpthread \
#include <sys/time.h>
#include "queue.h"
-#include "sbuf.h"
+#include "vsb.h"
#include "vcl_returns.h"
#include "common.h"
#define HTTP_HDR_FIRST 5
struct cli;
-struct sbuf;
+struct vsb;
struct sess;
struct object;
struct objhead;
#include "cli_priv.h"
#include "cli_common.h"
#include "cache.h"
-#include "sbuf.h"
+#include "vsb.h"
#include "heritage.h"
/*--------------------------------------------------------------------*/
cli = &clis;
memset(cli, 0, sizeof *cli);
- cli->sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ cli->sb = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(cli->sb != NULL);
lbuf = 4096;
buf = malloc(lbuf);
continue;
*p = '\0';
VSL(SLT_CLI, 0, "Rd %s", buf);
- sbuf_clear(cli->sb);
+ vsb_clear(cli->sb);
cli_dispatch(cli, CLI_cmds, buf);
- sbuf_finish(cli->sb);
+ vsb_finish(cli->sb);
i = cli_writeres(heritage.fds[1], cli);
if (i) {
VSL(SLT_Error, 0, "CLI write failed (errno=%d)", errno);
return;
}
VSL(SLT_CLI, 0, "Wr %d %d %s",
- i, cli->result, sbuf_data(cli->sb));
+ i, cli->result, vsb_data(cli->sb));
i = ++p - buf;
assert(i <= nbuf);
if (i < nbuf)
RES_Error(struct sess *sp, int error, const char *msg)
{
char buf[40];
- struct sbuf *sb;
+ struct vsb *sb;
- sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ sb = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(sb != NULL);
if (msg == NULL) {
}
}
- sbuf_clear(sb);
- sbuf_printf(sb, "HTTP/1.1 %03d %s\r\n", error, msg);
+ vsb_clear(sb);
+ vsb_printf(sb, "HTTP/1.1 %03d %s\r\n", error, msg);
TIM_format(sp->t_req.tv_sec, buf);
- sbuf_printf(sb, "Date: %s\r\n", buf);
- sbuf_cat(sb,
+ vsb_printf(sb, "Date: %s\r\n", buf);
+ vsb_cat(sb,
"Server: Varnish\r\n"
"Connection: close\r\n"
"content-Type: text/html; charset=iso-8859-1\r\n"
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
"<HTML>\r\n"
" <HEAD>\r\n");
- sbuf_printf(sb, " <TITLE>%03d %s</TITLE>\r\n", error, msg);
- sbuf_cat(sb,
+ vsb_printf(sb, " <TITLE>%03d %s</TITLE>\r\n", error, msg);
+ vsb_cat(sb,
" </HEAD>\r\n"
" <BODY>\r\n");
- sbuf_printf(sb, " <H1>Error %03d %s</H1>\r\n", error, msg);
+ vsb_printf(sb, " <H1>Error %03d %s</H1>\r\n", error, msg);
switch(error) {
case 400:
- sbuf_cat(sb,
+ vsb_cat(sb,
" Your HTTP protocol request did not make sense.\r\n");
break;
case 500:
default:
- sbuf_cat(sb,
+ vsb_cat(sb,
" Something unexpected happened.\r\n");
break;
}
- sbuf_cat(sb,
+ vsb_cat(sb,
" <P>\r\n"
" <I>\r\n"
" <A href=\"http://varnish.linpro.no/\">Varnish</A>\r\n"
" </BODY>\r\n"
"</HTML>\r\n");
- sbuf_finish(sb);
- WRK_Write(sp->wrk, sbuf_data(sb), sbuf_len(sb));
+ vsb_finish(sb);
+ WRK_Write(sp->wrk, vsb_data(sb), vsb_len(sb));
WRK_Flush(sp->wrk);
vca_close_session(sp, msg);
- sbuf_delete(sb);
+ vsb_delete(sb);
}
#include "shmlog.h"
#include "vrt.h"
-#include "sbuf.h"
+#include "vsb.h"
#include "vcl.h"
#include "cache.h"
}
int
-VRT_re_test(struct sbuf *sb, const char *re)
+VRT_re_test(struct vsb *sb, const char *re)
{
int i;
regex_t t;
return (0);
}
(void)regerror(i, &t, buf, sizeof buf);
- sbuf_printf(sb, "Regexp compilation error:\n\n%s\n\n", buf);
+ vsb_printf(sb, "Regexp compilation error:\n\n%s\n\n", buf);
regfree(&t);
return (1);
}
-esym(534, memcpy) // Ignoring return value of function
-esym(534, memmove) // Ignoring return value of function
-esym(534, strcpy) // Ignoring return value of function
--esym(534, sbuf_printf) // Ignoring return value of function
--esym(534, sbuf_cat) // Ignoring return value of function
+-esym(534, vsb_printf) // Ignoring return value of function
+-esym(534, vsb_cat) // Ignoring return value of function
// cache.h
-emacro(506, INCOMPL) // Constant value Boolean
#include "cli_priv.h"
#include "cli.h"
-#include "sbuf.h"
+#include "vsb.h"
#include "cli_common.h"
#include "mgt.h"
#include "mgt_cli.h"
return (0);
*p = '\0';
fprintf(stderr, "CLI <%s>\n", cp->buf);
- sbuf_clear(cp->cli->sb);
+ vsb_clear(cp->cli->sb);
cli_dispatch(cp->cli, cli_proto, cp->buf);
- sbuf_finish(cp->cli->sb);
+ vsb_finish(cp->cli->sb);
/* XXX: cp->verbose */
if (cli_writeres(cp->fdo, cp->cli))
break;
cp->nbuf -= i;
return (0);
}
- sbuf_delete(cp->cli->sb);
+ vsb_delete(cp->cli->sb);
free(cp->buf);
close(cp->fdi);
close(cp->fdo);
cp->buf = malloc(cp->lbuf);
assert(cp->buf != NULL);
- cp->cli->sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ cp->cli->sb = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(cp->cli->sb != NULL);
cp->ev = calloc(sizeof *cp->ev, 1);
#include <assert.h>
#include <sys/types.h>
-#include "sbuf.h"
+#include "vsb.h"
#include "queue.h"
#include "libvarnish.h"
{
char *buf, *vf;
const char *p, *q;
- struct sbuf *sb;
+ struct vsb *sb;
struct vclprog *vp;
- sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ sb = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(sb != NULL);
if (bflag != NULL) {
/*
} else {
vf = VCC_CompileFile(sb, fflag);
}
- sbuf_finish(sb);
- if (sbuf_len(sb) > 0) {
- fprintf(stderr, "%s", sbuf_data(sb));
- sbuf_delete(sb);
+ vsb_finish(sb);
+ if (vsb_len(sb) > 0) {
+ fprintf(stderr, "%s", vsb_data(sb));
+ vsb_delete(sb);
return (1);
}
- sbuf_delete(sb);
+ vsb_delete(sb);
vp = mgt_vcc_add("boot", vf);
vp->active = 1;
return (0);
mcf_config_inline(struct cli *cli, char **av, void *priv)
{
char *vf, *p;
- struct sbuf *sb;
+ struct vsb *sb;
unsigned status;
(void)priv;
- sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ sb = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(sb != NULL);
vf = VCC_Compile(sb, av[3], NULL);
- sbuf_finish(sb);
- if (sbuf_len(sb) > 0) {
- cli_out(cli, "%s", sbuf_data(sb));
- sbuf_delete(sb);
+ vsb_finish(sb);
+ if (vsb_len(sb) > 0) {
+ cli_out(cli, "%s", vsb_data(sb));
+ vsb_delete(sb);
cli_result(cli, CLIS_PARAM);
return;
}
- sbuf_delete(sb);
+ vsb_delete(sb);
if (child_pid >= 0 &&
mgt_cli_askchild(&status, &p, "config.load %s %s\n", av[2], vf)) {
cli_result(cli, status);
mcf_config_load(struct cli *cli, char **av, void *priv)
{
char *vf;
- struct sbuf *sb;
+ struct vsb *sb;
unsigned status;
char *p;
(void)priv;
- sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ sb = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(sb != NULL);
vf = VCC_CompileFile(sb, av[3]);
- sbuf_finish(sb);
- if (sbuf_len(sb) > 0) {
- cli_out(cli, "%s", sbuf_data(sb));
- sbuf_delete(sb);
+ vsb_finish(sb);
+ if (vsb_len(sb) > 0) {
+ cli_out(cli, "%s", vsb_data(sb));
+ vsb_delete(sb);
cli_result(cli, CLIS_PARAM);
return;
}
- sbuf_delete(sb);
+ vsb_delete(sb);
if (child_pid >= 0 &&
mgt_cli_askchild(&status, &p, "config.load %s %s\n", av[2], vf)) {
cli_result(cli, status);
#include <time.h>
#include <unistd.h>
-#include "sbuf.h"
+#include "vsb.h"
#include "libvarnish.h"
#include "cli.h"
varnishlog_CFLAGS = -include config.h
varnishlog_LDADD = \
- $(top_builddir)/lib/libvarnishapi/libvarnishapi.la \
- $(top_builddir)/lib/libsbuf/libsbuf.a
+ $(top_builddir)/lib/libvarnish/libvarnish.la \
+ $(top_builddir)/lib/libvarnishapi/libvarnishapi.la
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
-#include <sbuf.h>
+#include <vsb.h>
#include <vis.h>
#include "shmlog.h"
/* Ordering-----------------------------------------------------------*/
-static struct sbuf *ob[65536];
+static struct vsb *ob[65536];
static int hc[65536];
static int xrf[65536];
for (u = 0; u < 65536; u++) {
if (ob[u] == NULL)
continue;
- sbuf_finish(ob[u]);
- if (sbuf_len(ob[u]))
- printf("%s\n", sbuf_data(ob[u]));
- sbuf_clear(ob[u]);
+ vsb_finish(ob[u]);
+ if (vsb_len(ob[u]))
+ printf("%s\n", vsb_data(ob[u]));
+ vsb_clear(ob[u]);
}
}
u = (p[2] << 8) | p[3];
if (ob[u] == NULL) {
- ob[u] = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ ob[u] = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(ob[u] != NULL);
}
v = 0;
switch (p[0]) {
case SLT_VCL_call:
- sbuf_printf(ob[u], "%02x %3d %4d %-12s",
+ vsb_printf(ob[u], "%02x %3d %4d %-12s",
p[0], p[1], u, VSL_tags[p[0]]);
if (p[1] > 0) {
- sbuf_cat(ob[u], " <");
- sbuf_bcat(ob[u], p + 4, p[1]);
+ vsb_cat(ob[u], " <");
+ vsb_bcat(ob[u], p + 4, p[1]);
}
if (h_opt && p[1] == 3 && !memcmp(p + 4, "hit", 3))
hc[u]++;
break;
case SLT_VCL_trace:
if (p[1] > 0) {
- sbuf_cat(ob[u], " ");
- sbuf_bcat(ob[u], p + 4, p[1]);
+ vsb_cat(ob[u], " ");
+ vsb_bcat(ob[u], p + 4, p[1]);
}
break;
case SLT_VCL_return:
if (p[1] > 0) {
- sbuf_cat(ob[u], " ");
- sbuf_bcat(ob[u], p + 4, p[1]);
- sbuf_cat(ob[u], ">\n");
+ vsb_cat(ob[u], " ");
+ vsb_bcat(ob[u], p + 4, p[1]);
+ vsb_cat(ob[u], ">\n");
}
if (h_opt && p[1] == 7 && !memcmp(p + 4, "deliver", 7))
hc[u]++;
;
else if (p[1] > 4 && !memcmp(p + 4, "TTD:", 4))
break;
- sbuf_printf(ob[u], "%02x %3d %4d %-12s",
+ vsb_printf(ob[u], "%02x %3d %4d %-12s",
p[0], p[1], u, VSL_tags[p[0]]);
if (p[1] > 0)
- sbuf_cat(ob[u], vis_it(p));
- sbuf_cat(ob[u], "\n");
+ vsb_cat(ob[u], vis_it(p));
+ vsb_cat(ob[u], "\n");
break;
case SLT_HttpError:
if (!h_opt)
break;
}
if (v) {
- sbuf_printf(ob[u], "%02x %3d %4d %-12s",
+ vsb_printf(ob[u], "%02x %3d %4d %-12s",
p[0], p[1], u, VSL_tags[p[0]]);
if (p[1] > 0) {
- sbuf_cat(ob[u], " <");
- sbuf_bcat(ob[u], p + 4, p[1]);
- sbuf_cat(ob[u], ">");
+ vsb_cat(ob[u], " <");
+ vsb_bcat(ob[u], p + 4, p[1]);
+ vsb_cat(ob[u], ">");
}
- sbuf_cat(ob[u], "\n");
+ vsb_cat(ob[u], "\n");
}
if (u == 0) {
- sbuf_finish(ob[u]);
- printf("%s", sbuf_data(ob[u]));
- sbuf_clear(ob[u]);
+ vsb_finish(ob[u]);
+ printf("%s", vsb_data(ob[u]));
+ vsb_clear(ob[u]);
return;
}
switch (p[0]) {
case SLT_SessionReuse:
case SLT_BackendClose:
case SLT_BackendReuse:
- sbuf_finish(ob[u]);
- if ((hc[u] != 4 || h_opt == 0) && sbuf_len(ob[u]) > 1)
- printf("%s\n", sbuf_data(ob[u]));
- sbuf_clear(ob[u]);
+ vsb_finish(ob[u]);
+ if ((hc[u] != 4 || h_opt == 0) && vsb_len(ob[u]) > 1)
+ printf("%s\n", vsb_data(ob[u]));
+ vsb_clear(ob[u]);
hc[u] = 0;
xrf[u] = 0;
break;
varnishncsa_CFLAGS = -include config.h
varnishncsa_LDADD = \
- $(top_builddir)/lib/libvarnishapi/libvarnishapi.la \
- $(top_builddir)/lib/libsbuf/libsbuf.a
+ $(top_builddir)/lib/libvarnish/libvarnish.la \
+ $(top_builddir)/lib/libvarnishapi/libvarnishapi.la
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
-#include <sbuf.h>
+#include <vsb.h>
#include <vis.h>
#include <time.h>
};
-/* We make a array of pointers to sbuf's. Sbuf is a string buffer.
+/* We make a array of pointers to vsb's. Sbuf is a string buffer.
* * The buffer can be made/extended/cleared etc. through a API.
* * The array is 65536 long because we will use sessionid as key.
* *
* */
-static struct sbuf *ob[65536];
+static struct vsb *ob[65536];
static struct logline ll[65536];
for (u = 0; u < 65536; u++) {
if (ob[u] == NULL)
continue;
- sbuf_finish(ob[u]);
- sbuf_clear(ob[u]);
+ vsb_finish(ob[u]);
+ vsb_clear(ob[u]);
}
}
u = (p[2] << 8) | p[3];
if (ob[u] == NULL) {
- ob[u] = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ ob[u] = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(ob[u] != NULL);
}
varnishtop_CFLAGS = -include config.h
varnishtop_LDADD = \
+ $(top_builddir)/lib/libvarnish/libvarnish.la \
$(top_builddir)/lib/libvarnishapi/libvarnishapi.la \
- $(top_builddir)/lib/libsbuf/libsbuf.a \
-lcurses
#include <unistd.h>
#include <assert.h>
#include <curses.h>
-#include <sbuf.h>
+#include <vsb.h>
#include <vis.h>
#include "shmlog.h"
include/Makefile
lib/Makefile
lib/libcompat/Makefile
- lib/libsbuf/Makefile
lib/libvarnish/Makefile
lib/libvarnishapi/Makefile
lib/libvcl/Makefile
libvcl.h \
miniobj.h \
queue.h \
- sbuf.h \
+ vsb.h \
shmlog.h \
shmlog_tags.h \
stat_field.h \
*/
struct cli {
- struct sbuf *sb;
+ struct vsb *sb;
enum cli_status_e result;
};
* $Id$
*/
-char *VCC_Compile(struct sbuf *sb, const char *b, const char *e);
-char *VCC_CompileFile(struct sbuf *sb, const char *fn);
+char *VCC_Compile(struct vsb *sb, const char *b, const char *e);
+char *VCC_CompileFile(struct vsb *sb, const char *fn);
void VCC_InitCompile(const char *default_vcl);
*/
struct sess;
-struct sbuf;
+struct vsb;
struct backend;
struct VCL_conf;
void VRT_re_init(void **, const char *);
void VRT_re_fini(void *);
int VRT_re_match(const char *, void *re);
-int VRT_re_test(struct sbuf *, const char *);
+int VRT_re_test(struct vsb *, const char *);
void VRT_count(struct sess *, unsigned);
int VRT_rewrite(const char *, const char *);
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
- * $FreeBSD: src/sys/sys/sbuf.h,v 1.14 2004/07/09 11:35:30 des Exp $
+ * $FreeBSD: src/sys/sys/vsb.h,v 1.14 2004/07/09 11:35:30 des Exp $
*/
-#ifndef SBUF_H_INCLUDED
-#define SBUF_H_INCLUDED
+#ifndef VSB_H_INCLUDED
+#define VSB_H_INCLUDED
/*
* Structure definition
*/
-struct sbuf {
+struct vsb {
char *s_buf; /* storage buffer */
void *s_unused; /* binary compatibility. */
int s_size; /* size of storage buffer */
int s_len; /* current length of string */
-#define SBUF_FIXEDLEN 0x00000000 /* fixed length buffer (default) */
-#define SBUF_AUTOEXTEND 0x00000001 /* automatically extend buffer */
-#define SBUF_USRFLAGMSK 0x0000ffff /* mask of flags the user may specify */
-#define SBUF_DYNAMIC 0x00010000 /* s_buf must be freed */
-#define SBUF_FINISHED 0x00020000 /* set by sbuf_finish() */
-#define SBUF_OVERFLOWED 0x00040000 /* sbuf overflowed */
-#define SBUF_DYNSTRUCT 0x00080000 /* sbuf must be freed */
+#define VSB_FIXEDLEN 0x00000000 /* fixed length buffer (default) */
+#define VSB_AUTOEXTEND 0x00000001 /* automatically extend buffer */
+#define VSB_USRFLAGMSK 0x0000ffff /* mask of flags the user may specify */
+#define VSB_DYNAMIC 0x00010000 /* s_buf must be freed */
+#define VSB_FINISHED 0x00020000 /* set by vsb_finish() */
+#define VSB_OVERFLOWED 0x00040000 /* vsb overflowed */
+#define VSB_DYNSTRUCT 0x00080000 /* vsb must be freed */
int s_flags; /* flags */
};
/*
* API functions
*/
-struct sbuf *sbuf_new(struct sbuf *, char *, int, int);
-void sbuf_clear(struct sbuf *);
-int sbuf_setpos(struct sbuf *, int);
-int sbuf_bcat(struct sbuf *, const void *, size_t);
-int sbuf_bcpy(struct sbuf *, const void *, size_t);
-int sbuf_cat(struct sbuf *, const char *);
-int sbuf_cpy(struct sbuf *, const char *);
-int sbuf_printf(struct sbuf *, const char *, ...) /* __printflike(2, 3) */;
+struct vsb *vsb_new(struct vsb *, char *, int, int);
+void vsb_clear(struct vsb *);
+int vsb_setpos(struct vsb *, int);
+int vsb_bcat(struct vsb *, const void *, size_t);
+int vsb_bcpy(struct vsb *, const void *, size_t);
+int vsb_cat(struct vsb *, const char *);
+int vsb_cpy(struct vsb *, const char *);
+int vsb_printf(struct vsb *, const char *, ...) /* __printflike(2, 3) */;
#ifdef va_start
-int sbuf_vprintf(struct sbuf *, const char *, va_list) /* __printflike(2, 0) */;
+int vsb_vprintf(struct vsb *, const char *, va_list) /* __printflike(2, 0) */;
#endif
-int sbuf_putc(struct sbuf *, int);
-int sbuf_trim(struct sbuf *);
-int sbuf_overflowed(struct sbuf *);
-void sbuf_finish(struct sbuf *);
-char *sbuf_data(struct sbuf *);
-int sbuf_len(struct sbuf *);
-int sbuf_done(struct sbuf *);
-void sbuf_delete(struct sbuf *);
+int vsb_putc(struct vsb *, int);
+int vsb_trim(struct vsb *);
+int vsb_overflowed(struct vsb *);
+void vsb_finish(struct vsb *);
+char *vsb_data(struct vsb *);
+int vsb_len(struct vsb *);
+int vsb_done(struct vsb *);
+void vsb_delete(struct vsb *);
__END_DECLS
#endif
SUBDIRS = \
libcompat \
- libsbuf \
libvarnish \
libvarnishapi \
libvcl
+++ /dev/null
-# $Id$
-
-INCLUDES = -I$(top_srcdir)/include
-
-lib_LIBRARIES = libsbuf.a
-
-libsbuf_a_SOURCES = \
- sbuf.c
-
-libsbuf_a_CFLAGS = -include config.h
+++ /dev/null
-/*-
- * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Coïdan Smørgrav
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer
- * in this position and unchanged.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * $Id$
- * $FreeBSD: src/sys/kern/subr_sbuf.c,v 1.30 2005/12/23 11:49:53 phk Exp $
- */
-
-#include <ctype.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "sbuf.h"
-
-#define KASSERT(e, m)
-#define SBMALLOC(size) malloc(size)
-#define SBFREE(buf) free(buf)
-#define min(x,y) (x < y ? x : y)
-
-/*
- * Predicates
- */
-#define SBUF_ISDYNAMIC(s) ((s)->s_flags & SBUF_DYNAMIC)
-#define SBUF_ISDYNSTRUCT(s) ((s)->s_flags & SBUF_DYNSTRUCT)
-#define SBUF_ISFINISHED(s) ((s)->s_flags & SBUF_FINISHED)
-#define SBUF_HASOVERFLOWED(s) ((s)->s_flags & SBUF_OVERFLOWED)
-#define SBUF_HASROOM(s) ((s)->s_len < (s)->s_size - 1)
-#define SBUF_FREESPACE(s) ((s)->s_size - (s)->s_len - 1)
-#define SBUF_CANEXTEND(s) ((s)->s_flags & SBUF_AUTOEXTEND)
-
-/*
- * Set / clear flags
- */
-#define SBUF_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
-#define SBUF_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
-
-#define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */
-#define SBUF_MAXEXTENDSIZE 4096
-#define SBUF_MAXEXTENDINCR 4096
-
-/*
- * Debugging support
- */
-#if !defined(NDEBUG)
-static void
-_assert_sbuf_integrity(const char *fun, struct sbuf *s)
-{
- KASSERT(s != NULL,
- ("%s called with a NULL sbuf pointer", fun));
- KASSERT(s->s_buf != NULL,
- ("%s called with uninitialized or corrupt sbuf", fun));
- KASSERT(s->s_len < s->s_size,
- ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
-}
-
-static void
-_assert_sbuf_state(const char *fun, struct sbuf *s, int state)
-{
- KASSERT((s->s_flags & SBUF_FINISHED) == state,
- ("%s called with %sfinished or corrupt sbuf", fun,
- (state ? "un" : "")));
-}
-#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
-#define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i))
-#else
-#define assert_sbuf_integrity(s) do { } while (0)
-#define assert_sbuf_state(s, i) do { } while (0)
-#endif
-
-static int
-sbuf_extendsize(int size)
-{
- int newsize;
-
- newsize = SBUF_MINEXTENDSIZE;
- while (newsize < size) {
- if (newsize < (int)SBUF_MAXEXTENDSIZE)
- newsize *= 2;
- else
- newsize += SBUF_MAXEXTENDINCR;
- }
-
- return (newsize);
-}
-
-
-/*
- * Extend an sbuf.
- */
-static int
-sbuf_extend(struct sbuf *s, int addlen)
-{
- char *newbuf;
- int newsize;
-
- if (!SBUF_CANEXTEND(s))
- return (-1);
-
- newsize = sbuf_extendsize(s->s_size + addlen);
- newbuf = (char *)SBMALLOC(newsize);
- if (newbuf == NULL)
- return (-1);
- bcopy(s->s_buf, newbuf, s->s_size);
- if (SBUF_ISDYNAMIC(s))
- SBFREE(s->s_buf);
- else
- SBUF_SETFLAG(s, SBUF_DYNAMIC);
- s->s_buf = newbuf;
- s->s_size = newsize;
- return (0);
-}
-
-/*
- * Initialize an sbuf.
- * If buf is non-NULL, it points to a static or already-allocated string
- * big enough to hold at least length characters.
- */
-struct sbuf *
-sbuf_new(struct sbuf *s, char *buf, int length, int flags)
-{
- KASSERT(length >= 0,
- ("attempt to create an sbuf of negative length (%d)", length));
- KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
- ("%s called with invalid flags", __func__));
-
- flags &= SBUF_USRFLAGMSK;
- if (s == NULL) {
- s = (struct sbuf *)SBMALLOC(sizeof *s);
- if (s == NULL)
- return (NULL);
- bzero(s, sizeof *s);
- s->s_flags = flags;
- SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
- } else {
- bzero(s, sizeof *s);
- s->s_flags = flags;
- }
- s->s_size = length;
- if (buf) {
- s->s_buf = buf;
- return (s);
- }
- if (flags & SBUF_AUTOEXTEND)
- s->s_size = sbuf_extendsize(s->s_size);
- s->s_buf = (char *)SBMALLOC(s->s_size);
- if (s->s_buf == NULL) {
- if (SBUF_ISDYNSTRUCT(s))
- SBFREE(s);
- return (NULL);
- }
- SBUF_SETFLAG(s, SBUF_DYNAMIC);
- return (s);
-}
-
-/*
- * Clear an sbuf and reset its position.
- */
-void
-sbuf_clear(struct sbuf *s)
-{
- assert_sbuf_integrity(s);
- /* don't care if it's finished or not */
-
- SBUF_CLEARFLAG(s, SBUF_FINISHED);
- SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
- s->s_len = 0;
-}
-
-/*
- * Set the sbuf's end position to an arbitrary value.
- * Effectively truncates the sbuf at the new position.
- */
-int
-sbuf_setpos(struct sbuf *s, int pos)
-{
- assert_sbuf_integrity(s);
- assert_sbuf_state(s, 0);
-
- KASSERT(pos >= 0,
- ("attempt to seek to a negative position (%d)", pos));
- KASSERT(pos < s->s_size,
- ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size));
-
- if (pos < 0 || pos > s->s_len)
- return (-1);
- s->s_len = pos;
- return (0);
-}
-
-/*
- * Append a byte string to an sbuf.
- */
-int
-sbuf_bcat(struct sbuf *s, const void *buf, size_t len)
-{
- const char *str = buf;
-
- assert_sbuf_integrity(s);
- assert_sbuf_state(s, 0);
-
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
-
- for (; len; len--) {
- if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0)
- break;
- s->s_buf[s->s_len++] = *str++;
- }
- if (len) {
- SBUF_SETFLAG(s, SBUF_OVERFLOWED);
- return (-1);
- }
- return (0);
-}
-
-/*
- * Copy a byte string into an sbuf.
- */
-int
-sbuf_bcpy(struct sbuf *s, const void *buf, size_t len)
-{
- assert_sbuf_integrity(s);
- assert_sbuf_state(s, 0);
-
- sbuf_clear(s);
- return (sbuf_bcat(s, buf, len));
-}
-
-/*
- * Append a string to an sbuf.
- */
-int
-sbuf_cat(struct sbuf *s, const char *str)
-{
- assert_sbuf_integrity(s);
- assert_sbuf_state(s, 0);
-
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
-
- while (*str) {
- if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0)
- break;
- s->s_buf[s->s_len++] = *str++;
- }
- if (*str) {
- SBUF_SETFLAG(s, SBUF_OVERFLOWED);
- return (-1);
- }
- return (0);
-}
-
-/*
- * Copy a string into an sbuf.
- */
-int
-sbuf_cpy(struct sbuf *s, const char *str)
-{
- assert_sbuf_integrity(s);
- assert_sbuf_state(s, 0);
-
- sbuf_clear(s);
- return (sbuf_cat(s, str));
-}
-
-/*
- * Format the given argument list and append the resulting string to an sbuf.
- */
-int
-sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
-{
- va_list ap_copy;
- int len;
-
- assert_sbuf_integrity(s);
- assert_sbuf_state(s, 0);
-
- KASSERT(fmt != NULL,
- ("%s called with a NULL format string", __func__));
-
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
-
- do {
- va_copy(ap_copy, ap);
- len = vsnprintf(&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
- fmt, ap_copy);
- va_end(ap_copy);
- } while (len > SBUF_FREESPACE(s) &&
- sbuf_extend(s, len - SBUF_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, hence the min() calculation below.
- */
- s->s_len += min(len, SBUF_FREESPACE(s));
- if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s))
- SBUF_SETFLAG(s, SBUF_OVERFLOWED);
-
- KASSERT(s->s_len < s->s_size,
- ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
-
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
- return (0);
-}
-
-/*
- * Format the given arguments and append the resulting string to an sbuf.
- */
-int
-sbuf_printf(struct sbuf *s, const char *fmt, ...)
-{
- va_list ap;
- int result;
-
- va_start(ap, fmt);
- result = sbuf_vprintf(s, fmt, ap);
- va_end(ap);
- return(result);
-}
-
-/*
- * Append a character to an sbuf.
- */
-int
-sbuf_putc(struct sbuf *s, int c)
-{
- assert_sbuf_integrity(s);
- assert_sbuf_state(s, 0);
-
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
-
- if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) {
- SBUF_SETFLAG(s, SBUF_OVERFLOWED);
- return (-1);
- }
- if (c != '\0')
- s->s_buf[s->s_len++] = c;
- return (0);
-}
-
-/*
- * Trim whitespace characters from end of an sbuf.
- */
-int
-sbuf_trim(struct sbuf *s)
-{
- assert_sbuf_integrity(s);
- assert_sbuf_state(s, 0);
-
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
-
- while (s->s_len && isspace(s->s_buf[s->s_len-1]))
- --s->s_len;
-
- return (0);
-}
-
-/*
- * Check if an sbuf overflowed
- */
-int
-sbuf_overflowed(struct sbuf *s)
-{
- return SBUF_HASOVERFLOWED(s);
-}
-
-/*
- * Finish off an sbuf.
- */
-void
-sbuf_finish(struct sbuf *s)
-{
- assert_sbuf_integrity(s);
- assert_sbuf_state(s, 0);
-
- s->s_buf[s->s_len] = '\0';
- SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
- SBUF_SETFLAG(s, SBUF_FINISHED);
-}
-
-/*
- * Return a pointer to the sbuf data.
- */
-char *
-sbuf_data(struct sbuf *s)
-{
- assert_sbuf_integrity(s);
- assert_sbuf_state(s, SBUF_FINISHED);
-
- return s->s_buf;
-}
-
-/*
- * Return the length of the sbuf data.
- */
-int
-sbuf_len(struct sbuf *s)
-{
- assert_sbuf_integrity(s);
- /* don't care if it's finished or not */
-
- if (SBUF_HASOVERFLOWED(s))
- return (-1);
- return s->s_len;
-}
-
-/*
- * Clear an sbuf, free its buffer if necessary.
- */
-void
-sbuf_delete(struct sbuf *s)
-{
- int isdyn;
-
- assert_sbuf_integrity(s);
- /* don't care if it's finished or not */
-
- if (SBUF_ISDYNAMIC(s))
- SBFREE(s->s_buf);
- isdyn = SBUF_ISDYNSTRUCT(s);
- bzero(s, sizeof *s);
- if (isdyn)
- SBFREE(s);
-}
-
-/*
- * Check if an sbuf has been finished.
- */
-int
-sbuf_done(struct sbuf *s)
-{
-
- return(SBUF_ISFINISHED(s));
-}
binary_heap.c \
cli.c \
cli_common.c \
- time.c
+ time.c \
+ vsb.c
libvarnish_la_CFLAGS = -include config.h
#include <sys/wait.h>
-#include "sbuf.h"
+#include "vsb.h"
#include "cli.h"
#include "cli_priv.h"
va_list ap;
va_start(ap, fmt);
- sbuf_vprintf(cli->sb, fmt, ap);
+ vsb_vprintf(cli->sb, fmt, ap);
va_end(ap);
}
*/
i = snprintf(res, sizeof res,
- "%-3d %-8d\n", cli->result, sbuf_len(cli->sb));
+ "%-3d %-8d\n", cli->result, vsb_len(cli->sb));
assert(i == CLI_LINE0_LEN);
iov[0].iov_base = (void*)(uintptr_t)res;
- iov[1].iov_base = (void*)(uintptr_t)sbuf_data(cli->sb);
+ iov[1].iov_base = (void*)(uintptr_t)vsb_data(cli->sb);
iov[2].iov_base = (void*)(uintptr_t)"\n";
for (l = i = 0; i < 3; i++) {
iov[i].iov_len = strlen(iov[i].iov_base);
.\" SUCH DAMAGE.
.\"
.\" $Id$
-.\" $FreeBSD: src/share/man/man9/sbuf.9,v 1.25 2005/12/23 11:49:52 phk Exp $
+.\" $FreeBSD: src/share/man/man9/vsb.9,v 1.25 2005/12/23 11:49:52 phk Exp $
.\"
.Dd March 14, 2006
-.Dt SBUF 3
+.Dt VSB 3
.Os
.Sh NAME
-.Nm sbuf_new ,
-.Nm sbuf_clear ,
-.Nm sbuf_setpos ,
-.Nm sbuf_bcat ,
-.Nm sbuf_bcpy ,
-.Nm sbuf_cat ,
-.Nm sbuf_cpy ,
-.Nm sbuf_printf ,
-.Nm sbuf_vprintf ,
-.Nm sbuf_putc ,
-.Nm sbuf_trim ,
-.Nm sbuf_overflowed ,
-.Nm sbuf_finish ,
-.Nm sbuf_data ,
-.Nm sbuf_len ,
-.Nm sbuf_done ,
-.Nm sbuf_delete
+.Nm vsb_new ,
+.Nm vsb_clear ,
+.Nm vsb_setpos ,
+.Nm vsb_bcat ,
+.Nm vsb_bcpy ,
+.Nm vsb_cat ,
+.Nm vsb_cpy ,
+.Nm vsb_printf ,
+.Nm vsb_vprintf ,
+.Nm vsb_putc ,
+.Nm vsb_trim ,
+.Nm vsb_overflowed ,
+.Nm vsb_finish ,
+.Nm vsb_data ,
+.Nm vsb_len ,
+.Nm vsb_done ,
+.Nm vsb_delete
.Nd safe string formatting
.Sh LIBRARY
-.Lb libsbuf
+.Lb libvarnish
.Sh SYNOPSIS
-.In sbuf.h
-.Ft struct sbuf *
-.Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags"
+.In vsb.h
+.Ft struct vsb *
+.Fn vsb_new "struct vsb *s" "char *buf" "int length" "int flags"
.Ft void
-.Fn sbuf_clear "struct sbuf *s"
+.Fn vsb_clear "struct vsb *s"
.Ft int
-.Fn sbuf_setpos "struct sbuf *s" "int pos"
+.Fn vsb_setpos "struct vsb *s" "int pos"
.Ft int
-.Fn sbuf_bcat "struct sbuf *s" "const void *buf" "size_t len"
+.Fn vsb_bcat "struct vsb *s" "const void *buf" "size_t len"
.Ft int
-.Fn sbuf_bcpy "struct sbuf *s" "const void *buf" "size_t len"
+.Fn vsb_bcpy "struct vsb *s" "const void *buf" "size_t len"
.Ft int
-.Fn sbuf_cat "struct sbuf *s" "const char *str"
+.Fn vsb_cat "struct vsb *s" "const char *str"
.Ft int
-.Fn sbuf_cpy "struct sbuf *s" "const char *str"
+.Fn vsb_cpy "struct vsb *s" "const char *str"
.Ft int
-.Fn sbuf_printf "struct sbuf *s" "const char *fmt" "..."
+.Fn vsb_printf "struct vsb *s" "const char *fmt" "..."
.Ft int
-.Fn sbuf_vprintf "struct sbuf *s" "const char *fmt" "va_list ap"
+.Fn vsb_vprintf "struct vsb *s" "const char *fmt" "va_list ap"
.Ft int
-.Fn sbuf_putc "struct sbuf *s" "int c"
+.Fn vsb_putc "struct vsb *s" "int c"
.Ft int
-.Fn sbuf_trim "struct sbuf *s"
+.Fn vsb_trim "struct vsb *s"
.Ft int
-.Fn sbuf_overflowed "struct sbuf *s"
+.Fn vsb_overflowed "struct vsb *s"
.Ft void
-.Fn sbuf_finish "struct sbuf *s"
+.Fn vsb_finish "struct vsb *s"
.Ft char *
-.Fn sbuf_data "struct sbuf *s"
+.Fn vsb_data "struct vsb *s"
.Ft int
-.Fn sbuf_len "struct sbuf *s"
+.Fn vsb_len "struct vsb *s"
.Ft int
-.Fn sbuf_done "struct sbuf *s"
+.Fn vsb_done "struct vsb *s"
.Ft void
-.Fn sbuf_delete "struct sbuf *s"
+.Fn vsb_delete "struct vsb *s"
.Sh DESCRIPTION
The
-.Nm sbuf
+.Nm vsb
family of functions allows one to safely allocate, construct and
release bounded null-terminated strings in kernel space.
Instead of arrays of characters, these functions operate on structures
called
-.Fa sbufs ,
+.Fa vsbs ,
defined in
-.In sys/sbuf.h .
+.In sys/vsb.h .
.Pp
The
-.Fn sbuf_new
+.Fn vsb_new
function initializes the
-.Fa sbuf
+.Fa vsb
pointed to by its first argument.
If that pointer is
.Dv NULL ,
-.Fn sbuf_new
+.Fn vsb_new
allocates a
-.Vt struct sbuf
+.Vt struct vsb
using
.Xr malloc 9 .
The
argument is a pointer to a buffer in which to store the actual string;
if it is
.Dv NULL ,
-.Fn sbuf_new
+.Fn vsb_new
will allocate one using
.Xr malloc 9 .
The
The fourth argument,
.Fa flags ,
may be comprised of the following flags:
-.Bl -tag -width ".Dv SBUF_AUTOEXTEND"
-.It Dv SBUF_FIXEDLEN
+.Bl -tag -width ".Dv VSB_AUTOEXTEND"
+.It Dv VSB_FIXEDLEN
The storage buffer is fixed at its initial size.
-Attempting to extend the sbuf beyond this size results in an overflow condition.
-.It Dv SBUF_AUTOEXTEND
+Attempting to extend the vsb beyond this size results in an overflow condition.
+.It Dv VSB_AUTOEXTEND
This indicates that the storage buffer may be extended as necessary, so long
as resources allow, to hold additional data.
.El
.Fa length
characters.
The result of accessing that array directly while it is in use by the
-sbuf is undefined.
+vsb is undefined.
.Pp
The
-.Fn sbuf_delete
+.Fn vsb_delete
function clears the
-.Fa sbuf
+.Fa vsb
and frees any memory allocated for it.
There must be a call to
-.Fn sbuf_delete
+.Fn vsb_delete
for every call to
-.Fn sbuf_new .
-Any attempt to access the sbuf after it has been deleted will fail.
+.Fn vsb_new .
+Any attempt to access the vsb after it has been deleted will fail.
.Pp
The
-.Fn sbuf_clear
+.Fn vsb_clear
function invalidates the contents of the
-.Fa sbuf
+.Fa vsb
and resets its position to zero.
.Pp
The
-.Fn sbuf_setpos
+.Fn vsb_setpos
function sets the
-.Fa sbuf Ns 's
+.Fa vsb Ns 's
end position to
.Fa pos ,
which is a value between zero and one less than the size of the
storage buffer.
-This effectively truncates the sbuf at the new position.
+This effectively truncates the vsb at the new position.
.Pp
The
-.Fn sbuf_bcat
+.Fn vsb_bcat
function appends the first
.Fa len
bytes from the buffer
.Fa buf
to the
-.Fa sbuf .
+.Fa vsb .
.Pp
The
-.Fn sbuf_bcpy
+.Fn vsb_bcpy
function replaces the contents of the
-.Fa sbuf
+.Fa vsb
with the first
.Fa len
bytes from the buffer
.Fa buf .
.Pp
The
-.Fn sbuf_cat
+.Fn vsb_cat
function appends the NUL-terminated string
.Fa str
to the
-.Fa sbuf
+.Fa vsb
at the current position.
.Pp
The
-.Fn sbuf_cpy
+.Fn vsb_cpy
function replaces the contents of the
-.Fa sbuf
+.Fa vsb
with those of the NUL-terminated string
.Fa str .
This is equivalent to calling
-.Fn sbuf_cat
+.Fn vsb_cat
with a fresh
-.Fa sbuf
+.Fa vsb
or one which position has been reset to zero with
-.Fn sbuf_clear
+.Fn vsb_clear
or
-.Fn sbuf_setpos .
+.Fn vsb_setpos .
.Pp
The
-.Fn sbuf_printf
+.Fn vsb_printf
function formats its arguments according to the format string pointed
to by
.Fa fmt
and appends the resulting string to the
-.Fa sbuf
+.Fa vsb
at the current position.
.Pp
The
-.Fn sbuf_vprintf
+.Fn vsb_vprintf
function behaves the same as
-.Fn sbuf_printf
+.Fn vsb_printf
except that the arguments are obtained from the variable-length argument list
.Fa ap .
.Pp
The
-.Fn sbuf_putc
+.Fn vsb_putc
function appends the character
.Fa c
to the
-.Fa sbuf
+.Fa vsb
at the current position.
.Pp
The
-.Fn sbuf_trim
+.Fn vsb_trim
function removes trailing whitespace from the
-.Fa sbuf .
+.Fa vsb .
.Pp
The
-.Fn sbuf_overflowed
+.Fn vsb_overflowed
function returns a non-zero value if the
-.Fa sbuf
+.Fa vsb
overflowed.
.Pp
The
-.Fn sbuf_finish
+.Fn vsb_finish
function null-terminates the
-.Fa sbuf
+.Fa vsb
and marks it as finished, which means that it may no longer be
modified using
-.Fn sbuf_setpos ,
-.Fn sbuf_cat ,
-.Fn sbuf_cpy ,
-.Fn sbuf_printf
+.Fn vsb_setpos ,
+.Fn vsb_cat ,
+.Fn vsb_cpy ,
+.Fn vsb_printf
or
-.Fn sbuf_putc .
+.Fn vsb_putc .
.Pp
The
-.Fn sbuf_data
+.Fn vsb_data
and
-.Fn sbuf_len
+.Fn vsb_len
functions return the actual string and its length, respectively;
-.Fn sbuf_data
+.Fn vsb_data
only works on a finished
-.Fa sbuf .
-.Fn sbuf_done
-returns non-zero if the sbuf is finished.
+.Fa vsb .
+.Fn vsb_done
+returns non-zero if the vsb is finished.
.Sh NOTES
If an operation caused an
-.Fa sbuf
+.Fa vsb
to overflow, most subsequent operations on it will fail until the
-.Fa sbuf
+.Fa vsb
is finished using
-.Fn sbuf_finish
+.Fn vsb_finish
or reset using
-.Fn sbuf_clear ,
+.Fn vsb_clear ,
or its position is reset to a value between 0 and one less than the
size of its storage buffer using
-.Fn sbuf_setpos ,
+.Fn vsb_setpos ,
or it is reinitialized to a sufficiently short string using
-.Fn sbuf_cpy .
+.Fn vsb_cpy .
.Sh RETURN VALUES
-.Fn sbuf_new
+.Fn vsb_new
returns
.Dv NULL
if it failed to allocate a storage buffer, and a pointer to the new
-.Fa sbuf
+.Fa vsb
otherwise.
.Pp
-.Fn sbuf_setpos
+.Fn vsb_setpos
returns \-1 if
.Fa pos
was invalid, and zero otherwise.
.Pp
-.Fn sbuf_cat ,
-.Fn sbuf_cpy ,
-.Fn sbuf_printf ,
-.Fn sbuf_putc ,
+.Fn vsb_cat ,
+.Fn vsb_cpy ,
+.Fn vsb_printf ,
+.Fn vsb_putc ,
and
-.Fn sbuf_trim
+.Fn vsb_trim
all return \-1 if the buffer overflowed, and zero otherwise.
.Pp
-.Fn sbuf_overflowed
+.Fn vsb_overflowed
returns a non-zero value if the buffer overflowed, and zero otherwise.
.Pp
-.Fn sbuf_data
+.Fn vsb_data
and
-.Fn sbuf_len
+.Fn vsb_len
return
.Dv NULL
and \-1, respectively, if the buffer overflowed.
.Xr strcpy 3
.Sh HISTORY
The
-.Nm sbuf
+.Nm vsb
family of functions first appeared in
.Fx 4.4 .
.Sh AUTHORS
.An -nosplit
The
-.Nm sbuf
+.Nm vsb
family of functions was designed by
.An Poul-Henning Kamp Aq phk@FreeBSD.org
and implemented by
--- /dev/null
+/*-
+ * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Coïdan Smørgrav
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer
+ * in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $Id$
+ * $FreeBSD: src/sys/kern/subr_sbuf.c,v 1.30 2005/12/23 11:49:53 phk Exp $
+ */
+
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "vsb.h"
+
+#define KASSERT(e, m)
+#define SBMALLOC(size) malloc(size)
+#define SBFREE(buf) free(buf)
+#define min(x,y) (x < y ? x : y)
+
+/*
+ * Predicates
+ */
+#define VSB_ISDYNAMIC(s) ((s)->s_flags & VSB_DYNAMIC)
+#define VSB_ISDYNSTRUCT(s) ((s)->s_flags & VSB_DYNSTRUCT)
+#define VSB_ISFINISHED(s) ((s)->s_flags & VSB_FINISHED)
+#define VSB_HASOVERFLOWED(s) ((s)->s_flags & VSB_OVERFLOWED)
+#define VSB_HASROOM(s) ((s)->s_len < (s)->s_size - 1)
+#define VSB_FREESPACE(s) ((s)->s_size - (s)->s_len - 1)
+#define VSB_CANEXTEND(s) ((s)->s_flags & VSB_AUTOEXTEND)
+
+/*
+ * Set / clear flags
+ */
+#define VSB_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0)
+#define VSB_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0)
+
+#define VSB_MINEXTENDSIZE 16 /* Should be power of 2. */
+#define VSB_MAXEXTENDSIZE 4096
+#define VSB_MAXEXTENDINCR 4096
+
+/*
+ * Debugging support
+ */
+#if !defined(NDEBUG)
+static void
+_vsb_assert_integrity(const char *fun, struct vsb *s)
+{
+ KASSERT(s != NULL,
+ ("%s called with a NULL vsb pointer", fun));
+ KASSERT(s->s_buf != NULL,
+ ("%s called with uninitialized or corrupt vsb", fun));
+ KASSERT(s->s_len < s->s_size,
+ ("wrote past end of vsb (%d >= %d)", s->s_len, s->s_size));
+}
+
+static void
+_vsb_assert_state(const char *fun, struct vsb *s, int state)
+{
+ KASSERT((s->s_flags & VSB_FINISHED) == state,
+ ("%s called with %sfinished or corrupt vsb", fun,
+ (state ? "un" : "")));
+}
+#define vsb_assert_integrity(s) _vsb_assert_integrity(__func__, (s))
+#define vsb_assert_state(s, i) _vsb_assert_state(__func__, (s), (i))
+#else
+#define vsb_assert_integrity(s) do { } while (0)
+#define vsb_assert_state(s, i) do { } while (0)
+#endif
+
+static int
+vsb_extendsize(int size)
+{
+ int newsize;
+
+ newsize = VSB_MINEXTENDSIZE;
+ while (newsize < size) {
+ if (newsize < (int)VSB_MAXEXTENDSIZE)
+ newsize *= 2;
+ else
+ newsize += VSB_MAXEXTENDINCR;
+ }
+
+ return (newsize);
+}
+
+
+/*
+ * Extend an vsb.
+ */
+static int
+vsb_extend(struct vsb *s, int addlen)
+{
+ char *newbuf;
+ int newsize;
+
+ if (!VSB_CANEXTEND(s))
+ return (-1);
+
+ newsize = vsb_extendsize(s->s_size + addlen);
+ newbuf = (char *)SBMALLOC(newsize);
+ if (newbuf == NULL)
+ return (-1);
+ bcopy(s->s_buf, newbuf, s->s_size);
+ if (VSB_ISDYNAMIC(s))
+ SBFREE(s->s_buf);
+ else
+ VSB_SETFLAG(s, VSB_DYNAMIC);
+ s->s_buf = newbuf;
+ s->s_size = newsize;
+ return (0);
+}
+
+/*
+ * Initialize an vsb.
+ * If buf is non-NULL, it points to a static or already-allocated string
+ * big enough to hold at least length characters.
+ */
+struct vsb *
+vsb_new(struct vsb *s, char *buf, int length, int flags)
+{
+ KASSERT(length >= 0,
+ ("attempt to create an vsb of negative length (%d)", length));
+ KASSERT((flags & ~VSB_USRFLAGMSK) == 0,
+ ("%s called with invalid flags", __func__));
+
+ flags &= VSB_USRFLAGMSK;
+ if (s == NULL) {
+ s = (struct vsb *)SBMALLOC(sizeof *s);
+ if (s == NULL)
+ return (NULL);
+ bzero(s, sizeof *s);
+ s->s_flags = flags;
+ VSB_SETFLAG(s, VSB_DYNSTRUCT);
+ } else {
+ bzero(s, sizeof *s);
+ s->s_flags = flags;
+ }
+ s->s_size = length;
+ if (buf) {
+ s->s_buf = buf;
+ return (s);
+ }
+ if (flags & VSB_AUTOEXTEND)
+ s->s_size = vsb_extendsize(s->s_size);
+ s->s_buf = (char *)SBMALLOC(s->s_size);
+ if (s->s_buf == NULL) {
+ if (VSB_ISDYNSTRUCT(s))
+ SBFREE(s);
+ return (NULL);
+ }
+ VSB_SETFLAG(s, VSB_DYNAMIC);
+ return (s);
+}
+
+/*
+ * Clear an vsb and reset its position.
+ */
+void
+vsb_clear(struct vsb *s)
+{
+ vsb_assert_integrity(s);
+ /* don't care if it's finished or not */
+
+ VSB_CLEARFLAG(s, VSB_FINISHED);
+ VSB_CLEARFLAG(s, VSB_OVERFLOWED);
+ s->s_len = 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, int pos)
+{
+ vsb_assert_integrity(s);
+ vsb_assert_state(s, 0);
+
+ KASSERT(pos >= 0,
+ ("attempt to seek to a negative position (%d)", pos));
+ KASSERT(pos < s->s_size,
+ ("attempt to seek past end of vsb (%d >= %d)", pos, 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, size_t len)
+{
+ const char *str = buf;
+
+ vsb_assert_integrity(s);
+ vsb_assert_state(s, 0);
+
+ if (VSB_HASOVERFLOWED(s))
+ return (-1);
+
+ for (; len; len--) {
+ if (!VSB_HASROOM(s) && vsb_extend(s, len) < 0)
+ break;
+ s->s_buf[s->s_len++] = *str++;
+ }
+ if (len) {
+ VSB_SETFLAG(s, VSB_OVERFLOWED);
+ return (-1);
+ }
+ return (0);
+}
+
+/*
+ * Copy a byte string into an vsb.
+ */
+int
+vsb_bcpy(struct vsb *s, const void *buf, size_t len)
+{
+ vsb_assert_integrity(s);
+ vsb_assert_state(s, 0);
+
+ vsb_clear(s);
+ return (vsb_bcat(s, buf, len));
+}
+
+/*
+ * Append a string to an vsb.
+ */
+int
+vsb_cat(struct vsb *s, const char *str)
+{
+ vsb_assert_integrity(s);
+ vsb_assert_state(s, 0);
+
+ if (VSB_HASOVERFLOWED(s))
+ return (-1);
+
+ while (*str) {
+ if (!VSB_HASROOM(s) && vsb_extend(s, strlen(str)) < 0)
+ break;
+ s->s_buf[s->s_len++] = *str++;
+ }
+ if (*str) {
+ VSB_SETFLAG(s, VSB_OVERFLOWED);
+ return (-1);
+ }
+ return (0);
+}
+
+/*
+ * Copy a string into an vsb.
+ */
+int
+vsb_cpy(struct vsb *s, const char *str)
+{
+ vsb_assert_integrity(s);
+ vsb_assert_state(s, 0);
+
+ vsb_clear(s);
+ return (vsb_cat(s, str));
+}
+
+/*
+ * 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;
+
+ vsb_assert_integrity(s);
+ vsb_assert_state(s, 0);
+
+ KASSERT(fmt != NULL,
+ ("%s called with a NULL format string", __func__));
+
+ if (VSB_HASOVERFLOWED(s))
+ return (-1);
+
+ 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, hence the min() calculation below.
+ */
+ s->s_len += min(len, VSB_FREESPACE(s));
+ if (!VSB_HASROOM(s) && !VSB_CANEXTEND(s))
+ VSB_SETFLAG(s, VSB_OVERFLOWED);
+
+ KASSERT(s->s_len < s->s_size,
+ ("wrote past end of vsb (%d >= %d)", s->s_len, s->s_size));
+
+ if (VSB_HASOVERFLOWED(s))
+ return (-1);
+ return (0);
+}
+
+/*
+ * Format the given arguments and append the resulting string to an vsb.
+ */
+int
+vsb_printf(struct vsb *s, const char *fmt, ...)
+{
+ va_list ap;
+ int result;
+
+ va_start(ap, fmt);
+ result = vsb_vprintf(s, fmt, ap);
+ va_end(ap);
+ return(result);
+}
+
+/*
+ * Append a character to an vsb.
+ */
+int
+vsb_putc(struct vsb *s, int c)
+{
+ vsb_assert_integrity(s);
+ vsb_assert_state(s, 0);
+
+ if (VSB_HASOVERFLOWED(s))
+ return (-1);
+
+ if (!VSB_HASROOM(s) && vsb_extend(s, 1) < 0) {
+ VSB_SETFLAG(s, VSB_OVERFLOWED);
+ return (-1);
+ }
+ if (c != '\0')
+ s->s_buf[s->s_len++] = c;
+ return (0);
+}
+
+/*
+ * Trim whitespace characters from end of an vsb.
+ */
+int
+vsb_trim(struct vsb *s)
+{
+ vsb_assert_integrity(s);
+ vsb_assert_state(s, 0);
+
+ if (VSB_HASOVERFLOWED(s))
+ return (-1);
+
+ while (s->s_len && isspace(s->s_buf[s->s_len-1]))
+ --s->s_len;
+
+ return (0);
+}
+
+/*
+ * Check if an vsb overflowed
+ */
+int
+vsb_overflowed(struct vsb *s)
+{
+ return VSB_HASOVERFLOWED(s);
+}
+
+/*
+ * Finish off an vsb.
+ */
+void
+vsb_finish(struct vsb *s)
+{
+ vsb_assert_integrity(s);
+ vsb_assert_state(s, 0);
+
+ s->s_buf[s->s_len] = '\0';
+ VSB_CLEARFLAG(s, VSB_OVERFLOWED);
+ VSB_SETFLAG(s, VSB_FINISHED);
+}
+
+/*
+ * Return a pointer to the vsb data.
+ */
+char *
+vsb_data(struct vsb *s)
+{
+ vsb_assert_integrity(s);
+ vsb_assert_state(s, VSB_FINISHED);
+
+ return s->s_buf;
+}
+
+/*
+ * Return the length of the vsb data.
+ */
+int
+vsb_len(struct vsb *s)
+{
+ vsb_assert_integrity(s);
+ /* don't care if it's finished or not */
+
+ if (VSB_HASOVERFLOWED(s))
+ return (-1);
+ return s->s_len;
+}
+
+/*
+ * Clear an vsb, free its buffer if necessary.
+ */
+void
+vsb_delete(struct vsb *s)
+{
+ int isdyn;
+
+ vsb_assert_integrity(s);
+ /* don't care if it's finished or not */
+
+ if (VSB_ISDYNAMIC(s))
+ SBFREE(s->s_buf);
+ isdyn = VSB_ISDYNSTRUCT(s);
+ bzero(s, sizeof *s);
+ if (isdyn)
+ SBFREE(s);
+}
+
+/*
+ * Check if an vsb has been finished.
+ */
+int
+vsb_done(struct vsb *s)
+{
+
+ return(VSB_ISFINISHED(s));
+}
#include <netdb.h>
#include <stdio.h>
#include <stdarg.h>
-#include <sbuf.h>
+#include <vsb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
vcc_NextToken(tl);
break;
default:
- sbuf_printf(tl->sb, "Illegal condition ");
+ vsb_printf(tl->sb, "Illegal condition ");
vcc_ErrToken(tl, tl->t);
- sbuf_printf(tl->sb, " on IP number variable\n");
- sbuf_printf(tl->sb, " only '==', '!=' and '~' are legal\n");
+ vsb_printf(tl->sb, " on IP number variable\n");
+ vsb_printf(tl->sb, " only '==', '!=' and '~' are legal\n");
vcc_ErrWhere(tl, tl->t);
break;
}
#include <stdio.h>
#include <printf.h>
#include <stdarg.h>
-#include <sbuf.h>
+#include <vsb.h>
#include <stdlib.h>
#include <string.h>
#include <queue.h>
} while (0)
/*--------------------------------------------------------------------
- * Printf output to the two sbufs, possibly indented
+ * Printf output to the two vsbs, possibly indented
*/
void
va_list ap;
if (indent)
- sbuf_printf(tl->fh, "%*.*s", tl->indent, tl->indent, "");
+ vsb_printf(tl->fh, "%*.*s", tl->indent, tl->indent, "");
va_start(ap, fmt);
- sbuf_vprintf(tl->fh, fmt, ap);
+ vsb_vprintf(tl->fh, fmt, ap);
va_end(ap);
}
va_list ap;
if (indent)
- sbuf_printf(tl->fc, "%*.*s", tl->indent, tl->indent, "");
+ vsb_printf(tl->fc, "%*.*s", tl->indent, tl->indent, "");
va_start(ap, fmt);
- sbuf_vprintf(tl->fc, fmt, ap);
+ vsb_vprintf(tl->fc, fmt, ap);
va_end(ap);
}
va_list ap;
if (indent)
- sbuf_printf(tl->fi, "%*.*s", tl->indent, tl->indent, "");
+ vsb_printf(tl->fi, "%*.*s", tl->indent, tl->indent, "");
va_start(ap, fmt);
- sbuf_vprintf(tl->fi, fmt, ap);
+ vsb_vprintf(tl->fi, fmt, ap);
va_end(ap);
}
va_list ap;
if (indent)
- sbuf_printf(tl->ff, "%*.*s", tl->indent, tl->indent, "");
+ vsb_printf(tl->ff, "%*.*s", tl->indent, tl->indent, "");
va_start(ap, fmt);
- sbuf_vprintf(tl->ff, fmt, ap);
+ vsb_vprintf(tl->ff, fmt, ap);
va_end(ap);
}
else if (vcc_IdIs(tl->t, "d"))
sc = 60.0 * 60.0 * 24.0;
else {
- sbuf_printf(tl->sb, "Unknown time unit ");
+ vsb_printf(tl->sb, "Unknown time unit ");
vcc_ErrToken(tl, tl->t);
- sbuf_printf(tl->sb, ". Legal are 's', 'm', 'h' and 'd'\n");
+ vsb_printf(tl->sb, ". Legal are 's', 'm', 'h' and 'd'\n");
vcc_ErrWhere(tl, tl->t);
return (1.0);
}
else if (vcc_IdIs(tl->t, "gb") || vcc_IdIs(tl->t, "Gb"))
sc = 1024.0 * 1024.0 * 1024.0;
else {
- sbuf_printf(tl->sb, "Unknown size unit ");
+ vsb_printf(tl->sb, "Unknown size unit ");
vcc_ErrToken(tl, tl->t);
- sbuf_printf(tl->sb, ". Legal are 'kb', 'mb' and 'gb'\n");
+ vsb_printf(tl->sb, ". Legal are 'kb', 'mb' and 'gb'\n");
vcc_ErrWhere(tl, tl->t);
return (1.0);
}
return (v);
return (HeaderVar(tl, t, v));
}
- sbuf_printf(tl->sb, "Unknown variable ");
+ vsb_printf(tl->sb, "Unknown variable ");
vcc_ErrToken(tl, t);
- sbuf_cat(tl->sb, "\nAt: ");
+ vsb_cat(tl->sb, "\nAt: ");
vcc_ErrWhere(tl, t);
return (NULL);
}
SizeVal(tl);
break;
default:
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"No conditions available for variable '%s'\n",
vp->name);
vcc_ErrWhere(tl, tl->t);
Fc(tl, 0, "\n");
break;
default:
- sbuf_printf(tl->sb, "Illegal condition ");
+ vsb_printf(tl->sb, "Illegal condition ");
vcc_ErrToken(tl, tl->t);
- sbuf_printf(tl->sb, " on integer variable\n");
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb, " on integer variable\n");
+ vsb_printf(tl->sb,
" only '==', '!=', '<', '>', '<=' and '>=' are legal\n");
vcc_ErrWhere(tl, tl->t);
break;
case TIME: L(tl, Cond_Int(vp, tl)); break;
/* XXX backend == */
default:
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"Variable '%s'"
" has no conditions that can be checked\n",
vp->name);
return;
}
} else {
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"Syntax error in condition, expected '(', '!' or"
" variable name, found ");
vcc_ErrToken(tl, tl->t);
- sbuf_printf(tl->sb, "\n");
+ vsb_printf(tl->sb, "\n");
vcc_ErrWhere(tl, tl->t);
return;
}
u & 0xff);
break;
}
- sbuf_printf(tl->sb, "Illegal assignment operator ");
+ vsb_printf(tl->sb, "Illegal assignment operator ");
vcc_ErrToken(tl, tl->t);
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
" only '=' is legal for IP numbers\n");
vcc_ErrWhere(tl, tl->t);
return;
vcc_NextToken(tl);
break;
}
- sbuf_printf(tl->sb, "Illegal assignment operator ");
+ vsb_printf(tl->sb, "Illegal assignment operator ");
vcc_ErrToken(tl, tl->t);
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
" only '=' is legal for backend\n");
vcc_ErrWhere(tl, tl->t);
return;
default:
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"Assignments not possible for '%s'\n", vp->name);
vcc_ErrWhere(tl, tl->t);
return;
}
return;
default:
- sbuf_printf(tl->sb, "Expected action, 'if' or '}'\n");
+ vsb_printf(tl->sb, "Expected action, 'if' or '}'\n");
vcc_ErrWhere(tl, at);
return;
}
Fc(tl, 1, "}\n");
return;
case EOI:
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"End of input while in compound statement\n");
tl->err = 1;
return;
vcc_NextToken(tl);
break;
default:
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"Assignments not possible for '%s'\n", vp->name);
vcc_ErrWhere(tl, tl->t);
return;
}
ExpectErr(tl, '}');
if (t_host == NULL) {
- sbuf_printf(tl->sb, "Backend '%T' has no hostname\n", t_be);
+ vsb_printf(tl->sb, "Backend '%T' has no hostname\n", t_be);
vcc_ErrWhere(tl, tl->t);
return;
}
host = EncString(t_host);
ep = CheckHostPort(host, "80");
if (ep != NULL) {
- sbuf_printf(tl->sb, "Backend '%T': %s\n", t_be, ep);
+ vsb_printf(tl->sb, "Backend '%T': %s\n", t_be, ep);
vcc_ErrWhere(tl, t_host);
return;
}
port = EncString(t_port);
ep = CheckHostPort(host, port);
if (ep != NULL) {
- sbuf_printf(tl->sb, "Backend '%T': %s\n", t_be, ep);
+ vsb_printf(tl->sb, "Backend '%T': %s\n", t_be, ep);
vcc_ErrWhere(tl, t_port);
return;
}
case EOI:
break;
default:
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"Expected 'acl', 'sub' or 'backend', found ");
vcc_ErrToken(tl, tl->t);
- sbuf_printf(tl->sb, " at\n");
+ vsb_printf(tl->sb, " at\n");
vcc_ErrWhere(tl, tl->t);
return;
}
struct proccall *pc;
if (!p->exists) {
- sbuf_printf(tl->sb, "Function %T does not exist\n", p->name);
+ vsb_printf(tl->sb, "Function %T does not exist\n", p->name);
return (1);
}
if (p->active) {
- sbuf_printf(tl->sb, "Function recurses on\n");
+ vsb_printf(tl->sb, "Function recurses on\n");
vcc_ErrWhere(tl, p->name);
return (1);
}
if (u) {
#define VCL_RET_MAC(a, b, c, d) \
if (u & VCL_RET_##b) { \
- sbuf_printf(tl->sb, "Illegal return for method\n"); \
+ vsb_printf(tl->sb, "Illegal return for method\n"); \
vcc_ErrWhere(tl, p->returnt[d]); \
}
#include "vcl_returns.h"
#undef VCL_RET_MAC
- sbuf_printf(tl->sb, "In function\n");
+ vsb_printf(tl->sb, "In function\n");
vcc_ErrWhere(tl, p->name);
return (1);
}
p->active = 1;
TAILQ_FOREACH(pc, &p->calls, list) {
if (Consist_Decend(tl, pc->p, returns)) {
- sbuf_printf(tl->sb, "\nCalled from\n");
+ vsb_printf(tl->sb, "\nCalled from\n");
vcc_ErrWhere(tl, p->name);
- sbuf_printf(tl->sb, "at\n");
+ vsb_printf(tl->sb, "at\n");
vcc_ErrWhere(tl, pc->t);
return (1);
}
if (m->name == NULL)
continue;
if (Consist_Decend(tl, p, m->returns)) {
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"\nwhich is a %s method\n", m->name);
return (1);
}
TAILQ_FOREACH(p, &tl->procs, list) {
if (p->called)
continue;
- sbuf_printf(tl->sb, "Function unused\n");
+ vsb_printf(tl->sb, "Function unused\n");
vcc_ErrWhere(tl, p->name);
return (1);
}
break;
default:
ErrInternal(tl);
- sbuf_printf(tl->sb, "Ref ");
+ vsb_printf(tl->sb, "Ref ");
vcc_ErrToken(tl, r->name);
- sbuf_printf(tl->sb, " has unknown type %d\n",
+ vsb_printf(tl->sb, " has unknown type %d\n",
r->type);
continue;
}
if (r->defcnt == 0 && r->name->tok == METHOD) {
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"No definition for method %T\n", r->name);
continue;
}
if (r->defcnt == 0) {
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"Undefined %s %T, first reference:\n",
type, r->name);
vcc_ErrWhere(tl, r->name);
continue;
}
- sbuf_printf(tl->sb, "Unused %s %T, defined:\n", type, r->name);
+ vsb_printf(tl->sb, "Unused %s %T, defined:\n", type, r->name);
vcc_ErrWhere(tl, r->name);
}
return (nerr);
{
Fc(tl, 0, "\nstatic void\nVGC_Init(void)\n{\n\n");
- sbuf_finish(tl->fi);
- sbuf_cat(tl->fc, sbuf_data(tl->fi));
+ vsb_finish(tl->fi);
+ vsb_cat(tl->fc, vsb_data(tl->fi));
Fc(tl, 0, "}\n");
}
{
Fc(tl, 0, "\nstatic void\nVGC_Fini(void)\n{\n\n");
- sbuf_finish(tl->ff);
- sbuf_cat(tl->fc, sbuf_data(tl->ff));
+ vsb_finish(tl->ff);
+ vsb_cat(tl->fc, vsb_data(tl->ff));
Fc(tl, 0, "}\n");
}
/*--------------------------------------------------------------------*/
char *
-VCC_Compile(struct sbuf *sb, const char *b, const char *e)
+VCC_Compile(struct vsb *sb, const char *b, const char *e)
{
struct tokenlist tokens;
struct ref *r;
TAILQ_INIT(&tokens.procs);
tokens.sb = sb;
- tokens.fc = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ tokens.fc = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(tokens.fc != NULL);
- tokens.fh = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ tokens.fh = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(tokens.fh != NULL);
- tokens.fi = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ tokens.fi = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(tokens.fi != NULL);
- tokens.ff = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+ tokens.ff = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
assert(tokens.ff != NULL);
Fh(&tokens, 0, "extern struct VCL_conf VCL_conf;\n");
vcl_output_lang_h(fo);
fputs(vrt_obj_h, fo);
- sbuf_finish(tokens.fh);
- fputs(sbuf_data(tokens.fh), fo);
- sbuf_delete(tokens.fh);
+ vsb_finish(tokens.fh);
+ fputs(vsb_data(tokens.fh), fo);
+ vsb_delete(tokens.fh);
- sbuf_finish(tokens.fc);
- fputs(sbuf_data(tokens.fc), fo);
- sbuf_delete(tokens.fc);
+ vsb_finish(tokens.fc);
+ fputs(vsb_data(tokens.fc), fo);
+ vsb_delete(tokens.fc);
i = pclose(fo);
fprintf(stderr, "pclose=%d\n", i);
if (i) {
- sbuf_printf(sb, "Internal error: GCC returned 0x%04x\n", i);
+ vsb_printf(sb, "Internal error: GCC returned 0x%04x\n", i);
unlink(of);
free(of);
return (NULL);
/*--------------------------------------------------------------------*/
char *
-VCC_CompileFile(struct sbuf *sb, const char *fn)
+VCC_CompileFile(struct vsb *sb, const char *fn)
{
char *f, *r;
int fd, i;
fd = open(fn, O_RDONLY);
if (fd < 0) {
- sbuf_printf(sb, "Cannot open file '%s': %s",
+ vsb_printf(sb, "Cannot open file '%s': %s",
fn, strerror(errno));
return (NULL);
}
struct token *t;
int indent;
unsigned cnt;
- struct sbuf *fc, *fh, *fi, *ff;
+ struct vsb *fc, *fh, *fi, *ff;
TAILQ_HEAD(, ref) refs;
- struct sbuf *sb;
+ struct vsb *sb;
int err;
int nbackend;
TAILQ_HEAD(, proc) procs;
fputs(" */\n", f);
fputs("\n", f);
fputs("struct sess;\n", f);
- fputs("struct sbuf;\n", f);
+ fputs("struct vsb;\n", f);
fputs("struct backend;\n", f);
fputs("struct VCL_conf;\n", f);
fputs("\n", f);
fputs("void VRT_re_init(void **, const char *);\n", f);
fputs("void VRT_re_fini(void *);\n", f);
fputs("int VRT_re_match(const char *, void *re);\n", f);
- fputs("int VRT_re_test(struct sbuf *, const char *);\n", f);
+ fputs("int VRT_re_test(struct vsb *, const char *);\n", f);
fputs("\n", f);
fputs("void VRT_count(struct sess *, unsigned);\n", f);
fputs("int VRT_rewrite(const char *, const char *);\n", f);
#include <stdio.h>
#include <printf.h>
#include <stdarg.h>
-#include <sbuf.h>
+#include <vsb.h>
#include <stdlib.h>
#include <string.h>
#include <queue.h>
{
if (t->tok == EOI)
- sbuf_printf(tl->sb, "end of input");
+ vsb_printf(tl->sb, "end of input");
else
- sbuf_printf(tl->sb, "'%T'", t);
+ vsb_printf(tl->sb, "'%T'", t);
}
void
vcc__ErrInternal(struct tokenlist *tl, const char *func, unsigned line)
{
- sbuf_printf(tl->sb, "VCL compiler internal error at %s():%u\n",
+ vsb_printf(tl->sb, "VCL compiler internal error at %s():%u\n",
func, line);
tl->err = 1;
}
} else
pos++;
}
- sbuf_printf(tl->sb, "In %s Line %d Pos %d\n", f, lin, pos);
+ vsb_printf(tl->sb, "In %s Line %d Pos %d\n", f, lin, pos);
x = y = 0;
for (p = l; p < e && *p != '\n'; p++) {
if (*p == '\t') {
y &= ~7;
y += 8;
while (x < y) {
- sbuf_bcat(tl->sb, " ", 1);
+ vsb_bcat(tl->sb, " ", 1);
x++;
}
} else {
x++;
y++;
- sbuf_bcat(tl->sb, p, 1);
+ vsb_bcat(tl->sb, p, 1);
}
}
- sbuf_cat(tl->sb, "\n");
+ vsb_cat(tl->sb, "\n");
x = y = 0;
for (p = l; p < e && *p != '\n'; p++) {
if (p >= t->b && p < t->e) {
- sbuf_bcat(tl->sb, "#", 1);
+ vsb_bcat(tl->sb, "#", 1);
x++;
y++;
continue;
} else
y++;
while (x < y) {
- sbuf_bcat(tl->sb, "-", 1);
+ vsb_bcat(tl->sb, "-", 1);
x++;
}
}
- sbuf_cat(tl->sb, "\n");
+ vsb_cat(tl->sb, "\n");
tl->err = 1;
}
{
tl->t = TAILQ_NEXT(tl->t, list);
if (tl->t == NULL) {
- sbuf_printf(tl->sb,
+ vsb_printf(tl->sb,
"Ran out of input, something is missing or"
" maybe unbalanced (...) or {...}\n");
tl->err = 1;
{
if (tl->t->tok == tok)
return;
- sbuf_printf(tl->sb, "Expected %s got ", vcl_tnames[tok]);
+ vsb_printf(tl->sb, "Expected %s got ", vcl_tnames[tok]);
vcc_ErrToken(tl, tl->t);
- sbuf_printf(tl->sb, "\n(program line %u), at\n", line);
+ vsb_printf(tl->sb, "\n(program line %u), at\n", line);
vcc_ErrWhere(tl, tl->t);
}
continue;
}
vcc_AddToken(tl, EOI, p, p + 1);
- sbuf_printf(tl->sb, "Syntax error at\n");
+ vsb_printf(tl->sb, "Syntax error at\n");
vcc_ErrWhere(tl, tl->t);
return;
}