CommandObjectCommands.cpp revision f737d372a9672c9feaedf4b2cd7b16e31357d38e
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
1540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton#include "llvm/ADT/StringRef.h"
1640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
17767af88aa617288e584afcfed055f7755e408542Jim Ingham// Project includes
18767af88aa617288e584afcfed055f7755e408542Jim Ingham#include "lldb/Core/Debugger.h"
19d12aeab33bab559e138307f599077da3918a3238Greg Clayton#include "lldb/Core/InputReader.h"
20c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata#include "lldb/Core/InputReaderEZ.h"
21c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata#include "lldb/Core/StringList.h"
22d12aeab33bab559e138307f599077da3918a3238Greg Clayton#include "lldb/Interpreter/Args.h"
23767af88aa617288e584afcfed055f7755e408542Jim Ingham#include "lldb/Interpreter/CommandInterpreter.h"
24d12aeab33bab559e138307f599077da3918a3238Greg Clayton#include "lldb/Interpreter/CommandObjectRegexCommand.h"
25767af88aa617288e584afcfed055f7755e408542Jim Ingham#include "lldb/Interpreter/CommandReturnObject.h"
26767af88aa617288e584afcfed055f7755e408542Jim Ingham#include "lldb/Interpreter/Options.h"
27e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata#include "lldb/Interpreter/ScriptInterpreter.h"
28e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata#include "lldb/Interpreter/ScriptInterpreterPython.h"
29767af88aa617288e584afcfed055f7755e408542Jim Ingham
30767af88aa617288e584afcfed055f7755e408542Jim Inghamusing namespace lldb;
31767af88aa617288e584afcfed055f7755e408542Jim Inghamusing namespace lldb_private;
32767af88aa617288e584afcfed055f7755e408542Jim Ingham
33767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
34767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectCommandsSource
35767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
36767af88aa617288e584afcfed055f7755e408542Jim Ingham
37da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectCommandsHistory : public CommandObjectParsed
386247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{
39da26bd203cbb104291b39891febf7481794f205fJim Inghampublic:
40da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectCommandsHistory(CommandInterpreter &interpreter) :
41da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
42da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "command history",
43da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Dump the history of commands in this session.",
44da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL),
45da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_options (interpreter)
46da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
47da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
48da26bd203cbb104291b39891febf7481794f205fJim Ingham
49da26bd203cbb104291b39891febf7481794f205fJim Ingham    ~CommandObjectCommandsHistory () {}
50da26bd203cbb104291b39891febf7481794f205fJim Ingham
51da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual Options *
52da26bd203cbb104291b39891febf7481794f205fJim Ingham    GetOptions ()
53da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
54da26bd203cbb104291b39891febf7481794f205fJim Ingham        return &m_options;
55da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
56da26bd203cbb104291b39891febf7481794f205fJim Ingham
57da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
586247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
596247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    class CommandOptions : public Options
606247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    {
616247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    public:
626247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
636247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        CommandOptions (CommandInterpreter &interpreter) :
646247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            Options (interpreter)
656247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        {
666247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        }
676247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
686247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        virtual
696247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        ~CommandOptions (){}
706247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
716247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        virtual Error
726247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        SetOptionValue (uint32_t option_idx, const char *option_arg)
736247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        {
746247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            Error error;
756247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            char short_option = (char) m_getopt_table[option_idx].val;
766247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            bool success;
776247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
786247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            switch (short_option)
796247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            {
806247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                case 'c':
816247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    m_end_idx = Args::StringToUInt32(option_arg, UINT_MAX, 0, &success);
826247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    if (!success)
839c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                        error.SetErrorStringWithFormat("invalid value for count: %s", option_arg);
846247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    if (m_end_idx != 0)
856247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                        m_end_idx--;
866247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    m_start_idx = 0;
876247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    break;
886247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                case 'e':
896247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    m_end_idx = Args::StringToUInt32(option_arg, 0, 0, &success);
906247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    if (!success)
919c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                        error.SetErrorStringWithFormat("invalid value for end index: %s", option_arg);
926247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    break;
936247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                case 's':
946247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    m_start_idx = Args::StringToUInt32(option_arg, 0, 0, &success);
956247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    if (!success)
969c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                        error.SetErrorStringWithFormat("invalid value for start index: %s", option_arg);
976247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    break;
986247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                default:
999c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                    error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1006247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    break;
1016247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            }
1026247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1036247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            return error;
1046247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        }
1056247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1066247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        void
1076247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        OptionParsingStarting ()
1086247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        {
1096247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            m_start_idx = 0;
1106247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            m_end_idx = UINT_MAX;
1116247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        }
1126247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1136247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        const OptionDefinition*
1146247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        GetDefinitions ()
1156247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        {
1166247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            return g_option_table;
1176247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        }
1186247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1196247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        // Options table: Required for subclasses of Options.
1206247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1216247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        static OptionDefinition g_option_table[];
1226247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1236247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        // Instance variables to hold the values for command options.
1246247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1256247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        uint32_t m_start_idx;
1266247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        uint32_t m_end_idx;
1276247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    };
1286247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1296247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    bool
130da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
1316247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    {
1326247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1336247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        m_interpreter.DumpHistory (result.GetOutputStream(),
1346247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                                   m_options.m_start_idx,
1356247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                                   m_options.m_end_idx);
1366247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        return result.Succeeded();
1376247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1386247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    }
139da26bd203cbb104291b39891febf7481794f205fJim Ingham
140da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandOptions m_options;
1416247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham};
1426247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1436247dbee41ec51e9a082df7e86269c0f47f28160Jim InghamOptionDefinition
1446247dbee41ec51e9a082df7e86269c0f47f28160Jim InghamCommandObjectCommandsHistory::CommandOptions::g_option_table[] =
1456247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{
1466247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{ LLDB_OPT_SET_1, false, "count", 'c', required_argument, NULL, 0, eArgTypeUnsignedInteger,        "How many history commands to print."},
1476247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{ LLDB_OPT_SET_1, false, "start-index", 's', required_argument, NULL, 0, eArgTypeUnsignedInteger,  "Index at which to start printing history commands."},
1486247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{ LLDB_OPT_SET_1, false, "end-index", 'e', required_argument, NULL, 0, eArgTypeUnsignedInteger,    "Index at which to stop printing history commands."},
1496247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1506247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham};
1516247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1526247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1536247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham//-------------------------------------------------------------------------
1546247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham// CommandObjectCommandsSource
1556247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham//-------------------------------------------------------------------------
1566247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
157da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectCommandsSource : public CommandObjectParsed
158767af88aa617288e584afcfed055f7755e408542Jim Ingham{
159da26bd203cbb104291b39891febf7481794f205fJim Inghampublic:
160da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectCommandsSource(CommandInterpreter &interpreter) :
161da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
162da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "command source",
163da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Read in debugger commands from the file <filename> and execute them.",
164da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL),
165da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_options (interpreter)
166da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
167da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentEntry arg;
168da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentData file_arg;
169da26bd203cbb104291b39891febf7481794f205fJim Ingham
170da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Define the first (and only) variant of this arg.
171da26bd203cbb104291b39891febf7481794f205fJim Ingham        file_arg.arg_type = eArgTypeFilename;
172da26bd203cbb104291b39891febf7481794f205fJim Ingham        file_arg.arg_repetition = eArgRepeatPlain;
173da26bd203cbb104291b39891febf7481794f205fJim Ingham
174da26bd203cbb104291b39891febf7481794f205fJim Ingham        // There is only one variant this argument could be; put it into the argument entry.
175da26bd203cbb104291b39891febf7481794f205fJim Ingham        arg.push_back (file_arg);
176da26bd203cbb104291b39891febf7481794f205fJim Ingham
177da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Push the data for the first argument into the m_arguments vector.
178da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_arguments.push_back (arg);
179da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
180da26bd203cbb104291b39891febf7481794f205fJim Ingham
181da26bd203cbb104291b39891febf7481794f205fJim Ingham    ~CommandObjectCommandsSource () {}
182da26bd203cbb104291b39891febf7481794f205fJim Ingham
183da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual const char*
184da26bd203cbb104291b39891febf7481794f205fJim Ingham    GetRepeatCommand (Args &current_command_args, uint32_t index)
185da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
186da26bd203cbb104291b39891febf7481794f205fJim Ingham        return "";
187da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
188da26bd203cbb104291b39891febf7481794f205fJim Ingham
189da26bd203cbb104291b39891febf7481794f205fJim Ingham    int
190da26bd203cbb104291b39891febf7481794f205fJim Ingham    HandleArgumentCompletion (Args &input,
191da26bd203cbb104291b39891febf7481794f205fJim Ingham                              int &cursor_index,
192da26bd203cbb104291b39891febf7481794f205fJim Ingham                              int &cursor_char_position,
193da26bd203cbb104291b39891febf7481794f205fJim Ingham                              OptionElementVector &opt_element_vector,
194da26bd203cbb104291b39891febf7481794f205fJim Ingham                              int match_start_point,
195da26bd203cbb104291b39891febf7481794f205fJim Ingham                              int max_return_elements,
196da26bd203cbb104291b39891febf7481794f205fJim Ingham                              bool &word_complete,
197da26bd203cbb104291b39891febf7481794f205fJim Ingham                              StringList &matches)
198da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
199da26bd203cbb104291b39891febf7481794f205fJim Ingham        std::string completion_str (input.GetArgumentAtIndex(cursor_index));
200da26bd203cbb104291b39891febf7481794f205fJim Ingham        completion_str.erase (cursor_char_position);
201da26bd203cbb104291b39891febf7481794f205fJim Ingham
202da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
203da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             CommandCompletions::eDiskFileCompletion,
204da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             completion_str.c_str(),
205da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             match_start_point,
206da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             max_return_elements,
207da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             NULL,
208da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             word_complete,
209da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             matches);
210da26bd203cbb104291b39891febf7481794f205fJim Ingham        return matches.GetSize();
211da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
212da26bd203cbb104291b39891febf7481794f205fJim Ingham
213da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual Options *
214da26bd203cbb104291b39891febf7481794f205fJim Ingham    GetOptions ()
215da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
216da26bd203cbb104291b39891febf7481794f205fJim Ingham        return &m_options;
217da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
218da26bd203cbb104291b39891febf7481794f205fJim Ingham
219da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
220949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
221949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    class CommandOptions : public Options
222949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    {
223949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    public:
224949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
225f15996eea072cdaa8a092f22d3a1212b3d95f0ecGreg Clayton        CommandOptions (CommandInterpreter &interpreter) :
226f15996eea072cdaa8a092f22d3a1212b3d95f0ecGreg Clayton            Options (interpreter)
227f15996eea072cdaa8a092f22d3a1212b3d95f0ecGreg Clayton        {
228f15996eea072cdaa8a092f22d3a1212b3d95f0ecGreg Clayton        }
229949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
230949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        virtual
231949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        ~CommandOptions (){}
232949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
233949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        virtual Error
234143fcc3a15425659b381502ed4e1e50a3e726f36Greg Clayton        SetOptionValue (uint32_t option_idx, const char *option_arg)
235949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        {
236949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            Error error;
237949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            char short_option = (char) m_getopt_table[option_idx].val;
238949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            bool success;
239949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
240949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            switch (short_option)
241949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            {
242949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                case 'e':
243949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    m_stop_on_error = Args::StringToBoolean(option_arg, true, &success);
244949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    if (!success)
2459c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                        error.SetErrorStringWithFormat("invalid value for stop-on-error: %s", option_arg);
246949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    break;
247949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                case 'c':
248949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    m_stop_on_continue = Args::StringToBoolean(option_arg, true, &success);
249949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    if (!success)
2509c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                        error.SetErrorStringWithFormat("invalid value for stop-on-continue: %s", option_arg);
251949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    break;
252949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                default:
2539c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                    error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
254949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    break;
255949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            }
256949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
257949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            return error;
258949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        }
259949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
260949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        void
261143fcc3a15425659b381502ed4e1e50a3e726f36Greg Clayton        OptionParsingStarting ()
262949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        {
263949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            m_stop_on_error = true;
264949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            m_stop_on_continue = true;
265949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        }
266949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
267b344843f75ef893762c93fd0a22d2d45712ce74dGreg Clayton        const OptionDefinition*
268949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        GetDefinitions ()
269949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        {
270949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            return g_option_table;
271949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        }
272949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
273949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        // Options table: Required for subclasses of Options.
274949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
275b344843f75ef893762c93fd0a22d2d45712ce74dGreg Clayton        static OptionDefinition g_option_table[];
276949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
277949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        // Instance variables to hold the values for command options.
278949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
279949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        bool m_stop_on_error;
280949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        bool m_stop_on_continue;
281949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    };
282949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
283767af88aa617288e584afcfed055f7755e408542Jim Ingham    bool
284da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute(Args& command, CommandReturnObject &result)
285767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
286da26bd203cbb104291b39891febf7481794f205fJim Ingham        const int argc = command.GetArgumentCount();
287767af88aa617288e584afcfed055f7755e408542Jim Ingham        if (argc == 1)
288767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
289da26bd203cbb104291b39891febf7481794f205fJim Ingham            const char *filename = command.GetArgumentAtIndex(0);
290767af88aa617288e584afcfed055f7755e408542Jim Ingham
291767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendMessageWithFormat ("Executing commands in '%s'.\n", filename);
292767af88aa617288e584afcfed055f7755e408542Jim Ingham
293a83ea887f6165d59cfeac1bbb96011f3b0568af8Johnny Chen            FileSpec cmd_file (filename, true);
294949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            ExecutionContext *exe_ctx = NULL;  // Just use the default context.
295949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            bool echo_commands    = true;
296949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            bool print_results    = true;
297949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
298949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            m_interpreter.HandleCommandsFromFile (cmd_file,
299949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  exe_ctx,
300949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  m_options.m_stop_on_continue,
301949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  m_options.m_stop_on_error,
302949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  echo_commands,
30301bc2d493b48e4904e3241a7768e18fdd9bb6835Enrico Granata                                                  print_results,
30401bc2d493b48e4904e3241a7768e18fdd9bb6835Enrico Granata                                                  eLazyBoolCalculate,
305949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  result);
306767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
307767af88aa617288e584afcfed055f7755e408542Jim Ingham        else
308767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
309767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendErrorWithFormat("'%s' takes exactly one executable filename argument.\n", GetCommandName());
310767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
311767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
312767af88aa617288e584afcfed055f7755e408542Jim Ingham        return result.Succeeded();
313767af88aa617288e584afcfed055f7755e408542Jim Ingham
314767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
315da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandOptions m_options;
316767af88aa617288e584afcfed055f7755e408542Jim Ingham};
317767af88aa617288e584afcfed055f7755e408542Jim Ingham
318b344843f75ef893762c93fd0a22d2d45712ce74dGreg ClaytonOptionDefinition
319949d5acde6684b5824a26034457410cdf3823dfeJim InghamCommandObjectCommandsSource::CommandOptions::g_option_table[] =
320949d5acde6684b5824a26034457410cdf3823dfeJim Ingham{
321949d5acde6684b5824a26034457410cdf3823dfeJim Ingham{ LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', required_argument, NULL, 0, eArgTypeBoolean,    "If true, stop executing commands on error."},
322949d5acde6684b5824a26034457410cdf3823dfeJim Ingham{ LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', required_argument, NULL, 0, eArgTypeBoolean, "If true, stop executing commands on continue."},
323949d5acde6684b5824a26034457410cdf3823dfeJim Ingham{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
324949d5acde6684b5824a26034457410cdf3823dfeJim Ingham};
325949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
326767af88aa617288e584afcfed055f7755e408542Jim Ingham#pragma mark CommandObjectCommandsAlias
327767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
328767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectCommandsAlias
329767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
330767af88aa617288e584afcfed055f7755e408542Jim Ingham
331c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granatastatic const char *g_python_command_instructions =   "Enter your Python command(s). Type 'DONE' to end.\n"
332c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata                                                     "You must define a Python function with this signature:\n"
333c1ca9dcd0e8a1c7c5a882281afdd2a5145026fd0Enrico Granata                                                     "def my_command_impl(debugger, args, result, internal_dict):";
334c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata
335c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata
336da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectCommandsAlias : public CommandObjectRaw
337767af88aa617288e584afcfed055f7755e408542Jim Ingham{
338c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata
339c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata
340767af88aa617288e584afcfed055f7755e408542Jim Inghampublic:
341238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectCommandsAlias (CommandInterpreter &interpreter) :
342da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectRaw (interpreter,
34340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                       "command alias",
344abb507ae79e20ab2dd8365049921df73a24c95a5Caroline Tice                       "Allow users to define their own debugger command abbreviations.",
34543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice                       NULL)
346767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
347767af88aa617288e584afcfed055f7755e408542Jim Ingham        SetHelpLong(
348767af88aa617288e584afcfed055f7755e408542Jim Ingham    "'alias' allows the user to create a short-cut or abbreviation for long \n\
349767af88aa617288e584afcfed055f7755e408542Jim Ingham    commands, multi-word commands, and commands that take particular options. \n\
350767af88aa617288e584afcfed055f7755e408542Jim Ingham    Below are some simple examples of how one might use the 'alias' command: \n\
3514049bc3c000c15a1815d2ff82c3e596e303ded0fJason Molenda    \n    'command alias sc script'            // Creates the abbreviation 'sc' for the 'script' \n\
35231fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // command. \n\
3534049bc3c000c15a1815d2ff82c3e596e303ded0fJason Molenda    'command alias bp breakpoint'        // Creates the abbreviation 'bp' for the 'breakpoint' \n\
35431fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // command.  Since breakpoint commands are two-word \n\
35531fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // commands, the user will still need to enter the \n\
35631fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // second word after 'bp', e.g. 'bp enable' or \n\
35731fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // 'bp delete'. \n\
3584049bc3c000c15a1815d2ff82c3e596e303ded0fJason Molenda    'command alias bpl breakpoint list'  // Creates the abbreviation 'bpl' for the \n\
35931fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // two-word command 'breakpoint list'. \n\
360767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nAn alias can include some options for the command, with the values either \n\
361767af88aa617288e584afcfed055f7755e408542Jim Ingham    filled in at the time the alias is created, or specified as positional \n\
362767af88aa617288e584afcfed055f7755e408542Jim Ingham    arguments, to be filled in when the alias is invoked.  The following example \n\
363767af88aa617288e584afcfed055f7755e408542Jim Ingham    shows how to create aliases with options: \n\
364767af88aa617288e584afcfed055f7755e408542Jim Ingham    \n\
3654049bc3c000c15a1815d2ff82c3e596e303ded0fJason Molenda    'command alias bfl breakpoint set -f %1 -l %2' \n\
366767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nThis creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \n\
367767af88aa617288e584afcfed055f7755e408542Jim Ingham    options already part of the alias.  So if the user wants to set a breakpoint \n\
368767af88aa617288e584afcfed055f7755e408542Jim Ingham    by file and line without explicitly having to use the -f and -l options, the \n\
369767af88aa617288e584afcfed055f7755e408542Jim Ingham    user can now use 'bfl' instead.  The '%1' and '%2' are positional placeholders \n\
370767af88aa617288e584afcfed055f7755e408542Jim Ingham    for the actual arguments that will be passed when the alias command is used. \n\
371767af88aa617288e584afcfed055f7755e408542Jim Ingham    The number in the placeholder refers to the position/order the actual value \n\
372536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    occupies when the alias is used.  All the occurrences of '%1' in the alias \n\
373767af88aa617288e584afcfed055f7755e408542Jim Ingham    will be replaced with the first argument, all the occurrences of '%2' in the \n\
374767af88aa617288e584afcfed055f7755e408542Jim Ingham    alias will be replaced with the second argument, and so on.  This also allows \n\
375767af88aa617288e584afcfed055f7755e408542Jim Ingham    actual arguments to be used multiple times within an alias (see 'process \n\
376536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    launch' example below).  \n\
377536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    Note: the positional arguments must substitute as whole words in the resultant\n\
378536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    command, so you can't at present do something like:\n\
379536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    \n\
3804049bc3c000c15a1815d2ff82c3e596e303ded0fJason Molenda    command alias bcppfl breakpoint set -f %1.cpp -l %2\n\
381536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    \n\
382536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    to get the file extension \".cpp\" automatically appended.  For more complex\n\
383536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    aliasing, use the \"command regex\" command instead.\n\
384536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    \nSo in the 'bfl' case, the actual file value will be \n\
385767af88aa617288e584afcfed055f7755e408542Jim Ingham    filled in with the first argument following 'bfl' and the actual line number \n\
386767af88aa617288e584afcfed055f7755e408542Jim Ingham    value will be filled in with the second argument.  The user would use this \n\
387767af88aa617288e584afcfed055f7755e408542Jim Ingham    alias as follows: \n\
3884049bc3c000c15a1815d2ff82c3e596e303ded0fJason Molenda    \n    (lldb)  command alias bfl breakpoint set -f %1 -l %2 \n\
389a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    <... some time later ...> \n\
39031fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice    (lldb)  bfl my-file.c 137 \n\
391767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nThis would be the same as if the user had entered \n\
392767af88aa617288e584afcfed055f7755e408542Jim Ingham    'breakpoint set -f my-file.c -l 137'. \n\
393767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nAnother example: \n\
3944049bc3c000c15a1815d2ff82c3e596e303ded0fJason Molenda    \n    (lldb)  command alias pltty  process launch -s -o %1 -e %1 \n\
39531fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice    (lldb)  pltty /dev/tty0 \n\
396a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan           // becomes 'process launch -s -o /dev/tty0 -e /dev/tty0' \n\
397767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nIf the user always wanted to pass the same value to a particular option, the \n\
398767af88aa617288e584afcfed055f7755e408542Jim Ingham    alias could be defined with that value directly in the alias as a constant, \n\
399767af88aa617288e584afcfed055f7755e408542Jim Ingham    rather than using a positional placeholder: \n\
4004049bc3c000c15a1815d2ff82c3e596e303ded0fJason Molenda    \n    command alias bl3  breakpoint set -f %1 -l 3  // Always sets a breakpoint on line \n\
401a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan                                                   // 3 of whatever file is indicated. \n");
402767af88aa617288e584afcfed055f7755e408542Jim Ingham
40343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg1;
40443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg2;
40543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg3;
40643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData alias_arg;
40743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData cmd_arg;
40843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData options_arg;
40943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
41043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
41143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        alias_arg.arg_type = eArgTypeAliasName;
41243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        alias_arg.arg_repetition = eArgRepeatPlain;
41343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
41443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
41543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg1.push_back (alias_arg);
41643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
41743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
41843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        cmd_arg.arg_type = eArgTypeCommandName;
41943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        cmd_arg.arg_repetition = eArgRepeatPlain;
42043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
42143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
42243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg2.push_back (cmd_arg);
42343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
42443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
42543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        options_arg.arg_type = eArgTypeAliasOptions;
42643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        options_arg.arg_repetition = eArgRepeatOptional;
42743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
42843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
42943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg3.push_back (options_arg);
43043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
43143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Push the data for the first argument into the m_arguments vector.
43243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg1);
43343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg2);
43443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg3);
435767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
436767af88aa617288e584afcfed055f7755e408542Jim Ingham
437767af88aa617288e584afcfed055f7755e408542Jim Ingham    ~CommandObjectCommandsAlias ()
438767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
439767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
440767af88aa617288e584afcfed055f7755e408542Jim Ingham
441da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
442da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual bool
443da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (const char *raw_command_line, CommandReturnObject &result)
444e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice    {
445e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        Args args (raw_command_line);
446e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        std::string raw_command_string (raw_command_line);
447e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
448e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        size_t argc = args.GetArgumentCount();
449e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
450e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        if (argc < 2)
451e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
452e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.AppendError ("'alias' requires at least two arguments");
453e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.SetStatus (eReturnStatusFailed);
454e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            return false;
455e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
456e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
457e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // Get the alias command.
458e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
459e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        const std::string alias_command = args.GetArgumentAtIndex (0);
460c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata
461e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // Strip the new alias name off 'raw_command_string'  (leave it on args, which gets passed to 'Execute', which
462e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // does the stripping itself.
463e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        size_t pos = raw_command_string.find (alias_command);
464e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        if (pos == 0)
465e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
466e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            raw_command_string = raw_command_string.substr (alias_command.size());
467e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            pos = raw_command_string.find_first_not_of (' ');
468e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            if ((pos != std::string::npos) && (pos > 0))
469e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                raw_command_string = raw_command_string.substr (pos);
470e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
471e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        else
472e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
473e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.AppendError ("Error parsing command string.  No alias created.");
474e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.SetStatus (eReturnStatusFailed);
475e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            return false;
476e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
477e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
478e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
479e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // Verify that the command is alias-able.
480e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        if (m_interpreter.CommandExists (alias_command.c_str()))
481e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
482e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
483e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                                          alias_command.c_str());
484e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.SetStatus (eReturnStatusFailed);
485e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            return false;
486e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
487e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
488e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // Get CommandObject that is being aliased. The command name is read from the front of raw_command_string.
489e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // raw_command_string is returned with the name of the command object stripped off the front.
490e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        CommandObject *cmd_obj = m_interpreter.GetCommandObjectForCommand (raw_command_string);
491e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
492e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        if (!cmd_obj)
493e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
4949c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton            result.AppendErrorWithFormat ("invalid command given to 'alias'. '%s' does not begin with a valid command."
495e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                                          "  No alias created.", raw_command_string.c_str());
496e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.SetStatus (eReturnStatusFailed);
497e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            return false;
498e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
499e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        else if (!cmd_obj->WantsRawCommandString ())
500e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
501e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            // Note that args was initialized with the original command, and has not been updated to this point.
502e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            // Therefore can we pass it to the version of Execute that does not need/expect raw input in the alias.
503da26bd203cbb104291b39891febf7481794f205fJim Ingham            return HandleAliasingNormalCommand (args, result);
504e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
505e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        else
506e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
507da26bd203cbb104291b39891febf7481794f205fJim Ingham            return HandleAliasingRawCommand (alias_command, raw_command_string, *cmd_obj, result);
508da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
509da26bd203cbb104291b39891febf7481794f205fJim Ingham        return result.Succeeded();
510da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
511da26bd203cbb104291b39891febf7481794f205fJim Ingham
512da26bd203cbb104291b39891febf7481794f205fJim Ingham    bool
513da26bd203cbb104291b39891febf7481794f205fJim Ingham    HandleAliasingRawCommand (const std::string &alias_command, std::string &raw_command_string, CommandObject &cmd_obj, CommandReturnObject &result)
514da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
515e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            // Verify & handle any options/arguments passed to the alias command
516e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
517e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector);
518e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
519e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
520da26bd203cbb104291b39891febf7481794f205fJim Ingham            CommandObjectSP cmd_obj_sp = m_interpreter.GetCommandSPExact (cmd_obj.GetCommandName(), false);
5215ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice
5225ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice            if (!m_interpreter.ProcessAliasOptionsArgs (cmd_obj_sp, raw_command_string.c_str(), option_arg_vector_sp))
523e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            {
5245ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                result.AppendError ("Unable to create requested alias.\n");
5255ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                result.SetStatus (eReturnStatusFailed);
5265ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                return false;
527e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            }
528e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
529e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            // Create the alias
530e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            if (m_interpreter.AliasExists (alias_command.c_str())
531e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                || m_interpreter.UserCommandExists (alias_command.c_str()))
532e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            {
533e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                OptionArgVectorSP temp_option_arg_sp (m_interpreter.GetAliasOptions (alias_command.c_str()));
534e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                if (temp_option_arg_sp.get())
535e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                {
536e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                    if (option_arg_vector->size() == 0)
537e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                        m_interpreter.RemoveAliasOptions (alias_command.c_str());
538e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                }
539e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
540e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                                                alias_command.c_str());
541e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            }
542e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
54356d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            if (cmd_obj_sp)
54456d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            {
54556d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                m_interpreter.AddAlias (alias_command.c_str(), cmd_obj_sp);
54656d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                if (option_arg_vector->size() > 0)
54756d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                    m_interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp);
54856d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                result.SetStatus (eReturnStatusSuccessFinishNoResult);
54956d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            }
55056d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            else
55156d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            {
55256d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                result.AppendError ("Unable to create requested alias.\n");
55356d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                result.SetStatus (eReturnStatusFailed);
55456d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            }
555da26bd203cbb104291b39891febf7481794f205fJim Ingham            return result.Succeeded ();
556e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice    }
557da26bd203cbb104291b39891febf7481794f205fJim Ingham
558767af88aa617288e584afcfed055f7755e408542Jim Ingham    bool
559da26bd203cbb104291b39891febf7481794f205fJim Ingham    HandleAliasingNormalCommand (Args& args, CommandReturnObject &result)
560767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
5618bb61f099795a3fd98edf655c3e6899cd2668f6cCaroline Tice        size_t argc = args.GetArgumentCount();
562767af88aa617288e584afcfed055f7755e408542Jim Ingham
563767af88aa617288e584afcfed055f7755e408542Jim Ingham        if (argc < 2)
56454e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton        {
565767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendError ("'alias' requires at least two arguments");
566767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
567767af88aa617288e584afcfed055f7755e408542Jim Ingham            return false;
56854e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton        }
569767af88aa617288e584afcfed055f7755e408542Jim Ingham
570767af88aa617288e584afcfed055f7755e408542Jim Ingham        const std::string alias_command = args.GetArgumentAtIndex(0);
571767af88aa617288e584afcfed055f7755e408542Jim Ingham        const std::string actual_command = args.GetArgumentAtIndex(1);
572767af88aa617288e584afcfed055f7755e408542Jim Ingham
573767af88aa617288e584afcfed055f7755e408542Jim Ingham        args.Shift();  // Shift the alias command word off the argument vector.
574767af88aa617288e584afcfed055f7755e408542Jim Ingham        args.Shift();  // Shift the old command word off the argument vector.
575767af88aa617288e584afcfed055f7755e408542Jim Ingham
576767af88aa617288e584afcfed055f7755e408542Jim Ingham        // Verify that the command is alias'able, and get the appropriate command object.
577767af88aa617288e584afcfed055f7755e408542Jim Ingham
578238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton        if (m_interpreter.CommandExists (alias_command.c_str()))
579767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
580767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
581767af88aa617288e584afcfed055f7755e408542Jim Ingham                                         alias_command.c_str());
582767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
583767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
584767af88aa617288e584afcfed055f7755e408542Jim Ingham        else
585767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
586238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton             CommandObjectSP command_obj_sp(m_interpreter.GetCommandSPExact (actual_command.c_str(), true));
587767af88aa617288e584afcfed055f7755e408542Jim Ingham             CommandObjectSP subcommand_obj_sp;
588767af88aa617288e584afcfed055f7755e408542Jim Ingham             bool use_subcommand = false;
589767af88aa617288e584afcfed055f7755e408542Jim Ingham             if (command_obj_sp.get())
590767af88aa617288e584afcfed055f7755e408542Jim Ingham             {
591767af88aa617288e584afcfed055f7755e408542Jim Ingham                 CommandObject *cmd_obj = command_obj_sp.get();
59254e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton                 CommandObject *sub_cmd_obj = NULL;
593767af88aa617288e584afcfed055f7755e408542Jim Ingham                 OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector);
594767af88aa617288e584afcfed055f7755e408542Jim Ingham                 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
595767af88aa617288e584afcfed055f7755e408542Jim Ingham
596e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                 while (cmd_obj->IsMultiwordObject() && args.GetArgumentCount() > 0)
597767af88aa617288e584afcfed055f7755e408542Jim Ingham                 {
598767af88aa617288e584afcfed055f7755e408542Jim Ingham                     if (argc >= 3)
599767af88aa617288e584afcfed055f7755e408542Jim Ingham                     {
600767af88aa617288e584afcfed055f7755e408542Jim Ingham                         const std::string sub_command = args.GetArgumentAtIndex(0);
601767af88aa617288e584afcfed055f7755e408542Jim Ingham                         assert (sub_command.length() != 0);
602767af88aa617288e584afcfed055f7755e408542Jim Ingham                         subcommand_obj_sp =
603767af88aa617288e584afcfed055f7755e408542Jim Ingham                                           (((CommandObjectMultiword *) cmd_obj)->GetSubcommandSP (sub_command.c_str()));
604767af88aa617288e584afcfed055f7755e408542Jim Ingham                         if (subcommand_obj_sp.get())
605767af88aa617288e584afcfed055f7755e408542Jim Ingham                         {
606767af88aa617288e584afcfed055f7755e408542Jim Ingham                             sub_cmd_obj = subcommand_obj_sp.get();
607767af88aa617288e584afcfed055f7755e408542Jim Ingham                             use_subcommand = true;
608767af88aa617288e584afcfed055f7755e408542Jim Ingham                             args.Shift();  // Shift the sub_command word off the argument vector.
609e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                             cmd_obj = sub_cmd_obj;
610767af88aa617288e584afcfed055f7755e408542Jim Ingham                         }
611767af88aa617288e584afcfed055f7755e408542Jim Ingham                         else
612767af88aa617288e584afcfed055f7755e408542Jim Ingham                         {
6135d53b621dea6a2ca6ece7528b22d089f864ae18fCaroline Tice                             result.AppendErrorWithFormat("'%s' is not a valid sub-command of '%s'.  "
6145d53b621dea6a2ca6ece7528b22d089f864ae18fCaroline Tice                                                          "Unable to create alias.\n",
6155d53b621dea6a2ca6ece7528b22d089f864ae18fCaroline Tice                                                          sub_command.c_str(), actual_command.c_str());
616767af88aa617288e584afcfed055f7755e408542Jim Ingham                             result.SetStatus (eReturnStatusFailed);
617767af88aa617288e584afcfed055f7755e408542Jim Ingham                             return false;
618767af88aa617288e584afcfed055f7755e408542Jim Ingham                         }
619767af88aa617288e584afcfed055f7755e408542Jim Ingham                     }
620767af88aa617288e584afcfed055f7755e408542Jim Ingham                 }
621767af88aa617288e584afcfed055f7755e408542Jim Ingham
622767af88aa617288e584afcfed055f7755e408542Jim Ingham                 // Verify & handle any options/arguments passed to the alias command
623767af88aa617288e584afcfed055f7755e408542Jim Ingham
624767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (args.GetArgumentCount () > 0)
625767af88aa617288e584afcfed055f7755e408542Jim Ingham                 {
6265ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    CommandObjectSP tmp_sp = m_interpreter.GetCommandSPExact (cmd_obj->GetCommandName(), false);
6275ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    if (use_subcommand)
6285ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                        tmp_sp = m_interpreter.GetCommandSPExact (sub_cmd_obj->GetCommandName(), false);
6295ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice
6305ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    std::string args_string;
6315ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    args.GetCommandString (args_string);
6325ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice
6335ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    if (!m_interpreter.ProcessAliasOptionsArgs (tmp_sp, args_string.c_str(), option_arg_vector_sp))
6345ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    {
6355ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                        result.AppendError ("Unable to create requested alias.\n");
6365ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                        result.SetStatus (eReturnStatusFailed);
6375ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                        return false;
6385ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    }
639767af88aa617288e584afcfed055f7755e408542Jim Ingham                 }
640767af88aa617288e584afcfed055f7755e408542Jim Ingham
641767af88aa617288e584afcfed055f7755e408542Jim Ingham                 // Create the alias.
642767af88aa617288e584afcfed055f7755e408542Jim Ingham
643238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                 if (m_interpreter.AliasExists (alias_command.c_str())
644238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                     || m_interpreter.UserCommandExists (alias_command.c_str()))
645767af88aa617288e584afcfed055f7755e408542Jim Ingham                 {
646238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                     OptionArgVectorSP tmp_option_arg_sp (m_interpreter.GetAliasOptions (alias_command.c_str()));
647767af88aa617288e584afcfed055f7755e408542Jim Ingham                     if (tmp_option_arg_sp.get())
648767af88aa617288e584afcfed055f7755e408542Jim Ingham                     {
649767af88aa617288e584afcfed055f7755e408542Jim Ingham                         if (option_arg_vector->size() == 0)
650238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                             m_interpreter.RemoveAliasOptions (alias_command.c_str());
651767af88aa617288e584afcfed055f7755e408542Jim Ingham                     }
652767af88aa617288e584afcfed055f7755e408542Jim Ingham                     result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
653767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                     alias_command.c_str());
654767af88aa617288e584afcfed055f7755e408542Jim Ingham                 }
655767af88aa617288e584afcfed055f7755e408542Jim Ingham
656767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (use_subcommand)
657238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                     m_interpreter.AddAlias (alias_command.c_str(), subcommand_obj_sp);
658767af88aa617288e584afcfed055f7755e408542Jim Ingham                 else
659238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                     m_interpreter.AddAlias (alias_command.c_str(), command_obj_sp);
660767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (option_arg_vector->size() > 0)
661238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                     m_interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp);
662767af88aa617288e584afcfed055f7755e408542Jim Ingham                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
663767af88aa617288e584afcfed055f7755e408542Jim Ingham             }
664767af88aa617288e584afcfed055f7755e408542Jim Ingham             else
665767af88aa617288e584afcfed055f7755e408542Jim Ingham             {
666767af88aa617288e584afcfed055f7755e408542Jim Ingham                 result.AppendErrorWithFormat ("'%s' is not an existing command.\n", actual_command.c_str());
667767af88aa617288e584afcfed055f7755e408542Jim Ingham                 result.SetStatus (eReturnStatusFailed);
668e6866a3819d68be21004ffdf773e36e7f00a6a26Caroline Tice                 return false;
669767af88aa617288e584afcfed055f7755e408542Jim Ingham             }
670767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
671767af88aa617288e584afcfed055f7755e408542Jim Ingham
672767af88aa617288e584afcfed055f7755e408542Jim Ingham        return result.Succeeded();
673767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
674da26bd203cbb104291b39891febf7481794f205fJim Ingham
675767af88aa617288e584afcfed055f7755e408542Jim Ingham};
676767af88aa617288e584afcfed055f7755e408542Jim Ingham
677767af88aa617288e584afcfed055f7755e408542Jim Ingham#pragma mark CommandObjectCommandsUnalias
678767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
679767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectCommandsUnalias
680767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
681767af88aa617288e584afcfed055f7755e408542Jim Ingham
682da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectCommandsUnalias : public CommandObjectParsed
683767af88aa617288e584afcfed055f7755e408542Jim Ingham{
684767af88aa617288e584afcfed055f7755e408542Jim Inghampublic:
685238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectCommandsUnalias (CommandInterpreter &interpreter) :
686da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
68740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                       "command unalias",
688146292c83470680b98a4e4046428e0849e1ffe8fCaroline Tice                       "Allow the user to remove/delete a user-defined command abbreviation.",
68943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice                       NULL)
690767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
69143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg;
69243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData alias_arg;
69343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
69443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
69543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        alias_arg.arg_type = eArgTypeAliasName;
69643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        alias_arg.arg_repetition = eArgRepeatPlain;
69743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
69843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
69943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg.push_back (alias_arg);
70043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
70143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Push the data for the first argument into the m_arguments vector.
70243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg);
703767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
704767af88aa617288e584afcfed055f7755e408542Jim Ingham
705767af88aa617288e584afcfed055f7755e408542Jim Ingham    ~CommandObjectCommandsUnalias()
706767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
707767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
708767af88aa617288e584afcfed055f7755e408542Jim Ingham
709da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
710767af88aa617288e584afcfed055f7755e408542Jim Ingham    bool
711da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& args, CommandReturnObject &result)
712767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
713767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandObject::CommandMap::iterator pos;
714767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandObject *cmd_obj;
715767af88aa617288e584afcfed055f7755e408542Jim Ingham
716767af88aa617288e584afcfed055f7755e408542Jim Ingham        if (args.GetArgumentCount() != 0)
717767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
718767af88aa617288e584afcfed055f7755e408542Jim Ingham            const char *command_name = args.GetArgumentAtIndex(0);
719238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton            cmd_obj = m_interpreter.GetCommandObject(command_name);
720767af88aa617288e584afcfed055f7755e408542Jim Ingham            if (cmd_obj)
721767af88aa617288e584afcfed055f7755e408542Jim Ingham            {
722238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                if (m_interpreter.CommandExists (command_name))
723767af88aa617288e584afcfed055f7755e408542Jim Ingham                {
724767af88aa617288e584afcfed055f7755e408542Jim Ingham                    result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be removed.\n",
725767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                  command_name);
726767af88aa617288e584afcfed055f7755e408542Jim Ingham                    result.SetStatus (eReturnStatusFailed);
727767af88aa617288e584afcfed055f7755e408542Jim Ingham                }
728767af88aa617288e584afcfed055f7755e408542Jim Ingham                else
729767af88aa617288e584afcfed055f7755e408542Jim Ingham                {
730767af88aa617288e584afcfed055f7755e408542Jim Ingham
731238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                    if (m_interpreter.RemoveAlias (command_name) == false)
732767af88aa617288e584afcfed055f7755e408542Jim Ingham                    {
733238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                        if (m_interpreter.AliasExists (command_name))
734767af88aa617288e584afcfed055f7755e408542Jim Ingham                            result.AppendErrorWithFormat ("Error occurred while attempting to unalias '%s'.\n",
735767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                          command_name);
736767af88aa617288e584afcfed055f7755e408542Jim Ingham                        else
737767af88aa617288e584afcfed055f7755e408542Jim Ingham                            result.AppendErrorWithFormat ("'%s' is not an existing alias.\n", command_name);
738767af88aa617288e584afcfed055f7755e408542Jim Ingham                        result.SetStatus (eReturnStatusFailed);
739767af88aa617288e584afcfed055f7755e408542Jim Ingham                    }
740767af88aa617288e584afcfed055f7755e408542Jim Ingham                    else
741767af88aa617288e584afcfed055f7755e408542Jim Ingham                        result.SetStatus (eReturnStatusSuccessFinishNoResult);
742767af88aa617288e584afcfed055f7755e408542Jim Ingham                }
743767af88aa617288e584afcfed055f7755e408542Jim Ingham            }
744767af88aa617288e584afcfed055f7755e408542Jim Ingham            else
745767af88aa617288e584afcfed055f7755e408542Jim Ingham            {
746767af88aa617288e584afcfed055f7755e408542Jim Ingham                result.AppendErrorWithFormat ("'%s' is not a known command.\nTry 'help' to see a "
747767af88aa617288e584afcfed055f7755e408542Jim Ingham                                              "current list of commands.\n",
748767af88aa617288e584afcfed055f7755e408542Jim Ingham                                             command_name);
749767af88aa617288e584afcfed055f7755e408542Jim Ingham                result.SetStatus (eReturnStatusFailed);
750767af88aa617288e584afcfed055f7755e408542Jim Ingham            }
751767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
752767af88aa617288e584afcfed055f7755e408542Jim Ingham        else
753767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
754767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendError ("must call 'unalias' with a valid alias");
755767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
756767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
757767af88aa617288e584afcfed055f7755e408542Jim Ingham
758767af88aa617288e584afcfed055f7755e408542Jim Ingham        return result.Succeeded();
759767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
760767af88aa617288e584afcfed055f7755e408542Jim Ingham};
761767af88aa617288e584afcfed055f7755e408542Jim Ingham
762d12aeab33bab559e138307f599077da3918a3238Greg Clayton//-------------------------------------------------------------------------
763d12aeab33bab559e138307f599077da3918a3238Greg Clayton// CommandObjectCommandsAddRegex
764d12aeab33bab559e138307f599077da3918a3238Greg Clayton//-------------------------------------------------------------------------
765da26bd203cbb104291b39891febf7481794f205fJim Ingham#pragma mark CommandObjectCommandsAddRegex
766d12aeab33bab559e138307f599077da3918a3238Greg Clayton
767da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectCommandsAddRegex : public CommandObjectParsed
768d12aeab33bab559e138307f599077da3918a3238Greg Clayton{
769d12aeab33bab559e138307f599077da3918a3238Greg Claytonpublic:
770d12aeab33bab559e138307f599077da3918a3238Greg Clayton    CommandObjectCommandsAddRegex (CommandInterpreter &interpreter) :
771da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
77240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                       "command regex",
773d12aeab33bab559e138307f599077da3918a3238Greg Clayton                       "Allow the user to create a regular expression command.",
77440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                       "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
775d12aeab33bab559e138307f599077da3918a3238Greg Clayton        m_options (interpreter)
776d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
77740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        SetHelpLong(
77840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"This command allows the user to create powerful regular expression commands\n"
77940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"with substitutions. The regular expressions and substitutions are specified\n"
78040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"using the regular exression substitution format of:\n"
78140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"\n"
78240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"    s/<regex>/<subst>/\n"
78340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"\n"
78440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"<regex> is a regular expression that can use parenthesis to capture regular\n"
78540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"expression input and substitute the captured matches in the output using %1\n"
78640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"for the first match, %2 for the second, and so on.\n"
78740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"\n"
78840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"The regular expressions can all be specified on the command line if more than\n"
78940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"one argument is provided. If just the command name is provided on the command\n"
79040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"line, then the regular expressions and substitutions can be entered on separate\n"
79140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton" lines, followed by an empty line to terminate the command definition.\n"
79240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"\n"
79340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"EXAMPLES\n"
79440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"\n"
7957acd60a47e145f92e7e19215542aaaa37e5d74a8Sean Callanan"The following example will define a regular expression command named 'f' that\n"
79640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if\n"
79740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"a number follows 'f':\n"
7987acd60a47e145f92e7e19215542aaaa37e5d74a8Sean Callanan"\n"
7997acd60a47e145f92e7e19215542aaaa37e5d74a8Sean Callanan"    (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/'\n"
8007acd60a47e145f92e7e19215542aaaa37e5d74a8Sean Callanan"\n"
80140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    );
802d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
803d12aeab33bab559e138307f599077da3918a3238Greg Clayton
804d12aeab33bab559e138307f599077da3918a3238Greg Clayton    ~CommandObjectCommandsAddRegex()
805d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
806d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
807d12aeab33bab559e138307f599077da3918a3238Greg Clayton
808d12aeab33bab559e138307f599077da3918a3238Greg Clayton
809da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
810d12aeab33bab559e138307f599077da3918a3238Greg Clayton    bool
811da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
812d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
813da26bd203cbb104291b39891febf7481794f205fJim Ingham        const size_t argc = command.GetArgumentCount();
81440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (argc == 0)
815d12aeab33bab559e138307f599077da3918a3238Greg Clayton        {
8164049bc3c000c15a1815d2ff82c3e596e303ded0fJason Molenda            result.AppendError ("usage: 'command regex <command-name> [s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
81740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            result.SetStatus (eReturnStatusFailed);
81840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
81940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        else
82040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
82140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            Error error;
822da26bd203cbb104291b39891febf7481794f205fJim Ingham            const char *name = command.GetArgumentAtIndex(0);
823d12aeab33bab559e138307f599077da3918a3238Greg Clayton            m_regex_cmd_ap.reset (new CommandObjectRegexCommand (m_interpreter,
824d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                                 name,
825d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                                 m_options.GetHelp (),
826d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                                 m_options.GetSyntax (),
827d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                                 10));
82840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
82940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            if (argc == 1)
830d12aeab33bab559e138307f599077da3918a3238Greg Clayton            {
83140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
83240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                if (reader_sp)
83340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                {
83440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    error =reader_sp->Initialize (CommandObjectCommandsAddRegex::InputReaderCallback,
835d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                  this,                         // baton
836d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                  eInputReaderGranularityLine,  // token size, to pass to callback function
83740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                                  NULL,                         // end token
838d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                  "> ",                         // prompt
83940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                                  true);                        // echo input
84040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    if (error.Success())
84140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    {
84240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                        m_interpreter.GetDebugger().PushInputReader (reader_sp);
84340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                        result.SetStatus (eReturnStatusSuccessFinishNoResult);
84440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                        return true;
84540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    }
84640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                }
84740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            }
84840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            else
84940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            {
85040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                for (size_t arg_idx = 1; arg_idx < argc; ++arg_idx)
851d12aeab33bab559e138307f599077da3918a3238Greg Clayton                {
852da26bd203cbb104291b39891febf7481794f205fJim Ingham                    llvm::StringRef arg_strref (command.GetArgumentAtIndex(arg_idx));
85340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    error = AppendRegexSubstitution (arg_strref);
85440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    if (error.Fail())
85540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                        break;
856d12aeab33bab559e138307f599077da3918a3238Greg Clayton                }
85740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
85840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                if (error.Success())
859d12aeab33bab559e138307f599077da3918a3238Greg Clayton                {
86040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    AddRegexCommandToInterpreter();
861d12aeab33bab559e138307f599077da3918a3238Greg Clayton                }
862d12aeab33bab559e138307f599077da3918a3238Greg Clayton            }
86340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            if (error.Fail())
86440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            {
86540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                result.AppendError (error.AsCString());
86640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                result.SetStatus (eReturnStatusFailed);
86740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            }
868d12aeab33bab559e138307f599077da3918a3238Greg Clayton        }
86940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
870d12aeab33bab559e138307f599077da3918a3238Greg Clayton        return result.Succeeded();
871d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
872d12aeab33bab559e138307f599077da3918a3238Greg Clayton
87340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    Error
87440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    AppendRegexSubstitution (const llvm::StringRef &regex_sed)
875d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
87640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        Error error;
87740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
87840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (m_regex_cmd_ap.get() == NULL)
87940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
88040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("invalid regular expression command object for: '%.*s'",
88140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)regex_sed.size(),
88240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data());
88340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
88440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
88540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
88640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        size_t regex_sed_size = regex_sed.size();
88740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
88840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (regex_sed_size <= 1)
889d12aeab33bab559e138307f599077da3918a3238Greg Clayton        {
89040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("regular expression substitution string is too short: '%.*s'",
89140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)regex_sed.size(),
89240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data());
89340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
894d12aeab33bab559e138307f599077da3918a3238Greg Clayton        }
89540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
89640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (regex_sed[0] != 's')
89740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
89840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("regular expression substitution string doesn't start with 's': '%.*s'",
89940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)regex_sed.size(),
90040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data());
90140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
90240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
90340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        const size_t first_separator_char_pos = 1;
90440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        // use the char that follows 's' as the regex separator character
90540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        // so we can have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
90640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        const char separator_char = regex_sed[first_separator_char_pos];
90740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        const size_t second_separator_char_pos = regex_sed.find (separator_char, first_separator_char_pos + 1);
90840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
90940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (second_separator_char_pos == std::string::npos)
91040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
91140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("missing second '%c' separator char after '%.*s'",
91240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
91340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)(regex_sed.size() - first_separator_char_pos - 1),
91440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data() + (first_separator_char_pos + 1));
91540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
91640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
91740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
91840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        const size_t third_separator_char_pos = regex_sed.find (separator_char, second_separator_char_pos + 1);
91940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
92040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (third_separator_char_pos == std::string::npos)
92140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
92240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("missing third '%c' separator char after '%.*s'",
92340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
92440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)(regex_sed.size() - second_separator_char_pos - 1),
92540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data() + (second_separator_char_pos + 1));
92640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
92740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
92840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
92940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (third_separator_char_pos != regex_sed_size - 1)
93040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
93140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            // Make sure that everything that follows the last regex
93240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            // separator char
93340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            if (regex_sed.find_first_not_of("\t\n\v\f\r ", third_separator_char_pos + 1) != std::string::npos)
93440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            {
93540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                error.SetErrorStringWithFormat("extra data found after the '%.*s' regular expression substitution string: '%.*s'",
93640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                               (int)third_separator_char_pos + 1,
93740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                               regex_sed.data(),
93840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                               (int)(regex_sed.size() - third_separator_char_pos - 1),
93940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                               regex_sed.data() + (third_separator_char_pos + 1));
94040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                return error;
94140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            }
94240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
94340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
94440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        else if (first_separator_char_pos + 1 == second_separator_char_pos)
94540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
94640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
94740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
94840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
94940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
95040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)regex_sed.size(),
95140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data());
95240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
95340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
95440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        else if (second_separator_char_pos + 1 == third_separator_char_pos)
95540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
95640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
95740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
95840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
95940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
96040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)regex_sed.size(),
96140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data());
96240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
96340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
96440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        std::string regex(regex_sed.substr(first_separator_char_pos + 1, second_separator_char_pos - first_separator_char_pos - 1));
96540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        std::string subst(regex_sed.substr(second_separator_char_pos + 1, third_separator_char_pos - second_separator_char_pos - 1));
96640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        m_regex_cmd_ap->AddRegexCommand (regex.c_str(),
96740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                         subst.c_str());
96840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        return error;
969d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
970d12aeab33bab559e138307f599077da3918a3238Greg Clayton
971d12aeab33bab559e138307f599077da3918a3238Greg Clayton    void
97240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    AddRegexCommandToInterpreter()
973d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
974d12aeab33bab559e138307f599077da3918a3238Greg Clayton        if (m_regex_cmd_ap.get())
975d12aeab33bab559e138307f599077da3918a3238Greg Clayton        {
976d12aeab33bab559e138307f599077da3918a3238Greg Clayton            if (m_regex_cmd_ap->HasRegexEntries())
977d12aeab33bab559e138307f599077da3918a3238Greg Clayton            {
978d12aeab33bab559e138307f599077da3918a3238Greg Clayton                CommandObjectSP cmd_sp (m_regex_cmd_ap.release());
979d12aeab33bab559e138307f599077da3918a3238Greg Clayton                m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
980d12aeab33bab559e138307f599077da3918a3238Greg Clayton            }
981d12aeab33bab559e138307f599077da3918a3238Greg Clayton        }
982d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
983d12aeab33bab559e138307f599077da3918a3238Greg Clayton
98440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    void
98540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    InputReaderDidCancel()
98640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    {
98740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        m_regex_cmd_ap.reset();
98840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    }
98940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
990d12aeab33bab559e138307f599077da3918a3238Greg Clayton    static size_t
991d12aeab33bab559e138307f599077da3918a3238Greg Clayton    InputReaderCallback (void *baton,
992d12aeab33bab559e138307f599077da3918a3238Greg Clayton                         InputReader &reader,
993d12aeab33bab559e138307f599077da3918a3238Greg Clayton                         lldb::InputReaderAction notification,
994d12aeab33bab559e138307f599077da3918a3238Greg Clayton                         const char *bytes,
995da26bd203cbb104291b39891febf7481794f205fJim Ingham                         size_t bytes_len)
996da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
997da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectCommandsAddRegex *add_regex_cmd = (CommandObjectCommandsAddRegex *) baton;
998da26bd203cbb104291b39891febf7481794f205fJim Ingham        bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
999da26bd203cbb104291b39891febf7481794f205fJim Ingham
1000da26bd203cbb104291b39891febf7481794f205fJim Ingham        switch (notification)
1001da26bd203cbb104291b39891febf7481794f205fJim Ingham        {
1002da26bd203cbb104291b39891febf7481794f205fJim Ingham            case eInputReaderActivate:
1003da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (!batch_mode)
1004da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
1005da26bd203cbb104291b39891febf7481794f205fJim Ingham                    StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream ();
1006da26bd203cbb104291b39891febf7481794f205fJim Ingham                    out_stream->Printf("%s\n", "Enter regular expressions in the form 's/<regex>/<subst>/' and terminate with an empty line:");
1007da26bd203cbb104291b39891febf7481794f205fJim Ingham                    out_stream->Flush();
1008da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
1009da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
1010da26bd203cbb104291b39891febf7481794f205fJim Ingham            case eInputReaderReactivate:
1011da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
1012da26bd203cbb104291b39891febf7481794f205fJim Ingham
1013da26bd203cbb104291b39891febf7481794f205fJim Ingham            case eInputReaderDeactivate:
1014da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
1015da26bd203cbb104291b39891febf7481794f205fJim Ingham
1016da26bd203cbb104291b39891febf7481794f205fJim Ingham            case eInputReaderAsynchronousOutputWritten:
1017da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
1018da26bd203cbb104291b39891febf7481794f205fJim Ingham
1019da26bd203cbb104291b39891febf7481794f205fJim Ingham            case eInputReaderGotToken:
1020da26bd203cbb104291b39891febf7481794f205fJim Ingham                while (bytes_len > 0 && (bytes[bytes_len-1] == '\r' || bytes[bytes_len-1] == '\n'))
1021da26bd203cbb104291b39891febf7481794f205fJim Ingham                    --bytes_len;
1022da26bd203cbb104291b39891febf7481794f205fJim Ingham                if (bytes_len == 0)
1023da26bd203cbb104291b39891febf7481794f205fJim Ingham                    reader.SetIsDone(true);
1024da26bd203cbb104291b39891febf7481794f205fJim Ingham                else if (bytes)
1025da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
1026da26bd203cbb104291b39891febf7481794f205fJim Ingham                    llvm::StringRef bytes_strref (bytes, bytes_len);
1027da26bd203cbb104291b39891febf7481794f205fJim Ingham                    Error error (add_regex_cmd->AppendRegexSubstitution (bytes_strref));
1028da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (error.Fail())
1029da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
1030da26bd203cbb104291b39891febf7481794f205fJim Ingham                        if (!batch_mode)
1031da26bd203cbb104291b39891febf7481794f205fJim Ingham                        {
1032da26bd203cbb104291b39891febf7481794f205fJim Ingham                            StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1033da26bd203cbb104291b39891febf7481794f205fJim Ingham                            out_stream->Printf("error: %s\n", error.AsCString());
1034da26bd203cbb104291b39891febf7481794f205fJim Ingham                            out_stream->Flush();
1035da26bd203cbb104291b39891febf7481794f205fJim Ingham                        }
1036da26bd203cbb104291b39891febf7481794f205fJim Ingham                        add_regex_cmd->InputReaderDidCancel ();
1037da26bd203cbb104291b39891febf7481794f205fJim Ingham                        reader.SetIsDone (true);
1038da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
1039da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
1040da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
1041da26bd203cbb104291b39891febf7481794f205fJim Ingham
1042da26bd203cbb104291b39891febf7481794f205fJim Ingham            case eInputReaderInterrupt:
1043da26bd203cbb104291b39891febf7481794f205fJim Ingham                {
1044da26bd203cbb104291b39891febf7481794f205fJim Ingham                    reader.SetIsDone (true);
1045da26bd203cbb104291b39891febf7481794f205fJim Ingham                    if (!batch_mode)
1046da26bd203cbb104291b39891febf7481794f205fJim Ingham                    {
1047da26bd203cbb104291b39891febf7481794f205fJim Ingham                        StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1048da26bd203cbb104291b39891febf7481794f205fJim Ingham                        out_stream->PutCString("Regular expression command creations was cancelled.\n");
1049da26bd203cbb104291b39891febf7481794f205fJim Ingham                        out_stream->Flush();
1050da26bd203cbb104291b39891febf7481794f205fJim Ingham                    }
1051da26bd203cbb104291b39891febf7481794f205fJim Ingham                    add_regex_cmd->InputReaderDidCancel ();
1052da26bd203cbb104291b39891febf7481794f205fJim Ingham                }
1053da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
1054da26bd203cbb104291b39891febf7481794f205fJim Ingham
1055da26bd203cbb104291b39891febf7481794f205fJim Ingham            case eInputReaderEndOfFile:
1056da26bd203cbb104291b39891febf7481794f205fJim Ingham                reader.SetIsDone (true);
1057da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
1058da26bd203cbb104291b39891febf7481794f205fJim Ingham
1059da26bd203cbb104291b39891febf7481794f205fJim Ingham            case eInputReaderDone:
1060da26bd203cbb104291b39891febf7481794f205fJim Ingham                add_regex_cmd->AddRegexCommandToInterpreter();
1061da26bd203cbb104291b39891febf7481794f205fJim Ingham                break;
1062da26bd203cbb104291b39891febf7481794f205fJim Ingham        }
1063da26bd203cbb104291b39891febf7481794f205fJim Ingham
1064da26bd203cbb104291b39891febf7481794f205fJim Ingham        return bytes_len;
1065da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
1066da26bd203cbb104291b39891febf7481794f205fJim Ingham
1067d12aeab33bab559e138307f599077da3918a3238Greg Claytonprivate:
1068d12aeab33bab559e138307f599077da3918a3238Greg Clayton    std::auto_ptr<CommandObjectRegexCommand> m_regex_cmd_ap;
1069d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1070d12aeab33bab559e138307f599077da3918a3238Greg Clayton     class CommandOptions : public Options
1071d12aeab33bab559e138307f599077da3918a3238Greg Clayton     {
1072d12aeab33bab559e138307f599077da3918a3238Greg Clayton     public:
1073d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1074d12aeab33bab559e138307f599077da3918a3238Greg Clayton         CommandOptions (CommandInterpreter &interpreter) :
1075d12aeab33bab559e138307f599077da3918a3238Greg Clayton            Options (interpreter)
1076d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
1077d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1078d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1079d12aeab33bab559e138307f599077da3918a3238Greg Clayton         virtual
1080d12aeab33bab559e138307f599077da3918a3238Greg Clayton         ~CommandOptions (){}
1081d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1082d12aeab33bab559e138307f599077da3918a3238Greg Clayton         virtual Error
1083d12aeab33bab559e138307f599077da3918a3238Greg Clayton         SetOptionValue (uint32_t option_idx, const char *option_arg)
1084d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
1085d12aeab33bab559e138307f599077da3918a3238Greg Clayton             Error error;
1086d12aeab33bab559e138307f599077da3918a3238Greg Clayton             char short_option = (char) m_getopt_table[option_idx].val;
1087d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1088d12aeab33bab559e138307f599077da3918a3238Greg Clayton             switch (short_option)
1089d12aeab33bab559e138307f599077da3918a3238Greg Clayton             {
1090d12aeab33bab559e138307f599077da3918a3238Greg Clayton                 case 'h':
1091d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     m_help.assign (option_arg);
1092d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     break;
1093d12aeab33bab559e138307f599077da3918a3238Greg Clayton                 case 's':
1094d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     m_syntax.assign (option_arg);
1095d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     break;
1096d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1097d12aeab33bab559e138307f599077da3918a3238Greg Clayton                 default:
10989c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                     error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1099d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     break;
1100d12aeab33bab559e138307f599077da3918a3238Greg Clayton             }
1101d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1102d12aeab33bab559e138307f599077da3918a3238Greg Clayton             return error;
1103d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1104d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1105d12aeab33bab559e138307f599077da3918a3238Greg Clayton         void
1106d12aeab33bab559e138307f599077da3918a3238Greg Clayton         OptionParsingStarting ()
1107d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
1108d12aeab33bab559e138307f599077da3918a3238Greg Clayton             m_help.clear();
1109d12aeab33bab559e138307f599077da3918a3238Greg Clayton             m_syntax.clear();
1110d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1111d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1112d12aeab33bab559e138307f599077da3918a3238Greg Clayton         const OptionDefinition*
1113d12aeab33bab559e138307f599077da3918a3238Greg Clayton         GetDefinitions ()
1114d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
1115d12aeab33bab559e138307f599077da3918a3238Greg Clayton             return g_option_table;
1116d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1117d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1118d12aeab33bab559e138307f599077da3918a3238Greg Clayton         // Options table: Required for subclasses of Options.
1119d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1120d12aeab33bab559e138307f599077da3918a3238Greg Clayton         static OptionDefinition g_option_table[];
1121d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1122d12aeab33bab559e138307f599077da3918a3238Greg Clayton         const char *
1123d12aeab33bab559e138307f599077da3918a3238Greg Clayton         GetHelp ()
1124d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
1125d12aeab33bab559e138307f599077da3918a3238Greg Clayton             if (m_help.empty())
1126d12aeab33bab559e138307f599077da3918a3238Greg Clayton                 return NULL;
1127d12aeab33bab559e138307f599077da3918a3238Greg Clayton             return m_help.c_str();
1128d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1129d12aeab33bab559e138307f599077da3918a3238Greg Clayton         const char *
1130d12aeab33bab559e138307f599077da3918a3238Greg Clayton         GetSyntax ()
1131d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
1132d12aeab33bab559e138307f599077da3918a3238Greg Clayton             if (m_syntax.empty())
1133d12aeab33bab559e138307f599077da3918a3238Greg Clayton                 return NULL;
1134d12aeab33bab559e138307f599077da3918a3238Greg Clayton             return m_syntax.c_str();
1135d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1136d12aeab33bab559e138307f599077da3918a3238Greg Clayton         // Instance variables to hold the values for command options.
1137d12aeab33bab559e138307f599077da3918a3238Greg Clayton     protected:
1138d12aeab33bab559e138307f599077da3918a3238Greg Clayton         std::string m_help;
1139d12aeab33bab559e138307f599077da3918a3238Greg Clayton         std::string m_syntax;
1140d12aeab33bab559e138307f599077da3918a3238Greg Clayton     };
1141da26bd203cbb104291b39891febf7481794f205fJim Ingham
1142d12aeab33bab559e138307f599077da3918a3238Greg Clayton     virtual Options *
1143d12aeab33bab559e138307f599077da3918a3238Greg Clayton     GetOptions ()
1144d12aeab33bab559e138307f599077da3918a3238Greg Clayton     {
1145d12aeab33bab559e138307f599077da3918a3238Greg Clayton         return &m_options;
1146d12aeab33bab559e138307f599077da3918a3238Greg Clayton     }
1147da26bd203cbb104291b39891febf7481794f205fJim Ingham
1148da26bd203cbb104291b39891febf7481794f205fJim Ingham     CommandOptions m_options;
1149d12aeab33bab559e138307f599077da3918a3238Greg Clayton};
1150d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1151d12aeab33bab559e138307f599077da3918a3238Greg ClaytonOptionDefinition
1152d12aeab33bab559e138307f599077da3918a3238Greg ClaytonCommandObjectCommandsAddRegex::CommandOptions::g_option_table[] =
1153d12aeab33bab559e138307f599077da3918a3238Greg Clayton{
115440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton{ LLDB_OPT_SET_1, false, "help"  , 'h', required_argument, NULL, 0, eArgTypeNone, "The help text to display for this command."},
1155d12aeab33bab559e138307f599077da3918a3238Greg Clayton{ LLDB_OPT_SET_1, false, "syntax", 's', required_argument, NULL, 0, eArgTypeNone, "A syntax string showing the typical usage syntax."},
115640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton{ 0             , false,  NULL   , 0  , 0                , NULL, 0, eArgTypeNone, NULL }
1157d12aeab33bab559e138307f599077da3918a3238Greg Clayton};
1158d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1159d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1160da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectPythonFunction : public CommandObjectRaw
11616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
11626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataprivate:
11636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    std::string m_function_name;
11646010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    ScriptedCommandSynchronicity m_synchro;
11655b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata    bool m_fetched_help_long;
11666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
11676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
11686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
11696b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectPythonFunction (CommandInterpreter &interpreter,
11706b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                 std::string name,
11716010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                                 std::string funct,
11726010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                                 ScriptedCommandSynchronicity synch) :
1173da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectRaw (interpreter,
1174da26bd203cbb104291b39891febf7481794f205fJim Ingham                          name.c_str(),
1175da26bd203cbb104291b39891febf7481794f205fJim Ingham                          (std::string("Run Python function ") + funct).c_str(),
1176da26bd203cbb104291b39891febf7481794f205fJim Ingham                          NULL),
1177da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_function_name(funct),
11785b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata        m_synchro(synch),
11795b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata        m_fetched_help_long(false)
11806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
11816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
11826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
11836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    virtual
11846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectPythonFunction ()
11856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
11866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
11876b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
11886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    virtual bool
1189f737d372a9672c9feaedf4b2cd7b16e31357d38eGreg Clayton    IsRemovable () const
1190da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
1191da26bd203cbb104291b39891febf7481794f205fJim Ingham        return true;
1192da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
1193da26bd203cbb104291b39891febf7481794f205fJim Ingham
1194da26bd203cbb104291b39891febf7481794f205fJim Ingham    const std::string&
1195da26bd203cbb104291b39891febf7481794f205fJim Ingham    GetFunctionName ()
1196da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
1197da26bd203cbb104291b39891febf7481794f205fJim Ingham        return m_function_name;
1198da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
1199da26bd203cbb104291b39891febf7481794f205fJim Ingham
1200da26bd203cbb104291b39891febf7481794f205fJim Ingham    ScriptedCommandSynchronicity
1201da26bd203cbb104291b39891febf7481794f205fJim Ingham    GetSynchronicity ()
1202da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
1203da26bd203cbb104291b39891febf7481794f205fJim Ingham        return m_synchro;
1204da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
1205da26bd203cbb104291b39891febf7481794f205fJim Ingham
12065b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata    virtual const char *
12075b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata    GetHelpLong ()
12085b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata    {
12095b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata        if (!m_fetched_help_long)
12105b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata        {
12115b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata            ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
12125b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata            if (scripter)
12135b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata            {
12145b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata                std::string docstring;
12155b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata                m_fetched_help_long = scripter->GetDocumentationForItem(m_function_name.c_str(),docstring);
12165b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata                if (!docstring.empty())
12175b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata                    SetHelpLong(docstring);
12185b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata            }
12195b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata        }
12205b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata        return CommandObjectRaw::GetHelpLong();
12215b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata    }
12225b9f77786d1e9fd356e1fe2d18ce6fcd74977c98Enrico Granata
1223da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
1224da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual bool
1225da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (const char *raw_command_line, CommandReturnObject &result)
12266b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
12276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
12286b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12296b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        Error error;
12306b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1231839de396873071d193ed76b0206686af41540962Jim Ingham        result.SetStatus(eReturnStatusInvalid);
1232839de396873071d193ed76b0206686af41540962Jim Ingham
12336b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        if (!scripter || scripter->RunScriptBasedCommand(m_function_name.c_str(),
12346b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                         raw_command_line,
12356010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                                                         m_synchro,
12366b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                         result,
12376b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                         error) == false)
12386b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
12396b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.AppendError(error.AsCString());
12406b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus(eReturnStatusFailed);
12416b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
12426b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        else
1243839de396873071d193ed76b0206686af41540962Jim Ingham        {
1244839de396873071d193ed76b0206686af41540962Jim Ingham            // Don't change the status if the command already set it...
1245839de396873071d193ed76b0206686af41540962Jim Ingham            if (result.GetStatus() == eReturnStatusInvalid)
1246839de396873071d193ed76b0206686af41540962Jim Ingham            {
1247839de396873071d193ed76b0206686af41540962Jim Ingham                if (result.GetOutputData() == NULL || result.GetOutputData()[0] == '\0')
1248839de396873071d193ed76b0206686af41540962Jim Ingham                    result.SetStatus(eReturnStatusSuccessFinishNoResult);
1249839de396873071d193ed76b0206686af41540962Jim Ingham                else
1250839de396873071d193ed76b0206686af41540962Jim Ingham                    result.SetStatus(eReturnStatusSuccessFinishResult);
1251839de396873071d193ed76b0206686af41540962Jim Ingham            }
1252839de396873071d193ed76b0206686af41540962Jim Ingham        }
12536b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12546b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return result.Succeeded();
12556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
12566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1257da26bd203cbb104291b39891febf7481794f205fJim Ingham};
12586010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
1259da26bd203cbb104291b39891febf7481794f205fJim Ingham//-------------------------------------------------------------------------
1260da26bd203cbb104291b39891febf7481794f205fJim Ingham// CommandObjectCommandsScriptImport
1261da26bd203cbb104291b39891febf7481794f205fJim Ingham//-------------------------------------------------------------------------
12626010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
1263da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectCommandsScriptImport : public CommandObjectParsed
1264da26bd203cbb104291b39891febf7481794f205fJim Ingham{
1265da26bd203cbb104291b39891febf7481794f205fJim Inghampublic:
1266da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectCommandsScriptImport (CommandInterpreter &interpreter) :
1267da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
1268da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "command script import",
1269da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Import a scripting module in LLDB.",
1270da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL),
1271da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_options(interpreter)
12726010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    {
1273da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentEntry arg1;
1274da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentData cmd_arg;
1275da26bd203cbb104291b39891febf7481794f205fJim Ingham
1276da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Define the first (and only) variant of this arg.
1277da26bd203cbb104291b39891febf7481794f205fJim Ingham        cmd_arg.arg_type = eArgTypeFilename;
1278da26bd203cbb104291b39891febf7481794f205fJim Ingham        cmd_arg.arg_repetition = eArgRepeatPlain;
1279da26bd203cbb104291b39891febf7481794f205fJim Ingham
1280da26bd203cbb104291b39891febf7481794f205fJim Ingham        // There is only one variant this argument could be; put it into the argument entry.
1281da26bd203cbb104291b39891febf7481794f205fJim Ingham        arg1.push_back (cmd_arg);
1282da26bd203cbb104291b39891febf7481794f205fJim Ingham
1283da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Push the data for the first argument into the m_arguments vector.
1284da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_arguments.push_back (arg1);
12856010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    }
1286da26bd203cbb104291b39891febf7481794f205fJim Ingham
1287da26bd203cbb104291b39891febf7481794f205fJim Ingham    ~CommandObjectCommandsScriptImport ()
12886010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    {
12896010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    }
1290da26bd203cbb104291b39891febf7481794f205fJim Ingham
1291da26bd203cbb104291b39891febf7481794f205fJim Ingham    int
1292da26bd203cbb104291b39891febf7481794f205fJim Ingham    HandleArgumentCompletion (Args &input,
1293da26bd203cbb104291b39891febf7481794f205fJim Ingham                              int &cursor_index,
1294da26bd203cbb104291b39891febf7481794f205fJim Ingham                              int &cursor_char_position,
1295da26bd203cbb104291b39891febf7481794f205fJim Ingham                              OptionElementVector &opt_element_vector,
1296da26bd203cbb104291b39891febf7481794f205fJim Ingham                              int match_start_point,
1297da26bd203cbb104291b39891febf7481794f205fJim Ingham                              int max_return_elements,
1298da26bd203cbb104291b39891febf7481794f205fJim Ingham                              bool &word_complete,
1299da26bd203cbb104291b39891febf7481794f205fJim Ingham                              StringList &matches)
13006010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    {
1301da26bd203cbb104291b39891febf7481794f205fJim Ingham        std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1302da26bd203cbb104291b39891febf7481794f205fJim Ingham        completion_str.erase (cursor_char_position);
1303da26bd203cbb104291b39891febf7481794f205fJim Ingham
1304da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1305da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             CommandCompletions::eDiskFileCompletion,
1306da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             completion_str.c_str(),
1307da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             match_start_point,
1308da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             max_return_elements,
1309da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             NULL,
1310da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             word_complete,
1311da26bd203cbb104291b39891febf7481794f205fJim Ingham                                                             matches);
1312da26bd203cbb104291b39891febf7481794f205fJim Ingham        return matches.GetSize();
13136010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    }
13146b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1315da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual Options *
1316da26bd203cbb104291b39891febf7481794f205fJim Ingham    GetOptions ()
1317da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
1318da26bd203cbb104291b39891febf7481794f205fJim Ingham        return &m_options;
1319da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
132059df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata
1321da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
13226010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13236010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    class CommandOptions : public Options
13246010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    {
13256010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    public:
13266010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13276010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        CommandOptions (CommandInterpreter &interpreter) :
1328da26bd203cbb104291b39891febf7481794f205fJim Ingham            Options (interpreter)
13296010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        {
13306010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        }
13316010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13326010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        virtual
13336010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        ~CommandOptions (){}
13346010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13356010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        virtual Error
13366010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        SetOptionValue (uint32_t option_idx, const char *option_arg)
13376010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        {
13386010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            Error error;
13396010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            char short_option = (char) m_getopt_table[option_idx].val;
13406010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13416010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            switch (short_option)
13426010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            {
13436010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                case 'r':
13446010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                    m_allow_reload = true;
13456010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                    break;
13466010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                default:
13476010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                    error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
13486010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                    break;
13496010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            }
13506010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13516010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            return error;
13526010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        }
13536010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13546010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        void
13556010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        OptionParsingStarting ()
13566010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        {
13576010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            m_allow_reload = false;
13586010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        }
13596010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13606010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        const OptionDefinition*
13616010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        GetDefinitions ()
13626010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        {
13636010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            return g_option_table;
13646010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        }
13656010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13666010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        // Options table: Required for subclasses of Options.
13676010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13686010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        static OptionDefinition g_option_table[];
13696010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13706010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        // Instance variables to hold the values for command options.
13716010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
13726010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        bool m_allow_reload;
13736010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    };
13746010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
137559df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata    bool
1376da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
137759df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata    {
137859df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata
137959df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        if (m_interpreter.GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython)
138059df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        {
138159df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata            result.AppendError ("only scripting language supported for module importing is currently Python");
138259df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata            result.SetStatus (eReturnStatusFailed);
138359df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata            return false;
138459df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        }
138559df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata
1386da26bd203cbb104291b39891febf7481794f205fJim Ingham        size_t argc = command.GetArgumentCount();
138759df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata
138859df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        if (argc != 1)
138959df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        {
139059df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata            result.AppendError ("'command script import' requires one argument");
139159df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata            result.SetStatus (eReturnStatusFailed);
139259df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata            return false;
139359df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        }
139459df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata
1395da26bd203cbb104291b39891febf7481794f205fJim Ingham        std::string path = command.GetArgumentAtIndex(0);
139659df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        Error error;
139759df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata
139859df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule(path.c_str(),
13996010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                                                                      m_options.m_allow_reload,
140059df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata                                                                      error))
140159df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        {
140259df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata            result.SetStatus (eReturnStatusSuccessFinishNoResult);
140359df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        }
140459df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        else
140559df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        {
140659df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata            result.AppendErrorWithFormat("module importing failed: %s", error.AsCString());
140759df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata            result.SetStatus (eReturnStatusFailed);
140859df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        }
140959df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata
141059df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        return result.Succeeded();
141159df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata    }
14126010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
1413da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandOptions m_options;
14146010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata};
14156010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
14166010acef0cee54e044d4e7e472ef3d99e13c5045Enrico GranataOptionDefinition
14176010acef0cee54e044d4e7e472ef3d99e13c5045Enrico GranataCommandObjectCommandsScriptImport::CommandOptions::g_option_table[] =
14186010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata{
14196010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    { LLDB_OPT_SET_1, false, "allow-reload", 'r', no_argument, NULL, 0, eArgTypeNone,        "Allow the script to be loaded even if it was already loaded before (for Python, the __lldb_init_module function will be called again, but the module will not be reloaded from disk)."},
14206010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
142159df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata};
14226b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14236010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
14246b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
14256b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata// CommandObjectCommandsScriptAdd
14266b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
14276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1428da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectCommandsScriptAdd : public CommandObjectParsed
14296b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
1430da26bd203cbb104291b39891febf7481794f205fJim Inghampublic:
1431da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter) :
1432da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
1433da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "command script add",
1434da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Add a scripted function as an LLDB command.",
1435da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL),
1436da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_options (interpreter)
1437da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
1438da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentEntry arg1;
1439da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandArgumentData cmd_arg;
1440da26bd203cbb104291b39891febf7481794f205fJim Ingham
1441da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Define the first (and only) variant of this arg.
1442da26bd203cbb104291b39891febf7481794f205fJim Ingham        cmd_arg.arg_type = eArgTypeCommandName;
1443da26bd203cbb104291b39891febf7481794f205fJim Ingham        cmd_arg.arg_repetition = eArgRepeatPlain;
1444da26bd203cbb104291b39891febf7481794f205fJim Ingham
1445da26bd203cbb104291b39891febf7481794f205fJim Ingham        // There is only one variant this argument could be; put it into the argument entry.
1446da26bd203cbb104291b39891febf7481794f205fJim Ingham        arg1.push_back (cmd_arg);
1447da26bd203cbb104291b39891febf7481794f205fJim Ingham
1448da26bd203cbb104291b39891febf7481794f205fJim Ingham        // Push the data for the first argument into the m_arguments vector.
1449da26bd203cbb104291b39891febf7481794f205fJim Ingham        m_arguments.push_back (arg1);
1450da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
1451da26bd203cbb104291b39891febf7481794f205fJim Ingham
1452da26bd203cbb104291b39891febf7481794f205fJim Ingham    ~CommandObjectCommandsScriptAdd ()
1453da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
1454da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
1455da26bd203cbb104291b39891febf7481794f205fJim Ingham
1456da26bd203cbb104291b39891febf7481794f205fJim Ingham    virtual Options *
1457da26bd203cbb104291b39891febf7481794f205fJim Ingham    GetOptions ()
1458da26bd203cbb104291b39891febf7481794f205fJim Ingham    {
1459da26bd203cbb104291b39891febf7481794f205fJim Ingham        return &m_options;
1460da26bd203cbb104291b39891febf7481794f205fJim Ingham    }
1461da26bd203cbb104291b39891febf7481794f205fJim Ingham
1462da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
14636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    class CommandOptions : public Options
14656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
14666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    public:
14676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        CommandOptions (CommandInterpreter &interpreter) :
14696b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        Options (interpreter)
14706b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
14716b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
14726b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14736b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual
14746b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        ~CommandOptions (){}
14756b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual Error
14776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        SetOptionValue (uint32_t option_idx, const char *option_arg)
14786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
14796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            Error error;
14806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            char short_option = (char) m_getopt_table[option_idx].val;
14816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            switch (short_option)
14836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
14846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                case 'f':
14856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    m_funct_name = std::string(option_arg);
14866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    break;
14876010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                case 's':
14886010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                    m_synchronous = (ScriptedCommandSynchronicity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error);
14896010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                    if (!error.Success())
14906010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                        error.SetErrorStringWithFormat ("unrecognized value for synchronicity '%s'", option_arg);
14916010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                    break;
14926b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                default:
14939c236733d43e6250c8a5671a438f4a2afeb9c0b2Greg Clayton                    error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
14946b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    break;
14956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
14966b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14976b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            return error;
14986b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
14996b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15006b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        void
15016b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        OptionParsingStarting ()
15026b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
15036b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            m_funct_name = "";
15046010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            m_synchronous = eScriptedCommandSynchronicitySynchronous;
15056b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
15066b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15076b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        const OptionDefinition*
15086b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        GetDefinitions ()
15096b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
15106b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            return g_option_table;
15116b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
15126b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15136b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // Options table: Required for subclasses of Options.
15146b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15156b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        static OptionDefinition g_option_table[];
15166b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15176b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // Instance variables to hold the values for command options.
15186b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15196b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        std::string m_funct_name;
15206010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        ScriptedCommandSynchronicity m_synchronous;
15216b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    };
1522da26bd203cbb104291b39891febf7481794f205fJim Ingham
1523da26bd203cbb104291b39891febf7481794f205fJim Inghamprivate:
15246b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    class PythonAliasReader : public InputReaderEZ
15256b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
15266b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    private:
15276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        CommandInterpreter& m_interpreter;
15286b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        std::string m_cmd_name;
15296010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        ScriptedCommandSynchronicity m_synchronous;
15306b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        StringList m_user_input;
15316b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        DISALLOW_COPY_AND_ASSIGN (PythonAliasReader);
15326b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    public:
15336b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        PythonAliasReader(Debugger& debugger,
15346b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                          CommandInterpreter& interpreter,
15356010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                          std::string cmd_name,
15366010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                          ScriptedCommandSynchronicity synch) :
15376b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        InputReaderEZ(debugger),
15386b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_interpreter(interpreter),
15396b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_cmd_name(cmd_name),
15406010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata        m_synchronous(synch),
15416b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_user_input()
15426b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {}
15436b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15446b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual
15456b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        ~PythonAliasReader()
15466b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
15476b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
15486b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15496b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void ActivateHandler(HandlerData& data)
15506b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
15516b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StreamSP out_stream = data.GetOutStream();
15526b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            bool batch_mode = data.GetBatchMode();
15536b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!batch_mode)
15546b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
15556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("%s\n", g_python_command_instructions);
15566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                if (data.reader.GetPrompt())
15576b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    out_stream->Printf ("%s", data.reader.GetPrompt());
15586b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
15596b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
15606b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
15616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void ReactivateHandler(HandlerData& data)
15636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
15646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StreamSP out_stream = data.GetOutStream();
15656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            bool batch_mode = data.GetBatchMode();
15666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (data.reader.GetPrompt() && !batch_mode)
15676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
15686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("%s", data.reader.GetPrompt());
15696b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
15706b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
15716b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
15726b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void GotTokenHandler(HandlerData& data)
15736b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
15746b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StreamSP out_stream = data.GetOutStream();
15756b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            bool batch_mode = data.GetBatchMode();
15766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (data.bytes && data.bytes_len)
15776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
15786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                m_user_input.AppendString(data.bytes, data.bytes_len);
15796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
15806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!data.reader.IsDone() && data.reader.GetPrompt() && !batch_mode)
15816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
15826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("%s", data.reader.GetPrompt());
15836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
15846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
15856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
15866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void InterruptHandler(HandlerData& data)
15876b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
15886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StreamSP out_stream = data.GetOutStream();
15896b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            bool batch_mode = data.GetBatchMode();
15906b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            data.reader.SetIsDone (true);
15916b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!batch_mode)
15926b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
15936010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                out_stream->Printf ("Warning: No script attached.\n");
15946b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
15956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
15966b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
15976b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void EOFHandler(HandlerData& data)
15986b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
15996b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            data.reader.SetIsDone (true);
16006b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
16016b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void DoneHandler(HandlerData& data)
16026b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
16036b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StreamSP out_stream = data.GetOutStream();
16046b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16056b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            ScriptInterpreter *interpreter = data.reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
16066b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!interpreter)
16076b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
16086010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                out_stream->Printf ("Script interpreter missing: no script attached.\n");
16096b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
16106b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                return;
16116b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
1612400105dd92731a0c8968d6296b0b7279b483ebf0Enrico Granata            std::string funct_name_str;
16136b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!interpreter->GenerateScriptAliasFunction (m_user_input,
1614400105dd92731a0c8968d6296b0b7279b483ebf0Enrico Granata                                                           funct_name_str))
16156b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
16166010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                out_stream->Printf ("Unable to create function: no script attached.\n");
16176b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
16186b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                return;
16196b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
1620400105dd92731a0c8968d6296b0b7279b483ebf0Enrico Granata            if (funct_name_str.empty())
16216b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
16226010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                out_stream->Printf ("Unable to obtain a function name: no script attached.\n");
16236b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
16246b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                return;
16256b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
16266b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            // everything should be fine now, let's add this alias
16276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16286b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(m_interpreter,
16296b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                                           m_cmd_name,
1630400105dd92731a0c8968d6296b0b7279b483ebf0Enrico Granata                                                                           funct_name_str.c_str(),
16316010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                                                                           m_synchronous));
16326b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16336010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp, true))
16346b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
16356010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                out_stream->Printf ("Unable to add selected command: no script attached.\n");
16366b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
16376b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                return;
16386b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
16396b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
16406b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    };
16416b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1642da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
16436b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    bool
1644da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
16456b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
1646e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata
1647e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata        if (m_interpreter.GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython)
1648e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata        {
1649e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata            result.AppendError ("only scripting language supported for scripted commands is currently Python");
1650e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata            result.SetStatus (eReturnStatusFailed);
1651e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata            return false;
1652e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata        }
1653e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata
1654da26bd203cbb104291b39891febf7481794f205fJim Ingham        size_t argc = command.GetArgumentCount();
16556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        if (argc != 1)
16576b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
16586b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.AppendError ("'command script add' requires one argument");
16596b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus (eReturnStatusFailed);
16606b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            return false;
16616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
16626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1663da26bd203cbb104291b39891febf7481794f205fJim Ingham        std::string cmd_name = command.GetArgumentAtIndex(0);
16646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        if (m_options.m_funct_name.empty())
16666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
16676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            InputReaderSP reader_sp (new PythonAliasReader (m_interpreter.GetDebugger(),
16686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                            m_interpreter,
16696010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                                                            cmd_name,
16706010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                                                            m_options.m_synchronous));
16716b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16726b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (reader_sp)
16736b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
16746b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16756b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                InputReaderEZ::InitializationParameters ipr;
16766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                Error err (reader_sp->Initialize (ipr.SetBaton(NULL).SetPrompt("     ")));
16786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                if (err.Success())
16796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                {
16806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    m_interpreter.GetDebugger().PushInputReader (reader_sp);
16816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    result.SetStatus (eReturnStatusSuccessFinishNoResult);
16826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                }
16836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                else
16846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                {
16856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    result.AppendError (err.AsCString());
16866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    result.SetStatus (eReturnStatusFailed);
16876b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                }
16886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
16896b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            else
16906b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
16916b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                result.AppendError("out of memory");
16926b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                result.SetStatus (eReturnStatusFailed);
16936b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
16946b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
16956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        else
16966b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
16976010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            CommandObjectSP new_cmd(new CommandObjectPythonFunction(m_interpreter,
16986010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                                                                    cmd_name,
16996010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                                                                    m_options.m_funct_name,
17006010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata                                                                    m_options.m_synchronous));
17016010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata            if (m_interpreter.AddUserCommand(cmd_name, new_cmd, true))
17026b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
17036b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                result.SetStatus (eReturnStatusSuccessFinishNoResult);
17046b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
17056b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            else
17066b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
17076b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                result.AppendError("cannot add command");
17086b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                result.SetStatus (eReturnStatusFailed);
17096b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
17106b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
17116b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17126b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return result.Succeeded();
17136b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17146b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
1715da26bd203cbb104291b39891febf7481794f205fJim Ingham
1716da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandOptions m_options;
17176b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
17186b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17196010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granatastatic OptionEnumValueElement g_script_synchro_type[] =
17206010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata{
17216010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    { eScriptedCommandSynchronicitySynchronous,      "synchronous",       "Run synchronous"},
17226010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    { eScriptedCommandSynchronicityAsynchronous,     "asynchronous",      "Run asynchronous"},
17236010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    { eScriptedCommandSynchronicityCurrentValue,     "current",           "Do not alter current setting"},
17246010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    { 0, NULL, NULL }
17256010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata};
17266010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
17276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico GranataOptionDefinition
17286b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico GranataCommandObjectCommandsScriptAdd::CommandOptions::g_option_table[] =
17296b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
1730915448044bac6fdac22a33cc46697dcb771a8df2Enrico Granata    { LLDB_OPT_SET_1, false, "function", 'f', required_argument, NULL, 0, eArgTypePythonFunction,        "Name of the Python function to bind to this command name."},
17316010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata    { LLDB_OPT_SET_1, false, "synchronicity", 's', required_argument, g_script_synchro_type, 0, eArgTypeScriptedCommandSynchronicity,        "Set the synchronicity of this command's executions with regard to LLDB event system."},
17326b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
17336b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
17346b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17356b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
17366b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata// CommandObjectCommandsScriptList
17376b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
17386b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1739da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectCommandsScriptList : public CommandObjectParsed
17406b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
17416b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataprivate:
17426010acef0cee54e044d4e7e472ef3d99e13c5045Enrico Granata
17436b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
17446b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectCommandsScriptList(CommandInterpreter &interpreter) :
1745da26bd203cbb104291b39891febf7481794f205fJim Ingham    CommandObjectParsed (interpreter,
17466b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   "command script list",
17476b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   "List defined scripted commands.",
17486b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   NULL)
17496b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
17506b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
17516b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17526b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectCommandsScriptList ()
17536b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
17546b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
17556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    bool
1757da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
17586b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
17596b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17606b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_interpreter.GetHelp(result,
17616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                              CommandInterpreter::eCommandTypesUserDef);
17626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        result.SetStatus (eReturnStatusSuccessFinishResult);
17646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return true;
17666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
17696b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
17706b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17716b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
17726b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata// CommandObjectCommandsScriptClear
17736b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
17746b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1775da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectCommandsScriptClear : public CommandObjectParsed
17766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
17776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataprivate:
17786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
17806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectCommandsScriptClear(CommandInterpreter &interpreter) :
1781da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
1782da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "command script clear",
1783da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Delete all scripted commands.",
1784da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL)
17856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
17866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
17876b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectCommandsScriptClear ()
17896b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
17906b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
17916b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1792da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
17936b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    bool
1794da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
17956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
17966b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17976b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_interpreter.RemoveAllUser();
17986b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
17996b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        result.SetStatus (eReturnStatusSuccessFinishResult);
18006b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18016b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return true;
18026b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
18036b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
18046b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18056b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
18066b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata// CommandObjectCommandsScriptDelete
18076b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
18086b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1809da26bd203cbb104291b39891febf7481794f205fJim Inghamclass CommandObjectCommandsScriptDelete : public CommandObjectParsed
18106b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
18116b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
18126b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter) :
1813da26bd203cbb104291b39891febf7481794f205fJim Ingham        CommandObjectParsed (interpreter,
1814da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "command script delete",
1815da26bd203cbb104291b39891febf7481794f205fJim Ingham                             "Delete a scripted command.",
1816da26bd203cbb104291b39891febf7481794f205fJim Ingham                             NULL)
18176b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
18186b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        CommandArgumentEntry arg1;
18196b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        CommandArgumentData cmd_arg;
18206b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18216b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // Define the first (and only) variant of this arg.
18226b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        cmd_arg.arg_type = eArgTypeCommandName;
18236b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        cmd_arg.arg_repetition = eArgRepeatPlain;
18246b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18256b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // There is only one variant this argument could be; put it into the argument entry.
18266b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        arg1.push_back (cmd_arg);
18276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18286b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // Push the data for the first argument into the m_arguments vector.
18296b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_arguments.push_back (arg1);
18306b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
18316b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18326b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectCommandsScriptDelete ()
18336b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
18346b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
18356b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1836da26bd203cbb104291b39891febf7481794f205fJim Inghamprotected:
18376b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    bool
1838da26bd203cbb104291b39891febf7481794f205fJim Ingham    DoExecute (Args& command, CommandReturnObject &result)
18396b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
18406b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1841da26bd203cbb104291b39891febf7481794f205fJim Ingham        size_t argc = command.GetArgumentCount();
18426b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18436b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        if (argc != 1)
18446b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
18456b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.AppendError ("'command script delete' requires one argument");
18466b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus (eReturnStatusFailed);
18476b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            return false;
18486b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
18496b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1850da26bd203cbb104291b39891febf7481794f205fJim Ingham        const char* cmd_name = command.GetArgumentAtIndex(0);
18516b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18526b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        if (cmd_name && *cmd_name && m_interpreter.HasUserCommands() && m_interpreter.UserCommandExists(cmd_name))
18536b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
18546b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            m_interpreter.RemoveUser(cmd_name);
18556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus (eReturnStatusSuccessFinishResult);
18566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
18576b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        else
18586b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
18596b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.AppendErrorWithFormat ("command %s not found", cmd_name);
18606b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus (eReturnStatusFailed);
18616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
18626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return result.Succeeded();
18646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
18666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
18676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata#pragma mark CommandObjectMultiwordCommandsScript
18696b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18706b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
18716b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata// CommandObjectMultiwordCommandsScript
18726b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
18736b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18746b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataclass CommandObjectMultiwordCommandsScript : public CommandObjectMultiword
18756b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
18766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
18776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectMultiwordCommandsScript (CommandInterpreter &interpreter) :
18786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectMultiword (interpreter,
18796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                            "command script",
18806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                            "A set of commands for managing or customizing script commands.",
18816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                            "command script <subcommand> [<subcommand-options>]")
18826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
18836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        LoadSubCommand ("add",  CommandObjectSP (new CommandObjectCommandsScriptAdd (interpreter)));
18846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        LoadSubCommand ("delete",   CommandObjectSP (new CommandObjectCommandsScriptDelete (interpreter)));
18856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        LoadSubCommand ("clear", CommandObjectSP (new CommandObjectCommandsScriptClear (interpreter)));
18866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        LoadSubCommand ("list",   CommandObjectSP (new CommandObjectCommandsScriptList (interpreter)));
188759df36f99b76e33852e6848a162f5c2851074ea2Enrico Granata        LoadSubCommand ("import",   CommandObjectSP (new CommandObjectCommandsScriptImport (interpreter)));
18886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
18896b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18906b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectMultiwordCommandsScript ()
18916b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
18926b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
18936b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18946b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
18956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
18966b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1897767af88aa617288e584afcfed055f7755e408542Jim Ingham#pragma mark CommandObjectMultiwordCommands
1898767af88aa617288e584afcfed055f7755e408542Jim Ingham
1899767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
1900767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectMultiwordCommands
1901767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
1902767af88aa617288e584afcfed055f7755e408542Jim Ingham
1903767af88aa617288e584afcfed055f7755e408542Jim InghamCommandObjectMultiwordCommands::CommandObjectMultiwordCommands (CommandInterpreter &interpreter) :
1904238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectMultiword (interpreter,
190540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                            "command",
1906c1ad82eb979ee856b86aec6e2acb7bddf75f7c4fCaroline Tice                            "A set of commands for managing or customizing the debugger commands.",
190740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                            "command <subcommand> [<subcommand-options>]")
1908767af88aa617288e584afcfed055f7755e408542Jim Ingham{
1909238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    LoadSubCommand ("source",  CommandObjectSP (new CommandObjectCommandsSource (interpreter)));
1910238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    LoadSubCommand ("alias",   CommandObjectSP (new CommandObjectCommandsAlias (interpreter)));
1911238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    LoadSubCommand ("unalias", CommandObjectSP (new CommandObjectCommandsUnalias (interpreter)));
1912d12aeab33bab559e138307f599077da3918a3238Greg Clayton    LoadSubCommand ("regex",   CommandObjectSP (new CommandObjectCommandsAddRegex (interpreter)));
19136247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    LoadSubCommand ("history",   CommandObjectSP (new CommandObjectCommandsHistory (interpreter)));
19146b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    LoadSubCommand ("script",   CommandObjectSP (new CommandObjectMultiwordCommandsScript (interpreter)));
1915767af88aa617288e584afcfed055f7755e408542Jim Ingham}
1916767af88aa617288e584afcfed055f7755e408542Jim Ingham
1917767af88aa617288e584afcfed055f7755e408542Jim InghamCommandObjectMultiwordCommands::~CommandObjectMultiwordCommands ()
1918767af88aa617288e584afcfed055f7755e408542Jim Ingham{
1919767af88aa617288e584afcfed055f7755e408542Jim Ingham}
1920767af88aa617288e584afcfed055f7755e408542Jim Ingham
1921