1 //===-- CommandObjectHelp.cpp -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "CommandObjectHelp.h"
15 #include "lldb/Interpreter/CommandObjectMultiword.h"
16 #include "lldb/Interpreter/CommandInterpreter.h"
17 #include "lldb/Interpreter/Options.h"
18 #include "lldb/Interpreter/CommandReturnObject.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 //-------------------------------------------------------------------------
24 // CommandObjectHelp
25 //-------------------------------------------------------------------------
26 
27 void
28 CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage (Stream *s,
29                                                          const char* command,
30                                                          const char* prefix,
31                                                          const char* subcommand,
32                                                          bool include_apropos,
33                                                          bool include_type_lookup)
34 {
35     if (s && command && *command)
36     {
37         s->Printf("'%s' is not a known command.\n", command);
38         if (prefix && *prefix)
39         {
40             s->Printf("Try '%shelp' to see a current list of commands.\n", prefix);
41         }
42         else
43         {
44             s->PutCString("Try 'help' to see a current list of commands.\n");
45         }
46 
47         if (include_apropos)
48         {
49             s->Printf("Try 'apropos %s' for a list of related commands.\n", subcommand ? subcommand : command);
50         }
51         if (include_type_lookup)
52         {
53             s->Printf("Try 'type lookup %s' for information on types, methods, functions, modules, etc.", subcommand ? subcommand : command);
54         }
55     }
56 }
57 
58 CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) :
59     CommandObjectParsed (interpreter,
60                          "help",
61                          "Show a list of all debugger commands, or give details about specific commands.",
62                          "help [<cmd-name>]"), m_options (interpreter)
63 {
64     CommandArgumentEntry arg;
65     CommandArgumentData command_arg;
66 
67     // Define the first (and only) variant of this arg.
68     command_arg.arg_type = eArgTypeCommandName;
69     command_arg.arg_repetition = eArgRepeatStar;
70 
71     // There is only one variant this argument could be; put it into the argument entry.
72     arg.push_back (command_arg);
73 
74     // Push the data for the first argument into the m_arguments vector.
75     m_arguments.push_back (arg);
76 }
77 
78 CommandObjectHelp::~CommandObjectHelp() = default;
79 
80 OptionDefinition
81 CommandObjectHelp::CommandOptions::g_option_table[] =
82 {
83     { LLDB_OPT_SET_ALL, false, "hide-aliases", 'a', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,         "Hide aliases in the command list."},
84     { LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,         "Hide user-defined commands from the list."},
85     { LLDB_OPT_SET_ALL, false, "show-hidden-commands", 'h', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,         "Include commands prefixed with an underscore."},
86     { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
87 };
88 
89 bool
90 CommandObjectHelp::DoExecute (Args& command, CommandReturnObject &result)
91 {
92     CommandObject::CommandMap::iterator pos;
93     CommandObject *cmd_obj;
94     const size_t argc = command.GetArgumentCount ();
95 
96     // 'help' doesn't take any arguments, other than command names.  If argc is 0, we show the user
97     // all commands (aliases and user commands if asked for).  Otherwise every argument must be the name of a command or a sub-command.
98     if (argc == 0)
99     {
100         uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin;
101         if (m_options.m_show_aliases)
102             cmd_types |= CommandInterpreter::eCommandTypesAliases;
103         if (m_options.m_show_user_defined)
104             cmd_types |= CommandInterpreter::eCommandTypesUserDef;
105         if (m_options.m_show_hidden)
106             cmd_types |= CommandInterpreter::eCommandTypesHidden;
107 
108         result.SetStatus (eReturnStatusSuccessFinishNoResult);
109         m_interpreter.GetHelp (result, cmd_types);  // General help
110     }
111     else
112     {
113         // Get command object for the first command argument. Only search built-in command dictionary.
114         StringList matches;
115         cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
116         bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0));
117         std::string alias_name = command.GetArgumentAtIndex(0);
118 
119         if (cmd_obj != nullptr)
120         {
121             StringList matches;
122             bool all_okay = true;
123             CommandObject *sub_cmd_obj = cmd_obj;
124             // Loop down through sub_command dictionaries until we find the command object that corresponds
125             // to the help command entered.
126             std::string sub_command;
127             for (size_t i = 1; i < argc && all_okay; ++i)
128             {
129                 sub_command = command.GetArgumentAtIndex(i);
130                 matches.Clear();
131                 if (! sub_cmd_obj->IsMultiwordObject ())
132                 {
133                     all_okay = false;
134                 }
135                 else
136                 {
137                     CommandObject *found_cmd;
138                     found_cmd = sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
139                     if (found_cmd == nullptr)
140                         all_okay = false;
141                     else if (matches.GetSize() > 1)
142                         all_okay = false;
143                     else
144                         sub_cmd_obj = found_cmd;
145                 }
146             }
147 
148             if (!all_okay || (sub_cmd_obj == nullptr))
149             {
150                 std::string cmd_string;
151                 command.GetCommandString (cmd_string);
152                 if (matches.GetSize() >= 2)
153                 {
154                     StreamString s;
155                     s.Printf ("ambiguous command %s", cmd_string.c_str());
156                     size_t num_matches = matches.GetSize();
157                     for (size_t match_idx = 0; match_idx < num_matches; match_idx++)
158                     {
159                         s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx));
160                     }
161                     s.Printf ("\n");
162                     result.AppendError(s.GetData());
163                     result.SetStatus (eReturnStatusFailed);
164                     return false;
165                 }
166                 else if (!sub_cmd_obj)
167                 {
168                     StreamString error_msg_stream;
169                     GenerateAdditionalHelpAvenuesMessage(&error_msg_stream,
170                                                          cmd_string.c_str(),
171                                                          m_interpreter.GetCommandPrefix(),
172                                                          sub_command.c_str());
173                     result.AppendErrorWithFormat("%s",error_msg_stream.GetData());
174                     result.SetStatus (eReturnStatusFailed);
175                     return false;
176                 }
177                 else
178                 {
179                     GenerateAdditionalHelpAvenuesMessage(&result.GetOutputStream(),
180                                                          cmd_string.c_str(),
181                                                          m_interpreter.GetCommandPrefix(),
182                                                          sub_command.c_str());
183                     result.GetOutputStream().Printf("\nThe closest match is '%s'. Help on it follows.\n\n", sub_cmd_obj->GetCommandName());
184                 }
185             }
186 
187             sub_cmd_obj->GenerateHelpText(result);
188 
189             if (is_alias_command)
190             {
191                 StreamString sstr;
192                 m_interpreter.GetAlias(alias_name.c_str()).GetAliasHelp(sstr);
193                 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData());
194             }
195         }
196         else if (matches.GetSize() > 0)
197         {
198             Stream &output_strm = result.GetOutputStream();
199             output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
200             const size_t match_count = matches.GetSize();
201             for (size_t i = 0; i < match_count; i++)
202             {
203                 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
204             }
205         }
206         else
207         {
208             // Maybe the user is asking for help about a command argument rather than a command.
209             const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
210             if (arg_type != eArgTypeLastArg)
211             {
212                 Stream &output_strm = result.GetOutputStream ();
213                 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
214                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
215             }
216             else
217             {
218                 StreamString error_msg_stream;
219                 GenerateAdditionalHelpAvenuesMessage(&error_msg_stream, command.GetArgumentAtIndex(0), m_interpreter.GetCommandPrefix());
220                 result.AppendErrorWithFormat("%s",error_msg_stream.GetData());
221                 result.SetStatus (eReturnStatusFailed);
222             }
223         }
224     }
225 
226     return result.Succeeded();
227 }
228 
229 int
230 CommandObjectHelp::HandleCompletion(Args &input,
231                                     int &cursor_index,
232                                     int &cursor_char_position,
233                                     int match_start_point,
234                                     int max_return_elements,
235                                     bool &word_complete,
236                                     StringList &matches)
237 {
238     // Return the completions of the commands in the help system:
239     if (cursor_index == 0)
240     {
241         return m_interpreter.HandleCompletionMatches (input,
242                                                     cursor_index,
243                                                     cursor_char_position,
244                                                     match_start_point,
245                                                     max_return_elements,
246                                                     word_complete,
247                                                     matches);
248     }
249     else
250     {
251         CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
252 
253         // The command that they are getting help on might be ambiguous, in which case we should complete that,
254         // otherwise complete with the command the user is getting help on...
255 
256         if (cmd_obj)
257         {
258             input.Shift();
259             cursor_index--;
260             return cmd_obj->HandleCompletion (input,
261                                               cursor_index,
262                                               cursor_char_position,
263                                               match_start_point,
264                                               max_return_elements,
265                                               word_complete,
266                                               matches);
267         }
268         else
269         {
270             return m_interpreter.HandleCompletionMatches (input,
271                                                         cursor_index,
272                                                         cursor_char_position,
273                                                         match_start_point,
274                                                         max_return_elements,
275                                                         word_complete,
276                                                         matches);
277         }
278     }
279 }
280