1 //===-- CommandObjectMultiword.cpp ----------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Interpreter/CommandObjectMultiword.h"
10 #include "lldb/Interpreter/CommandInterpreter.h"
11 #include "lldb/Interpreter/CommandReturnObject.h"
12 #include "lldb/Interpreter/Options.h"
16 using namespace lldb_private
;
18 // CommandObjectMultiword
20 CommandObjectMultiword::CommandObjectMultiword(CommandInterpreter
&interpreter
,
25 : CommandObject(interpreter
, name
, help
, syntax
, flags
),
26 m_can_be_removed(false) {}
28 CommandObjectMultiword::~CommandObjectMultiword() = default;
31 CommandObjectMultiword::GetSubcommandSPExact(llvm::StringRef sub_cmd
) {
32 if (m_subcommand_dict
.empty())
35 auto pos
= m_subcommand_dict
.find(std::string(sub_cmd
));
36 if (pos
== m_subcommand_dict
.end())
42 CommandObjectSP
CommandObjectMultiword::GetSubcommandSP(llvm::StringRef sub_cmd
,
43 StringList
*matches
) {
44 if (m_subcommand_dict
.empty())
47 CommandObjectSP return_cmd_sp
= GetSubcommandSPExact(sub_cmd
);
50 matches
->AppendString(sub_cmd
);
54 CommandObject::CommandMap::iterator pos
;
56 StringList local_matches
;
57 if (matches
== nullptr)
58 matches
= &local_matches
;
60 AddNamesMatchingPartialString(m_subcommand_dict
, sub_cmd
, *matches
);
62 if (num_matches
== 1) {
63 // Cleaner, but slightly less efficient would be to call back into this
64 // function, since I now know I have an exact match...
66 sub_cmd
= matches
->GetStringAtIndex(0);
67 pos
= m_subcommand_dict
.find(std::string(sub_cmd
));
68 if (pos
!= m_subcommand_dict
.end())
69 return_cmd_sp
= pos
->second
;
76 CommandObjectMultiword::GetSubcommandObject(llvm::StringRef sub_cmd
,
77 StringList
*matches
) {
78 return GetSubcommandSP(sub_cmd
, matches
).get();
81 bool CommandObjectMultiword::LoadSubCommand(llvm::StringRef name
,
82 const CommandObjectSP
&cmd_obj_sp
) {
84 lldbassert((&GetCommandInterpreter() == &cmd_obj_sp
->GetCommandInterpreter()) &&
85 "tried to add a CommandObject from a different interpreter");
87 CommandMap::iterator pos
;
90 pos
= m_subcommand_dict
.find(std::string(name
));
91 if (pos
== m_subcommand_dict
.end()) {
92 m_subcommand_dict
[std::string(name
)] = cmd_obj_sp
;
99 llvm::Error
CommandObjectMultiword::LoadUserSubcommand(
100 llvm::StringRef name
, const CommandObjectSP
&cmd_obj_sp
, bool can_replace
) {
103 lldbassert((&GetCommandInterpreter() == &cmd_obj_sp
->GetCommandInterpreter()) &&
104 "tried to add a CommandObject from a different interpreter");
105 if (!IsUserCommand()) {
106 return llvm::createStringError(llvm::inconvertibleErrorCode(),
107 "can't add a user subcommand to a builtin container command.");
109 // Make sure this a user command if it isn't already:
110 cmd_obj_sp
->SetIsUserCommand(true);
112 std::string
str_name(name
);
114 auto pos
= m_subcommand_dict
.find(str_name
);
115 if (pos
== m_subcommand_dict
.end()) {
116 m_subcommand_dict
[str_name
] = cmd_obj_sp
;
117 return llvm::Error::success();
120 const char *error_str
= nullptr;
122 error_str
= "sub-command already exists";
123 if (!(*pos
).second
->IsUserCommand())
124 error_str
= "can't replace a builtin subcommand";
127 return llvm::createStringError(llvm::inconvertibleErrorCode(), error_str
);
129 m_subcommand_dict
[str_name
] = cmd_obj_sp
;
130 return llvm::Error::success();
133 llvm::Error
CommandObjectMultiword::RemoveUserSubcommand(llvm::StringRef cmd_name
,
134 bool must_be_multiword
) {
135 CommandMap::iterator pos
;
136 std::string
str_name(cmd_name
);
138 pos
= m_subcommand_dict
.find(str_name
);
139 if (pos
== m_subcommand_dict
.end()) {
140 return llvm::createStringError(llvm::inconvertibleErrorCode(),"subcommand '%s' not found.",
143 if (!(*pos
).second
->IsUserCommand()) {
144 return llvm::createStringError(llvm::inconvertibleErrorCode(),"subcommand '%s' not a user command.",
148 if (must_be_multiword
&& !(*pos
).second
->IsMultiwordObject()) {
149 return llvm::createStringError(llvm::inconvertibleErrorCode(),"subcommand '%s' is not a container command",
152 if (!must_be_multiword
&& (*pos
).second
->IsMultiwordObject()) {
153 return llvm::createStringError(llvm::inconvertibleErrorCode(),"subcommand '%s' is not a user command",
157 m_subcommand_dict
.erase(pos
);
159 return llvm::Error::success();
162 void CommandObjectMultiword::Execute(const char *args_string
,
163 CommandReturnObject
&result
) {
164 Args
args(args_string
);
165 const size_t argc
= args
.GetArgumentCount();
167 this->CommandObject::GenerateHelpText(result
);
171 auto sub_command
= args
[0].ref();
172 if (sub_command
.empty()) {
173 result
.AppendError("Need to specify a non-empty subcommand.");
177 if (m_subcommand_dict
.empty()) {
178 result
.AppendErrorWithFormat("'%s' does not have any subcommands.\n",
179 GetCommandName().str().c_str());
184 CommandObject
*sub_cmd_obj
= GetSubcommandObject(sub_command
, &matches
);
185 if (sub_cmd_obj
!= nullptr) {
186 // Now call CommandObject::Execute to process options in `rest_of_line`.
187 // From there the command-specific version of Execute will be called, with
188 // the processed arguments.
191 sub_cmd_obj
->Execute(args_string
, result
);
195 std::string error_msg
;
196 const size_t num_subcmd_matches
= matches
.GetSize();
197 if (num_subcmd_matches
> 0)
198 error_msg
.assign("ambiguous command ");
200 error_msg
.assign("invalid command ");
202 error_msg
.append("'");
203 error_msg
.append(std::string(GetCommandName()));
204 error_msg
.append(" ");
205 error_msg
.append(std::string(sub_command
));
206 error_msg
.append("'.");
208 if (num_subcmd_matches
> 0) {
209 error_msg
.append(" Possible completions:");
210 for (const std::string
&match
: matches
) {
211 error_msg
.append("\n\t");
212 error_msg
.append(match
);
215 error_msg
.append("\n");
216 result
.AppendRawError(error_msg
.c_str());
219 void CommandObjectMultiword::GenerateHelpText(Stream
&output_stream
) {
220 // First time through here, generate the help text for the object and push it
221 // to the return result object as well
223 CommandObject::GenerateHelpText(output_stream
);
224 output_stream
.PutCString("\nThe following subcommands are supported:\n\n");
226 CommandMap::iterator pos
;
227 uint32_t max_len
= FindLongestCommandWord(m_subcommand_dict
);
230 max_len
+= 4; // Indent the output by 4 spaces.
232 for (pos
= m_subcommand_dict
.begin(); pos
!= m_subcommand_dict
.end(); ++pos
) {
233 std::string
indented_command(" ");
234 indented_command
.append(pos
->first
);
235 if (pos
->second
->WantsRawCommandString()) {
236 std::string
help_text(std::string(pos
->second
->GetHelp()));
237 help_text
.append(" Expects 'raw' input (see 'help raw-input'.)");
238 m_interpreter
.OutputFormattedHelpText(output_stream
, indented_command
,
239 "--", help_text
, max_len
);
241 m_interpreter
.OutputFormattedHelpText(output_stream
, indented_command
,
242 "--", pos
->second
->GetHelp(),
246 output_stream
.PutCString("\nFor more help on any particular subcommand, type "
247 "'help <command> <subcommand>'.\n");
250 void CommandObjectMultiword::HandleCompletion(CompletionRequest
&request
) {
251 auto arg0
= request
.GetParsedLine()[0].ref();
252 if (request
.GetCursorIndex() == 0) {
253 StringList new_matches
, descriptions
;
254 AddNamesMatchingPartialString(m_subcommand_dict
, arg0
, new_matches
,
256 request
.AddCompletions(new_matches
, descriptions
);
258 if (new_matches
.GetSize() == 1 &&
259 new_matches
.GetStringAtIndex(0) != nullptr &&
260 (arg0
== new_matches
.GetStringAtIndex(0))) {
261 StringList temp_matches
;
262 CommandObject
*cmd_obj
= GetSubcommandObject(arg0
, &temp_matches
);
263 if (cmd_obj
!= nullptr) {
264 if (request
.GetParsedLine().GetArgumentCount() != 1) {
265 request
.GetParsedLine().Shift();
266 request
.AppendEmptyArgument();
267 cmd_obj
->HandleCompletion(request
);
274 StringList new_matches
;
275 CommandObject
*sub_command_object
= GetSubcommandObject(arg0
, &new_matches
);
277 // The subcommand is ambiguous. The completion isn't meaningful.
278 if (!sub_command_object
)
281 // Remove the one match that we got from calling GetSubcommandObject.
282 new_matches
.DeleteStringAtIndex(0);
283 request
.AddCompletions(new_matches
);
284 request
.ShiftArguments();
285 sub_command_object
->HandleCompletion(request
);
288 std::optional
<std::string
>
289 CommandObjectMultiword::GetRepeatCommand(Args
¤t_command_args
,
292 if (current_command_args
.GetArgumentCount() <= index
)
294 CommandObject
*sub_command_object
=
295 GetSubcommandObject(current_command_args
[index
].ref());
296 if (sub_command_object
== nullptr)
298 return sub_command_object
->GetRepeatCommand(current_command_args
, index
);
301 CommandObjectProxy::CommandObjectProxy(CommandInterpreter
&interpreter
,
302 const char *name
, const char *help
,
303 const char *syntax
, uint32_t flags
)
304 : CommandObject(interpreter
, name
, help
, syntax
, flags
) {}
306 CommandObjectProxy::~CommandObjectProxy() = default;
308 Options
*CommandObjectProxy::GetOptions() {
309 CommandObject
*proxy_command
= GetProxyCommandObject();
311 return proxy_command
->GetOptions();
312 return CommandObject::GetOptions();
315 llvm::StringRef
CommandObjectProxy::GetHelp() {
316 CommandObject
*proxy_command
= GetProxyCommandObject();
318 return proxy_command
->GetHelp();
319 return CommandObject::GetHelp();
322 llvm::StringRef
CommandObjectProxy::GetSyntax() {
323 CommandObject
*proxy_command
= GetProxyCommandObject();
325 return proxy_command
->GetSyntax();
326 return CommandObject::GetSyntax();
329 llvm::StringRef
CommandObjectProxy::GetHelpLong() {
330 CommandObject
*proxy_command
= GetProxyCommandObject();
332 return proxy_command
->GetHelpLong();
333 return CommandObject::GetHelpLong();
336 bool CommandObjectProxy::IsRemovable() const {
337 const CommandObject
*proxy_command
=
338 const_cast<CommandObjectProxy
*>(this)->GetProxyCommandObject();
340 return proxy_command
->IsRemovable();
344 bool CommandObjectProxy::IsMultiwordObject() {
345 CommandObject
*proxy_command
= GetProxyCommandObject();
347 return proxy_command
->IsMultiwordObject();
351 CommandObjectMultiword
*CommandObjectProxy::GetAsMultiwordCommand() {
352 CommandObject
*proxy_command
= GetProxyCommandObject();
354 return proxy_command
->GetAsMultiwordCommand();
358 void CommandObjectProxy::GenerateHelpText(Stream
&result
) {
359 CommandObject
*proxy_command
= GetProxyCommandObject();
361 proxy_command
->GenerateHelpText(result
);
363 CommandObject::GenerateHelpText(result
);
366 lldb::CommandObjectSP
367 CommandObjectProxy::GetSubcommandSP(llvm::StringRef sub_cmd
,
368 StringList
*matches
) {
369 CommandObject
*proxy_command
= GetProxyCommandObject();
371 return proxy_command
->GetSubcommandSP(sub_cmd
, matches
);
372 return lldb::CommandObjectSP();
375 CommandObject
*CommandObjectProxy::GetSubcommandObject(llvm::StringRef sub_cmd
,
376 StringList
*matches
) {
377 CommandObject
*proxy_command
= GetProxyCommandObject();
379 return proxy_command
->GetSubcommandObject(sub_cmd
, matches
);
383 bool CommandObjectProxy::LoadSubCommand(
384 llvm::StringRef cmd_name
, const lldb::CommandObjectSP
&command_sp
) {
385 CommandObject
*proxy_command
= GetProxyCommandObject();
387 return proxy_command
->LoadSubCommand(cmd_name
, command_sp
);
391 bool CommandObjectProxy::WantsRawCommandString() {
392 CommandObject
*proxy_command
= GetProxyCommandObject();
394 return proxy_command
->WantsRawCommandString();
398 bool CommandObjectProxy::WantsCompletion() {
399 CommandObject
*proxy_command
= GetProxyCommandObject();
401 return proxy_command
->WantsCompletion();
405 void CommandObjectProxy::HandleCompletion(CompletionRequest
&request
) {
406 CommandObject
*proxy_command
= GetProxyCommandObject();
408 proxy_command
->HandleCompletion(request
);
411 void CommandObjectProxy::HandleArgumentCompletion(
412 CompletionRequest
&request
, OptionElementVector
&opt_element_vector
) {
413 CommandObject
*proxy_command
= GetProxyCommandObject();
415 proxy_command
->HandleArgumentCompletion(request
, opt_element_vector
);
418 std::optional
<std::string
>
419 CommandObjectProxy::GetRepeatCommand(Args
¤t_command_args
,
421 CommandObject
*proxy_command
= GetProxyCommandObject();
423 return proxy_command
->GetRepeatCommand(current_command_args
, index
);
427 llvm::StringRef
CommandObjectProxy::GetUnsupportedError() {
428 return "command is not implemented";
431 void CommandObjectProxy::Execute(const char *args_string
,
432 CommandReturnObject
&result
) {
433 if (CommandObject
*proxy_command
= GetProxyCommandObject())
434 proxy_command
->Execute(args_string
, result
);
436 result
.AppendError(GetUnsupportedError());