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 () :
29     CommandObject ("help",
30                    "Show a list of all debugger commands, or give details about specific commands.",
31                    "help [<cmd-name>]")
32 {
33 }
34 
35 CommandObjectHelp::~CommandObjectHelp()
36 {
37 }
38 
39 
40 bool
41 CommandObjectHelp::Execute (CommandInterpreter &interpreter, Args& command, CommandReturnObject &result)
42 {
43     CommandObject::CommandMap::iterator pos;
44     CommandObject *cmd_obj;
45     const int argc = command.GetArgumentCount ();
46 
47     // 'help' doesn't take any options or arguments, other than command names.  If argc is 0, we show the user
48     // all commands and aliases.  Otherwise every argument must be the name of a command or a sub-command.
49     if (argc == 0)
50     {
51         result.SetStatus (eReturnStatusSuccessFinishNoResult);
52         interpreter.GetHelp (result);  // General help, for ALL commands.
53     }
54     else
55     {
56         // Get command object for the first command argument. Only search built-in command dictionary.
57         StringList matches;
58         cmd_obj = interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches);
59 
60         if (cmd_obj != NULL)
61         {
62             bool all_okay = true;
63             CommandObject *sub_cmd_obj = cmd_obj;
64             // Loop down through sub_command dictionaries until we find the command object that corresponds
65             // to the help command entered.
66             for (int i = 1; i < argc && all_okay; ++i)
67             {
68                 std::string sub_command = command.GetArgumentAtIndex(i);
69                 if (! sub_cmd_obj->IsMultiwordObject ())
70                 {
71                     all_okay = false;
72                 }
73                 else
74                 {
75                     pos = ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.find (sub_command);
76                     if (pos != ((CommandObjectMultiword *) sub_cmd_obj)->m_subcommand_dict.end())
77                         sub_cmd_obj = pos->second.get();
78                     else
79                         all_okay = false;
80                 }
81             }
82 
83             if (!all_okay || (sub_cmd_obj == NULL))
84             {
85                 std::string cmd_string;
86                 command.GetCommandString (cmd_string);
87                 result.AppendErrorWithFormat
88                 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
89                  cmd_string.c_str());
90                 result.SetStatus (eReturnStatusFailed);
91             }
92             else
93             {
94                 Stream &output_strm = result.GetOutputStream();
95                 if (sub_cmd_obj->GetOptions() != NULL)
96                 {
97                     interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
98                     output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
99                     sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj,
100                                                                     interpreter.GetDebugger().GetInstanceName().AsCString());
101                     const char *long_help = sub_cmd_obj->GetHelpLong();
102                     if ((long_help != NULL)
103                         && (strlen (long_help) > 0))
104                         output_strm.Printf ("\n%s", long_help);
105                 }
106                 else if (sub_cmd_obj->IsMultiwordObject())
107                 {
108                     interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
109                     ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (interpreter, result);
110                 }
111                 else
112                 {
113                     const char *long_help = sub_cmd_obj->GetHelpLong();
114                     if ((long_help != NULL)
115                         && (strlen (long_help) > 0))
116                         output_strm.Printf ("\n%s", long_help);
117                     else
118                         interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1);
119                     output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax());
120                 }
121             }
122         }
123         else if (matches.GetSize() > 0)
124         {
125             Stream &output_strm = result.GetOutputStream();
126             output_strm.Printf("Help requested with ambiguous command name, possible completions:\n");
127             const uint32_t match_count = matches.GetSize();
128             for (uint32_t i = 0; i < match_count; i++)
129             {
130                 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i));
131             }
132         }
133         else
134         {
135             result.AppendErrorWithFormat
136             ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
137              command.GetArgumentAtIndex(0));
138             result.SetStatus (eReturnStatusFailed);
139         }
140     }
141 
142     return result.Succeeded();
143 }
144 
145 int
146 CommandObjectHelp::HandleCompletion
147 (
148     CommandInterpreter &interpreter,
149     Args &input,
150     int &cursor_index,
151     int &cursor_char_position,
152     int match_start_point,
153     int max_return_elements,
154     bool &word_complete,
155     StringList &matches
156 )
157 {
158     // Return the completions of the commands in the help system:
159     if (cursor_index == 0)
160     {
161         return interpreter.HandleCompletionMatches(input, cursor_index, cursor_char_position, match_start_point,
162                                                    max_return_elements, word_complete, matches);
163     }
164     else
165     {
166         CommandObject *cmd_obj = interpreter.GetCommandObject (input.GetArgumentAtIndex(0));
167         input.Shift();
168         cursor_index--;
169         return cmd_obj->HandleCompletion (interpreter, input, cursor_index, cursor_char_position, match_start_point,
170                                           max_return_elements, word_complete, matches);
171     }
172 }
173