blob: 5c09a2aec0b002742a28d06b88edc79422d24bbb [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Ingham84cdc152010-06-15 19:49:27 +000018#include "lldb/Interpreter/Args.h"
19#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Interpreter/CommandInterpreter.h"
21#include "lldb/Interpreter/CommandReturnObject.h"
22#include "lldb/Interpreter/CommandInterpreter.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
27//-------------------------------------------------------------------------
28// CommandObjectAlias
29//-------------------------------------------------------------------------
30
31CommandObjectAlias::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\
38commands, multi-word commands, and commands that take particular options. \n\
39Below 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\
50filled in at the time the alias is created, or specified as positional \n\
51arguments, to be filled in when the alias is invoked. The following example \n\
52shows 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\
56options already part of the alias. So if the user wants to set a breakpoint \n\
57by file and line without explicitly having to use the -f and -l options, the \n\
58user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \n\
59for the actual arguments that will be passed when the alias command is used. \n\
60The number in the placeholder refers to the position/order the actual value \n\
61occupies when the alias is used. So all the occurrences of '%1' in the alias \n\
62will be replaced with the first argument, all the occurrences of '%2' in the \n\
63alias will be replaced with the second argument, and so on. This also allows \n\
64actual arguments to be used multiple times within an alias (see 'process \n\
65launch' example below). So in the 'bfl' case, the actual file value will be \n\
66filled in with the first argument following 'bfl' and the actual line number \n\
67value will be filled in with the second argument. The user would use this \n\
68alias 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\
79alias could be defined with that value directly in the alias as a constant, \n\
80rather 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
86CommandObjectAlias::~CommandObjectAlias ()
87{
88}
89
90
91bool
Greg Clayton63094e02010-06-23 01:19:29 +000092CommandObjectAlias::Execute
93(
94 CommandInterpreter &interpreter,
95 Args& args,
96 CommandReturnObject &result
97)
Chris Lattner24943d22010-06-08 16:52:24 +000098{
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 Clayton63094e02010-06-23 01:19:29 +0000116 if (interpreter.CommandExists (alias_command.c_str()))
Chris Lattner24943d22010-06-08 16:52:24 +0000117 {
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 Inghamd40f8a62010-07-06 22:46:59 +0000124 CommandObjectSP command_obj_sp(interpreter.GetCommandSPExact (actual_command.c_str(), true));
Chris Lattner24943d22010-06-08 16:52:24 +0000125 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 Clayton63094e02010-06-23 01:19:29 +0000200 if (interpreter.AliasExists (alias_command.c_str())
201 || interpreter.UserCommandExists (alias_command.c_str()))
Chris Lattner24943d22010-06-08 16:52:24 +0000202 {
Greg Clayton63094e02010-06-23 01:19:29 +0000203 OptionArgVectorSP tmp_option_arg_sp (interpreter.GetAliasOptions (alias_command.c_str()));
Chris Lattner24943d22010-06-08 16:52:24 +0000204 if (tmp_option_arg_sp.get())
205 {
206 if (option_arg_vector->size() == 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000207 interpreter.RemoveAliasOptions (alias_command.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000208 }
209 result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n", alias_command.c_str());
210 }
211
212 if (use_subcommand)
Greg Clayton63094e02010-06-23 01:19:29 +0000213 interpreter.AddAlias (alias_command.c_str(), subcommand_obj_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000214 else
Greg Clayton63094e02010-06-23 01:19:29 +0000215 interpreter.AddAlias (alias_command.c_str(), command_obj_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000216 if (option_arg_vector->size() > 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000217 interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000218 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