[master] 13b4890 Rename vcli_priv.h to vcli_serve.h for consistency, and eliminate a couple of unnecessary includes of it.

Poul-Henning Kamp phk at FreeBSD.org
Sun May 22 01:19:05 CEST 2016


commit 13b4890b3375150453fb8f1610465bf41c31e4fe
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sat May 21 23:18:18 2016 +0000

    Rename vcli_priv.h to vcli_serve.h for consistency, and eliminate
    a couple of unnecessary includes of it.

diff --git a/bin/varnishd/cache/cache_acceptor.c b/bin/varnishd/cache/cache_acceptor.c
index b45c4f4..58481d5 100644
--- a/bin/varnishd/cache/cache_acceptor.c
+++ b/bin/varnishd/cache/cache_acceptor.c
@@ -45,7 +45,7 @@
 #include "cache_pool.h"
 #include "common/heritage.h"
 
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 #include "vsa.h"
 #include "vtcp.h"
 #include "vtim.h"
diff --git a/bin/varnishd/cache/cache_backend_cfg.c b/bin/varnishd/cache/cache_backend_cfg.c
index ad94900..6c7d8e4 100644
--- a/bin/varnishd/cache/cache_backend_cfg.c
+++ b/bin/varnishd/cache/cache_backend_cfg.c
@@ -39,8 +39,7 @@
 #include "cache.h"
 
 #include "vcl.h"
-#include "vcli.h"
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 #include "vrt.h"
 #include "vtim.h"
 
diff --git a/bin/varnishd/cache/cache_backend_probe.c b/bin/varnishd/cache/cache_backend_probe.c
index ea63abe..0adf25d 100644
--- a/bin/varnishd/cache/cache_backend_probe.c
+++ b/bin/varnishd/cache/cache_backend_probe.c
@@ -44,7 +44,7 @@
 #include "cache.h"
 
 #include "binary_heap.h"
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 #include "vrt.h"
 #include "vsa.h"
 #include "vtcp.h"
diff --git a/bin/varnishd/cache/cache_ban.c b/bin/varnishd/cache/cache_ban.c
index 302d1e9..e4b25e1 100644
--- a/bin/varnishd/cache/cache_ban.c
+++ b/bin/varnishd/cache/cache_ban.c
@@ -36,8 +36,7 @@
 #include "cache_ban.h"
 
 #include "hash/hash_slinger.h"
-#include "vcli.h"
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 #include "vend.h"
 #include "vmb.h"
 
diff --git a/bin/varnishd/cache/cache_cli.c b/bin/varnishd/cache/cache_cli.c
index db72904..fa8aba8 100644
--- a/bin/varnishd/cache/cache_cli.c
+++ b/bin/varnishd/cache/cache_cli.c
@@ -39,9 +39,6 @@
 #include "cache.h"
 #include "common/heritage.h"
 
-#include "vcli.h"
-#include "vcli_common.h"
-#include "vcli_priv.h"
 #include "vcli_serve.h"
 
 pthread_t		cli_thread;
diff --git a/bin/varnishd/cache/cache_fetch_proc.c b/bin/varnishd/cache/cache_fetch_proc.c
index 49862db..cb094fd 100644
--- a/bin/varnishd/cache/cache_fetch_proc.c
+++ b/bin/varnishd/cache/cache_fetch_proc.c
@@ -33,8 +33,7 @@
 
 #include "cache.h"
 #include "cache_filter.h"
-
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 
 static unsigned fetchfrag;
 
diff --git a/bin/varnishd/cache/cache_main.c b/bin/varnishd/cache/cache_main.c
index 413affb..19b519d 100644
--- a/bin/varnishd/cache/cache_main.c
+++ b/bin/varnishd/cache/cache_main.c
@@ -35,7 +35,7 @@
 #include "cache.h"
 #include "common/heritage.h"
 
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 #include "vrnd.h"
 
 #include "hash/hash_slinger.h"
