Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | //------------------------------------------------------------------------- |
| 25 | // CommandObjectHelp |
| 26 | //------------------------------------------------------------------------- |
| 27 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 28 | CommandObjectHelp::CommandObjectHelp (CommandInterpreter &interpreter) : |
| 29 | CommandObject (interpreter, |
| 30 | "help", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 31 | "Show a list of all debugger commands, or give details about specific commands.", |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 32 | "help [<cmd-name>]") |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | { |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 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); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | CommandObjectHelp::~CommandObjectHelp() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | |
| 53 | bool |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 54 | CommandObjectHelp::Execute (Args& command, CommandReturnObject &result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | { |
| 56 | CommandObject::CommandMap::iterator pos; |
| 57 | CommandObject *cmd_obj; |
| 58 | const int argc = command.GetArgumentCount (); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | if (argc == 0) |
| 63 | { |
| 64 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 65 | m_interpreter.GetHelp (result); // General help, for ALL commands. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
| 69 | // Get command object for the first command argument. Only search built-in command dictionary. |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 70 | StringList matches; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 71 | cmd_obj = m_interpreter.GetCommandObject (command.GetArgumentAtIndex (0), &matches); |
Caroline Tice | e6866a3 | 2010-10-28 23:17:48 +0000 | [diff] [blame] | 72 | bool is_alias_command = m_interpreter.AliasExists (command.GetArgumentAtIndex (0)); |
| 73 | std::string alias_name = command.GetArgumentAtIndex(0); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | if (cmd_obj != NULL) |
| 76 | { |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 77 | StringList matches; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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); |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 85 | matches.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | if (! sub_cmd_obj->IsMultiwordObject ()) |
| 87 | { |
| 88 | all_okay = false; |
| 89 | } |
| 90 | else |
| 91 | { |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 92 | CommandObject *found_cmd; |
| 93 | found_cmd = ((CommandObjectMultiword *) sub_cmd_obj)->GetSubcommandObject(sub_command.c_str(), |
| 94 | &matches); |
| 95 | if (found_cmd == NULL) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 96 | all_okay = false; |
Jim Ingham | dd1f02a | 2010-12-01 00:42:17 +0000 | [diff] [blame^] | 97 | else if (matches.GetSize() > 1) |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 98 | all_okay = false; |
| 99 | else |
| 100 | sub_cmd_obj = found_cmd; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | if (!all_okay || (sub_cmd_obj == NULL)) |
| 105 | { |
| 106 | std::string cmd_string; |
| 107 | command.GetCommandString (cmd_string); |
Jim Ingham | c2d382c | 2010-11-30 22:59:37 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 127 | result.SetStatus (eReturnStatusFailed); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | Stream &output_strm = result.GetOutputStream(); |
| 132 | if (sub_cmd_obj->GetOptions() != NULL) |
| 133 | { |
Caroline Tice | fb35511 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 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); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 143 | sub_cmd_obj->GetOptions()->GenerateOptionUsage (m_interpreter, output_strm, sub_cmd_obj); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | const char *long_help = sub_cmd_obj->GetHelpLong(); |
| 145 | if ((long_help != NULL) |
| 146 | && (strlen (long_help) > 0)) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 147 | output_strm.Printf ("\n%s", long_help); |
Johnny Chen | 8e46859 | 2010-10-08 17:21:27 +0000 | [diff] [blame] | 148 | // Mark this help command with a success status. |
| 149 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 150 | } |
| 151 | else if (sub_cmd_obj->IsMultiwordObject()) |
| 152 | { |
Caroline Tice | fb35511 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 153 | if (sub_cmd_obj->WantsRawCommandString()) |
| 154 | { |
| 155 | std::string help_text (sub_cmd_obj->GetHelp()); |
| 156 | help_text.append (" This command takes 'raw' input (no need to quote stuff)."); |
| 157 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); |
| 158 | } |
| 159 | else |
| 160 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 161 | ((CommandObjectMultiword *) sub_cmd_obj)->GenerateHelpText (result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | } |
| 163 | else |
| 164 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 165 | const char *long_help = sub_cmd_obj->GetHelpLong(); |
| 166 | if ((long_help != NULL) |
| 167 | && (strlen (long_help) > 0)) |
Caroline Tice | abb507a | 2010-09-08 21:06:11 +0000 | [diff] [blame] | 168 | output_strm.Printf ("\n%s", long_help); |
Caroline Tice | fb35511 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 169 | else if (sub_cmd_obj->WantsRawCommandString()) |
| 170 | { |
| 171 | std::string help_text (sub_cmd_obj->GetHelp()); |
| 172 | help_text.append (" This command takes 'raw' input (no need to quote stuff)."); |
| 173 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1); |
| 174 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 175 | else |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 176 | m_interpreter.OutputFormattedHelpText (output_strm, "", "", sub_cmd_obj->GetHelp(), 1); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 177 | output_strm.Printf ("\nSyntax: %s\n", sub_cmd_obj->GetSyntax()); |
Johnny Chen | 8e46859 | 2010-10-08 17:21:27 +0000 | [diff] [blame] | 178 | // Mark this help command with a success status. |
| 179 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
Caroline Tice | e6866a3 | 2010-10-28 23:17:48 +0000 | [diff] [blame] | 182 | |
| 183 | if (is_alias_command) |
| 184 | { |
| 185 | StreamString sstr; |
| 186 | m_interpreter.GetAliasHelp (alias_name.c_str(), cmd_obj->GetCommandName(), sstr); |
| 187 | result.GetOutputStream().Printf ("\n'%s' is an abbreviation for %s\n", alias_name.c_str(), sstr.GetData()); |
| 188 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | } |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 190 | else if (matches.GetSize() > 0) |
| 191 | { |
| 192 | Stream &output_strm = result.GetOutputStream(); |
| 193 | output_strm.Printf("Help requested with ambiguous command name, possible completions:\n"); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 194 | const uint32_t match_count = matches.GetSize(); |
| 195 | for (uint32_t i = 0; i < match_count; i++) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 196 | { |
| 197 | output_strm.Printf("\t%s\n", matches.GetStringAtIndex(i)); |
| 198 | } |
| 199 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 200 | else |
| 201 | { |
Caroline Tice | fb35511 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 202 | // Maybe the user is asking for help about a command argument rather than a command. |
| 203 | const CommandArgumentType arg_type = CommandObject::LookupArgumentName (command.GetArgumentAtIndex (0)); |
| 204 | if (arg_type != eArgTypeLastArg) |
| 205 | { |
| 206 | Stream &output_strm = result.GetOutputStream (); |
| 207 | CommandObject::GetArgumentHelp (output_strm, arg_type, m_interpreter); |
| 208 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | result.AppendErrorWithFormat |
| 213 | ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n", |
| 214 | command.GetArgumentAtIndex(0)); |
| 215 | result.SetStatus (eReturnStatusFailed); |
| 216 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | return result.Succeeded(); |
| 221 | } |
| 222 | |
| 223 | int |
| 224 | CommandObjectHelp::HandleCompletion |
| 225 | ( |
| 226 | Args &input, |
| 227 | int &cursor_index, |
| 228 | int &cursor_char_position, |
| 229 | int match_start_point, |
| 230 | int max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 231 | bool &word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | StringList &matches |
| 233 | ) |
| 234 | { |
| 235 | // Return the completions of the commands in the help system: |
| 236 | if (cursor_index == 0) |
| 237 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 238 | return m_interpreter.HandleCompletionMatches (input, |
| 239 | cursor_index, |
| 240 | cursor_char_position, |
| 241 | match_start_point, |
| 242 | max_return_elements, |
| 243 | word_complete, |
| 244 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 245 | } |
| 246 | else |
| 247 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 248 | CommandObject *cmd_obj = m_interpreter.GetCommandObject (input.GetArgumentAtIndex(0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 249 | input.Shift(); |
| 250 | cursor_index--; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 251 | return cmd_obj->HandleCompletion (input, |
| 252 | cursor_index, |
| 253 | cursor_char_position, |
| 254 | match_start_point, |
| 255 | max_return_elements, |
| 256 | word_complete, |
| 257 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 258 | } |
| 259 | } |