CommandObjectCommands.cpp revision 536f633e2218f306f94c47398f43f0c7b46137d6
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
376247dbee41ec51e9a082df7e86269c0f47f28160Jim Inghamclass CommandObjectCommandsHistory : public CommandObject
386247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{
396247dbee41ec51e9a082df7e86269c0f47f28160Jim Inghamprivate:
406247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
416247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    class CommandOptions : public Options
426247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    {
436247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    public:
446247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
456247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        CommandOptions (CommandInterpreter &interpreter) :
466247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            Options (interpreter)
476247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        {
486247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        }
496247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
506247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        virtual
516247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        ~CommandOptions (){}
526247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
536247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        virtual Error
546247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        SetOptionValue (uint32_t option_idx, const char *option_arg)
556247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        {
566247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            Error error;
576247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            char short_option = (char) m_getopt_table[option_idx].val;
586247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            bool success;
596247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
606247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            switch (short_option)
616247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            {
626247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                case 'c':
636247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    m_end_idx = Args::StringToUInt32(option_arg, UINT_MAX, 0, &success);
646247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    if (!success)
656247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                        error.SetErrorStringWithFormat("Invalid value for count: %s.\n", option_arg);
666247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    if (m_end_idx != 0)
676247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                        m_end_idx--;
686247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    m_start_idx = 0;
696247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    break;
706247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                case 'e':
716247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    m_end_idx = Args::StringToUInt32(option_arg, 0, 0, &success);
726247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    if (!success)
736247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                        error.SetErrorStringWithFormat("Invalid value for end index: %s.\n", option_arg);
746247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    break;
756247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                case 's':
766247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    m_start_idx = Args::StringToUInt32(option_arg, 0, 0, &success);
776247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    if (!success)
786247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                        error.SetErrorStringWithFormat("Invalid value for start index: %s.\n", option_arg);
796247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    break;
806247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                default:
816247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
826247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                    break;
836247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            }
846247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
856247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            return error;
866247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        }
876247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
886247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        void
896247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        OptionParsingStarting ()
906247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        {
916247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            m_start_idx = 0;
926247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            m_end_idx = UINT_MAX;
936247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        }
946247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
956247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        const OptionDefinition*
966247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        GetDefinitions ()
976247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        {
986247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham            return g_option_table;
996247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        }
1006247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1016247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        // Options table: Required for subclasses of Options.
1026247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1036247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        static OptionDefinition g_option_table[];
1046247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1056247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        // Instance variables to hold the values for command options.
1066247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1076247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        uint32_t m_start_idx;
1086247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        uint32_t m_end_idx;
1096247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    };
1106247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1116247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    CommandOptions m_options;
1126247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1136247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    virtual Options *
1146247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    GetOptions ()
1156247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    {
1166247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        return &m_options;
1176247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    }
1186247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1196247dbee41ec51e9a082df7e86269c0f47f28160Jim Inghampublic:
1206247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    CommandObjectCommandsHistory(CommandInterpreter &interpreter) :
1216247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        CommandObject (interpreter,
1226247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                       "command history",
1236247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                       "Dump the history of commands in this session.",
1246247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                       NULL),
1256247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        m_options (interpreter)
1266247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    {
1276247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    }
1286247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1296247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    ~CommandObjectCommandsHistory ()
1306247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    {
1316247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    }
1326247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1336247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    bool
1346247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    Execute
1356247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    (
1366247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        Args& args,
1376247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        CommandReturnObject &result
1386247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    )
1396247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    {
1406247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1416247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        m_interpreter.DumpHistory (result.GetOutputStream(),
1426247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                                   m_options.m_start_idx,
1436247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham                                   m_options.m_end_idx);
1446247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham        return result.Succeeded();
1456247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1466247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    }
1476247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham};
1486247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1496247dbee41ec51e9a082df7e86269c0f47f28160Jim InghamOptionDefinition
1506247dbee41ec51e9a082df7e86269c0f47f28160Jim InghamCommandObjectCommandsHistory::CommandOptions::g_option_table[] =
1516247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{
1526247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{ LLDB_OPT_SET_1, false, "count", 'c', required_argument, NULL, 0, eArgTypeUnsignedInteger,        "How many history commands to print."},
1536247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{ LLDB_OPT_SET_1, false, "start-index", 's', required_argument, NULL, 0, eArgTypeUnsignedInteger,  "Index at which to start printing history commands."},
1546247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{ LLDB_OPT_SET_1, false, "end-index", 'e', required_argument, NULL, 0, eArgTypeUnsignedInteger,    "Index at which to stop printing history commands."},
1556247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1566247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham};
1576247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1586247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
1596247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham//-------------------------------------------------------------------------
1606247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham// CommandObjectCommandsSource
1616247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham//-------------------------------------------------------------------------
1626247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham
163767af88aa617288e584afcfed055f7755e408542Jim Inghamclass CommandObjectCommandsSource : public CommandObject
164767af88aa617288e584afcfed055f7755e408542Jim Ingham{
165949d5acde6684b5824a26034457410cdf3823dfeJim Inghamprivate:
166949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
167949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    class CommandOptions : public Options
168949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    {
169949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    public:
170949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
171f15996eea072cdaa8a092f22d3a1212b3d95f0ecGreg Clayton        CommandOptions (CommandInterpreter &interpreter) :
172f15996eea072cdaa8a092f22d3a1212b3d95f0ecGreg Clayton            Options (interpreter)
173f15996eea072cdaa8a092f22d3a1212b3d95f0ecGreg Clayton        {
174f15996eea072cdaa8a092f22d3a1212b3d95f0ecGreg Clayton        }
175949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
176949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        virtual
177949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        ~CommandOptions (){}
178949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
179949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        virtual Error
180143fcc3a15425659b381502ed4e1e50a3e726f36Greg Clayton        SetOptionValue (uint32_t option_idx, const char *option_arg)
181949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        {
182949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            Error error;
183949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            char short_option = (char) m_getopt_table[option_idx].val;
184949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            bool success;
185949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
186949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            switch (short_option)
187949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            {
188949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                case 'e':
189949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    m_stop_on_error = Args::StringToBoolean(option_arg, true, &success);
190949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    if (!success)
191949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                        error.SetErrorStringWithFormat("Invalid value for stop-on-error: %s.\n", option_arg);
192949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    break;
193949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                case 'c':
194949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    m_stop_on_continue = Args::StringToBoolean(option_arg, true, &success);
195949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    if (!success)
196949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                        error.SetErrorStringWithFormat("Invalid value for stop-on-continue: %s.\n", option_arg);
197949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    break;
198949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                default:
199949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
200949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                    break;
201949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            }
202949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
203949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            return error;
204949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        }
205949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
206949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        void
207143fcc3a15425659b381502ed4e1e50a3e726f36Greg Clayton        OptionParsingStarting ()
208949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        {
209949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            m_stop_on_error = true;
210949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            m_stop_on_continue = true;
211949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        }
212949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
213b344843f75ef893762c93fd0a22d2d45712ce74dGreg Clayton        const OptionDefinition*
214949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        GetDefinitions ()
215949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        {
216949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            return g_option_table;
217949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        }
218949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
219949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        // Options table: Required for subclasses of Options.
220949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
221b344843f75ef893762c93fd0a22d2d45712ce74dGreg Clayton        static OptionDefinition g_option_table[];
222949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
223949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        // Instance variables to hold the values for command options.
224949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
225949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        bool m_stop_on_error;
226949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        bool m_stop_on_continue;
227949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    };
228949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
229949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    CommandOptions m_options;
230949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
231949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    virtual Options *
232949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    GetOptions ()
233949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    {
234949d5acde6684b5824a26034457410cdf3823dfeJim Ingham        return &m_options;
235949d5acde6684b5824a26034457410cdf3823dfeJim Ingham    }
236949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
237767af88aa617288e584afcfed055f7755e408542Jim Inghampublic:
238238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectCommandsSource(CommandInterpreter &interpreter) :
239238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton        CommandObject (interpreter,
24040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                       "command source",
241238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                       "Read in debugger commands from the file <filename> and execute them.",
242f15996eea072cdaa8a092f22d3a1212b3d95f0ecGreg Clayton                       NULL),
243f15996eea072cdaa8a092f22d3a1212b3d95f0ecGreg Clayton        m_options (interpreter)
244767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
24543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg;
24643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData file_arg;
24743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
24843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
24943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        file_arg.arg_type = eArgTypeFilename;
25043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        file_arg.arg_repetition = eArgRepeatPlain;
25143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
25243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
25343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg.push_back (file_arg);
25443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
25543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Push the data for the first argument into the m_arguments vector.
25643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg);
257767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
258767af88aa617288e584afcfed055f7755e408542Jim Ingham
259767af88aa617288e584afcfed055f7755e408542Jim Ingham    ~CommandObjectCommandsSource ()
260767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
261767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
262767af88aa617288e584afcfed055f7755e408542Jim Ingham
263767af88aa617288e584afcfed055f7755e408542Jim Ingham    bool
264767af88aa617288e584afcfed055f7755e408542Jim Ingham    Execute
265767af88aa617288e584afcfed055f7755e408542Jim Ingham    (
266767af88aa617288e584afcfed055f7755e408542Jim Ingham        Args& args,
267767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandReturnObject &result
268767af88aa617288e584afcfed055f7755e408542Jim Ingham    )
269767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
270767af88aa617288e584afcfed055f7755e408542Jim Ingham        const int argc = args.GetArgumentCount();
271767af88aa617288e584afcfed055f7755e408542Jim Ingham        if (argc == 1)
272767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
273767af88aa617288e584afcfed055f7755e408542Jim Ingham            const char *filename = args.GetArgumentAtIndex(0);
274767af88aa617288e584afcfed055f7755e408542Jim Ingham
275767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendMessageWithFormat ("Executing commands in '%s'.\n", filename);
276767af88aa617288e584afcfed055f7755e408542Jim Ingham
277a83ea887f6165d59cfeac1bbb96011f3b0568af8Johnny Chen            FileSpec cmd_file (filename, true);
278949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            ExecutionContext *exe_ctx = NULL;  // Just use the default context.
279949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            bool echo_commands    = true;
280949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            bool print_results    = true;
281949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
282949d5acde6684b5824a26034457410cdf3823dfeJim Ingham            m_interpreter.HandleCommandsFromFile (cmd_file,
283949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  exe_ctx,
284949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  m_options.m_stop_on_continue,
285949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  m_options.m_stop_on_error,
286949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  echo_commands,
287949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  print_results,
288949d5acde6684b5824a26034457410cdf3823dfeJim Ingham                                                  result);
289767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
290767af88aa617288e584afcfed055f7755e408542Jim Ingham        else
291767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
292767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendErrorWithFormat("'%s' takes exactly one executable filename argument.\n", GetCommandName());
293767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
294767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
295767af88aa617288e584afcfed055f7755e408542Jim Ingham        return result.Succeeded();
296767af88aa617288e584afcfed055f7755e408542Jim Ingham
297767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
298767af88aa617288e584afcfed055f7755e408542Jim Ingham};
299767af88aa617288e584afcfed055f7755e408542Jim Ingham
300b344843f75ef893762c93fd0a22d2d45712ce74dGreg ClaytonOptionDefinition
301949d5acde6684b5824a26034457410cdf3823dfeJim InghamCommandObjectCommandsSource::CommandOptions::g_option_table[] =
302949d5acde6684b5824a26034457410cdf3823dfeJim Ingham{
303949d5acde6684b5824a26034457410cdf3823dfeJim Ingham{ LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', required_argument, NULL, 0, eArgTypeBoolean,    "If true, stop executing commands on error."},
304949d5acde6684b5824a26034457410cdf3823dfeJim Ingham{ LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', required_argument, NULL, 0, eArgTypeBoolean, "If true, stop executing commands on continue."},
305949d5acde6684b5824a26034457410cdf3823dfeJim Ingham{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
306949d5acde6684b5824a26034457410cdf3823dfeJim Ingham};
307949d5acde6684b5824a26034457410cdf3823dfeJim Ingham
308767af88aa617288e584afcfed055f7755e408542Jim Ingham#pragma mark CommandObjectCommandsAlias
309767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
310767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectCommandsAlias
311767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
312767af88aa617288e584afcfed055f7755e408542Jim Ingham
313c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granatastatic const char *g_python_command_instructions =   "Enter your Python command(s). Type 'DONE' to end.\n"
314c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata                                                     "You must define a Python function with this signature:\n"
3156b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                     "def my_command_impl(debugger, args, result, dict):";
316c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata
317c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata
318767af88aa617288e584afcfed055f7755e408542Jim Inghamclass CommandObjectCommandsAlias : public CommandObject
319767af88aa617288e584afcfed055f7755e408542Jim Ingham{
320c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata
321c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata
322767af88aa617288e584afcfed055f7755e408542Jim Inghampublic:
323238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectCommandsAlias (CommandInterpreter &interpreter) :
324238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton        CommandObject (interpreter,
32540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                       "command alias",
326abb507ae79e20ab2dd8365049921df73a24c95a5Caroline Tice                       "Allow users to define their own debugger command abbreviations.",
32743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice                       NULL)
328767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
329767af88aa617288e584afcfed055f7755e408542Jim Ingham        SetHelpLong(
330767af88aa617288e584afcfed055f7755e408542Jim Ingham    "'alias' allows the user to create a short-cut or abbreviation for long \n\
331767af88aa617288e584afcfed055f7755e408542Jim Ingham    commands, multi-word commands, and commands that take particular options. \n\
332767af88aa617288e584afcfed055f7755e408542Jim Ingham    Below are some simple examples of how one might use the 'alias' command: \n\
33331fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice    \n    'commands alias sc script'           // Creates the abbreviation 'sc' for the 'script' \n\
33431fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // command. \n\
33531fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice    'commands alias bp breakpoint'       // Creates the abbreviation 'bp' for the 'breakpoint' \n\
33631fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // command.  Since breakpoint commands are two-word \n\
33731fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // commands, the user will still need to enter the \n\
33831fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // second word after 'bp', e.g. 'bp enable' or \n\
33931fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // 'bp delete'. \n\
34031fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice    'commands alias bpl breakpoint list' // Creates the abbreviation 'bpl' for the \n\
34131fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice                                         // two-word command 'breakpoint list'. \n\
342767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nAn alias can include some options for the command, with the values either \n\
343767af88aa617288e584afcfed055f7755e408542Jim Ingham    filled in at the time the alias is created, or specified as positional \n\
344767af88aa617288e584afcfed055f7755e408542Jim Ingham    arguments, to be filled in when the alias is invoked.  The following example \n\
345767af88aa617288e584afcfed055f7755e408542Jim Ingham    shows how to create aliases with options: \n\
346767af88aa617288e584afcfed055f7755e408542Jim Ingham    \n\
34731fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice    'commands alias bfl breakpoint set -f %1 -l %2' \n\
348767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nThis creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \n\
349767af88aa617288e584afcfed055f7755e408542Jim Ingham    options already part of the alias.  So if the user wants to set a breakpoint \n\
350767af88aa617288e584afcfed055f7755e408542Jim Ingham    by file and line without explicitly having to use the -f and -l options, the \n\
351767af88aa617288e584afcfed055f7755e408542Jim Ingham    user can now use 'bfl' instead.  The '%1' and '%2' are positional placeholders \n\
352767af88aa617288e584afcfed055f7755e408542Jim Ingham    for the actual arguments that will be passed when the alias command is used. \n\
353767af88aa617288e584afcfed055f7755e408542Jim Ingham    The number in the placeholder refers to the position/order the actual value \n\
354536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    occupies when the alias is used.  All the occurrences of '%1' in the alias \n\
355767af88aa617288e584afcfed055f7755e408542Jim Ingham    will be replaced with the first argument, all the occurrences of '%2' in the \n\
356767af88aa617288e584afcfed055f7755e408542Jim Ingham    alias will be replaced with the second argument, and so on.  This also allows \n\
357767af88aa617288e584afcfed055f7755e408542Jim Ingham    actual arguments to be used multiple times within an alias (see 'process \n\
358536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    launch' example below).  \n\
359536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    Note: the positional arguments must substitute as whole words in the resultant\n\
360536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    command, so you can't at present do something like:\n\
361536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    \n\
362536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    commands alias bcppfl breakpoint set -f %1.cpp -l %2\n\
363536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    \n\
364536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    to get the file extension \".cpp\" automatically appended.  For more complex\n\
365536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    aliasing, use the \"command regex\" command instead.\n\
366536f633e2218f306f94c47398f43f0c7b46137d6Jim Ingham    \nSo in the 'bfl' case, the actual file value will be \n\
367767af88aa617288e584afcfed055f7755e408542Jim Ingham    filled in with the first argument following 'bfl' and the actual line number \n\
368767af88aa617288e584afcfed055f7755e408542Jim Ingham    value will be filled in with the second argument.  The user would use this \n\
369767af88aa617288e584afcfed055f7755e408542Jim Ingham    alias as follows: \n\
37031fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice    \n    (lldb)  commands alias bfl breakpoint set -f %1 -l %2 \n\
371a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    <... some time later ...> \n\
37231fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice    (lldb)  bfl my-file.c 137 \n\
373767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nThis would be the same as if the user had entered \n\
374767af88aa617288e584afcfed055f7755e408542Jim Ingham    'breakpoint set -f my-file.c -l 137'. \n\
375767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nAnother example: \n\
37631fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice    \n    (lldb)  commands alias pltty  process launch -s -o %1 -e %1 \n\
37731fbb64aee1432672f776610e15466cbdc2f9795Caroline Tice    (lldb)  pltty /dev/tty0 \n\
378a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan           // becomes 'process launch -s -o /dev/tty0 -e /dev/tty0' \n\
379767af88aa617288e584afcfed055f7755e408542Jim Ingham    \nIf the user always wanted to pass the same value to a particular option, the \n\
380767af88aa617288e584afcfed055f7755e408542Jim Ingham    alias could be defined with that value directly in the alias as a constant, \n\
381767af88aa617288e584afcfed055f7755e408542Jim Ingham    rather than using a positional placeholder: \n\
382a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan    \n    commands alias bl3  breakpoint set -f %1 -l 3  // Always sets a breakpoint on line \n\
383a3aff73b5151c97b3adb1cea3022df967e33db45Sean Callanan                                                   // 3 of whatever file is indicated. \n");
384767af88aa617288e584afcfed055f7755e408542Jim Ingham
38543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg1;
38643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg2;
38743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg3;
38843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData alias_arg;
38943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData cmd_arg;
39043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData options_arg;
39143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
39243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
39343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        alias_arg.arg_type = eArgTypeAliasName;
39443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        alias_arg.arg_repetition = eArgRepeatPlain;
39543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
39643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
39743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg1.push_back (alias_arg);
39843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
39943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
40043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        cmd_arg.arg_type = eArgTypeCommandName;
40143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        cmd_arg.arg_repetition = eArgRepeatPlain;
40243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
40343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
40443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg2.push_back (cmd_arg);
40543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
40643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
40743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        options_arg.arg_type = eArgTypeAliasOptions;
40843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        options_arg.arg_repetition = eArgRepeatOptional;
40943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
41043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
41143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg3.push_back (options_arg);
41243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
41343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Push the data for the first argument into the m_arguments vector.
41443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg1);
41543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg2);
41643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg3);
417767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
418767af88aa617288e584afcfed055f7755e408542Jim Ingham
419767af88aa617288e584afcfed055f7755e408542Jim Ingham    ~CommandObjectCommandsAlias ()
420767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
421767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
422767af88aa617288e584afcfed055f7755e408542Jim Ingham
423e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice    bool
424e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice    WantsRawCommandString ()
425e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice    {
426e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        return true;
427e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice    }
428e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
429e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice    bool
430e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice    ExecuteRawCommandString (const char *raw_command_line, CommandReturnObject &result)
431e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice    {
432e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        Args args (raw_command_line);
433e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        std::string raw_command_string (raw_command_line);
434e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
435e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        size_t argc = args.GetArgumentCount();
436e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
437e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        if (argc < 2)
438e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
439e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.AppendError ("'alias' requires at least two arguments");
440e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.SetStatus (eReturnStatusFailed);
441e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            return false;
442e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
443e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
444e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // Get the alias command.
445e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
446e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        const std::string alias_command = args.GetArgumentAtIndex (0);
447c2a2825d787be5459fc55264e55b6020ff62f68aEnrico Granata
448e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // Strip the new alias name off 'raw_command_string'  (leave it on args, which gets passed to 'Execute', which
449e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // does the stripping itself.
450e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        size_t pos = raw_command_string.find (alias_command);
451e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        if (pos == 0)
452e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
453e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            raw_command_string = raw_command_string.substr (alias_command.size());
454e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            pos = raw_command_string.find_first_not_of (' ');
455e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            if ((pos != std::string::npos) && (pos > 0))
456e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                raw_command_string = raw_command_string.substr (pos);
457e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
458e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        else
459e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
460e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.AppendError ("Error parsing command string.  No alias created.");
461e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.SetStatus (eReturnStatusFailed);
462e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            return false;
463e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
464e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
465e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
466e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // Verify that the command is alias-able.
467e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        if (m_interpreter.CommandExists (alias_command.c_str()))
468e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
469e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
470e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                                          alias_command.c_str());
471e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.SetStatus (eReturnStatusFailed);
472e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            return false;
473e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
474e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
475e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // Get CommandObject that is being aliased. The command name is read from the front of raw_command_string.
476e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        // raw_command_string is returned with the name of the command object stripped off the front.
477e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        CommandObject *cmd_obj = m_interpreter.GetCommandObjectForCommand (raw_command_string);
478e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
479e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        if (!cmd_obj)
480e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
481e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.AppendErrorWithFormat ("Invalid command given to 'alias'. '%s' does not begin with a valid command."
482e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                                          "  No alias created.", raw_command_string.c_str());
483e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            result.SetStatus (eReturnStatusFailed);
484e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            return false;
485e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
486e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        else if (!cmd_obj->WantsRawCommandString ())
487e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
488e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            // Note that args was initialized with the original command, and has not been updated to this point.
489e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            // Therefore can we pass it to the version of Execute that does not need/expect raw input in the alias.
490e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            return Execute (args, result);
491e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
492e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        else
493e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        {
494e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            // Verify & handle any options/arguments passed to the alias command
495e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
496e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector);
497e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
498e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
4995ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice            CommandObjectSP cmd_obj_sp = m_interpreter.GetCommandSPExact (cmd_obj->GetCommandName(), false);
5005ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice
5015ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice            if (!m_interpreter.ProcessAliasOptionsArgs (cmd_obj_sp, raw_command_string.c_str(), option_arg_vector_sp))
502e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            {
5035ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                result.AppendError ("Unable to create requested alias.\n");
5045ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                result.SetStatus (eReturnStatusFailed);
5055ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                return false;
506e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            }
507e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
508e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            // Create the alias
509e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            if (m_interpreter.AliasExists (alias_command.c_str())
510e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                || m_interpreter.UserCommandExists (alias_command.c_str()))
511e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            {
512e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                OptionArgVectorSP temp_option_arg_sp (m_interpreter.GetAliasOptions (alias_command.c_str()));
513e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                if (temp_option_arg_sp.get())
514e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                {
515e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                    if (option_arg_vector->size() == 0)
516e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                        m_interpreter.RemoveAliasOptions (alias_command.c_str());
517e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                }
518e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
519e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                                                alias_command.c_str());
520e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice            }
521e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice
52256d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            if (cmd_obj_sp)
52356d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            {
52456d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                m_interpreter.AddAlias (alias_command.c_str(), cmd_obj_sp);
52556d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                if (option_arg_vector->size() > 0)
52656d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                    m_interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp);
52756d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                result.SetStatus (eReturnStatusSuccessFinishNoResult);
52856d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            }
52956d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            else
53056d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            {
53156d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                result.AppendError ("Unable to create requested alias.\n");
53256d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice                result.SetStatus (eReturnStatusFailed);
53356d2fc45b9461d50054af20c33b836eb8ebd93f5Caroline Tice            }
534e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        }
535e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice        return result.Succeeded();
536e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice    }
537767af88aa617288e584afcfed055f7755e408542Jim Ingham
538767af88aa617288e584afcfed055f7755e408542Jim Ingham    bool
539767af88aa617288e584afcfed055f7755e408542Jim Ingham    Execute
540767af88aa617288e584afcfed055f7755e408542Jim Ingham    (
541767af88aa617288e584afcfed055f7755e408542Jim Ingham        Args& args,
542767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandReturnObject &result
543767af88aa617288e584afcfed055f7755e408542Jim Ingham    )
544767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
5458bb61f099795a3fd98edf655c3e6899cd2668f6cCaroline Tice        size_t argc = args.GetArgumentCount();
546767af88aa617288e584afcfed055f7755e408542Jim Ingham
547767af88aa617288e584afcfed055f7755e408542Jim Ingham        if (argc < 2)
54854e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton        {
549767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendError ("'alias' requires at least two arguments");
550767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
551767af88aa617288e584afcfed055f7755e408542Jim Ingham            return false;
55254e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton        }
553767af88aa617288e584afcfed055f7755e408542Jim Ingham
554767af88aa617288e584afcfed055f7755e408542Jim Ingham        const std::string alias_command = args.GetArgumentAtIndex(0);
555767af88aa617288e584afcfed055f7755e408542Jim Ingham        const std::string actual_command = args.GetArgumentAtIndex(1);
556767af88aa617288e584afcfed055f7755e408542Jim Ingham
557767af88aa617288e584afcfed055f7755e408542Jim Ingham        args.Shift();  // Shift the alias command word off the argument vector.
558767af88aa617288e584afcfed055f7755e408542Jim Ingham        args.Shift();  // Shift the old command word off the argument vector.
559767af88aa617288e584afcfed055f7755e408542Jim Ingham
560767af88aa617288e584afcfed055f7755e408542Jim Ingham        // Verify that the command is alias'able, and get the appropriate command object.
561767af88aa617288e584afcfed055f7755e408542Jim Ingham
562238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton        if (m_interpreter.CommandExists (alias_command.c_str()))
563767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
564767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
565767af88aa617288e584afcfed055f7755e408542Jim Ingham                                         alias_command.c_str());
566767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
567767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
568767af88aa617288e584afcfed055f7755e408542Jim Ingham        else
569767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
570238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton             CommandObjectSP command_obj_sp(m_interpreter.GetCommandSPExact (actual_command.c_str(), true));
571767af88aa617288e584afcfed055f7755e408542Jim Ingham             CommandObjectSP subcommand_obj_sp;
572767af88aa617288e584afcfed055f7755e408542Jim Ingham             bool use_subcommand = false;
573767af88aa617288e584afcfed055f7755e408542Jim Ingham             if (command_obj_sp.get())
574767af88aa617288e584afcfed055f7755e408542Jim Ingham             {
575767af88aa617288e584afcfed055f7755e408542Jim Ingham                 CommandObject *cmd_obj = command_obj_sp.get();
57654e7afa84d945f9137f9372ecde432f9e1a702fcGreg Clayton                 CommandObject *sub_cmd_obj = NULL;
577767af88aa617288e584afcfed055f7755e408542Jim Ingham                 OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector);
578767af88aa617288e584afcfed055f7755e408542Jim Ingham                 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
579767af88aa617288e584afcfed055f7755e408542Jim Ingham
580e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                 while (cmd_obj->IsMultiwordObject() && args.GetArgumentCount() > 0)
581767af88aa617288e584afcfed055f7755e408542Jim Ingham                 {
582767af88aa617288e584afcfed055f7755e408542Jim Ingham                     if (argc >= 3)
583767af88aa617288e584afcfed055f7755e408542Jim Ingham                     {
584767af88aa617288e584afcfed055f7755e408542Jim Ingham                         const std::string sub_command = args.GetArgumentAtIndex(0);
585767af88aa617288e584afcfed055f7755e408542Jim Ingham                         assert (sub_command.length() != 0);
586767af88aa617288e584afcfed055f7755e408542Jim Ingham                         subcommand_obj_sp =
587767af88aa617288e584afcfed055f7755e408542Jim Ingham                                           (((CommandObjectMultiword *) cmd_obj)->GetSubcommandSP (sub_command.c_str()));
588767af88aa617288e584afcfed055f7755e408542Jim Ingham                         if (subcommand_obj_sp.get())
589767af88aa617288e584afcfed055f7755e408542Jim Ingham                         {
590767af88aa617288e584afcfed055f7755e408542Jim Ingham                             sub_cmd_obj = subcommand_obj_sp.get();
591767af88aa617288e584afcfed055f7755e408542Jim Ingham                             use_subcommand = true;
592767af88aa617288e584afcfed055f7755e408542Jim Ingham                             args.Shift();  // Shift the sub_command word off the argument vector.
593e0da7a5ba288669df3a4d51059aa0f78f729c6b1Caroline Tice                             cmd_obj = sub_cmd_obj;
594767af88aa617288e584afcfed055f7755e408542Jim Ingham                         }
595767af88aa617288e584afcfed055f7755e408542Jim Ingham                         else
596767af88aa617288e584afcfed055f7755e408542Jim Ingham                         {
5975d53b621dea6a2ca6ece7528b22d089f864ae18fCaroline Tice                             result.AppendErrorWithFormat("'%s' is not a valid sub-command of '%s'.  "
5985d53b621dea6a2ca6ece7528b22d089f864ae18fCaroline Tice                                                          "Unable to create alias.\n",
5995d53b621dea6a2ca6ece7528b22d089f864ae18fCaroline Tice                                                          sub_command.c_str(), actual_command.c_str());
600767af88aa617288e584afcfed055f7755e408542Jim Ingham                             result.SetStatus (eReturnStatusFailed);
601767af88aa617288e584afcfed055f7755e408542Jim Ingham                             return false;
602767af88aa617288e584afcfed055f7755e408542Jim Ingham                         }
603767af88aa617288e584afcfed055f7755e408542Jim Ingham                     }
604767af88aa617288e584afcfed055f7755e408542Jim Ingham                 }
605767af88aa617288e584afcfed055f7755e408542Jim Ingham
606767af88aa617288e584afcfed055f7755e408542Jim Ingham                 // Verify & handle any options/arguments passed to the alias command
607767af88aa617288e584afcfed055f7755e408542Jim Ingham
608767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (args.GetArgumentCount () > 0)
609767af88aa617288e584afcfed055f7755e408542Jim Ingham                 {
6105ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    CommandObjectSP tmp_sp = m_interpreter.GetCommandSPExact (cmd_obj->GetCommandName(), false);
6115ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    if (use_subcommand)
6125ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                        tmp_sp = m_interpreter.GetCommandSPExact (sub_cmd_obj->GetCommandName(), false);
6135ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice
6145ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    std::string args_string;
6155ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    args.GetCommandString (args_string);
6165ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice
6175ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    if (!m_interpreter.ProcessAliasOptionsArgs (tmp_sp, args_string.c_str(), option_arg_vector_sp))
6185ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    {
6195ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                        result.AppendError ("Unable to create requested alias.\n");
6205ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                        result.SetStatus (eReturnStatusFailed);
6215ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                        return false;
6225ddbe214153f60f8ab62ba98dea089a21f1779f3Caroline Tice                    }
623767af88aa617288e584afcfed055f7755e408542Jim Ingham                 }
624767af88aa617288e584afcfed055f7755e408542Jim Ingham
625767af88aa617288e584afcfed055f7755e408542Jim Ingham                 // Create the alias.
626767af88aa617288e584afcfed055f7755e408542Jim Ingham
627238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                 if (m_interpreter.AliasExists (alias_command.c_str())
628238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                     || m_interpreter.UserCommandExists (alias_command.c_str()))
629767af88aa617288e584afcfed055f7755e408542Jim Ingham                 {
630238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                     OptionArgVectorSP tmp_option_arg_sp (m_interpreter.GetAliasOptions (alias_command.c_str()));
631767af88aa617288e584afcfed055f7755e408542Jim Ingham                     if (tmp_option_arg_sp.get())
632767af88aa617288e584afcfed055f7755e408542Jim Ingham                     {
633767af88aa617288e584afcfed055f7755e408542Jim Ingham                         if (option_arg_vector->size() == 0)
634238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                             m_interpreter.RemoveAliasOptions (alias_command.c_str());
635767af88aa617288e584afcfed055f7755e408542Jim Ingham                     }
636767af88aa617288e584afcfed055f7755e408542Jim Ingham                     result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
637767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                     alias_command.c_str());
638767af88aa617288e584afcfed055f7755e408542Jim Ingham                 }
639767af88aa617288e584afcfed055f7755e408542Jim Ingham
640767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (use_subcommand)
641238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                     m_interpreter.AddAlias (alias_command.c_str(), subcommand_obj_sp);
642767af88aa617288e584afcfed055f7755e408542Jim Ingham                 else
643238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                     m_interpreter.AddAlias (alias_command.c_str(), command_obj_sp);
644767af88aa617288e584afcfed055f7755e408542Jim Ingham                 if (option_arg_vector->size() > 0)
645238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                     m_interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp);
646767af88aa617288e584afcfed055f7755e408542Jim Ingham                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
647767af88aa617288e584afcfed055f7755e408542Jim Ingham             }
648767af88aa617288e584afcfed055f7755e408542Jim Ingham             else
649767af88aa617288e584afcfed055f7755e408542Jim Ingham             {
650767af88aa617288e584afcfed055f7755e408542Jim Ingham                 result.AppendErrorWithFormat ("'%s' is not an existing command.\n", actual_command.c_str());
651767af88aa617288e584afcfed055f7755e408542Jim Ingham                 result.SetStatus (eReturnStatusFailed);
652e6866a3819d68be21004ffdf773e36e7f00a6a26Caroline Tice                 return false;
653767af88aa617288e584afcfed055f7755e408542Jim Ingham             }
654767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
655767af88aa617288e584afcfed055f7755e408542Jim Ingham
656767af88aa617288e584afcfed055f7755e408542Jim Ingham        return result.Succeeded();
657767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
658767af88aa617288e584afcfed055f7755e408542Jim Ingham};
659767af88aa617288e584afcfed055f7755e408542Jim Ingham
660767af88aa617288e584afcfed055f7755e408542Jim Ingham#pragma mark CommandObjectCommandsUnalias
661767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
662767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectCommandsUnalias
663767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
664767af88aa617288e584afcfed055f7755e408542Jim Ingham
665767af88aa617288e584afcfed055f7755e408542Jim Inghamclass CommandObjectCommandsUnalias : public CommandObject
666767af88aa617288e584afcfed055f7755e408542Jim Ingham{
667767af88aa617288e584afcfed055f7755e408542Jim Inghampublic:
668238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectCommandsUnalias (CommandInterpreter &interpreter) :
669238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton        CommandObject (interpreter,
67040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                       "command unalias",
671146292c83470680b98a4e4046428e0849e1ffe8fCaroline Tice                       "Allow the user to remove/delete a user-defined command abbreviation.",
67243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice                       NULL)
673767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
67443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentEntry arg;
67543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        CommandArgumentData alias_arg;
67643b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
67743b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Define the first (and only) variant of this arg.
67843b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        alias_arg.arg_type = eArgTypeAliasName;
67943b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        alias_arg.arg_repetition = eArgRepeatPlain;
68043b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
68143b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // There is only one variant this argument could be; put it into the argument entry.
68243b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        arg.push_back (alias_arg);
68343b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice
68443b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        // Push the data for the first argument into the m_arguments vector.
68543b014aa33e20e61790e16ed69a2c57aae2fbc6eCaroline Tice        m_arguments.push_back (arg);
686767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
687767af88aa617288e584afcfed055f7755e408542Jim Ingham
688767af88aa617288e584afcfed055f7755e408542Jim Ingham    ~CommandObjectCommandsUnalias()
689767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
690767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
691767af88aa617288e584afcfed055f7755e408542Jim Ingham
692767af88aa617288e584afcfed055f7755e408542Jim Ingham
693767af88aa617288e584afcfed055f7755e408542Jim Ingham    bool
694767af88aa617288e584afcfed055f7755e408542Jim Ingham    Execute
695767af88aa617288e584afcfed055f7755e408542Jim Ingham    (
696767af88aa617288e584afcfed055f7755e408542Jim Ingham        Args& args,
697767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandReturnObject &result
698767af88aa617288e584afcfed055f7755e408542Jim Ingham    )
699767af88aa617288e584afcfed055f7755e408542Jim Ingham    {
700767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandObject::CommandMap::iterator pos;
701767af88aa617288e584afcfed055f7755e408542Jim Ingham        CommandObject *cmd_obj;
702767af88aa617288e584afcfed055f7755e408542Jim Ingham
703767af88aa617288e584afcfed055f7755e408542Jim Ingham        if (args.GetArgumentCount() != 0)
704767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
705767af88aa617288e584afcfed055f7755e408542Jim Ingham            const char *command_name = args.GetArgumentAtIndex(0);
706238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton            cmd_obj = m_interpreter.GetCommandObject(command_name);
707767af88aa617288e584afcfed055f7755e408542Jim Ingham            if (cmd_obj)
708767af88aa617288e584afcfed055f7755e408542Jim Ingham            {
709238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                if (m_interpreter.CommandExists (command_name))
710767af88aa617288e584afcfed055f7755e408542Jim Ingham                {
711767af88aa617288e584afcfed055f7755e408542Jim Ingham                    result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be removed.\n",
712767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                  command_name);
713767af88aa617288e584afcfed055f7755e408542Jim Ingham                    result.SetStatus (eReturnStatusFailed);
714767af88aa617288e584afcfed055f7755e408542Jim Ingham                }
715767af88aa617288e584afcfed055f7755e408542Jim Ingham                else
716767af88aa617288e584afcfed055f7755e408542Jim Ingham                {
717767af88aa617288e584afcfed055f7755e408542Jim Ingham
718238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                    if (m_interpreter.RemoveAlias (command_name) == false)
719767af88aa617288e584afcfed055f7755e408542Jim Ingham                    {
720238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton                        if (m_interpreter.AliasExists (command_name))
721767af88aa617288e584afcfed055f7755e408542Jim Ingham                            result.AppendErrorWithFormat ("Error occurred while attempting to unalias '%s'.\n",
722767af88aa617288e584afcfed055f7755e408542Jim Ingham                                                          command_name);
723767af88aa617288e584afcfed055f7755e408542Jim Ingham                        else
724767af88aa617288e584afcfed055f7755e408542Jim Ingham                            result.AppendErrorWithFormat ("'%s' is not an existing alias.\n", command_name);
725767af88aa617288e584afcfed055f7755e408542Jim Ingham                        result.SetStatus (eReturnStatusFailed);
726767af88aa617288e584afcfed055f7755e408542Jim Ingham                    }
727767af88aa617288e584afcfed055f7755e408542Jim Ingham                    else
728767af88aa617288e584afcfed055f7755e408542Jim Ingham                        result.SetStatus (eReturnStatusSuccessFinishNoResult);
729767af88aa617288e584afcfed055f7755e408542Jim Ingham                }
730767af88aa617288e584afcfed055f7755e408542Jim Ingham            }
731767af88aa617288e584afcfed055f7755e408542Jim Ingham            else
732767af88aa617288e584afcfed055f7755e408542Jim Ingham            {
733767af88aa617288e584afcfed055f7755e408542Jim Ingham                result.AppendErrorWithFormat ("'%s' is not a known command.\nTry 'help' to see a "
734767af88aa617288e584afcfed055f7755e408542Jim Ingham                                              "current list of commands.\n",
735767af88aa617288e584afcfed055f7755e408542Jim Ingham                                             command_name);
736767af88aa617288e584afcfed055f7755e408542Jim Ingham                result.SetStatus (eReturnStatusFailed);
737767af88aa617288e584afcfed055f7755e408542Jim Ingham            }
738767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
739767af88aa617288e584afcfed055f7755e408542Jim Ingham        else
740767af88aa617288e584afcfed055f7755e408542Jim Ingham        {
741767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.AppendError ("must call 'unalias' with a valid alias");
742767af88aa617288e584afcfed055f7755e408542Jim Ingham            result.SetStatus (eReturnStatusFailed);
743767af88aa617288e584afcfed055f7755e408542Jim Ingham        }
744767af88aa617288e584afcfed055f7755e408542Jim Ingham
745767af88aa617288e584afcfed055f7755e408542Jim Ingham        return result.Succeeded();
746767af88aa617288e584afcfed055f7755e408542Jim Ingham    }
747767af88aa617288e584afcfed055f7755e408542Jim Ingham};
748767af88aa617288e584afcfed055f7755e408542Jim Ingham
749d12aeab33bab559e138307f599077da3918a3238Greg Clayton#pragma mark CommandObjectCommandsAddRegex
750d12aeab33bab559e138307f599077da3918a3238Greg Clayton//-------------------------------------------------------------------------
751d12aeab33bab559e138307f599077da3918a3238Greg Clayton// CommandObjectCommandsAddRegex
752d12aeab33bab559e138307f599077da3918a3238Greg Clayton//-------------------------------------------------------------------------
753d12aeab33bab559e138307f599077da3918a3238Greg Clayton
754d12aeab33bab559e138307f599077da3918a3238Greg Claytonclass CommandObjectCommandsAddRegex : public CommandObject
755d12aeab33bab559e138307f599077da3918a3238Greg Clayton{
756d12aeab33bab559e138307f599077da3918a3238Greg Claytonpublic:
757d12aeab33bab559e138307f599077da3918a3238Greg Clayton    CommandObjectCommandsAddRegex (CommandInterpreter &interpreter) :
758d12aeab33bab559e138307f599077da3918a3238Greg Clayton        CommandObject (interpreter,
75940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                       "command regex",
760d12aeab33bab559e138307f599077da3918a3238Greg Clayton                       "Allow the user to create a regular expression command.",
76140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                       "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
762d12aeab33bab559e138307f599077da3918a3238Greg Clayton        m_options (interpreter)
763d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
76440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        SetHelpLong(
76540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"This command allows the user to create powerful regular expression commands\n"
76640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"with substitutions. The regular expressions and substitutions are specified\n"
76740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"using the regular exression substitution format of:\n"
76840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"\n"
76940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"    s/<regex>/<subst>/\n"
77040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"\n"
77140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"<regex> is a regular expression that can use parenthesis to capture regular\n"
77240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"expression input and substitute the captured matches in the output using %1\n"
77340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"for the first match, %2 for the second, and so on.\n"
77440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"\n"
77540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"The regular expressions can all be specified on the command line if more than\n"
77640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"one argument is provided. If just the command name is provided on the command\n"
77740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"line, then the regular expressions and substitutions can be entered on separate\n"
77840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton" lines, followed by an empty line to terminate the command definition.\n"
77940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"\n"
78040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"EXAMPLES\n"
78140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"\n"
78240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"The following example with define a regular expression command named 'f' that\n"
78340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if\n"
78440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"a number follows 'f':\n"
78540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton"(lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/'\n"
78640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    );
787d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
788d12aeab33bab559e138307f599077da3918a3238Greg Clayton
789d12aeab33bab559e138307f599077da3918a3238Greg Clayton    ~CommandObjectCommandsAddRegex()
790d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
791d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
792d12aeab33bab559e138307f599077da3918a3238Greg Clayton
793d12aeab33bab559e138307f599077da3918a3238Greg Clayton
794d12aeab33bab559e138307f599077da3918a3238Greg Clayton    bool
795d12aeab33bab559e138307f599077da3918a3238Greg Clayton    Execute (Args& args, CommandReturnObject &result)
796d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
79740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        const size_t argc = args.GetArgumentCount();
79840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (argc == 0)
799d12aeab33bab559e138307f599077da3918a3238Greg Clayton        {
80040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            result.AppendError ("usage: 'commands regex <command-name> [s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
80140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            result.SetStatus (eReturnStatusFailed);
80240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
80340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        else
80440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
80540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            Error error;
806d12aeab33bab559e138307f599077da3918a3238Greg Clayton            const char *name = args.GetArgumentAtIndex(0);
807d12aeab33bab559e138307f599077da3918a3238Greg Clayton            m_regex_cmd_ap.reset (new CommandObjectRegexCommand (m_interpreter,
808d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                                 name,
809d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                                 m_options.GetHelp (),
810d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                                 m_options.GetSyntax (),
811d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                                 10));
81240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
81340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            if (argc == 1)
814d12aeab33bab559e138307f599077da3918a3238Greg Clayton            {
81540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
81640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                if (reader_sp)
81740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                {
81840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    error =reader_sp->Initialize (CommandObjectCommandsAddRegex::InputReaderCallback,
819d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                  this,                         // baton
820d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                  eInputReaderGranularityLine,  // token size, to pass to callback function
82140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                                  NULL,                         // end token
822d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                  "> ",                         // prompt
82340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                                  true);                        // echo input
82440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    if (error.Success())
82540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    {
82640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                        m_interpreter.GetDebugger().PushInputReader (reader_sp);
82740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                        result.SetStatus (eReturnStatusSuccessFinishNoResult);
82840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                        return true;
82940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    }
83040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                }
83140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            }
83240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            else
83340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            {
83440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                for (size_t arg_idx = 1; arg_idx < argc; ++arg_idx)
835d12aeab33bab559e138307f599077da3918a3238Greg Clayton                {
83640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    llvm::StringRef arg_strref (args.GetArgumentAtIndex(arg_idx));
83740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    error = AppendRegexSubstitution (arg_strref);
83840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    if (error.Fail())
83940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                        break;
840d12aeab33bab559e138307f599077da3918a3238Greg Clayton                }
84140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
84240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                if (error.Success())
843d12aeab33bab559e138307f599077da3918a3238Greg Clayton                {
84440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    AddRegexCommandToInterpreter();
845d12aeab33bab559e138307f599077da3918a3238Greg Clayton                }
846d12aeab33bab559e138307f599077da3918a3238Greg Clayton            }
84740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            if (error.Fail())
84840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            {
84940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                result.AppendError (error.AsCString());
85040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                result.SetStatus (eReturnStatusFailed);
85140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            }
852d12aeab33bab559e138307f599077da3918a3238Greg Clayton        }
85340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
854d12aeab33bab559e138307f599077da3918a3238Greg Clayton        return result.Succeeded();
855d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
856d12aeab33bab559e138307f599077da3918a3238Greg Clayton
85740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    Error
85840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    AppendRegexSubstitution (const llvm::StringRef &regex_sed)
859d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
86040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        Error error;
86140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
86240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (m_regex_cmd_ap.get() == NULL)
86340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
86440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("invalid regular expression command object for: '%.*s'",
86540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)regex_sed.size(),
86640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data());
86740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
86840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
86940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
87040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        size_t regex_sed_size = regex_sed.size();
87140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
87240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (regex_sed_size <= 1)
873d12aeab33bab559e138307f599077da3918a3238Greg Clayton        {
87440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("regular expression substitution string is too short: '%.*s'",
87540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)regex_sed.size(),
87640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data());
87740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
878d12aeab33bab559e138307f599077da3918a3238Greg Clayton        }
87940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
88040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (regex_sed[0] != 's')
88140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
88240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("regular expression substitution string doesn't start with 's': '%.*s'",
88340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)regex_sed.size(),
88440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data());
88540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
88640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
88740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        const size_t first_separator_char_pos = 1;
88840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        // use the char that follows 's' as the regex separator character
88940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        // so we can have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
89040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        const char separator_char = regex_sed[first_separator_char_pos];
89140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        const size_t second_separator_char_pos = regex_sed.find (separator_char, first_separator_char_pos + 1);
89240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
89340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (second_separator_char_pos == std::string::npos)
89440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
89540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("missing second '%c' separator char after '%.*s'",
89640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
89740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)(regex_sed.size() - first_separator_char_pos - 1),
89840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data() + (first_separator_char_pos + 1));
89940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
90040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
90140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
90240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        const size_t third_separator_char_pos = regex_sed.find (separator_char, second_separator_char_pos + 1);
90340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
90440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (third_separator_char_pos == std::string::npos)
90540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
90640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("missing third '%c' separator char after '%.*s'",
90740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
90840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)(regex_sed.size() - second_separator_char_pos - 1),
90940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data() + (second_separator_char_pos + 1));
91040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
91140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
91240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
91340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        if (third_separator_char_pos != regex_sed_size - 1)
91440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
91540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            // Make sure that everything that follows the last regex
91640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            // separator char
91740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            if (regex_sed.find_first_not_of("\t\n\v\f\r ", third_separator_char_pos + 1) != std::string::npos)
91840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            {
91940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                error.SetErrorStringWithFormat("extra data found after the '%.*s' regular expression substitution string: '%.*s'",
92040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                               (int)third_separator_char_pos + 1,
92140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                               regex_sed.data(),
92240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                               (int)(regex_sed.size() - third_separator_char_pos - 1),
92340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                               regex_sed.data() + (third_separator_char_pos + 1));
92440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                return error;
92540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            }
92640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
92740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
92840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        else if (first_separator_char_pos + 1 == second_separator_char_pos)
92940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
93040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
93140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
93240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
93340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
93440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)regex_sed.size(),
93540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data());
93640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
93740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
93840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        else if (second_separator_char_pos + 1 == third_separator_char_pos)
93940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        {
94040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            error.SetErrorStringWithFormat("<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
94140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
94240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
94340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           separator_char,
94440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           (int)regex_sed.size(),
94540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                           regex_sed.data());
94640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            return error;
94740e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        }
94840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        std::string regex(regex_sed.substr(first_separator_char_pos + 1, second_separator_char_pos - first_separator_char_pos - 1));
94940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        std::string subst(regex_sed.substr(second_separator_char_pos + 1, third_separator_char_pos - second_separator_char_pos - 1));
95040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        m_regex_cmd_ap->AddRegexCommand (regex.c_str(),
95140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                                         subst.c_str());
95240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        return error;
953d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
954d12aeab33bab559e138307f599077da3918a3238Greg Clayton
955d12aeab33bab559e138307f599077da3918a3238Greg Clayton    void
95640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    AddRegexCommandToInterpreter()
957d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
958d12aeab33bab559e138307f599077da3918a3238Greg Clayton        if (m_regex_cmd_ap.get())
959d12aeab33bab559e138307f599077da3918a3238Greg Clayton        {
960d12aeab33bab559e138307f599077da3918a3238Greg Clayton            if (m_regex_cmd_ap->HasRegexEntries())
961d12aeab33bab559e138307f599077da3918a3238Greg Clayton            {
962d12aeab33bab559e138307f599077da3918a3238Greg Clayton                CommandObjectSP cmd_sp (m_regex_cmd_ap.release());
963d12aeab33bab559e138307f599077da3918a3238Greg Clayton                m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
964d12aeab33bab559e138307f599077da3918a3238Greg Clayton            }
965d12aeab33bab559e138307f599077da3918a3238Greg Clayton        }
966d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
967d12aeab33bab559e138307f599077da3918a3238Greg Clayton
96840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    void
96940e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    InputReaderDidCancel()
97040e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    {
97140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton        m_regex_cmd_ap.reset();
97240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton    }
97340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton
974d12aeab33bab559e138307f599077da3918a3238Greg Clayton    static size_t
975d12aeab33bab559e138307f599077da3918a3238Greg Clayton    InputReaderCallback (void *baton,
976d12aeab33bab559e138307f599077da3918a3238Greg Clayton                         InputReader &reader,
977d12aeab33bab559e138307f599077da3918a3238Greg Clayton                         lldb::InputReaderAction notification,
978d12aeab33bab559e138307f599077da3918a3238Greg Clayton                         const char *bytes,
979d12aeab33bab559e138307f599077da3918a3238Greg Clayton                         size_t bytes_len);
980d12aeab33bab559e138307f599077da3918a3238Greg Claytonprivate:
981d12aeab33bab559e138307f599077da3918a3238Greg Clayton    std::auto_ptr<CommandObjectRegexCommand> m_regex_cmd_ap;
982d12aeab33bab559e138307f599077da3918a3238Greg Clayton
983d12aeab33bab559e138307f599077da3918a3238Greg Clayton     class CommandOptions : public Options
984d12aeab33bab559e138307f599077da3918a3238Greg Clayton     {
985d12aeab33bab559e138307f599077da3918a3238Greg Clayton     public:
986d12aeab33bab559e138307f599077da3918a3238Greg Clayton
987d12aeab33bab559e138307f599077da3918a3238Greg Clayton         CommandOptions (CommandInterpreter &interpreter) :
988d12aeab33bab559e138307f599077da3918a3238Greg Clayton            Options (interpreter)
989d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
990d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
991d12aeab33bab559e138307f599077da3918a3238Greg Clayton
992d12aeab33bab559e138307f599077da3918a3238Greg Clayton         virtual
993d12aeab33bab559e138307f599077da3918a3238Greg Clayton         ~CommandOptions (){}
994d12aeab33bab559e138307f599077da3918a3238Greg Clayton
995d12aeab33bab559e138307f599077da3918a3238Greg Clayton         virtual Error
996d12aeab33bab559e138307f599077da3918a3238Greg Clayton         SetOptionValue (uint32_t option_idx, const char *option_arg)
997d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
998d12aeab33bab559e138307f599077da3918a3238Greg Clayton             Error error;
999d12aeab33bab559e138307f599077da3918a3238Greg Clayton             char short_option = (char) m_getopt_table[option_idx].val;
1000d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1001d12aeab33bab559e138307f599077da3918a3238Greg Clayton             switch (short_option)
1002d12aeab33bab559e138307f599077da3918a3238Greg Clayton             {
1003d12aeab33bab559e138307f599077da3918a3238Greg Clayton                 case 'h':
1004d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     m_help.assign (option_arg);
1005d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     break;
1006d12aeab33bab559e138307f599077da3918a3238Greg Clayton                 case 's':
1007d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     m_syntax.assign (option_arg);
1008d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     break;
1009d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1010d12aeab33bab559e138307f599077da3918a3238Greg Clayton                 default:
1011d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
1012d12aeab33bab559e138307f599077da3918a3238Greg Clayton                     break;
1013d12aeab33bab559e138307f599077da3918a3238Greg Clayton             }
1014d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1015d12aeab33bab559e138307f599077da3918a3238Greg Clayton             return error;
1016d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1017d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1018d12aeab33bab559e138307f599077da3918a3238Greg Clayton         void
1019d12aeab33bab559e138307f599077da3918a3238Greg Clayton         OptionParsingStarting ()
1020d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
1021d12aeab33bab559e138307f599077da3918a3238Greg Clayton             m_help.clear();
1022d12aeab33bab559e138307f599077da3918a3238Greg Clayton             m_syntax.clear();
1023d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1024d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1025d12aeab33bab559e138307f599077da3918a3238Greg Clayton         const OptionDefinition*
1026d12aeab33bab559e138307f599077da3918a3238Greg Clayton         GetDefinitions ()
1027d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
1028d12aeab33bab559e138307f599077da3918a3238Greg Clayton             return g_option_table;
1029d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1030d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1031d12aeab33bab559e138307f599077da3918a3238Greg Clayton         // Options table: Required for subclasses of Options.
1032d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1033d12aeab33bab559e138307f599077da3918a3238Greg Clayton         static OptionDefinition g_option_table[];
1034d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1035d12aeab33bab559e138307f599077da3918a3238Greg Clayton         const char *
1036d12aeab33bab559e138307f599077da3918a3238Greg Clayton         GetHelp ()
1037d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
1038d12aeab33bab559e138307f599077da3918a3238Greg Clayton             if (m_help.empty())
1039d12aeab33bab559e138307f599077da3918a3238Greg Clayton                 return NULL;
1040d12aeab33bab559e138307f599077da3918a3238Greg Clayton             return m_help.c_str();
1041d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1042d12aeab33bab559e138307f599077da3918a3238Greg Clayton         const char *
1043d12aeab33bab559e138307f599077da3918a3238Greg Clayton         GetSyntax ()
1044d12aeab33bab559e138307f599077da3918a3238Greg Clayton         {
1045d12aeab33bab559e138307f599077da3918a3238Greg Clayton             if (m_syntax.empty())
1046d12aeab33bab559e138307f599077da3918a3238Greg Clayton                 return NULL;
1047d12aeab33bab559e138307f599077da3918a3238Greg Clayton             return m_syntax.c_str();
1048d12aeab33bab559e138307f599077da3918a3238Greg Clayton         }
1049d12aeab33bab559e138307f599077da3918a3238Greg Clayton         // Instance variables to hold the values for command options.
1050d12aeab33bab559e138307f599077da3918a3238Greg Clayton     protected:
1051d12aeab33bab559e138307f599077da3918a3238Greg Clayton         std::string m_help;
1052d12aeab33bab559e138307f599077da3918a3238Greg Clayton         std::string m_syntax;
1053d12aeab33bab559e138307f599077da3918a3238Greg Clayton     };
1054d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1055d12aeab33bab559e138307f599077da3918a3238Greg Clayton     CommandOptions m_options;
1056d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1057d12aeab33bab559e138307f599077da3918a3238Greg Clayton     virtual Options *
1058d12aeab33bab559e138307f599077da3918a3238Greg Clayton     GetOptions ()
1059d12aeab33bab559e138307f599077da3918a3238Greg Clayton     {
1060d12aeab33bab559e138307f599077da3918a3238Greg Clayton         return &m_options;
1061d12aeab33bab559e138307f599077da3918a3238Greg Clayton     }
1062d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1063d12aeab33bab559e138307f599077da3918a3238Greg Clayton};
1064d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1065d12aeab33bab559e138307f599077da3918a3238Greg Claytonsize_t
1066d12aeab33bab559e138307f599077da3918a3238Greg ClaytonCommandObjectCommandsAddRegex::InputReaderCallback (void *baton,
1067d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                    InputReader &reader,
1068d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                    lldb::InputReaderAction notification,
1069d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                    const char *bytes,
1070d12aeab33bab559e138307f599077da3918a3238Greg Clayton                                                    size_t bytes_len)
1071d12aeab33bab559e138307f599077da3918a3238Greg Clayton{
1072d12aeab33bab559e138307f599077da3918a3238Greg Clayton    CommandObjectCommandsAddRegex *add_regex_cmd = (CommandObjectCommandsAddRegex *) baton;
1073892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice    bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1074d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1075d12aeab33bab559e138307f599077da3918a3238Greg Clayton    switch (notification)
1076d12aeab33bab559e138307f599077da3918a3238Greg Clayton    {
1077d12aeab33bab559e138307f599077da3918a3238Greg Clayton        case eInputReaderActivate:
1078892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice            if (!batch_mode)
10792b5e4e6bc442fb77e7fe96c459f9ca38d4970ed0Caroline Tice            {
10802b5e4e6bc442fb77e7fe96c459f9ca38d4970ed0Caroline Tice                StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream ();
10812b5e4e6bc442fb77e7fe96c459f9ca38d4970ed0Caroline Tice                out_stream->Printf("%s\n", "Enter regular expressions in the form 's/<regex>/<subst>/' and terminate with an empty line:");
10822b5e4e6bc442fb77e7fe96c459f9ca38d4970ed0Caroline Tice                out_stream->Flush();
10832b5e4e6bc442fb77e7fe96c459f9ca38d4970ed0Caroline Tice            }
1084d12aeab33bab559e138307f599077da3918a3238Greg Clayton            break;
1085d12aeab33bab559e138307f599077da3918a3238Greg Clayton        case eInputReaderReactivate:
1086d12aeab33bab559e138307f599077da3918a3238Greg Clayton            break;
1087d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1088d12aeab33bab559e138307f599077da3918a3238Greg Clayton        case eInputReaderDeactivate:
1089d12aeab33bab559e138307f599077da3918a3238Greg Clayton            break;
10904a348081030cdd2af758fddc869518357d9befd3Caroline Tice
10914a348081030cdd2af758fddc869518357d9befd3Caroline Tice        case eInputReaderAsynchronousOutputWritten:
10924a348081030cdd2af758fddc869518357d9befd3Caroline Tice            break;
10934a348081030cdd2af758fddc869518357d9befd3Caroline Tice
1094d12aeab33bab559e138307f599077da3918a3238Greg Clayton        case eInputReaderGotToken:
109540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            while (bytes_len > 0 && (bytes[bytes_len-1] == '\r' || bytes[bytes_len-1] == '\n'))
109640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                --bytes_len;
1097d12aeab33bab559e138307f599077da3918a3238Greg Clayton            if (bytes_len == 0)
1098d12aeab33bab559e138307f599077da3918a3238Greg Clayton                reader.SetIsDone(true);
1099d12aeab33bab559e138307f599077da3918a3238Greg Clayton            else if (bytes)
1100d12aeab33bab559e138307f599077da3918a3238Greg Clayton            {
110140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                llvm::StringRef bytes_strref (bytes, bytes_len);
110240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                Error error (add_regex_cmd->AppendRegexSubstitution (bytes_strref));
110340e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                if (error.Fail())
1104d12aeab33bab559e138307f599077da3918a3238Greg Clayton                {
1105892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                    if (!batch_mode)
1106892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                    {
1107892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                        StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1108892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                        out_stream->Printf("error: %s\n", error.AsCString());
1109892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                        out_stream->Flush();
1110892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                    }
111140e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    add_regex_cmd->InputReaderDidCancel ();
111240e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                    reader.SetIsDone (true);
1113d12aeab33bab559e138307f599077da3918a3238Greg Clayton                }
1114d12aeab33bab559e138307f599077da3918a3238Greg Clayton            }
1115d12aeab33bab559e138307f599077da3918a3238Greg Clayton            break;
1116d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1117d12aeab33bab559e138307f599077da3918a3238Greg Clayton        case eInputReaderInterrupt:
11182b5e4e6bc442fb77e7fe96c459f9ca38d4970ed0Caroline Tice            {
11192b5e4e6bc442fb77e7fe96c459f9ca38d4970ed0Caroline Tice                reader.SetIsDone (true);
1120892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                if (!batch_mode)
1121892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                {
1122892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                    StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1123892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                    out_stream->PutCString("Regular expression command creations was cancelled.\n");
1124892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                    out_stream->Flush();
1125892fadd1f1001d1082cd2edcf282fee0cba8ac87Caroline Tice                }
11262b5e4e6bc442fb77e7fe96c459f9ca38d4970ed0Caroline Tice                add_regex_cmd->InputReaderDidCancel ();
11272b5e4e6bc442fb77e7fe96c459f9ca38d4970ed0Caroline Tice            }
1128d12aeab33bab559e138307f599077da3918a3238Greg Clayton            break;
1129d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1130d12aeab33bab559e138307f599077da3918a3238Greg Clayton        case eInputReaderEndOfFile:
1131d12aeab33bab559e138307f599077da3918a3238Greg Clayton            reader.SetIsDone (true);
1132d12aeab33bab559e138307f599077da3918a3238Greg Clayton            break;
1133d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1134d12aeab33bab559e138307f599077da3918a3238Greg Clayton        case eInputReaderDone:
113540e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton            add_regex_cmd->AddRegexCommandToInterpreter();
1136d12aeab33bab559e138307f599077da3918a3238Greg Clayton            break;
1137d12aeab33bab559e138307f599077da3918a3238Greg Clayton    }
1138d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1139d12aeab33bab559e138307f599077da3918a3238Greg Clayton    return bytes_len;
1140d12aeab33bab559e138307f599077da3918a3238Greg Clayton}
1141d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1142d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1143d12aeab33bab559e138307f599077da3918a3238Greg ClaytonOptionDefinition
1144d12aeab33bab559e138307f599077da3918a3238Greg ClaytonCommandObjectCommandsAddRegex::CommandOptions::g_option_table[] =
1145d12aeab33bab559e138307f599077da3918a3238Greg Clayton{
114640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton{ LLDB_OPT_SET_1, false, "help"  , 'h', required_argument, NULL, 0, eArgTypeNone, "The help text to display for this command."},
1147d12aeab33bab559e138307f599077da3918a3238Greg Clayton{ LLDB_OPT_SET_1, false, "syntax", 's', required_argument, NULL, 0, eArgTypeNone, "A syntax string showing the typical usage syntax."},
114840e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton{ 0             , false,  NULL   , 0  , 0                , NULL, 0, eArgTypeNone, NULL }
1149d12aeab33bab559e138307f599077da3918a3238Greg Clayton};
1150d12aeab33bab559e138307f599077da3918a3238Greg Clayton
1151d12aeab33bab559e138307f599077da3918a3238Greg Clayton
11526b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataclass CommandObjectPythonFunction : public CommandObject
11536b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
11546b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataprivate:
11556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    std::string m_function_name;
11566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
11576b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
11586b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
11596b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectPythonFunction (CommandInterpreter &interpreter,
11606b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                 std::string name,
11616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                 std::string funct) :
11626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObject (interpreter,
11636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   name.c_str(),
11646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   (std::string("Run Python function ") + funct).c_str(),
11656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   NULL),
11666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    m_function_name(funct)
11676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
1168e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata        ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
1169e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata        if (scripter)
1170e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata        {
1171e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata            std::string docstring = scripter->GetDocumentationForItem(funct.c_str());
1172e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata            if (!docstring.empty())
1173e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata                SetHelpLong(docstring);
1174e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata        }
11756b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
11766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
11776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    virtual
11786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectPythonFunction ()
11796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
11806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
11816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
11826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    virtual bool
11836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ExecuteRawCommandString (const char *raw_command_line, CommandReturnObject &result)
11846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
11856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
11866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
11876b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        Error error;
11886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
11896b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        if (!scripter || scripter->RunScriptBasedCommand(m_function_name.c_str(),
11906b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                         raw_command_line,
11916b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                         result,
11926b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                         error) == false)
11936b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
11946b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.AppendError(error.AsCString());
11956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus(eReturnStatusFailed);
11966b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
11976b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        else
11986b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus(eReturnStatusSuccessFinishNoResult);
11996b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12006b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return result.Succeeded();
12016b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
12026b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12036b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    virtual bool
12046b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    WantsRawCommandString ()
12056b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
12066b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return true;
12076b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
12086b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12096b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    bool
12106b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    Execute (Args& command,
12116b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata             CommandReturnObject &result)
12126b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
12136b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        std::string cmd_string;
12146b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        command.GetCommandString(cmd_string);
12156b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return ExecuteRawCommandString(cmd_string.c_str(), result);
12166b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
12176b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12186b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    virtual bool
12196b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    IsRemovable() { return true; }
12206b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12216b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
12226b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12236b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12246b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
12256b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata// CommandObjectCommandsScriptAdd
12266b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
12276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12286b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataclass CommandObjectCommandsScriptAdd : public CommandObject
12296b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
12306b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataprivate:
12316b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12326b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    class CommandOptions : public Options
12336b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
12346b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    public:
12356b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12366b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        CommandOptions (CommandInterpreter &interpreter) :
12376b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        Options (interpreter)
12386b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
12396b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
12406b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12416b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual
12426b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        ~CommandOptions (){}
12436b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12446b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual Error
12456b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        SetOptionValue (uint32_t option_idx, const char *option_arg)
12466b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
12476b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            Error error;
12486b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            char short_option = (char) m_getopt_table[option_idx].val;
12496b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12506b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            switch (short_option)
12516b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
12526b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                case 'f':
12536b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    m_funct_name = std::string(option_arg);
12546b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    break;
12556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                default:
12566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
12576b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    break;
12586b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
12596b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12606b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            return error;
12616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
12626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        void
12646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        OptionParsingStarting ()
12656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
12666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            m_funct_name = "";
12676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
12686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12696b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        const OptionDefinition*
12706b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        GetDefinitions ()
12716b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
12726b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            return g_option_table;
12736b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
12746b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12756b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // Options table: Required for subclasses of Options.
12766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        static OptionDefinition g_option_table[];
12786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // Instance variables to hold the values for command options.
12806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        std::string m_funct_name;
12826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    };
12836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandOptions m_options;
12856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    virtual Options *
12876b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    GetOptions ()
12886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
12896b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return &m_options;
12906b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
12916b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
12926b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    class PythonAliasReader : public InputReaderEZ
12936b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
12946b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    private:
12956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        CommandInterpreter& m_interpreter;
12966b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        std::string m_cmd_name;
12976b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        StringList m_user_input;
12986b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        DISALLOW_COPY_AND_ASSIGN (PythonAliasReader);
12996b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    public:
13006b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        PythonAliasReader(Debugger& debugger,
13016b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                          CommandInterpreter& interpreter,
13026b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                          std::string cmd_name) :
13036b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        InputReaderEZ(debugger),
13046b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_interpreter(interpreter),
13056b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_cmd_name(cmd_name),
13066b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_user_input()
13076b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {}
13086b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
13096b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual
13106b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        ~PythonAliasReader()
13116b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
13126b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
13136b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
13146b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void ActivateHandler(HandlerData& data)
13156b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
13166b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StreamSP out_stream = data.GetOutStream();
13176b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            bool batch_mode = data.GetBatchMode();
13186b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!batch_mode)
13196b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
13206b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("%s\n", g_python_command_instructions);
13216b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                if (data.reader.GetPrompt())
13226b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    out_stream->Printf ("%s", data.reader.GetPrompt());
13236b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
13246b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
13256b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
13266b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
13276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void ReactivateHandler(HandlerData& data)
13286b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
13296b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StreamSP out_stream = data.GetOutStream();
13306b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            bool batch_mode = data.GetBatchMode();
13316b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (data.reader.GetPrompt() && !batch_mode)
13326b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
13336b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("%s", data.reader.GetPrompt());
13346b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
13356b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
13366b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
13376b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void GotTokenHandler(HandlerData& data)
13386b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
13396b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StreamSP out_stream = data.GetOutStream();
13406b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            bool batch_mode = data.GetBatchMode();
13416b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (data.bytes && data.bytes_len)
13426b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
13436b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                m_user_input.AppendString(data.bytes, data.bytes_len);
13446b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
13456b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!data.reader.IsDone() && data.reader.GetPrompt() && !batch_mode)
13466b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
13476b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("%s", data.reader.GetPrompt());
13486b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
13496b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
13506b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
13516b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void InterruptHandler(HandlerData& data)
13526b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
13536b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StreamSP out_stream = data.GetOutStream();
13546b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            bool batch_mode = data.GetBatchMode();
13556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            data.reader.SetIsDone (true);
13566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!batch_mode)
13576b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
13586b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("Warning: No command attached to breakpoint.\n");
13596b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
13606b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
13616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
13626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void EOFHandler(HandlerData& data)
13636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
13646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            data.reader.SetIsDone (true);
13656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
13666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        virtual void DoneHandler(HandlerData& data)
13676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
13686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StreamSP out_stream = data.GetOutStream();
13696b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
13706b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            ScriptInterpreter *interpreter = data.reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
13716b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!interpreter)
13726b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
13736b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("Internal error #1: no script attached.\n");
13746b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
13756b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                return;
13766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
13776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            StringList funct_name_sl;
13786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!interpreter->GenerateScriptAliasFunction (m_user_input,
13796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                           funct_name_sl))
13806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
13816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("Internal error #2: no script attached.\n");
13826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
13836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                return;
13846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
13856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (funct_name_sl.GetSize() == 0)
13866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
13876b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("Internal error #3: no script attached.\n");
13886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
13896b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                return;
13906b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
13916b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            const char *funct_name = funct_name_sl.GetStringAtIndex(0);
13926b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!funct_name || !funct_name[0])
13936b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
13946b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("Internal error #4: no script attached.\n");
13956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
13966b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                return;
13976b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
13986b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
13996b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            // everything should be fine now, let's add this alias
14006b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14016b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(m_interpreter,
14026b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                                           m_cmd_name,
14036b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                                           funct_name));
14046b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14056b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (!m_interpreter.AddUserCommand(m_cmd_name.c_str(), command_obj_sp, true))
14066b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
14076b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Printf ("Internal error #5: no script attached.\n");
14086b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                out_stream->Flush();
14096b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                return;
14106b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
14116b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
14126b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    };
14136b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14146b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
14156b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter) :
14166b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObject (interpreter,
14176b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   "command script add",
14186b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   "Add a scripted function as an LLDB command.",
14196b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   NULL),
14206b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    m_options (interpreter)
14216b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
14226b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        CommandArgumentEntry arg1;
14236b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        CommandArgumentData cmd_arg;
14246b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14256b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // Define the first (and only) variant of this arg.
14266b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        cmd_arg.arg_type = eArgTypeCommandName;
14276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        cmd_arg.arg_repetition = eArgRepeatPlain;
14286b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14296b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // There is only one variant this argument could be; put it into the argument entry.
14306b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        arg1.push_back (cmd_arg);
14316b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14326b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // Push the data for the first argument into the m_arguments vector.
14336b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_arguments.push_back (arg1);
14346b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
14356b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14366b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectCommandsScriptAdd ()
14376b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
14386b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
14396b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14406b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    bool
14416b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    Execute
14426b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    (
14436b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     Args& args,
14446b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     CommandReturnObject &result
14456b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     )
14466b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
1447e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata
1448e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata        if (m_interpreter.GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython)
1449e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata        {
1450e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata            result.AppendError ("only scripting language supported for scripted commands is currently Python");
1451e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata            result.SetStatus (eReturnStatusFailed);
1452e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata            return false;
1453e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata        }
1454e5e34cb15f031237004b05abfa623c71f879f6c0Enrico Granata
14556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        size_t argc = args.GetArgumentCount();
14566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14576b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        if (argc != 1)
14586b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
14596b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.AppendError ("'command script add' requires one argument");
14606b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus (eReturnStatusFailed);
14616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            return false;
14626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
14636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        std::string cmd_name = args.GetArgumentAtIndex(0);
14656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        if (m_options.m_funct_name.empty())
14676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
14686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            InputReaderSP reader_sp (new PythonAliasReader (m_interpreter.GetDebugger(),
14696b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                            m_interpreter,
14706b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                                                            cmd_name));
14716b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14726b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (reader_sp)
14736b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
14746b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14756b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                InputReaderEZ::InitializationParameters ipr;
14766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
14776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                Error err (reader_sp->Initialize (ipr.SetBaton(NULL).SetPrompt("     ")));
14786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                if (err.Success())
14796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                {
14806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    m_interpreter.GetDebugger().PushInputReader (reader_sp);
14816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    result.SetStatus (eReturnStatusSuccessFinishNoResult);
14826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                }
14836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                else
14846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                {
14856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    result.AppendError (err.AsCString());
14866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                    result.SetStatus (eReturnStatusFailed);
14876b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                }
14886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
14896b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            else
14906b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
14916b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                result.AppendError("out of memory");
14926b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                result.SetStatus (eReturnStatusFailed);
14936b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
14946b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
14956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        else
14966b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
14976b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            CommandObjectSP new_cmd(new CommandObjectPythonFunction(m_interpreter, cmd_name, m_options.m_funct_name));
14986b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            if (m_interpreter.AddUserCommand(cmd_name.c_str(), new_cmd, true))
14996b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
15006b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                result.SetStatus (eReturnStatusSuccessFinishNoResult);
15016b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
15026b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            else
15036b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            {
15046b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                result.AppendError("cannot add command");
15056b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                result.SetStatus (eReturnStatusFailed);
15066b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            }
15076b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
15086b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15096b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return result.Succeeded();
15106b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15116b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
15126b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
15136b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15146b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico GranataOptionDefinition
15156b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico GranataCommandObjectCommandsScriptAdd::CommandOptions::g_option_table[] =
15166b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
15176b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    { LLDB_OPT_SET_1, false, "function", 'f', required_argument, NULL, 0, eArgTypeName,        "Name of a Python function to use."},
15186b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
15196b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
15206b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15216b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
15226b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata// CommandObjectCommandsScriptList
15236b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
15246b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15256b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataclass CommandObjectCommandsScriptList : public CommandObject
15266b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
15276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataprivate:
15286b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15296b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
15306b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectCommandsScriptList(CommandInterpreter &interpreter) :
15316b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObject (interpreter,
15326b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   "command script list",
15336b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   "List defined scripted commands.",
15346b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   NULL)
15356b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
15366b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
15376b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15386b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectCommandsScriptList ()
15396b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
15406b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
15416b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15426b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    bool
15436b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    Execute
15446b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    (
15456b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     Args& args,
15466b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     CommandReturnObject &result
15476b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     )
15486b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
15496b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15506b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_interpreter.GetHelp(result,
15516b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                              CommandInterpreter::eCommandTypesUserDef);
15526b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15536b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        result.SetStatus (eReturnStatusSuccessFinishResult);
15546b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return true;
15566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15576b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15586b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
15596b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
15606b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
15626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata// CommandObjectCommandsScriptClear
15636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
15646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataclass CommandObjectCommandsScriptClear : public CommandObject
15666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
15676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataprivate:
15686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15696b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
15706b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectCommandsScriptClear(CommandInterpreter &interpreter) :
15716b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObject (interpreter,
15726b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   "command script clear",
15736b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   "Delete all scripted commands.",
15746b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   NULL)
15756b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
15766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
15776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectCommandsScriptClear ()
15796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
15806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
15816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    bool
15836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    Execute
15846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    (
15856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     Args& args,
15866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     CommandReturnObject &result
15876b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     )
15886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
15896b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15906b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_interpreter.RemoveAllUser();
15916b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15926b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        result.SetStatus (eReturnStatusSuccessFinishResult);
15936b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15946b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return true;
15956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15966b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
15976b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
15986b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
15996b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16006b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
16016b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata// CommandObjectCommandsScriptDelete
16026b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
16036b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16046b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataclass CommandObjectCommandsScriptDelete : public CommandObject
16056b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
16066b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataprivate:
16076b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16086b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
16096b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter) :
16106b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObject (interpreter,
16116b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   "command script delete",
16126b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   "Delete a scripted command.",
16136b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                   NULL)
16146b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
16156b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        CommandArgumentEntry arg1;
16166b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        CommandArgumentData cmd_arg;
16176b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16186b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // Define the first (and only) variant of this arg.
16196b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        cmd_arg.arg_type = eArgTypeCommandName;
16206b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        cmd_arg.arg_repetition = eArgRepeatPlain;
16216b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16226b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // There is only one variant this argument could be; put it into the argument entry.
16236b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        arg1.push_back (cmd_arg);
16246b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16256b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        // Push the data for the first argument into the m_arguments vector.
16266b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        m_arguments.push_back (arg1);
16276b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
16286b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16296b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectCommandsScriptDelete ()
16306b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
16316b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
16326b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16336b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    bool
16346b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    Execute
16356b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    (
16366b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     Args& args,
16376b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     CommandReturnObject &result
16386b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata     )
16396b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
16406b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16416b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        size_t argc = args.GetArgumentCount();
16426b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16436b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        if (argc != 1)
16446b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
16456b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.AppendError ("'command script delete' requires one argument");
16466b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus (eReturnStatusFailed);
16476b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            return false;
16486b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
16496b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16506b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        const char* cmd_name = args.GetArgumentAtIndex(0);
16516b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16526b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        if (cmd_name && *cmd_name && m_interpreter.HasUserCommands() && m_interpreter.UserCommandExists(cmd_name))
16536b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
16546b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            m_interpreter.RemoveUser(cmd_name);
16556b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus (eReturnStatusSuccessFinishResult);
16566b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
16576b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        else
16586b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        {
16596b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.AppendErrorWithFormat ("command %s not found", cmd_name);
16606b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata            result.SetStatus (eReturnStatusFailed);
16616b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        }
16626b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16636b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        return result.Succeeded();
16646b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16656b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
16666b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
16676b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16686b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata#pragma mark CommandObjectMultiwordCommandsScript
16696b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16706b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
16716b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata// CommandObjectMultiwordCommandsScript
16726b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata//-------------------------------------------------------------------------
16736b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16746b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granataclass CommandObjectMultiwordCommandsScript : public CommandObjectMultiword
16756b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata{
16766b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granatapublic:
16776b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectMultiwordCommandsScript (CommandInterpreter &interpreter) :
16786b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    CommandObjectMultiword (interpreter,
16796b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                            "command script",
16806b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                            "A set of commands for managing or customizing script commands.",
16816b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata                            "command script <subcommand> [<subcommand-options>]")
16826b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
16836b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        LoadSubCommand ("add",  CommandObjectSP (new CommandObjectCommandsScriptAdd (interpreter)));
16846b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        LoadSubCommand ("delete",   CommandObjectSP (new CommandObjectCommandsScriptDelete (interpreter)));
16856b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        LoadSubCommand ("clear", CommandObjectSP (new CommandObjectCommandsScriptClear (interpreter)));
16866b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata        LoadSubCommand ("list",   CommandObjectSP (new CommandObjectCommandsScriptList (interpreter)));
16876b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
16886b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16896b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    ~CommandObjectMultiwordCommandsScript ()
16906b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    {
16916b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    }
16926b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16936b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata};
16946b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
16956b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata
1696767af88aa617288e584afcfed055f7755e408542Jim Ingham#pragma mark CommandObjectMultiwordCommands
1697767af88aa617288e584afcfed055f7755e408542Jim Ingham
1698767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
1699767af88aa617288e584afcfed055f7755e408542Jim Ingham// CommandObjectMultiwordCommands
1700767af88aa617288e584afcfed055f7755e408542Jim Ingham//-------------------------------------------------------------------------
1701767af88aa617288e584afcfed055f7755e408542Jim Ingham
1702767af88aa617288e584afcfed055f7755e408542Jim InghamCommandObjectMultiwordCommands::CommandObjectMultiwordCommands (CommandInterpreter &interpreter) :
1703238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    CommandObjectMultiword (interpreter,
170440e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                            "command",
1705c1ad82eb979ee856b86aec6e2acb7bddf75f7c4fCaroline Tice                            "A set of commands for managing or customizing the debugger commands.",
170640e482434007a7cac49fb1f437fe7d13db2cf523Greg Clayton                            "command <subcommand> [<subcommand-options>]")
1707767af88aa617288e584afcfed055f7755e408542Jim Ingham{
1708238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    LoadSubCommand ("source",  CommandObjectSP (new CommandObjectCommandsSource (interpreter)));
1709238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    LoadSubCommand ("alias",   CommandObjectSP (new CommandObjectCommandsAlias (interpreter)));
1710238c0a1e7b733cee539258faa656159c63f9e893Greg Clayton    LoadSubCommand ("unalias", CommandObjectSP (new CommandObjectCommandsUnalias (interpreter)));
1711d12aeab33bab559e138307f599077da3918a3238Greg Clayton    LoadSubCommand ("regex",   CommandObjectSP (new CommandObjectCommandsAddRegex (interpreter)));
17126247dbee41ec51e9a082df7e86269c0f47f28160Jim Ingham    LoadSubCommand ("history",   CommandObjectSP (new CommandObjectCommandsHistory (interpreter)));
17136b1596d81c34c6efb10ed51a3572d6b145b73f5bEnrico Granata    LoadSubCommand ("script",   CommandObjectSP (new CommandObjectMultiwordCommandsScript (interpreter)));
1714767af88aa617288e584afcfed055f7755e408542Jim Ingham}
1715767af88aa617288e584afcfed055f7755e408542Jim Ingham
1716767af88aa617288e584afcfed055f7755e408542Jim InghamCommandObjectMultiwordCommands::~CommandObjectMultiwordCommands ()
1717767af88aa617288e584afcfed055f7755e408542Jim Ingham{
1718767af88aa617288e584afcfed055f7755e408542Jim Ingham}
1719767af88aa617288e584afcfed055f7755e408542Jim Ingham
1720