Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectAlias.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 "CommandObjectAlias.h" |
| 11 | |
| 12 | |
| 13 | // C Includes |
| 14 | // C++ Includes |
| 15 | // Other libraries and framework includes |
| 16 | // Project includes |
| 17 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/Args.h" |
| 19 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 21 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 22 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 23 | |
| 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
| 27 | //------------------------------------------------------------------------- |
| 28 | // CommandObjectAlias |
| 29 | //------------------------------------------------------------------------- |
| 30 | |
| 31 | CommandObjectAlias::CommandObjectAlias () : |
| 32 | CommandObject ("alias", |
| 33 | "Allows users to define their own debugger command abbreviations.", |
| 34 | "alias <new_command> <old_command> [<options-for-aliased-command>]") |
| 35 | { |
| 36 | SetHelpLong( |
| 37 | "'alias' allows the user to create a short-cut or abbreviation for long \n\ |
| 38 | commands, multi-word commands, and commands that take particular options. \n\ |
| 39 | Below are some simple examples of how one might use the 'alias' command: \n\ |
| 40 | \n 'alias sc script' // Creates the abbreviation 'sc' for the 'script' \n\ |
| 41 | // command. \n\ |
| 42 | 'alias bp breakpoint' // Creates the abbreviation 'bp' for the 'breakpoint' \n\ |
| 43 | // command. Since breakpoint commands are two-word \n\ |
| 44 | // commands, the user will still need to enter the \n\ |
| 45 | // second word after 'bp', e.g. 'bp enable' or \n\ |
| 46 | // 'bp delete'. \n\ |
| 47 | 'alias bpi breakpoint list' // Creates the abbreviation 'bpi' for the \n\ |
| 48 | // two-word command 'breakpoint list'. \n\ |
| 49 | \nAn alias can include some options for the command, with the values either \n\ |
| 50 | filled in at the time the alias is created, or specified as positional \n\ |
| 51 | arguments, to be filled in when the alias is invoked. The following example \n\ |
| 52 | shows how to create aliases with options: \n\ |
| 53 | \n\ |
| 54 | 'alias bfl breakpoint set -f %1 -l %2' \n\ |
| 55 | \nThis creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \n\ |
| 56 | options already part of the alias. So if the user wants to set a breakpoint \n\ |
| 57 | by file and line without explicitly having to use the -f and -l options, the \n\ |
| 58 | user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \n\ |
| 59 | for the actual arguments that will be passed when the alias command is used. \n\ |
| 60 | The number in the placeholder refers to the position/order the actual value \n\ |
| 61 | occupies when the alias is used. So all the occurrences of '%1' in the alias \n\ |
| 62 | will be replaced with the first argument, all the occurrences of '%2' in the \n\ |
| 63 | alias will be replaced with the second argument, and so on. This also allows \n\ |
| 64 | actual arguments to be used multiple times within an alias (see 'process \n\ |
| 65 | launch' example below). So in the 'bfl' case, the actual file value will be \n\ |
| 66 | filled in with the first argument following 'bfl' and the actual line number \n\ |
| 67 | value will be filled in with the second argument. The user would use this \n\ |
| 68 | alias as follows: \n\ |
| 69 | \n (dbg) alias bfl breakpoint set -f %1 -l %2 \n\ |
| 70 | <... some time later ...> \n\ |
| 71 | (dbg) bfl my-file.c 137 \n\ |
| 72 | \nThis would be the same as if the user had entered \n\ |
| 73 | 'breakpoint set -f my-file.c -l 137'. \n\ |
| 74 | \nAnother example: \n\ |
| 75 | \n (dbg) alias pltty process launch -s -o %1 -e %1 \n\ |
| 76 | (dbg) pltty /dev/tty0 \n\ |
| 77 | // becomes 'process launch -s -o /dev/tty0 -e /dev/tty0' \n\ |
| 78 | \nIf the user always wanted to pass the same value to a particular option, the \n\ |
| 79 | alias could be defined with that value directly in the alias as a constant, \n\ |
| 80 | rather than using a positional placeholder: \n\ |
| 81 | \n alias bl3 breakpoint set -f %1 -l 3 // Always sets a breakpoint on line \n\ |
| 82 | // 3 of whatever file is indicated. \n"); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | CommandObjectAlias::~CommandObjectAlias () |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | |
| 91 | bool |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 92 | CommandObjectAlias::Execute |
| 93 | ( |
| 94 | CommandInterpreter &interpreter, |
| 95 | Args& args, |
| 96 | CommandReturnObject &result |
| 97 | ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | { |
| 99 | const int argc = args.GetArgumentCount(); |
| 100 | |
| 101 | if (argc < 2) |
| 102 | { |
| 103 | result.AppendError ("'alias' requires at least two arguments"); |
| 104 | result.SetStatus (eReturnStatusFailed); |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | const std::string alias_command = args.GetArgumentAtIndex(0); |
| 109 | const std::string actual_command = args.GetArgumentAtIndex(1); |
| 110 | |
| 111 | args.Shift(); // Shift the alias command word off the argument vector. |
| 112 | args.Shift(); // Shift the old command word off the argument vector. |
| 113 | |
| 114 | // Verify that the command is alias'able, and get the appropriate command object. |
| 115 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 116 | if (interpreter.CommandExists (alias_command.c_str())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | { |
| 118 | result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n", |
| 119 | alias_command.c_str()); |
| 120 | result.SetStatus (eReturnStatusFailed); |
| 121 | } |
| 122 | else |
| 123 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 124 | CommandObjectSP command_obj_sp(interpreter.GetCommandSPExact (actual_command.c_str(), true)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 125 | CommandObjectSP subcommand_obj_sp; |
| 126 | bool use_subcommand = false; |
| 127 | if (command_obj_sp.get()) |
| 128 | { |
| 129 | CommandObject *cmd_obj = command_obj_sp.get(); |
| 130 | CommandObject *sub_cmd_obj; |
| 131 | OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector); |
| 132 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 133 | |
| 134 | if (cmd_obj->IsMultiwordObject()) |
| 135 | { |
| 136 | if (argc >= 3) |
| 137 | { |
| 138 | const std::string sub_command = args.GetArgumentAtIndex(0); |
| 139 | assert (sub_command.length() != 0); |
| 140 | subcommand_obj_sp = |
| 141 | (((CommandObjectMultiword *) cmd_obj)->GetSubcommandSP (sub_command.c_str())); |
| 142 | if (subcommand_obj_sp.get()) |
| 143 | { |
| 144 | sub_cmd_obj = subcommand_obj_sp.get(); |
| 145 | use_subcommand = true; |
| 146 | args.Shift(); // Shift the sub_command word off the argument vector. |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | result.AppendErrorWithFormat ("Error occurred while attempting to look up command '%s %s'.\n", |
| 151 | alias_command.c_str(), sub_command.c_str()); |
| 152 | result.SetStatus (eReturnStatusFailed); |
| 153 | return false; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Verify & handle any options/arguments passed to the alias command |
| 159 | |
| 160 | if (args.GetArgumentCount () > 0) |
| 161 | { |
| 162 | if ((!use_subcommand && (cmd_obj->WantsRawCommandString())) |
| 163 | || (use_subcommand && (sub_cmd_obj->WantsRawCommandString()))) |
| 164 | { |
| 165 | result.AppendErrorWithFormat ("'%s' cannot be aliased with any options or arguments.\n", |
| 166 | (use_subcommand ? sub_cmd_obj->GetCommandName() |
| 167 | : cmd_obj->GetCommandName())); |
| 168 | result.SetStatus (eReturnStatusFailed); |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | // options or arguments have been passed to the alias command, and must be verified & processed here. |
| 173 | if ((!use_subcommand && (cmd_obj->GetOptions() != NULL)) |
| 174 | || (use_subcommand && (sub_cmd_obj->GetOptions() != NULL))) |
| 175 | { |
| 176 | Options *options; |
| 177 | if (use_subcommand) |
| 178 | options = sub_cmd_obj->GetOptions(); |
| 179 | else |
| 180 | options = cmd_obj->GetOptions(); |
| 181 | options->ResetOptionValues (); |
| 182 | args.Unshift ("dummy_arg"); |
| 183 | args.ParseAliasOptions (*options, result, option_arg_vector); |
| 184 | args.Shift (); |
| 185 | if (result.Succeeded()) |
| 186 | options->VerifyPartialOptions (result); |
| 187 | if (!result.Succeeded()) |
| 188 | return false; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | for (int i = 0; i < args.GetArgumentCount(); ++i) |
| 193 | option_arg_vector->push_back (OptionArgPair ("<argument>", |
| 194 | std::string (args.GetArgumentAtIndex (i)))); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Create the alias. |
| 199 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 200 | if (interpreter.AliasExists (alias_command.c_str()) |
| 201 | || interpreter.UserCommandExists (alias_command.c_str())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 202 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 203 | OptionArgVectorSP tmp_option_arg_sp (interpreter.GetAliasOptions (alias_command.c_str())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | if (tmp_option_arg_sp.get()) |
| 205 | { |
| 206 | if (option_arg_vector->size() == 0) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 207 | interpreter.RemoveAliasOptions (alias_command.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | } |
| 209 | result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n", alias_command.c_str()); |
| 210 | } |
| 211 | |
| 212 | if (use_subcommand) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 213 | interpreter.AddAlias (alias_command.c_str(), subcommand_obj_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | else |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 215 | interpreter.AddAlias (alias_command.c_str(), command_obj_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | if (option_arg_vector->size() > 0) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 217 | interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 218 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | result.AppendErrorWithFormat ("'%s' is not an existing command.\n", actual_command.c_str()); |
| 223 | result.SetStatus (eReturnStatusFailed); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return result.Succeeded(); |
| 228 | } |
| 229 | |