vsh: Drop redundant definition searches from vshCmd{def,Grp}Help

These helpers are called from a single place only - cmdHelp wrapper and
just before the wrapper invokes the helpers, it performs the search,
either for command group or for the command itself, except the result is
discarded and the helper therefore needs to do it again. Drop this
inefficient handling and pass the @def structure rather than a name,
thus preventing the helper from needing to perform the search again.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Erik Skultety 2018-01-25 16:08:46 +01:00
parent 7697706135
commit da60878c4c
2 changed files with 17 additions and 30 deletions

View File

@ -629,44 +629,32 @@ vshCmdGrpSearch(const char *grpname)
} }
bool bool
vshCmdGrpHelp(vshControl *ctl, const char *grpname) vshCmdGrpHelp(vshControl *ctl, const vshCmdGrp *grp)
{ {
const vshCmdGrp *grp = vshCmdGrpSearch(grpname);
const vshCmdDef *cmd = NULL; const vshCmdDef *cmd = NULL;
if (!grp) { vshPrint(ctl, _(" %s (help keyword '%s'):\n"), grp->name,
vshError(ctl, _("command group '%s' doesn't exist"), grpname); grp->keyword);
return false;
} else {
vshPrint(ctl, _(" %s (help keyword '%s'):\n"), grp->name,
grp->keyword);
for (cmd = grp->commands; cmd->name; cmd++) { for (cmd = grp->commands; cmd->name; cmd++) {
if (cmd->flags & VSH_CMD_FLAG_ALIAS) if (cmd->flags & VSH_CMD_FLAG_ALIAS)
continue; continue;
vshPrint(ctl, " %-30s %s\n", cmd->name, vshPrint(ctl, " %-30s %s\n", cmd->name,
_(vshCmddefGetInfo(cmd, "help"))); _(vshCmddefGetInfo(cmd, "help")));
}
} }
return true; return true;
} }
bool bool
vshCmddefHelp(vshControl *ctl, const char *cmdname) vshCmddefHelp(vshControl *ctl, const vshCmdDef *def)
{ {
const vshCmdDef *def = vshCmddefSearch(cmdname);
const char *desc = NULL; const char *desc = NULL;
char buf[256]; char buf[256];
uint64_t opts_need_arg; uint64_t opts_need_arg;
uint64_t opts_required; uint64_t opts_required;
bool shortopt = false; /* true if 'arg' works instead of '--opt arg' */ bool shortopt = false; /* true if 'arg' works instead of '--opt arg' */
if (!def) {
vshError(ctl, _("command '%s' doesn't exist"), cmdname);
return false;
}
if (vshCmddefOptParse(def, &opts_need_arg, &opts_required)) { if (vshCmddefOptParse(def, &opts_need_arg, &opts_required)) {
vshError(ctl, _("internal error: bad options in command: '%s'"), vshError(ctl, _("internal error: bad options in command: '%s'"),
def->name); def->name);
@ -3181,12 +3169,11 @@ const vshCmdInfo info_help[] = {
bool bool
cmdHelp(vshControl *ctl, const vshCmd *cmd) cmdHelp(vshControl *ctl, const vshCmd *cmd)
{ {
const vshCmdDef *def = NULL;
const vshCmdGrp *grp = NULL;
const char *name = NULL; const char *name = NULL;
if (vshCommandOptStringQuiet(ctl, cmd, "command", &name) <= 0) { if (vshCommandOptStringQuiet(ctl, cmd, "command", &name) <= 0) {
const vshCmdGrp *grp;
const vshCmdDef *def;
vshPrint(ctl, "%s", _("Grouped commands:\n\n")); vshPrint(ctl, "%s", _("Grouped commands:\n\n"));
for (grp = cmdGroups; grp->name; grp++) { for (grp = cmdGroups; grp->name; grp++) {
@ -3206,10 +3193,10 @@ cmdHelp(vshControl *ctl, const vshCmd *cmd)
return true; return true;
} }
if (vshCmddefSearch(name)) { if ((def = vshCmddefSearch(name))) {
return vshCmddefHelp(ctl, name); return vshCmddefHelp(ctl, def);
} else if (vshCmdGrpSearch(name)) { } else if ((grp = vshCmdGrpSearch(name))) {
return vshCmdGrpHelp(ctl, name); return vshCmdGrpHelp(ctl, grp);
} else { } else {
vshError(ctl, _("command or command group '%s' doesn't exist"), name); vshError(ctl, _("command or command group '%s' doesn't exist"), name);
return false; return false;

View File

@ -257,9 +257,9 @@ void vshCloseLogFile(vshControl *ctl);
const char *vshCmddefGetInfo(const vshCmdDef *cmd, const char *info); const char *vshCmddefGetInfo(const vshCmdDef *cmd, const char *info);
const vshCmdDef *vshCmddefSearch(const char *cmdname); const vshCmdDef *vshCmddefSearch(const char *cmdname);
bool vshCmddefHelp(vshControl *ctl, const char *name); bool vshCmddefHelp(vshControl *ctl, const vshCmdDef *def);
const vshCmdGrp *vshCmdGrpSearch(const char *grpname); const vshCmdGrp *vshCmdGrpSearch(const char *grpname);
bool vshCmdGrpHelp(vshControl *ctl, const char *name); bool vshCmdGrpHelp(vshControl *ctl, const vshCmdGrp *grp);
int vshCommandOptInt(vshControl *ctl, const vshCmd *cmd, int vshCommandOptInt(vshControl *ctl, const vshCmd *cmd,
const char *name, int *value) const char *name, int *value)