diff --git a/bin/varnishd/cache/cache_panic.c b/bin/varnishd/cache/cache_panic.c
index 51aeb46..7fb6bd8 100644
--- a/bin/varnishd/cache/cache_panic.c
+++ b/bin/varnishd/cache/cache_panic.c
@@ -48,7 +48,7 @@
 #include "vrt.h"
 #include "cache_director.h"
 #include "storage/storage.h"
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 
 /*
  * The panic string is constructed in memory, then copied to the
diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index a8702e7..f36f2b4 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -44,8 +44,7 @@
 
 #include "cache_director.h"
 #include "cache_backend.h"
-#include "vcli.h"
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 
 static const char * const VCL_TEMP_INIT = "init";
 static const char * const VCL_TEMP_COLD = "cold";
diff --git a/bin/varnishd/cache/cache_vrt_vmod.c b/bin/varnishd/cache/cache_vrt_vmod.c
index 3904f28..0b7e15f 100644
--- a/bin/varnishd/cache/cache_vrt_vmod.c
+++ b/bin/varnishd/cache/cache_vrt_vmod.c
@@ -37,7 +37,7 @@
 
 #include "cache.h"
 
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 #include "vrt.h"
 
 /*--------------------------------------------------------------------
diff --git a/bin/varnishd/hash/hash_critbit.c b/bin/varnishd/hash/hash_critbit.c
index 9ac2518..3b09fc5 100644
--- a/bin/varnishd/hash/hash_critbit.c
+++ b/bin/varnishd/hash/hash_critbit.c
@@ -37,7 +37,6 @@
 #include "cache/cache.h"
 
 #include "hash/hash_slinger.h"
-#include "vcli_priv.h"
 #include "vmb.h"
 #include "vtim.h"
 
diff --git a/bin/varnishd/http1/cache_http1_fetch.c b/bin/varnishd/http1/cache_http1_fetch.c
index 1cf127c..153f1d3 100644
--- a/bin/varnishd/http1/cache_http1_fetch.c
+++ b/bin/varnishd/http1/cache_http1_fetch.c
@@ -35,7 +35,6 @@
 
 #include "cache/cache.h"
 
-#include "vcli_priv.h"
 #include "vrt.h"
 #include "vtcp.h"
 #include "vtim.h"
diff --git a/bin/varnishd/mgt/mgt_acceptor.c b/bin/varnishd/mgt/mgt_acceptor.c
index aa3ce5a..70a333a 100644
--- a/bin/varnishd/mgt/mgt_acceptor.c
+++ b/bin/varnishd/mgt/mgt_acceptor.c
@@ -44,7 +44,7 @@
 #include "common/heritage.h"
 
 #include "vav.h"
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 #include "vsa.h"
 #include "vss.h"
 #include "vtcp.h"
diff --git a/bin/varnishd/mgt/mgt_child.c b/bin/varnishd/mgt/mgt_child.c
index d451e97..836a049 100644
--- a/bin/varnishd/mgt/mgt_child.c
+++ b/bin/varnishd/mgt/mgt_child.c
@@ -47,8 +47,6 @@
 #include "common/heritage.h"
 
 #include "vbm.h"
-#include "vcli.h"
-#include "vcli_priv.h"
 #include "vcli_serve.h"
 #include "vev.h"
 #include "vlu.h"
diff --git a/bin/varnishd/mgt/mgt_cli.c b/bin/varnishd/mgt/mgt_cli.c
index e6ed6a5..2fbcdb3 100644
--- a/bin/varnishd/mgt/mgt_cli.c
+++ b/bin/varnishd/mgt/mgt_cli.c
@@ -45,9 +45,6 @@
 
 #include "mgt/mgt.h"
 
-#include "vcli.h"
-#include "vcli_common.h"
-#include "vcli_priv.h"
 #include "vcli_serve.h"
 #include "vev.h"
 #include "vrnd.h"
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index f5b41c9..56394be 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -49,8 +49,7 @@
 
 #include "hash/hash_slinger.h"
 #include "vav.h"
-#include "vcli.h"
-#include "vcli_common.h"
+#include "vcli_serve.h"
 #include "vev.h"
 #include "vfil.h"
 #include "vin.h"
diff --git a/bin/varnishd/mgt/mgt_param.c b/bin/varnishd/mgt/mgt_param.c
index 4211920..ef63fcd 100644
--- a/bin/varnishd/mgt/mgt_param.c
+++ b/bin/varnishd/mgt/mgt_param.c
@@ -40,9 +40,6 @@
 
 #include "mgt/mgt_param.h"
 #include "vav.h"
-#include "vcli.h"
-#include "vcli_common.h"
-#include "vcli_priv.h"
 #include "vcli_serve.h"
 
 struct plist {
diff --git a/bin/varnishd/mgt/mgt_vcc.c b/bin/varnishd/mgt/mgt_vcc.c
index d35ccd4..62b12e8 100644
--- a/bin/varnishd/mgt/mgt_vcc.c
+++ b/bin/varnishd/mgt/mgt_vcc.c
@@ -44,8 +44,7 @@
 #include "storage/storage.h"
 
 #include "libvcc.h"
-#include "vcli.h"
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 #include "vfil.h"
 #include "vsub.h"
 
diff --git a/bin/varnishd/mgt/mgt_vcl.c b/bin/varnishd/mgt/mgt_vcl.c
index 67d8c40..8817440 100644
--- a/bin/varnishd/mgt/mgt_vcl.c
+++ b/bin/varnishd/mgt/mgt_vcl.c
@@ -40,8 +40,6 @@
 
 #include "mgt/mgt.h"
 
-#include "vcli.h"
-#include "vcli_priv.h"
 #include "vcli_serve.h"
 #include "vev.h"
 #include "vtim.h"
diff --git a/bin/varnishd/storage/mgt_stevedore.c b/bin/varnishd/storage/mgt_stevedore.c
index a540136..dff69fd 100644
--- a/bin/varnishd/storage/mgt_stevedore.c
+++ b/bin/varnishd/storage/mgt_stevedore.c
@@ -39,7 +39,6 @@
 #include <unistd.h>
 
 #include "mgt/mgt.h"
-#include "vcli_priv.h"
 #include "vcli_serve.h"
 
 #include "storage/storage.h"
diff --git a/bin/varnishd/storage/storage_persistent.c b/bin/varnishd/storage/storage_persistent.c
index d439517..7991d9b 100644
--- a/bin/varnishd/storage/storage_persistent.c
+++ b/bin/varnishd/storage/storage_persistent.c
@@ -48,8 +48,7 @@
 #include "storage/storage_simple.h"
 
 #include "hash/hash_slinger.h"
-#include "vcli.h"
-#include "vcli_priv.h"
+#include "vcli_serve.h"
 #include "vsha256.h"
 #include "vtim.h"
 
diff --git a/doc/sphinx/reference/varnish-cli.rst b/doc/sphinx/reference/varnish-cli.rst
index 314e9a0..67c8dc8 100644
--- a/doc/sphinx/reference/varnish-cli.rst
+++ b/doc/sphinx/reference/varnish-cli.rst
@@ -149,8 +149,8 @@ One particular magic number to know, is that the line with the status
 code and length field always is exactly 13 characters long, including
 the NL character.
 
-For your reference the sourcefile lib/libvarnish/cli_common.h contains
-the functions Varnish code uses to read and write CLI response.
+The varnishapi library contains functions to implement the basics of
+the CLI protocol, see the `vcli.h` include file.
 
 .. _ref_psk_auth:
 
diff --git a/include/Makefile.am b/include/Makefile.am
index caae1c7..ca51410 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -69,8 +69,6 @@ nobase_noinst_HEADERS = \
 	compat/execinfo.h \
 	vfl.h \
 	libvcc.h \
-	vcli_common.h \
-	vcli_priv.h \
 	vcli_serve.h \
 	vcs_version.h \
 	vct.h \
diff --git a/include/vcli_priv.h b/include/vcli_priv.h
deleted file mode 100644
index 09bd042..0000000
--- a/include/vcli_priv.h
+++ /dev/null
@@ -1,73 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2011 Varnish Software AS
- * All rights reserved.
- *
- * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
- *
- * 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.
- * 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.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 AUTHOR OR CONTRIBUTORS 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.
- *
- * Varnish process internal CLI stuff.
- *
- * XXX: at a latter date we may want to move some to cli.h/libvarnishapi
- */
-
-#define CLI_PRIV_H
-
-struct cli;	/* NB: struct cli is opaque at this level.  */
-
-typedef void cli_func_t(struct cli*, const char * const *av, void *priv);
-
-struct cli_cmd_desc {
-	/* Must match CLI_CMD macro in include/tbl/cli_cmds.h */
-	const char			*request;
-	const char			*syntax;
-	const char			*help;
-	const char			*doc;
-	int				minarg;
-	int				maxarg;
-};
-
-#define CLI_CMD(U,l,s,h,d,m,M) extern const struct cli_cmd_desc CLICMD_##U[1];
-#include "tbl/cli_cmds.h"
-#undef CLI_CMD
-
-struct cli_proto {
-	const struct cli_cmd_desc	*desc;
-	const char			*flags;
-
-	/* Dispatch information */
-	cli_func_t			*func;
-	cli_func_t			*jsonfunc;
-	void				*priv;
-
-	unsigned			auth;
-	VTAILQ_ENTRY(cli_proto)		list;
-};
-
-/* The implementation must provide these functions */
-int VCLI_Overflow(struct cli *cli);
-void VCLI_Out(struct cli *cli, const char *fmt, ...) __v_printflike(2, 3);
-void VCLI_Quote(struct cli *cli, const char *str);
-void VCLI_JSON_str(struct cli *cli, const char *str);
-void VCLI_JSON_ver(struct cli *cli, unsigned ver, const char * const * av);
-void VCLI_SetResult(struct cli *cli, unsigned r);
diff --git a/include/vcli_serve.h b/include/vcli_serve.h
new file mode 100644
index 0000000..cfe4265
--- /dev/null
+++ b/include/vcli_serve.h
@@ -0,0 +1,109 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2011 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * 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.
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 AUTHOR OR CONTRIBUTORS 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.
+ *
+ * Varnish process internal CLI stuff.
+ *
+ * XXX: at a latter date we may want to move some to cli.h/libvarnishapi
+ */
+
+#include "vcli.h"
+
+struct cli;	/* NB: struct cli is opaque at this level.  */
+struct vlu;
+struct VCLS;
+
+typedef void cli_func_t(struct cli*, const char * const *av, void *priv);
+
+struct cli_cmd_desc {
+	/* Must match CLI_CMD macro in include/tbl/cli_cmds.h */
+	const char			*request;
+	const char			*syntax;
+	const char			*help;
+	const char			*doc;
+	int				minarg;
+	int				maxarg;
+};
+
+#define CLI_CMD(U,l,s,h,d,m,M) extern const struct cli_cmd_desc CLICMD_##U[1];
+#include "tbl/cli_cmds.h"
+#undef CLI_CMD
+
+/* A CLI command */
+struct cli_proto {
+	const struct cli_cmd_desc	*desc;
+	const char			*flags;
+
+	/* Dispatch information */
+	cli_func_t			*func;
+	cli_func_t			*jsonfunc;
+	void				*priv;
+
+	unsigned			auth;
+	VTAILQ_ENTRY(cli_proto)		list;
+};
+
+/* a CLI session */
+struct cli {
+	unsigned                magic;
+#define CLI_MAGIC              0x4038d570
+	struct vsb              *sb;
+	enum VCLI_status_e      result;
+	char                    *cmd;
+	unsigned                auth;
+	char                    challenge[34];
+	char                    *ident;
+	struct vlu              *vlu;
+	struct VCLS             *cls;
+	volatile unsigned       *limit;
+};
+
+/* The implementation must provide these functions */
+int VCLI_Overflow(struct cli *cli);
+void VCLI_Out(struct cli *cli, const char *fmt, ...) __v_printflike(2, 3);
+void VCLI_Quote(struct cli *cli, const char *str);
+void VCLI_JSON_str(struct cli *cli, const char *str);
+void VCLI_JSON_ver(struct cli *cli, unsigned ver, const char * const * av);
+void VCLI_SetResult(struct cli *cli, unsigned r);
+
+typedef void cls_cb_f(void *priv);
+typedef void cls_cbc_f(const struct cli*);
+struct VCLS *VCLS_New(cls_cbc_f *before, cls_cbc_f *after,
+    volatile unsigned *maxlen, volatile unsigned *limit);
+struct cli *VCLS_AddFd(struct VCLS *cs, int fdi, int fdo, cls_cb_f *closefunc,
+    void *priv);
+void VCLS_AddFunc(struct VCLS *cs, unsigned auth, struct cli_proto *clp);
+int VCLS_Poll(struct VCLS *cs, int timeout);
+int VCLS_PollFd(struct VCLS *cs, int fd, int timeout);
+void VCLS_Destroy(struct VCLS **);
+void VCLS_Clone(struct VCLS *cs, struct VCLS *cso);
+
+/* From libvarnish/cli.c */
+cli_func_t	VCLS_func_close;
+cli_func_t	VCLS_func_help;
+cli_func_t	VCLS_func_help_json;
+cli_func_t	VCLS_func_ping;
diff --git a/lib/libvarnish/Makefile.am b/lib/libvarnish/Makefile.am
index 6d6665a..5dd728e 100644
--- a/lib/libvarnish/Makefile.am
+++ b/lib/libvarnish/Makefile.am
@@ -14,9 +14,8 @@ libvarnish_la_SOURCES = \
 	vas.c \
 	binary_heap.c \
 	vsub.c \
