Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectMultiword.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 "lldb/Interpreter/CommandObjectMultiword.h" |
| 11 | // C Includes |
| 12 | // C++ Includes |
| 13 | // Other libraries and framework includes |
| 14 | // Project includes |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/CommandInterpreter.h" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
| 23 | //------------------------------------------------------------------------- |
| 24 | // CommandObjectMultiword |
| 25 | //------------------------------------------------------------------------- |
| 26 | |
| 27 | CommandObjectMultiword::CommandObjectMultiword |
| 28 | ( |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 29 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | const char *name, |
| 31 | const char *help, |
| 32 | const char *syntax, |
| 33 | uint32_t flags |
| 34 | ) : |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 35 | CommandObject (interpreter, name, help, syntax, flags) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | CommandObjectMultiword::~CommandObjectMultiword () |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | CommandObjectSP |
| 44 | CommandObjectMultiword::GetSubcommandSP (const char *sub_cmd, StringList *matches) |
| 45 | { |
| 46 | CommandObjectSP return_cmd_sp; |
| 47 | CommandObject::CommandMap::iterator pos; |
| 48 | |
| 49 | if (!m_subcommand_dict.empty()) |
| 50 | { |
| 51 | pos = m_subcommand_dict.find (sub_cmd); |
Johnny Chen | dc6c263 | 2010-12-01 01:04:22 +0000 | [diff] [blame] | 52 | if (pos != m_subcommand_dict.end()) { |
| 53 | // An exact match; append the sub_cmd to the 'matches' string list. |
| 54 | if (matches) |
| 55 | matches->AppendString(sub_cmd); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | return_cmd_sp = pos->second; |
Johnny Chen | dc6c263 | 2010-12-01 01:04:22 +0000 | [diff] [blame] | 57 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | else |
| 59 | { |
| 60 | |
| 61 | StringList local_matches; |
| 62 | if (matches == NULL) |
| 63 | matches = &local_matches; |
| 64 | int num_matches = CommandObject::AddNamesMatchingPartialString (m_subcommand_dict, sub_cmd, *matches); |
| 65 | |
| 66 | if (num_matches == 1) |
| 67 | { |
| 68 | // Cleaner, but slightly less efficient would be to call back into this function, since I now |
| 69 | // know I have an exact match... |
| 70 | |
| 71 | sub_cmd = matches->GetStringAtIndex(0); |
| 72 | pos = m_subcommand_dict.find(sub_cmd); |
| 73 | if (pos != m_subcommand_dict.end()) |
| 74 | return_cmd_sp = pos->second; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | return return_cmd_sp; |
| 79 | } |
| 80 | |
| 81 | CommandObject * |
| 82 | CommandObjectMultiword::GetSubcommandObject (const char *sub_cmd, StringList *matches) |
| 83 | { |
| 84 | return GetSubcommandSP(sub_cmd, matches).get(); |
| 85 | } |
| 86 | |
| 87 | bool |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 88 | CommandObjectMultiword::LoadSubCommand |
| 89 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 90 | const char *name, |
| 91 | const CommandObjectSP& cmd_obj |
| 92 | ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | { |
| 94 | CommandMap::iterator pos; |
| 95 | bool success = true; |
| 96 | |
| 97 | pos = m_subcommand_dict.find(name); |
| 98 | if (pos == m_subcommand_dict.end()) |
| 99 | { |
| 100 | m_subcommand_dict[name] = cmd_obj; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 101 | m_interpreter.CrossRegisterCommand (name, GetCommandName()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | } |
| 103 | else |
| 104 | success = false; |
| 105 | |
| 106 | return success; |
| 107 | } |
| 108 | |
| 109 | bool |
| 110 | CommandObjectMultiword::Execute |
| 111 | ( |
| 112 | Args& args, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | CommandReturnObject &result |
| 114 | ) |
| 115 | { |
| 116 | const size_t argc = args.GetArgumentCount(); |
| 117 | if (argc == 0) |
| 118 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 119 | GenerateHelpText (result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | } |
| 121 | else |
| 122 | { |
| 123 | const char *sub_command = args.GetArgumentAtIndex (0); |
| 124 | |
| 125 | if (sub_command) |
| 126 | { |
| 127 | if (::strcasecmp (sub_command, "help") == 0) |
| 128 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 129 | GenerateHelpText (result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 130 | } |
| 131 | else if (!m_subcommand_dict.empty()) |
| 132 | { |
| 133 | StringList matches; |
| 134 | CommandObject *sub_cmd_obj = GetSubcommandObject(sub_command, &matches); |
| 135 | if (sub_cmd_obj != NULL) |
| 136 | { |
| 137 | // Now call CommandObject::Execute to process and options in 'rest_of_line'. From there |
| 138 | // the command-specific version of Execute will be called, with the processed arguments. |
| 139 | |
| 140 | args.Shift(); |
| 141 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 142 | sub_cmd_obj->ExecuteWithOptions (args, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | } |
| 144 | else |
| 145 | { |
| 146 | std::string error_msg; |
| 147 | int num_subcmd_matches = matches.GetSize(); |
| 148 | if (num_subcmd_matches > 0) |
| 149 | error_msg.assign ("ambiguous command "); |
| 150 | else |
| 151 | error_msg.assign ("invalid command "); |
| 152 | |
| 153 | error_msg.append ("'"); |
| 154 | error_msg.append (GetCommandName()); |
| 155 | error_msg.append (" "); |
| 156 | error_msg.append (sub_command); |
| 157 | error_msg.append ("'"); |
| 158 | |
| 159 | if (num_subcmd_matches > 0) |
| 160 | { |
| 161 | error_msg.append (" Possible completions:"); |
| 162 | for (int i = 0; i < num_subcmd_matches; i++) |
| 163 | { |
| 164 | error_msg.append ("\n\t"); |
| 165 | error_msg.append (matches.GetStringAtIndex (i)); |
| 166 | } |
| 167 | } |
| 168 | error_msg.append ("\n"); |
| 169 | result.AppendRawError (error_msg.c_str(), error_msg.size()); |
| 170 | result.SetStatus (eReturnStatusFailed); |
| 171 | } |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | result.AppendErrorWithFormat ("'%s' does not have any subcommands.\n", GetCommandName()); |
| 176 | result.SetStatus (eReturnStatusFailed); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return result.Succeeded(); |
| 182 | } |
| 183 | |
| 184 | void |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 185 | CommandObjectMultiword::GenerateHelpText (CommandReturnObject &result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 186 | { |
| 187 | // First time through here, generate the help text for the object and |
| 188 | // push it to the return result object as well |
| 189 | |
Jim Ingham | 2e8cb8a | 2011-02-19 02:53:09 +0000 | [diff] [blame^] | 190 | Stream &output_stream = result.GetOutputStream(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 191 | output_stream.PutCString ("The following subcommands are supported:\n\n"); |
| 192 | |
| 193 | CommandMap::iterator pos; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 194 | uint32_t max_len = m_interpreter.FindLongestCommandWord (m_subcommand_dict); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 195 | |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 196 | if (max_len) |
| 197 | max_len += 4; // Indent the output by 4 spaces. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 198 | |
| 199 | for (pos = m_subcommand_dict.begin(); pos != m_subcommand_dict.end(); ++pos) |
| 200 | { |
| 201 | std::string indented_command (" "); |
| 202 | indented_command.append (pos->first); |
Caroline Tice | fb35511 | 2010-10-01 17:46:38 +0000 | [diff] [blame] | 203 | if (pos->second->WantsRawCommandString ()) |
| 204 | { |
| 205 | std::string help_text (pos->second->GetHelp()); |
| 206 | help_text.append (" This command takes 'raw' input (no need to quote stuff)."); |
| 207 | m_interpreter.OutputFormattedHelpText (result.GetOutputStream(), |
| 208 | indented_command.c_str(), |
| 209 | "--", |
| 210 | help_text.c_str(), |
| 211 | max_len); |
| 212 | } |
| 213 | else |
| 214 | m_interpreter.OutputFormattedHelpText (result.GetOutputStream(), |
| 215 | indented_command.c_str(), |
| 216 | "--", |
| 217 | pos->second->GetHelp(), |
| 218 | max_len); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | output_stream.PutCString ("\nFor more help on any particular subcommand, type 'help <command> <subcommand>'.\n"); |
| 222 | |
| 223 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 224 | } |
| 225 | |
| 226 | int |
| 227 | CommandObjectMultiword::HandleCompletion |
| 228 | ( |
| 229 | Args &input, |
| 230 | int &cursor_index, |
| 231 | int &cursor_char_position, |
| 232 | int match_start_point, |
| 233 | int max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 234 | bool &word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 235 | StringList &matches |
| 236 | ) |
| 237 | { |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 238 | // Any of the command matches will provide a complete word, otherwise the individual |
| 239 | // completers will override this. |
| 240 | word_complete = true; |
| 241 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | if (cursor_index == 0) |
| 243 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 244 | CommandObject::AddNamesMatchingPartialString (m_subcommand_dict, |
| 245 | input.GetArgumentAtIndex(0), |
| 246 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 247 | |
| 248 | if (matches.GetSize() == 1 |
| 249 | && matches.GetStringAtIndex(0) != NULL |
| 250 | && strcmp (input.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0) |
| 251 | { |
| 252 | StringList temp_matches; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 253 | CommandObject *cmd_obj = GetSubcommandObject (input.GetArgumentAtIndex(0), |
| 254 | &temp_matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 255 | if (cmd_obj != NULL) |
| 256 | { |
| 257 | matches.DeleteStringAtIndex (0); |
| 258 | input.Shift(); |
| 259 | cursor_char_position = 0; |
| 260 | input.AppendArgument (""); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 261 | return cmd_obj->HandleCompletion (input, |
| 262 | cursor_index, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 263 | cursor_char_position, |
| 264 | match_start_point, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 265 | max_return_elements, |
| 266 | word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 267 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 268 | } |
| 269 | else |
| 270 | return matches.GetSize(); |
| 271 | } |
| 272 | else |
| 273 | return matches.GetSize(); |
| 274 | } |
| 275 | else |
| 276 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 277 | CommandObject *sub_command_object = GetSubcommandObject (input.GetArgumentAtIndex(0), |
| 278 | &matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 279 | if (sub_command_object == NULL) |
| 280 | { |
| 281 | return matches.GetSize(); |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | // Remove the one match that we got from calling GetSubcommandObject. |
| 286 | matches.DeleteStringAtIndex(0); |
| 287 | input.Shift(); |
| 288 | cursor_index--; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 289 | return sub_command_object->HandleCompletion (input, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 290 | cursor_index, |
| 291 | cursor_char_position, |
| 292 | match_start_point, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 293 | max_return_elements, |
| 294 | word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 295 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | } |
| 299 | } |
| 300 | |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 301 | const char * |
| 302 | CommandObjectMultiword::GetRepeatCommand (Args ¤t_command_args, uint32_t index) |
| 303 | { |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 304 | index++; |
Greg Clayton | 578ddbb | 2010-07-20 22:54:09 +0000 | [diff] [blame] | 305 | if (current_command_args.GetArgumentCount() <= index) |
| 306 | return NULL; |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 307 | CommandObject *sub_command_object = GetSubcommandObject (current_command_args.GetArgumentAtIndex(index)); |
| 308 | if (sub_command_object == NULL) |
| 309 | return NULL; |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 310 | return sub_command_object->GetRepeatCommand(current_command_args, index); |
| 311 | } |
| 312 | |