diff options
author | Guillaume Nault <g.nault@alphalink.fr> | 2013-02-08 14:27:29 +0100 |
---|---|---|
committer | Kozlov Dmitry <xeb@mail.ru> | 2013-02-12 00:05:34 +0400 |
commit | 58e46bf8447f7c4ab96dd15251b769bc5647f7db (patch) | |
tree | 53bbd4c8ff794f2932bfec17374ccc0433b9677d /accel-pppd/cli/cli.c | |
parent | 9673dfcdd3866345c1641876b64a11c5f729f72e (diff) | |
download | accel-ppp-58e46bf8447f7c4ab96dd15251b769bc5647f7db.tar.gz accel-ppp-58e46bf8447f7c4ab96dd15251b769bc5647f7db.zip |
cli: Modify regexp command handler registration
Pass a full string, instead of an array of words, to the "help" callback
of regexp command handlers. Also register these command handlers using
a regexp to apply on "help" commands, just like for exec commands. The
"help" callback will then be called only if the command matches the
"help" regexp.
As a side effect, the "help" word and its following spaces are skipped
before calling the "help" callback for both simple and regexp commands.
Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
Diffstat (limited to 'accel-pppd/cli/cli.c')
-rw-r--r-- | accel-pppd/cli/cli.c | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/accel-pppd/cli/cli.c b/accel-pppd/cli/cli.c index 7ed2c44..e7e9e92 100644 --- a/accel-pppd/cli/cli.c +++ b/accel-pppd/cli/cli.c @@ -68,6 +68,16 @@ void __export cli_register_regexp_cmd(struct cli_regexp_cmd_t *cmd) int erroffset; const char *errptr; + if (cmd->exec == NULL) { + log_emerg("cli: impossible to register regexp command" + " without an execution callback function\n"); + _exit(EXIT_FAILURE); + } + if (cmd->pattern == NULL) { + log_emerg("cli: impossible to register regexp command" + " without pattern\n"); + _exit(EXIT_FAILURE); + } cmd->re = pcre_compile2(cmd->pattern, cmd->options, &err, &errptr, &erroffset, NULL); if (!cmd->re) { @@ -77,6 +87,23 @@ void __export cli_register_regexp_cmd(struct cli_regexp_cmd_t *cmd) cmd->pattern + erroffset); _exit(EXIT_FAILURE); } + + if (cmd->h_pattern) { + cmd->h_re = pcre_compile2(cmd->h_pattern, cmd->h_options, &err, + &errptr, &erroffset, NULL); + if (!cmd->h_re) { + log_emerg("cli: failed to compile help regexp \"%s\":" + " %s (error %i) at position %i (unprocessed" + " characters: \"%s\")\n", + cmd->h_pattern, errptr, err, erroffset, + cmd->h_pattern + erroffset); + _exit(EXIT_FAILURE); + } + } else { + cmd->h_re = NULL; + cmd->h_pattern = NULL; + } + list_add_tail(&cmd->entry, ®exp_cmd_list); } @@ -153,10 +180,18 @@ static int cli_process_help_cmd(struct cli_client_t *cln) if (!isblank(cmd[helpcmd_len]) && cmd[helpcmd_len] != '\0') return 0; + cmd = skip_space(cmd + helpcmd_len); + + list_for_each_entry(recmd, ®exp_cmd_list, entry) { + if (cmd[0] == '\0' + || pcre_exec(recmd->h_re, NULL, cmd, strlen(cmd), + 0, 0, NULL, 0) >= 0) { + if (recmd->help) + recmd->help(cmd, cln); + } + } + 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); |