Clean up _RegisterCommands().

- _RegisterCommand() now accepts a space separated list of names
  to register a command by and creates a registration for each.
This commit is contained in:
Rene Gollent 2012-12-19 22:27:45 -05:00
parent cef640f784
commit 7024fac220
2 changed files with 35 additions and 30 deletions

View File

@ -306,43 +306,22 @@ CommandLineUserInterface::_InputLoop()
status_t
CommandLineUserInterface::_RegisterCommands()
{
BReference<CliCommand> stackTraceCommandReference(
new(std::nothrow) CliStackTraceCommand, true);
BReference<CliCommand> stackTraceCommandReference2(
stackTraceCommandReference.Get());
BReference<CliCommand> dumpCommandReference(
new(std::nothrow) CliDumpMemoryCommand, true);
BReference<CliCommand> dumpCommandReference2(
dumpCommandReference.Get());
if (!_RegisterCommand("db", dumpCommandReference.Detach()))
return B_NO_MEMORY;
dumpCommandReference = dumpCommandReference2.Get();
if (!_RegisterCommand("ds", dumpCommandReference.Detach()))
return B_NO_MEMORY;
dumpCommandReference = dumpCommandReference2.Get();
if (!_RegisterCommand("dw", dumpCommandReference.Detach()))
return B_NO_MEMORY;
dumpCommandReference = dumpCommandReference2.Get();
if (!_RegisterCommand("dl", dumpCommandReference.Detach()))
return B_NO_MEMORY;
if (!_RegisterCommand("string", dumpCommandReference2.Detach()))
return B_NO_MEMORY;
if (_RegisterCommand("bt", stackTraceCommandReference.Detach())
if (_RegisterCommand("bt sc", new(std::nothrow) CliStackTraceCommand)
&& _RegisterCommand("continue", new(std::nothrow) CliContinueCommand)
&& _RegisterCommand("db ds dw dl string", new(std::nothrow)
CliDumpMemoryCommand)
&& _RegisterCommand("frame", new(std::nothrow) CliStackFrameCommand)
&& _RegisterCommand("help", new(std::nothrow) HelpCommand(this))
&& _RegisterCommand("print", new(std::nothrow) CliPrintVariableCommand)
&& _RegisterCommand("quit", new(std::nothrow) CliQuitCommand)
&& _RegisterCommand("save-report",
new(std::nothrow) CliDebugReportCommand)
&& _RegisterCommand("sc", stackTraceCommandReference2.Detach())
&& _RegisterCommand("stop", new(std::nothrow) CliStopCommand)
&& _RegisterCommand("thread", new(std::nothrow) CliThreadCommand)
&& _RegisterCommand("threads", new(std::nothrow) CliThreadsCommand)
&& _RegisterCommand("variables",
new(std::nothrow) CliVariablesCommand)) {
fCommands.SortItems(&_CompareCommandEntries);
return B_OK;
}
@ -358,11 +337,23 @@ CommandLineUserInterface::_RegisterCommand(const BString& name,
if (name.IsEmpty() || command == NULL)
return false;
CommandEntry* entry = new(std::nothrow) CommandEntry(name, command);
if (entry == NULL || !fCommands.AddItem(entry)) {
delete entry;
return false;
}
BString nextName;
int32 startIndex = 0;
int32 spaceIndex;
do {
spaceIndex = name.FindFirst(' ', startIndex);
if (spaceIndex == B_ERROR)
spaceIndex = name.Length();
name.CopyInto(nextName, startIndex, spaceIndex - startIndex);
CommandEntry* entry = new(std::nothrow) CommandEntry(nextName,
command);
if (entry == NULL || !fCommands.AddItem(entry)) {
delete entry;
return false;
}
startIndex = spaceIndex + 1;
} while (startIndex < name.Length());
return true;
}
@ -441,3 +432,14 @@ CommandLineUserInterface::_PrintHelp(const char* commandName)
entry->Command()->Summary());
}
}
/*static */
int
CommandLineUserInterface::_CompareCommandEntries(const CommandEntry* command1,
const CommandEntry* command2)
{
return ::Compare(command1->Name(), command2->Name());
}

View File

@ -72,6 +72,9 @@ private:
const char* const* argv);
CommandEntry* _FindCommand(const char* commandName);
void _PrintHelp(const char* commandName);
static int _CompareCommandEntries(
const CommandEntry* command1,
const CommandEntry* command2);
private:
CliContext fContext;