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