From: phk Date: Thu, 16 Mar 2006 09:02:24 +0000 (+0000) Subject: Generic and public stuff for CLI protocol handling. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=daa9308c94838bf1db683c0183506114763630a0;p=varnish Generic and public stuff for CLI protocol handling. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@54 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/include/cli.h b/varnish-cache/include/cli.h index 0886a6de..c79350aa 100644 --- a/varnish-cache/include/cli.h +++ b/varnish-cache/include/cli.h @@ -1,5 +1,24 @@ /* * $Id$ + * + * Public definition of the CLI protocol, part of the published Varnish-API. + * + */ + +/* + * These macros define the common data for requests in the CLI protocol. + * The fields are: + * const char * request_name + * const char * request_syntax (for short help) + * const char * request_help (for long help) + * unsigned minimum_arguments + * unsigned maximum_arguments + * + * If you only want a subset of these fields do this: + * #define CLIF145(a,b,c,d,e) a,d,e + * [...] + * CLIF145(CLI_URL_QUERY) + * */ #define CLI_URL_QUERY \ @@ -142,6 +161,10 @@ "\tClose connection", \ 0, 0 +/* + * Status/return codes in the CLI protocol + */ + enum cli_status_e { CLIS_SYNTAX = 100, CLIS_UNKNOWN = 101, diff --git a/varnish-cache/include/cli_priv.h b/varnish-cache/include/cli_priv.h new file mode 100644 index 00000000..716804a0 --- /dev/null +++ b/varnish-cache/include/cli_priv.h @@ -0,0 +1,34 @@ +/* + * $Id$ + * + * Varnish process internal CLI stuff. + * + * XXX: at a latter date we may want to move some to cli.h/libvarnishapi + * + */ + +struct cli; /* NB: struct cli is opaque at this level. */ + +typedef void cli_func_t(struct cli*, char **av, void *priv); + +struct cli_proto { + /* These must match the CLI_* macros in cli.h */ + const char *request; + const char *syntax; + const char *help; + unsigned minarg; + unsigned maxarg; + + /* Dispatch information */ + cli_func_t *func; + void *priv; +}; + +/* The implementation must provide these functions */ +void cli_out(struct cli *cli, const char *fmt, ...); +void cli_param(struct cli *cli); +void cli_result(struct cli *cli, unsigned r); + +/* From libvarnish/cli.c */ +void cli_dispatch(struct cli *cli, struct cli_proto *clp, const char *line); +cli_func_t cli_func_help; diff --git a/varnish-cache/lib/libvarnish/Makefile.am b/varnish-cache/lib/libvarnish/Makefile.am index 6b1ed7c6..476f683e 100644 --- a/varnish-cache/lib/libvarnish/Makefile.am +++ b/varnish-cache/lib/libvarnish/Makefile.am @@ -5,4 +5,5 @@ INCLUDES = -I$(top_srcdir)/include lib_LTLIBRARIES = libvarnish.la libvarnish_la_SOURCES = \ - argv.c + argv.c \ + cli.c diff --git a/varnish-cache/lib/libvarnish/cli.c b/varnish-cache/lib/libvarnish/cli.c new file mode 100644 index 00000000..3a5ed102 --- /dev/null +++ b/varnish-cache/lib/libvarnish/cli.c @@ -0,0 +1,103 @@ +/* + * $Id$ + * + * Stuff for handling the CLI protocol + */ + +#include +#include + +#include +#include +#include + +/* + * Generic help function. + * + * priv must point to cli_proto array + */ + +void +cli_func_help(struct cli *cli, char **av, void *priv) +{ + unsigned u; + struct cli_proto *cp; + + if (av[2] == NULL) { + cli_out(cli, "Available commands:\n"); + for (cp = priv; cp->request != NULL; cp++) + cli_out(cli, "%s\n", cp->syntax); + return; + } + for (cp = priv; cp->request != NULL; cp++) { + if (!strcmp(cp->request, av[2])) { + cli_out(cli, "%s\n%s\n", cp->syntax, cp->help); + return; + } + } + cli_param(cli); +} + +void +cli_dispatch(struct cli *cli, struct cli_proto *clp, const char *line) +{ + char **av; + unsigned u; + struct cli_proto *cp; + + cli_result(cli, CLIS_OK); + /* XXX: syslog commands */ + av = ParseArgv(line, 0); + do { + if (av[0] != NULL) { + cli_out(cli, "Syntax Error: %s\n", av[0]); + cli_result(cli, CLIS_SYNTAX); + break; + } + if (av[1] == NULL) + break; + if (isupper(av[1][0])) { + cli_out(cli, + "all commands are in lower-case.\n"); + cli_result(cli, CLIS_UNKNOWN); + break; + } + for (cp = clp; cp->request != NULL; cp++) + if (!strcmp(av[1], cp->request)) + break; + if (cp->request == NULL) { + cli_out(cli, + "Unknown request, type 'help' for more info.\n"); + cli_result(cli, CLIS_UNKNOWN); + break; + } + + if (cp->func == NULL) { + cli_out(cli, "Unimplemented\n"); + cli_result(cli, CLIS_UNIMPL); + break; + } + + for (u = 0; u <= cp->minarg; u++) { + if (av[u + 1] != NULL) + continue; + cli_out(cli, "Too few parameters\n"); + cli_result(cli, CLIS_TOOFEW); + break; + } + if (u <= cp->minarg) + break; + for (; u <= cp->maxarg; u++) + if (av[u + 1] == NULL) + break; + if (av[u + 1] != NULL) { + cli_out(cli, "Too many parameters\n"); + cli_result(cli, CLIS_TOOMANY); + break; + } + + cp->func(cli, av, cp->priv); + + } while (0); + FreeArgv(av); +} diff --git a/varnish-cache/lib/libvcl/Makefile b/varnish-cache/lib/libvcl/Makefile index 9b97e0b4..4c664f76 100644 --- a/varnish-cache/lib/libvcl/Makefile +++ b/varnish-cache/lib/libvcl/Makefile @@ -18,3 +18,6 @@ test: ${PROG} flint: flint flint.lnt -I/usr/include -I. ${SRCS} + +distclean: clean +