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':
887594f14fSEnrico Granata                     error = m_count.SetValueFromCString(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
977594f14fSEnrico Granata                         error = m_start_idx.SetValueFromCString(option_arg,eVarSetOperationAssign);
987594f14fSEnrico Granata                     break;
997594f14fSEnrico Granata                 case 'e':
1007594f14fSEnrico Granata                     error = m_stop_idx.SetValueFromCString(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 {
229e2607b50SVirgile Bello { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, NULL, 0, eArgTypeUnsignedInteger,        "How many history commands to print."},
230e2607b50SVirgile Bello { LLDB_OPT_SET_1, false, "start-index", 's', OptionParser::eRequiredArgument, NULL, 0, eArgTypeUnsignedInteger,  "Index at which to start printing history commands (or end to mean tail mode)."},
231e2607b50SVirgile Bello { LLDB_OPT_SET_1, false, "end-index", 'e', OptionParser::eRequiredArgument, NULL, 0, eArgTypeUnsignedInteger,    "Index at which to stop printing history commands."},
232e2607b50SVirgile Bello { LLDB_OPT_SET_2, false, "clear", 'C', OptionParser::eNoArgument, NULL, 0, eArgTypeBoolean,    "Clears the current command history."},
233a5a97ebeSJim Ingham { 0, false, NULL, 0, 0, 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),
311*340b0309SGreg Clayton             m_stop_on_error (true),
312*340b0309SGreg Clayton             m_silent_run (false),
313*340b0309SGreg 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':
32915571f15SEnrico Granata                     error = m_stop_on_error.SetValueFromCString(option_arg);
330e16c50a1SJim Ingham                     break;
331*340b0309SGreg Clayton 
332e16c50a1SJim Ingham                 case 'c':
333*340b0309SGreg Clayton                     error = m_stop_on_continue.SetValueFromCString(option_arg);
334e16c50a1SJim Ingham                     break;
335*340b0309SGreg Clayton 
33660986174SMichael Sartain                 case 's':
337*340b0309SGreg Clayton                     error = m_silent_run.SetValueFromCString(option_arg);
33860986174SMichael Sartain                     break;
339*340b0309SGreg 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();
352*340b0309SGreg Clayton             m_silent_run.Clear();
353*340b0309SGreg 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;
369*340b0309SGreg Clayton 	    OptionValueBoolean m_silent_run;
370*340b0309SGreg 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 
381ebc09c36SJim Ingham             result.AppendMessageWithFormat ("Executing commands in '%s'.\n", filename);
382ebc09c36SJim Ingham 
3831ee3853fSJohnny Chen             FileSpec cmd_file (filename, true);
384e16c50a1SJim Ingham             ExecutionContext *exe_ctx = NULL;  // Just use the default context.
385ebc09c36SJim Ingham 
386*340b0309SGreg Clayton             // If any options were set, then use them
387*340b0309SGreg Clayton             if (m_options.m_stop_on_error.OptionWasSet()    ||
388*340b0309SGreg Clayton                 m_options.m_silent_run.OptionWasSet()       ||
389*340b0309SGreg Clayton                 m_options.m_stop_on_continue.OptionWasSet())
390*340b0309SGreg Clayton             {
391*340b0309SGreg Clayton                 // Use user set settings
392*340b0309SGreg Clayton                 LazyBool print_command = m_options.m_silent_run.GetCurrentValue() ? eLazyBoolNo : eLazyBoolYes;
393e16c50a1SJim Ingham                 m_interpreter.HandleCommandsFromFile (cmd_file,
394e16c50a1SJim Ingham                                                       exe_ctx,
395*340b0309SGreg Clayton                                                       m_options.m_stop_on_continue.GetCurrentValue() ? eLazyBoolYes : eLazyBoolNo, // Stop on continue
396*340b0309SGreg Clayton                                                       m_options.m_stop_on_error.GetCurrentValue() ? eLazyBoolYes : eLazyBoolNo, // Stop on error
397*340b0309SGreg Clayton                                                       print_command,        // Echo command
398*340b0309SGreg Clayton                                                       print_command,        // Print command output
399*340b0309SGreg Clayton                                                       eLazyBoolCalculate,   // Add to history
400e16c50a1SJim Ingham                                                       result);
401*340b0309SGreg Clayton 
402*340b0309SGreg Clayton             }
403*340b0309SGreg Clayton             else
404*340b0309SGreg Clayton             {
405*340b0309SGreg Clayton                 // No options were set, inherit any settings from nested "command source" commands,
406*340b0309SGreg Clayton                 // or set to sane default settings...
407*340b0309SGreg Clayton                 m_interpreter.HandleCommandsFromFile (cmd_file,
408*340b0309SGreg Clayton                                                       exe_ctx,
409*340b0309SGreg Clayton                                                       eLazyBoolCalculate, // Stop on continue
410*340b0309SGreg Clayton                                                       eLazyBoolCalculate, // Stop on error
411*340b0309SGreg Clayton                                                       eLazyBoolCalculate, // Echo command
412*340b0309SGreg Clayton                                                       eLazyBoolCalculate, // Print command output
413*340b0309SGreg Clayton                                                       eLazyBoolCalculate, // Add to history
414*340b0309SGreg Clayton                                                       result);
415*340b0309SGreg Clayton 
416*340b0309SGreg Clayton             }
417ebc09c36SJim Ingham         }
418ebc09c36SJim Ingham         else
419ebc09c36SJim Ingham         {
420ebc09c36SJim Ingham             result.AppendErrorWithFormat("'%s' takes exactly one executable filename argument.\n", GetCommandName());
421ebc09c36SJim Ingham             result.SetStatus (eReturnStatusFailed);
422ebc09c36SJim Ingham         }
423ebc09c36SJim Ingham         return result.Succeeded();
424ebc09c36SJim Ingham 
425ebc09c36SJim Ingham     }
4265a988416SJim Ingham     CommandOptions m_options;
427ebc09c36SJim Ingham };
428ebc09c36SJim Ingham 
429e0d378b3SGreg Clayton OptionDefinition
430e16c50a1SJim Ingham CommandObjectCommandsSource::CommandOptions::g_option_table[] =
431e16c50a1SJim Ingham {
432e2607b50SVirgile Bello { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean,    "If true, stop executing commands on error."},
433e2607b50SVirgile Bello { LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean, "If true, stop executing commands on continue."},
434e2607b50SVirgile Bello { LLDB_OPT_SET_ALL, false, "silent-run", 's', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean, "If true don't echo commands while executing."},
435e16c50a1SJim Ingham { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
436e16c50a1SJim Ingham };
437e16c50a1SJim Ingham 
438ebc09c36SJim Ingham #pragma mark CommandObjectCommandsAlias
439ebc09c36SJim Ingham //-------------------------------------------------------------------------
440ebc09c36SJim Ingham // CommandObjectCommandsAlias
441ebc09c36SJim Ingham //-------------------------------------------------------------------------
442ebc09c36SJim Ingham 
443be93a35aSEnrico Granata static const char *g_python_command_instructions =   "Enter your Python command(s). Type 'DONE' to end.\n"
444be93a35aSEnrico Granata                                                      "You must define a Python function with this signature:\n"
44544d93782SGreg Clayton                                                      "def my_command_impl(debugger, args, result, internal_dict):\n";
446be93a35aSEnrico Granata 
447be93a35aSEnrico Granata 
4485a988416SJim Ingham class CommandObjectCommandsAlias : public CommandObjectRaw
449ebc09c36SJim Ingham {
450be93a35aSEnrico Granata 
451be93a35aSEnrico Granata 
452ebc09c36SJim Ingham public:
453a7015092SGreg Clayton     CommandObjectCommandsAlias (CommandInterpreter &interpreter) :
4545a988416SJim Ingham         CommandObjectRaw (interpreter,
4550e5e5a79SGreg Clayton                        "command alias",
456e3d26315SCaroline Tice                        "Allow users to define their own debugger command abbreviations.",
457405fe67fSCaroline Tice                        NULL)
458ebc09c36SJim Ingham     {
459ebc09c36SJim Ingham         SetHelpLong(
460ebc09c36SJim Ingham     "'alias' allows the user to create a short-cut or abbreviation for long \n\
461ebc09c36SJim Ingham     commands, multi-word commands, and commands that take particular options. \n\
462ebc09c36SJim Ingham     Below are some simple examples of how one might use the 'alias' command: \n\
46369c12ccbSJason Molenda     \n    'command alias sc script'            // Creates the abbreviation 'sc' for the 'script' \n\
464ebc09c36SJim Ingham                                          // command. \n\
46569c12ccbSJason Molenda     'command alias bp breakpoint'        // Creates the abbreviation 'bp' for the 'breakpoint' \n\
466ebc09c36SJim Ingham                                          // command.  Since breakpoint commands are two-word \n\
467ebc09c36SJim Ingham                                          // commands, the user will still need to enter the \n\
468ebc09c36SJim Ingham                                          // second word after 'bp', e.g. 'bp enable' or \n\
469ebc09c36SJim Ingham                                          // 'bp delete'. \n\
47069c12ccbSJason Molenda     'command alias bpl breakpoint list'  // Creates the abbreviation 'bpl' for the \n\
471ebc09c36SJim Ingham                                          // two-word command 'breakpoint list'. \n\
472ebc09c36SJim Ingham     \nAn alias can include some options for the command, with the values either \n\
473ebc09c36SJim Ingham     filled in at the time the alias is created, or specified as positional \n\
474ebc09c36SJim Ingham     arguments, to be filled in when the alias is invoked.  The following example \n\
475ebc09c36SJim Ingham     shows how to create aliases with options: \n\
476ebc09c36SJim Ingham     \n\
47769c12ccbSJason Molenda     'command alias bfl breakpoint set -f %1 -l %2' \n\
478ebc09c36SJim Ingham     \nThis creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \n\
479ebc09c36SJim Ingham     options already part of the alias.  So if the user wants to set a breakpoint \n\
480ebc09c36SJim Ingham     by file and line without explicitly having to use the -f and -l options, the \n\
481ebc09c36SJim Ingham     user can now use 'bfl' instead.  The '%1' and '%2' are positional placeholders \n\
482ebc09c36SJim Ingham     for the actual arguments that will be passed when the alias command is used. \n\
483ebc09c36SJim Ingham     The number in the placeholder refers to the position/order the actual value \n\
48481ded935SJim Ingham     occupies when the alias is used.  All the occurrences of '%1' in the alias \n\
485ebc09c36SJim Ingham     will be replaced with the first argument, all the occurrences of '%2' in the \n\
486ebc09c36SJim Ingham     alias will be replaced with the second argument, and so on.  This also allows \n\
487ebc09c36SJim Ingham     actual arguments to be used multiple times within an alias (see 'process \n\
48881ded935SJim Ingham     launch' example below).  \n\
48981ded935SJim Ingham     Note: the positional arguments must substitute as whole words in the resultant\n\
49081ded935SJim Ingham     command, so you can't at present do something like:\n\
49181ded935SJim Ingham     \n\
49269c12ccbSJason Molenda     command alias bcppfl breakpoint set -f %1.cpp -l %2\n\
49381ded935SJim Ingham     \n\
49481ded935SJim Ingham     to get the file extension \".cpp\" automatically appended.  For more complex\n\
49581ded935SJim Ingham     aliasing, use the \"command regex\" command instead.\n\
49681ded935SJim Ingham     \nSo in the 'bfl' case, the actual file value will be \n\
497ebc09c36SJim Ingham     filled in with the first argument following 'bfl' and the actual line number \n\
498ebc09c36SJim Ingham     value will be filled in with the second argument.  The user would use this \n\
499ebc09c36SJim Ingham     alias as follows: \n\
50069c12ccbSJason Molenda     \n    (lldb)  command alias bfl breakpoint set -f %1 -l %2 \n\
501ebc09c36SJim Ingham     <... some time later ...> \n\
50209799af6SCaroline Tice     (lldb)  bfl my-file.c 137 \n\
503ebc09c36SJim Ingham     \nThis would be the same as if the user had entered \n\
504ebc09c36SJim Ingham     'breakpoint set -f my-file.c -l 137'. \n\
505ebc09c36SJim Ingham     \nAnother example: \n\
50669c12ccbSJason Molenda     \n    (lldb)  command alias pltty  process launch -s -o %1 -e %1 \n\
50709799af6SCaroline Tice     (lldb)  pltty /dev/tty0 \n\
508ebc09c36SJim Ingham            // becomes 'process launch -s -o /dev/tty0 -e /dev/tty0' \n\
509ebc09c36SJim Ingham     \nIf the user always wanted to pass the same value to a particular option, the \n\
510ebc09c36SJim Ingham     alias could be defined with that value directly in the alias as a constant, \n\
511ebc09c36SJim Ingham     rather than using a positional placeholder: \n\
51269c12ccbSJason Molenda     \n    command alias bl3  breakpoint set -f %1 -l 3  // Always sets a breakpoint on line \n\
513ebc09c36SJim Ingham                                                    // 3 of whatever file is indicated. \n");
514ebc09c36SJim Ingham 
515405fe67fSCaroline Tice         CommandArgumentEntry arg1;
516405fe67fSCaroline Tice         CommandArgumentEntry arg2;
517405fe67fSCaroline Tice         CommandArgumentEntry arg3;
518405fe67fSCaroline Tice         CommandArgumentData alias_arg;
519405fe67fSCaroline Tice         CommandArgumentData cmd_arg;
520405fe67fSCaroline Tice         CommandArgumentData options_arg;
521405fe67fSCaroline Tice 
522405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
523405fe67fSCaroline Tice         alias_arg.arg_type = eArgTypeAliasName;
524405fe67fSCaroline Tice         alias_arg.arg_repetition = eArgRepeatPlain;
525405fe67fSCaroline Tice 
526405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
527405fe67fSCaroline Tice         arg1.push_back (alias_arg);
528405fe67fSCaroline Tice 
529405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
530405fe67fSCaroline Tice         cmd_arg.arg_type = eArgTypeCommandName;
531405fe67fSCaroline Tice         cmd_arg.arg_repetition = eArgRepeatPlain;
532405fe67fSCaroline Tice 
533405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
534405fe67fSCaroline Tice         arg2.push_back (cmd_arg);
535405fe67fSCaroline Tice 
536405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
537405fe67fSCaroline Tice         options_arg.arg_type = eArgTypeAliasOptions;
538405fe67fSCaroline Tice         options_arg.arg_repetition = eArgRepeatOptional;
539405fe67fSCaroline Tice 
540405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
541405fe67fSCaroline Tice         arg3.push_back (options_arg);
542405fe67fSCaroline Tice 
543405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
544405fe67fSCaroline Tice         m_arguments.push_back (arg1);
545405fe67fSCaroline Tice         m_arguments.push_back (arg2);
546405fe67fSCaroline Tice         m_arguments.push_back (arg3);
547ebc09c36SJim Ingham     }
548ebc09c36SJim Ingham 
549ebc09c36SJim Ingham     ~CommandObjectCommandsAlias ()
550ebc09c36SJim Ingham     {
551ebc09c36SJim Ingham     }
552ebc09c36SJim Ingham 
5535a988416SJim Ingham protected:
5545a988416SJim Ingham     virtual bool
5555a988416SJim Ingham     DoExecute (const char *raw_command_line, CommandReturnObject &result)
556844d2303SCaroline Tice     {
557844d2303SCaroline Tice         Args args (raw_command_line);
558844d2303SCaroline Tice         std::string raw_command_string (raw_command_line);
559844d2303SCaroline Tice 
560844d2303SCaroline Tice         size_t argc = args.GetArgumentCount();
561844d2303SCaroline Tice 
562844d2303SCaroline Tice         if (argc < 2)
563844d2303SCaroline Tice         {
564844d2303SCaroline Tice             result.AppendError ("'alias' requires at least two arguments");
565844d2303SCaroline Tice             result.SetStatus (eReturnStatusFailed);
566844d2303SCaroline Tice             return false;
567844d2303SCaroline Tice         }
568844d2303SCaroline Tice 
569844d2303SCaroline Tice         // Get the alias command.
570844d2303SCaroline Tice 
571844d2303SCaroline Tice         const std::string alias_command = args.GetArgumentAtIndex (0);
572844d2303SCaroline Tice 
573844d2303SCaroline Tice         // Strip the new alias name off 'raw_command_string'  (leave it on args, which gets passed to 'Execute', which
574844d2303SCaroline Tice         // does the stripping itself.
575844d2303SCaroline Tice         size_t pos = raw_command_string.find (alias_command);
576844d2303SCaroline Tice         if (pos == 0)
577844d2303SCaroline Tice         {
578844d2303SCaroline Tice             raw_command_string = raw_command_string.substr (alias_command.size());
579844d2303SCaroline Tice             pos = raw_command_string.find_first_not_of (' ');
580844d2303SCaroline Tice             if ((pos != std::string::npos) && (pos > 0))
581844d2303SCaroline Tice                 raw_command_string = raw_command_string.substr (pos);
582844d2303SCaroline Tice         }
583844d2303SCaroline Tice         else
584844d2303SCaroline Tice         {
585844d2303SCaroline Tice             result.AppendError ("Error parsing command string.  No alias created.");
586844d2303SCaroline Tice             result.SetStatus (eReturnStatusFailed);
587844d2303SCaroline Tice             return false;
588844d2303SCaroline Tice         }
589844d2303SCaroline Tice 
590844d2303SCaroline Tice 
591844d2303SCaroline Tice         // Verify that the command is alias-able.
592844d2303SCaroline Tice         if (m_interpreter.CommandExists (alias_command.c_str()))
593844d2303SCaroline Tice         {
594844d2303SCaroline Tice             result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
595844d2303SCaroline Tice                                           alias_command.c_str());
596844d2303SCaroline Tice             result.SetStatus (eReturnStatusFailed);
597844d2303SCaroline Tice             return false;
598844d2303SCaroline Tice         }
599844d2303SCaroline Tice 
600844d2303SCaroline Tice         // Get CommandObject that is being aliased. The command name is read from the front of raw_command_string.
601844d2303SCaroline Tice         // raw_command_string is returned with the name of the command object stripped off the front.
602844d2303SCaroline Tice         CommandObject *cmd_obj = m_interpreter.GetCommandObjectForCommand (raw_command_string);
603844d2303SCaroline Tice 
604844d2303SCaroline Tice         if (!cmd_obj)
605844d2303SCaroline Tice         {
60686edbf41SGreg Clayton             result.AppendErrorWithFormat ("invalid command given to 'alias'. '%s' does not begin with a valid command."
607844d2303SCaroline Tice                                           "  No alias created.", raw_command_string.c_str());
608844d2303SCaroline Tice             result.SetStatus (eReturnStatusFailed);
609844d2303SCaroline Tice             return false;
610844d2303SCaroline Tice         }
611844d2303SCaroline Tice         else if (!cmd_obj->WantsRawCommandString ())
612844d2303SCaroline Tice         {
613844d2303SCaroline Tice             // Note that args was initialized with the original command, and has not been updated to this point.
614844d2303SCaroline Tice             // Therefore can we pass it to the version of Execute that does not need/expect raw input in the alias.
6155a988416SJim Ingham             return HandleAliasingNormalCommand (args, result);
616844d2303SCaroline Tice         }
617844d2303SCaroline Tice         else
618844d2303SCaroline Tice         {
6195a988416SJim Ingham             return HandleAliasingRawCommand (alias_command, raw_command_string, *cmd_obj, result);
6205a988416SJim Ingham         }
6215a988416SJim Ingham         return result.Succeeded();
6225a988416SJim Ingham     }
6235a988416SJim Ingham 
6245a988416SJim Ingham     bool
6255a988416SJim Ingham     HandleAliasingRawCommand (const std::string &alias_command, std::string &raw_command_string, CommandObject &cmd_obj, CommandReturnObject &result)
6265a988416SJim Ingham     {
627844d2303SCaroline Tice             // Verify & handle any options/arguments passed to the alias command
628844d2303SCaroline Tice 
629844d2303SCaroline Tice             OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector);
630844d2303SCaroline Tice             OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
631844d2303SCaroline Tice 
6325a988416SJim Ingham             CommandObjectSP cmd_obj_sp = m_interpreter.GetCommandSPExact (cmd_obj.GetCommandName(), false);
633844d2303SCaroline Tice 
634ca90c47eSCaroline Tice             if (!m_interpreter.ProcessAliasOptionsArgs (cmd_obj_sp, raw_command_string.c_str(), option_arg_vector_sp))
635844d2303SCaroline Tice             {
636844d2303SCaroline Tice                 result.AppendError ("Unable to create requested alias.\n");
637ca90c47eSCaroline Tice                 result.SetStatus (eReturnStatusFailed);
638844d2303SCaroline Tice                 return false;
639844d2303SCaroline Tice             }
640844d2303SCaroline Tice 
641844d2303SCaroline Tice             // Create the alias
642844d2303SCaroline Tice             if (m_interpreter.AliasExists (alias_command.c_str())
643844d2303SCaroline Tice                 || m_interpreter.UserCommandExists (alias_command.c_str()))
644844d2303SCaroline Tice             {
645844d2303SCaroline Tice                 OptionArgVectorSP temp_option_arg_sp (m_interpreter.GetAliasOptions (alias_command.c_str()));
646844d2303SCaroline Tice                 if (temp_option_arg_sp.get())
647844d2303SCaroline Tice                 {
648844d2303SCaroline Tice                     if (option_arg_vector->size() == 0)
649844d2303SCaroline Tice                         m_interpreter.RemoveAliasOptions (alias_command.c_str());
650844d2303SCaroline Tice                 }
651844d2303SCaroline Tice                 result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
652844d2303SCaroline Tice                                                 alias_command.c_str());
653844d2303SCaroline Tice             }
654844d2303SCaroline Tice 
655472362e6SCaroline Tice             if (cmd_obj_sp)
656472362e6SCaroline Tice             {
657844d2303SCaroline Tice                 m_interpreter.AddAlias (alias_command.c_str(), cmd_obj_sp);
658844d2303SCaroline Tice                 if (option_arg_vector->size() > 0)
659844d2303SCaroline Tice                     m_interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp);
660844d2303SCaroline Tice                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
661844d2303SCaroline Tice             }
662472362e6SCaroline Tice             else
663472362e6SCaroline Tice             {
664472362e6SCaroline Tice                 result.AppendError ("Unable to create requested alias.\n");
665472362e6SCaroline Tice                 result.SetStatus (eReturnStatusFailed);
666472362e6SCaroline Tice             }
667844d2303SCaroline Tice             return result.Succeeded ();
668844d2303SCaroline Tice     }
669ebc09c36SJim Ingham 
670ebc09c36SJim Ingham     bool
6715a988416SJim Ingham     HandleAliasingNormalCommand (Args& args, CommandReturnObject &result)
672ebc09c36SJim Ingham     {
673867b185dSCaroline Tice         size_t argc = args.GetArgumentCount();
674ebc09c36SJim Ingham 
675ebc09c36SJim Ingham         if (argc < 2)
676ebc09c36SJim Ingham         {
677ebc09c36SJim Ingham             result.AppendError ("'alias' requires at least two arguments");
678ebc09c36SJim Ingham             result.SetStatus (eReturnStatusFailed);
679ebc09c36SJim Ingham             return false;
680ebc09c36SJim Ingham         }
681ebc09c36SJim Ingham 
682ebc09c36SJim Ingham         const std::string alias_command = args.GetArgumentAtIndex(0);
683ebc09c36SJim Ingham         const std::string actual_command = args.GetArgumentAtIndex(1);
684ebc09c36SJim Ingham 
685ebc09c36SJim Ingham         args.Shift();  // Shift the alias command word off the argument vector.
686ebc09c36SJim Ingham         args.Shift();  // Shift the old command word off the argument vector.
687ebc09c36SJim Ingham 
688ebc09c36SJim Ingham         // Verify that the command is alias'able, and get the appropriate command object.
689ebc09c36SJim Ingham 
690a7015092SGreg Clayton         if (m_interpreter.CommandExists (alias_command.c_str()))
691ebc09c36SJim Ingham         {
692ebc09c36SJim Ingham             result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be redefined.\n",
693ebc09c36SJim Ingham                                          alias_command.c_str());
694ebc09c36SJim Ingham             result.SetStatus (eReturnStatusFailed);
695ebc09c36SJim Ingham         }
696ebc09c36SJim Ingham         else
697ebc09c36SJim Ingham         {
698a7015092SGreg Clayton              CommandObjectSP command_obj_sp(m_interpreter.GetCommandSPExact (actual_command.c_str(), true));
699ebc09c36SJim Ingham              CommandObjectSP subcommand_obj_sp;
700ebc09c36SJim Ingham              bool use_subcommand = false;
701ebc09c36SJim Ingham              if (command_obj_sp.get())
702ebc09c36SJim Ingham              {
703ebc09c36SJim Ingham                  CommandObject *cmd_obj = command_obj_sp.get();
704c982c768SGreg Clayton                  CommandObject *sub_cmd_obj = NULL;
705ebc09c36SJim Ingham                  OptionArgVectorSP option_arg_vector_sp = OptionArgVectorSP (new OptionArgVector);
706ebc09c36SJim Ingham                  OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
707ebc09c36SJim Ingham 
708844d2303SCaroline Tice                  while (cmd_obj->IsMultiwordObject() && args.GetArgumentCount() > 0)
709ebc09c36SJim Ingham                  {
710ebc09c36SJim Ingham                      if (argc >= 3)
711ebc09c36SJim Ingham                      {
712ebc09c36SJim Ingham                          const std::string sub_command = args.GetArgumentAtIndex(0);
713ebc09c36SJim Ingham                          assert (sub_command.length() != 0);
714998255bfSGreg Clayton                          subcommand_obj_sp = cmd_obj->GetSubcommandSP (sub_command.c_str());
715ebc09c36SJim Ingham                          if (subcommand_obj_sp.get())
716ebc09c36SJim Ingham                          {
717ebc09c36SJim Ingham                              sub_cmd_obj = subcommand_obj_sp.get();
718ebc09c36SJim Ingham                              use_subcommand = true;
719ebc09c36SJim Ingham                              args.Shift();  // Shift the sub_command word off the argument vector.
720844d2303SCaroline Tice                              cmd_obj = sub_cmd_obj;
721ebc09c36SJim Ingham                          }
722ebc09c36SJim Ingham                          else
723ebc09c36SJim Ingham                          {
724f415eeb4SCaroline Tice                              result.AppendErrorWithFormat("'%s' is not a valid sub-command of '%s'.  "
725f415eeb4SCaroline Tice                                                           "Unable to create alias.\n",
726f415eeb4SCaroline Tice                                                           sub_command.c_str(), actual_command.c_str());
727ebc09c36SJim Ingham                              result.SetStatus (eReturnStatusFailed);
728ebc09c36SJim Ingham                              return false;
729ebc09c36SJim Ingham                          }
730ebc09c36SJim Ingham                      }
731ebc09c36SJim Ingham                  }
732ebc09c36SJim Ingham 
733ebc09c36SJim Ingham                  // Verify & handle any options/arguments passed to the alias command
734ebc09c36SJim Ingham 
735ebc09c36SJim Ingham                  if (args.GetArgumentCount () > 0)
736ebc09c36SJim Ingham                  {
737ca90c47eSCaroline Tice                     CommandObjectSP tmp_sp = m_interpreter.GetCommandSPExact (cmd_obj->GetCommandName(), false);
738ebc09c36SJim Ingham                     if (use_subcommand)
739ca90c47eSCaroline Tice                         tmp_sp = m_interpreter.GetCommandSPExact (sub_cmd_obj->GetCommandName(), false);
740ca90c47eSCaroline Tice 
741ca90c47eSCaroline Tice                     std::string args_string;
742ca90c47eSCaroline Tice                     args.GetCommandString (args_string);
743ca90c47eSCaroline Tice 
744ca90c47eSCaroline Tice                     if (!m_interpreter.ProcessAliasOptionsArgs (tmp_sp, args_string.c_str(), option_arg_vector_sp))
745ebc09c36SJim Ingham                     {
746ca90c47eSCaroline Tice                         result.AppendError ("Unable to create requested alias.\n");
747ca90c47eSCaroline Tice                         result.SetStatus (eReturnStatusFailed);
748e7941795SCaroline Tice                         return false;
749867b185dSCaroline Tice                     }
750867b185dSCaroline Tice                  }
751867b185dSCaroline Tice 
752ebc09c36SJim Ingham                  // Create the alias.
753ebc09c36SJim Ingham 
754a7015092SGreg Clayton                  if (m_interpreter.AliasExists (alias_command.c_str())
755a7015092SGreg Clayton                      || m_interpreter.UserCommandExists (alias_command.c_str()))
756ebc09c36SJim Ingham                  {
757a7015092SGreg Clayton                      OptionArgVectorSP tmp_option_arg_sp (m_interpreter.GetAliasOptions (alias_command.c_str()));
758ebc09c36SJim Ingham                      if (tmp_option_arg_sp.get())
759ebc09c36SJim Ingham                      {
760ebc09c36SJim Ingham                          if (option_arg_vector->size() == 0)
761a7015092SGreg Clayton                              m_interpreter.RemoveAliasOptions (alias_command.c_str());
762ebc09c36SJim Ingham                      }
763ebc09c36SJim Ingham                      result.AppendWarningWithFormat ("Overwriting existing definition for '%s'.\n",
764ebc09c36SJim Ingham                                                      alias_command.c_str());
765ebc09c36SJim Ingham                  }
766ebc09c36SJim Ingham 
767ebc09c36SJim Ingham                  if (use_subcommand)
768a7015092SGreg Clayton                      m_interpreter.AddAlias (alias_command.c_str(), subcommand_obj_sp);
769ebc09c36SJim Ingham                  else
770a7015092SGreg Clayton                      m_interpreter.AddAlias (alias_command.c_str(), command_obj_sp);
771ebc09c36SJim Ingham                  if (option_arg_vector->size() > 0)
772a7015092SGreg Clayton                      m_interpreter.AddOrReplaceAliasOptions (alias_command.c_str(), option_arg_vector_sp);
773ebc09c36SJim Ingham                  result.SetStatus (eReturnStatusSuccessFinishNoResult);
774ebc09c36SJim Ingham              }
775ebc09c36SJim Ingham              else
776ebc09c36SJim Ingham              {
777ebc09c36SJim Ingham                  result.AppendErrorWithFormat ("'%s' is not an existing command.\n", actual_command.c_str());
778ebc09c36SJim Ingham                  result.SetStatus (eReturnStatusFailed);
779e7941795SCaroline Tice                  return false;
780ebc09c36SJim Ingham              }
781ebc09c36SJim Ingham         }
782ebc09c36SJim Ingham 
783ebc09c36SJim Ingham         return result.Succeeded();
784ebc09c36SJim Ingham     }
7855a988416SJim Ingham 
786ebc09c36SJim Ingham };
787ebc09c36SJim Ingham 
788ebc09c36SJim Ingham #pragma mark CommandObjectCommandsUnalias
789ebc09c36SJim Ingham //-------------------------------------------------------------------------
790ebc09c36SJim Ingham // CommandObjectCommandsUnalias
791ebc09c36SJim Ingham //-------------------------------------------------------------------------
792ebc09c36SJim Ingham 
7935a988416SJim Ingham class CommandObjectCommandsUnalias : public CommandObjectParsed
794ebc09c36SJim Ingham {
795ebc09c36SJim Ingham public:
796a7015092SGreg Clayton     CommandObjectCommandsUnalias (CommandInterpreter &interpreter) :
7975a988416SJim Ingham         CommandObjectParsed (interpreter,
7980e5e5a79SGreg Clayton                        "command unalias",
79986ddae50SCaroline Tice                        "Allow the user to remove/delete a user-defined command abbreviation.",
800405fe67fSCaroline Tice                        NULL)
801ebc09c36SJim Ingham     {
802405fe67fSCaroline Tice         CommandArgumentEntry arg;
803405fe67fSCaroline Tice         CommandArgumentData alias_arg;
804405fe67fSCaroline Tice 
805405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
806405fe67fSCaroline Tice         alias_arg.arg_type = eArgTypeAliasName;
807405fe67fSCaroline Tice         alias_arg.arg_repetition = eArgRepeatPlain;
808405fe67fSCaroline Tice 
809405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
810405fe67fSCaroline Tice         arg.push_back (alias_arg);
811405fe67fSCaroline Tice 
812405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
813405fe67fSCaroline Tice         m_arguments.push_back (arg);
814ebc09c36SJim Ingham     }
815ebc09c36SJim Ingham 
816ebc09c36SJim Ingham     ~CommandObjectCommandsUnalias()
817ebc09c36SJim Ingham     {
818ebc09c36SJim Ingham     }
819ebc09c36SJim Ingham 
8205a988416SJim Ingham protected:
821ebc09c36SJim Ingham     bool
8225a988416SJim Ingham     DoExecute (Args& args, CommandReturnObject &result)
823ebc09c36SJim Ingham     {
824ebc09c36SJim Ingham         CommandObject::CommandMap::iterator pos;
825ebc09c36SJim Ingham         CommandObject *cmd_obj;
826ebc09c36SJim Ingham 
827ebc09c36SJim Ingham         if (args.GetArgumentCount() != 0)
828ebc09c36SJim Ingham         {
829ebc09c36SJim Ingham             const char *command_name = args.GetArgumentAtIndex(0);
830a7015092SGreg Clayton             cmd_obj = m_interpreter.GetCommandObject(command_name);
831ebc09c36SJim Ingham             if (cmd_obj)
832ebc09c36SJim Ingham             {
833a7015092SGreg Clayton                 if (m_interpreter.CommandExists (command_name))
834ebc09c36SJim Ingham                 {
835ebc09c36SJim Ingham                     result.AppendErrorWithFormat ("'%s' is a permanent debugger command and cannot be removed.\n",
836ebc09c36SJim Ingham                                                   command_name);
837ebc09c36SJim Ingham                     result.SetStatus (eReturnStatusFailed);
838ebc09c36SJim Ingham                 }
839ebc09c36SJim Ingham                 else
840ebc09c36SJim Ingham                 {
841ebc09c36SJim Ingham 
842a7015092SGreg Clayton                     if (m_interpreter.RemoveAlias (command_name) == false)
843ebc09c36SJim Ingham                     {
844a7015092SGreg Clayton                         if (m_interpreter.AliasExists (command_name))
845ebc09c36SJim Ingham                             result.AppendErrorWithFormat ("Error occurred while attempting to unalias '%s'.\n",
846ebc09c36SJim Ingham                                                           command_name);
847ebc09c36SJim Ingham                         else
848ebc09c36SJim Ingham                             result.AppendErrorWithFormat ("'%s' is not an existing alias.\n", command_name);
849ebc09c36SJim Ingham                         result.SetStatus (eReturnStatusFailed);
850ebc09c36SJim Ingham                     }
851ebc09c36SJim Ingham                     else
852ebc09c36SJim Ingham                         result.SetStatus (eReturnStatusSuccessFinishNoResult);
853ebc09c36SJim Ingham                 }
854ebc09c36SJim Ingham             }
855ebc09c36SJim Ingham             else
856ebc09c36SJim Ingham             {
857ebc09c36SJim Ingham                 result.AppendErrorWithFormat ("'%s' is not a known command.\nTry 'help' to see a "
858ebc09c36SJim Ingham                                               "current list of commands.\n",
859ebc09c36SJim Ingham                                              command_name);
860ebc09c36SJim Ingham                 result.SetStatus (eReturnStatusFailed);
861ebc09c36SJim Ingham             }
862ebc09c36SJim Ingham         }
863ebc09c36SJim Ingham         else
864ebc09c36SJim Ingham         {
865ebc09c36SJim Ingham             result.AppendError ("must call 'unalias' with a valid alias");
866ebc09c36SJim Ingham             result.SetStatus (eReturnStatusFailed);
867ebc09c36SJim Ingham         }
868ebc09c36SJim Ingham 
869ebc09c36SJim Ingham         return result.Succeeded();
870ebc09c36SJim Ingham     }
871ebc09c36SJim Ingham };
872ebc09c36SJim Ingham 
873de164aaaSGreg Clayton //-------------------------------------------------------------------------
874de164aaaSGreg Clayton // CommandObjectCommandsAddRegex
875de164aaaSGreg Clayton //-------------------------------------------------------------------------
8765a988416SJim Ingham #pragma mark CommandObjectCommandsAddRegex
877de164aaaSGreg Clayton 
87844d93782SGreg Clayton class CommandObjectCommandsAddRegex :
87944d93782SGreg Clayton     public CommandObjectParsed,
88044d93782SGreg Clayton     public IOHandlerDelegate
881de164aaaSGreg Clayton {
882de164aaaSGreg Clayton public:
883de164aaaSGreg Clayton     CommandObjectCommandsAddRegex (CommandInterpreter &interpreter) :
8845a988416SJim Ingham         CommandObjectParsed (interpreter,
8850e5e5a79SGreg Clayton                        "command regex",
886de164aaaSGreg Clayton                        "Allow the user to create a regular expression command.",
8870e5e5a79SGreg Clayton                        "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
88844d93782SGreg Clayton         IOHandlerDelegate(IOHandlerDelegate::Completion::LLDBCommand),
889de164aaaSGreg Clayton         m_options (interpreter)
890de164aaaSGreg Clayton     {
8910e5e5a79SGreg Clayton         SetHelpLong(
8920e5e5a79SGreg Clayton "This command allows the user to create powerful regular expression commands\n"
8930e5e5a79SGreg Clayton "with substitutions. The regular expressions and substitutions are specified\n"
8940e5e5a79SGreg Clayton "using the regular exression substitution format of:\n"
8950e5e5a79SGreg Clayton "\n"
8960e5e5a79SGreg Clayton "    s/<regex>/<subst>/\n"
8970e5e5a79SGreg Clayton "\n"
8980e5e5a79SGreg Clayton "<regex> is a regular expression that can use parenthesis to capture regular\n"
8990e5e5a79SGreg Clayton "expression input and substitute the captured matches in the output using %1\n"
9000e5e5a79SGreg Clayton "for the first match, %2 for the second, and so on.\n"
9010e5e5a79SGreg Clayton "\n"
9020e5e5a79SGreg Clayton "The regular expressions can all be specified on the command line if more than\n"
9030e5e5a79SGreg Clayton "one argument is provided. If just the command name is provided on the command\n"
9040e5e5a79SGreg Clayton "line, then the regular expressions and substitutions can be entered on separate\n"
9050e5e5a79SGreg Clayton " lines, followed by an empty line to terminate the command definition.\n"
9060e5e5a79SGreg Clayton "\n"
9070e5e5a79SGreg Clayton "EXAMPLES\n"
9080e5e5a79SGreg Clayton "\n"
909adc43c99SSean Callanan "The following example will define a regular expression command named 'f' that\n"
9100e5e5a79SGreg Clayton "will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if\n"
9110e5e5a79SGreg Clayton "a number follows 'f':\n"
912adc43c99SSean Callanan "\n"
9130e5e5a79SGreg Clayton "    (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/'\n"
914adc43c99SSean Callanan "\n"
9150e5e5a79SGreg Clayton                     );
916de164aaaSGreg Clayton     }
917de164aaaSGreg Clayton 
918de164aaaSGreg Clayton     ~CommandObjectCommandsAddRegex()
919de164aaaSGreg Clayton     {
920de164aaaSGreg Clayton     }
921de164aaaSGreg Clayton 
922de164aaaSGreg Clayton 
9235a988416SJim Ingham protected:
92444d93782SGreg Clayton 
92544d93782SGreg Clayton     virtual void
92644d93782SGreg Clayton     IOHandlerActivated (IOHandler &io_handler)
92744d93782SGreg Clayton     {
92844d93782SGreg Clayton         StreamFileSP output_sp(io_handler.GetOutputStreamFile());
92944d93782SGreg Clayton         if (output_sp)
93044d93782SGreg Clayton         {
93144d93782SGreg 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");
93244d93782SGreg Clayton             output_sp->Flush();
93344d93782SGreg Clayton         }
93444d93782SGreg Clayton     }
93544d93782SGreg Clayton 
93644d93782SGreg Clayton     virtual void
93744d93782SGreg Clayton     IOHandlerInputComplete (IOHandler &io_handler, std::string &data)
93844d93782SGreg Clayton     {
93944d93782SGreg Clayton         io_handler.SetIsDone(true);
94044d93782SGreg Clayton         if (m_regex_cmd_ap.get())
94144d93782SGreg Clayton         {
94244d93782SGreg Clayton             StringList lines;
94344d93782SGreg Clayton             if (lines.SplitIntoLines (data))
94444d93782SGreg Clayton             {
94544d93782SGreg Clayton                 const size_t num_lines = lines.GetSize();
94644d93782SGreg Clayton                 bool check_only = false;
94744d93782SGreg Clayton                 for (size_t i=0; i<num_lines; ++i)
94844d93782SGreg Clayton                 {
94944d93782SGreg Clayton                     printf ("regex[%zu] = %s\n", i, lines[i].c_str());
95044d93782SGreg Clayton                     llvm::StringRef bytes_strref (lines[i]);
95144d93782SGreg Clayton                     Error error = AppendRegexSubstitution (bytes_strref, check_only);
95244d93782SGreg Clayton                     if (error.Fail())
95344d93782SGreg Clayton                     {
95444d93782SGreg Clayton                         if (!m_interpreter.GetDebugger().GetCommandInterpreter().GetBatchCommandMode())
95544d93782SGreg Clayton                         {
95644d93782SGreg Clayton                             StreamSP out_stream = m_interpreter.GetDebugger().GetAsyncOutputStream();
95744d93782SGreg Clayton                             out_stream->Printf("error: %s\n", error.AsCString());
95844d93782SGreg Clayton                         }
95944d93782SGreg Clayton                     }
96044d93782SGreg Clayton                 }
96144d93782SGreg Clayton             }
96244d93782SGreg Clayton             if (m_regex_cmd_ap->HasRegexEntries())
96344d93782SGreg Clayton             {
96444d93782SGreg Clayton                 CommandObjectSP cmd_sp (m_regex_cmd_ap.release());
96544d93782SGreg Clayton                 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
96644d93782SGreg Clayton             }
96744d93782SGreg Clayton         }
96844d93782SGreg Clayton     }
96944d93782SGreg Clayton 
97044d93782SGreg Clayton     virtual LineStatus
97144d93782SGreg Clayton     IOHandlerLinesUpdated (IOHandler &io_handler,
97244d93782SGreg Clayton                            StringList &lines,
97344d93782SGreg Clayton                            uint32_t line_idx,
97444d93782SGreg Clayton                            Error &error)
97544d93782SGreg Clayton     {
97644d93782SGreg Clayton         if (line_idx == UINT32_MAX)
97744d93782SGreg Clayton         {
97844d93782SGreg Clayton             // Return true to indicate we are done getting lines (this
97944d93782SGreg Clayton             // is a "fake" line - the real terminating blank line was
98044d93782SGreg Clayton             // removed during a previous call with the code below)
98144d93782SGreg Clayton             error.Clear();
98244d93782SGreg Clayton             return LineStatus::Done;
98344d93782SGreg Clayton         }
98444d93782SGreg Clayton         else
98544d93782SGreg Clayton         {
98644d93782SGreg Clayton             const size_t num_lines = lines.GetSize();
98744d93782SGreg Clayton             if (line_idx + 1 == num_lines)
98844d93782SGreg Clayton             {
98944d93782SGreg Clayton                 // The last line was edited, if this line is empty, then we are done
99044d93782SGreg Clayton                 // getting our multiple lines.
99144d93782SGreg Clayton                 if (lines[line_idx].empty())
99244d93782SGreg Clayton                 {
99344d93782SGreg Clayton                     // Remove the last empty line from "lines" so it doesn't appear
99444d93782SGreg Clayton                     // in our final expression and return true to indicate we are done
99544d93782SGreg Clayton                     // getting lines
99644d93782SGreg Clayton                     lines.PopBack();
99744d93782SGreg Clayton                     return LineStatus::Done;
99844d93782SGreg Clayton                 }
99944d93782SGreg Clayton             }
100044d93782SGreg Clayton             // Check the current line to make sure it is formatted correctly
100144d93782SGreg Clayton             bool check_only = true;
100244d93782SGreg Clayton             llvm::StringRef regex_sed(lines[line_idx]);
100344d93782SGreg Clayton             error = AppendRegexSubstitution (regex_sed, check_only);
100444d93782SGreg Clayton             if (error.Fail())
100544d93782SGreg Clayton             {
100644d93782SGreg Clayton                 return LineStatus::Error;
100744d93782SGreg Clayton             }
100844d93782SGreg Clayton             else
100944d93782SGreg Clayton             {
101044d93782SGreg Clayton                 return LineStatus::Success;
101144d93782SGreg Clayton             }
101244d93782SGreg Clayton         }
101344d93782SGreg Clayton     }
101444d93782SGreg Clayton 
1015de164aaaSGreg Clayton     bool
10165a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
1017de164aaaSGreg Clayton     {
10185a988416SJim Ingham         const size_t argc = command.GetArgumentCount();
10190e5e5a79SGreg Clayton         if (argc == 0)
1020de164aaaSGreg Clayton         {
102169c12ccbSJason Molenda             result.AppendError ("usage: 'command regex <command-name> [s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
10220e5e5a79SGreg Clayton             result.SetStatus (eReturnStatusFailed);
10230e5e5a79SGreg Clayton         }
10240e5e5a79SGreg Clayton         else
10250e5e5a79SGreg Clayton         {
10260e5e5a79SGreg Clayton             Error error;
10275a988416SJim Ingham             const char *name = command.GetArgumentAtIndex(0);
1028de164aaaSGreg Clayton             m_regex_cmd_ap.reset (new CommandObjectRegexCommand (m_interpreter,
1029de164aaaSGreg Clayton                                                                  name,
1030de164aaaSGreg Clayton                                                                  m_options.GetHelp (),
1031de164aaaSGreg Clayton                                                                  m_options.GetSyntax (),
1032de164aaaSGreg Clayton                                                                  10));
10330e5e5a79SGreg Clayton 
10340e5e5a79SGreg Clayton             if (argc == 1)
10350e5e5a79SGreg Clayton             {
103644d93782SGreg Clayton                 Debugger &debugger = m_interpreter.GetDebugger();
103744d93782SGreg Clayton                 const bool multiple_lines = true; // Get multiple lines
103844d93782SGreg Clayton                 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
103944d93782SGreg Clayton                                                                   "lldb",      // Name of input reader for history
104044d93782SGreg Clayton                                                                   "\033[K> ",  // Prompt and clear line
104144d93782SGreg Clayton                                                                   multiple_lines,
104244d93782SGreg Clayton                                                                   *this));
104344d93782SGreg Clayton 
104444d93782SGreg Clayton                 if (io_handler_sp)
1045de164aaaSGreg Clayton                 {
104644d93782SGreg Clayton                     debugger.PushIOHandler(io_handler_sp);
1047de164aaaSGreg Clayton                     result.SetStatus (eReturnStatusSuccessFinishNoResult);
1048de164aaaSGreg Clayton                 }
1049de164aaaSGreg Clayton             }
1050de164aaaSGreg Clayton             else
1051de164aaaSGreg Clayton             {
10520e5e5a79SGreg Clayton                 for (size_t arg_idx = 1; arg_idx < argc; ++arg_idx)
10530e5e5a79SGreg Clayton                 {
10545a988416SJim Ingham                     llvm::StringRef arg_strref (command.GetArgumentAtIndex(arg_idx));
105544d93782SGreg Clayton                     bool check_only = false;
105644d93782SGreg Clayton                     error = AppendRegexSubstitution (arg_strref, check_only);
10570e5e5a79SGreg Clayton                     if (error.Fail())
10580e5e5a79SGreg Clayton                         break;
10590e5e5a79SGreg Clayton                 }
10600e5e5a79SGreg Clayton 
10610e5e5a79SGreg Clayton                 if (error.Success())
10620e5e5a79SGreg Clayton                 {
10630e5e5a79SGreg Clayton                     AddRegexCommandToInterpreter();
10640e5e5a79SGreg Clayton                 }
10650e5e5a79SGreg Clayton             }
10660e5e5a79SGreg Clayton             if (error.Fail())
10670e5e5a79SGreg Clayton             {
10680e5e5a79SGreg Clayton                 result.AppendError (error.AsCString());
1069de164aaaSGreg Clayton                 result.SetStatus (eReturnStatusFailed);
1070de164aaaSGreg Clayton             }
10710e5e5a79SGreg Clayton         }
10720e5e5a79SGreg Clayton 
1073de164aaaSGreg Clayton         return result.Succeeded();
1074de164aaaSGreg Clayton     }
1075de164aaaSGreg Clayton 
10760e5e5a79SGreg Clayton     Error
107744d93782SGreg Clayton     AppendRegexSubstitution (const llvm::StringRef &regex_sed, bool check_only)
1078de164aaaSGreg Clayton     {
10790e5e5a79SGreg Clayton         Error error;
10800e5e5a79SGreg Clayton 
10810e5e5a79SGreg Clayton         if (m_regex_cmd_ap.get() == NULL)
1082de164aaaSGreg Clayton         {
10830e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("invalid regular expression command object for: '%.*s'",
10840e5e5a79SGreg Clayton                                            (int)regex_sed.size(),
10850e5e5a79SGreg Clayton                                            regex_sed.data());
10860e5e5a79SGreg Clayton             return error;
1087de164aaaSGreg Clayton         }
10880e5e5a79SGreg Clayton 
10890e5e5a79SGreg Clayton         size_t regex_sed_size = regex_sed.size();
10900e5e5a79SGreg Clayton 
10910e5e5a79SGreg Clayton         if (regex_sed_size <= 1)
10920e5e5a79SGreg Clayton         {
10930e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("regular expression substitution string is too short: '%.*s'",
10940e5e5a79SGreg Clayton                                            (int)regex_sed.size(),
10950e5e5a79SGreg Clayton                                            regex_sed.data());
10960e5e5a79SGreg Clayton             return error;
10970e5e5a79SGreg Clayton         }
10980e5e5a79SGreg Clayton 
10990e5e5a79SGreg Clayton         if (regex_sed[0] != 's')
11000e5e5a79SGreg Clayton         {
11010e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("regular expression substitution string doesn't start with 's': '%.*s'",
11020e5e5a79SGreg Clayton                                            (int)regex_sed.size(),
11030e5e5a79SGreg Clayton                                            regex_sed.data());
11040e5e5a79SGreg Clayton             return error;
11050e5e5a79SGreg Clayton         }
11060e5e5a79SGreg Clayton         const size_t first_separator_char_pos = 1;
11070e5e5a79SGreg Clayton         // use the char that follows 's' as the regex separator character
11080e5e5a79SGreg Clayton         // so we can have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
11090e5e5a79SGreg Clayton         const char separator_char = regex_sed[first_separator_char_pos];
11100e5e5a79SGreg Clayton         const size_t second_separator_char_pos = regex_sed.find (separator_char, first_separator_char_pos + 1);
11110e5e5a79SGreg Clayton 
11120e5e5a79SGreg Clayton         if (second_separator_char_pos == std::string::npos)
11130e5e5a79SGreg Clayton         {
11140e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("missing second '%c' separator char after '%.*s'",
11150e5e5a79SGreg Clayton                                            separator_char,
11160e5e5a79SGreg Clayton                                            (int)(regex_sed.size() - first_separator_char_pos - 1),
11170e5e5a79SGreg Clayton                                            regex_sed.data() + (first_separator_char_pos + 1));
11180e5e5a79SGreg Clayton             return error;
11190e5e5a79SGreg Clayton         }
11200e5e5a79SGreg Clayton 
11210e5e5a79SGreg Clayton         const size_t third_separator_char_pos = regex_sed.find (separator_char, second_separator_char_pos + 1);
11220e5e5a79SGreg Clayton 
11230e5e5a79SGreg Clayton         if (third_separator_char_pos == std::string::npos)
11240e5e5a79SGreg Clayton         {
11250e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("missing third '%c' separator char after '%.*s'",
11260e5e5a79SGreg Clayton                                            separator_char,
11270e5e5a79SGreg Clayton                                            (int)(regex_sed.size() - second_separator_char_pos - 1),
11280e5e5a79SGreg Clayton                                            regex_sed.data() + (second_separator_char_pos + 1));
11290e5e5a79SGreg Clayton             return error;
11300e5e5a79SGreg Clayton         }
11310e5e5a79SGreg Clayton 
11320e5e5a79SGreg Clayton         if (third_separator_char_pos != regex_sed_size - 1)
11330e5e5a79SGreg Clayton         {
11340e5e5a79SGreg Clayton             // Make sure that everything that follows the last regex
11350e5e5a79SGreg Clayton             // separator char
11360e5e5a79SGreg Clayton             if (regex_sed.find_first_not_of("\t\n\v\f\r ", third_separator_char_pos + 1) != std::string::npos)
11370e5e5a79SGreg Clayton             {
11380e5e5a79SGreg Clayton                 error.SetErrorStringWithFormat("extra data found after the '%.*s' regular expression substitution string: '%.*s'",
11390e5e5a79SGreg Clayton                                                (int)third_separator_char_pos + 1,
11400e5e5a79SGreg Clayton                                                regex_sed.data(),
11410e5e5a79SGreg Clayton                                                (int)(regex_sed.size() - third_separator_char_pos - 1),
11420e5e5a79SGreg Clayton                                                regex_sed.data() + (third_separator_char_pos + 1));
11430e5e5a79SGreg Clayton                 return error;
11440e5e5a79SGreg Clayton             }
11450e5e5a79SGreg Clayton 
11460e5e5a79SGreg Clayton         }
11470e5e5a79SGreg Clayton         else if (first_separator_char_pos + 1 == second_separator_char_pos)
11480e5e5a79SGreg Clayton         {
11490e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
11500e5e5a79SGreg Clayton                                            separator_char,
11510e5e5a79SGreg Clayton                                            separator_char,
11520e5e5a79SGreg Clayton                                            separator_char,
11530e5e5a79SGreg Clayton                                            (int)regex_sed.size(),
11540e5e5a79SGreg Clayton                                            regex_sed.data());
11550e5e5a79SGreg Clayton             return error;
11560e5e5a79SGreg Clayton         }
11570e5e5a79SGreg Clayton         else if (second_separator_char_pos + 1 == third_separator_char_pos)
11580e5e5a79SGreg Clayton         {
11590e5e5a79SGreg Clayton             error.SetErrorStringWithFormat("<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
11600e5e5a79SGreg Clayton                                            separator_char,
11610e5e5a79SGreg Clayton                                            separator_char,
11620e5e5a79SGreg Clayton                                            separator_char,
11630e5e5a79SGreg Clayton                                            (int)regex_sed.size(),
11640e5e5a79SGreg Clayton                                            regex_sed.data());
11650e5e5a79SGreg Clayton             return error;
11660e5e5a79SGreg Clayton         }
116744d93782SGreg Clayton 
116844d93782SGreg Clayton         if (check_only == false)
116944d93782SGreg Clayton         {
11700e5e5a79SGreg Clayton             std::string regex(regex_sed.substr(first_separator_char_pos + 1, second_separator_char_pos - first_separator_char_pos - 1));
11710e5e5a79SGreg Clayton             std::string subst(regex_sed.substr(second_separator_char_pos + 1, third_separator_char_pos - second_separator_char_pos - 1));
11720e5e5a79SGreg Clayton             m_regex_cmd_ap->AddRegexCommand (regex.c_str(),
11730e5e5a79SGreg Clayton                                              subst.c_str());
117444d93782SGreg Clayton         }
11750e5e5a79SGreg Clayton         return error;
1176de164aaaSGreg Clayton     }
1177de164aaaSGreg Clayton 
1178de164aaaSGreg Clayton     void
11790e5e5a79SGreg Clayton     AddRegexCommandToInterpreter()
1180de164aaaSGreg Clayton     {
1181de164aaaSGreg Clayton         if (m_regex_cmd_ap.get())
1182de164aaaSGreg Clayton         {
1183de164aaaSGreg Clayton             if (m_regex_cmd_ap->HasRegexEntries())
1184de164aaaSGreg Clayton             {
1185de164aaaSGreg Clayton                 CommandObjectSP cmd_sp (m_regex_cmd_ap.release());
1186de164aaaSGreg Clayton                 m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1187de164aaaSGreg Clayton             }
1188de164aaaSGreg Clayton         }
1189de164aaaSGreg Clayton     }
1190de164aaaSGreg Clayton 
1191de164aaaSGreg Clayton private:
11927b0992d9SGreg Clayton     std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_ap;
1193de164aaaSGreg Clayton 
1194de164aaaSGreg Clayton      class CommandOptions : public Options
1195de164aaaSGreg Clayton      {
1196de164aaaSGreg Clayton      public:
1197de164aaaSGreg Clayton 
1198de164aaaSGreg Clayton          CommandOptions (CommandInterpreter &interpreter) :
1199de164aaaSGreg Clayton             Options (interpreter)
1200de164aaaSGreg Clayton          {
1201de164aaaSGreg Clayton          }
1202de164aaaSGreg Clayton 
1203de164aaaSGreg Clayton          virtual
1204de164aaaSGreg Clayton          ~CommandOptions (){}
1205de164aaaSGreg Clayton 
1206de164aaaSGreg Clayton          virtual Error
1207de164aaaSGreg Clayton          SetOptionValue (uint32_t option_idx, const char *option_arg)
1208de164aaaSGreg Clayton          {
1209de164aaaSGreg Clayton              Error error;
12103bcdfc0eSGreg Clayton              const int short_option = m_getopt_table[option_idx].val;
1211de164aaaSGreg Clayton 
1212de164aaaSGreg Clayton              switch (short_option)
1213de164aaaSGreg Clayton              {
1214de164aaaSGreg Clayton                  case 'h':
1215de164aaaSGreg Clayton                      m_help.assign (option_arg);
1216de164aaaSGreg Clayton                      break;
1217de164aaaSGreg Clayton                  case 's':
1218de164aaaSGreg Clayton                      m_syntax.assign (option_arg);
1219de164aaaSGreg Clayton                      break;
1220de164aaaSGreg Clayton 
1221de164aaaSGreg Clayton                  default:
122286edbf41SGreg Clayton                      error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1223de164aaaSGreg Clayton                      break;
1224de164aaaSGreg Clayton              }
1225de164aaaSGreg Clayton 
1226de164aaaSGreg Clayton              return error;
1227de164aaaSGreg Clayton          }
1228de164aaaSGreg Clayton 
1229de164aaaSGreg Clayton          void
1230de164aaaSGreg Clayton          OptionParsingStarting ()
1231de164aaaSGreg Clayton          {
1232de164aaaSGreg Clayton              m_help.clear();
1233de164aaaSGreg Clayton              m_syntax.clear();
1234de164aaaSGreg Clayton          }
1235de164aaaSGreg Clayton 
1236de164aaaSGreg Clayton          const OptionDefinition*
1237de164aaaSGreg Clayton          GetDefinitions ()
1238de164aaaSGreg Clayton          {
1239de164aaaSGreg Clayton              return g_option_table;
1240de164aaaSGreg Clayton          }
1241de164aaaSGreg Clayton 
1242de164aaaSGreg Clayton          // Options table: Required for subclasses of Options.
1243de164aaaSGreg Clayton 
1244de164aaaSGreg Clayton          static OptionDefinition g_option_table[];
1245de164aaaSGreg Clayton 
1246de164aaaSGreg Clayton          const char *
1247de164aaaSGreg Clayton          GetHelp ()
1248de164aaaSGreg Clayton          {
1249de164aaaSGreg Clayton              if (m_help.empty())
1250de164aaaSGreg Clayton                  return NULL;
1251de164aaaSGreg Clayton              return m_help.c_str();
1252de164aaaSGreg Clayton          }
1253de164aaaSGreg Clayton          const char *
1254de164aaaSGreg Clayton          GetSyntax ()
1255de164aaaSGreg Clayton          {
1256de164aaaSGreg Clayton              if (m_syntax.empty())
1257de164aaaSGreg Clayton                  return NULL;
1258de164aaaSGreg Clayton              return m_syntax.c_str();
1259de164aaaSGreg Clayton          }
1260de164aaaSGreg Clayton          // Instance variables to hold the values for command options.
1261de164aaaSGreg Clayton      protected:
1262de164aaaSGreg Clayton          std::string m_help;
1263de164aaaSGreg Clayton          std::string m_syntax;
1264de164aaaSGreg Clayton      };
1265de164aaaSGreg Clayton 
1266de164aaaSGreg Clayton      virtual Options *
1267de164aaaSGreg Clayton      GetOptions ()
1268de164aaaSGreg Clayton      {
1269de164aaaSGreg Clayton          return &m_options;
1270de164aaaSGreg Clayton      }
1271de164aaaSGreg Clayton 
12725a988416SJim Ingham      CommandOptions m_options;
1273de164aaaSGreg Clayton };
1274de164aaaSGreg Clayton 
1275de164aaaSGreg Clayton OptionDefinition
1276de164aaaSGreg Clayton CommandObjectCommandsAddRegex::CommandOptions::g_option_table[] =
1277de164aaaSGreg Clayton {
1278e2607b50SVirgile Bello { LLDB_OPT_SET_1, false, "help"  , 'h', OptionParser::eRequiredArgument, NULL, 0, eArgTypeNone, "The help text to display for this command."},
1279e2607b50SVirgile Bello { LLDB_OPT_SET_1, false, "syntax", 's', OptionParser::eRequiredArgument, NULL, 0, eArgTypeNone, "A syntax string showing the typical usage syntax."},
1280de164aaaSGreg Clayton { 0             , false,  NULL   , 0  , 0                , NULL, 0, eArgTypeNone, NULL }
1281de164aaaSGreg Clayton };
1282de164aaaSGreg Clayton 
1283de164aaaSGreg Clayton 
12845a988416SJim Ingham class CommandObjectPythonFunction : public CommandObjectRaw
1285223383edSEnrico Granata {
1286223383edSEnrico Granata private:
1287223383edSEnrico Granata     std::string m_function_name;
12880a305db7SEnrico Granata     ScriptedCommandSynchronicity m_synchro;
1289fac939e9SEnrico Granata     bool m_fetched_help_long;
1290223383edSEnrico Granata 
1291223383edSEnrico Granata public:
1292223383edSEnrico Granata 
1293223383edSEnrico Granata     CommandObjectPythonFunction (CommandInterpreter &interpreter,
1294223383edSEnrico Granata                                  std::string name,
12950a305db7SEnrico Granata                                  std::string funct,
12960a305db7SEnrico Granata                                  ScriptedCommandSynchronicity synch) :
12975a988416SJim Ingham         CommandObjectRaw (interpreter,
1298223383edSEnrico Granata                           name.c_str(),
1299223383edSEnrico Granata                           (std::string("Run Python function ") + funct).c_str(),
1300223383edSEnrico Granata                           NULL),
13010a305db7SEnrico Granata         m_function_name(funct),
1302fac939e9SEnrico Granata         m_synchro(synch),
1303fac939e9SEnrico Granata         m_fetched_help_long(false)
1304223383edSEnrico Granata     {
1305223383edSEnrico Granata     }
1306223383edSEnrico Granata 
1307223383edSEnrico Granata     virtual
1308223383edSEnrico Granata     ~CommandObjectPythonFunction ()
1309223383edSEnrico Granata     {
1310223383edSEnrico Granata     }
1311223383edSEnrico Granata 
1312223383edSEnrico Granata     virtual bool
13133a18e319SGreg Clayton     IsRemovable () const
13145a988416SJim Ingham     {
13155a988416SJim Ingham         return true;
13165a988416SJim Ingham     }
13175a988416SJim Ingham 
13185a988416SJim Ingham     const std::string&
13195a988416SJim Ingham     GetFunctionName ()
13205a988416SJim Ingham     {
13215a988416SJim Ingham         return m_function_name;
13225a988416SJim Ingham     }
13235a988416SJim Ingham 
13245a988416SJim Ingham     ScriptedCommandSynchronicity
13255a988416SJim Ingham     GetSynchronicity ()
13265a988416SJim Ingham     {
13275a988416SJim Ingham         return m_synchro;
13285a988416SJim Ingham     }
13295a988416SJim Ingham 
1330fac939e9SEnrico Granata     virtual const char *
1331fac939e9SEnrico Granata     GetHelpLong ()
1332fac939e9SEnrico Granata     {
1333fac939e9SEnrico Granata         if (!m_fetched_help_long)
1334fac939e9SEnrico Granata         {
1335fac939e9SEnrico Granata             ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
1336fac939e9SEnrico Granata             if (scripter)
1337fac939e9SEnrico Granata             {
1338fac939e9SEnrico Granata                 std::string docstring;
1339fac939e9SEnrico Granata                 m_fetched_help_long = scripter->GetDocumentationForItem(m_function_name.c_str(),docstring);
1340fac939e9SEnrico Granata                 if (!docstring.empty())
1341fac939e9SEnrico Granata                     SetHelpLong(docstring);
1342fac939e9SEnrico Granata             }
1343fac939e9SEnrico Granata         }
1344fac939e9SEnrico Granata         return CommandObjectRaw::GetHelpLong();
1345fac939e9SEnrico Granata     }
1346fac939e9SEnrico Granata 
13475a988416SJim Ingham protected:
13485a988416SJim Ingham     virtual bool
13495a988416SJim Ingham     DoExecute (const char *raw_command_line, CommandReturnObject &result)
1350223383edSEnrico Granata     {
1351223383edSEnrico Granata         ScriptInterpreter* scripter = m_interpreter.GetScriptInterpreter();
1352223383edSEnrico Granata 
1353223383edSEnrico Granata         Error error;
1354223383edSEnrico Granata 
135570f11f88SJim Ingham         result.SetStatus(eReturnStatusInvalid);
135670f11f88SJim Ingham 
1357223383edSEnrico Granata         if (!scripter || scripter->RunScriptBasedCommand(m_function_name.c_str(),
1358223383edSEnrico Granata                                                          raw_command_line,
13590a305db7SEnrico Granata                                                          m_synchro,
1360223383edSEnrico Granata                                                          result,
1361223383edSEnrico Granata                                                          error) == false)
1362223383edSEnrico Granata         {
1363223383edSEnrico Granata             result.AppendError(error.AsCString());
1364223383edSEnrico Granata             result.SetStatus(eReturnStatusFailed);
1365223383edSEnrico Granata         }
1366223383edSEnrico Granata         else
136770f11f88SJim Ingham         {
136870f11f88SJim Ingham             // Don't change the status if the command already set it...
136970f11f88SJim Ingham             if (result.GetStatus() == eReturnStatusInvalid)
137070f11f88SJim Ingham             {
13719a71a7d8SDaniel Malea                 if (result.GetOutputData() == NULL || result.GetOutputData()[0] == '\0')
1372223383edSEnrico Granata                     result.SetStatus(eReturnStatusSuccessFinishNoResult);
137370f11f88SJim Ingham                 else
137470f11f88SJim Ingham                     result.SetStatus(eReturnStatusSuccessFinishResult);
137570f11f88SJim Ingham             }
137670f11f88SJim Ingham         }
1377223383edSEnrico Granata 
1378223383edSEnrico Granata         return result.Succeeded();
1379223383edSEnrico Granata     }
1380223383edSEnrico Granata 
1381223383edSEnrico Granata };
1382223383edSEnrico Granata 
1383a9dbf432SEnrico Granata //-------------------------------------------------------------------------
1384a9dbf432SEnrico Granata // CommandObjectCommandsScriptImport
1385a9dbf432SEnrico Granata //-------------------------------------------------------------------------
1386a9dbf432SEnrico Granata 
13875a988416SJim Ingham class CommandObjectCommandsScriptImport : public CommandObjectParsed
1388a9dbf432SEnrico Granata {
13895a988416SJim Ingham public:
13905a988416SJim Ingham     CommandObjectCommandsScriptImport (CommandInterpreter &interpreter) :
13915a988416SJim Ingham         CommandObjectParsed (interpreter,
13925a988416SJim Ingham                              "command script import",
13935a988416SJim Ingham                              "Import a scripting module in LLDB.",
13945a988416SJim Ingham                              NULL),
13955a988416SJim Ingham         m_options(interpreter)
13965a988416SJim Ingham     {
13975a988416SJim Ingham         CommandArgumentEntry arg1;
13985a988416SJim Ingham         CommandArgumentData cmd_arg;
13995a988416SJim Ingham 
14005a988416SJim Ingham         // Define the first (and only) variant of this arg.
14015a988416SJim Ingham         cmd_arg.arg_type = eArgTypeFilename;
14025a988416SJim Ingham         cmd_arg.arg_repetition = eArgRepeatPlain;
14035a988416SJim Ingham 
14045a988416SJim Ingham         // There is only one variant this argument could be; put it into the argument entry.
14055a988416SJim Ingham         arg1.push_back (cmd_arg);
14065a988416SJim Ingham 
14075a988416SJim Ingham         // Push the data for the first argument into the m_arguments vector.
14085a988416SJim Ingham         m_arguments.push_back (arg1);
14095a988416SJim Ingham     }
14105a988416SJim Ingham 
14115a988416SJim Ingham     ~CommandObjectCommandsScriptImport ()
14125a988416SJim Ingham     {
14135a988416SJim Ingham     }
14145a988416SJim Ingham 
1415c7bece56SGreg Clayton     virtual int
14165a988416SJim Ingham     HandleArgumentCompletion (Args &input,
14175a988416SJim Ingham                               int &cursor_index,
14185a988416SJim Ingham                               int &cursor_char_position,
14195a988416SJim Ingham                               OptionElementVector &opt_element_vector,
14205a988416SJim Ingham                               int match_start_point,
14215a988416SJim Ingham                               int max_return_elements,
14225a988416SJim Ingham                               bool &word_complete,
14235a988416SJim Ingham                               StringList &matches)
14245a988416SJim Ingham     {
14255a988416SJim Ingham         std::string completion_str (input.GetArgumentAtIndex(cursor_index));
14265a988416SJim Ingham         completion_str.erase (cursor_char_position);
14275a988416SJim Ingham 
14285a988416SJim Ingham         CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
14295a988416SJim Ingham                                                              CommandCompletions::eDiskFileCompletion,
14305a988416SJim Ingham                                                              completion_str.c_str(),
14315a988416SJim Ingham                                                              match_start_point,
14325a988416SJim Ingham                                                              max_return_elements,
14335a988416SJim Ingham                                                              NULL,
14345a988416SJim Ingham                                                              word_complete,
14355a988416SJim Ingham                                                              matches);
14365a988416SJim Ingham         return matches.GetSize();
14375a988416SJim Ingham     }
14385a988416SJim Ingham 
14395a988416SJim Ingham     virtual Options *
14405a988416SJim Ingham     GetOptions ()
14415a988416SJim Ingham     {
14425a988416SJim Ingham         return &m_options;
14435a988416SJim Ingham     }
14445a988416SJim Ingham 
14455a988416SJim Ingham protected:
14460a305db7SEnrico Granata 
14470a305db7SEnrico Granata     class CommandOptions : public Options
14480a305db7SEnrico Granata     {
14490a305db7SEnrico Granata     public:
14500a305db7SEnrico Granata 
14510a305db7SEnrico Granata         CommandOptions (CommandInterpreter &interpreter) :
14520a305db7SEnrico Granata             Options (interpreter)
14530a305db7SEnrico Granata         {
14540a305db7SEnrico Granata         }
14550a305db7SEnrico Granata 
14560a305db7SEnrico Granata         virtual
14570a305db7SEnrico Granata         ~CommandOptions (){}
14580a305db7SEnrico Granata 
14590a305db7SEnrico Granata         virtual Error
14600a305db7SEnrico Granata         SetOptionValue (uint32_t option_idx, const char *option_arg)
14610a305db7SEnrico Granata         {
14620a305db7SEnrico Granata             Error error;
14633bcdfc0eSGreg Clayton             const int short_option = m_getopt_table[option_idx].val;
14640a305db7SEnrico Granata 
14650a305db7SEnrico Granata             switch (short_option)
14660a305db7SEnrico Granata             {
14670a305db7SEnrico Granata                 case 'r':
14680a305db7SEnrico Granata                     m_allow_reload = true;
14690a305db7SEnrico Granata                     break;
14700a305db7SEnrico Granata                 default:
14710a305db7SEnrico Granata                     error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
14720a305db7SEnrico Granata                     break;
14730a305db7SEnrico Granata             }
14740a305db7SEnrico Granata 
14750a305db7SEnrico Granata             return error;
14760a305db7SEnrico Granata         }
14770a305db7SEnrico Granata 
14780a305db7SEnrico Granata         void
14790a305db7SEnrico Granata         OptionParsingStarting ()
14800a305db7SEnrico Granata         {
1481e0c70f1bSEnrico Granata             m_allow_reload = true;
14820a305db7SEnrico Granata         }
14830a305db7SEnrico Granata 
14840a305db7SEnrico Granata         const OptionDefinition*
14850a305db7SEnrico Granata         GetDefinitions ()
14860a305db7SEnrico Granata         {
14870a305db7SEnrico Granata             return g_option_table;
14880a305db7SEnrico Granata         }
14890a305db7SEnrico Granata 
14900a305db7SEnrico Granata         // Options table: Required for subclasses of Options.
14910a305db7SEnrico Granata 
14920a305db7SEnrico Granata         static OptionDefinition g_option_table[];
14930a305db7SEnrico Granata 
14940a305db7SEnrico Granata         // Instance variables to hold the values for command options.
14950a305db7SEnrico Granata 
14960a305db7SEnrico Granata         bool m_allow_reload;
14970a305db7SEnrico Granata     };
14980a305db7SEnrico Granata 
1499a9dbf432SEnrico Granata     bool
15005a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
1501a9dbf432SEnrico Granata     {
1502a9dbf432SEnrico Granata 
1503a9dbf432SEnrico Granata         if (m_interpreter.GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython)
1504a9dbf432SEnrico Granata         {
1505a9dbf432SEnrico Granata             result.AppendError ("only scripting language supported for module importing is currently Python");
1506a9dbf432SEnrico Granata             result.SetStatus (eReturnStatusFailed);
1507a9dbf432SEnrico Granata             return false;
1508a9dbf432SEnrico Granata         }
1509a9dbf432SEnrico Granata 
15105a988416SJim Ingham         size_t argc = command.GetArgumentCount();
1511a9dbf432SEnrico Granata 
1512a9dbf432SEnrico Granata         if (argc != 1)
1513a9dbf432SEnrico Granata         {
1514a9dbf432SEnrico Granata             result.AppendError ("'command script import' requires one argument");
1515a9dbf432SEnrico Granata             result.SetStatus (eReturnStatusFailed);
1516a9dbf432SEnrico Granata             return false;
1517a9dbf432SEnrico Granata         }
1518a9dbf432SEnrico Granata 
15195a988416SJim Ingham         std::string path = command.GetArgumentAtIndex(0);
1520a9dbf432SEnrico Granata         Error error;
1521a9dbf432SEnrico Granata 
1522c9d645d3SGreg Clayton         const bool init_session = true;
1523078551c7SEnrico Granata         // FIXME: this is necessary because CommandObject::CheckRequirements() assumes that
1524078551c7SEnrico Granata         // commands won't ever be recursively invoked, but it's actually possible to craft
1525078551c7SEnrico Granata         // a Python script that does other "command script imports" in __lldb_init_module
1526078551c7SEnrico Granata         // the real fix is to have recursive commands possible with a CommandInvocation object
1527078551c7SEnrico Granata         // separate from the CommandObject itself, so that recursive command invocations
1528078551c7SEnrico Granata         // won't stomp on each other (wrt to execution contents, options, and more)
1529078551c7SEnrico Granata         m_exe_ctx.Clear();
1530a9dbf432SEnrico Granata         if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule(path.c_str(),
15310a305db7SEnrico Granata                                                                       m_options.m_allow_reload,
1532c9d645d3SGreg Clayton                                                                       init_session,
1533a9dbf432SEnrico Granata                                                                       error))
1534a9dbf432SEnrico Granata         {
1535a9dbf432SEnrico Granata             result.SetStatus (eReturnStatusSuccessFinishNoResult);
1536a9dbf432SEnrico Granata         }
1537a9dbf432SEnrico Granata         else
1538a9dbf432SEnrico Granata         {
1539a9dbf432SEnrico Granata             result.AppendErrorWithFormat("module importing failed: %s", error.AsCString());
1540a9dbf432SEnrico Granata             result.SetStatus (eReturnStatusFailed);
1541a9dbf432SEnrico Granata         }
1542a9dbf432SEnrico Granata 
1543a9dbf432SEnrico Granata         return result.Succeeded();
1544a9dbf432SEnrico Granata     }
15450a305db7SEnrico Granata 
15465a988416SJim Ingham     CommandOptions m_options;
1547a9dbf432SEnrico Granata };
1548223383edSEnrico Granata 
15490a305db7SEnrico Granata OptionDefinition
15500a305db7SEnrico Granata CommandObjectCommandsScriptImport::CommandOptions::g_option_table[] =
15510a305db7SEnrico Granata {
1552e2607b50SVirgile Bello     { LLDB_OPT_SET_1, false, "allow-reload", 'r', OptionParser::eNoArgument, 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."},
15530a305db7SEnrico Granata     { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
15540a305db7SEnrico Granata };
15550a305db7SEnrico Granata 
15560a305db7SEnrico Granata 
1557223383edSEnrico Granata //-------------------------------------------------------------------------
1558223383edSEnrico Granata // CommandObjectCommandsScriptAdd
1559223383edSEnrico Granata //-------------------------------------------------------------------------
1560223383edSEnrico Granata 
156144d93782SGreg Clayton class CommandObjectCommandsScriptAdd :
156244d93782SGreg Clayton     public CommandObjectParsed,
156344d93782SGreg Clayton     public IOHandlerDelegateMultiline
1564223383edSEnrico Granata {
15655a988416SJim Ingham public:
15665a988416SJim Ingham     CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter) :
15675a988416SJim Ingham         CommandObjectParsed (interpreter,
15685a988416SJim Ingham                              "command script add",
15695a988416SJim Ingham                              "Add a scripted function as an LLDB command.",
15705a988416SJim Ingham                              NULL),
157144d93782SGreg Clayton         IOHandlerDelegateMultiline ("DONE"),
15725a988416SJim Ingham         m_options (interpreter)
15735a988416SJim Ingham     {
15745a988416SJim Ingham         CommandArgumentEntry arg1;
15755a988416SJim Ingham         CommandArgumentData cmd_arg;
15765a988416SJim Ingham 
15775a988416SJim Ingham         // Define the first (and only) variant of this arg.
15785a988416SJim Ingham         cmd_arg.arg_type = eArgTypeCommandName;
15795a988416SJim Ingham         cmd_arg.arg_repetition = eArgRepeatPlain;
15805a988416SJim Ingham 
15815a988416SJim Ingham         // There is only one variant this argument could be; put it into the argument entry.
15825a988416SJim Ingham         arg1.push_back (cmd_arg);
15835a988416SJim Ingham 
15845a988416SJim Ingham         // Push the data for the first argument into the m_arguments vector.
15855a988416SJim Ingham         m_arguments.push_back (arg1);
15865a988416SJim Ingham     }
15875a988416SJim Ingham 
15885a988416SJim Ingham     ~CommandObjectCommandsScriptAdd ()
15895a988416SJim Ingham     {
15905a988416SJim Ingham     }
15915a988416SJim Ingham 
15925a988416SJim Ingham     virtual Options *
15935a988416SJim Ingham     GetOptions ()
15945a988416SJim Ingham     {
15955a988416SJim Ingham         return &m_options;
15965a988416SJim Ingham     }
15975a988416SJim Ingham 
15985a988416SJim Ingham protected:
1599223383edSEnrico Granata 
1600223383edSEnrico Granata     class CommandOptions : public Options
1601223383edSEnrico Granata     {
1602223383edSEnrico Granata     public:
1603223383edSEnrico Granata 
1604223383edSEnrico Granata         CommandOptions (CommandInterpreter &interpreter) :
1605223383edSEnrico Granata             Options (interpreter)
1606223383edSEnrico Granata         {
1607223383edSEnrico Granata         }
1608223383edSEnrico Granata 
1609223383edSEnrico Granata         virtual
1610223383edSEnrico Granata         ~CommandOptions (){}
1611223383edSEnrico Granata 
1612223383edSEnrico Granata         virtual Error
1613223383edSEnrico Granata         SetOptionValue (uint32_t option_idx, const char *option_arg)
1614223383edSEnrico Granata         {
1615223383edSEnrico Granata             Error error;
16163bcdfc0eSGreg Clayton             const int short_option = m_getopt_table[option_idx].val;
1617223383edSEnrico Granata 
1618223383edSEnrico Granata             switch (short_option)
1619223383edSEnrico Granata             {
1620223383edSEnrico Granata                 case 'f':
1621223383edSEnrico Granata                     m_funct_name = std::string(option_arg);
1622223383edSEnrico Granata                     break;
16230a305db7SEnrico Granata                 case 's':
162444d93782SGreg Clayton                     m_synchronicity = (ScriptedCommandSynchronicity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error);
16250a305db7SEnrico Granata                     if (!error.Success())
16260a305db7SEnrico Granata                         error.SetErrorStringWithFormat ("unrecognized value for synchronicity '%s'", option_arg);
16270a305db7SEnrico Granata                     break;
1628223383edSEnrico Granata                 default:
162986edbf41SGreg Clayton                     error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1630223383edSEnrico Granata                     break;
1631223383edSEnrico Granata             }
1632223383edSEnrico Granata 
1633223383edSEnrico Granata             return error;
1634223383edSEnrico Granata         }
1635223383edSEnrico Granata 
1636223383edSEnrico Granata         void
1637223383edSEnrico Granata         OptionParsingStarting ()
1638223383edSEnrico Granata         {
1639223383edSEnrico Granata             m_funct_name = "";
164044d93782SGreg Clayton             m_synchronicity = eScriptedCommandSynchronicitySynchronous;
1641223383edSEnrico Granata         }
1642223383edSEnrico Granata 
1643223383edSEnrico Granata         const OptionDefinition*
1644223383edSEnrico Granata         GetDefinitions ()
1645223383edSEnrico Granata         {
1646223383edSEnrico Granata             return g_option_table;
1647223383edSEnrico Granata         }
1648223383edSEnrico Granata 
1649223383edSEnrico Granata         // Options table: Required for subclasses of Options.
1650223383edSEnrico Granata 
1651223383edSEnrico Granata         static OptionDefinition g_option_table[];
1652223383edSEnrico Granata 
1653223383edSEnrico Granata         // Instance variables to hold the values for command options.
1654223383edSEnrico Granata 
1655223383edSEnrico Granata         std::string m_funct_name;
165644d93782SGreg Clayton         ScriptedCommandSynchronicity m_synchronicity;
1657223383edSEnrico Granata     };
1658223383edSEnrico Granata 
165944d93782SGreg Clayton     virtual void
166044d93782SGreg Clayton     IOHandlerActivated (IOHandler &io_handler)
1661223383edSEnrico Granata     {
166244d93782SGreg Clayton         StreamFileSP output_sp(io_handler.GetOutputStreamFile());
166344d93782SGreg Clayton         if (output_sp)
1664223383edSEnrico Granata         {
166544d93782SGreg Clayton             output_sp->PutCString(g_python_command_instructions);
166644d93782SGreg Clayton             output_sp->Flush();
1667223383edSEnrico Granata         }
1668223383edSEnrico Granata     }
1669223383edSEnrico Granata 
1670223383edSEnrico Granata 
167144d93782SGreg Clayton     virtual void
167244d93782SGreg Clayton     IOHandlerInputComplete (IOHandler &io_handler, std::string &data)
1673223383edSEnrico Granata     {
167444d93782SGreg Clayton         StreamFileSP error_sp = io_handler.GetErrorStreamFile();
167544d93782SGreg Clayton 
167644d93782SGreg Clayton         ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
167744d93782SGreg Clayton         if (interpreter)
167844d93782SGreg Clayton         {
167944d93782SGreg Clayton 
168044d93782SGreg Clayton             StringList lines;
168144d93782SGreg Clayton             lines.SplitIntoLines(data);
168244d93782SGreg Clayton             if (lines.GetSize() > 0)
168344d93782SGreg Clayton             {
1684a73b7df7SEnrico Granata                 std::string funct_name_str;
168544d93782SGreg Clayton                 if (interpreter->GenerateScriptAliasFunction (lines, funct_name_str))
1686223383edSEnrico Granata                 {
1687a73b7df7SEnrico Granata                     if (funct_name_str.empty())
1688223383edSEnrico Granata                     {
168944d93782SGreg Clayton                         error_sp->Printf ("error: unable to obtain a function name, didn't add python command.\n");
169044d93782SGreg Clayton                         error_sp->Flush();
1691223383edSEnrico Granata                     }
169244d93782SGreg Clayton                     else
169344d93782SGreg Clayton                     {
1694223383edSEnrico Granata                         // everything should be fine now, let's add this alias
1695223383edSEnrico Granata 
1696223383edSEnrico Granata                         CommandObjectSP command_obj_sp(new CommandObjectPythonFunction (m_interpreter,
1697223383edSEnrico Granata                                                                                         m_cmd_name,
1698a73b7df7SEnrico Granata                                                                                         funct_name_str.c_str(),
169944d93782SGreg Clayton                                                                                         m_synchronicity));
1700223383edSEnrico Granata 
17010a305db7SEnrico Granata                         if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp, true))
1702223383edSEnrico Granata                         {
170344d93782SGreg Clayton                             error_sp->Printf ("error: unable to add selected command, didn't add python command.\n");
170444d93782SGreg Clayton                             error_sp->Flush();
1705223383edSEnrico Granata                         }
1706223383edSEnrico Granata                     }
170744d93782SGreg Clayton                 }
170844d93782SGreg Clayton                 else
170944d93782SGreg Clayton                 {
171044d93782SGreg Clayton                     error_sp->Printf ("error: unable to create function, didn't add python command.\n");
171144d93782SGreg Clayton                     error_sp->Flush();
171244d93782SGreg Clayton                 }
171344d93782SGreg Clayton             }
171444d93782SGreg Clayton             else
171544d93782SGreg Clayton             {
171644d93782SGreg Clayton                 error_sp->Printf ("error: empty function, didn't add python command.\n");
171744d93782SGreg Clayton                 error_sp->Flush();
171844d93782SGreg Clayton             }
171944d93782SGreg Clayton         }
172044d93782SGreg Clayton         else
172144d93782SGreg Clayton         {
172244d93782SGreg Clayton             error_sp->Printf ("error: script interpreter missing, didn't add python command.\n");
172344d93782SGreg Clayton             error_sp->Flush();
172444d93782SGreg Clayton         }
172544d93782SGreg Clayton 
172644d93782SGreg Clayton         io_handler.SetIsDone(true);
172744d93782SGreg Clayton 
172844d93782SGreg Clayton 
172944d93782SGreg Clayton     }
1730223383edSEnrico Granata 
17315a988416SJim Ingham protected:
1732223383edSEnrico Granata     bool
17335a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
1734223383edSEnrico Granata     {
173599f0b8f9SEnrico Granata 
173699f0b8f9SEnrico Granata         if (m_interpreter.GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython)
173799f0b8f9SEnrico Granata         {
173899f0b8f9SEnrico Granata             result.AppendError ("only scripting language supported for scripted commands is currently Python");
173999f0b8f9SEnrico Granata             result.SetStatus (eReturnStatusFailed);
174099f0b8f9SEnrico Granata             return false;
174199f0b8f9SEnrico Granata         }
174299f0b8f9SEnrico Granata 
17435a988416SJim Ingham         size_t argc = command.GetArgumentCount();
1744223383edSEnrico Granata 
1745223383edSEnrico Granata         if (argc != 1)
1746223383edSEnrico Granata         {
1747223383edSEnrico Granata             result.AppendError ("'command script add' requires one argument");
1748223383edSEnrico Granata             result.SetStatus (eReturnStatusFailed);
1749223383edSEnrico Granata             return false;
1750223383edSEnrico Granata         }
1751223383edSEnrico Granata 
175244d93782SGreg Clayton         // Store the command name and synchronicity in case we get multi-line input
175344d93782SGreg Clayton         m_cmd_name = command.GetArgumentAtIndex(0);
175444d93782SGreg Clayton         m_synchronicity = m_options.m_synchronicity;
1755223383edSEnrico Granata 
1756223383edSEnrico Granata         if (m_options.m_funct_name.empty())
1757223383edSEnrico Granata         {
175844d93782SGreg Clayton             m_interpreter.GetPythonCommandsFromIOHandler ("     ",  // Prompt
175944d93782SGreg Clayton                                                           *this,    // IOHandlerDelegate
176044d93782SGreg Clayton                                                           true,     // Run IOHandler in async mode
176144d93782SGreg Clayton                                                           NULL);    // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
1762223383edSEnrico Granata         }
1763223383edSEnrico Granata         else
1764223383edSEnrico Granata         {
17650a305db7SEnrico Granata             CommandObjectSP new_cmd(new CommandObjectPythonFunction(m_interpreter,
176644d93782SGreg Clayton                                                                     m_cmd_name,
17670a305db7SEnrico Granata                                                                     m_options.m_funct_name,
176844d93782SGreg Clayton                                                                     m_synchronicity));
176944d93782SGreg Clayton             if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true))
1770223383edSEnrico Granata             {
1771223383edSEnrico Granata                 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1772223383edSEnrico Granata             }
1773223383edSEnrico Granata             else
1774223383edSEnrico Granata             {
1775223383edSEnrico Granata                 result.AppendError("cannot add command");
1776223383edSEnrico Granata                 result.SetStatus (eReturnStatusFailed);
1777223383edSEnrico Granata             }
1778223383edSEnrico Granata         }
1779223383edSEnrico Granata 
1780223383edSEnrico Granata         return result.Succeeded();
1781223383edSEnrico Granata 
1782223383edSEnrico Granata     }
17835a988416SJim Ingham 
17845a988416SJim Ingham     CommandOptions m_options;
178544d93782SGreg Clayton     std::string m_cmd_name;
178644d93782SGreg Clayton     ScriptedCommandSynchronicity m_synchronicity;
1787223383edSEnrico Granata };
1788223383edSEnrico Granata 
17890a305db7SEnrico Granata static OptionEnumValueElement g_script_synchro_type[] =
17900a305db7SEnrico Granata {
17910a305db7SEnrico Granata     { eScriptedCommandSynchronicitySynchronous,      "synchronous",       "Run synchronous"},
17920a305db7SEnrico Granata     { eScriptedCommandSynchronicityAsynchronous,     "asynchronous",      "Run asynchronous"},
17930a305db7SEnrico Granata     { eScriptedCommandSynchronicityCurrentValue,     "current",           "Do not alter current setting"},
17940a305db7SEnrico Granata     { 0, NULL, NULL }
17950a305db7SEnrico Granata };
17960a305db7SEnrico Granata 
1797223383edSEnrico Granata OptionDefinition
1798223383edSEnrico Granata CommandObjectCommandsScriptAdd::CommandOptions::g_option_table[] =
1799223383edSEnrico Granata {
1800e2607b50SVirgile Bello     { LLDB_OPT_SET_1, false, "function", 'f', OptionParser::eRequiredArgument, NULL, 0, eArgTypePythonFunction,        "Name of the Python function to bind to this command name."},
1801e2607b50SVirgile Bello     { LLDB_OPT_SET_1, false, "synchronicity", 's', OptionParser::eRequiredArgument, g_script_synchro_type, 0, eArgTypeScriptedCommandSynchronicity,        "Set the synchronicity of this command's executions with regard to LLDB event system."},
1802223383edSEnrico Granata     { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1803223383edSEnrico Granata };
1804223383edSEnrico Granata 
1805223383edSEnrico Granata //-------------------------------------------------------------------------
1806223383edSEnrico Granata // CommandObjectCommandsScriptList
1807223383edSEnrico Granata //-------------------------------------------------------------------------
1808223383edSEnrico Granata 
18095a988416SJim Ingham class CommandObjectCommandsScriptList : public CommandObjectParsed
1810223383edSEnrico Granata {
1811223383edSEnrico Granata private:
1812223383edSEnrico Granata 
1813223383edSEnrico Granata public:
1814223383edSEnrico Granata     CommandObjectCommandsScriptList(CommandInterpreter &interpreter) :
18155a988416SJim Ingham     CommandObjectParsed (interpreter,
1816223383edSEnrico Granata                    "command script list",
1817223383edSEnrico Granata                    "List defined scripted commands.",
1818223383edSEnrico Granata                    NULL)
1819223383edSEnrico Granata     {
1820223383edSEnrico Granata     }
1821223383edSEnrico Granata 
1822223383edSEnrico Granata     ~CommandObjectCommandsScriptList ()
1823223383edSEnrico Granata     {
1824223383edSEnrico Granata     }
1825223383edSEnrico Granata 
1826223383edSEnrico Granata     bool
18275a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
1828223383edSEnrico Granata     {
1829223383edSEnrico Granata 
1830223383edSEnrico Granata         m_interpreter.GetHelp(result,
1831223383edSEnrico Granata                               CommandInterpreter::eCommandTypesUserDef);
1832223383edSEnrico Granata 
1833223383edSEnrico Granata         result.SetStatus (eReturnStatusSuccessFinishResult);
1834223383edSEnrico Granata 
1835223383edSEnrico Granata         return true;
1836223383edSEnrico Granata 
1837223383edSEnrico Granata 
1838223383edSEnrico Granata     }
1839223383edSEnrico Granata };
1840223383edSEnrico Granata 
1841223383edSEnrico Granata //-------------------------------------------------------------------------
1842223383edSEnrico Granata // CommandObjectCommandsScriptClear
1843223383edSEnrico Granata //-------------------------------------------------------------------------
1844223383edSEnrico Granata 
18455a988416SJim Ingham class CommandObjectCommandsScriptClear : public CommandObjectParsed
1846223383edSEnrico Granata {
1847223383edSEnrico Granata private:
1848223383edSEnrico Granata 
1849223383edSEnrico Granata public:
1850223383edSEnrico Granata     CommandObjectCommandsScriptClear(CommandInterpreter &interpreter) :
18515a988416SJim Ingham         CommandObjectParsed (interpreter,
1852223383edSEnrico Granata                              "command script clear",
1853223383edSEnrico Granata                              "Delete all scripted commands.",
1854223383edSEnrico Granata                              NULL)
1855223383edSEnrico Granata     {
1856223383edSEnrico Granata     }
1857223383edSEnrico Granata 
1858223383edSEnrico Granata     ~CommandObjectCommandsScriptClear ()
1859223383edSEnrico Granata     {
1860223383edSEnrico Granata     }
1861223383edSEnrico Granata 
18625a988416SJim Ingham protected:
1863223383edSEnrico Granata     bool
18645a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
1865223383edSEnrico Granata     {
1866223383edSEnrico Granata 
1867223383edSEnrico Granata         m_interpreter.RemoveAllUser();
1868223383edSEnrico Granata 
1869223383edSEnrico Granata         result.SetStatus (eReturnStatusSuccessFinishResult);
1870223383edSEnrico Granata 
1871223383edSEnrico Granata         return true;
1872223383edSEnrico Granata     }
1873223383edSEnrico Granata };
1874223383edSEnrico Granata 
1875223383edSEnrico Granata //-------------------------------------------------------------------------
1876223383edSEnrico Granata // CommandObjectCommandsScriptDelete
1877223383edSEnrico Granata //-------------------------------------------------------------------------
1878223383edSEnrico Granata 
18795a988416SJim Ingham class CommandObjectCommandsScriptDelete : public CommandObjectParsed
1880223383edSEnrico Granata {
1881223383edSEnrico Granata public:
1882223383edSEnrico Granata     CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter) :
18835a988416SJim Ingham         CommandObjectParsed (interpreter,
1884223383edSEnrico Granata                              "command script delete",
1885223383edSEnrico Granata                              "Delete a scripted command.",
1886223383edSEnrico Granata                              NULL)
1887223383edSEnrico Granata     {
1888223383edSEnrico Granata         CommandArgumentEntry arg1;
1889223383edSEnrico Granata         CommandArgumentData cmd_arg;
1890223383edSEnrico Granata 
1891223383edSEnrico Granata         // Define the first (and only) variant of this arg.
1892223383edSEnrico Granata         cmd_arg.arg_type = eArgTypeCommandName;
1893223383edSEnrico Granata         cmd_arg.arg_repetition = eArgRepeatPlain;
1894223383edSEnrico Granata 
1895223383edSEnrico Granata         // There is only one variant this argument could be; put it into the argument entry.
1896223383edSEnrico Granata         arg1.push_back (cmd_arg);
1897223383edSEnrico Granata 
1898223383edSEnrico Granata         // Push the data for the first argument into the m_arguments vector.
1899223383edSEnrico Granata         m_arguments.push_back (arg1);
1900223383edSEnrico Granata     }
1901223383edSEnrico Granata 
1902223383edSEnrico Granata     ~CommandObjectCommandsScriptDelete ()
1903223383edSEnrico Granata     {
1904223383edSEnrico Granata     }
1905223383edSEnrico Granata 
19065a988416SJim Ingham protected:
1907223383edSEnrico Granata     bool
19085a988416SJim Ingham     DoExecute (Args& command, CommandReturnObject &result)
1909223383edSEnrico Granata     {
1910223383edSEnrico Granata 
19115a988416SJim Ingham         size_t argc = command.GetArgumentCount();
1912223383edSEnrico Granata 
1913223383edSEnrico Granata         if (argc != 1)
1914223383edSEnrico Granata         {
1915223383edSEnrico Granata             result.AppendError ("'command script delete' requires one argument");
1916223383edSEnrico Granata             result.SetStatus (eReturnStatusFailed);
1917223383edSEnrico Granata             return false;
1918223383edSEnrico Granata         }
1919223383edSEnrico Granata 
19205a988416SJim Ingham         const char* cmd_name = command.GetArgumentAtIndex(0);
1921223383edSEnrico Granata 
1922223383edSEnrico Granata         if (cmd_name && *cmd_name && m_interpreter.HasUserCommands() && m_interpreter.UserCommandExists(cmd_name))
1923223383edSEnrico Granata         {
1924223383edSEnrico Granata             m_interpreter.RemoveUser(cmd_name);
1925223383edSEnrico Granata             result.SetStatus (eReturnStatusSuccessFinishResult);
1926223383edSEnrico Granata         }
1927223383edSEnrico Granata         else
1928223383edSEnrico Granata         {
1929223383edSEnrico Granata             result.AppendErrorWithFormat ("command %s not found", cmd_name);
1930223383edSEnrico Granata             result.SetStatus (eReturnStatusFailed);
1931223383edSEnrico Granata         }
1932223383edSEnrico Granata 
1933223383edSEnrico Granata         return result.Succeeded();
1934223383edSEnrico Granata 
1935223383edSEnrico Granata     }
1936223383edSEnrico Granata };
1937223383edSEnrico Granata 
1938223383edSEnrico Granata #pragma mark CommandObjectMultiwordCommandsScript
1939223383edSEnrico Granata 
1940223383edSEnrico Granata //-------------------------------------------------------------------------
1941223383edSEnrico Granata // CommandObjectMultiwordCommandsScript
1942223383edSEnrico Granata //-------------------------------------------------------------------------
1943223383edSEnrico Granata 
1944223383edSEnrico Granata class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword
1945223383edSEnrico Granata {
1946223383edSEnrico Granata public:
1947223383edSEnrico Granata     CommandObjectMultiwordCommandsScript (CommandInterpreter &interpreter) :
1948223383edSEnrico Granata     CommandObjectMultiword (interpreter,
1949223383edSEnrico Granata                             "command script",
1950223383edSEnrico Granata                             "A set of commands for managing or customizing script commands.",
1951223383edSEnrico Granata                             "command script <subcommand> [<subcommand-options>]")
1952223383edSEnrico Granata     {
1953223383edSEnrico Granata         LoadSubCommand ("add",  CommandObjectSP (new CommandObjectCommandsScriptAdd (interpreter)));
1954223383edSEnrico Granata         LoadSubCommand ("delete",   CommandObjectSP (new CommandObjectCommandsScriptDelete (interpreter)));
1955223383edSEnrico Granata         LoadSubCommand ("clear", CommandObjectSP (new CommandObjectCommandsScriptClear (interpreter)));
1956223383edSEnrico Granata         LoadSubCommand ("list",   CommandObjectSP (new CommandObjectCommandsScriptList (interpreter)));
1957a9dbf432SEnrico Granata         LoadSubCommand ("import",   CommandObjectSP (new CommandObjectCommandsScriptImport (interpreter)));
1958223383edSEnrico Granata     }
1959223383edSEnrico Granata 
1960223383edSEnrico Granata     ~CommandObjectMultiwordCommandsScript ()
1961223383edSEnrico Granata     {
1962223383edSEnrico Granata     }
1963223383edSEnrico Granata 
1964223383edSEnrico Granata };
1965223383edSEnrico Granata 
1966223383edSEnrico Granata 
1967ebc09c36SJim Ingham #pragma mark CommandObjectMultiwordCommands
1968ebc09c36SJim Ingham 
1969ebc09c36SJim Ingham //-------------------------------------------------------------------------
1970ebc09c36SJim Ingham // CommandObjectMultiwordCommands
1971ebc09c36SJim Ingham //-------------------------------------------------------------------------
1972ebc09c36SJim Ingham 
1973ebc09c36SJim Ingham CommandObjectMultiwordCommands::CommandObjectMultiwordCommands (CommandInterpreter &interpreter) :
1974a7015092SGreg Clayton     CommandObjectMultiword (interpreter,
19750e5e5a79SGreg Clayton                             "command",
19763f4c09c1SCaroline Tice                             "A set of commands for managing or customizing the debugger commands.",
19770e5e5a79SGreg Clayton                             "command <subcommand> [<subcommand-options>]")
1978ebc09c36SJim Ingham {
1979a7015092SGreg Clayton     LoadSubCommand ("source",  CommandObjectSP (new CommandObjectCommandsSource (interpreter)));
1980a7015092SGreg Clayton     LoadSubCommand ("alias",   CommandObjectSP (new CommandObjectCommandsAlias (interpreter)));
1981a7015092SGreg Clayton     LoadSubCommand ("unalias", CommandObjectSP (new CommandObjectCommandsUnalias (interpreter)));
1982de164aaaSGreg Clayton     LoadSubCommand ("regex",   CommandObjectSP (new CommandObjectCommandsAddRegex (interpreter)));
1983a5a97ebeSJim Ingham     LoadSubCommand ("history",   CommandObjectSP (new CommandObjectCommandsHistory (interpreter)));
1984223383edSEnrico Granata     LoadSubCommand ("script",   CommandObjectSP (new CommandObjectMultiwordCommandsScript (interpreter)));
1985ebc09c36SJim Ingham }
1986ebc09c36SJim Ingham 
1987ebc09c36SJim Ingham CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands ()
1988ebc09c36SJim Ingham {
1989ebc09c36SJim Ingham }
1990ebc09c36SJim Ingham 
1991