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 #include "CommandObjectHelp.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Interpreter/CommandObjectMultiword.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Interpreter/Options.h"
19 #include "lldb/Interpreter/CommandReturnObject.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 //-------------------------------------------------------------------------
25 // CommandObjectHelp
26 //-------------------------------------------------------------------------
27 
28 CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) :
29     CommandObject (interpreter,
30                    "help",
31                    "Show a list of all debugger commands, or give details about specific commands.",
32                    "help [<cmd-name>]")
33 {
34     CommandArgumentEntry arg;
35     CommandArgumentData command_arg;
36 
37     // Define the first (and only) variant of this arg.
38     command_arg.arg_type = eArgTypeCommandName;
39     command_arg.arg_repetition = eArgRepeatStar;
40 
41     // There is only one variant this argument could be; put it into the argument entry.
42     arg.push_back (command_arg);
43 
44     // Push the data for the first argument into the m_arguments vector.
45     m_arguments.push_back (arg);
46 }
47 
48 CommandObjectHelp::~CommandObjectHelp()
49 {
50 }
51 
52 bool
53 CommandObjectHelp::Execute (Args& command, CommandReturnObject &result)
54 {
55     CommandObject::CommandMap::iterator pos;
56     CommandObject *cmd_obj;
57     const int argc = command.GetArgumentCount ();
58 
59     // 'help' doesn't take any options or arguments, other than command names.  If argc is 0, we show the user
60     // all commands and aliases.  Otherwise every argument must be the name of a command or a sub-command.
61     if (argc == 0)
62     {
63         result.SetStatus (eReturnStatusSuccessFinishNoResult);
64         m_interpreter.GetHelp (result);  // General help, for ALL commands.
65     }
66     else
67     {
68         // Get command object for the first command argument. Only search built-in command dictionary.
69         StringList matches;
70         cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
71         bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0));
72         std::string alias_name = command.GetArgumentAtIndex(0);
73 
74         if (cmd_obj != NULL)
75         {
76             StringList matches;
77             bool all_okay = true;
78             CommandObject *sub_cmd_obj = cmd_obj;
79             // Loop down through sub_command dictionaries until we find the command object that corresponds
80             // to the help command entered.
81             for (int i = 1; i < argc && all_okay; ++i)
82             {
83                 std::string sub_command = command.GetArgumentAtIndex(i);
84                 matches.Clear();
85                 if (! sub_cmd_obj->IsMultiwordObject ())
86                 {
87                     all_okay = false;
88                 }
89                 else
90                 {
91                     CommandObject *found_cmd;
92                     found_cmd = ((CommandObjectMultiword *) sub_cmd_obj)->GetSubcommandObject(sub_command.c_str(),
93                                                                                               &matches);
94                     if (found_cmd == NULL)
95                         all_okay = false;
96                     else if (matches.GetSize() > 1)
97                         all_okay = false;
98                     else
99                         sub_cmd_obj = found_cmd;
100                 }
101             }
102 
103             if (!all_okay || (sub_cmd_obj == NULL))
104             {
105                 std::string cmd_string;
106                 command.GetCommandString (cmd_string);
107                 if (matches.GetSize() < 2)
108                 {
109                     result.AppendErrorWithFormat("'%s' is not a known command.\n"
110                                                  "Try 'help' to see a current list of commands.\n",
111                                                  cmd_string.c_str());
112                 }
113                 else
114                 {
115                     StreamString s;
116                     s.Printf ("ambiguous command %s", cmd_string.c_str());
117                     size_t num_matches = matches.GetSize();
118                     for (size_t match_idx = 0; match_idx < num_matches; match_idx++)
119                     {
120                         s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx));
121                     }
122                     s.Printf ("\n");
123                     result.AppendError(s.GetData());
124                 }
125 
126                 result.SetStatus (eReturnStatusFailed);
127             }
128             else
129             {
130                 Stream &output_strm = result.GetOutputStream();
131                 if (sub_cmd_obj->GetOptions() != NULL)
132                 {
133                     if (sub_cmd_obj->WantsRawCommandString())
134                     {
135                         std::string help_text (sub_cmd_obj->GetHelp());
136                         help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
137                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
138                     }
139                     else
140                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
141                     output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
142                     sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj);
143                     const char *long_help = sub_cmd_obj->GetHelpLong();
144                     if ((long_help != NULL)
145                         && (strlen (long_help) > 0))
146                         output_strm.Printf ("\n%s", long_help);
147                     // Mark this help command with a success status.
148                     if (sub_cmd_obj->WantsRawCommandString())
149                     {
150                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", "\nIMPORTANT NOTE:  Because this command takes 'raw' input, if you use any command options you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1);
151                     }
152                     result.SetStatus (eReturnStatusSuccessFinishNoResult);
153                 }
154                 else if (sub_cmd_obj->IsMultiwordObject())
155                 {
156                     if (sub_cmd_obj->WantsRawCommandString())
157                     {
158                         std::string help_text (sub_cmd_obj->GetHelp());
159                         help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
160                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
161                     }
162                     else
163                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
164                     ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result);
165                 }
166                 else
167                 {
168                     const char *long_help = sub_cmd_obj->GetHelpLong();
169                     if ((long_help != NULL)
170                         && (strlen (long_help) > 0))
171                         output_strm.Printf ("\n%s", long_help);
172                     else if (sub_cmd_obj->WantsRawCommandString())
173                     {
174                         std::string help_text (sub_cmd_obj->GetHelp());
175                         help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
176                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
177                     }
178                     else
179                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
180                     output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
181                     // Mark this help command with a success status.
182                     result.SetStatus (eReturnStatusSuccessFinishNoResult);
183                 }
184             }
185 
186             if (is_alias_command)
187             {
188                 StreamString sstr;
189                 m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr);
190                 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData());
191             }
192         }
193         else if (matches.GetSize() > 0)
194         {
195             Stream &output_strm = result.GetOutputStream();
196             output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
197             const uint32_t match_count = matches.GetSize();
198             for (uint32_t i = 0; i < match_count; i++)
199             {
200                 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
201             }
202         }
203         else
204         {
205             // Maybe the user is asking for help about a command argument rather than a command.
206             const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
207             if (arg_type != eArgTypeLastArg)
208             {
209                 Stream &output_strm = result.GetOutputStream ();
210                 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
211                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
212             }
213             else
214             {
215                 result.AppendErrorWithFormat
216                     ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
217                      command.GetArgumentAtIndex(0));
218                 result.SetStatus (eReturnStatusFailed);
219             }
220         }
221     }
222 
223     return result.Succeeded();
224 }
225 
226 int
227 CommandObjectHelp::HandleCompletion
228 (
229     Args &input,
230     int &cursor_index,
231     int &cursor_char_position,
232     int match_start_point,
233     int max_return_elements,
234     bool &word_complete,
235     StringList &matches
236 )
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         input.Shift();
253         cursor_index--;
254         return cmd_obj->HandleCompletion (input,
255                                           cursor_index,
256                                           cursor_char_position,
257                                           match_start_point,
258                                           max_return_elements,
259                                           word_complete,
260                                           matches);
261     }
262 }
263