1ebc09c36SJim Ingham //===-- CommandObjectSource.cpp ---------------------------------*- C++ -*-===//
2ebc09c36SJim Ingham //
3ebc09c36SJim Ingham //                     The LLVM Compiler Infrastructure
4ebc09c36SJim Ingham //
5ebc09c36SJim Ingham // This file is distributed under the University of Illinois Open Source
6ebc09c36SJim Ingham // License. See LICENSE.TXT for details.
7ebc09c36SJim Ingham //
8ebc09c36SJim Ingham //===----------------------------------------------------------------------===//
9ebc09c36SJim Ingham 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
12ebc09c36SJim Ingham #include "CommandObjectCommands.h"
13ebc09c36SJim Ingham 
14ebc09c36SJim Ingham // C Includes
15ebc09c36SJim Ingham // C++ Includes
16ebc09c36SJim Ingham // Other libraries and framework includes
170e5e5a79SGreg Clayton #include "llvm/ADT/StringRef.h"
180e5e5a79SGreg Clayton 
19ebc09c36SJim Ingham // Project includes
20ebc09c36SJim Ingham #include "lldb/Core/Debugger.h"
2144d93782SGreg Clayton #include "lldb/Core/IOHandler.h"
22be93a35aSEnrico Granata #include "lldb/Core/StringList.h"
23de164aaaSGreg Clayton #include "lldb/Interpreter/Args.h"
247594f14fSEnrico Granata #include "lldb/Interpreter/CommandHistory.h"
25ebc09c36SJim Ingham #include "lldb/Interpreter/CommandInterpreter.h"
26de164aaaSGreg Clayton #include "lldb/Interpreter/CommandObjectRegexCommand.h"
27ebc09c36SJim Ingham #include "lldb/Interpreter/CommandReturnObject.h"
28012d4fcaSEnrico Granata #include "lldb/Interpreter/OptionValueBoolean.h"
297594f14fSEnrico Granata #include "lldb/Interpreter/OptionValueUInt64.h"
30ebc09c36SJim Ingham #include "lldb/Interpreter/Options.h"
3199f0b8f9SEnrico Granata #include "lldb/Interpreter/ScriptInterpreter.h"
3299f0b8f9SEnrico Granata #include "lldb/Interpreter/ScriptInterpreterPython.h"
33ebc09c36SJim Ingham 
34ebc09c36SJim Ingham using namespace lldb;
35ebc09c36SJim Ingham using namespace lldb_private;
36ebc09c36SJim Ingham 
37ebc09c36SJim Ingham //-------------------------------------------------------------------------
38ebc09c36SJim Ingham // CommandObjectCommandsSource
39ebc09c36SJim Ingham //-------------------------------------------------------------------------
40ebc09c36SJim Ingham 
415a988416SJim Ingham class CommandObjectCommandsHistory : public CommandObjectParsed
42a5a97ebeSJim Ingham {
435a988416SJim Ingham public:
445a988416SJim Ingham     CommandObjectCommandsHistory(CommandInterpreter &interpreter) :
455a988416SJim Ingham         CommandObjectParsed (interpreter,
465a988416SJim Ingham                              "command history",
475a988416SJim Ingham                              "Dump the history of commands in this session.",
485a988416SJim Ingham                              NULL),
495a988416SJim Ingham         m_options (interpreter)
505a988416SJim Ingham     {
515a988416SJim Ingham     }
525a988416SJim Ingham 
535a988416SJim Ingham     ~CommandObjectCommandsHistory () {}
545a988416SJim Ingham 
555a988416SJim Ingham     virtual Options *
565a988416SJim Ingham     GetOptions ()
575a988416SJim Ingham     {
585a988416SJim Ingham         return &m_options;
595a988416SJim Ingham     }
605a988416SJim Ingham 
615a988416SJim Ingham protected:
62a5a97ebeSJim Ingham 
63a5a97ebeSJim Ingham     class CommandOptions : public Options
64a5a97ebeSJim Ingham     {
65a5a97ebeSJim Ingham     public:
66a5a97ebeSJim Ingham 
67a5a97ebeSJim Ingham         CommandOptions (CommandInterpreter &interpreter) :
687594f14fSEnrico Granata             Options (interpreter),
697594f14fSEnrico Granata             m_start_idx(0),
707594f14fSEnrico Granata             m_stop_idx(0),
717594f14fSEnrico Granata             m_count(0),
7263123b64SEnrico Granata             m_clear(false)
73a5a97ebeSJim Ingham         {
74a5a97ebeSJim Ingham         }
75a5a97ebeSJim Ingham 
76a5a97ebeSJim Ingham         virtual
77a5a97ebeSJim Ingham         ~CommandOptions (){}
78a5a97ebeSJim Ingham 
79a5a97ebeSJim Ingham         virtual Error
80a5a97ebeSJim Ingham         SetOptionValue (uint32_t option_idx, const char *option_arg)
81a5a97ebeSJim Ingham         {
82a5a97ebeSJim Ingham             Error error;
833bcdfc0eSGreg Clayton             const int short_option = m_getopt_table[option_idx].val;
84a5a97ebeSJim Ingham 
85a5a97ebeSJim Ingham             switch (short_option)
86a5a97ebeSJim Ingham             {
87a5a97ebeSJim Ingham                 case 'c':
88c95f7e2aSPavel Labath                     error = m_count.SetValueFromString(option_arg,eVarSetOperationAssign);
89a5a97ebeSJim Ingham                     break;
90a5a97ebeSJim Ingham                 case 's':
917594f14fSEnrico Granata                     if (option_arg && strcmp("end", option_arg) == 0)
927594f14fSEnrico Granata                     {
937594f14fSEnrico Granata                         m_start_idx.SetCurrentValue(UINT64_MAX);
947594f14fSEnrico Granata                         m_start_idx.SetOptionWasSet();
957594f14fSEnrico Granata                     }
967594f14fSEnrico Granata                     else
97c95f7e2aSPavel Labath                         error = m_start_idx.SetValueFromString(option_arg,eVarSetOperationAssign);
987594f14fSEnrico Granata                     break;
997594f14fSEnrico Granata                 case 'e':
100c95f7e2aSPavel Labath                     error = m_stop_idx.SetValueFromString(option_arg,eVarSetOperationAssign);
1017594f14fSEnrico Granata                     break;
10263123b64SEnrico Granata                 case 'C':
10363123b64SEnrico Granata                     m_clear.SetCurrentValue(true);
10463123b64SEnrico Granata                     m_clear.SetOptionWasSet();
105a5a97ebeSJim Ingham                     break;
106a5a97ebeSJim Ingham                 default:
10786edbf41SGreg Clayton                     error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
108a5a97ebeSJim Ingham                     break;
109a5a97ebeSJim Ingham             }
110a5a97ebeSJim Ingham 
111a5a97ebeSJim Ingham             return error;
112a5a97ebeSJim Ingham         }
113a5a97ebeSJim Ingham 
114a5a97ebeSJim Ingham         void
115a5a97ebeSJim Ingham         OptionParsingStarting ()
116a5a97ebeSJim Ingham         {
1177594f14fSEnrico Granata             m_start_idx.Clear();
1187594f14fSEnrico Granata             m_stop_idx.Clear();
1197594f14fSEnrico Granata             m_count.Clear();
12063123b64SEnrico Granata             m_clear.Clear();
121a5a97ebeSJim Ingham         }
122a5a97ebeSJim Ingham 
123a5a97ebeSJim Ingham         const OptionDefinition*
124a5a97ebeSJim Ingham         GetDefinitions ()
125a5a97ebeSJim Ingham         {
126a5a97ebeSJim Ingham             return g_option_table;
127a5a97ebeSJim Ingham         }
128a5a97ebeSJim Ingham 
129a5a97ebeSJim Ingham         // Options table: Required for subclasses of Options.
130a5a97ebeSJim Ingham 
131a5a97ebeSJim Ingham         static OptionDefinition g_option_table[];
132a5a97ebeSJim Ingham 
133a5a97ebeSJim Ingham         // Instance variables to hold the values for command options.
134a5a97ebeSJim Ingham 
1357594f14fSEnrico Granata         OptionValueUInt64 m_start_idx;
1367594f14fSEnrico Granata         OptionValueUInt64 m_stop_idx;
1377594f14fSEnrico Granata         OptionValueUInt64 m_count;
13863123b64SEnrico Granata         OptionValueBoolean m_clear;
139a5a97ebeSJim Ingham     };
140a5a97ebeSJim Ingham 
141a5a97ebeSJim Ingham     bool
1425a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
143a5a97ebeSJim Ingham     {
14463123b64SEnrico Granata         if (m_options.m_clear.GetCurrentValue() && m_options.m_clear.OptionWasSet())
1457594f14fSEnrico Granata         {
1467594f14fSEnrico Granata             m_interpreter.GetCommandHistory().Clear();
1477594f14fSEnrico Granata             result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
1487594f14fSEnrico Granata         }
1497594f14fSEnrico Granata         else
1507594f14fSEnrico Granata         {
1517594f14fSEnrico Granata             if (m_options.m_start_idx.OptionWasSet() && m_options.m_stop_idx.OptionWasSet() && m_options.m_count.OptionWasSet())
1527594f14fSEnrico Granata             {
1537594f14fSEnrico Granata                 result.AppendError("--count, --start-index and --end-index cannot be all specified in the same invocation");
1547594f14fSEnrico Granata                 result.SetStatus(lldb::eReturnStatusFailed);
1557594f14fSEnrico Granata             }
1567594f14fSEnrico Granata             else
1577594f14fSEnrico Granata             {
15884400ec7SVirgile Bello                 std::pair<bool,uint64_t> start_idx(m_options.m_start_idx.OptionWasSet(),m_options.m_start_idx.GetCurrentValue());
15984400ec7SVirgile Bello                 std::pair<bool,uint64_t> stop_idx(m_options.m_stop_idx.OptionWasSet(),m_options.m_stop_idx.GetCurrentValue());
16084400ec7SVirgile Bello                 std::pair<bool,uint64_t> count(m_options.m_count.OptionWasSet(),m_options.m_count.GetCurrentValue());
161a5a97ebeSJim Ingham 
1627594f14fSEnrico Granata                 const CommandHistory& history(m_interpreter.GetCommandHistory());
1637594f14fSEnrico Granata 
1647594f14fSEnrico Granata                 if (start_idx.first && start_idx.second == UINT64_MAX)
1657594f14fSEnrico Granata                 {
1667594f14fSEnrico Granata                     if (count.first)
1677594f14fSEnrico Granata                     {
1687594f14fSEnrico Granata                         start_idx.second = history.GetSize() - count.second;
1697594f14fSEnrico Granata                         stop_idx.second = history.GetSize() - 1;
1707594f14fSEnrico Granata                     }
1717594f14fSEnrico Granata                     else if (stop_idx.first)
1727594f14fSEnrico Granata                     {
1737594f14fSEnrico Granata                         start_idx.second = stop_idx.second;
1747594f14fSEnrico Granata                         stop_idx.second = history.GetSize() - 1;
1757594f14fSEnrico Granata                     }
1767594f14fSEnrico Granata                     else
1777594f14fSEnrico Granata                     {
1787594f14fSEnrico Granata                         start_idx.second = 0;
1797594f14fSEnrico Granata                         stop_idx.second = history.GetSize() - 1;
1807594f14fSEnrico Granata                     }
1817594f14fSEnrico Granata                 }
1827594f14fSEnrico Granata                 else
1837594f14fSEnrico Granata                 {
1847594f14fSEnrico Granata                     if (!start_idx.first && !stop_idx.first && !count.first)
1857594f14fSEnrico Granata                     {
1867594f14fSEnrico Granata                         start_idx.second = 0;
1877594f14fSEnrico Granata                         stop_idx.second = history.GetSize() - 1;
1887594f14fSEnrico Granata                     }
1897594f14fSEnrico Granata                     else if (start_idx.first)
1907594f14fSEnrico Granata                     {
1917594f14fSEnrico Granata                         if (count.first)
1927594f14fSEnrico Granata                         {
1937594f14fSEnrico Granata                             stop_idx.second = start_idx.second + count.second - 1;
1947594f14fSEnrico Granata                         }
1957594f14fSEnrico Granata                         else if (!stop_idx.first)
1967594f14fSEnrico Granata                         {
1977594f14fSEnrico Granata                             stop_idx.second = history.GetSize() - 1;
1987594f14fSEnrico Granata                         }
1997594f14fSEnrico Granata                     }
2007594f14fSEnrico Granata                     else if (stop_idx.first)
2017594f14fSEnrico Granata                     {
2027594f14fSEnrico Granata                         if (count.first)
2037594f14fSEnrico Granata                         {
2047594f14fSEnrico Granata                             if (stop_idx.second >= count.second)
2057594f14fSEnrico Granata                                 start_idx.second = stop_idx.second - count.second + 1;
2067594f14fSEnrico Granata                             else
2077594f14fSEnrico Granata                                 start_idx.second = 0;
2087594f14fSEnrico Granata                         }
2097594f14fSEnrico Granata                     }
2107594f14fSEnrico Granata                     else /* if (count.first) */
2117594f14fSEnrico Granata                     {
2127594f14fSEnrico Granata                         start_idx.second = 0;
2137594f14fSEnrico Granata                         stop_idx.second = count.second - 1;
2147594f14fSEnrico Granata                     }
2157594f14fSEnrico Granata                 }
2167594f14fSEnrico Granata                 history.Dump(result.GetOutputStream(), start_idx.second, stop_idx.second);
2177594f14fSEnrico Granata             }
2187594f14fSEnrico Granata         }
219a5a97ebeSJim Ingham         return result.Succeeded();
220a5a97ebeSJim Ingham 
221a5a97ebeSJim Ingham     }
2225a988416SJim Ingham 
2235a988416SJim Ingham     CommandOptions m_options;
224a5a97ebeSJim Ingham };
225a5a97ebeSJim Ingham 
226a5a97ebeSJim Ingham OptionDefinition
227a5a97ebeSJim Ingham CommandObjectCommandsHistory::CommandOptions::g_option_table[] =
228a5a97ebeSJim Ingham {
229d37221dcSZachary Turner { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeUnsignedInteger,        "How many history commands to print."},
230d37221dcSZachary Turner { LLDB_OPT_SET_1, false, "start-index", 's', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeUnsignedInteger,  "Index at which to start printing history commands (or end to mean tail mode)."},
231d37221dcSZachary Turner { LLDB_OPT_SET_1, false, "end-index", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeUnsignedInteger,    "Index at which to stop printing history commands."},
232d37221dcSZachary Turner { LLDB_OPT_SET_2, false, "clear", 'C', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeBoolean,    "Clears the current command history."},
233d37221dcSZachary Turner { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
234a5a97ebeSJim Ingham };
235a5a97ebeSJim Ingham 
236a5a97ebeSJim Ingham 
237a5a97ebeSJim Ingham //-------------------------------------------------------------------------
238a5a97ebeSJim Ingham // CommandObjectCommandsSource
239a5a97ebeSJim Ingham //-------------------------------------------------------------------------
240a5a97ebeSJim Ingham 
2415a988416SJim Ingham class CommandObjectCommandsSource : public CommandObjectParsed
242ebc09c36SJim Ingham {
2435a988416SJim Ingham public:
2445a988416SJim Ingham     CommandObjectCommandsSource(CommandInterpreter &interpreter) :
2455a988416SJim Ingham         CommandObjectParsed (interpreter,
2465a988416SJim Ingham                              "command source",
2475a988416SJim Ingham                              "Read in debugger commands from the file <filename> and execute them.",
2485a988416SJim Ingham                              NULL),
2495a988416SJim Ingham         m_options (interpreter)
2505a988416SJim Ingham     {
2515a988416SJim Ingham         CommandArgumentEntry arg;
2525a988416SJim Ingham         CommandArgumentData file_arg;
2535a988416SJim Ingham 
2545a988416SJim Ingham         // Define the first (and only) variant of this arg.
2555a988416SJim Ingham         file_arg.arg_type = eArgTypeFilename;
2565a988416SJim Ingham         file_arg.arg_repetition = eArgRepeatPlain;
2575a988416SJim Ingham 
2585a988416SJim Ingham         // There is only one variant this argument could be; put it into the argument entry.
2595a988416SJim Ingham         arg.push_back (file_arg);
2605a988416SJim Ingham 
2615a988416SJim Ingham         // Push the data for the first argument into the m_arguments vector.
2625a988416SJim Ingham         m_arguments.push_back (arg);
2635a988416SJim Ingham     }
2645a988416SJim Ingham 
2655a988416SJim Ingham     ~CommandObjectCommandsSource () {}
2665a988416SJim Ingham 
2675a988416SJim Ingham     virtual const char*
2685a988416SJim Ingham     GetRepeatCommand (Args &current_command_args, uint32_t index)
2695a988416SJim Ingham     {
2705a988416SJim Ingham         return "";
2715a988416SJim Ingham     }
2725a988416SJim Ingham 
273c7bece56SGreg Clayton     virtual int
2745a988416SJim Ingham     HandleArgumentCompletion (Args &input,
2755a988416SJim Ingham                               int &cursor_index,
2765a988416SJim Ingham                               int &cursor_char_position,
2775a988416SJim Ingham                               OptionElementVector &opt_element_vector,
2785a988416SJim Ingham                               int match_start_point,
2795a988416SJim Ingham                               int max_return_elements,
2805a988416SJim Ingham                               bool &word_complete,
2815a988416SJim Ingham                               StringList &matches)
2825a988416SJim Ingham     {
2835a988416SJim Ingham         std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2845a988416SJim Ingham         completion_str.erase (cursor_char_position);
2855a988416SJim Ingham 
2865a988416SJim Ingham         CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2875a988416SJim Ingham                                                              CommandCompletions::eDiskFileCompletion,
2885a988416SJim Ingham                                                              completion_str.c_str(),
2895a988416SJim Ingham                                                              match_start_point,
2905a988416SJim Ingham                                                              max_return_elements,
2915a988416SJim Ingham                                                              NULL,
2925a988416SJim Ingham                                                              word_complete,
2935a988416SJim Ingham                                                              matches);
2945a988416SJim Ingham         return matches.GetSize();
2955a988416SJim Ingham     }
2965a988416SJim Ingham 
2975a988416SJim Ingham     virtual Options *
2985a988416SJim Ingham     GetOptions ()
2995a988416SJim Ingham     {
3005a988416SJim Ingham         return &m_options;
3015a988416SJim Ingham     }
3025a988416SJim Ingham 
3035a988416SJim Ingham protected:
304e16c50a1SJim Ingham 
305e16c50a1SJim Ingham     class CommandOptions : public Options
306e16c50a1SJim Ingham     {
307e16c50a1SJim Ingham     public:
308e16c50a1SJim Ingham 
309eb0103f2SGreg Clayton         CommandOptions (CommandInterpreter &interpreter) :
310012d4fcaSEnrico Granata             Options (interpreter),
311340b0309SGreg Clayton             m_stop_on_error (true),
312340b0309SGreg Clayton             m_silent_run (false),
313340b0309SGreg Clayton             m_stop_on_continue (true)
314eb0103f2SGreg Clayton         {
315eb0103f2SGreg Clayton         }
316e16c50a1SJim Ingham 
317e16c50a1SJim Ingham         virtual
318e16c50a1SJim Ingham         ~CommandOptions (){}
319e16c50a1SJim Ingham 
320e16c50a1SJim Ingham         virtual Error
321f6b8b581SGreg Clayton         SetOptionValue (uint32_t option_idx, const char *option_arg)
322e16c50a1SJim Ingham         {
323e16c50a1SJim Ingham             Error error;
3243bcdfc0eSGreg Clayton             const int short_option = m_getopt_table[option_idx].val;
325e16c50a1SJim Ingham 
326e16c50a1SJim Ingham             switch (short_option)
327e16c50a1SJim Ingham             {
328e16c50a1SJim Ingham                 case 'e':
329c95f7e2aSPavel Labath                     error = m_stop_on_error.SetValueFromString(option_arg);
330e16c50a1SJim Ingham                     break;
331340b0309SGreg Clayton 
332e16c50a1SJim Ingham                 case 'c':
333c95f7e2aSPavel Labath                     error = m_stop_on_continue.SetValueFromString(option_arg);
334e16c50a1SJim Ingham                     break;
335340b0309SGreg Clayton 
33660986174SMichael Sartain                 case 's':
337c95f7e2aSPavel Labath                     error = m_silent_run.SetValueFromString(option_arg);
33860986174SMichael Sartain                     break;
339340b0309SGreg Clayton 
340e16c50a1SJim Ingham                 default:
34186edbf41SGreg Clayton                     error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
342e16c50a1SJim Ingham                     break;
343e16c50a1SJim Ingham             }
344e16c50a1SJim Ingham 
345e16c50a1SJim Ingham             return error;
346e16c50a1SJim Ingham         }
347e16c50a1SJim Ingham 
348e16c50a1SJim Ingham         void
349f6b8b581SGreg Clayton         OptionParsingStarting ()
350e16c50a1SJim Ingham         {
351012d4fcaSEnrico Granata             m_stop_on_error.Clear();
352340b0309SGreg Clayton             m_silent_run.Clear();
353340b0309SGreg Clayton             m_stop_on_continue.Clear();
354e16c50a1SJim Ingham         }
355e16c50a1SJim Ingham 
356e0d378b3SGreg Clayton         const OptionDefinition*
357e16c50a1SJim Ingham         GetDefinitions ()
358e16c50a1SJim Ingham         {
359e16c50a1SJim Ingham             return g_option_table;
360e16c50a1SJim Ingham         }
361e16c50a1SJim Ingham 
362e16c50a1SJim Ingham         // Options table: Required for subclasses of Options.
363e16c50a1SJim Ingham 
364e0d378b3SGreg Clayton         static OptionDefinition g_option_table[];
365e16c50a1SJim Ingham 
366e16c50a1SJim Ingham         // Instance variables to hold the values for command options.
367e16c50a1SJim Ingham 
368012d4fcaSEnrico Granata         OptionValueBoolean m_stop_on_error;
369340b0309SGreg Clayton         OptionValueBoolean m_silent_run;
370340b0309SGreg Clayton         OptionValueBoolean m_stop_on_continue;
371e16c50a1SJim Ingham     };
372e16c50a1SJim Ingham 
373ebc09c36SJim Ingham     bool
3745a988416SJim Ingham     DoExecute(Args& command, CommandReturnObject &result)
375ebc09c36SJim Ingham     {
376c7bece56SGreg Clayton         const size_t argc = command.GetArgumentCount();
377ebc09c36SJim Ingham         if (argc == 1)
378ebc09c36SJim Ingham         {
3795a988416SJim Ingham             const char *filename = command.GetArgumentAtIndex(0);
380ebc09c36SJim Ingham 
3811ee3853fSJohnny Chen             FileSpec cmd_file (filename, true);
382e16c50a1SJim Ingham             ExecutionContext *exe_ctx = NULL;  // Just use the default context.
383ebc09c36SJim Ingham 
384340b0309SGreg Clayton             // If any options were set, then use them
385340b0309SGreg Clayton             if (m_options.m_stop_on_error.OptionWasSet()    ||
386340b0309SGreg Clayton                 m_options.m_silent_run.OptionWasSet()       ||
387340b0309SGreg Clayton                 m_options.m_stop_on_continue.OptionWasSet())
388340b0309SGreg Clayton             {
389340b0309SGreg Clayton                 // Use user set settings
39026c7bf93SJim Ingham                 CommandInterpreterRunOptions options;
39126c7bf93SJim Ingham                 options.SetStopOnContinue(m_options.m_stop_on_continue.GetCurrentValue());
39226c7bf93SJim Ingham                 options.SetStopOnError (m_options.m_stop_on_error.GetCurrentValue());
3937d8555c4SJim Ingham                 options.SetEchoCommands (!m_options.m_silent_run.GetCurrentValue());
3947d8555c4SJim Ingham                 options.SetPrintResults (!m_options.m_silent_run.GetCurrentValue());
39526c7bf93SJim Ingham 
396e16c50a1SJim Ingham                 m_interpreter.HandleCommandsFromFile (cmd_file,
397e16c50a1SJim Ingham                                                       exe_ctx,
39826c7bf93SJim Ingham                                                       options,
399e16c50a1SJim Ingham                                                       result);
400340b0309SGreg Clayton 
401340b0309SGreg Clayton             }
402340b0309SGreg Clayton             else
403340b0309SGreg Clayton             {
404340b0309SGreg Clayton                 // No options were set, inherit any settings from nested "command source" commands,
405340b0309SGreg Clayton                 // or set to sane default settings...
40626c7bf93SJim Ingham                 CommandInterpreterRunOptions options;
407340b0309SGreg Clayton                 m_interpreter.HandleCommandsFromFile (cmd_file,
408340b0309SGreg Clayton                                                       exe_ctx,
40926c7bf93SJim Ingham                                                       options,
410340b0309SGreg Clayton                                                       result);
411340b0309SGreg Clayton 
412340b0309SGreg Clayton             }
413ebc09c36SJim Ingham         }
414ebc09c36SJim Ingham         else
415ebc09c36SJim Ingham         {
416ebc09c36SJim Ingham             result.AppendErrorWithFormat("'%s' takes exactly one executable filename argument.\n", GetCommandName());
417ebc09c36SJim Ingham             result.SetStatus (eReturnStatusFailed);
418ebc09c36SJim Ingham         }
419ebc09c36SJim Ingham         return result.Succeeded();
420ebc09c36SJim Ingham 
421ebc09c36SJim Ingham     }
4225a988416SJim Ingham     CommandOptions m_options;
423ebc09c36SJim Ingham };
424ebc09c36SJim Ingham 
425e0d378b3SGreg Clayton OptionDefinition
426e16c50a1SJim Ingham CommandObjectCommandsSource::CommandOptions::g_option_table[] =
427e16c50a1SJim Ingham {
428d37221dcSZachary Turner { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean,    "If true, stop executing commands on error."},
429d37221dcSZachary Turner { LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "If true, stop executing commands on continue."},
430d37221dcSZachary Turner { LLDB_OPT_SET_ALL, false, "silent-run", 's', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "If true don't echo commands while executing."},
431d37221dcSZachary Turner { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
432e16c50a1SJim Ingham };
433e16c50a1SJim Ingham 
434ebc09c36SJim Ingham #pragma mark CommandObjectCommandsAlias
435ebc09c36SJim Ingham //-------------------------------------------------------------------------
436ebc09c36SJim Ingham // CommandObjectCommandsAlias
437ebc09c36SJim Ingham //-------------------------------------------------------------------------
438ebc09c36SJim Ingham 
439be93a35aSEnrico Granata static const char *g_python_command_instructions =   "Enter your Python command(s). Type 'DONE' to end.\n"
440be93a35aSEnrico Granata                                                      "You must define a Python function with this signature:\n"
44144d93782SGreg Clayton                                                      "def my_command_impl(debugger, args, result, internal_dict):\n";
442be93a35aSEnrico Granata 
443be93a35aSEnrico Granata 
4445a988416SJim Ingham class CommandObjectCommandsAlias : public CommandObjectRaw
445ebc09c36SJim Ingham {
446be93a35aSEnrico Granata 
447be93a35aSEnrico Granata 
448ebc09c36SJim Ingham public:
449a7015092SGreg Clayton     CommandObjectCommandsAlias (CommandInterpreter &interpreter) :
4505a988416SJim Ingham         CommandObjectRaw (interpreter,
4510e5e5a79SGreg Clayton                        "command alias",
452e3d26315SCaroline Tice                        "Allow users to define their own debugger command abbreviations.",
453405fe67fSCaroline Tice                        NULL)
454ebc09c36SJim Ingham     {
455ebc09c36SJim Ingham         SetHelpLong(
456ebc09c36SJim Ingham     "'alias' allows the user to create a short-cut or abbreviation for long \n\
457ebc09c36SJim Ingham     commands, multi-word commands, and commands that take particular options. \n\
458ebc09c36SJim Ingham     Below are some simple examples of how one might use the 'alias' command: \n\
45969c12ccbSJason Molenda     \n    'command alias sc script'            // Creates the abbreviation 'sc' for the 'script' \n\
460ebc09c36SJim Ingham                                          // command. \n\
46169c12ccbSJason Molenda     'command alias bp breakpoint'        // Creates the abbreviation 'bp' for the 'breakpoint' \n\
462ebc09c36SJim Ingham                                          // command.  Since breakpoint commands are two-word \n\
463ebc09c36SJim Ingham                                          // commands, the user will still need to enter the \n\
464ebc09c36SJim Ingham                                          // second word after 'bp', e.g. 'bp enable' or \n\
465ebc09c36SJim Ingham                                          // 'bp delete'. \n\
46669c12ccbSJason Molenda     'command alias bpl breakpoint list'  // Creates the abbreviation 'bpl' for the \n\
467ebc09c36SJim Ingham                                          // two-word command 'breakpoint list'. \n\
468ebc09c36SJim Ingham     \nAn alias can include some options for the command, with the values either \n\
469ebc09c36SJim Ingham     filled in at the time the alias is created, or specified as positional \n\
470ebc09c36SJim Ingham     arguments, to be filled in when the alias is invoked.  The following example \n\
471ebc09c36SJim Ingham     shows how to create aliases with options: \n\
472ebc09c36SJim Ingham     \n\
47369c12ccbSJason Molenda     'command alias bfl breakpoint set -f %1 -l %2' \n\
474ebc09c36SJim Ingham     \nThis creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \n\
475ebc09c36SJim Ingham     options already part of the alias.  So if the user wants to set a breakpoint \n\
476ebc09c36SJim Ingham     by file and line without explicitly having to use the -f and -l options, the \n\
477ebc09c36SJim Ingham     user can now use 'bfl' instead.  The '%1' and '%2' are positional placeholders \n\
478ebc09c36SJim Ingham     for the actual arguments that will be passed when the alias command is used. \n\
479ebc09c36SJim Ingham     The number in the placeholder refers to the position/order the actual value \n\
48081ded935SJim Ingham     occupies when the alias is used.  All the occurrences of '%1' in the alias \n\
481ebc09c36SJim Ingham     will be replaced with the first argument, all the occurrences of '%2' in the \n\
482ebc09c36SJim Ingham     alias will be replaced with the second argument, and so on.  This also allows \n\
483ebc09c36SJim Ingham     actual arguments to be used multiple times within an alias (see 'process \n\
48481ded935SJim Ingham     launch' example below).  \n\
48581ded935SJim Ingham     Note: the positional arguments must substitute as whole words in the resultant\n\
48681ded935SJim Ingham     command, so you can't at present do something like:\n\
48781ded935SJim Ingham     \n\
48869c12ccbSJason Molenda     command alias bcppfl breakpoint set -f %1.cpp -l %2\n\
48981ded935SJim Ingham     \n\
49081ded935SJim Ingham     to get the file extension \".cpp\" automatically appended.  For more complex\n\
49181ded935SJim Ingham     aliasing, use the \"command regex\" command instead.\n\
49281ded935SJim Ingham     \nSo in the 'bfl' case, the actual file value will be \n\
493ebc09c36SJim Ingham     filled in with the first argument following 'bfl' and the actual line number \n\
494ebc09c36SJim Ingham     value will be filled in with the second argument.  The user would use this \n\
495ebc09c36SJim Ingham     alias as follows: \n\
49669c12ccbSJason Molenda     \n    (lldb)  command alias bfl breakpoint set -f %1 -l %2 \n\
497ebc09c36SJim Ingham     <... some time later ...> \n\
49809799af6SCaroline Tice     (lldb)  bfl my-file.c 137 \n\
499ebc09c36SJim Ingham     \nThis would be the same as if the user had entered \n\
500ebc09c36SJim Ingham     'breakpoint set -f my-file.c -l 137'. \n\
501ebc09c36SJim Ingham     \nAnother example: \n\
50269c12ccbSJason Molenda     \n    (lldb)  command alias pltty  process launch -s -o %1 -e %1 \n\
50309799af6SCaroline Tice     (lldb)  pltty /dev/tty0 \n\
504ebc09c36SJim Ingham            // becomes 'process launch -s -o /dev/tty0 -e /dev/tty0' \n\
505ebc09c36SJim Ingham     \nIf the user always wanted to pass the same value to a particular option, the \n\
506ebc09c36SJim Ingham     alias could be defined with that value directly in the alias as a constant, \n\
507ebc09c36SJim Ingham     rather than using a positional placeholder: \n\
50869c12ccbSJason Molenda     \n    command alias bl3  breakpoint set -f %1 -l 3  // Always sets a breakpoint on line \n\
509ebc09c36SJim Ingham                                                    // 3 of whatever file is indicated. \n");
510ebc09c36SJim Ingham 
511405fe67fSCaroline Tice         CommandArgumentEntry arg1;
512405fe67fSCaroline Tice         CommandArgumentEntry arg2;
513405fe67fSCaroline Tice         CommandArgumentEntry arg3;
514405fe67fSCaroline Tice         CommandArgumentData alias_arg;
515405fe67fSCaroline Tice         CommandArgumentData cmd_arg;
516405fe67fSCaroline Tice         CommandArgumentData options_arg;
517405fe67fSCaroline Tice 
518405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
519405fe67fSCaroline Tice         alias_arg.arg_type = eArgTypeAliasName;
520405fe67fSCaroline Tice         alias_arg.arg_repetition = eArgRepeatPlain;
521405fe67fSCaroline Tice 
522405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
523405fe67fSCaroline Tice         arg1.push_back (alias_arg);
524405fe67fSCaroline Tice 
525405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
526405fe67fSCaroline Tice         cmd_arg.arg_type = eArgTypeCommandName;
527405fe67fSCaroline Tice         cmd_arg.arg_repetition = eArgRepeatPlain;
528405fe67fSCaroline Tice 
529405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
530405fe67fSCaroline Tice         arg2.push_back (cmd_arg);
531405fe67fSCaroline Tice 
532405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
533405fe67fSCaroline Tice         options_arg.arg_type = eArgTypeAliasOptions;
534405fe67fSCaroline Tice         options_arg.arg_repetition = eArgRepeatOptional;
535405fe67fSCaroline Tice 
536405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
537405fe67fSCaroline Tice         arg3.push_back (options_arg);
538405fe67fSCaroline Tice 
539405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
540405fe67fSCaroline Tice         m_arguments.push_back (arg1);
541405fe67fSCaroline Tice         m_arguments.push_back (arg2);
542405fe67fSCaroline Tice         m_arguments.push_back (arg3);
543ebc09c36SJim Ingham     }
544ebc09c36SJim Ingham 
545ebc09c36SJim Ingham     ~CommandObjectCommandsAlias ()
546ebc09c36SJim Ingham     {
547ebc09c36SJim Ingham     }
548ebc09c36SJim Ingham 
5495a988416SJim Ingham protected:
5505a988416SJim Ingham     virtual bool
5515a988416SJim Ingham     DoExecute (const char *raw_command_line, CommandReturnObject &result)
552844d2303SCaroline Tice     {
553844d2303SCaroline Tice         Args args (raw_command_line);
554844d2303SCaroline Tice         std::string raw_command_string (raw_command_line);
555844d2303SCaroline Tice 
556844d2303SCaroline Tice         size_t argc = args.GetArgumentCount();
557844d2303SCaroline Tice 
558844d2303SCaroline Tice         if (argc < 2)
559844d2303SCaroline Tice         {
560844d2303SCaroline Tice             result.AppendError ("'alias' requires at least two arguments");
561844d2303SCaroline Tice             result.SetStatus (eReturnStatusFailed);
562844d2303SCaroline Tice             return false;
563844d2303SCaroline Tice         }
564844d2303SCaroline Tice 
565844d2303SCaroline Tice         // Get the alias command.
566844d2303SCaroline Tice 
567844d2303SCaroline Tice         const std::string alias_command = args.GetArgumentAtIndex (0);
568844d2303SCaroline Tice 
569844d2303SCaroline Tice         // Strip the new alias name off 'raw_command_string'  (leave it on args, which gets passed to 'Execute', which
570844d2303SCaroline Tice         // does the stripping itself.
571844d2303SCaroline Tice         size_t pos = raw_command_string.find (alias_command);
572844d2303SCaroline Tice         if (pos == 0)
573844d2303SCaroline Tice         {
574844d2303SCaroline Tice             raw_command_string = raw_command_string.substr (alias_command.size());
575844d2303SCaroline Tice             pos = raw_command_string.find_first_not_of (' ');
576844d2303SCaroline Tice             if ((pos != std::string::npos) && (pos > 0))
577844d2303SCaroline Tice                 raw_command_string = raw_command_string.substr (pos);
578844d2303SCaroline Tice         }
579844d2303SCaroline Tice         else
580844d2303SCaroline Tice         {
581844d2303SCaroline Tice             result.AppendError ("Error parsing command string.  No alias created.");
582844d2303SCaroline Tice             result.SetStatus (eReturnStatusFailed);
583844d2303SCaroline Tice             return false;
584844d2303SCaroline Tice         }
585844d2303SCaroline Tice 
586844d2303SCaroline Tice 
587844d2303SCaroline Tice         // Verify that the command is alias-able.
588844d2303SCaroline Tice         if (m_interpreter.CommandExists (alias_command.c_str()))
589844d2303SCaroline Tice         {
590844d2303SCaroline Tice             result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
591844d2303SCaroline Tice                                           alias_command.c_str());
592844d2303SCaroline Tice             result.SetStatus (eReturnStatusFailed);
593844d2303SCaroline Tice             return false;
594844d2303SCaroline Tice         }
595844d2303SCaroline Tice 
596844d2303SCaroline Tice         // Get CommandObject that is being aliased. The command name is read from the front of raw_command_string.
597844d2303SCaroline Tice         // raw_command_string is returned with the name of the command object stripped off the front.
598844d2303SCaroline Tice         CommandObject *cmd_obj = m_interpreter.GetCommandObjectForCommand (raw_command_string);
599844d2303SCaroline Tice 
600844d2303SCaroline Tice         if (!cmd_obj)
601844d2303SCaroline Tice         {
60286edbf41SGreg Clayton             result.AppendErrorWithFormat ("invalid command given to 'alias'. '%s' does not begin with a valid command."
603844d2303SCaroline Tice                                           "  No alias created.", raw_command_string.c_str());
604844d2303SCaroline Tice             result.SetStatus (eReturnStatusFailed);
605844d2303SCaroline Tice             return false;
606844d2303SCaroline Tice         }
607844d2303SCaroline Tice         else if (!cmd_obj->WantsRawCommandString ())
608844d2303SCaroline Tice         {
609844d2303SCaroline Tice             // Note that args was initialized with the original command, and has not been updated to this point.
610844d2303SCaroline Tice             // Therefore can we pass it to the version of Execute that does not need/expect raw input in the alias.
6115a988416SJim Ingham             return HandleAliasingNormalCommand (args, result);
612844d2303SCaroline Tice         }
613844d2303SCaroline Tice         else
614844d2303SCaroline Tice         {
6155a988416SJim Ingham             return HandleAliasingRawCommand (alias_command, raw_command_string, *cmd_obj, result);
6165a988416SJim Ingham         }
6175a988416SJim Ingham         return result.Succeeded();
6185a988416SJim Ingham     }
6195a988416SJim Ingham 
6205a988416SJim Ingham     bool
6215a988416SJim Ingham     HandleAliasingRawCommand (const std::string &alias_command, std::string &raw_command_string, CommandObject &cmd_obj, CommandReturnObject &result)
6225a988416SJim Ingham     {
623844d2303SCaroline Tice             // Verify & handle any options/arguments passed to the alias command
624844d2303SCaroline Tice 
625844d2303SCaroline Tice             OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector);
626844d2303SCaroline Tice             OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
627844d2303SCaroline Tice 
6285a988416SJim Ingham             CommandObjectSP cmd_obj_sp = m_interpreter.GetCommandSPExact (cmd_obj.GetCommandName(), false);
629844d2303SCaroline Tice 
630ca90c47eSCaroline Tice             if (!m_interpreter.ProcessAliasOptionsArgs (cmd_obj_sp, raw_command_string.c_str(), option_arg_vector_sp))
631844d2303SCaroline Tice             {
632844d2303SCaroline Tice                 result.AppendError ("Unable to create requested alias.\n");
633ca90c47eSCaroline Tice                 result.SetStatus (eReturnStatusFailed);
634844d2303SCaroline Tice                 return false;
635844d2303SCaroline Tice             }
636844d2303SCaroline Tice 
637844d2303SCaroline Tice             // Create the alias
638844d2303SCaroline Tice             if (m_interpreter.AliasExists (alias_command.c_str())
639844d2303SCaroline Tice                 || m_interpreter.UserCommandExists (alias_command.c_str()))
640844d2303SCaroline Tice             {
641844d2303SCaroline Tice                 OptionArgVectorSP temp_option_arg_sp (m_interpreter.GetAliasOptions (alias_command.c_str()));
642844d2303SCaroline Tice                 if (temp_option_arg_sp.get())
643844d2303SCaroline Tice                 {
644844d2303SCaroline Tice                     if (option_arg_vector->size() == 0)
645844d2303SCaroline Tice                         m_interpreter.RemoveAliasOptions (alias_command.c_str());
646844d2303SCaroline Tice                 }
647844d2303SCaroline Tice                 result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
648844d2303SCaroline Tice                                                 alias_command.c_str());
649844d2303SCaroline Tice             }
650844d2303SCaroline Tice 
651472362e6SCaroline Tice             if (cmd_obj_sp)
652472362e6SCaroline Tice             {
653844d2303SCaroline Tice                 m_interpreter.AddAlias (alias_command.c_str(), cmd_obj_sp);
654844d2303SCaroline Tice                 if (option_arg_vector->size() > 0)
655844d2303SCaroline Tice                     m_interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp);
656844d2303SCaroline Tice                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
657844d2303SCaroline Tice             }
658472362e6SCaroline Tice             else
659472362e6SCaroline Tice             {
660472362e6SCaroline Tice                 result.AppendError ("Unable to create requested alias.\n");
661472362e6SCaroline Tice                 result.SetStatus (eReturnStatusFailed);
662472362e6SCaroline Tice             }
663844d2303SCaroline Tice             return result.Succeeded ();
664844d2303SCaroline Tice     }
665ebc09c36SJim Ingham 
666ebc09c36SJim Ingham     bool
6675a988416SJim Ingham     HandleAliasingNormalCommand (Args& args, CommandReturnObject &result)
668ebc09c36SJim Ingham     {
669867b185dSCaroline Tice         size_t argc = args.GetArgumentCount();
670ebc09c36SJim Ingham 
671ebc09c36SJim Ingham         if (argc < 2)
672ebc09c36SJim Ingham         {
673ebc09c36SJim Ingham             result.AppendError ("'alias' requires at least two arguments");
674ebc09c36SJim Ingham             result.SetStatus (eReturnStatusFailed);
675ebc09c36SJim Ingham             return false;
676ebc09c36SJim Ingham         }
677ebc09c36SJim Ingham 
678ebc09c36SJim Ingham         const std::string alias_command = args.GetArgumentAtIndex(0);
679ebc09c36SJim Ingham         const std::string actual_command = args.GetArgumentAtIndex(1);
680ebc09c36SJim Ingham 
681ebc09c36SJim Ingham         args.Shift();  // Shift the alias command word off the argument vector.
682ebc09c36SJim Ingham         args.Shift();  // Shift the old command word off the argument vector.
683ebc09c36SJim Ingham 
684ebc09c36SJim Ingham         // Verify that the command is alias'able, and get the appropriate command object.
685ebc09c36SJim Ingham 
686a7015092SGreg Clayton         if (m_interpreter.CommandExists (alias_command.c_str()))
687ebc09c36SJim Ingham         {
688ebc09c36SJim Ingham             result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
689ebc09c36SJim Ingham                                          alias_command.c_str());
690ebc09c36SJim Ingham             result.SetStatus (eReturnStatusFailed);
691ebc09c36SJim Ingham         }
692ebc09c36SJim Ingham         else
693ebc09c36SJim Ingham         {
694a7015092SGreg Clayton              CommandObjectSP command_obj_sp(m_interpreter.GetCommandSPExact (actual_command.c_str(), true));
695ebc09c36SJim Ingham              CommandObjectSP subcommand_obj_sp;
696ebc09c36SJim Ingham              bool use_subcommand = false;
697ebc09c36SJim Ingham              if (command_obj_sp.get())
698ebc09c36SJim Ingham              {
699ebc09c36SJim Ingham                  CommandObject *cmd_obj = command_obj_sp.get();
700c982c768SGreg Clayton                  CommandObject *sub_cmd_obj = NULL;
701ebc09c36SJim Ingham                  OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector);
702ebc09c36SJim Ingham                  OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
703ebc09c36SJim Ingham 
704844d2303SCaroline Tice                  while (cmd_obj->IsMultiwordObject() && args.GetArgumentCount() > 0)
705ebc09c36SJim Ingham                  {
706ebc09c36SJim Ingham                      if (argc >= 3)
707ebc09c36SJim Ingham                      {
708ebc09c36SJim Ingham                          const std::string sub_command = args.GetArgumentAtIndex(0);
709ebc09c36SJim Ingham                          assert (sub_command.length() != 0);
710998255bfSGreg Clayton                          subcommand_obj_sp = cmd_obj->GetSubcommandSP (sub_command.c_str());
711ebc09c36SJim Ingham                          if (subcommand_obj_sp.get())
712ebc09c36SJim Ingham                          {
713ebc09c36SJim Ingham                              sub_cmd_obj = subcommand_obj_sp.get();
714ebc09c36SJim Ingham                              use_subcommand = true;
715ebc09c36SJim Ingham                              args.Shift();  // Shift the sub_command word off the argument vector.
716844d2303SCaroline Tice                              cmd_obj = sub_cmd_obj;
717ebc09c36SJim Ingham                          }
718ebc09c36SJim Ingham                          else
719ebc09c36SJim Ingham                          {
720f415eeb4SCaroline Tice                              result.AppendErrorWithFormat("'%s' is not a valid sub-command of '%s'.  "
721f415eeb4SCaroline Tice                                                           "Unable to create alias.\n",
722f415eeb4SCaroline Tice                                                           sub_command.c_str(), actual_command.c_str());
723ebc09c36SJim Ingham                              result.SetStatus (eReturnStatusFailed);
724ebc09c36SJim Ingham                              return false;
725ebc09c36SJim Ingham                          }
726ebc09c36SJim Ingham                      }
727ebc09c36SJim Ingham                  }
728ebc09c36SJim Ingham 
729ebc09c36SJim Ingham                  // Verify & handle any options/arguments passed to the alias command
730ebc09c36SJim Ingham 
731ebc09c36SJim Ingham                  if (args.GetArgumentCount () > 0)
732ebc09c36SJim Ingham                  {
733ca90c47eSCaroline Tice                     CommandObjectSP tmp_sp = m_interpreter.GetCommandSPExact (cmd_obj->GetCommandName(), false);
734ebc09c36SJim Ingham                     if (use_subcommand)
735ca90c47eSCaroline Tice                         tmp_sp = m_interpreter.GetCommandSPExact (sub_cmd_obj->GetCommandName(), false);
736ca90c47eSCaroline Tice 
737ca90c47eSCaroline Tice                     std::string args_string;
738ca90c47eSCaroline Tice                     args.GetCommandString (args_string);
739ca90c47eSCaroline Tice 
740ca90c47eSCaroline Tice                     if (!m_interpreter.ProcessAliasOptionsArgs (tmp_sp, args_string.c_str(), option_arg_vector_sp))
741ebc09c36SJim Ingham                     {
742ca90c47eSCaroline Tice                         result.AppendError ("Unable to create requested alias.\n");
743ca90c47eSCaroline Tice                         result.SetStatus (eReturnStatusFailed);
744e7941795SCaroline Tice                         return false;
745867b185dSCaroline Tice                     }
746867b185dSCaroline Tice                  }
747867b185dSCaroline Tice 
748ebc09c36SJim Ingham                  // Create the alias.
749ebc09c36SJim Ingham 
750a7015092SGreg Clayton                  if (m_interpreter.AliasExists (alias_command.c_str())
751a7015092SGreg Clayton                      || m_interpreter.UserCommandExists (alias_command.c_str()))
752ebc09c36SJim Ingham                  {
753a7015092SGreg Clayton                      OptionArgVectorSP tmp_option_arg_sp (m_interpreter.GetAliasOptions (alias_command.c_str()));
754ebc09c36SJim Ingham                      if (tmp_option_arg_sp.get())
755ebc09c36SJim Ingham                      {
756ebc09c36SJim Ingham                          if (option_arg_vector->size() == 0)
757a7015092SGreg Clayton                              m_interpreter.RemoveAliasOptions (alias_command.c_str());
758ebc09c36SJim Ingham                      }
759ebc09c36SJim Ingham                      result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
760ebc09c36SJim Ingham                                                      alias_command.c_str());
761ebc09c36SJim Ingham                  }
762ebc09c36SJim Ingham 
763ebc09c36SJim Ingham                  if (use_subcommand)
764a7015092SGreg Clayton                      m_interpreter.AddAlias (alias_command.c_str(), subcommand_obj_sp);
765ebc09c36SJim Ingham                  else
766a7015092SGreg Clayton                      m_interpreter.AddAlias (alias_command.c_str(), command_obj_sp);
767ebc09c36SJim Ingham                  if (option_arg_vector->size() > 0)
768a7015092SGreg Clayton                      m_interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp);
769ebc09c36SJim Ingham                  result.SetStatus (eReturnStatusSuccessFinishNoResult);
770ebc09c36SJim Ingham              }
771ebc09c36SJim Ingham              else
772ebc09c36SJim Ingham              {
773ebc09c36SJim Ingham                  result.AppendErrorWithFormat ("'%s' is not an existing command.\n", actual_command.c_str());
774ebc09c36SJim Ingham                  result.SetStatus (eReturnStatusFailed);
775e7941795SCaroline Tice                  return false;
776ebc09c36SJim Ingham              }
777ebc09c36SJim Ingham         }
778ebc09c36SJim Ingham 
779ebc09c36SJim Ingham         return result.Succeeded();
780ebc09c36SJim Ingham     }
7815a988416SJim Ingham 
782ebc09c36SJim Ingham };
783ebc09c36SJim Ingham 
784ebc09c36SJim Ingham #pragma mark CommandObjectCommandsUnalias
785ebc09c36SJim Ingham //-------------------------------------------------------------------------
786ebc09c36SJim Ingham // CommandObjectCommandsUnalias
787ebc09c36SJim Ingham //-------------------------------------------------------------------------
788ebc09c36SJim Ingham 
7895a988416SJim Ingham class CommandObjectCommandsUnalias : public CommandObjectParsed
790ebc09c36SJim Ingham {
791ebc09c36SJim Ingham public:
792a7015092SGreg Clayton     CommandObjectCommandsUnalias (CommandInterpreter &interpreter) :
7935a988416SJim Ingham         CommandObjectParsed (interpreter,
7940e5e5a79SGreg Clayton                        "command unalias",
79586ddae50SCaroline Tice                        "Allow the user to remove/delete a user-defined command abbreviation.",
796405fe67fSCaroline Tice                        NULL)
797ebc09c36SJim Ingham     {
798405fe67fSCaroline Tice         CommandArgumentEntry arg;
799405fe67fSCaroline Tice         CommandArgumentData alias_arg;
800405fe67fSCaroline Tice 
801405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
802405fe67fSCaroline Tice         alias_arg.arg_type = eArgTypeAliasName;
803405fe67fSCaroline Tice         alias_arg.arg_repetition = eArgRepeatPlain;
804405fe67fSCaroline Tice 
805405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
806405fe67fSCaroline Tice         arg.push_back (alias_arg);
807405fe67fSCaroline Tice 
808405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
809405fe67fSCaroline Tice         m_arguments.push_back (arg);
810ebc09c36SJim Ingham     }
811ebc09c36SJim Ingham 
812ebc09c36SJim Ingham     ~CommandObjectCommandsUnalias()
813ebc09c36SJim Ingham     {
814ebc09c36SJim Ingham     }
815ebc09c36SJim Ingham 
8165a988416SJim Ingham protected:
817ebc09c36SJim Ingham     bool
8185a988416SJim Ingham     DoExecute (Args& args, CommandReturnObject &result)
819ebc09c36SJim Ingham     {
820ebc09c36SJim Ingham         CommandObject::CommandMap::iterator pos;
821ebc09c36SJim Ingham         CommandObject *cmd_obj;
822ebc09c36SJim Ingham 
823ebc09c36SJim Ingham         if (args.GetArgumentCount() != 0)
824ebc09c36SJim Ingham         {
825ebc09c36SJim Ingham             const char *command_name = args.GetArgumentAtIndex(0);
826a7015092SGreg Clayton             cmd_obj = m_interpreter.GetCommandObject(command_name);
827ebc09c36SJim Ingham             if (cmd_obj)
828ebc09c36SJim Ingham             {
829a7015092SGreg Clayton                 if (m_interpreter.CommandExists (command_name))
830ebc09c36SJim Ingham                 {
831b547278cSGreg Clayton                     if (cmd_obj->IsRemovable())
832b547278cSGreg Clayton                     {
833b547278cSGreg Clayton                         result.AppendErrorWithFormat ("'%s' is not an alias, it is a debugger command which can be removed using the 'command delete' command.\n",
834b547278cSGreg Clayton                                                       command_name);
835b547278cSGreg Clayton                     }
836b547278cSGreg Clayton                     else
837b547278cSGreg Clayton                     {
838ebc09c36SJim Ingham                         result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be removed.\n",
839ebc09c36SJim Ingham                                                       command_name);
840b547278cSGreg Clayton                     }
841ebc09c36SJim Ingham                     result.SetStatus (eReturnStatusFailed);
842ebc09c36SJim Ingham                 }
843ebc09c36SJim Ingham                 else
844ebc09c36SJim Ingham                 {
845ebc09c36SJim Ingham 
846a7015092SGreg Clayton                     if (m_interpreter.RemoveAlias (command_name) == false)
847ebc09c36SJim Ingham                     {
848a7015092SGreg Clayton                         if (m_interpreter.AliasExists (command_name))
849ebc09c36SJim Ingham                             result.AppendErrorWithFormat ("Error occurred while attempting to unalias '%s'.\n",
850ebc09c36SJim Ingham                                                           command_name);
851ebc09c36SJim Ingham                         else
852ebc09c36SJim Ingham                             result.AppendErrorWithFormat ("'%s' is not an existing alias.\n", command_name);
853ebc09c36SJim Ingham                         result.SetStatus (eReturnStatusFailed);
854ebc09c36SJim Ingham                     }
855ebc09c36SJim Ingham                     else
856ebc09c36SJim Ingham                         result.SetStatus (eReturnStatusSuccessFinishNoResult);
857ebc09c36SJim Ingham                 }
858ebc09c36SJim Ingham             }
859ebc09c36SJim Ingham             else
860ebc09c36SJim Ingham             {
861ebc09c36SJim Ingham                 result.AppendErrorWithFormat ("'%s' is not a known command.\nTry 'help' to see a "
862ebc09c36SJim Ingham                                               "current list of commands.\n",
863ebc09c36SJim Ingham                                              command_name);
864ebc09c36SJim Ingham                 result.SetStatus (eReturnStatusFailed);
865ebc09c36SJim Ingham             }
866ebc09c36SJim Ingham         }
867ebc09c36SJim Ingham         else
868ebc09c36SJim Ingham         {
869ebc09c36SJim Ingham             result.AppendError ("must call 'unalias' with a valid alias");
870ebc09c36SJim Ingham             result.SetStatus (eReturnStatusFailed);
871ebc09c36SJim Ingham         }
872ebc09c36SJim Ingham 
873ebc09c36SJim Ingham         return result.Succeeded();
874ebc09c36SJim Ingham     }
875ebc09c36SJim Ingham };
876ebc09c36SJim Ingham 
877b547278cSGreg Clayton #pragma mark CommandObjectCommandsDelete
878b547278cSGreg Clayton //-------------------------------------------------------------------------
879b547278cSGreg Clayton // CommandObjectCommandsDelete
880b547278cSGreg Clayton //-------------------------------------------------------------------------
881b547278cSGreg Clayton 
882b547278cSGreg Clayton class CommandObjectCommandsDelete : public CommandObjectParsed
883b547278cSGreg Clayton {
884b547278cSGreg Clayton public:
885b547278cSGreg Clayton     CommandObjectCommandsDelete (CommandInterpreter &interpreter) :
886b547278cSGreg Clayton     CommandObjectParsed (interpreter,
887b547278cSGreg Clayton                          "command delete",
888b547278cSGreg Clayton                          "Allow the user to delete user-defined regular expression, python or multi-word commands.",
889b547278cSGreg Clayton                          NULL)
890b547278cSGreg Clayton     {
891b547278cSGreg Clayton         CommandArgumentEntry arg;
892b547278cSGreg Clayton         CommandArgumentData alias_arg;
893b547278cSGreg Clayton 
894b547278cSGreg Clayton         // Define the first (and only) variant of this arg.
895b547278cSGreg Clayton         alias_arg.arg_type = eArgTypeCommandName;
896b547278cSGreg Clayton         alias_arg.arg_repetition = eArgRepeatPlain;
897b547278cSGreg Clayton 
898b547278cSGreg Clayton         // There is only one variant this argument could be; put it into the argument entry.
899b547278cSGreg Clayton         arg.push_back (alias_arg);
900b547278cSGreg Clayton 
901b547278cSGreg Clayton         // Push the data for the first argument into the m_arguments vector.
902b547278cSGreg Clayton         m_arguments.push_back (arg);
903b547278cSGreg Clayton     }
904b547278cSGreg Clayton 
905b547278cSGreg Clayton     ~CommandObjectCommandsDelete()
906b547278cSGreg Clayton     {
907b547278cSGreg Clayton     }
908b547278cSGreg Clayton 
909b547278cSGreg Clayton protected:
910b547278cSGreg Clayton     bool
911b547278cSGreg Clayton     DoExecute (Args& args, CommandReturnObject &result)
912b547278cSGreg Clayton     {
913b547278cSGreg Clayton         CommandObject::CommandMap::iterator pos;
914b547278cSGreg Clayton 
915b547278cSGreg Clayton         if (args.GetArgumentCount() != 0)
916b547278cSGreg Clayton         {
917b547278cSGreg Clayton             const char *command_name = args.GetArgumentAtIndex(0);
918b547278cSGreg Clayton             if (m_interpreter.CommandExists (command_name))
919b547278cSGreg Clayton             {
920b547278cSGreg Clayton                 if (m_interpreter.RemoveCommand (command_name))
921b547278cSGreg Clayton                 {
922b547278cSGreg Clayton                     result.SetStatus (eReturnStatusSuccessFinishNoResult);
923b547278cSGreg Clayton                 }
924b547278cSGreg Clayton                 else
925b547278cSGreg Clayton                 {
926b547278cSGreg Clayton                     result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be removed.\n",
927b547278cSGreg Clayton                                                   command_name);
928b547278cSGreg Clayton                     result.SetStatus (eReturnStatusFailed);
929b547278cSGreg Clayton                 }
930b547278cSGreg Clayton             }
931b547278cSGreg Clayton             else
932b547278cSGreg Clayton             {
933b547278cSGreg Clayton                 result.AppendErrorWithFormat ("'%s' is not a known command.\nTry 'help' to see a current list of commands.\n",
934b547278cSGreg Clayton                                               command_name);
935b547278cSGreg Clayton                 result.SetStatus (eReturnStatusFailed);
936b547278cSGreg Clayton             }
937b547278cSGreg Clayton         }
938b547278cSGreg Clayton         else
939b547278cSGreg Clayton         {
940b547278cSGreg Clayton             result.AppendErrorWithFormat ("must call '%s' with one or more valid user defined regular expression, python or multi-word command names", GetCommandName ());
941b547278cSGreg Clayton             result.SetStatus (eReturnStatusFailed);
942b547278cSGreg Clayton         }
943b547278cSGreg Clayton 
944b547278cSGreg Clayton         return result.Succeeded();
945b547278cSGreg Clayton     }
946b547278cSGreg Clayton };
947b547278cSGreg Clayton 
948de164aaaSGreg Clayton //-------------------------------------------------------------------------
949de164aaaSGreg Clayton // CommandObjectCommandsAddRegex
950de164aaaSGreg Clayton //-------------------------------------------------------------------------
9515a988416SJim Ingham #pragma mark CommandObjectCommandsAddRegex
952de164aaaSGreg Clayton 
95344d93782SGreg Clayton class CommandObjectCommandsAddRegex :
95444d93782SGreg Clayton     public CommandObjectParsed,
955ea508635SGreg Clayton     public IOHandlerDelegateMultiline
956de164aaaSGreg Clayton {
957de164aaaSGreg Clayton public:
958de164aaaSGreg Clayton     CommandObjectCommandsAddRegex (CommandInterpreter &interpreter) :
9595a988416SJim Ingham         CommandObjectParsed (interpreter,
9600e5e5a79SGreg Clayton                        "command regex",
961de164aaaSGreg Clayton                        "Allow the user to create a regular expression command.",
9620e5e5a79SGreg Clayton                        "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
963ea508635SGreg Clayton         IOHandlerDelegateMultiline ("", IOHandlerDelegate::Completion::LLDBCommand),
964de164aaaSGreg Clayton         m_options (interpreter)
965de164aaaSGreg Clayton     {
9660e5e5a79SGreg Clayton         SetHelpLong(
9670e5e5a79SGreg Clayton "This command allows the user to create powerful regular expression commands\n"
9680e5e5a79SGreg Clayton "with substitutions. The regular expressions and substitutions are specified\n"
969d93c4a33SBruce Mitchener "using the regular expression substitution format of:\n"
9700e5e5a79SGreg Clayton "\n"
9710e5e5a79SGreg Clayton "    s/<regex>/<subst>/\n"
9720e5e5a79SGreg Clayton "\n"
9730e5e5a79SGreg Clayton "<regex> is a regular expression that can use parenthesis to capture regular\n"
9740e5e5a79SGreg Clayton "expression input and substitute the captured matches in the output using %1\n"
9750e5e5a79SGreg Clayton "for the first match, %2 for the second, and so on.\n"
9760e5e5a79SGreg Clayton "\n"
9770e5e5a79SGreg Clayton "The regular expressions can all be specified on the command line if more than\n"
9780e5e5a79SGreg Clayton "one argument is provided. If just the command name is provided on the command\n"
9790e5e5a79SGreg Clayton "line, then the regular expressions and substitutions can be entered on separate\n"
9800e5e5a79SGreg Clayton " lines, followed by an empty line to terminate the command definition.\n"
9810e5e5a79SGreg Clayton "\n"
9820e5e5a79SGreg Clayton "EXAMPLES\n"
9830e5e5a79SGreg Clayton "\n"
984adc43c99SSean Callanan "The following example will define a regular expression command named 'f' that\n"
9850e5e5a79SGreg Clayton "will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if\n"
9860e5e5a79SGreg Clayton "a number follows 'f':\n"
987adc43c99SSean Callanan "\n"
9880e5e5a79SGreg Clayton "    (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/'\n"
989adc43c99SSean Callanan "\n"
9900e5e5a79SGreg Clayton                     );
991de164aaaSGreg Clayton     }
992de164aaaSGreg Clayton 
993de164aaaSGreg Clayton     ~CommandObjectCommandsAddRegex()
994de164aaaSGreg Clayton     {
995de164aaaSGreg Clayton     }
996de164aaaSGreg Clayton 
997de164aaaSGreg Clayton 
9985a988416SJim Ingham protected:
99944d93782SGreg Clayton 
1000ea508635SGreg Clayton     void
1001ea508635SGreg Clayton     IOHandlerActivated (IOHandler &io_handler) override
100244d93782SGreg Clayton     {
100344d93782SGreg Clayton         StreamFileSP output_sp(io_handler.GetOutputStreamFile());
100444d93782SGreg Clayton         if (output_sp)
100544d93782SGreg Clayton         {
100644d93782SGreg Clayton             output_sp->PutCString("Enter one of more sed substitution commands in the form: 's/<regex>/<subst>/'.\nTerminate the substitution list with an empty line.\n");
100744d93782SGreg Clayton             output_sp->Flush();
100844d93782SGreg Clayton         }
100944d93782SGreg Clayton     }
101044d93782SGreg Clayton 
1011ea508635SGreg Clayton     void
1012ea508635SGreg Clayton     IOHandlerInputComplete (IOHandler &io_handler, std::string &data) override
101344d93782SGreg Clayton     {
101444d93782SGreg Clayton         io_handler.SetIsDone(true);
101544d93782SGreg Clayton         if (m_regex_cmd_ap.get())
101644d93782SGreg Clayton         {
101744d93782SGreg Clayton             StringList lines;
101844d93782SGreg Clayton             if (lines.SplitIntoLines (data))
101944d93782SGreg Clayton             {
102044d93782SGreg Clayton                 const size_t num_lines = lines.GetSize();
102144d93782SGreg Clayton                 bool check_only = false;
102244d93782SGreg Clayton                 for (size_t i=0; i<num_lines; ++i)
102344d93782SGreg Clayton                 {
102444d93782SGreg Clayton                     llvm::StringRef bytes_strref (lines[i]);
102544d93782SGreg Clayton                     Error error = AppendRegexSubstitution (bytes_strref, check_only);
102644d93782SGreg Clayton                     if (error.Fail())
102744d93782SGreg Clayton                     {
102844d93782SGreg Clayton                         if (!m_interpreter.GetDebugger().GetCommandInterpreter().GetBatchCommandMode())
102944d93782SGreg Clayton                         {
103044d93782SGreg Clayton                             StreamSP out_stream = m_interpreter.GetDebugger().GetAsyncOutputStream();
103144d93782SGreg Clayton                             out_stream->Printf("error: %s\n", error.AsCString());
103244d93782SGreg Clayton                         }
103344d93782SGreg Clayton                     }
103444d93782SGreg Clayton                 }
103544d93782SGreg Clayton             }
103644d93782SGreg Clayton             if (m_regex_cmd_ap->HasRegexEntries())
103744d93782SGreg Clayton             {
103844d93782SGreg Clayton                 CommandObjectSP cmd_sp (m_regex_cmd_ap.release());
103944d93782SGreg Clayton                 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
104044d93782SGreg Clayton             }
104144d93782SGreg Clayton         }
104244d93782SGreg Clayton     }
104344d93782SGreg Clayton 
1044de164aaaSGreg Clayton     bool
1045b0a1814fSEric Christopher     DoExecute (Args& command, CommandReturnObject &result) override
1046de164aaaSGreg Clayton     {
10475a988416SJim Ingham         const size_t argc = command.GetArgumentCount();
10480e5e5a79SGreg Clayton         if (argc == 0)
1049de164aaaSGreg Clayton         {
105069c12ccbSJason Molenda             result.AppendError ("usage: 'command regex <command-name> [s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
10510e5e5a79SGreg Clayton             result.SetStatus (eReturnStatusFailed);
10520e5e5a79SGreg Clayton         }
10530e5e5a79SGreg Clayton         else
10540e5e5a79SGreg Clayton         {
10550e5e5a79SGreg Clayton             Error error;
10565a988416SJim Ingham             const char *name = command.GetArgumentAtIndex(0);
1057de164aaaSGreg Clayton             m_regex_cmd_ap.reset (new CommandObjectRegexCommand (m_interpreter,
1058de164aaaSGreg Clayton                                                                  name,
1059de164aaaSGreg Clayton                                                                  m_options.GetHelp (),
1060de164aaaSGreg Clayton                                                                  m_options.GetSyntax (),
1061b547278cSGreg Clayton                                                                  10,
1062b547278cSGreg Clayton                                                                  0,
1063b547278cSGreg Clayton                                                                  true));
10640e5e5a79SGreg Clayton 
10650e5e5a79SGreg Clayton             if (argc == 1)
10660e5e5a79SGreg Clayton             {
106744d93782SGreg Clayton                 Debugger &debugger = m_interpreter.GetDebugger();
1068e30f11d9SKate Stone                 bool color_prompt = debugger.GetUseColor();
106944d93782SGreg Clayton                 const bool multiple_lines = true; // Get multiple lines
107044d93782SGreg Clayton                 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
1071e30f11d9SKate Stone                                                                   IOHandler::Type::Other,
107273d80faaSGreg Clayton                                                                   "lldb-regex", // Name of input reader for history
1073ea508635SGreg Clayton                                                                   "> ",         // Prompt
1074e30f11d9SKate Stone                                                                   NULL,         // Continuation prompt
107544d93782SGreg Clayton                                                                   multiple_lines,
1076e30f11d9SKate Stone                                                                   color_prompt,
1077f6913cd7SGreg Clayton                                                                   0,            // Don't show line numbers
107844d93782SGreg Clayton                                                                   *this));
107944d93782SGreg Clayton 
108044d93782SGreg Clayton                 if (io_handler_sp)
1081de164aaaSGreg Clayton                 {
108244d93782SGreg Clayton                     debugger.PushIOHandler(io_handler_sp);
1083de164aaaSGreg Clayton                     result.SetStatus (eReturnStatusSuccessFinishNoResult);
1084de164aaaSGreg Clayton                 }
1085de164aaaSGreg Clayton             }
1086de164aaaSGreg Clayton             else
1087de164aaaSGreg Clayton             {
10880e5e5a79SGreg Clayton                 for (size_t arg_idx = 1; arg_idx < argc; ++arg_idx)
10890e5e5a79SGreg Clayton                 {
10905a988416SJim Ingham                     llvm::StringRef arg_strref (command.GetArgumentAtIndex(arg_idx));
109144d93782SGreg Clayton                     bool check_only = false;
109244d93782SGreg Clayton                     error = AppendRegexSubstitution (arg_strref, check_only);
10930e5e5a79SGreg Clayton                     if (error.Fail())
10940e5e5a79SGreg Clayton                         break;
10950e5e5a79SGreg Clayton                 }
10960e5e5a79SGreg Clayton 
10970e5e5a79SGreg Clayton                 if (error.Success())
10980e5e5a79SGreg Clayton                 {
10990e5e5a79SGreg Clayton                     AddRegexCommandToInterpreter();
11000e5e5a79SGreg Clayton                 }
11010e5e5a79SGreg Clayton             }
11020e5e5a79SGreg Clayton             if (error.Fail())
11030e5e5a79SGreg Clayton             {
11040e5e5a79SGreg Clayton                 result.AppendError (error.AsCString());
1105de164aaaSGreg Clayton                 result.SetStatus (eReturnStatusFailed);
1106de164aaaSGreg Clayton             }
11070e5e5a79SGreg Clayton         }
11080e5e5a79SGreg Clayton 
1109de164aaaSGreg Clayton         return result.Succeeded();
1110de164aaaSGreg Clayton     }
1111de164aaaSGreg Clayton 
11120e5e5a79SGreg Clayton     Error
111344d93782SGreg Clayton     AppendRegexSubstitution (const llvm::StringRef &regex_sed, bool check_only)
1114de164aaaSGreg Clayton     {
11150e5e5a79SGreg Clayton         Error error;
11160e5e5a79SGreg Clayton 
11170e5e5a79SGreg Clayton         if (m_regex_cmd_ap.get() == NULL)
1118de164aaaSGreg Clayton         {
11190e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("invalid regular expression command object for: '%.*s'",
11200e5e5a79SGreg Clayton                                            (int)regex_sed.size(),
11210e5e5a79SGreg Clayton                                            regex_sed.data());
11220e5e5a79SGreg Clayton             return error;
1123de164aaaSGreg Clayton         }
11240e5e5a79SGreg Clayton 
11250e5e5a79SGreg Clayton         size_t regex_sed_size = regex_sed.size();
11260e5e5a79SGreg Clayton 
11270e5e5a79SGreg Clayton         if (regex_sed_size <= 1)
11280e5e5a79SGreg Clayton         {
11290e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("regular expression substitution string is too short: '%.*s'",
11300e5e5a79SGreg Clayton                                            (int)regex_sed.size(),
11310e5e5a79SGreg Clayton                                            regex_sed.data());
11320e5e5a79SGreg Clayton             return error;
11330e5e5a79SGreg Clayton         }
11340e5e5a79SGreg Clayton 
11350e5e5a79SGreg Clayton         if (regex_sed[0] != 's')
11360e5e5a79SGreg Clayton         {
11370e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("regular expression substitution string doesn't start with 's': '%.*s'",
11380e5e5a79SGreg Clayton                                            (int)regex_sed.size(),
11390e5e5a79SGreg Clayton                                            regex_sed.data());
11400e5e5a79SGreg Clayton             return error;
11410e5e5a79SGreg Clayton         }
11420e5e5a79SGreg Clayton         const size_t first_separator_char_pos = 1;
11430e5e5a79SGreg Clayton         // use the char that follows 's' as the regex separator character
11440e5e5a79SGreg Clayton         // so we can have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
11450e5e5a79SGreg Clayton         const char separator_char = regex_sed[first_separator_char_pos];
11460e5e5a79SGreg Clayton         const size_t second_separator_char_pos = regex_sed.find (separator_char, first_separator_char_pos + 1);
11470e5e5a79SGreg Clayton 
11480e5e5a79SGreg Clayton         if (second_separator_char_pos == std::string::npos)
11490e5e5a79SGreg Clayton         {
1150ea508635SGreg Clayton             error.SetErrorStringWithFormat("missing second '%c' separator char after '%.*s' in '%.*s'",
11510e5e5a79SGreg Clayton                                            separator_char,
11520e5e5a79SGreg Clayton                                            (int)(regex_sed.size() - first_separator_char_pos - 1),
1153ea508635SGreg Clayton                                            regex_sed.data() + (first_separator_char_pos + 1),
1154ea508635SGreg Clayton                                            (int)regex_sed.size(),
1155ea508635SGreg Clayton                                            regex_sed.data());
11560e5e5a79SGreg Clayton             return error;
11570e5e5a79SGreg Clayton         }
11580e5e5a79SGreg Clayton 
11590e5e5a79SGreg Clayton         const size_t third_separator_char_pos = regex_sed.find (separator_char, second_separator_char_pos + 1);
11600e5e5a79SGreg Clayton 
11610e5e5a79SGreg Clayton         if (third_separator_char_pos == std::string::npos)
11620e5e5a79SGreg Clayton         {
1163ea508635SGreg Clayton             error.SetErrorStringWithFormat("missing third '%c' separator char after '%.*s' in '%.*s'",
11640e5e5a79SGreg Clayton                                            separator_char,
11650e5e5a79SGreg Clayton                                            (int)(regex_sed.size() - second_separator_char_pos - 1),
1166ea508635SGreg Clayton                                            regex_sed.data() + (second_separator_char_pos + 1),
1167ea508635SGreg Clayton                                            (int)regex_sed.size(),
1168ea508635SGreg Clayton                                            regex_sed.data());
11690e5e5a79SGreg Clayton             return error;
11700e5e5a79SGreg Clayton         }
11710e5e5a79SGreg Clayton 
11720e5e5a79SGreg Clayton         if (third_separator_char_pos != regex_sed_size - 1)
11730e5e5a79SGreg Clayton         {
11740e5e5a79SGreg Clayton             // Make sure that everything that follows the last regex
11750e5e5a79SGreg Clayton             // separator char
11760e5e5a79SGreg Clayton             if (regex_sed.find_first_not_of("\t\n\v\f\r ", third_separator_char_pos + 1) != std::string::npos)
11770e5e5a79SGreg Clayton             {
11780e5e5a79SGreg Clayton                 error.SetErrorStringWithFormat("extra data found after the '%.*s' regular expression substitution string: '%.*s'",
11790e5e5a79SGreg Clayton                                                (int)third_separator_char_pos + 1,
11800e5e5a79SGreg Clayton                                                regex_sed.data(),
11810e5e5a79SGreg Clayton                                                (int)(regex_sed.size() - third_separator_char_pos - 1),
11820e5e5a79SGreg Clayton                                                regex_sed.data() + (third_separator_char_pos + 1));
11830e5e5a79SGreg Clayton                 return error;
11840e5e5a79SGreg Clayton             }
11850e5e5a79SGreg Clayton 
11860e5e5a79SGreg Clayton         }
11870e5e5a79SGreg Clayton         else if (first_separator_char_pos + 1 == second_separator_char_pos)
11880e5e5a79SGreg Clayton         {
11890e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
11900e5e5a79SGreg Clayton                                            separator_char,
11910e5e5a79SGreg Clayton                                            separator_char,
11920e5e5a79SGreg Clayton                                            separator_char,
11930e5e5a79SGreg Clayton                                            (int)regex_sed.size(),
11940e5e5a79SGreg Clayton                                            regex_sed.data());
11950e5e5a79SGreg Clayton             return error;
11960e5e5a79SGreg Clayton         }
11970e5e5a79SGreg Clayton         else if (second_separator_char_pos + 1 == third_separator_char_pos)
11980e5e5a79SGreg Clayton         {
11990e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
12000e5e5a79SGreg Clayton                                            separator_char,
12010e5e5a79SGreg Clayton                                            separator_char,
12020e5e5a79SGreg Clayton                                            separator_char,
12030e5e5a79SGreg Clayton                                            (int)regex_sed.size(),
12040e5e5a79SGreg Clayton                                            regex_sed.data());
12050e5e5a79SGreg Clayton             return error;
12060e5e5a79SGreg Clayton         }
120744d93782SGreg Clayton 
120844d93782SGreg Clayton         if (check_only == false)
120944d93782SGreg Clayton         {
12100e5e5a79SGreg Clayton             std::string regex(regex_sed.substr(first_separator_char_pos + 1, second_separator_char_pos - first_separator_char_pos - 1));
12110e5e5a79SGreg Clayton             std::string subst(regex_sed.substr(second_separator_char_pos + 1, third_separator_char_pos - second_separator_char_pos - 1));
12120e5e5a79SGreg Clayton             m_regex_cmd_ap->AddRegexCommand (regex.c_str(),
12130e5e5a79SGreg Clayton                                              subst.c_str());
121444d93782SGreg Clayton         }
12150e5e5a79SGreg Clayton         return error;
1216de164aaaSGreg Clayton     }
1217de164aaaSGreg Clayton 
1218de164aaaSGreg Clayton     void
12190e5e5a79SGreg Clayton     AddRegexCommandToInterpreter()
1220de164aaaSGreg Clayton     {
1221de164aaaSGreg Clayton         if (m_regex_cmd_ap.get())
1222de164aaaSGreg Clayton         {
1223de164aaaSGreg Clayton             if (m_regex_cmd_ap->HasRegexEntries())
1224de164aaaSGreg Clayton             {
1225de164aaaSGreg Clayton                 CommandObjectSP cmd_sp (m_regex_cmd_ap.release());
1226de164aaaSGreg Clayton                 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1227de164aaaSGreg Clayton             }
1228de164aaaSGreg Clayton         }
1229de164aaaSGreg Clayton     }
1230de164aaaSGreg Clayton 
1231de164aaaSGreg Clayton private:
12327b0992d9SGreg Clayton     std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_ap;
1233de164aaaSGreg Clayton 
1234de164aaaSGreg Clayton      class CommandOptions : public Options
1235de164aaaSGreg Clayton      {
1236de164aaaSGreg Clayton      public:
1237de164aaaSGreg Clayton 
1238de164aaaSGreg Clayton          CommandOptions (CommandInterpreter &interpreter) :
1239de164aaaSGreg Clayton             Options (interpreter)
1240de164aaaSGreg Clayton          {
1241de164aaaSGreg Clayton          }
1242de164aaaSGreg Clayton 
1243de164aaaSGreg Clayton          virtual
1244de164aaaSGreg Clayton          ~CommandOptions (){}
1245de164aaaSGreg Clayton 
1246de164aaaSGreg Clayton          virtual Error
1247de164aaaSGreg Clayton          SetOptionValue (uint32_t option_idx, const char *option_arg)
1248de164aaaSGreg Clayton          {
1249de164aaaSGreg Clayton              Error error;
12503bcdfc0eSGreg Clayton              const int short_option = m_getopt_table[option_idx].val;
1251de164aaaSGreg Clayton 
1252de164aaaSGreg Clayton              switch (short_option)
1253de164aaaSGreg Clayton              {
1254de164aaaSGreg Clayton                  case 'h':
1255de164aaaSGreg Clayton                      m_help.assign (option_arg);
1256de164aaaSGreg Clayton                      break;
1257de164aaaSGreg Clayton                  case 's':
1258de164aaaSGreg Clayton                      m_syntax.assign (option_arg);
1259de164aaaSGreg Clayton                      break;
1260de164aaaSGreg Clayton 
1261de164aaaSGreg Clayton                  default:
126286edbf41SGreg Clayton                      error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1263de164aaaSGreg Clayton                      break;
1264de164aaaSGreg Clayton              }
1265de164aaaSGreg Clayton 
1266de164aaaSGreg Clayton              return error;
1267de164aaaSGreg Clayton          }
1268de164aaaSGreg Clayton 
1269de164aaaSGreg Clayton          void
1270de164aaaSGreg Clayton          OptionParsingStarting ()
1271de164aaaSGreg Clayton          {
1272de164aaaSGreg Clayton              m_help.clear();
1273de164aaaSGreg Clayton              m_syntax.clear();
1274de164aaaSGreg Clayton          }
1275de164aaaSGreg Clayton 
1276de164aaaSGreg Clayton          const OptionDefinition*
1277de164aaaSGreg Clayton          GetDefinitions ()
1278de164aaaSGreg Clayton          {
1279de164aaaSGreg Clayton              return g_option_table;
1280de164aaaSGreg Clayton          }
1281de164aaaSGreg Clayton 
1282de164aaaSGreg Clayton          // Options table: Required for subclasses of Options.
1283de164aaaSGreg Clayton 
1284de164aaaSGreg Clayton          static OptionDefinition g_option_table[];
1285de164aaaSGreg Clayton 
1286de164aaaSGreg Clayton          const char *
1287de164aaaSGreg Clayton          GetHelp ()
1288de164aaaSGreg Clayton          {
1289de164aaaSGreg Clayton              if (m_help.empty())
1290de164aaaSGreg Clayton                  return NULL;
1291de164aaaSGreg Clayton              return m_help.c_str();
1292de164aaaSGreg Clayton          }
1293de164aaaSGreg Clayton          const char *
1294de164aaaSGreg Clayton          GetSyntax ()
1295de164aaaSGreg Clayton          {
1296de164aaaSGreg Clayton              if (m_syntax.empty())
1297de164aaaSGreg Clayton                  return NULL;
1298de164aaaSGreg Clayton              return m_syntax.c_str();
1299de164aaaSGreg Clayton          }
1300de164aaaSGreg Clayton          // Instance variables to hold the values for command options.
1301de164aaaSGreg Clayton      protected:
1302de164aaaSGreg Clayton          std::string m_help;
1303de164aaaSGreg Clayton          std::string m_syntax;
1304de164aaaSGreg Clayton      };
1305de164aaaSGreg Clayton 
1306b0a1814fSEric Christopher      Options *
1307b0a1814fSEric Christopher      GetOptions () override
1308de164aaaSGreg Clayton      {
1309de164aaaSGreg Clayton          return &m_options;
1310de164aaaSGreg Clayton      }
1311de164aaaSGreg Clayton 
13125a988416SJim Ingham      CommandOptions m_options;
1313de164aaaSGreg Clayton };
1314de164aaaSGreg Clayton 
1315de164aaaSGreg Clayton OptionDefinition
1316de164aaaSGreg Clayton CommandObjectCommandsAddRegex::CommandOptions::g_option_table[] =
1317de164aaaSGreg Clayton {
1318d37221dcSZachary Turner { LLDB_OPT_SET_1, false, "help"  , 'h', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeNone, "The help text to display for this command."},
1319d37221dcSZachary Turner { LLDB_OPT_SET_1, false, "syntax", 's', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeNone, "A syntax string showing the typical usage syntax."},
1320d37221dcSZachary Turner { 0             , false,  NULL   , 0  , 0                , NULL, NULL, 0, eArgTypeNone, NULL }
1321de164aaaSGreg Clayton };
1322de164aaaSGreg Clayton 
1323de164aaaSGreg Clayton 
13245a988416SJim Ingham class CommandObjectPythonFunction : public CommandObjectRaw
1325223383edSEnrico Granata {
1326223383edSEnrico Granata private:
1327223383edSEnrico Granata     std::string m_function_name;
13280a305db7SEnrico Granata     ScriptedCommandSynchronicity m_synchro;
1329fac939e9SEnrico Granata     bool m_fetched_help_long;
1330223383edSEnrico Granata 
1331223383edSEnrico Granata public:
1332223383edSEnrico Granata 
1333223383edSEnrico Granata     CommandObjectPythonFunction (CommandInterpreter &interpreter,
1334223383edSEnrico Granata                                  std::string name,
13350a305db7SEnrico Granata                                  std::string funct,
1336735152e3SEnrico Granata                                  std::string help,
13370a305db7SEnrico Granata                                  ScriptedCommandSynchronicity synch) :
13385a988416SJim Ingham         CommandObjectRaw (interpreter,
1339223383edSEnrico Granata                           name.c_str(),
1340735152e3SEnrico Granata                           NULL,
1341223383edSEnrico Granata                           NULL),
13420a305db7SEnrico Granata         m_function_name(funct),
1343fac939e9SEnrico Granata         m_synchro(synch),
1344fac939e9SEnrico Granata         m_fetched_help_long(false)
1345223383edSEnrico Granata     {
1346735152e3SEnrico Granata         if (!help.empty())
1347735152e3SEnrico Granata             SetHelp(help.c_str());
1348735152e3SEnrico Granata         else
1349735152e3SEnrico Granata         {
1350735152e3SEnrico Granata             StreamString stream;
1351735152e3SEnrico Granata             stream.Printf("For more information run 'help %s'",name.c_str());
1352735152e3SEnrico Granata             SetHelp(stream.GetData());
1353735152e3SEnrico Granata         }
1354223383edSEnrico Granata     }
1355223383edSEnrico Granata 
1356223383edSEnrico Granata     virtual
1357223383edSEnrico Granata     ~CommandObjectPythonFunction ()
1358223383edSEnrico Granata     {
1359223383edSEnrico Granata     }
1360223383edSEnrico Granata 
1361223383edSEnrico Granata     virtual bool
13623a18e319SGreg Clayton     IsRemovable () const
13635a988416SJim Ingham     {
13645a988416SJim Ingham         return true;
13655a988416SJim Ingham     }
13665a988416SJim Ingham 
13675a988416SJim Ingham     const std::string&
13685a988416SJim Ingham     GetFunctionName ()
13695a988416SJim Ingham     {
13705a988416SJim Ingham         return m_function_name;
13715a988416SJim Ingham     }
13725a988416SJim Ingham 
13735a988416SJim Ingham     ScriptedCommandSynchronicity
13745a988416SJim Ingham     GetSynchronicity ()
13755a988416SJim Ingham     {
13765a988416SJim Ingham         return m_synchro;
13775a988416SJim Ingham     }
13785a988416SJim Ingham 
1379fac939e9SEnrico Granata     virtual const char *
1380fac939e9SEnrico Granata     GetHelpLong ()
1381fac939e9SEnrico Granata     {
1382fac939e9SEnrico Granata         if (!m_fetched_help_long)
1383fac939e9SEnrico Granata         {
1384fac939e9SEnrico Granata             ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
1385fac939e9SEnrico Granata             if (scripter)
1386fac939e9SEnrico Granata             {
1387fac939e9SEnrico Granata                 std::string docstring;
1388fac939e9SEnrico Granata                 m_fetched_help_long = scripter->GetDocumentationForItem(m_function_name.c_str(),docstring);
1389fac939e9SEnrico Granata                 if (!docstring.empty())
1390fac939e9SEnrico Granata                     SetHelpLong(docstring);
1391fac939e9SEnrico Granata             }
1392fac939e9SEnrico Granata         }
1393fac939e9SEnrico Granata         return CommandObjectRaw::GetHelpLong();
1394fac939e9SEnrico Granata     }
1395fac939e9SEnrico Granata 
13965a988416SJim Ingham protected:
13975a988416SJim Ingham     virtual bool
13985a988416SJim Ingham     DoExecute (const char *raw_command_line, CommandReturnObject &result)
1399223383edSEnrico Granata     {
1400223383edSEnrico Granata         ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
1401223383edSEnrico Granata 
1402223383edSEnrico Granata         Error error;
1403223383edSEnrico Granata 
140470f11f88SJim Ingham         result.SetStatus(eReturnStatusInvalid);
140570f11f88SJim Ingham 
1406223383edSEnrico Granata         if (!scripter || scripter->RunScriptBasedCommand(m_function_name.c_str(),
1407223383edSEnrico Granata                                                          raw_command_line,
14080a305db7SEnrico Granata                                                          m_synchro,
1409223383edSEnrico Granata                                                          result,
141006be059aSEnrico Granata                                                          error,
141106be059aSEnrico Granata                                                          m_exe_ctx) == false)
1412223383edSEnrico Granata         {
1413223383edSEnrico Granata             result.AppendError(error.AsCString());
1414223383edSEnrico Granata             result.SetStatus(eReturnStatusFailed);
1415223383edSEnrico Granata         }
1416223383edSEnrico Granata         else
141770f11f88SJim Ingham         {
141870f11f88SJim Ingham             // Don't change the status if the command already set it...
141970f11f88SJim Ingham             if (result.GetStatus() == eReturnStatusInvalid)
142070f11f88SJim Ingham             {
14219a71a7d8SDaniel Malea                 if (result.GetOutputData() == NULL || result.GetOutputData()[0] == '\0')
1422223383edSEnrico Granata                     result.SetStatus(eReturnStatusSuccessFinishNoResult);
142370f11f88SJim Ingham                 else
142470f11f88SJim Ingham                     result.SetStatus(eReturnStatusSuccessFinishResult);
142570f11f88SJim Ingham             }
142670f11f88SJim Ingham         }
1427223383edSEnrico Granata 
1428223383edSEnrico Granata         return result.Succeeded();
1429223383edSEnrico Granata     }
1430223383edSEnrico Granata 
1431223383edSEnrico Granata };
1432223383edSEnrico Granata 
14339fe00e52SEnrico Granata class CommandObjectScriptingObject : public CommandObjectRaw
14349fe00e52SEnrico Granata {
14359fe00e52SEnrico Granata private:
14369fe00e52SEnrico Granata     lldb::ScriptInterpreterObjectSP m_cmd_obj_sp;
14379fe00e52SEnrico Granata     ScriptedCommandSynchronicity m_synchro;
1438*6f79bb2dSEnrico Granata     bool m_fetched_help_short:1;
1439*6f79bb2dSEnrico Granata     bool m_fetched_help_long:1;
14409fe00e52SEnrico Granata 
14419fe00e52SEnrico Granata public:
14429fe00e52SEnrico Granata 
14439fe00e52SEnrico Granata     CommandObjectScriptingObject (CommandInterpreter &interpreter,
14449fe00e52SEnrico Granata                                   std::string name,
14459fe00e52SEnrico Granata                                   lldb::ScriptInterpreterObjectSP cmd_obj_sp,
14469fe00e52SEnrico Granata                                   ScriptedCommandSynchronicity synch) :
14479fe00e52SEnrico Granata     CommandObjectRaw (interpreter,
14489fe00e52SEnrico Granata                       name.c_str(),
14499fe00e52SEnrico Granata                       NULL,
14509fe00e52SEnrico Granata                       NULL),
14519fe00e52SEnrico Granata     m_cmd_obj_sp(cmd_obj_sp),
1452*6f79bb2dSEnrico Granata     m_synchro(synch),
1453*6f79bb2dSEnrico Granata     m_fetched_help_short(false),
1454*6f79bb2dSEnrico Granata     m_fetched_help_long(false)
14559fe00e52SEnrico Granata     {
14569fe00e52SEnrico Granata         StreamString stream;
14579fe00e52SEnrico Granata         stream.Printf("For more information run 'help %s'",name.c_str());
14589fe00e52SEnrico Granata         SetHelp(stream.GetData());
14599fe00e52SEnrico Granata     }
14609fe00e52SEnrico Granata 
14619fe00e52SEnrico Granata     virtual
14629fe00e52SEnrico Granata     ~CommandObjectScriptingObject ()
14639fe00e52SEnrico Granata     {
14649fe00e52SEnrico Granata     }
14659fe00e52SEnrico Granata 
14669fe00e52SEnrico Granata     virtual bool
14679fe00e52SEnrico Granata     IsRemovable () const
14689fe00e52SEnrico Granata     {
14699fe00e52SEnrico Granata         return true;
14709fe00e52SEnrico Granata     }
14719fe00e52SEnrico Granata 
14729fe00e52SEnrico Granata     lldb::ScriptInterpreterObjectSP
14739fe00e52SEnrico Granata     GetImplementingObject ()
14749fe00e52SEnrico Granata     {
14759fe00e52SEnrico Granata         return m_cmd_obj_sp;
14769fe00e52SEnrico Granata     }
14779fe00e52SEnrico Granata 
14789fe00e52SEnrico Granata     ScriptedCommandSynchronicity
14799fe00e52SEnrico Granata     GetSynchronicity ()
14809fe00e52SEnrico Granata     {
14819fe00e52SEnrico Granata         return m_synchro;
14829fe00e52SEnrico Granata     }
14839fe00e52SEnrico Granata 
14849fe00e52SEnrico Granata     virtual const char *
1485*6f79bb2dSEnrico Granata     GetHelp ()
1486*6f79bb2dSEnrico Granata     {
1487*6f79bb2dSEnrico Granata         if (!m_fetched_help_short)
1488*6f79bb2dSEnrico Granata         {
1489*6f79bb2dSEnrico Granata             ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
1490*6f79bb2dSEnrico Granata             if (scripter)
1491*6f79bb2dSEnrico Granata             {
1492*6f79bb2dSEnrico Granata                 std::string docstring;
1493*6f79bb2dSEnrico Granata                 m_fetched_help_short = scripter->GetShortHelpForCommandObject(m_cmd_obj_sp,docstring);
1494*6f79bb2dSEnrico Granata                 if (!docstring.empty())
1495*6f79bb2dSEnrico Granata                     SetHelp(docstring);
1496*6f79bb2dSEnrico Granata             }
1497*6f79bb2dSEnrico Granata         }
1498*6f79bb2dSEnrico Granata         return CommandObjectRaw::GetHelp();
1499*6f79bb2dSEnrico Granata     }
1500*6f79bb2dSEnrico Granata 
1501*6f79bb2dSEnrico Granata     virtual const char *
15029fe00e52SEnrico Granata     GetHelpLong ()
15039fe00e52SEnrico Granata     {
1504*6f79bb2dSEnrico Granata         if (!m_fetched_help_long)
1505*6f79bb2dSEnrico Granata         {
1506*6f79bb2dSEnrico Granata             ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
1507*6f79bb2dSEnrico Granata             if (scripter)
1508*6f79bb2dSEnrico Granata             {
1509*6f79bb2dSEnrico Granata                 std::string docstring;
1510*6f79bb2dSEnrico Granata                 m_fetched_help_long = scripter->GetLongHelpForCommandObject(m_cmd_obj_sp,docstring);
1511*6f79bb2dSEnrico Granata                 if (!docstring.empty())
1512*6f79bb2dSEnrico Granata                     SetHelpLong(docstring);
1513*6f79bb2dSEnrico Granata             }
1514*6f79bb2dSEnrico Granata         }
15159fe00e52SEnrico Granata         return CommandObjectRaw::GetHelpLong();
15169fe00e52SEnrico Granata     }
15179fe00e52SEnrico Granata 
15189fe00e52SEnrico Granata protected:
15199fe00e52SEnrico Granata     virtual bool
15209fe00e52SEnrico Granata     DoExecute (const char *raw_command_line, CommandReturnObject &result)
15219fe00e52SEnrico Granata     {
15229fe00e52SEnrico Granata         ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
15239fe00e52SEnrico Granata 
15249fe00e52SEnrico Granata         Error error;
15259fe00e52SEnrico Granata 
15269fe00e52SEnrico Granata         result.SetStatus(eReturnStatusInvalid);
15279fe00e52SEnrico Granata 
15289fe00e52SEnrico Granata         if (!scripter || scripter->RunScriptBasedCommand(m_cmd_obj_sp,
15299fe00e52SEnrico Granata                                                          raw_command_line,
15309fe00e52SEnrico Granata                                                          m_synchro,
15319fe00e52SEnrico Granata                                                          result,
15329fe00e52SEnrico Granata                                                          error,
15339fe00e52SEnrico Granata                                                          m_exe_ctx) == false)
15349fe00e52SEnrico Granata         {
15359fe00e52SEnrico Granata             result.AppendError(error.AsCString());
15369fe00e52SEnrico Granata             result.SetStatus(eReturnStatusFailed);
15379fe00e52SEnrico Granata         }
15389fe00e52SEnrico Granata         else
15399fe00e52SEnrico Granata         {
15409fe00e52SEnrico Granata             // Don't change the status if the command already set it...
15419fe00e52SEnrico Granata             if (result.GetStatus() == eReturnStatusInvalid)
15429fe00e52SEnrico Granata             {
15439fe00e52SEnrico Granata                 if (result.GetOutputData() == NULL || result.GetOutputData()[0] == '\0')
15449fe00e52SEnrico Granata                     result.SetStatus(eReturnStatusSuccessFinishNoResult);
15459fe00e52SEnrico Granata                 else
15469fe00e52SEnrico Granata                     result.SetStatus(eReturnStatusSuccessFinishResult);
15479fe00e52SEnrico Granata             }
15489fe00e52SEnrico Granata         }
15499fe00e52SEnrico Granata 
15509fe00e52SEnrico Granata         return result.Succeeded();
15519fe00e52SEnrico Granata     }
15529fe00e52SEnrico Granata 
15539fe00e52SEnrico Granata };
15549fe00e52SEnrico Granata 
1555a9dbf432SEnrico Granata //-------------------------------------------------------------------------
1556a9dbf432SEnrico Granata // CommandObjectCommandsScriptImport
1557a9dbf432SEnrico Granata //-------------------------------------------------------------------------
1558a9dbf432SEnrico Granata 
15595a988416SJim Ingham class CommandObjectCommandsScriptImport : public CommandObjectParsed
1560a9dbf432SEnrico Granata {
15615a988416SJim Ingham public:
15625a988416SJim Ingham     CommandObjectCommandsScriptImport (CommandInterpreter &interpreter) :
15635a988416SJim Ingham         CommandObjectParsed (interpreter,
15645a988416SJim Ingham                              "command script import",
15655a988416SJim Ingham                              "Import a scripting module in LLDB.",
15665a988416SJim Ingham                              NULL),
15675a988416SJim Ingham         m_options(interpreter)
15685a988416SJim Ingham     {
15695a988416SJim Ingham         CommandArgumentEntry arg1;
15705a988416SJim Ingham         CommandArgumentData cmd_arg;
15715a988416SJim Ingham 
15725a988416SJim Ingham         // Define the first (and only) variant of this arg.
15735a988416SJim Ingham         cmd_arg.arg_type = eArgTypeFilename;
15745a988416SJim Ingham         cmd_arg.arg_repetition = eArgRepeatPlain;
15755a988416SJim Ingham 
15765a988416SJim Ingham         // There is only one variant this argument could be; put it into the argument entry.
15775a988416SJim Ingham         arg1.push_back (cmd_arg);
15785a988416SJim Ingham 
15795a988416SJim Ingham         // Push the data for the first argument into the m_arguments vector.
15805a988416SJim Ingham         m_arguments.push_back (arg1);
15815a988416SJim Ingham     }
15825a988416SJim Ingham 
15835a988416SJim Ingham     ~CommandObjectCommandsScriptImport ()
15845a988416SJim Ingham     {
15855a988416SJim Ingham     }
15865a988416SJim Ingham 
1587c7bece56SGreg Clayton     virtual int
15885a988416SJim Ingham     HandleArgumentCompletion (Args &input,
15895a988416SJim Ingham                               int &cursor_index,
15905a988416SJim Ingham                               int &cursor_char_position,
15915a988416SJim Ingham                               OptionElementVector &opt_element_vector,
15925a988416SJim Ingham                               int match_start_point,
15935a988416SJim Ingham                               int max_return_elements,
15945a988416SJim Ingham                               bool &word_complete,
15955a988416SJim Ingham                               StringList &matches)
15965a988416SJim Ingham     {
15975a988416SJim Ingham         std::string completion_str (input.GetArgumentAtIndex(cursor_index));
15985a988416SJim Ingham         completion_str.erase (cursor_char_position);
15995a988416SJim Ingham 
16005a988416SJim Ingham         CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
16015a988416SJim Ingham                                                              CommandCompletions::eDiskFileCompletion,
16025a988416SJim Ingham                                                              completion_str.c_str(),
16035a988416SJim Ingham                                                              match_start_point,
16045a988416SJim Ingham                                                              max_return_elements,
16055a988416SJim Ingham                                                              NULL,
16065a988416SJim Ingham                                                              word_complete,
16075a988416SJim Ingham                                                              matches);
16085a988416SJim Ingham         return matches.GetSize();
16095a988416SJim Ingham     }
16105a988416SJim Ingham 
16115a988416SJim Ingham     virtual Options *
16125a988416SJim Ingham     GetOptions ()
16135a988416SJim Ingham     {
16145a988416SJim Ingham         return &m_options;
16155a988416SJim Ingham     }
16165a988416SJim Ingham 
16175a988416SJim Ingham protected:
16180a305db7SEnrico Granata 
16190a305db7SEnrico Granata     class CommandOptions : public Options
16200a305db7SEnrico Granata     {
16210a305db7SEnrico Granata     public:
16220a305db7SEnrico Granata 
16230a305db7SEnrico Granata         CommandOptions (CommandInterpreter &interpreter) :
16240a305db7SEnrico Granata             Options (interpreter)
16250a305db7SEnrico Granata         {
16260a305db7SEnrico Granata         }
16270a305db7SEnrico Granata 
16280a305db7SEnrico Granata         virtual
16290a305db7SEnrico Granata         ~CommandOptions (){}
16300a305db7SEnrico Granata 
16310a305db7SEnrico Granata         virtual Error
16320a305db7SEnrico Granata         SetOptionValue (uint32_t option_idx, const char *option_arg)
16330a305db7SEnrico Granata         {
16340a305db7SEnrico Granata             Error error;
16353bcdfc0eSGreg Clayton             const int short_option = m_getopt_table[option_idx].val;
16360a305db7SEnrico Granata 
16370a305db7SEnrico Granata             switch (short_option)
16380a305db7SEnrico Granata             {
16390a305db7SEnrico Granata                 case 'r':
16400a305db7SEnrico Granata                     m_allow_reload = true;
16410a305db7SEnrico Granata                     break;
16420a305db7SEnrico Granata                 default:
16430a305db7SEnrico Granata                     error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
16440a305db7SEnrico Granata                     break;
16450a305db7SEnrico Granata             }
16460a305db7SEnrico Granata 
16470a305db7SEnrico Granata             return error;
16480a305db7SEnrico Granata         }
16490a305db7SEnrico Granata 
16500a305db7SEnrico Granata         void
16510a305db7SEnrico Granata         OptionParsingStarting ()
16520a305db7SEnrico Granata         {
1653e0c70f1bSEnrico Granata             m_allow_reload = true;
16540a305db7SEnrico Granata         }
16550a305db7SEnrico Granata 
16560a305db7SEnrico Granata         const OptionDefinition*
16570a305db7SEnrico Granata         GetDefinitions ()
16580a305db7SEnrico Granata         {
16590a305db7SEnrico Granata             return g_option_table;
16600a305db7SEnrico Granata         }
16610a305db7SEnrico Granata 
16620a305db7SEnrico Granata         // Options table: Required for subclasses of Options.
16630a305db7SEnrico Granata 
16640a305db7SEnrico Granata         static OptionDefinition g_option_table[];
16650a305db7SEnrico Granata 
16660a305db7SEnrico Granata         // Instance variables to hold the values for command options.
16670a305db7SEnrico Granata 
16680a305db7SEnrico Granata         bool m_allow_reload;
16690a305db7SEnrico Granata     };
16700a305db7SEnrico Granata 
1671a9dbf432SEnrico Granata     bool
16725a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
1673a9dbf432SEnrico Granata     {
1674a9dbf432SEnrico Granata 
1675a9dbf432SEnrico Granata         if (m_interpreter.GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython)
1676a9dbf432SEnrico Granata         {
1677a9dbf432SEnrico Granata             result.AppendError ("only scripting language supported for module importing is currently Python");
1678a9dbf432SEnrico Granata             result.SetStatus (eReturnStatusFailed);
1679a9dbf432SEnrico Granata             return false;
1680a9dbf432SEnrico Granata         }
1681a9dbf432SEnrico Granata 
16825a988416SJim Ingham         size_t argc = command.GetArgumentCount();
1683a9dbf432SEnrico Granata 
1684a9dbf432SEnrico Granata         if (argc != 1)
1685a9dbf432SEnrico Granata         {
1686a9dbf432SEnrico Granata             result.AppendError ("'command script import' requires one argument");
1687a9dbf432SEnrico Granata             result.SetStatus (eReturnStatusFailed);
1688a9dbf432SEnrico Granata             return false;
1689a9dbf432SEnrico Granata         }
1690a9dbf432SEnrico Granata 
16915a988416SJim Ingham         std::string path = command.GetArgumentAtIndex(0);
1692a9dbf432SEnrico Granata         Error error;
1693a9dbf432SEnrico Granata 
1694c9d645d3SGreg Clayton         const bool init_session = true;
1695078551c7SEnrico Granata         // FIXME: this is necessary because CommandObject::CheckRequirements() assumes that
1696078551c7SEnrico Granata         // commands won't ever be recursively invoked, but it's actually possible to craft
1697078551c7SEnrico Granata         // a Python script that does other "command script imports" in __lldb_init_module
1698078551c7SEnrico Granata         // the real fix is to have recursive commands possible with a CommandInvocation object
1699078551c7SEnrico Granata         // separate from the CommandObject itself, so that recursive command invocations
1700078551c7SEnrico Granata         // won't stomp on each other (wrt to execution contents, options, and more)
1701078551c7SEnrico Granata         m_exe_ctx.Clear();
1702a9dbf432SEnrico Granata         if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule(path.c_str(),
17030a305db7SEnrico Granata                                                                       m_options.m_allow_reload,
1704c9d645d3SGreg Clayton                                                                       init_session,
1705a9dbf432SEnrico Granata                                                                       error))
1706a9dbf432SEnrico Granata         {
1707a9dbf432SEnrico Granata             result.SetStatus (eReturnStatusSuccessFinishNoResult);
1708a9dbf432SEnrico Granata         }
1709a9dbf432SEnrico Granata         else
1710a9dbf432SEnrico Granata         {
1711a9dbf432SEnrico Granata             result.AppendErrorWithFormat("module importing failed: %s", error.AsCString());
1712a9dbf432SEnrico Granata             result.SetStatus (eReturnStatusFailed);
1713a9dbf432SEnrico Granata         }
1714a9dbf432SEnrico Granata 
1715a9dbf432SEnrico Granata         return result.Succeeded();
1716a9dbf432SEnrico Granata     }
17170a305db7SEnrico Granata 
17185a988416SJim Ingham     CommandOptions m_options;
1719a9dbf432SEnrico Granata };
1720223383edSEnrico Granata 
17210a305db7SEnrico Granata OptionDefinition
17220a305db7SEnrico Granata CommandObjectCommandsScriptImport::CommandOptions::g_option_table[] =
17230a305db7SEnrico Granata {
1724d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "allow-reload", 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone,        "Allow the script to be loaded even if it was already loaded before. This argument exists for backwards compatibility, but reloading is always allowed, whether you specify it or not."},
1725d37221dcSZachary Turner     { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
17260a305db7SEnrico Granata };
17270a305db7SEnrico Granata 
17280a305db7SEnrico Granata 
1729223383edSEnrico Granata //-------------------------------------------------------------------------
1730223383edSEnrico Granata // CommandObjectCommandsScriptAdd
1731223383edSEnrico Granata //-------------------------------------------------------------------------
1732223383edSEnrico Granata 
173344d93782SGreg Clayton class CommandObjectCommandsScriptAdd :
173444d93782SGreg Clayton     public CommandObjectParsed,
173544d93782SGreg Clayton     public IOHandlerDelegateMultiline
1736223383edSEnrico Granata {
17375a988416SJim Ingham public:
17385a988416SJim Ingham     CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter) :
17395a988416SJim Ingham         CommandObjectParsed (interpreter,
17405a988416SJim Ingham                              "command script add",
17415a988416SJim Ingham                              "Add a scripted function as an LLDB command.",
17425a988416SJim Ingham                              NULL),
1743c3d874a5SGreg Clayton         IOHandlerDelegateMultiline ("DONE"),
17445a988416SJim Ingham         m_options (interpreter)
17455a988416SJim Ingham     {
17465a988416SJim Ingham         CommandArgumentEntry arg1;
17475a988416SJim Ingham         CommandArgumentData cmd_arg;
17485a988416SJim Ingham 
17495a988416SJim Ingham         // Define the first (and only) variant of this arg.
17505a988416SJim Ingham         cmd_arg.arg_type = eArgTypeCommandName;
17515a988416SJim Ingham         cmd_arg.arg_repetition = eArgRepeatPlain;
17525a988416SJim Ingham 
17535a988416SJim Ingham         // There is only one variant this argument could be; put it into the argument entry.
17545a988416SJim Ingham         arg1.push_back (cmd_arg);
17555a988416SJim Ingham 
17565a988416SJim Ingham         // Push the data for the first argument into the m_arguments vector.
17575a988416SJim Ingham         m_arguments.push_back (arg1);
17585a988416SJim Ingham     }
17595a988416SJim Ingham 
17605a988416SJim Ingham     ~CommandObjectCommandsScriptAdd ()
17615a988416SJim Ingham     {
17625a988416SJim Ingham     }
17635a988416SJim Ingham 
17645a988416SJim Ingham     virtual Options *
17655a988416SJim Ingham     GetOptions ()
17665a988416SJim Ingham     {
17675a988416SJim Ingham         return &m_options;
17685a988416SJim Ingham     }
17695a988416SJim Ingham 
17705a988416SJim Ingham protected:
1771223383edSEnrico Granata 
1772223383edSEnrico Granata     class CommandOptions : public Options
1773223383edSEnrico Granata     {
1774223383edSEnrico Granata     public:
1775223383edSEnrico Granata 
1776223383edSEnrico Granata         CommandOptions (CommandInterpreter &interpreter) :
17779fe00e52SEnrico Granata             Options (interpreter),
17789fe00e52SEnrico Granata             m_class_name(),
17799fe00e52SEnrico Granata             m_funct_name(),
17809fe00e52SEnrico Granata             m_short_help(),
17819fe00e52SEnrico Granata             m_synchronicity(eScriptedCommandSynchronicitySynchronous)
1782223383edSEnrico Granata         {
1783223383edSEnrico Granata         }
1784223383edSEnrico Granata 
1785223383edSEnrico Granata         virtual
1786223383edSEnrico Granata         ~CommandOptions (){}
1787223383edSEnrico Granata 
1788223383edSEnrico Granata         virtual Error
1789223383edSEnrico Granata         SetOptionValue (uint32_t option_idx, const char *option_arg)
1790223383edSEnrico Granata         {
1791223383edSEnrico Granata             Error error;
17923bcdfc0eSGreg Clayton             const int short_option = m_getopt_table[option_idx].val;
1793223383edSEnrico Granata 
1794223383edSEnrico Granata             switch (short_option)
1795223383edSEnrico Granata             {
1796223383edSEnrico Granata                 case 'f':
1797735152e3SEnrico Granata                     if (option_arg)
1798735152e3SEnrico Granata                         m_funct_name.assign(option_arg);
1799735152e3SEnrico Granata                     break;
18009fe00e52SEnrico Granata                 case 'c':
18019fe00e52SEnrico Granata                     if (option_arg)
18029fe00e52SEnrico Granata                         m_class_name.assign(option_arg);
18039fe00e52SEnrico Granata                     break;
1804735152e3SEnrico Granata                 case 'h':
1805735152e3SEnrico Granata                     if (option_arg)
1806735152e3SEnrico Granata                         m_short_help.assign(option_arg);
1807223383edSEnrico Granata                     break;
18080a305db7SEnrico Granata                 case 's':
180944d93782SGreg Clayton                     m_synchronicity = (ScriptedCommandSynchronicity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error);
18100a305db7SEnrico Granata                     if (!error.Success())
18110a305db7SEnrico Granata                         error.SetErrorStringWithFormat ("unrecognized value for synchronicity '%s'", option_arg);
18120a305db7SEnrico Granata                     break;
1813223383edSEnrico Granata                 default:
181486edbf41SGreg Clayton                     error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1815223383edSEnrico Granata                     break;
1816223383edSEnrico Granata             }
1817223383edSEnrico Granata 
1818223383edSEnrico Granata             return error;
1819223383edSEnrico Granata         }
1820223383edSEnrico Granata 
1821223383edSEnrico Granata         void
1822223383edSEnrico Granata         OptionParsingStarting ()
1823223383edSEnrico Granata         {
18249fe00e52SEnrico Granata             m_class_name.clear();
1825735152e3SEnrico Granata             m_funct_name.clear();
1826735152e3SEnrico Granata             m_short_help.clear();
182744d93782SGreg Clayton             m_synchronicity = eScriptedCommandSynchronicitySynchronous;
1828223383edSEnrico Granata         }
1829223383edSEnrico Granata 
1830223383edSEnrico Granata         const OptionDefinition*
1831223383edSEnrico Granata         GetDefinitions ()
1832223383edSEnrico Granata         {
1833223383edSEnrico Granata             return g_option_table;
1834223383edSEnrico Granata         }
1835223383edSEnrico Granata 
1836223383edSEnrico Granata         // Options table: Required for subclasses of Options.
1837223383edSEnrico Granata 
1838223383edSEnrico Granata         static OptionDefinition g_option_table[];
1839223383edSEnrico Granata 
1840223383edSEnrico Granata         // Instance variables to hold the values for command options.
1841223383edSEnrico Granata 
18429fe00e52SEnrico Granata         std::string m_class_name;
1843223383edSEnrico Granata         std::string m_funct_name;
1844735152e3SEnrico Granata         std::string m_short_help;
184544d93782SGreg Clayton         ScriptedCommandSynchronicity m_synchronicity;
1846223383edSEnrico Granata     };
1847223383edSEnrico Granata 
184844d93782SGreg Clayton     virtual void
184944d93782SGreg Clayton     IOHandlerActivated (IOHandler &io_handler)
1850223383edSEnrico Granata     {
185144d93782SGreg Clayton         StreamFileSP output_sp(io_handler.GetOutputStreamFile());
185244d93782SGreg Clayton         if (output_sp)
1853223383edSEnrico Granata         {
185444d93782SGreg Clayton             output_sp->PutCString(g_python_command_instructions);
185544d93782SGreg Clayton             output_sp->Flush();
1856223383edSEnrico Granata         }
1857223383edSEnrico Granata     }
1858223383edSEnrico Granata 
1859223383edSEnrico Granata 
186044d93782SGreg Clayton     virtual void
186144d93782SGreg Clayton     IOHandlerInputComplete (IOHandler &io_handler, std::string &data)
1862223383edSEnrico Granata     {
186344d93782SGreg Clayton         StreamFileSP error_sp = io_handler.GetErrorStreamFile();
186444d93782SGreg Clayton 
186544d93782SGreg Clayton         ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
186644d93782SGreg Clayton         if (interpreter)
186744d93782SGreg Clayton         {
186844d93782SGreg Clayton 
186944d93782SGreg Clayton             StringList lines;
187044d93782SGreg Clayton             lines.SplitIntoLines(data);
187144d93782SGreg Clayton             if (lines.GetSize() > 0)
187244d93782SGreg Clayton             {
1873a73b7df7SEnrico Granata                 std::string funct_name_str;
187444d93782SGreg Clayton                 if (interpreter->GenerateScriptAliasFunction (lines, funct_name_str))
1875223383edSEnrico Granata                 {
1876a73b7df7SEnrico Granata                     if (funct_name_str.empty())
1877223383edSEnrico Granata                     {
187844d93782SGreg Clayton                         error_sp->Printf ("error: unable to obtain a function name, didn't add python command.\n");
187944d93782SGreg Clayton                         error_sp->Flush();
1880223383edSEnrico Granata                     }
188144d93782SGreg Clayton                     else
188244d93782SGreg Clayton                     {
1883223383edSEnrico Granata                         // everything should be fine now, let's add this alias
1884223383edSEnrico Granata 
1885223383edSEnrico Granata                         CommandObjectSP command_obj_sp(new CommandObjectPythonFunction (m_interpreter,
1886223383edSEnrico Granata                                                                                         m_cmd_name,
1887a73b7df7SEnrico Granata                                                                                         funct_name_str.c_str(),
1888735152e3SEnrico Granata                                                                                         m_short_help,
188944d93782SGreg Clayton                                                                                         m_synchronicity));
1890223383edSEnrico Granata 
18910a305db7SEnrico Granata                         if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp, true))
1892223383edSEnrico Granata                         {
189344d93782SGreg Clayton                             error_sp->Printf ("error: unable to add selected command, didn't add python command.\n");
189444d93782SGreg Clayton                             error_sp->Flush();
1895223383edSEnrico Granata                         }
1896223383edSEnrico Granata                     }
189744d93782SGreg Clayton                 }
189844d93782SGreg Clayton                 else
189944d93782SGreg Clayton                 {
190044d93782SGreg Clayton                     error_sp->Printf ("error: unable to create function, didn't add python command.\n");
190144d93782SGreg Clayton                     error_sp->Flush();
190244d93782SGreg Clayton                 }
190344d93782SGreg Clayton             }
190444d93782SGreg Clayton             else
190544d93782SGreg Clayton             {
190644d93782SGreg Clayton                 error_sp->Printf ("error: empty function, didn't add python command.\n");
190744d93782SGreg Clayton                 error_sp->Flush();
190844d93782SGreg Clayton             }
190944d93782SGreg Clayton         }
191044d93782SGreg Clayton         else
191144d93782SGreg Clayton         {
191244d93782SGreg Clayton             error_sp->Printf ("error: script interpreter missing, didn't add python command.\n");
191344d93782SGreg Clayton             error_sp->Flush();
191444d93782SGreg Clayton         }
191544d93782SGreg Clayton 
191644d93782SGreg Clayton         io_handler.SetIsDone(true);
191744d93782SGreg Clayton 
191844d93782SGreg Clayton 
191944d93782SGreg Clayton     }
1920223383edSEnrico Granata 
19215a988416SJim Ingham protected:
1922223383edSEnrico Granata     bool
19235a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
1924223383edSEnrico Granata     {
192599f0b8f9SEnrico Granata 
192699f0b8f9SEnrico Granata         if (m_interpreter.GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython)
192799f0b8f9SEnrico Granata         {
192899f0b8f9SEnrico Granata             result.AppendError ("only scripting language supported for scripted commands is currently Python");
192999f0b8f9SEnrico Granata             result.SetStatus (eReturnStatusFailed);
193099f0b8f9SEnrico Granata             return false;
193199f0b8f9SEnrico Granata         }
193299f0b8f9SEnrico Granata 
19335a988416SJim Ingham         size_t argc = command.GetArgumentCount();
1934223383edSEnrico Granata 
1935223383edSEnrico Granata         if (argc != 1)
1936223383edSEnrico Granata         {
1937223383edSEnrico Granata             result.AppendError ("'command script add' requires one argument");
1938223383edSEnrico Granata             result.SetStatus (eReturnStatusFailed);
1939223383edSEnrico Granata             return false;
1940223383edSEnrico Granata         }
1941223383edSEnrico Granata 
1942735152e3SEnrico Granata         // Store the options in case we get multi-line input
194344d93782SGreg Clayton         m_cmd_name = command.GetArgumentAtIndex(0);
1944735152e3SEnrico Granata         m_short_help.assign(m_options.m_short_help);
194544d93782SGreg Clayton         m_synchronicity = m_options.m_synchronicity;
1946223383edSEnrico Granata 
19479fe00e52SEnrico Granata         if (m_options.m_class_name.empty())
19489fe00e52SEnrico Granata         {
1949223383edSEnrico Granata             if (m_options.m_funct_name.empty())
1950223383edSEnrico Granata             {
195144d93782SGreg Clayton                 m_interpreter.GetPythonCommandsFromIOHandler ("     ",  // Prompt
195244d93782SGreg Clayton                                                               *this,    // IOHandlerDelegate
195344d93782SGreg Clayton                                                               true,     // Run IOHandler in async mode
195444d93782SGreg Clayton                                                               NULL);    // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
1955223383edSEnrico Granata             }
1956223383edSEnrico Granata             else
1957223383edSEnrico Granata             {
19580a305db7SEnrico Granata                 CommandObjectSP new_cmd(new CommandObjectPythonFunction(m_interpreter,
195944d93782SGreg Clayton                                                                         m_cmd_name,
19600a305db7SEnrico Granata                                                                         m_options.m_funct_name,
1961735152e3SEnrico Granata                                                                         m_options.m_short_help,
196244d93782SGreg Clayton                                                                         m_synchronicity));
196344d93782SGreg Clayton                 if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true))
1964223383edSEnrico Granata                 {
1965223383edSEnrico Granata                     result.SetStatus (eReturnStatusSuccessFinishNoResult);
1966223383edSEnrico Granata                 }
1967223383edSEnrico Granata                 else
1968223383edSEnrico Granata                 {
1969223383edSEnrico Granata                     result.AppendError("cannot add command");
1970223383edSEnrico Granata                     result.SetStatus (eReturnStatusFailed);
1971223383edSEnrico Granata                 }
1972223383edSEnrico Granata             }
19739fe00e52SEnrico Granata         }
19749fe00e52SEnrico Granata         else
19759fe00e52SEnrico Granata         {
19769fe00e52SEnrico Granata             ScriptInterpreter *interpreter = GetCommandInterpreter().GetScriptInterpreter();
19779fe00e52SEnrico Granata             if (!interpreter)
19789fe00e52SEnrico Granata             {
19799fe00e52SEnrico Granata                 result.AppendError("cannot find ScriptInterpreter");
19809fe00e52SEnrico Granata                 result.SetStatus(eReturnStatusFailed);
19819fe00e52SEnrico Granata                 return false;
19829fe00e52SEnrico Granata             }
19839fe00e52SEnrico Granata 
19849fe00e52SEnrico Granata             auto cmd_obj_sp = interpreter->CreateScriptCommandObject(m_options.m_class_name.c_str());
19859fe00e52SEnrico Granata             if (!cmd_obj_sp)
19869fe00e52SEnrico Granata             {
19879fe00e52SEnrico Granata                 result.AppendError("cannot create helper object");
19889fe00e52SEnrico Granata                 result.SetStatus(eReturnStatusFailed);
19899fe00e52SEnrico Granata                 return false;
19909fe00e52SEnrico Granata             }
19919fe00e52SEnrico Granata 
19929fe00e52SEnrico Granata             CommandObjectSP new_cmd(new CommandObjectScriptingObject(m_interpreter,
19939fe00e52SEnrico Granata                                                                      m_cmd_name,
19949fe00e52SEnrico Granata                                                                      cmd_obj_sp,
19959fe00e52SEnrico Granata                                                                      m_synchronicity));
19969fe00e52SEnrico Granata             if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true))
19979fe00e52SEnrico Granata             {
19989fe00e52SEnrico Granata                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
19999fe00e52SEnrico Granata             }
20009fe00e52SEnrico Granata             else
20019fe00e52SEnrico Granata             {
20029fe00e52SEnrico Granata                 result.AppendError("cannot add command");
20039fe00e52SEnrico Granata                 result.SetStatus (eReturnStatusFailed);
20049fe00e52SEnrico Granata             }
20059fe00e52SEnrico Granata         }
2006223383edSEnrico Granata 
2007223383edSEnrico Granata         return result.Succeeded();
2008223383edSEnrico Granata 
2009223383edSEnrico Granata     }
20105a988416SJim Ingham 
20115a988416SJim Ingham     CommandOptions m_options;
201244d93782SGreg Clayton     std::string m_cmd_name;
2013735152e3SEnrico Granata     std::string m_short_help;
201444d93782SGreg Clayton     ScriptedCommandSynchronicity m_synchronicity;
2015223383edSEnrico Granata };
2016223383edSEnrico Granata 
20170a305db7SEnrico Granata static OptionEnumValueElement g_script_synchro_type[] =
20180a305db7SEnrico Granata {
20190a305db7SEnrico Granata     { eScriptedCommandSynchronicitySynchronous,      "synchronous",       "Run synchronous"},
20200a305db7SEnrico Granata     { eScriptedCommandSynchronicityAsynchronous,     "asynchronous",      "Run asynchronous"},
20210a305db7SEnrico Granata     { eScriptedCommandSynchronicityCurrentValue,     "current",           "Do not alter current setting"},
20220a305db7SEnrico Granata     { 0, NULL, NULL }
20230a305db7SEnrico Granata };
20240a305db7SEnrico Granata 
2025223383edSEnrico Granata OptionDefinition
2026223383edSEnrico Granata CommandObjectCommandsScriptAdd::CommandOptions::g_option_table[] =
2027223383edSEnrico Granata {
2028d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "function", 'f', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePythonFunction,        "Name of the Python function to bind to this command name."},
20299fe00e52SEnrico Granata     { LLDB_OPT_SET_2, false, "class", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePythonClass,        "Name of the Python class to bind to this command name."},
20309fe00e52SEnrico Granata     { LLDB_OPT_SET_ALL, false, "help"  , 'h', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeHelpText, "The help text to display for this command."},
20319fe00e52SEnrico Granata     { LLDB_OPT_SET_ALL, false, "synchronicity", 's', OptionParser::eRequiredArgument, NULL, g_script_synchro_type, 0, eArgTypeScriptedCommandSynchronicity,        "Set the synchronicity of this command's executions with regard to LLDB event system."},
2032d37221dcSZachary Turner     { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
2033223383edSEnrico Granata };
2034223383edSEnrico Granata 
2035223383edSEnrico Granata //-------------------------------------------------------------------------
2036223383edSEnrico Granata // CommandObjectCommandsScriptList
2037223383edSEnrico Granata //-------------------------------------------------------------------------
2038223383edSEnrico Granata 
20395a988416SJim Ingham class CommandObjectCommandsScriptList : public CommandObjectParsed
2040223383edSEnrico Granata {
2041223383edSEnrico Granata private:
2042223383edSEnrico Granata 
2043223383edSEnrico Granata public:
2044223383edSEnrico Granata     CommandObjectCommandsScriptList(CommandInterpreter &interpreter) :
20455a988416SJim Ingham     CommandObjectParsed (interpreter,
2046223383edSEnrico Granata                    "command script list",
2047223383edSEnrico Granata                    "List defined scripted commands.",
2048223383edSEnrico Granata                    NULL)
2049223383edSEnrico Granata     {
2050223383edSEnrico Granata     }
2051223383edSEnrico Granata 
2052223383edSEnrico Granata     ~CommandObjectCommandsScriptList ()
2053223383edSEnrico Granata     {
2054223383edSEnrico Granata     }
2055223383edSEnrico Granata 
2056223383edSEnrico Granata     bool
20575a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
2058223383edSEnrico Granata     {
2059223383edSEnrico Granata 
2060223383edSEnrico Granata         m_interpreter.GetHelp(result,
2061223383edSEnrico Granata                               CommandInterpreter::eCommandTypesUserDef);
2062223383edSEnrico Granata 
2063223383edSEnrico Granata         result.SetStatus (eReturnStatusSuccessFinishResult);
2064223383edSEnrico Granata 
2065223383edSEnrico Granata         return true;
2066223383edSEnrico Granata 
2067223383edSEnrico Granata 
2068223383edSEnrico Granata     }
2069223383edSEnrico Granata };
2070223383edSEnrico Granata 
2071223383edSEnrico Granata //-------------------------------------------------------------------------
2072223383edSEnrico Granata // CommandObjectCommandsScriptClear
2073223383edSEnrico Granata //-------------------------------------------------------------------------
2074223383edSEnrico Granata 
20755a988416SJim Ingham class CommandObjectCommandsScriptClear : public CommandObjectParsed
2076223383edSEnrico Granata {
2077223383edSEnrico Granata private:
2078223383edSEnrico Granata 
2079223383edSEnrico Granata public:
2080223383edSEnrico Granata     CommandObjectCommandsScriptClear(CommandInterpreter &interpreter) :
20815a988416SJim Ingham         CommandObjectParsed (interpreter,
2082223383edSEnrico Granata                              "command script clear",
2083223383edSEnrico Granata                              "Delete all scripted commands.",
2084223383edSEnrico Granata                              NULL)
2085223383edSEnrico Granata     {
2086223383edSEnrico Granata     }
2087223383edSEnrico Granata 
2088223383edSEnrico Granata     ~CommandObjectCommandsScriptClear ()
2089223383edSEnrico Granata     {
2090223383edSEnrico Granata     }
2091223383edSEnrico Granata 
20925a988416SJim Ingham protected:
2093223383edSEnrico Granata     bool
20945a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
2095223383edSEnrico Granata     {
2096223383edSEnrico Granata 
2097223383edSEnrico Granata         m_interpreter.RemoveAllUser();
2098223383edSEnrico Granata 
2099223383edSEnrico Granata         result.SetStatus (eReturnStatusSuccessFinishResult);
2100223383edSEnrico Granata 
2101223383edSEnrico Granata         return true;
2102223383edSEnrico Granata     }
2103223383edSEnrico Granata };
2104223383edSEnrico Granata 
2105223383edSEnrico Granata //-------------------------------------------------------------------------
2106223383edSEnrico Granata // CommandObjectCommandsScriptDelete
2107223383edSEnrico Granata //-------------------------------------------------------------------------
2108223383edSEnrico Granata 
21095a988416SJim Ingham class CommandObjectCommandsScriptDelete : public CommandObjectParsed
2110223383edSEnrico Granata {
2111223383edSEnrico Granata public:
2112223383edSEnrico Granata     CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter) :
21135a988416SJim Ingham         CommandObjectParsed (interpreter,
2114223383edSEnrico Granata                              "command script delete",
2115223383edSEnrico Granata                              "Delete a scripted command.",
2116223383edSEnrico Granata                              NULL)
2117223383edSEnrico Granata     {
2118223383edSEnrico Granata         CommandArgumentEntry arg1;
2119223383edSEnrico Granata         CommandArgumentData cmd_arg;
2120223383edSEnrico Granata 
2121223383edSEnrico Granata         // Define the first (and only) variant of this arg.
2122223383edSEnrico Granata         cmd_arg.arg_type = eArgTypeCommandName;
2123223383edSEnrico Granata         cmd_arg.arg_repetition = eArgRepeatPlain;
2124223383edSEnrico Granata 
2125223383edSEnrico Granata         // There is only one variant this argument could be; put it into the argument entry.
2126223383edSEnrico Granata         arg1.push_back (cmd_arg);
2127223383edSEnrico Granata 
2128223383edSEnrico Granata         // Push the data for the first argument into the m_arguments vector.
2129223383edSEnrico Granata         m_arguments.push_back (arg1);
2130223383edSEnrico Granata     }
2131223383edSEnrico Granata 
2132223383edSEnrico Granata     ~CommandObjectCommandsScriptDelete ()
2133223383edSEnrico Granata     {
2134223383edSEnrico Granata     }
2135223383edSEnrico Granata 
21365a988416SJim Ingham protected:
2137223383edSEnrico Granata     bool
21385a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
2139223383edSEnrico Granata     {
2140223383edSEnrico Granata 
21415a988416SJim Ingham         size_t argc = command.GetArgumentCount();
2142223383edSEnrico Granata 
2143223383edSEnrico Granata         if (argc != 1)
2144223383edSEnrico Granata         {
2145223383edSEnrico Granata             result.AppendError ("'command script delete' requires one argument");
2146223383edSEnrico Granata             result.SetStatus (eReturnStatusFailed);
2147223383edSEnrico Granata             return false;
2148223383edSEnrico Granata         }
2149223383edSEnrico Granata 
21505a988416SJim Ingham         const char* cmd_name = command.GetArgumentAtIndex(0);
2151223383edSEnrico Granata 
2152223383edSEnrico Granata         if (cmd_name && *cmd_name && m_interpreter.HasUserCommands() && m_interpreter.UserCommandExists(cmd_name))
2153223383edSEnrico Granata         {
2154223383edSEnrico Granata             m_interpreter.RemoveUser(cmd_name);
2155223383edSEnrico Granata             result.SetStatus (eReturnStatusSuccessFinishResult);
2156223383edSEnrico Granata         }
2157223383edSEnrico Granata         else
2158223383edSEnrico Granata         {
2159223383edSEnrico Granata             result.AppendErrorWithFormat ("command %s not found", cmd_name);
2160223383edSEnrico Granata             result.SetStatus (eReturnStatusFailed);
2161223383edSEnrico Granata         }
2162223383edSEnrico Granata 
2163223383edSEnrico Granata         return result.Succeeded();
2164223383edSEnrico Granata 
2165223383edSEnrico Granata     }
2166223383edSEnrico Granata };
2167223383edSEnrico Granata 
2168223383edSEnrico Granata #pragma mark CommandObjectMultiwordCommandsScript
2169223383edSEnrico Granata 
2170223383edSEnrico Granata //-------------------------------------------------------------------------
2171223383edSEnrico Granata // CommandObjectMultiwordCommandsScript
2172223383edSEnrico Granata //-------------------------------------------------------------------------
2173223383edSEnrico Granata 
2174223383edSEnrico Granata class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword
2175223383edSEnrico Granata {
2176223383edSEnrico Granata public:
2177223383edSEnrico Granata     CommandObjectMultiwordCommandsScript (CommandInterpreter &interpreter) :
2178223383edSEnrico Granata     CommandObjectMultiword (interpreter,
2179223383edSEnrico Granata                             "command script",
2180223383edSEnrico Granata                             "A set of commands for managing or customizing script commands.",
2181223383edSEnrico Granata                             "command script <subcommand> [<subcommand-options>]")
2182223383edSEnrico Granata     {
2183223383edSEnrico Granata         LoadSubCommand ("add",    CommandObjectSP (new CommandObjectCommandsScriptAdd (interpreter)));
2184223383edSEnrico Granata         LoadSubCommand ("delete", CommandObjectSP (new CommandObjectCommandsScriptDelete (interpreter)));
2185223383edSEnrico Granata         LoadSubCommand ("clear",  CommandObjectSP (new CommandObjectCommandsScriptClear (interpreter)));
2186223383edSEnrico Granata         LoadSubCommand ("list",   CommandObjectSP (new CommandObjectCommandsScriptList (interpreter)));
2187a9dbf432SEnrico Granata         LoadSubCommand ("import", CommandObjectSP (new CommandObjectCommandsScriptImport (interpreter)));
2188223383edSEnrico Granata     }
2189223383edSEnrico Granata 
2190223383edSEnrico Granata     ~CommandObjectMultiwordCommandsScript ()
2191223383edSEnrico Granata     {
2192223383edSEnrico Granata     }
2193223383edSEnrico Granata 
2194223383edSEnrico Granata };
2195223383edSEnrico Granata 
2196223383edSEnrico Granata 
2197ebc09c36SJim Ingham #pragma mark CommandObjectMultiwordCommands
2198ebc09c36SJim Ingham 
2199ebc09c36SJim Ingham //-------------------------------------------------------------------------
2200ebc09c36SJim Ingham // CommandObjectMultiwordCommands
2201ebc09c36SJim Ingham //-------------------------------------------------------------------------
2202ebc09c36SJim Ingham 
2203ebc09c36SJim Ingham CommandObjectMultiwordCommands::CommandObjectMultiwordCommands (CommandInterpreter &interpreter) :
2204a7015092SGreg Clayton     CommandObjectMultiword (interpreter,
22050e5e5a79SGreg Clayton                             "command",
22063f4c09c1SCaroline Tice                             "A set of commands for managing or customizing the debugger commands.",
22070e5e5a79SGreg Clayton                             "command <subcommand> [<subcommand-options>]")
2208ebc09c36SJim Ingham {
2209a7015092SGreg Clayton     LoadSubCommand ("source",  CommandObjectSP (new CommandObjectCommandsSource (interpreter)));
2210a7015092SGreg Clayton     LoadSubCommand ("alias",   CommandObjectSP (new CommandObjectCommandsAlias (interpreter)));
2211a7015092SGreg Clayton     LoadSubCommand ("unalias", CommandObjectSP (new CommandObjectCommandsUnalias (interpreter)));
2212b547278cSGreg Clayton     LoadSubCommand ("delete",  CommandObjectSP (new CommandObjectCommandsDelete (interpreter)));
2213de164aaaSGreg Clayton     LoadSubCommand ("regex",   CommandObjectSP (new CommandObjectCommandsAddRegex (interpreter)));
2214a5a97ebeSJim Ingham     LoadSubCommand ("history", CommandObjectSP (new CommandObjectCommandsHistory (interpreter)));
2215223383edSEnrico Granata     LoadSubCommand ("script",  CommandObjectSP (new CommandObjectMultiwordCommandsScript (interpreter)));
2216ebc09c36SJim Ingham }
2217ebc09c36SJim Ingham 
2218ebc09c36SJim Ingham CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands ()
2219ebc09c36SJim Ingham {
2220ebc09c36SJim Ingham }
2221ebc09c36SJim Ingham 
2222