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 bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0)); 73 std::string alias_name = command.GetArgumentAtIndex(0); 74 75 if (cmd_obj != NULL) 76 { 77 StringList matches; 78 bool all_okay = true; 79 CommandObject *sub_cmd_obj = cmd_obj; 80 // Loop down through sub_command dictionaries until we find the command object that corresponds 81 // to the help command entered. 82 for (int i = 1; i < argc && all_okay; ++i) 83 { 84 std::string sub_command = command.GetArgumentAtIndex(i); 85 matches.Clear(); 86 if (! sub_cmd_obj->IsMultiwordObject ()) 87 { 88 all_okay = false; 89 } 90 else 91 { 92 CommandObject *found_cmd; 93 found_cmd = ((CommandObjectMultiword *) sub_cmd_obj)->GetSubcommandObject(sub_command.c_str(), 94 &matches); 95 if (found_cmd == NULL) 96 all_okay = false; 97 else if (matches.GetSize() > 1) 98 all_okay = false; 99 else 100 sub_cmd_obj = found_cmd; 101 } 102 } 103 104 if (!all_okay || (sub_cmd_obj == NULL)) 105 { 106 std::string cmd_string; 107 command.GetCommandString (cmd_string); 108 if (matches.GetSize() < 2) 109 { 110 result.AppendErrorWithFormat("'%s' is not a known command.\n" 111 "Try 'help' to see a current list of commands.\n", 112 cmd_string.c_str()); 113 } 114 else 115 { 116 StreamString s; 117 s.Printf ("ambiguous command %s", cmd_string.c_str()); 118 size_t num_matches = matches.GetSize(); 119 for (size_t match_idx = 0; match_idx < num_matches; match_idx++) 120 { 121 s.Printf ("\n\t%s", matches.GetStringAtIndex(match_idx)); 122 } 123 s.Printf ("\n"); 124 result.AppendError(s.GetData()); 125 } 126 127 result.SetStatus (eReturnStatusFailed); 128 } 129 else 130 { 131 Stream &output_strm = result.GetOutputStream(); 132 if (sub_cmd_obj->GetOptions() != NULL) 133 { 134 if (sub_cmd_obj->WantsRawCommandString()) 135 { 136 std::string help_text (sub_cmd_obj->GetHelp()); 137 help_text.append (" This command takes 'raw' input (no need to quote stuff)."); 138 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); 139 } 140 else 141 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 142 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); 143 sub_cmd_obj->GetOptions()->GenerateOptionUsage (output_strm, sub_cmd_obj); 144 const char *long_help = sub_cmd_obj->GetHelpLong(); 145 if ((long_help != NULL) 146 && (strlen (long_help) > 0)) 147 output_strm.Printf ("\n%s", long_help); 148 // Mark this help command with a success status. 149 if (sub_cmd_obj->WantsRawCommandString()) 150 { 151 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); 152 } 153 result.SetStatus (eReturnStatusSuccessFinishNoResult); 154 } 155 else if (sub_cmd_obj->IsMultiwordObject()) 156 { 157 if (sub_cmd_obj->WantsRawCommandString()) 158 { 159 std::string help_text (sub_cmd_obj->GetHelp()); 160 help_text.append (" This command takes 'raw' input (no need to quote stuff)."); 161 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); 162 } 163 else 164 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 165 ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result); 166 } 167 else 168 { 169 const char *long_help = sub_cmd_obj->GetHelpLong(); 170 if ((long_help != NULL) 171 && (strlen (long_help) > 0)) 172 output_strm.Printf ("\n%s", long_help); 173 else if (sub_cmd_obj->WantsRawCommandString()) 174 { 175 std::string help_text (sub_cmd_obj->GetHelp()); 176 help_text.append (" This command takes 'raw' input (no need to quote stuff)."); 177 m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); 178 } 179 else 180 m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); 181 output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); 182 // Mark this help command with a success status. 183 result.SetStatus (eReturnStatusSuccessFinishNoResult); 184 } 185 } 186 187 if (is_alias_command) 188 { 189 StreamString sstr; 190 m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr); 191 result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData()); 192 } 193 } 194 else if (matches.GetSize() > 0) 195 { 196 Stream &output_strm = result.GetOutputStream(); 197 output_strm.Printf("Help requested with ambiguous command name, possible completions:\n"); 198 const uint32_t match_count = matches.GetSize(); 199 for (uint32_t i = 0; i < match_count; i++) 200 { 201 output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); 202 } 203 } 204 else 205 { 206 // Maybe the user is asking for help about a command argument rather than a command. 207 const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0)); 208 if (arg_type != eArgTypeLastArg) 209 { 210 Stream &output_strm = result.GetOutputStream (); 211 CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter); 212 result.SetStatus (eReturnStatusSuccessFinishNoResult); 213 } 214 else 215 { 216 result.AppendErrorWithFormat 217 ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", 218 command.GetArgumentAtIndex(0)); 219 result.SetStatus (eReturnStatusFailed); 220 } 221 } 222 } 223 224 return result.Succeeded(); 225 } 226 227 int 228 CommandObjectHelp::HandleCompletion 229 ( 230 Args &input, 231 int &cursor_index, 232 int &cursor_char_position, 233 int match_start_point, 234 int max_return_elements, 235 bool &word_complete, 236 StringList &matches 237 ) 238 { 239 // Return the completions of the commands in the help system: 240 if (cursor_index == 0) 241 { 242 return m_interpreter.HandleCompletionMatches (input, 243 cursor_index, 244 cursor_char_position, 245 match_start_point, 246 max_return_elements, 247 word_complete, 248 matches); 249 } 250 else 251 { 252 CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0)); 253 input.Shift(); 254 cursor_index--; 255 return cmd_obj->HandleCompletion (input, 256 cursor_index, 257 cursor_char_position, 258 match_start_point, 259 max_return_elements, 260 word_complete, 261 matches); 262 } 263 } 264