-	cli_auth.c \
-	cli_common.c \
-	cli_serve.c \
+	vcli_proto.c \
+	vcli_serve.c \
 	vfl.c \
 	vnum.c \
 	vtim.c \
diff --git a/lib/libvarnish/vcli_proto.c b/lib/libvarnish/vcli_proto.c
index d7b7bf2..27e2104 100644
--- a/lib/libvarnish/vcli_proto.c
+++ b/lib/libvarnish/vcli_proto.c
@@ -29,9 +29,14 @@
 #include "config.h"
 
 #include <sys/types.h>
+#include <sys/uio.h>
 
+#include <errno.h>
+#include <poll.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
 #include "vas.h"
@@ -62,3 +67,130 @@ VCLI_AuthResponse(int S_fd, const char *challenge,
 	for(i = 0; i < SHA256_LEN; i++)
 		sprintf(response + 2 * i, "%02x", buf[i]);
 }
+
+int
+VCLI_WriteResult(int fd, unsigned status, const char *result)
+{
+	int i, l;
+	struct iovec iov[3];
+	char nl[2] = "\n";
+	size_t len;
+	char res[CLI_LINE0_LEN + 2];	/*
+					 * NUL + one more so we can catch
+					 * any misformats by snprintf
+					 */
+
+	assert(status >= 100);
+	assert(status <= 999);		/*lint !e650 const out of range */
+
+	len = strlen(result);
+
+	i = snprintf(res, sizeof res,
+	    "%-3d %-8zd\n", status, len);
+	assert(i == CLI_LINE0_LEN);
+	assert(strtoul(res + 3, NULL, 10) == len);
+
+	iov[0].iov_base = res;
+	iov[0].iov_len = CLI_LINE0_LEN;
+
+	iov[1].iov_base = (void*)(uintptr_t)result;	/* TRUST ME */
+	iov[1].iov_len = len;
+
+	iov[2].iov_base = nl;
+	iov[2].iov_len = 1;
+
+	for (l = i = 0; i < 3; i++)
+		l += iov[i].iov_len;
+	i = writev(fd, iov, 3);
+	return (i != l);
+}
+
+static int
+read_tmo(int fd, char *ptr, unsigned len, double tmo)
+{
+	int i, j, to;
+	struct pollfd pfd;
+
+	if (tmo > 0)
+		to = (int)(tmo * 1e3);
+	else
+		to = -1;
+	pfd.fd = fd;
+	pfd.events = POLLIN;
+	for (j = 0; len > 0; ) {
+		i = poll(&pfd, 1, to);
+		if (i == 0) {
+			errno = ETIMEDOUT;
+			return (-1);
+		}
+		i = read(fd, ptr, len);
+		if (i < 0)
+			return (i);
+		if (i == 0)
+			break;
+		len -= i;
+		ptr += i;
+		j += i;
+	}
+	return (j);
+}
+
+int
+VCLI_ReadResult(int fd, unsigned *status, char **ptr, double tmo)
+{
+	char res[CLI_LINE0_LEN];	/* For NUL */
+	int i, j;
+	unsigned u, v, s;
+	char *p = NULL;
+	const char *err = "CLI communication error (hdr)";
+
+	if (status == NULL)
+		status = &s;
+	if (ptr != NULL)
+		*ptr = NULL;
+	do {
+		i = read_tmo(fd, res, CLI_LINE0_LEN, tmo);
+		if (i != CLI_LINE0_LEN)
+			break;
+
+		if (res[3] != ' ')
+			break;
+
+		if (res[CLI_LINE0_LEN - 1] != '\n')
+			break;
+
+		res[CLI_LINE0_LEN - 1] = '\0';
+		j = sscanf(res, "%u %u\n", &u, &v);
+		if (j != 2)
+			break;
+
+		err = "CLI communication error (body)";
+
+		*status = u;
+		p = malloc(v + 1L);
+		if (p == NULL)
+			break;
+
+		i = read_tmo(fd, p, v + 1, tmo);
+		if (i < 0)
+			break;
+		if (i != v + 1)
+			break;
+		if (p[v] != '\n')
+			break;
+
+		p[v] = '\0';
+		if (ptr == NULL)
+			free(p);
+		else
+			*ptr = p;
+		return (0);
+	} while(0);
+
+	if (p != NULL)
+		free(p);
+	*status = CLIS_COMMS;
+	if (ptr != NULL)
+		*ptr = strdup(err);
+	return (*status);
+}
diff --git a/lib/libvarnish/vcli_serve.c b/lib/libvarnish/vcli_serve.c
index 4e88bdc..80d09de 100644
--- a/lib/libvarnish/vcli_serve.c
+++ b/lib/libvarnish/vcli_serve.c
@@ -34,6 +34,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <poll.h>
+#include <stdarg.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -47,9 +48,6 @@
 #include "miniobj.h"
 
 #include "vav.h"
