char **word, int all)
{
int n, rc = 0, completion = (word != NULL);
+ int help = 0; /* Are we asking for help? */
struct cmd_env env = {
.elements = TAILQ_HEAD_INITIALIZER(env.elements),
.stack = TAILQ_HEAD_INITIALIZER(env.stack),
* execution until we reach the cursor position. */
struct cmd_node *current = NULL;
while ((current = cmdenv_top(&env))) {
+ if (!completion)
+ help = !!cmdenv_get(&env, "help"); /* Are we asking for help? */
+
struct cmd_node *candidate, *best = NULL;
const char *token = (env.argp < env.argc) ? env.argv[env.argp] :
- (env.argp == env.argc) ? NEWLINE : NULL;
+ (env.argp == env.argc && !help) ? NEWLINE : NULL;
if (token == NULL ||
(completion && env.argp == env.argc - 1))
goto end;
env.argp++;
}
end:
- if (!completion) {
+ if (!completion && !help) {
if (rc == 0 && env.argp != env.argc + 1) {
log_warnx("lldpctl", "incomplete command");
rc = -1;
}
- } else if (rc == 0 && env.argp == env.argc - 1) {
+ } else if (rc == 0 && (env.argp == env.argc - 1 || help)) {
/* We need to complete. Let's build the list of candidate words. */
struct cmd_node *candidate = NULL;
int maxl = 10; /* Max length of a word */
TAILQ_INIT(&words);
current = cmdenv_top(&env);
TAILQ_FOREACH(candidate, ¤t->subentries, next) {
- if ((!candidate->token ||
+ if ((!candidate->token || help ||
!strncmp(env.argv[env.argc - 1], candidate->token,
strlen(env.argv[env.argc -1 ]))) &&
(!candidate->validate ||
}
/* If the prefix is complete, add a space, otherwise,
* just return it as is. */
- if (!all && strcmp(prefix, NEWLINE) &&
+ if (!all && !help && strcmp(prefix, NEWLINE) &&
strlen(prefix) > 0 &&
strlen(env.argv[env.argc-1]) < strlen(prefix)) {
TAILQ_FOREACH(cword, &words, next) {
NULL, cmd_update, NULL);
register_commands_configure(root);
}
+ commands_new(root, "help", "Get help on a possible command",
+ NULL, cmd_store_env_and_pop, "help");
commands_new(
commands_new(root, "exit", "Exit interpreter", NULL, NULL, NULL),
NEWLINE, "Exit interpreter", NULL, cmd_exit, NULL);
} else {
#ifdef HAVE_LIBREADLINE
/* Shell session */
- rl_bind_key('?', cmd_help);
+ rl_bind_key('?', cmd_help);
rl_bind_key('\t', cmd_complete);
#endif
}