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 
53 bool
54 CommandObjectHelp::Execute (Args& command, CommandReturnObject &result)
55 {
56     CommandObject::CommandMap::iterator pos;
57     CommandObject *cmd_obj;
58     const int argc = command.GetArgumentCount ();
59 
60     // 'help' doesn't take any options or arguments, other than command names.  If argc is 0, we show the user
61     // all commands and aliases.  Otherwise every argument must be the name of a command or a sub-command.
62     if (argc == 0)
63     {
64         result.SetStatus (eReturnStatusSuccessFinishNoResult);
65         m_interpreter.GetHelp (result);  // General help, for ALL commands.
66     }
67     else
68     {
69         // Get command object for the first command argument. Only search built-in command dictionary.
70         StringList matches;
71         cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
72 
73         if (cmd_obj != NULL)
74         {
75             bool all_okay = true;
76             CommandObject *sub_cmd_obj = cmd_obj;
77             // Loop down through sub_command dictionaries until we find the command object that corresponds
78             // to the help command entered.
79             for (int i = 1; i < argc && all_okay; ++i)
80             {
81                 std::string sub_command = command.GetArgumentAtIndex(i);
82                 if (! sub_cmd_obj->IsMultiwordObject ())
83                 {
84                     all_okay = false;
85                 }
86                 else
87                 {
88                     pos = ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.find (sub_command);
89                     if (pos != ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.end())
90                         sub_cmd_obj = pos->second.get();
91                     else
92                         all_okay = false;
93                 }
94             }
95 
96             if (!all_okay || (sub_cmd_obj == NULL))
97             {
98                 std::string cmd_string;
99                 command.GetCommandString (cmd_string);
100                 result.AppendErrorWithFormat
101                 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
102                  cmd_string.c_str());
103                 result.SetStatus (eReturnStatusFailed);
104             }
105             else
106             {
107                 Stream &output_strm = result.GetOutputStream();
108                 if (sub_cmd_obj->GetOptions() != NULL)
109                 {
110                     if (sub_cmd_obj->WantsRawCommandString())
111                     {
112                         std::string help_text (sub_cmd_obj->GetHelp());
113                         help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
114                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
115                     }
116                     else
117                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
118                     output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
119                     sub_cmd_obj->GetOptions()->GenerateOptionUsage (m_interpreter, output_strm, sub_cmd_obj);
120                     const char *long_help = sub_cmd_obj->GetHelpLong();
121                     if ((long_help != NULL)
122                         && (strlen (long_help) > 0))
123                         output_strm.Printf ("\n%s", long_help);
124                     // Mark this help command with a success status.
125                     result.SetStatus (eReturnStatusSuccessFinishNoResult);
126                 }
127                 else if (sub_cmd_obj->IsMultiwordObject())
128                 {
129                     if (sub_cmd_obj->WantsRawCommandString())
130                     {
131                         std::string help_text (sub_cmd_obj->GetHelp());
132                         help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
133                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
134                     }
135                     else
136                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
137                     ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result);
138                 }
139                 else
140                 {
141                     const char *long_help = sub_cmd_obj->GetHelpLong();
142                     if ((long_help != NULL)
143                         && (strlen (long_help) > 0))
144                         output_strm.Printf ("\n%s", long_help);
145                     else if (sub_cmd_obj->WantsRawCommandString())
146                     {
147                         std::string help_text (sub_cmd_obj->GetHelp());
148                         help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
149                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
150                     }
151                     else
152                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
153                     output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
154                     // Mark this help command with a success status.
155                     result.SetStatus (eReturnStatusSuccessFinishNoResult);
156                 }
157             }
158         }
159         else if (matches.GetSize() > 0)
160         {
161             Stream &output_strm = result.GetOutputStream();
162             output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
163             const uint32_t match_count = matches.GetSize();
164             for (uint32_t i = 0; i < match_count; i++)
165             {
166                 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
167             }
168         }
169         else
170         {
171             // Maybe the user is asking for help about a command argument rather than a command.
172             const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
173             if (arg_type != eArgTypeLastArg)
174             {
175                 Stream &output_strm = result.GetOutputStream ();
176                 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
177                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
178             }
179             else
180             {
181                 result.AppendErrorWithFormat
182                     ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
183                      command.GetArgumentAtIndex(0));
184                 result.SetStatus (eReturnStatusFailed);
185             }
186         }
187     }
188 
189     return result.Succeeded();
190 }
191 
192 int
193 CommandObjectHelp::HandleCompletion
194 (
195     Args &input,
196     int &cursor_index,
197     int &cursor_char_position,
198     int match_start_point,
199     int max_return_elements,
200     bool &word_complete,
201     StringList &matches
202 )
203 {
204     // Return the completions of the commands in the help system:
205     if (cursor_index == 0)
206     {
207         return m_interpreter.HandleCompletionMatches (input,
208                                                     cursor_index,
209                                                     cursor_char_position,
210                                                     match_start_point,
211                                                     max_return_elements,
212                                                     word_complete,
213                                                     matches);
214     }
215     else
216     {
217         CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
218         input.Shift();
219         cursor_index--;
220         return cmd_obj->HandleCompletion (input,
221                                           cursor_index,
222                                           cursor_char_position,
223                                           match_start_point,
224                                           max_return_elements,
225                                           word_complete,
226                                           matches);
227     }
228 }
229