130fdc8d8SChris Lattner //===-- CommandObjectHelp.cpp -----------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner #include "CommandObjectHelp.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
1630fdc8d8SChris Lattner #include "lldb/Interpreter/CommandObjectMultiword.h"
1730fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h"
1840af72e1SJim Ingham #include "lldb/Interpreter/Options.h"
1930fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
2030fdc8d8SChris Lattner 
2130fdc8d8SChris Lattner using namespace lldb;
2230fdc8d8SChris Lattner using namespace lldb_private;
2330fdc8d8SChris Lattner 
2430fdc8d8SChris Lattner //-------------------------------------------------------------------------
2530fdc8d8SChris Lattner // CommandObjectHelp
2630fdc8d8SChris Lattner //-------------------------------------------------------------------------
2730fdc8d8SChris Lattner 
28a7015092SGreg Clayton CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) :
295a988416SJim Ingham     CommandObjectParsed (interpreter,
30a7015092SGreg Clayton                          "help",
313f4c09c1SCaroline Tice                          "Show a list of all debugger commands, or give details about specific commands.",
3208633eeaSEnrico Granata                          "help [<cmd-name>]"), m_options (interpreter)
3330fdc8d8SChris Lattner {
34405fe67fSCaroline Tice     CommandArgumentEntry arg;
35405fe67fSCaroline Tice     CommandArgumentData command_arg;
36405fe67fSCaroline Tice 
37405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
38405fe67fSCaroline Tice     command_arg.arg_type = eArgTypeCommandName;
39405fe67fSCaroline Tice     command_arg.arg_repetition = eArgRepeatStar;
40405fe67fSCaroline Tice 
41405fe67fSCaroline Tice     // There is only one variant this argument could be; put it into the argument entry.
42405fe67fSCaroline Tice     arg.push_back (command_arg);
43405fe67fSCaroline Tice 
44405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
45405fe67fSCaroline Tice     m_arguments.push_back (arg);
4630fdc8d8SChris Lattner }
4730fdc8d8SChris Lattner 
4830fdc8d8SChris Lattner CommandObjectHelp::~CommandObjectHelp()
4930fdc8d8SChris Lattner {
5030fdc8d8SChris Lattner }
5130fdc8d8SChris Lattner 
5208633eeaSEnrico Granata OptionDefinition
5308633eeaSEnrico Granata CommandObjectHelp::CommandOptions::g_option_table[] =
5408633eeaSEnrico Granata {
5508633eeaSEnrico Granata     { LLDB_OPT_SET_ALL, false, "show-aliases", 'a', no_argument, NULL, 0, eArgTypeNone,         "Show aliases in the command list."},
5608633eeaSEnrico Granata     { LLDB_OPT_SET_ALL, false, "hide-user-commands", 'u', no_argument, NULL, 0, eArgTypeNone,         "Hide user-defined commands from the list."},
5708633eeaSEnrico Granata     { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
5808633eeaSEnrico Granata };
5908633eeaSEnrico Granata 
6030fdc8d8SChris Lattner bool
615a988416SJim Ingham CommandObjectHelp::DoExecute (Args& command, CommandReturnObject &result)
6230fdc8d8SChris Lattner {
6330fdc8d8SChris Lattner     CommandObject::CommandMap::iterator pos;
6430fdc8d8SChris Lattner     CommandObject *cmd_obj;
6530fdc8d8SChris Lattner     const int argc = command.GetArgumentCount ();
6630fdc8d8SChris Lattner 
6708633eeaSEnrico Granata     // 'help' doesn't take any arguments, other than command names.  If argc is 0, we show the user
6808633eeaSEnrico Granata     // all commands (aliases and user commands if asked for).  Otherwise every argument must be the name of a command or a sub-command.
6930fdc8d8SChris Lattner     if (argc == 0)
7030fdc8d8SChris Lattner     {
7108633eeaSEnrico Granata         uint32_t cmd_types = CommandInterpreter::eCommandTypesBuiltin;
7208633eeaSEnrico Granata         if (m_options.m_show_aliases)
7308633eeaSEnrico Granata             cmd_types |= CommandInterpreter::eCommandTypesAliases;
7408633eeaSEnrico Granata         if (m_options.m_show_user_defined)
7508633eeaSEnrico Granata             cmd_types |= CommandInterpreter::eCommandTypesUserDef;
7608633eeaSEnrico Granata 
7730fdc8d8SChris Lattner         result.SetStatus (eReturnStatusSuccessFinishNoResult);
7808633eeaSEnrico Granata         m_interpreter.GetHelp (result, cmd_types);  // General help
7930fdc8d8SChris Lattner     }
8030fdc8d8SChris Lattner     else
8130fdc8d8SChris Lattner     {
8230fdc8d8SChris Lattner         // Get command object for the first command argument. Only search built-in command dictionary.
83279a6c26SJim Ingham         StringList matches;
84a7015092SGreg Clayton         cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
85e7941795SCaroline Tice         bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0));
86e7941795SCaroline Tice         std::string alias_name = command.GetArgumentAtIndex(0);
8730fdc8d8SChris Lattner 
8830fdc8d8SChris Lattner         if (cmd_obj != NULL)
8930fdc8d8SChris Lattner         {
90271ad29aSJim Ingham             StringList matches;
9130fdc8d8SChris Lattner             bool all_okay = true;
9230fdc8d8SChris Lattner             CommandObject *sub_cmd_obj = cmd_obj;
9330fdc8d8SChris Lattner             // Loop down through sub_command dictionaries until we find the command object that corresponds
9430fdc8d8SChris Lattner             // to the help command entered.
9530fdc8d8SChris Lattner             for (int i = 1; i < argc && all_okay; ++i)
9630fdc8d8SChris Lattner             {
9730fdc8d8SChris Lattner                 std::string sub_command = command.GetArgumentAtIndex(i);
98271ad29aSJim Ingham                 matches.Clear();
9930fdc8d8SChris Lattner                 if (! sub_cmd_obj->IsMultiwordObject ())
10030fdc8d8SChris Lattner                 {
10130fdc8d8SChris Lattner                     all_okay = false;
10230fdc8d8SChris Lattner                 }
10330fdc8d8SChris Lattner                 else
10430fdc8d8SChris Lattner                 {
105271ad29aSJim Ingham                     CommandObject *found_cmd;
106*998255bfSGreg Clayton                     found_cmd = sub_cmd_obj->GetSubcommandObject(sub_command.c_str(), &matches);
107271ad29aSJim Ingham                     if (found_cmd == NULL)
10830fdc8d8SChris Lattner                         all_okay = false;
10997253e62SJim Ingham                     else if (matches.GetSize() > 1)
110271ad29aSJim Ingham                         all_okay = false;
111271ad29aSJim Ingham                     else
112271ad29aSJim Ingham                         sub_cmd_obj = found_cmd;
11330fdc8d8SChris Lattner                 }
11430fdc8d8SChris Lattner             }
11530fdc8d8SChris Lattner 
11630fdc8d8SChris Lattner             if (!all_okay || (sub_cmd_obj == NULL))
11730fdc8d8SChris Lattner             {
11830fdc8d8SChris Lattner                 std::string cmd_string;
11930fdc8d8SChris Lattner                 command.GetCommandString (cmd_string);
120271ad29aSJim Ingham                 if (matches.GetSize() < 2)
121271ad29aSJim Ingham                 {
122271ad29aSJim Ingham                     result.AppendErrorWithFormat("'%s' is not a known command.\n"
123271ad29aSJim Ingham                                                  "Try 'help' to see a current list of commands.\n",
12430fdc8d8SChris Lattner                                                  cmd_string.c_str());
125271ad29aSJim Ingham                 }
126271ad29aSJim Ingham                 else
127271ad29aSJim Ingham                 {
128271ad29aSJim Ingham                     StreamString s;
129271ad29aSJim Ingham                     s.Printf ("ambiguous command %s", cmd_string.c_str());
130271ad29aSJim Ingham                     size_t num_matches = matches.GetSize();
131271ad29aSJim Ingham                     for (size_t match_idx = 0; match_idx < num_matches; match_idx++)
132271ad29aSJim Ingham                     {
133271ad29aSJim Ingham                         s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx));
134271ad29aSJim Ingham                     }
135271ad29aSJim Ingham                     s.Printf ("\n");
136271ad29aSJim Ingham                     result.AppendError(s.GetData());
137271ad29aSJim Ingham                 }
138271ad29aSJim Ingham 
13930fdc8d8SChris Lattner                 result.SetStatus (eReturnStatusFailed);
14030fdc8d8SChris Lattner             }
14130fdc8d8SChris Lattner             else
14230fdc8d8SChris Lattner             {
14330fdc8d8SChris Lattner                 Stream &output_strm = result.GetOutputStream();
14430fdc8d8SChris Lattner                 if (sub_cmd_obj->GetOptions() != NULL)
14530fdc8d8SChris Lattner                 {
146e139cf23SCaroline Tice                     if (sub_cmd_obj->WantsRawCommandString())
147e139cf23SCaroline Tice                     {
148e139cf23SCaroline Tice                         std::string help_text (sub_cmd_obj->GetHelp());
149e139cf23SCaroline Tice                         help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
150e139cf23SCaroline Tice                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
151e139cf23SCaroline Tice                     }
152e139cf23SCaroline Tice                     else
153a7015092SGreg Clayton                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
15430fdc8d8SChris Lattner                     output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
155eb0103f2SGreg Clayton                     sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj);
15630fdc8d8SChris Lattner                     const char *long_help = sub_cmd_obj->GetHelpLong();
15730fdc8d8SChris Lattner                     if ((long_help != NULL)
15830fdc8d8SChris Lattner                         && (strlen (long_help) > 0))
15930fdc8d8SChris Lattner                         output_strm.Printf ("\n%s", long_help);
160921ca5d4SJohnny Chen                     if (sub_cmd_obj->WantsRawCommandString() && !sub_cmd_obj->WantsCompletion())
161d9d63369SCaroline Tice                     {
162c007e846SJim Ingham                         // Emit the message about using ' -- ' between the end of the command options and the raw input
163c007e846SJim Ingham                         // conditionally, i.e., only if the command object does not want completion.
164c007e846SJim Ingham                         m_interpreter.OutputFormattedHelpText (output_strm, "", "",
165c007e846SJim Ingham                                                                "\nIMPORTANT NOTE:  Because this command takes 'raw' input, if you use any command options"
166c007e846SJim Ingham                                                                " you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1);
167d9d63369SCaroline Tice                     }
168c007e846SJim Ingham                     else if (sub_cmd_obj->GetNumArgumentEntries() > 0
169c007e846SJim Ingham                              && sub_cmd_obj->GetOptions()
170c007e846SJim Ingham                              && sub_cmd_obj->GetOptions()->NumCommandOptions() > 0)
171c007e846SJim Ingham                     {
172c007e846SJim Ingham                             // Also emit a warning about using "--" in case you are using a command that takes options and arguments.
173c007e846SJim Ingham                             m_interpreter.OutputFormattedHelpText (output_strm, "", "",
174c007e846SJim Ingham                                                                    "\nThis command takes options and arguments, if your arguments look like option specifiers"
175c007e846SJim Ingham                                                                    " you must use '--' to terminate the options before starting to give the arguments.", 1);
176c007e846SJim Ingham                     }
177c007e846SJim Ingham 
178921ca5d4SJohnny Chen                     // Mark this help command with a success status.
17965045f21SJohnny Chen                     result.SetStatus (eReturnStatusSuccessFinishNoResult);
18030fdc8d8SChris Lattner                 }
18130fdc8d8SChris Lattner                 else if (sub_cmd_obj->IsMultiwordObject())
18230fdc8d8SChris Lattner                 {
183e139cf23SCaroline Tice                     if (sub_cmd_obj->WantsRawCommandString())
184e139cf23SCaroline Tice                     {
185e139cf23SCaroline Tice                         std::string help_text (sub_cmd_obj->GetHelp());
186e139cf23SCaroline Tice                         help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
187e139cf23SCaroline Tice                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
188e139cf23SCaroline Tice                     }
189e139cf23SCaroline Tice                     else
190a7015092SGreg Clayton                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
191*998255bfSGreg Clayton                     sub_cmd_obj->GenerateHelpText (result);
19230fdc8d8SChris Lattner                 }
19330fdc8d8SChris Lattner                 else
19430fdc8d8SChris Lattner                 {
19530fdc8d8SChris Lattner                     const char *long_help = sub_cmd_obj->GetHelpLong();
19630fdc8d8SChris Lattner                     if ((long_help != NULL)
19730fdc8d8SChris Lattner                         && (strlen (long_help) > 0))
19887637809SJohnny Chen                         output_strm.Printf ("%s", long_help);
199e139cf23SCaroline Tice                     else if (sub_cmd_obj->WantsRawCommandString())
200e139cf23SCaroline Tice                     {
201e139cf23SCaroline Tice                         std::string help_text (sub_cmd_obj->GetHelp());
202e139cf23SCaroline Tice                         help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
203e139cf23SCaroline Tice                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
204e139cf23SCaroline Tice                     }
20530fdc8d8SChris Lattner                     else
206a7015092SGreg Clayton                         m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
20730fdc8d8SChris Lattner                     output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
20865045f21SJohnny Chen                     // Mark this help command with a success status.
20965045f21SJohnny Chen                     result.SetStatus (eReturnStatusSuccessFinishNoResult);
21030fdc8d8SChris Lattner                 }
21130fdc8d8SChris Lattner             }
212e7941795SCaroline Tice 
213e7941795SCaroline Tice             if (is_alias_command)
214e7941795SCaroline Tice             {
215e7941795SCaroline Tice                 StreamString sstr;
216e7941795SCaroline Tice                 m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr);
217e7941795SCaroline Tice                 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData());
218e7941795SCaroline Tice             }
21930fdc8d8SChris Lattner         }
220279a6c26SJim Ingham         else if (matches.GetSize() > 0)
221279a6c26SJim Ingham         {
222279a6c26SJim Ingham             Stream &output_strm = result.GetOutputStream();
223279a6c26SJim Ingham             output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
224c982c768SGreg Clayton             const uint32_t match_count = matches.GetSize();
225c982c768SGreg Clayton             for (uint32_t i = 0; i < match_count; i++)
226279a6c26SJim Ingham             {
227279a6c26SJim Ingham                 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
228279a6c26SJim Ingham             }
229279a6c26SJim Ingham         }
23030fdc8d8SChris Lattner         else
23130fdc8d8SChris Lattner         {
232e139cf23SCaroline Tice             // Maybe the user is asking for help about a command argument rather than a command.
233e139cf23SCaroline Tice             const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0));
234e139cf23SCaroline Tice             if (arg_type != eArgTypeLastArg)
235e139cf23SCaroline Tice             {
236e139cf23SCaroline Tice                 Stream &output_strm = result.GetOutputStream ();
237e139cf23SCaroline Tice                 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter);
238e139cf23SCaroline Tice                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
239e139cf23SCaroline Tice             }
240e139cf23SCaroline Tice             else
241e139cf23SCaroline Tice             {
24230fdc8d8SChris Lattner                 result.AppendErrorWithFormat
24330fdc8d8SChris Lattner                     ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
24430fdc8d8SChris Lattner                      command.GetArgumentAtIndex(0));
24530fdc8d8SChris Lattner                 result.SetStatus (eReturnStatusFailed);
24630fdc8d8SChris Lattner             }
24730fdc8d8SChris Lattner         }
248e139cf23SCaroline Tice     }
24930fdc8d8SChris Lattner 
25030fdc8d8SChris Lattner     return result.Succeeded();
25130fdc8d8SChris Lattner }
25230fdc8d8SChris Lattner 
25330fdc8d8SChris Lattner int
25430fdc8d8SChris Lattner CommandObjectHelp::HandleCompletion
25530fdc8d8SChris Lattner (
25630fdc8d8SChris Lattner     Args &input,
25730fdc8d8SChris Lattner     int &cursor_index,
25830fdc8d8SChris Lattner     int &cursor_char_position,
25930fdc8d8SChris Lattner     int match_start_point,
26030fdc8d8SChris Lattner     int max_return_elements,
261558ce124SJim Ingham     bool &word_complete,
26230fdc8d8SChris Lattner     StringList &matches
26330fdc8d8SChris Lattner )
26430fdc8d8SChris Lattner {
26530fdc8d8SChris Lattner     // Return the completions of the commands in the help system:
26630fdc8d8SChris Lattner     if (cursor_index == 0)
26730fdc8d8SChris Lattner     {
268a7015092SGreg Clayton         return m_interpreter.HandleCompletionMatches (input,
269a7015092SGreg Clayton                                                     cursor_index,
270a7015092SGreg Clayton                                                     cursor_char_position,
271a7015092SGreg Clayton                                                     match_start_point,
272a7015092SGreg Clayton                                                     max_return_elements,
273a7015092SGreg Clayton                                                     word_complete,
274a7015092SGreg Clayton                                                     matches);
27530fdc8d8SChris Lattner     }
27630fdc8d8SChris Lattner     else
27730fdc8d8SChris Lattner     {
278a7015092SGreg Clayton         CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
2791d18ebf6SJim Ingham 
2801d18ebf6SJim Ingham         // The command that they are getting help on might be ambiguous, in which case we should complete that,
2811d18ebf6SJim Ingham         // otherwise complete with the command the user is getting help on...
2821d18ebf6SJim Ingham 
2831d18ebf6SJim Ingham         if (cmd_obj)
2841d18ebf6SJim Ingham         {
28530fdc8d8SChris Lattner             input.Shift();
28630fdc8d8SChris Lattner             cursor_index--;
287a7015092SGreg Clayton             return cmd_obj->HandleCompletion (input,
288a7015092SGreg Clayton                                               cursor_index,
289a7015092SGreg Clayton                                               cursor_char_position,
290a7015092SGreg Clayton                                               match_start_point,
291a7015092SGreg Clayton                                               max_return_elements,
292a7015092SGreg Clayton                                               word_complete,
293a7015092SGreg Clayton                                               matches);
29430fdc8d8SChris Lattner         }
2951d18ebf6SJim Ingham         else
2961d18ebf6SJim Ingham         {
2971d18ebf6SJim Ingham             return m_interpreter.HandleCompletionMatches (input,
2981d18ebf6SJim Ingham                                                         cursor_index,
2991d18ebf6SJim Ingham                                                         cursor_char_position,
3001d18ebf6SJim Ingham                                                         match_start_point,
3011d18ebf6SJim Ingham                                                         max_return_elements,
3021d18ebf6SJim Ingham                                                         word_complete,
3031d18ebf6SJim Ingham                                                         matches);
3041d18ebf6SJim Ingham         }
3051d18ebf6SJim Ingham     }
30630fdc8d8SChris Lattner }
307