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