-#include "vcli.h"
-#include "vcli_common.h"
-#include "vcli_priv.h"
 #include "vcli_serve.h"
 #include "vlu.h"
 #include "vsb.h"
@@ -614,3 +612,84 @@ VCLS_Destroy(struct VCLS **csp)
 	}
 	FREE_OBJ(cs);
 }
+
+/**********************************************************************
+ * Utility functions for implementing CLI commands
+ */
+
+/*lint -e{818} cli could be const */
+void
+VCLI_Out(struct cli *cli, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	if (cli != NULL) {
+		CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
+		if (VSB_len(cli->sb) < *cli->limit)
+			(void)VSB_vprintf(cli->sb, fmt, ap);
+		else if (cli->result == CLIS_OK)
+			cli->result = CLIS_TRUNCATED;
+	} else {
+		(void)vfprintf(stdout, fmt, ap);
+	}
+	va_end(ap);
+}
+
+/*lint -e{818} cli could be const */
+int
+VCLI_Overflow(struct cli *cli)
+{
+	CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
+	if (cli->result == CLIS_TRUNCATED ||
+	    VSB_len(cli->sb) >= *cli->limit)
+		return (1);
+	return (0);
+}
+
+/*lint -e{818} cli could be const */
+void
+VCLI_JSON_str(struct cli *cli, const char *s)
+{
+
+	CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
+	VSB_quote(cli->sb, s, -1, VSB_QUOTE_JSON);
+}
+
+/*lint -e{818} cli could be const */
+void
+VCLI_JSON_ver(struct cli *cli, unsigned ver, const char * const * av)
+{
+	int i;
+
+	CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
+	VCLI_Out(cli, "[ %u, [", ver);
+	for (i = 1; av[i] != NULL; i++) {
+		VCLI_JSON_str(cli, av[i]);
+		if (av[i + 1] != NULL)
+			VCLI_Out(cli, ", ");
+	}
+	VCLI_Out(cli, "]");
+}
+
+/*lint -e{818} cli could be const */
+void
+VCLI_Quote(struct cli *cli, const char *s)
+{
+
+	CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
+	VSB_quote(cli->sb, s, -1, 0);
+}
+
+void
+VCLI_SetResult(struct cli *cli, unsigned res)
+{
+
+	if (cli != NULL) {
+		CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
+		if (cli->result != CLIS_TRUNCATED || res != CLIS_OK)
+			cli->result = res;	/*lint !e64 type mismatch */
+	} else {
+		printf("CLI result = %u\n", res);
+	}
+}
diff --git a/lib/libvarnishapi/Makefile.am b/lib/libvarnishapi/Makefile.am
index 03fefe4..455cf3a 100644
--- a/lib/libvarnishapi/Makefile.am
+++ b/lib/libvarnishapi/Makefile.am
@@ -20,8 +20,7 @@ libvarnishapi_la_SOURCES = \
 	../libvarnish/vav.c \
 	../../include/vcs_version.h \
 	../libvarnish/version.c \
-	../libvarnish/cli_common.c \
-	../libvarnish/cli_auth.c \
+	../libvarnish/vcli_proto.c \
 	../libvarnish/vfl.c \
 	../libvarnish/vin.c \
 	../libvarnish/vmb.c \



More information about the varnish-commit mailing list