CommandObjectCommands.cpp revision c1ad82eb979ee856b86aec6e2acb7bddf75f7c4f
1767af88aa617288e584afcfed055f7755e408542Jim Ingham//===-- CommandObjectSource.cpp ---------------------------------*- C++ -*-===//
2767af88aa617288e584afcfed055f7755e408542Jim Ingham//
3767af88aa617288e584afcfed055f7755e408542Jim Ingham//                     The LLVM Compiler Infrastructure
4767af88aa617288e584afcfed055f7755e408542Jim Ingham//
5767af88aa617288e584afcfed055f7755e408542Jim Ingham// This file is distributed under the University of Illinois Open Source
6767af88aa617288e584afcfed055f7755e408542Jim Ingham// License. See LICENSE.TXT for details.
7767af88aa617288e584afcfed055f7755e408542Jim Ingham//
8767af88aa617288e584afcfed055f7755e408542Jim Ingham//===----------------------------------------------------------------------===//
9767af88aa617288e584afcfed055f7755e408542Jim Ingham
10767af88aa617288e584afcfed055f7755e408542Jim Ingham#include "CommandObjectCommands.h"
11767af88aa617288e584afcfed055f7755e408542Jim Ingham
12767af88aa617288e584afcfed055f7755e408542Jim Ingham// C Includes
13767af88aa617288e584afcfed055f7755e408542Jim Ingham// C++ Includes
14767af88aa617288e584afcfed055f7755e408542Jim Ingham// Other libraries and framework includes
15767af88aa617288e584afcfed055f7755e408542Jim Ingham// Project includes
16767af88aa617288e584afcfed055f7755e408542Jim Ingham#include "lldb/Interpreter/Args.h"
17767af88aa617288e584afcfed055f7755e408542Jim Ingham#include "lldb/Core/Debugger.h"
18767af88aa617288e584afcfed055f7755e408542Jim Ingham#include "lldb/Interpreter/CommandInterpreter.h"
19767af88aa617288e584afcfed055f7755e408542Jim Ingham#include "lldb/Interpreter/CommandReturnObject.h"
20767af88aa617288e584afcfed055f7755e408542Jim Ingham#include "lldb/Interpreter/Options.h"
21767af88aa617288e584afcfed055f7755e408542Jim Ingham
22767af88aa617288e584afcfed055f7755e408542Jim Inghamusing namespace lldb;
23767af88aa617288e584afcfed055f7755e408542Jim Inghamusing namespace lldb_private;
24767af88aa617288e584afcfed055f7755e408542Jim Ingham
25767af88aa617288e584afcfed055f7755e408542Jim Inghamconst char *k_space_characters = "\t\n\v\f\r ";
26767af88aa617288e584afcfed055f7755e408542Jim Ingham
27767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
28767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectCommandsSource
29767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
30767af88aa617288e584afcfed055f7755e408542Jim Ingham
31767af88aa617288e584afcfed055f7755e408542Jim Inghamclass CommandObjectCommandsSource : public CommandObject
32767af88aa617288e584afcfed055f7755e408542Jim Ingham{
33767af88aa617288e584afcfed055f7755e408542Jim Inghampublic:
34767af88aa617288e584afcfed055f7755e408542Jim Ingham    CommandObjectCommandsSource() :
35767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandObject ("commands source",
36767af88aa617288e584afcfed055f7755e408542Jim Ingham                   "Reads in debugger commands from the file <filename> and executes them.",
37767af88aa617288e584afcfed055f7755e408542Jim Ingham                   "command source <filename>")
38767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
39767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
40767af88aa617288e584afcfed055f7755e408542Jim Ingham
41767af88aa617288e584afcfed055f7755e408542Jim Ingham    ~CommandObjectCommandsSource ()
42767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
43767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
44767af88aa617288e584afcfed055f7755e408542Jim Ingham
45767af88aa617288e584afcfed055f7755e408542Jim Ingham    bool
46767af88aa617288e584afcfed055f7755e408542Jim Ingham    Execute
47767af88aa617288e584afcfed055f7755e408542Jim Ingham    (
48767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandInterpreter &interpreter,
49767af88aa617288e584afcfed055f7755e408542Jim Ingham        Args& args,
50767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandReturnObject &result
51767af88aa617288e584afcfed055f7755e408542Jim Ingham    )
52767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
53767af88aa617288e584afcfed055f7755e408542Jim Ingham        const int argc = args.GetArgumentCount();
54767af88aa617288e584afcfed055f7755e408542Jim Ingham        if (argc == 1)
55767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
56767af88aa617288e584afcfed055f7755e408542Jim Ingham            const char *filename = args.GetArgumentAtIndex(0);
57767af88aa617288e584afcfed055f7755e408542Jim Ingham            bool success = true;
58767af88aa617288e584afcfed055f7755e408542Jim Ingham
59767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendMessageWithFormat ("Executing commands in '%s'.\n", filename);
60767af88aa617288e584afcfed055f7755e408542Jim Ingham
61767af88aa617288e584afcfed055f7755e408542Jim Ingham            FileSpec cmd_file (filename);
62767af88aa617288e584afcfed055f7755e408542Jim Ingham            if (cmd_file.Exists())
63767af88aa617288e584afcfed055f7755e408542Jim Ingham            {
64767af88aa617288e584afcfed055f7755e408542Jim Ingham                STLStringArray commands;
65767af88aa617288e584afcfed055f7755e408542Jim Ingham                success = cmd_file.ReadFileLines (commands);
66767af88aa617288e584afcfed055f7755e408542Jim Ingham
67767af88aa617288e584afcfed055f7755e408542Jim Ingham                STLStringArray::iterator pos = commands.begin();
68767af88aa617288e584afcfed055f7755e408542Jim Ingham
69767af88aa617288e584afcfed055f7755e408542Jim Ingham                // Trim out any empty lines or lines that start with the comment
70767af88aa617288e584afcfed055f7755e408542Jim Ingham                // char '#'
71767af88aa617288e584afcfed055f7755e408542Jim Ingham                while (pos != commands.end())
72767af88aa617288e584afcfed055f7755e408542Jim Ingham                {
73767af88aa617288e584afcfed055f7755e408542Jim Ingham                    bool remove_string = false;
74767af88aa617288e584afcfed055f7755e408542Jim Ingham                    size_t non_space = pos->find_first_not_of (k_space_characters);
75767af88aa617288e584afcfed055f7755e408542Jim Ingham                    if (non_space == std::string::npos)
76767af88aa617288e584afcfed055f7755e408542Jim Ingham                        remove_string = true; // Empty line
77767af88aa617288e584afcfed055f7755e408542Jim Ingham                    else if ((*pos)[non_space] == '#')
78767af88aa617288e584afcfed055f7755e408542Jim Ingham                        remove_string = true; // Comment line that starts with '#'
79767af88aa617288e584afcfed055f7755e408542Jim Ingham
80767af88aa617288e584afcfed055f7755e408542Jim Ingham                    if (remove_string)
81767af88aa617288e584afcfed055f7755e408542Jim Ingham                        pos = commands.erase(pos);
82767af88aa617288e584afcfed055f7755e408542Jim Ingham                    else
83767af88aa617288e584afcfed055f7755e408542Jim Ingham                        ++pos;
84767af88aa617288e584afcfed055f7755e408542Jim Ingham                }
85767af88aa617288e584afcfed055f7755e408542Jim Ingham
86767af88aa617288e584afcfed055f7755e408542Jim Ingham                if (commands.size() > 0)
87767af88aa617288e584afcfed055f7755e408542Jim Ingham                {
88767af88aa617288e584afcfed055f7755e408542Jim Ingham                    const size_t num_commands = commands.size();
89767af88aa617288e584afcfed055f7755e408542Jim Ingham                    size_t i;
90767af88aa617288e584afcfed055f7755e408542Jim Ingham                    for (i = 0; i<num_commands; ++i)
91767af88aa617288e584afcfed055f7755e408542Jim Ingham                    {
92767af88aa617288e584afcfed055f7755e408542Jim Ingham                        result.GetOutputStream().Printf("%s %s\n", interpreter.GetPrompt(), commands[i].c_str());
93767af88aa617288e584afcfed055f7755e408542Jim Ingham                        if (!interpreter.HandleCommand(commands[i].c_str(), false, result))
94767af88aa617288e584afcfed055f7755e408542Jim Ingham                            break;
95767af88aa617288e584afcfed055f7755e408542Jim Ingham                    }
96767af88aa617288e584afcfed055f7755e408542Jim Ingham
97767af88aa617288e584afcfed055f7755e408542Jim Ingham                    if (i < num_commands)
98767af88aa617288e584afcfed055f7755e408542Jim Ingham                    {
99767af88aa617288e584afcfed055f7755e408542Jim Ingham                        result.AppendErrorWithFormat("Aborting source of '%s' after command '%s' failed.\n",
100767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                     filename, commands[i].c_str());
101767af88aa617288e584afcfed055f7755e408542Jim Ingham                        result.SetStatus (eReturnStatusSuccessFinishResult);
102767af88aa617288e584afcfed055f7755e408542Jim Ingham                    }
103767af88aa617288e584afcfed055f7755e408542Jim Ingham                    else
104767af88aa617288e584afcfed055f7755e408542Jim Ingham                    {
105767af88aa617288e584afcfed055f7755e408542Jim Ingham                        success = true;
106767af88aa617288e584afcfed055f7755e408542Jim Ingham                        result.SetStatus (eReturnStatusFailed);
107767af88aa617288e584afcfed055f7755e408542Jim Ingham                    }
108767af88aa617288e584afcfed055f7755e408542Jim Ingham                }
109767af88aa617288e584afcfed055f7755e408542Jim Ingham            }
110767af88aa617288e584afcfed055f7755e408542Jim Ingham            else
111767af88aa617288e584afcfed055f7755e408542Jim Ingham            {
112767af88aa617288e584afcfed055f7755e408542Jim Ingham                result.AppendErrorWithFormat ("File '%s' does not exist.\n", filename);
113767af88aa617288e584afcfed055f7755e408542Jim Ingham                result.SetStatus (eReturnStatusFailed);
114767af88aa617288e584afcfed055f7755e408542Jim Ingham                success = false;
115767af88aa617288e584afcfed055f7755e408542Jim Ingham            }
116767af88aa617288e584afcfed055f7755e408542Jim Ingham
117767af88aa617288e584afcfed055f7755e408542Jim Ingham            if (success)
118767af88aa617288e584afcfed055f7755e408542Jim Ingham            {
119767af88aa617288e584afcfed055f7755e408542Jim Ingham                result.SetStatus (eReturnStatusSuccessFinishNoResult);
120767af88aa617288e584afcfed055f7755e408542Jim Ingham            }
121767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
122767af88aa617288e584afcfed055f7755e408542Jim Ingham        else
123767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
124767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendErrorWithFormat("'%s' takes exactly one executable filename argument.\n", GetCommandName());
125767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
126767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
127767af88aa617288e584afcfed055f7755e408542Jim Ingham        return result.Succeeded();
128767af88aa617288e584afcfed055f7755e408542Jim Ingham
129767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
130767af88aa617288e584afcfed055f7755e408542Jim Ingham};
131767af88aa617288e584afcfed055f7755e408542Jim Ingham
132767af88aa617288e584afcfed055f7755e408542Jim Ingham#pragma mark CommandObjectCommandsAlias
133767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
134767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectCommandsAlias
135767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
136767af88aa617288e584afcfed055f7755e408542Jim Ingham
137767af88aa617288e584afcfed055f7755e408542Jim Inghamclass CommandObjectCommandsAlias : public CommandObject
138767af88aa617288e584afcfed055f7755e408542Jim Ingham{
139767af88aa617288e584afcfed055f7755e408542Jim Inghampublic:
140767af88aa617288e584afcfed055f7755e408542Jim Ingham    CommandObjectCommandsAlias () :
141767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandObject ("commands alias",
142767af88aa617288e584afcfed055f7755e408542Jim Ingham                         "Allows users to define their own debugger command abbreviations.",
143a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan                         "commands alias <new_command> <old_command> [<options-for-aliased-command>]")
144767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
145767af88aa617288e584afcfed055f7755e408542Jim Ingham        SetHelpLong(
146767af88aa617288e584afcfed055f7755e408542Jim Ingham    "'alias' allows the user to create a short-cut or abbreviation for long \n\
147767af88aa617288e584afcfed055f7755e408542Jim Ingham    commands, multi-word commands, and commands that take particular options. \n\
148767af88aa617288e584afcfed055f7755e408542Jim Ingham    Below are some simple examples of how one might use the 'alias' command: \n\
149a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    \n    'command alias sc script'           // Creates the abbreviation 'sc' for the 'script' \n\
150a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan                                        // command. \n\
151a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    'command alias bp breakpoint'       // Creates the abbreviation 'bp' for the 'breakpoint' \n\
152a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan                                        // command.  Since breakpoint commands are two-word \n\
153a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan                                        // commands, the user will still need to enter the \n\
154a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan                                        // second word after 'bp', e.g. 'bp enable' or \n\
155a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan                                        // 'bp delete'. \n\
156a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    'command alias bpi breakpoint list' // Creates the abbreviation 'bpi' for the \n\
157a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan                                        // two-word command 'breakpoint list'. \n\
158767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nAn alias can include some options for the command, with the values either \n\
159767af88aa617288e584afcfed055f7755e408542Jim Ingham    filled in at the time the alias is created, or specified as positional \n\
160767af88aa617288e584afcfed055f7755e408542Jim Ingham    arguments, to be filled in when the alias is invoked.  The following example \n\
161767af88aa617288e584afcfed055f7755e408542Jim Ingham    shows how to create aliases with options: \n\
162767af88aa617288e584afcfed055f7755e408542Jim Ingham    \n\
163a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    'command alias bfl breakpoint set -f %1 -l %2' \n\
164767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nThis creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \n\
165767af88aa617288e584afcfed055f7755e408542Jim Ingham    options already part of the alias.  So if the user wants to set a breakpoint \n\
166767af88aa617288e584afcfed055f7755e408542Jim Ingham    by file and line without explicitly having to use the -f and -l options, the \n\
167767af88aa617288e584afcfed055f7755e408542Jim Ingham    user can now use 'bfl' instead.  The '%1' and '%2' are positional placeholders \n\
168767af88aa617288e584afcfed055f7755e408542Jim Ingham    for the actual arguments that will be passed when the alias command is used. \n\
169767af88aa617288e584afcfed055f7755e408542Jim Ingham    The number in the placeholder refers to the position/order the actual value \n\
170767af88aa617288e584afcfed055f7755e408542Jim Ingham    occupies when the alias is used.  So all the occurrences of '%1' in the alias \n\
171767af88aa617288e584afcfed055f7755e408542Jim Ingham    will be replaced with the first argument, all the occurrences of '%2' in the \n\
172767af88aa617288e584afcfed055f7755e408542Jim Ingham    alias will be replaced with the second argument, and so on.  This also allows \n\
173767af88aa617288e584afcfed055f7755e408542Jim Ingham    actual arguments to be used multiple times within an alias (see 'process \n\
174767af88aa617288e584afcfed055f7755e408542Jim Ingham    launch' example below).  So in the 'bfl' case, the actual file value will be \n\
175767af88aa617288e584afcfed055f7755e408542Jim Ingham    filled in with the first argument following 'bfl' and the actual line number \n\
176767af88aa617288e584afcfed055f7755e408542Jim Ingham    value will be filled in with the second argument.  The user would use this \n\
177767af88aa617288e584afcfed055f7755e408542Jim Ingham    alias as follows: \n\
178a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    \n    (dbg)  commands alias bfl breakpoint set -f %1 -l %2 \n\
179a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    <... some time later ...> \n\
180a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    (dbg)  bfl my-file.c 137 \n\
181767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nThis would be the same as if the user had entered \n\
182767af88aa617288e584afcfed055f7755e408542Jim Ingham    'breakpoint set -f my-file.c -l 137'. \n\
183767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nAnother example: \n\
184a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    \n    (dbg)  commands alias pltty  process launch -s -o %1 -e %1 \n\
185a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    (dbg)  pltty /dev/tty0 \n\
186a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan           // becomes 'process launch -s -o /dev/tty0 -e /dev/tty0' \n\
187767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nIf the user always wanted to pass the same value to a particular option, the \n\
188767af88aa617288e584afcfed055f7755e408542Jim Ingham    alias could be defined with that value directly in the alias as a constant, \n\
189767af88aa617288e584afcfed055f7755e408542Jim Ingham    rather than using a positional placeholder: \n\
190a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    \n    commands alias bl3  breakpoint set -f %1 -l 3  // Always sets a breakpoint on line \n\
191a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan                                                   // 3 of whatever file is indicated. \n");
192767af88aa617288e584afcfed055f7755e408542Jim Ingham
193767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
194767af88aa617288e584afcfed055f7755e408542Jim Ingham
195767af88aa617288e584afcfed055f7755e408542Jim Ingham    ~CommandObjectCommandsAlias ()
196767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
197767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
198767af88aa617288e584afcfed055f7755e408542Jim Ingham
199767af88aa617288e584afcfed055f7755e408542Jim Ingham
200767af88aa617288e584afcfed055f7755e408542Jim Ingham    bool
201767af88aa617288e584afcfed055f7755e408542Jim Ingham    Execute
202767af88aa617288e584afcfed055f7755e408542Jim Ingham    (
203767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandInterpreter &interpreter,
204767af88aa617288e584afcfed055f7755e408542Jim Ingham        Args& args,
205767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandReturnObject &result
206767af88aa617288e584afcfed055f7755e408542Jim Ingham    )
207767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
20854e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton        const size_t argc = args.GetArgumentCount();
209767af88aa617288e584afcfed055f7755e408542Jim Ingham
210767af88aa617288e584afcfed055f7755e408542Jim Ingham        if (argc < 2)
21154e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton        {
212767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendError ("'alias' requires at least two arguments");
213767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
214767af88aa617288e584afcfed055f7755e408542Jim Ingham            return false;
21554e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton        }
216767af88aa617288e584afcfed055f7755e408542Jim Ingham
217767af88aa617288e584afcfed055f7755e408542Jim Ingham        const std::string alias_command = args.GetArgumentAtIndex(0);
218767af88aa617288e584afcfed055f7755e408542Jim Ingham        const std::string actual_command = args.GetArgumentAtIndex(1);
219767af88aa617288e584afcfed055f7755e408542Jim Ingham
220767af88aa617288e584afcfed055f7755e408542Jim Ingham        args.Shift();  // Shift the alias command word off the argument vector.
221767af88aa617288e584afcfed055f7755e408542Jim Ingham        args.Shift();  // Shift the old command word off the argument vector.
222767af88aa617288e584afcfed055f7755e408542Jim Ingham
223767af88aa617288e584afcfed055f7755e408542Jim Ingham        // Verify that the command is alias'able, and get the appropriate command object.
224767af88aa617288e584afcfed055f7755e408542Jim Ingham
225767af88aa617288e584afcfed055f7755e408542Jim Ingham        if (interpreter.CommandExists (alias_command.c_str()))
226767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
227767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
228767af88aa617288e584afcfed055f7755e408542Jim Ingham                                         alias_command.c_str());
229767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
230767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
231767af88aa617288e584afcfed055f7755e408542Jim Ingham        else
232767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
233767af88aa617288e584afcfed055f7755e408542Jim Ingham             CommandObjectSP command_obj_sp(interpreter.GetCommandSPExact (actual_command.c_str(), true));
234767af88aa617288e584afcfed055f7755e408542Jim Ingham             CommandObjectSP subcommand_obj_sp;
235767af88aa617288e584afcfed055f7755e408542Jim Ingham             bool use_subcommand = false;
236767af88aa617288e584afcfed055f7755e408542Jim Ingham             if (command_obj_sp.get())
237767af88aa617288e584afcfed055f7755e408542Jim Ingham             {
238767af88aa617288e584afcfed055f7755e408542Jim Ingham                 CommandObject *cmd_obj = command_obj_sp.get();
23954e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton                 CommandObject *sub_cmd_obj = NULL;
240767af88aa617288e584afcfed055f7755e408542Jim Ingham                 OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector);
241767af88aa617288e584afcfed055f7755e408542Jim Ingham                 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
242767af88aa617288e584afcfed055f7755e408542Jim Ingham
243767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (cmd_obj->IsMultiwordObject())
244767af88aa617288e584afcfed055f7755e408542Jim Ingham                 {
245767af88aa617288e584afcfed055f7755e408542Jim Ingham                     if (argc >= 3)
246767af88aa617288e584afcfed055f7755e408542Jim Ingham                     {
247767af88aa617288e584afcfed055f7755e408542Jim Ingham                         const std::string sub_command = args.GetArgumentAtIndex(0);
248767af88aa617288e584afcfed055f7755e408542Jim Ingham                         assert (sub_command.length() != 0);
249767af88aa617288e584afcfed055f7755e408542Jim Ingham                         subcommand_obj_sp =
250767af88aa617288e584afcfed055f7755e408542Jim Ingham                                           (((CommandObjectMultiword *) cmd_obj)->GetSubcommandSP (sub_command.c_str()));
251767af88aa617288e584afcfed055f7755e408542Jim Ingham                         if (subcommand_obj_sp.get())
252767af88aa617288e584afcfed055f7755e408542Jim Ingham                         {
253767af88aa617288e584afcfed055f7755e408542Jim Ingham                             sub_cmd_obj = subcommand_obj_sp.get();
254767af88aa617288e584afcfed055f7755e408542Jim Ingham                             use_subcommand = true;
255767af88aa617288e584afcfed055f7755e408542Jim Ingham                             args.Shift();  // Shift the sub_command word off the argument vector.
256767af88aa617288e584afcfed055f7755e408542Jim Ingham                         }
257767af88aa617288e584afcfed055f7755e408542Jim Ingham                         else
258767af88aa617288e584afcfed055f7755e408542Jim Ingham                         {
259767af88aa617288e584afcfed055f7755e408542Jim Ingham                             result.AppendErrorWithFormat ("Error occurred while attempting to look up command '%s %s'.\n",
260767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                          alias_command.c_str(), sub_command.c_str());
261767af88aa617288e584afcfed055f7755e408542Jim Ingham                             result.SetStatus (eReturnStatusFailed);
262767af88aa617288e584afcfed055f7755e408542Jim Ingham                             return false;
263767af88aa617288e584afcfed055f7755e408542Jim Ingham                         }
264767af88aa617288e584afcfed055f7755e408542Jim Ingham                     }
265767af88aa617288e584afcfed055f7755e408542Jim Ingham                 }
266767af88aa617288e584afcfed055f7755e408542Jim Ingham
267767af88aa617288e584afcfed055f7755e408542Jim Ingham                 // Verify & handle any options/arguments passed to the alias command
268767af88aa617288e584afcfed055f7755e408542Jim Ingham
269767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (args.GetArgumentCount () > 0)
270767af88aa617288e584afcfed055f7755e408542Jim Ingham                 {
271767af88aa617288e584afcfed055f7755e408542Jim Ingham                     if ((!use_subcommand && (cmd_obj->WantsRawCommandString()))
272767af88aa617288e584afcfed055f7755e408542Jim Ingham                         || (use_subcommand && (sub_cmd_obj->WantsRawCommandString())))
273767af88aa617288e584afcfed055f7755e408542Jim Ingham                     {
274767af88aa617288e584afcfed055f7755e408542Jim Ingham                         result.AppendErrorWithFormat ("'%s' cannot be aliased with any options or arguments.\n",
275767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                      (use_subcommand ? sub_cmd_obj->GetCommandName()
276767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                                      : cmd_obj->GetCommandName()));
277767af88aa617288e584afcfed055f7755e408542Jim Ingham                         result.SetStatus (eReturnStatusFailed);
278767af88aa617288e584afcfed055f7755e408542Jim Ingham                         return false;
279767af88aa617288e584afcfed055f7755e408542Jim Ingham                     }
280767af88aa617288e584afcfed055f7755e408542Jim Ingham
281767af88aa617288e584afcfed055f7755e408542Jim Ingham                     // options or arguments have been passed to the alias command, and must be
282767af88aa617288e584afcfed055f7755e408542Jim Ingham                     // verified & processed here.
283767af88aa617288e584afcfed055f7755e408542Jim Ingham                     if ((!use_subcommand && (cmd_obj->GetOptions() != NULL))
284767af88aa617288e584afcfed055f7755e408542Jim Ingham                         || (use_subcommand && (sub_cmd_obj->GetOptions() != NULL)))
285767af88aa617288e584afcfed055f7755e408542Jim Ingham                     {
286767af88aa617288e584afcfed055f7755e408542Jim Ingham                         Options *options;
287767af88aa617288e584afcfed055f7755e408542Jim Ingham                         if (use_subcommand)
288767af88aa617288e584afcfed055f7755e408542Jim Ingham                             options = sub_cmd_obj->GetOptions();
289767af88aa617288e584afcfed055f7755e408542Jim Ingham                         else
290767af88aa617288e584afcfed055f7755e408542Jim Ingham                             options = cmd_obj->GetOptions();
291767af88aa617288e584afcfed055f7755e408542Jim Ingham                         options->ResetOptionValues ();
292767af88aa617288e584afcfed055f7755e408542Jim Ingham                         args.Unshift ("dummy_arg");
293767af88aa617288e584afcfed055f7755e408542Jim Ingham                         args.ParseAliasOptions (*options, result, option_arg_vector);
294767af88aa617288e584afcfed055f7755e408542Jim Ingham                         args.Shift ();
295767af88aa617288e584afcfed055f7755e408542Jim Ingham                         if (result.Succeeded())
296767af88aa617288e584afcfed055f7755e408542Jim Ingham                             options->VerifyPartialOptions (result);
297767af88aa617288e584afcfed055f7755e408542Jim Ingham                         if (!result.Succeeded())
298767af88aa617288e584afcfed055f7755e408542Jim Ingham                             return false;
299767af88aa617288e584afcfed055f7755e408542Jim Ingham                     }
300767af88aa617288e584afcfed055f7755e408542Jim Ingham                     else
301767af88aa617288e584afcfed055f7755e408542Jim Ingham                     {
30254e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton                         for (size_t i = 0; i < argc; ++i)
303767af88aa617288e584afcfed055f7755e408542Jim Ingham                             option_arg_vector->push_back (OptionArgPair ("<argument>",
304767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                                          std::string (args.GetArgumentAtIndex (i))));
305767af88aa617288e584afcfed055f7755e408542Jim Ingham                     }
306767af88aa617288e584afcfed055f7755e408542Jim Ingham                 }
307767af88aa617288e584afcfed055f7755e408542Jim Ingham
308767af88aa617288e584afcfed055f7755e408542Jim Ingham                 // Create the alias.
309767af88aa617288e584afcfed055f7755e408542Jim Ingham
310767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (interpreter.AliasExists (alias_command.c_str())
311767af88aa617288e584afcfed055f7755e408542Jim Ingham                     || interpreter.UserCommandExists (alias_command.c_str()))
312767af88aa617288e584afcfed055f7755e408542Jim Ingham                 {
313767af88aa617288e584afcfed055f7755e408542Jim Ingham                     OptionArgVectorSP tmp_option_arg_sp (interpreter.GetAliasOptions (alias_command.c_str()));
314767af88aa617288e584afcfed055f7755e408542Jim Ingham                     if (tmp_option_arg_sp.get())
315767af88aa617288e584afcfed055f7755e408542Jim Ingham                     {
316767af88aa617288e584afcfed055f7755e408542Jim Ingham                         if (option_arg_vector->size() == 0)
317767af88aa617288e584afcfed055f7755e408542Jim Ingham                             interpreter.RemoveAliasOptions (alias_command.c_str());
318767af88aa617288e584afcfed055f7755e408542Jim Ingham                     }
319767af88aa617288e584afcfed055f7755e408542Jim Ingham                     result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
320767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                     alias_command.c_str());
321767af88aa617288e584afcfed055f7755e408542Jim Ingham                 }
322767af88aa617288e584afcfed055f7755e408542Jim Ingham
323767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (use_subcommand)
324767af88aa617288e584afcfed055f7755e408542Jim Ingham                     interpreter.AddAlias (alias_command.c_str(), subcommand_obj_sp);
325767af88aa617288e584afcfed055f7755e408542Jim Ingham                 else
326767af88aa617288e584afcfed055f7755e408542Jim Ingham                     interpreter.AddAlias (alias_command.c_str(), command_obj_sp);
327767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (option_arg_vector->size() > 0)
328767af88aa617288e584afcfed055f7755e408542Jim Ingham                     interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp);
329767af88aa617288e584afcfed055f7755e408542Jim Ingham                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
330767af88aa617288e584afcfed055f7755e408542Jim Ingham             }
331767af88aa617288e584afcfed055f7755e408542Jim Ingham             else
332767af88aa617288e584afcfed055f7755e408542Jim Ingham             {
333767af88aa617288e584afcfed055f7755e408542Jim Ingham                 result.AppendErrorWithFormat ("'%s' is not an existing command.\n", actual_command.c_str());
334767af88aa617288e584afcfed055f7755e408542Jim Ingham                 result.SetStatus (eReturnStatusFailed);
335767af88aa617288e584afcfed055f7755e408542Jim Ingham             }
336767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
337767af88aa617288e584afcfed055f7755e408542Jim Ingham
338767af88aa617288e584afcfed055f7755e408542Jim Ingham        return result.Succeeded();
339767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
340767af88aa617288e584afcfed055f7755e408542Jim Ingham};
341767af88aa617288e584afcfed055f7755e408542Jim Ingham
342767af88aa617288e584afcfed055f7755e408542Jim Ingham#pragma mark CommandObjectCommandsUnalias
343767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
344767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectCommandsUnalias
345767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
346767af88aa617288e584afcfed055f7755e408542Jim Ingham
347767af88aa617288e584afcfed055f7755e408542Jim Inghamclass CommandObjectCommandsUnalias : public CommandObject
348767af88aa617288e584afcfed055f7755e408542Jim Ingham{
349767af88aa617288e584afcfed055f7755e408542Jim Inghampublic:
350767af88aa617288e584afcfed055f7755e408542Jim Ingham    CommandObjectCommandsUnalias () :
351767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandObject ("commands unalias",
352767af88aa617288e584afcfed055f7755e408542Jim Ingham                   "Allows the user to remove/delete a user-defined command abbreviation.",
353767af88aa617288e584afcfed055f7755e408542Jim Ingham                   "unalias <alias-name-to-be-removed>")
354767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
355767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
356767af88aa617288e584afcfed055f7755e408542Jim Ingham
357767af88aa617288e584afcfed055f7755e408542Jim Ingham    ~CommandObjectCommandsUnalias()
358767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
359767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
360767af88aa617288e584afcfed055f7755e408542Jim Ingham
361767af88aa617288e584afcfed055f7755e408542Jim Ingham
362767af88aa617288e584afcfed055f7755e408542Jim Ingham    bool
363767af88aa617288e584afcfed055f7755e408542Jim Ingham    Execute
364767af88aa617288e584afcfed055f7755e408542Jim Ingham    (
365767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandInterpreter &interpreter,
366767af88aa617288e584afcfed055f7755e408542Jim Ingham        Args& args,
367767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandReturnObject &result
368767af88aa617288e584afcfed055f7755e408542Jim Ingham    )
369767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
370767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandObject::CommandMap::iterator pos;
371767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandObject *cmd_obj;
372767af88aa617288e584afcfed055f7755e408542Jim Ingham
373767af88aa617288e584afcfed055f7755e408542Jim Ingham        if (args.GetArgumentCount() != 0)
374767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
375767af88aa617288e584afcfed055f7755e408542Jim Ingham            const char *command_name = args.GetArgumentAtIndex(0);
376767af88aa617288e584afcfed055f7755e408542Jim Ingham            cmd_obj = interpreter.GetCommandObject(command_name);
377767af88aa617288e584afcfed055f7755e408542Jim Ingham            if (cmd_obj)
378767af88aa617288e584afcfed055f7755e408542Jim Ingham            {
379767af88aa617288e584afcfed055f7755e408542Jim Ingham                if (interpreter.CommandExists (command_name))
380767af88aa617288e584afcfed055f7755e408542Jim Ingham                {
381767af88aa617288e584afcfed055f7755e408542Jim Ingham                    result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be removed.\n",
382767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                  command_name);
383767af88aa617288e584afcfed055f7755e408542Jim Ingham                    result.SetStatus (eReturnStatusFailed);
384767af88aa617288e584afcfed055f7755e408542Jim Ingham                }
385767af88aa617288e584afcfed055f7755e408542Jim Ingham                else
386767af88aa617288e584afcfed055f7755e408542Jim Ingham                {
387767af88aa617288e584afcfed055f7755e408542Jim Ingham
388767af88aa617288e584afcfed055f7755e408542Jim Ingham                    if (interpreter.RemoveAlias (command_name) == false)
389767af88aa617288e584afcfed055f7755e408542Jim Ingham                    {
390767af88aa617288e584afcfed055f7755e408542Jim Ingham                        if (interpreter.AliasExists (command_name))
391767af88aa617288e584afcfed055f7755e408542Jim Ingham                            result.AppendErrorWithFormat ("Error occurred while attempting to unalias '%s'.\n",
392767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                          command_name);
393767af88aa617288e584afcfed055f7755e408542Jim Ingham                        else
394767af88aa617288e584afcfed055f7755e408542Jim Ingham                            result.AppendErrorWithFormat ("'%s' is not an existing alias.\n", command_name);
395767af88aa617288e584afcfed055f7755e408542Jim Ingham                        result.SetStatus (eReturnStatusFailed);
396767af88aa617288e584afcfed055f7755e408542Jim Ingham                    }
397767af88aa617288e584afcfed055f7755e408542Jim Ingham                    else
398767af88aa617288e584afcfed055f7755e408542Jim Ingham                        result.SetStatus (eReturnStatusSuccessFinishNoResult);
399767af88aa617288e584afcfed055f7755e408542Jim Ingham                }
400767af88aa617288e584afcfed055f7755e408542Jim Ingham            }
401767af88aa617288e584afcfed055f7755e408542Jim Ingham            else
402767af88aa617288e584afcfed055f7755e408542Jim Ingham            {
403767af88aa617288e584afcfed055f7755e408542Jim Ingham                result.AppendErrorWithFormat ("'%s' is not a known command.\nTry 'help' to see a "
404767af88aa617288e584afcfed055f7755e408542Jim Ingham                                              "current list of commands.\n",
405767af88aa617288e584afcfed055f7755e408542Jim Ingham                                             command_name);
406767af88aa617288e584afcfed055f7755e408542Jim Ingham                result.SetStatus (eReturnStatusFailed);
407767af88aa617288e584afcfed055f7755e408542Jim Ingham            }
408767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
409767af88aa617288e584afcfed055f7755e408542Jim Ingham        else
410767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
411767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendError ("must call 'unalias' with a valid alias");
412767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
413767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
414767af88aa617288e584afcfed055f7755e408542Jim Ingham
415767af88aa617288e584afcfed055f7755e408542Jim Ingham        return result.Succeeded();
416767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
417767af88aa617288e584afcfed055f7755e408542Jim Ingham};
418767af88aa617288e584afcfed055f7755e408542Jim Ingham
419767af88aa617288e584afcfed055f7755e408542Jim Ingham#pragma mark CommandObjectMultiwordCommands
420767af88aa617288e584afcfed055f7755e408542Jim Ingham
421767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
422767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectMultiwordCommands
423767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
424767af88aa617288e584afcfed055f7755e408542Jim Ingham
425767af88aa617288e584afcfed055f7755e408542Jim InghamCommandObjectMultiwordCommands::CommandObjectMultiwordCommands (CommandInterpreter &interpreter) :
426767af88aa617288e584afcfed055f7755e408542Jim Ingham    CommandObjectMultiword ("commands",
427c1ad82eb979ee856b86aec6e2acb7bddf75f7c4fCaroline Tice                            "A set of commands for managing or customizing the debugger commands.",
428767af88aa617288e584afcfed055f7755e408542Jim Ingham                            "commands <subcommand> [<subcommand-options>]")
429767af88aa617288e584afcfed055f7755e408542Jim Ingham{
430767af88aa617288e584afcfed055f7755e408542Jim Ingham    LoadSubCommand (interpreter, "source",   CommandObjectSP (new CommandObjectCommandsSource ()));
431767af88aa617288e584afcfed055f7755e408542Jim Ingham    LoadSubCommand (interpreter, "alias",   CommandObjectSP (new CommandObjectCommandsAlias ()));
432767af88aa617288e584afcfed055f7755e408542Jim Ingham}
433767af88aa617288e584afcfed055f7755e408542Jim Ingham
434767af88aa617288e584afcfed055f7755e408542Jim InghamCommandObjectMultiwordCommands::~CommandObjectMultiwordCommands ()
435767af88aa617288e584afcfed055f7755e408542Jim Ingham{
436767af88aa617288e584afcfed055f7755e408542Jim Ingham}
437767af88aa617288e584afcfed055f7755e408542Jim Ingham
438