diff options
author | Guillaume Nault <g.nault@alphalink.fr> | 2013-02-08 14:27:03 +0100 |
---|---|---|
committer | Kozlov Dmitry <xeb@mail.ru> | 2013-02-12 00:05:09 +0400 |
commit | cce2110e7ac9343372a65d82ec0791130a7580bf (patch) | |
tree | f2faf829c7f03591b75884d43d36ef2a561e22ba /accel-pppd/cli | |
parent | 1f20c83e04cfcf7ec0d9fefa934b1b7e79d03dc2 (diff) | |
download | accel-ppp-cce2110e7ac9343372a65d82ec0791130a7580bf.tar.gz accel-ppp-cce2110e7ac9343372a65d82ec0791130a7580bf.zip |
cli: Use dedicated "help" command handler
Process "help" commands in a separated function with no side effect
when the command doesn't match. This avoids splitting the input
command line if not necessary (regexp command handlers need to
receive the entire command line for being useful).
Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
Diffstat (limited to 'accel-pppd/cli')
-rw-r--r-- | accel-pppd/cli/cli.c | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/accel-pppd/cli/cli.c b/accel-pppd/cli/cli.c index 3240d77..0a7ca3a 100644 --- a/accel-pppd/cli/cli.c +++ b/accel-pppd/cli/cli.c @@ -1,3 +1,4 @@ +#include <ctype.h> #include <stdlib.h> #include <stdarg.h> #include <string.h> @@ -17,6 +18,9 @@ #define MSG_INVAL_ERROR "invalid argument\r\n" #define MSG_UNKNOWN_CMD "command unknown\r\n" +static const char helpcmd[] = "help"; +static const size_t helpcmd_len = sizeof(helpcmd) -1; + char *conf_cli_passwd; static const char *def_cli_prompt = "accel-ppp"; char *conf_cli_prompt; @@ -137,6 +141,32 @@ static int split(char *buf, char **ptr) return i; } +static int cli_process_help_cmd(struct cli_client_t *cln) +{ + struct cli_regexp_cmd_t *recmd = NULL; + struct cli_simple_cmd_t *sicmd = NULL; + char *cmd = (char *)cln->cmdline; + char *items[MAX_CMD_ITEMS] = { 0 }; + int nb_items; + + cmd = skip_space(cmd); + if (strncmp(helpcmd, cmd, helpcmd_len) != 0) + return 0; + + if (!isblank(cmd[helpcmd_len]) && cmd[helpcmd_len] != '\0') + return 0; + + nb_items = split(cmd, items); + list_for_each_entry(recmd, ®exp_cmd_list, entry) + if (recmd->help) + recmd->help(items, nb_items, cln); + list_for_each_entry(sicmd, &simple_cmd_list, entry) + if (sicmd->help) + sicmd->help(items, nb_items, cln); + + return 1; +} + int __export cli_process_cmd(struct cli_client_t *cln) { struct cli_simple_cmd_t *cmd1; @@ -144,19 +174,10 @@ int __export cli_process_cmd(struct cli_client_t *cln) char *f[MAX_CMD_ITEMS]; int r, i, n, found = 0; - n = split((char *)cln->cmdline, f); - - if (n >= 1 && !strcmp(f[0], "help")) { - list_for_each_entry(cmd1, &simple_cmd_list, entry) - if (cmd1->help) - cmd1->help(f, n, cln); - - list_for_each_entry(cmd2, ®exp_cmd_list, entry) - if (cmd2->help) - cmd2->help(f, n, cln); - + if (cli_process_help_cmd(cln)) return 0; - } + + n = split((char *)cln->cmdline, f); list_for_each_entry(cmd1, &simple_cmd_list, entry) { if (cmd1->hdr_len && n >= cmd1->hdr_len) { |