From: Willy Tarreau Date: Thu, 24 Nov 2016 15:23:38 +0000 (+0100) Subject: CLEANUP: cli: simplify the request parser a little bit X-Git-Tag: v1.7.0~29 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=419085656b35f4f2211b13948364b3359ac79c01;p=thirdparty%2Fhaproxy.git CLEANUP: cli: simplify the request parser a little bit stats_sock_parse_request() was renamed cli_parse_request(). It now takes an appctx instead of a stream interface, and presets ->st2 to 0 so that most handlers will not have to set it anymore. The io_handler is set by default to the keyword's IO handler so that the parser can simply change it without having to rewrite the new state. --- diff --git a/src/cli.c b/src/cli.c index 119e087cf5..5ae8959b31 100644 --- a/src/cli.c +++ b/src/cli.c @@ -391,15 +391,16 @@ int cli_has_level(struct appctx *appctx, int level) } -/* Processes the stats interpreter on the statistics socket. This function is - * called from an applet running in a stream interface. The function returns 1 - * if the request was understood, otherwise zero. It sets appctx->st0 to a value - * designating the function which will have to process the request, which can - * also be the print function to display the return message set into cli.msg. +/* Processes the CLI interpreter on the stats socket. This function is called + * from the CLI's IO handler running in an appctx context. The function returns 1 + * if the request was understood, otherwise zero. It is called with appctx->st0 + * set to CLI_ST_GETREQ and presets ->st2 to 0 so that parsers don't have to do + * it. It will possilbly leave st0 to CLI_ST_CALLBACK if the keyword needs to + * have its own I/O handler called again. Most of the time, parsers will only + * set st0 to CLI_ST_PRINT and put their message to be displayed into cli.msg. */ -static int stats_sock_parse_request(struct stream_interface *si, char *line) +static int cli_parse_request(struct appctx *appctx, char *line) { - struct appctx *appctx = __objt_appctx(si->end); char *args[MAX_STATS_ARGS + 1]; struct cli_kw *kw; int arg; @@ -450,17 +451,18 @@ static int stats_sock_parse_request(struct stream_interface *si, char *line) appctx->ctx.stats.scope_str = 0; appctx->ctx.stats.scope_len = 0; appctx->ctx.stats.flags = 0; - if ((kw = cli_find_kw(args))) { - if (kw->parse) { - if (kw->parse(args, appctx, kw->private) == 0 && kw->io_handler) { - appctx->st0 = CLI_ST_CALLBACK; - appctx->io_handler = kw->io_handler; - appctx->io_release = kw->io_release; - } - } - return 1; + appctx->st2 = 0; + + kw = cli_find_kw(args); + if (!kw || !kw->parse) + return 0; + + appctx->io_handler = kw->io_handler; + if (kw->parse(args, appctx, kw->private) == 0 && appctx->io_handler) { + appctx->st0 = CLI_ST_CALLBACK; + appctx->io_release = kw->io_release; } - return 0; + return 1; } /* This I/O handler runs as an applet embedded in a stream interface. It is @@ -551,7 +553,7 @@ static void cli_io_handler(struct appctx *appctx) else if (strcmp(trash.str, "prompt") == 0) appctx->st1 = !appctx->st1; else if (strcmp(trash.str, "help") == 0 || - !stats_sock_parse_request(si, trash.str)) { + !cli_parse_request(appctx, trash.str)) { cli_gen_usage_msg(); if (dynamic_usage_msg) appctx->ctx.cli.msg = dynamic_usage_msg;