]> git.ipfire.org Git - thirdparty/haproxy.git/commit
MEDIUM: cli: register CLI keywords with cli_register_kw()
authorWilliam Lallemand <wlallemand@haproxy.com>
Thu, 13 Oct 2016 15:57:55 +0000 (17:57 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 19 Oct 2016 17:03:40 +0000 (19:03 +0200)
commit1e08cd819aa9b622fae60923252ee357fe26db07
tree21eca1bc014045071ed89103a8e1b353ce3ff914
parent523cc9e8587aef8449dead14b7256ebeac9f3e9c
MEDIUM: cli: register CLI keywords with cli_register_kw()

To register a new cli keyword, you need to declare a cli_kw_list
structure in your source file:

static struct cli_kw_list cli_kws = {{ },{
{ { "test", "list", NULL }, "test list : do some tests on the cli", test_parsing, NULL },
{ { NULL }, NULL, NULL, NULL, NULL }
}};

And then register it:

cli_register_kw(&cli_kws);

The first field is an array of 5 elements, where you declare the
keywords combination which will match, it must be ended by a NULL
element.

The second field is used as a usage message, it will appear in the help
of the cli, you can set it to NULL if you don't want to show it, it's a
good idea if you want to overwrite some existing keywords.

The two last fields are callbacks.

The first one is used at parsing time, you can use it to parse the
arguments of your keywords and print small messages. The function must
return 1 in case of a failure, otherwise 0:

#include <proto/dumpstats.h>

static int test_parsing(char **args, struct appctx *appctx)
{
struct chunk out;

if (!*args[2]) {
appctx->ctx.cli.msg = "Error: the 3rd argument is mandatory !";
appctx->st0 = STAT_CLI_PRINT;
return 1;
}
chunk_reset(&trash);
chunk_printf(&trash, "arg[3]: %s\n", args[2]);
chunk_init(&out, NULL, 0);
chunk_dup(&out, &trash);
appctx->ctx.cli.err = out.str;
appctx->st0 = STAT_CLI_PRINT_FREE; /* print and free in the default cli_io_handler */
return 0;
}

The last field is the IO handler callback, it can be set to NULL if you
want to use the default cli_io_handler() otherwise you can write your
own. You can use the private pointer in the appctx if you need to store
a context or some data. stats_dump_sess_to_buffer() is a good example of
IO handler, IO handlers often use the appctx->st2 variable for the state
machine. The handler must return 0 in case it have to be recall later
otherwise 1.
include/proto/dumpstats.h
include/types/applet.h
src/dumpstats.c