1696bd635SAlexander Shaposhnikov //===-- CommandObjectCommands.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 
10ebc09c36SJim Ingham // C Includes
11ebc09c36SJim Ingham // C++ Includes
12ebc09c36SJim Ingham // Other libraries and framework includes
130e5e5a79SGreg Clayton #include "llvm/ADT/StringRef.h"
140e5e5a79SGreg Clayton 
15ebc09c36SJim Ingham // Project includes
166e3d8e7fSEugene Zelenko #include "CommandObjectCommands.h"
1746d4aa21SEnrico Granata #include "CommandObjectHelp.h"
18ebc09c36SJim Ingham #include "lldb/Core/Debugger.h"
1944d93782SGreg Clayton #include "lldb/Core/IOHandler.h"
203eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h"
217594f14fSEnrico Granata #include "lldb/Interpreter/CommandHistory.h"
22ebc09c36SJim Ingham #include "lldb/Interpreter/CommandInterpreter.h"
23de164aaaSGreg Clayton #include "lldb/Interpreter/CommandObjectRegexCommand.h"
24ebc09c36SJim Ingham #include "lldb/Interpreter/CommandReturnObject.h"
2547cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h"
26012d4fcaSEnrico Granata #include "lldb/Interpreter/OptionValueBoolean.h"
2745d0e238SEnrico Granata #include "lldb/Interpreter/OptionValueString.h"
287594f14fSEnrico Granata #include "lldb/Interpreter/OptionValueUInt64.h"
29ebc09c36SJim Ingham #include "lldb/Interpreter/Options.h"
3099f0b8f9SEnrico Granata #include "lldb/Interpreter/ScriptInterpreter.h"
31145d95c9SPavel Labath #include "lldb/Utility/Args.h"
32573ab909SZachary Turner #include "lldb/Utility/StringList.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 
411f0f5b5bSZachary Turner static OptionDefinition g_history_options[] = {
421f0f5b5bSZachary Turner     // clang-format off
431f0f5b5bSZachary Turner   { LLDB_OPT_SET_1, false, "count",       'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "How many history commands to print." },
441f0f5b5bSZachary Turner   { LLDB_OPT_SET_1, false, "start-index", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Index at which to start printing history commands (or end to mean tail mode)." },
451f0f5b5bSZachary Turner   { LLDB_OPT_SET_1, false, "end-index",   'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Index at which to stop printing history commands." },
461f0f5b5bSZachary Turner   { LLDB_OPT_SET_2, false, "clear",       'C', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeBoolean,         "Clears the current command history." },
471f0f5b5bSZachary Turner     // clang-format on
481f0f5b5bSZachary Turner };
491f0f5b5bSZachary Turner 
50b9c1b51eSKate Stone class CommandObjectCommandsHistory : public CommandObjectParsed {
515a988416SJim Ingham public:
52b9c1b51eSKate Stone   CommandObjectCommandsHistory(CommandInterpreter &interpreter)
53b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command history",
54aab5be05SJim Ingham                             "Dump the history of commands in this session.\n"
55aab5be05SJim Ingham                             "Commands in the history list can be run again "
56aab5be05SJim Ingham                             "using \"!<INDEX>\".   \"!-<OFFSET>\" will re-run "
57aab5be05SJim Ingham                             "the command that is <OFFSET> commands from the end"
58aab5be05SJim Ingham                             " of the list (counting the current command).",
596e3d8e7fSEugene Zelenko                             nullptr),
60b9c1b51eSKate Stone         m_options() {}
615a988416SJim Ingham 
626e3d8e7fSEugene Zelenko   ~CommandObjectCommandsHistory() override = default;
635a988416SJim Ingham 
64b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
655a988416SJim Ingham 
665a988416SJim Ingham protected:
67b9c1b51eSKate Stone   class CommandOptions : public Options {
68a5a97ebeSJim Ingham   public:
69b9c1b51eSKate Stone     CommandOptions()
70b9c1b51eSKate Stone         : Options(), m_start_idx(0), m_stop_idx(0), m_count(0), m_clear(false) {
71a5a97ebeSJim Ingham     }
72a5a97ebeSJim Ingham 
736e3d8e7fSEugene Zelenko     ~CommandOptions() override = default;
74a5a97ebeSJim Ingham 
7597206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
76b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
7797206d57SZachary Turner       Status error;
783bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
79a5a97ebeSJim Ingham 
80b9c1b51eSKate Stone       switch (short_option) {
81a5a97ebeSJim Ingham       case 'c':
82fe11483bSZachary Turner         error = m_count.SetValueFromString(option_arg, eVarSetOperationAssign);
83a5a97ebeSJim Ingham         break;
84a5a97ebeSJim Ingham       case 's':
85fe11483bSZachary Turner         if (option_arg == "end") {
867594f14fSEnrico Granata           m_start_idx.SetCurrentValue(UINT64_MAX);
877594f14fSEnrico Granata           m_start_idx.SetOptionWasSet();
88b9c1b51eSKate Stone         } else
89fe11483bSZachary Turner           error = m_start_idx.SetValueFromString(option_arg,
90b9c1b51eSKate Stone                                                  eVarSetOperationAssign);
917594f14fSEnrico Granata         break;
927594f14fSEnrico Granata       case 'e':
93fe11483bSZachary Turner         error =
94fe11483bSZachary Turner             m_stop_idx.SetValueFromString(option_arg, eVarSetOperationAssign);
957594f14fSEnrico Granata         break;
9663123b64SEnrico Granata       case 'C':
9763123b64SEnrico Granata         m_clear.SetCurrentValue(true);
9863123b64SEnrico Granata         m_clear.SetOptionWasSet();
99a5a97ebeSJim Ingham         break;
100a5a97ebeSJim Ingham       default:
101b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
102b9c1b51eSKate Stone                                        short_option);
103a5a97ebeSJim Ingham         break;
104a5a97ebeSJim Ingham       }
105a5a97ebeSJim Ingham 
106a5a97ebeSJim Ingham       return error;
107a5a97ebeSJim Ingham     }
108a5a97ebeSJim Ingham 
109b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
1107594f14fSEnrico Granata       m_start_idx.Clear();
1117594f14fSEnrico Granata       m_stop_idx.Clear();
1127594f14fSEnrico Granata       m_count.Clear();
11363123b64SEnrico Granata       m_clear.Clear();
114a5a97ebeSJim Ingham     }
115a5a97ebeSJim Ingham 
1161f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
11770602439SZachary Turner       return llvm::makeArrayRef(g_history_options);
1181f0f5b5bSZachary Turner     }
119a5a97ebeSJim Ingham 
120a5a97ebeSJim Ingham     // Instance variables to hold the values for command options.
121a5a97ebeSJim Ingham 
1227594f14fSEnrico Granata     OptionValueUInt64 m_start_idx;
1237594f14fSEnrico Granata     OptionValueUInt64 m_stop_idx;
1247594f14fSEnrico Granata     OptionValueUInt64 m_count;
12563123b64SEnrico Granata     OptionValueBoolean m_clear;
126a5a97ebeSJim Ingham   };
127a5a97ebeSJim Ingham 
128b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
129b9c1b51eSKate Stone     if (m_options.m_clear.GetCurrentValue() &&
130b9c1b51eSKate Stone         m_options.m_clear.OptionWasSet()) {
1317594f14fSEnrico Granata       m_interpreter.GetCommandHistory().Clear();
1327594f14fSEnrico Granata       result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
133b9c1b51eSKate Stone     } else {
134b9c1b51eSKate Stone       if (m_options.m_start_idx.OptionWasSet() &&
135b9c1b51eSKate Stone           m_options.m_stop_idx.OptionWasSet() &&
136b9c1b51eSKate Stone           m_options.m_count.OptionWasSet()) {
137b9c1b51eSKate Stone         result.AppendError("--count, --start-index and --end-index cannot be "
138b9c1b51eSKate Stone                            "all specified in the same invocation");
1397594f14fSEnrico Granata         result.SetStatus(lldb::eReturnStatusFailed);
140b9c1b51eSKate Stone       } else {
141b9c1b51eSKate Stone         std::pair<bool, uint64_t> start_idx(
142b9c1b51eSKate Stone             m_options.m_start_idx.OptionWasSet(),
143b9c1b51eSKate Stone             m_options.m_start_idx.GetCurrentValue());
144b9c1b51eSKate Stone         std::pair<bool, uint64_t> stop_idx(
145b9c1b51eSKate Stone             m_options.m_stop_idx.OptionWasSet(),
146b9c1b51eSKate Stone             m_options.m_stop_idx.GetCurrentValue());
147b9c1b51eSKate Stone         std::pair<bool, uint64_t> count(m_options.m_count.OptionWasSet(),
148b9c1b51eSKate Stone                                         m_options.m_count.GetCurrentValue());
149a5a97ebeSJim Ingham 
1507594f14fSEnrico Granata         const CommandHistory &history(m_interpreter.GetCommandHistory());
1517594f14fSEnrico Granata 
152b9c1b51eSKate Stone         if (start_idx.first && start_idx.second == UINT64_MAX) {
153b9c1b51eSKate Stone           if (count.first) {
1547594f14fSEnrico Granata             start_idx.second = history.GetSize() - count.second;
1557594f14fSEnrico Granata             stop_idx.second = history.GetSize() - 1;
156b9c1b51eSKate Stone           } else if (stop_idx.first) {
1577594f14fSEnrico Granata             start_idx.second = stop_idx.second;
1587594f14fSEnrico Granata             stop_idx.second = history.GetSize() - 1;
159b9c1b51eSKate Stone           } else {
1607594f14fSEnrico Granata             start_idx.second = 0;
1617594f14fSEnrico Granata             stop_idx.second = history.GetSize() - 1;
1627594f14fSEnrico Granata           }
163b9c1b51eSKate Stone         } else {
164b9c1b51eSKate Stone           if (!start_idx.first && !stop_idx.first && !count.first) {
1657594f14fSEnrico Granata             start_idx.second = 0;
1667594f14fSEnrico Granata             stop_idx.second = history.GetSize() - 1;
167b9c1b51eSKate Stone           } else if (start_idx.first) {
168b9c1b51eSKate Stone             if (count.first) {
1697594f14fSEnrico Granata               stop_idx.second = start_idx.second + count.second - 1;
170b9c1b51eSKate Stone             } else if (!stop_idx.first) {
1717594f14fSEnrico Granata               stop_idx.second = history.GetSize() - 1;
1727594f14fSEnrico Granata             }
173b9c1b51eSKate Stone           } else if (stop_idx.first) {
174b9c1b51eSKate Stone             if (count.first) {
1757594f14fSEnrico Granata               if (stop_idx.second >= count.second)
1767594f14fSEnrico Granata                 start_idx.second = stop_idx.second - count.second + 1;
1777594f14fSEnrico Granata               else
1787594f14fSEnrico Granata                 start_idx.second = 0;
1797594f14fSEnrico Granata             }
180b9c1b51eSKate Stone           } else /* if (count.first) */
1817594f14fSEnrico Granata           {
1827594f14fSEnrico Granata             start_idx.second = 0;
1837594f14fSEnrico Granata             stop_idx.second = count.second - 1;
1847594f14fSEnrico Granata           }
1857594f14fSEnrico Granata         }
186b9c1b51eSKate Stone         history.Dump(result.GetOutputStream(), start_idx.second,
187b9c1b51eSKate Stone                      stop_idx.second);
1887594f14fSEnrico Granata       }
1897594f14fSEnrico Granata     }
190a5a97ebeSJim Ingham     return result.Succeeded();
191a5a97ebeSJim Ingham   }
1925a988416SJim Ingham 
1935a988416SJim Ingham   CommandOptions m_options;
194a5a97ebeSJim Ingham };
195a5a97ebeSJim Ingham 
196a5a97ebeSJim Ingham //-------------------------------------------------------------------------
197a5a97ebeSJim Ingham // CommandObjectCommandsSource
198a5a97ebeSJim Ingham //-------------------------------------------------------------------------
199a5a97ebeSJim Ingham 
2001f0f5b5bSZachary Turner static OptionDefinition g_source_options[] = {
2011f0f5b5bSZachary Turner     // clang-format off
2021f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL, false, "stop-on-error",    'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on error." },
2031f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on continue." },
2041f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL, false, "silent-run",       's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true don't echo commands while executing." },
2051f0f5b5bSZachary Turner     // clang-format on
2061f0f5b5bSZachary Turner };
2071f0f5b5bSZachary Turner 
208b9c1b51eSKate Stone class CommandObjectCommandsSource : public CommandObjectParsed {
2095a988416SJim Ingham public:
2107428a18cSKate Stone   CommandObjectCommandsSource(CommandInterpreter &interpreter)
211b9c1b51eSKate Stone       : CommandObjectParsed(
212b9c1b51eSKate Stone             interpreter, "command source",
213b9c1b51eSKate Stone             "Read and execute LLDB commands from the file <filename>.",
2146e3d8e7fSEugene Zelenko             nullptr),
215b9c1b51eSKate Stone         m_options() {
2165a988416SJim Ingham     CommandArgumentEntry arg;
2175a988416SJim Ingham     CommandArgumentData file_arg;
2185a988416SJim Ingham 
2195a988416SJim Ingham     // Define the first (and only) variant of this arg.
2205a988416SJim Ingham     file_arg.arg_type = eArgTypeFilename;
2215a988416SJim Ingham     file_arg.arg_repetition = eArgRepeatPlain;
2225a988416SJim Ingham 
223b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
224b9c1b51eSKate Stone     // argument entry.
2255a988416SJim Ingham     arg.push_back(file_arg);
2265a988416SJim Ingham 
2275a988416SJim Ingham     // Push the data for the first argument into the m_arguments vector.
2285a988416SJim Ingham     m_arguments.push_back(arg);
2295a988416SJim Ingham   }
2305a988416SJim Ingham 
2316e3d8e7fSEugene Zelenko   ~CommandObjectCommandsSource() override = default;
2325a988416SJim Ingham 
233b9c1b51eSKate Stone   const char *GetRepeatCommand(Args &current_command_args,
234b9c1b51eSKate Stone                                uint32_t index) override {
2355a988416SJim Ingham     return "";
2365a988416SJim Ingham   }
2375a988416SJim Ingham 
2382443bbd4SRaphael Isemann   int HandleArgumentCompletion(
2392443bbd4SRaphael Isemann       CompletionRequest &request,
2402443bbd4SRaphael Isemann       OptionElementVector &opt_element_vector) override {
241b9c1b51eSKate Stone     CommandCompletions::InvokeCommonCompletionCallbacks(
242b9c1b51eSKate Stone         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
243*a2e76c0bSRaphael Isemann         request, nullptr);
2442443bbd4SRaphael Isemann     return request.GetMatches().GetSize();
2455a988416SJim Ingham   }
2465a988416SJim Ingham 
247b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
2485a988416SJim Ingham 
2495a988416SJim Ingham protected:
250b9c1b51eSKate Stone   class CommandOptions : public Options {
251e16c50a1SJim Ingham   public:
252b9c1b51eSKate Stone     CommandOptions()
253b9c1b51eSKate Stone         : Options(), m_stop_on_error(true), m_silent_run(false),
254b9c1b51eSKate Stone           m_stop_on_continue(true) {}
255e16c50a1SJim Ingham 
2566e3d8e7fSEugene Zelenko     ~CommandOptions() override = default;
257e16c50a1SJim Ingham 
25897206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
259b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
26097206d57SZachary Turner       Status error;
2613bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
262e16c50a1SJim Ingham 
263b9c1b51eSKate Stone       switch (short_option) {
264e16c50a1SJim Ingham       case 'e':
265fe11483bSZachary Turner         error = m_stop_on_error.SetValueFromString(option_arg);
266e16c50a1SJim Ingham         break;
267340b0309SGreg Clayton 
268e16c50a1SJim Ingham       case 'c':
269fe11483bSZachary Turner         error = m_stop_on_continue.SetValueFromString(option_arg);
270e16c50a1SJim Ingham         break;
271340b0309SGreg Clayton 
27260986174SMichael Sartain       case 's':
273fe11483bSZachary Turner         error = m_silent_run.SetValueFromString(option_arg);
27460986174SMichael Sartain         break;
275340b0309SGreg Clayton 
276e16c50a1SJim Ingham       default:
277b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
278b9c1b51eSKate Stone                                        short_option);
279e16c50a1SJim Ingham         break;
280e16c50a1SJim Ingham       }
281e16c50a1SJim Ingham 
282e16c50a1SJim Ingham       return error;
283e16c50a1SJim Ingham     }
284e16c50a1SJim Ingham 
285b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
286012d4fcaSEnrico Granata       m_stop_on_error.Clear();
287340b0309SGreg Clayton       m_silent_run.Clear();
288340b0309SGreg Clayton       m_stop_on_continue.Clear();
289e16c50a1SJim Ingham     }
290e16c50a1SJim Ingham 
2911f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
29270602439SZachary Turner       return llvm::makeArrayRef(g_source_options);
2931f0f5b5bSZachary Turner     }
294e16c50a1SJim Ingham 
295e16c50a1SJim Ingham     // Instance variables to hold the values for command options.
296e16c50a1SJim Ingham 
297012d4fcaSEnrico Granata     OptionValueBoolean m_stop_on_error;
298340b0309SGreg Clayton     OptionValueBoolean m_silent_run;
299340b0309SGreg Clayton     OptionValueBoolean m_stop_on_continue;
300e16c50a1SJim Ingham   };
301e16c50a1SJim Ingham 
302b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
3034574a890SZachary Turner     if (command.GetArgumentCount() != 1) {
3044574a890SZachary Turner       result.AppendErrorWithFormat(
3054574a890SZachary Turner           "'%s' takes exactly one executable filename argument.\n",
3064574a890SZachary Turner           GetCommandName().str().c_str());
3074574a890SZachary Turner       result.SetStatus(eReturnStatusFailed);
3084574a890SZachary Turner       return false;
3094574a890SZachary Turner     }
310ebc09c36SJim Ingham 
3114574a890SZachary Turner     FileSpec cmd_file(command[0].ref, true);
3126e3d8e7fSEugene Zelenko     ExecutionContext *exe_ctx = nullptr; // Just use the default context.
313ebc09c36SJim Ingham 
314340b0309SGreg Clayton     // If any options were set, then use them
315340b0309SGreg Clayton     if (m_options.m_stop_on_error.OptionWasSet() ||
316340b0309SGreg Clayton         m_options.m_silent_run.OptionWasSet() ||
317b9c1b51eSKate Stone         m_options.m_stop_on_continue.OptionWasSet()) {
318340b0309SGreg Clayton       // Use user set settings
31926c7bf93SJim Ingham       CommandInterpreterRunOptions options;
3204574a890SZachary Turner       options.SetStopOnContinue(m_options.m_stop_on_continue.GetCurrentValue());
32126c7bf93SJim Ingham       options.SetStopOnError(m_options.m_stop_on_error.GetCurrentValue());
3227d8555c4SJim Ingham       options.SetEchoCommands(!m_options.m_silent_run.GetCurrentValue());
3237d8555c4SJim Ingham       options.SetPrintResults(!m_options.m_silent_run.GetCurrentValue());
32426c7bf93SJim Ingham 
3254574a890SZachary Turner       m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, result);
326b9c1b51eSKate Stone     } else {
32705097246SAdrian Prantl       // No options were set, inherit any settings from nested "command source"
32805097246SAdrian Prantl       // commands, or set to sane default settings...
32926c7bf93SJim Ingham       CommandInterpreterRunOptions options;
3304574a890SZachary Turner       m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, result);
331ebc09c36SJim Ingham     }
332ebc09c36SJim Ingham     return result.Succeeded();
333ebc09c36SJim Ingham   }
3346e3d8e7fSEugene Zelenko 
3355a988416SJim Ingham   CommandOptions m_options;
336ebc09c36SJim Ingham };
337ebc09c36SJim Ingham 
338ebc09c36SJim Ingham #pragma mark CommandObjectCommandsAlias
339ebc09c36SJim Ingham //-------------------------------------------------------------------------
340ebc09c36SJim Ingham // CommandObjectCommandsAlias
341ebc09c36SJim Ingham //-------------------------------------------------------------------------
342ebc09c36SJim Ingham 
3431f0f5b5bSZachary Turner static OptionDefinition g_alias_options[] = {
3441f0f5b5bSZachary Turner     // clang-format off
3451f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL, false, "help",      'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Help text for this command" },
3461f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL, false, "long-help", 'H', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Long help text for this command" },
3471f0f5b5bSZachary Turner     // clang-format on
3481f0f5b5bSZachary Turner };
3491f0f5b5bSZachary Turner 
350b9c1b51eSKate Stone static const char *g_python_command_instructions =
351b9c1b51eSKate Stone     "Enter your Python command(s). Type 'DONE' to end.\n"
352be93a35aSEnrico Granata     "You must define a Python function with this signature:\n"
35344d93782SGreg Clayton     "def my_command_impl(debugger, args, result, internal_dict):\n";
354be93a35aSEnrico Granata 
355b9c1b51eSKate Stone class CommandObjectCommandsAlias : public CommandObjectRaw {
35645d0e238SEnrico Granata protected:
357b9c1b51eSKate Stone   class CommandOptions : public OptionGroup {
358ebc09c36SJim Ingham   public:
359b9c1b51eSKate Stone     CommandOptions() : OptionGroup(), m_help(), m_long_help() {}
36045d0e238SEnrico Granata 
36145d0e238SEnrico Granata     ~CommandOptions() override = default;
36245d0e238SEnrico Granata 
3631f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
36470602439SZachary Turner       return llvm::makeArrayRef(g_alias_options);
3651f0f5b5bSZachary Turner     }
36645d0e238SEnrico Granata 
36797206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_value,
368b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
36997206d57SZachary Turner       Status error;
37045d0e238SEnrico Granata 
3711f0f5b5bSZachary Turner       const int short_option = GetDefinitions()[option_idx].short_option;
3728cef4b0bSZachary Turner       std::string option_str(option_value);
37345d0e238SEnrico Granata 
374b9c1b51eSKate Stone       switch (short_option) {
37545d0e238SEnrico Granata       case 'h':
3768cef4b0bSZachary Turner         m_help.SetCurrentValue(option_str);
37745d0e238SEnrico Granata         m_help.SetOptionWasSet();
37845d0e238SEnrico Granata         break;
37945d0e238SEnrico Granata 
38045d0e238SEnrico Granata       case 'H':
3818cef4b0bSZachary Turner         m_long_help.SetCurrentValue(option_str);
38245d0e238SEnrico Granata         m_long_help.SetOptionWasSet();
38345d0e238SEnrico Granata         break;
38445d0e238SEnrico Granata 
38545d0e238SEnrico Granata       default:
386b9c1b51eSKate Stone         error.SetErrorStringWithFormat("invalid short option character '%c'",
387b9c1b51eSKate Stone                                        short_option);
38845d0e238SEnrico Granata         break;
38945d0e238SEnrico Granata       }
39045d0e238SEnrico Granata 
39145d0e238SEnrico Granata       return error;
39245d0e238SEnrico Granata     }
39345d0e238SEnrico Granata 
394b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
39545d0e238SEnrico Granata       m_help.Clear();
39645d0e238SEnrico Granata       m_long_help.Clear();
39745d0e238SEnrico Granata     }
39845d0e238SEnrico Granata 
39945d0e238SEnrico Granata     OptionValueString m_help;
40045d0e238SEnrico Granata     OptionValueString m_long_help;
40145d0e238SEnrico Granata   };
40245d0e238SEnrico Granata 
40345d0e238SEnrico Granata   OptionGroupOptions m_option_group;
40445d0e238SEnrico Granata   CommandOptions m_command_options;
40545d0e238SEnrico Granata 
40645d0e238SEnrico Granata public:
407b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
40845d0e238SEnrico Granata 
4097428a18cSKate Stone   CommandObjectCommandsAlias(CommandInterpreter &interpreter)
410b9c1b51eSKate Stone       : CommandObjectRaw(
411b9c1b51eSKate Stone             interpreter, "command alias",
412a449698cSZachary Turner             "Define a custom command in terms of an existing command."),
413b9c1b51eSKate Stone         m_option_group(), m_command_options() {
41445d0e238SEnrico Granata     m_option_group.Append(&m_command_options);
41545d0e238SEnrico Granata     m_option_group.Finalize();
41645d0e238SEnrico Granata 
417ebc09c36SJim Ingham     SetHelpLong(
418ea671fbdSKate Stone         "'alias' allows the user to create a short-cut or abbreviation for long \
419ea671fbdSKate Stone commands, multi-word commands, and commands that take particular options.  \
420b9c1b51eSKate Stone Below are some simple examples of how one might use the 'alias' command:"
421b9c1b51eSKate Stone         R"(
422ea671fbdSKate Stone 
423ea671fbdSKate Stone (lldb) command alias sc script
424ea671fbdSKate Stone 
425ea671fbdSKate Stone     Creates the abbreviation 'sc' for the 'script' command.
426ea671fbdSKate Stone 
427ea671fbdSKate Stone (lldb) command alias bp breakpoint
428ea671fbdSKate Stone 
429b9c1b51eSKate Stone )"
430b9c1b51eSKate Stone         "    Creates the abbreviation 'bp' for the 'breakpoint' command.  Since \
431ea671fbdSKate Stone breakpoint commands are two-word commands, the user would still need to \
432b9c1b51eSKate Stone enter the second word after 'bp', e.g. 'bp enable' or 'bp delete'."
433b9c1b51eSKate Stone         R"(
434ea671fbdSKate Stone 
435ea671fbdSKate Stone (lldb) command alias bpl breakpoint list
436ea671fbdSKate Stone 
437ea671fbdSKate Stone     Creates the abbreviation 'bpl' for the two-word command 'breakpoint list'.
438ea671fbdSKate Stone 
439b9c1b51eSKate Stone )"
440b9c1b51eSKate Stone         "An alias can include some options for the command, with the values either \
441ea671fbdSKate Stone filled in at the time the alias is created, or specified as positional \
442ea671fbdSKate Stone arguments, to be filled in when the alias is invoked.  The following example \
443b9c1b51eSKate Stone shows how to create aliases with options:"
444b9c1b51eSKate Stone         R"(
445ea671fbdSKate Stone 
446ea671fbdSKate Stone (lldb) command alias bfl breakpoint set -f %1 -l %2
447ea671fbdSKate Stone 
448b9c1b51eSKate Stone )"
449b9c1b51eSKate Stone         "    Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \
450ea671fbdSKate Stone options already part of the alias.  So if the user wants to set a breakpoint \
451ea671fbdSKate Stone by file and line without explicitly having to use the -f and -l options, the \
452ea671fbdSKate Stone user can now use 'bfl' instead.  The '%1' and '%2' are positional placeholders \
453ea671fbdSKate Stone for the actual arguments that will be passed when the alias command is used.  \
454ea671fbdSKate Stone The number in the placeholder refers to the position/order the actual value \
455ea671fbdSKate Stone occupies when the alias is used.  All the occurrences of '%1' in the alias \
456ea671fbdSKate Stone will be replaced with the first argument, all the occurrences of '%2' in the \
457ea671fbdSKate Stone alias will be replaced with the second argument, and so on.  This also allows \
458ea671fbdSKate Stone actual arguments to be used multiple times within an alias (see 'process \
459b9c1b51eSKate Stone launch' example below)."
460b9c1b51eSKate Stone         R"(
461ea671fbdSKate Stone 
462b9c1b51eSKate Stone )"
463b9c1b51eSKate Stone         "Note: the positional arguments must substitute as whole words in the resultant \
464ea671fbdSKate Stone command, so you can't at present do something like this to append the file extension \
465b9c1b51eSKate Stone \".cpp\":"
466b9c1b51eSKate Stone         R"(
467ea671fbdSKate Stone 
468ea671fbdSKate Stone (lldb) command alias bcppfl breakpoint set -f %1.cpp -l %2
469ea671fbdSKate Stone 
470b9c1b51eSKate Stone )"
471b9c1b51eSKate Stone         "For more complex aliasing, use the \"command regex\" command instead.  In the \
472ea671fbdSKate Stone 'bfl' case above, the actual file value will be filled in with the first argument \
473ea671fbdSKate Stone following 'bfl' and the actual line number value will be filled in with the second \
474b9c1b51eSKate Stone argument.  The user would use this alias as follows:"
475b9c1b51eSKate Stone         R"(
476ea671fbdSKate Stone 
477ea671fbdSKate Stone (lldb) command alias bfl breakpoint set -f %1 -l %2
478ea671fbdSKate Stone (lldb) bfl my-file.c 137
479ea671fbdSKate Stone 
480ea671fbdSKate Stone This would be the same as if the user had entered 'breakpoint set -f my-file.c -l 137'.
481ea671fbdSKate Stone 
482ea671fbdSKate Stone Another example:
483ea671fbdSKate Stone 
484ea671fbdSKate Stone (lldb) command alias pltty process launch -s -o %1 -e %1
485ea671fbdSKate Stone (lldb) pltty /dev/tty0
486ea671fbdSKate Stone 
487ea671fbdSKate Stone     Interpreted as 'process launch -s -o /dev/tty0 -e /dev/tty0'
488ea671fbdSKate Stone 
489b9c1b51eSKate Stone )"
490b9c1b51eSKate Stone         "If the user always wanted to pass the same value to a particular option, the \
491ea671fbdSKate Stone alias could be defined with that value directly in the alias as a constant, \
492b9c1b51eSKate Stone rather than using a positional placeholder:"
493b9c1b51eSKate Stone         R"(
494ea671fbdSKate Stone 
495ea671fbdSKate Stone (lldb) command alias bl3 breakpoint set -f %1 -l 3
496ea671fbdSKate Stone 
497b9c1b51eSKate Stone     Always sets a breakpoint on line 3 of whatever file is indicated.)");
498ebc09c36SJim Ingham 
499405fe67fSCaroline Tice     CommandArgumentEntry arg1;
500405fe67fSCaroline Tice     CommandArgumentEntry arg2;
501405fe67fSCaroline Tice     CommandArgumentEntry arg3;
502405fe67fSCaroline Tice     CommandArgumentData alias_arg;
503405fe67fSCaroline Tice     CommandArgumentData cmd_arg;
504405fe67fSCaroline Tice     CommandArgumentData options_arg;
505405fe67fSCaroline Tice 
506405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
507405fe67fSCaroline Tice     alias_arg.arg_type = eArgTypeAliasName;
508405fe67fSCaroline Tice     alias_arg.arg_repetition = eArgRepeatPlain;
509405fe67fSCaroline Tice 
510b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
511b9c1b51eSKate Stone     // argument entry.
512405fe67fSCaroline Tice     arg1.push_back(alias_arg);
513405fe67fSCaroline Tice 
514405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
515405fe67fSCaroline Tice     cmd_arg.arg_type = eArgTypeCommandName;
516405fe67fSCaroline Tice     cmd_arg.arg_repetition = eArgRepeatPlain;
517405fe67fSCaroline Tice 
518b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
519b9c1b51eSKate Stone     // argument entry.
520405fe67fSCaroline Tice     arg2.push_back(cmd_arg);
521405fe67fSCaroline Tice 
522405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
523405fe67fSCaroline Tice     options_arg.arg_type = eArgTypeAliasOptions;
524405fe67fSCaroline Tice     options_arg.arg_repetition = eArgRepeatOptional;
525405fe67fSCaroline Tice 
526b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
527b9c1b51eSKate Stone     // argument entry.
528405fe67fSCaroline Tice     arg3.push_back(options_arg);
529405fe67fSCaroline Tice 
530405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
531405fe67fSCaroline Tice     m_arguments.push_back(arg1);
532405fe67fSCaroline Tice     m_arguments.push_back(arg2);
533405fe67fSCaroline Tice     m_arguments.push_back(arg3);
534ebc09c36SJim Ingham   }
535ebc09c36SJim Ingham 
5366e3d8e7fSEugene Zelenko   ~CommandObjectCommandsAlias() override = default;
537ebc09c36SJim Ingham 
5385a988416SJim Ingham protected:
5394d51a902SRaphael Isemann   bool DoExecute(llvm::StringRef raw_command_line,
540b9c1b51eSKate Stone                  CommandReturnObject &result) override {
5414d51a902SRaphael Isemann     if (raw_command_line.empty()) {
542d72e412fSEnrico Granata       result.AppendError("'command alias' requires at least two arguments");
54345d0e238SEnrico Granata       return false;
54445d0e238SEnrico Granata     }
54545d0e238SEnrico Granata 
546e1cfbc79STodd Fiala     ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext();
547e1cfbc79STodd Fiala     m_option_group.NotifyOptionParsingStarting(&exe_ctx);
54845d0e238SEnrico Granata 
5493a0e1270SRaphael Isemann     OptionsWithRaw args_with_suffix(raw_command_line);
5503a0e1270SRaphael Isemann     const char *remainder = args_with_suffix.GetRawPart().c_str();
55145d0e238SEnrico Granata 
5523a0e1270SRaphael Isemann     if (args_with_suffix.HasArgs())
5533a0e1270SRaphael Isemann       if (!ParseOptionsAndNotify(args_with_suffix.GetArgs(), result,
5543a0e1270SRaphael Isemann                                  m_option_group, exe_ctx))
55545d0e238SEnrico Granata         return false;
55645d0e238SEnrico Granata 
557a01bccdbSZachary Turner     llvm::StringRef raw_command_string(remainder);
558a01bccdbSZachary Turner     Args args(raw_command_string);
559844d2303SCaroline Tice 
56011eb9c64SZachary Turner     if (args.GetArgumentCount() < 2) {
561d72e412fSEnrico Granata       result.AppendError("'command alias' requires at least two arguments");
562844d2303SCaroline Tice       result.SetStatus(eReturnStatusFailed);
563844d2303SCaroline Tice       return false;
564844d2303SCaroline Tice     }
565844d2303SCaroline Tice 
566844d2303SCaroline Tice     // Get the alias command.
567844d2303SCaroline Tice 
5684574a890SZachary Turner     auto alias_command = args[0].ref;
5694574a890SZachary Turner     if (alias_command.startswith("-")) {
570d72e412fSEnrico Granata       result.AppendError("aliases starting with a dash are not supported");
571b9c1b51eSKate Stone       if (alias_command == "--help" || alias_command == "--long-help") {
572b9c1b51eSKate Stone         result.AppendWarning("if trying to pass options to 'command alias' add "
573b9c1b51eSKate Stone                              "a -- at the end of the options");
574d72e412fSEnrico Granata       }
575d72e412fSEnrico Granata       result.SetStatus(eReturnStatusFailed);
576d72e412fSEnrico Granata       return false;
577d72e412fSEnrico Granata     }
578844d2303SCaroline Tice 
579b9c1b51eSKate Stone     // Strip the new alias name off 'raw_command_string'  (leave it on args,
58005097246SAdrian Prantl     // which gets passed to 'Execute', which does the stripping itself.
581844d2303SCaroline Tice     size_t pos = raw_command_string.find(alias_command);
582b9c1b51eSKate Stone     if (pos == 0) {
583844d2303SCaroline Tice       raw_command_string = raw_command_string.substr(alias_command.size());
584844d2303SCaroline Tice       pos = raw_command_string.find_first_not_of(' ');
585844d2303SCaroline Tice       if ((pos != std::string::npos) && (pos > 0))
586844d2303SCaroline Tice         raw_command_string = raw_command_string.substr(pos);
587b9c1b51eSKate Stone     } else {
588844d2303SCaroline Tice       result.AppendError("Error parsing command string.  No alias created.");
589844d2303SCaroline Tice       result.SetStatus(eReturnStatusFailed);
590844d2303SCaroline Tice       return false;
591844d2303SCaroline Tice     }
592844d2303SCaroline Tice 
593844d2303SCaroline Tice     // Verify that the command is alias-able.
594771ef6d4SMalcolm Parsons     if (m_interpreter.CommandExists(alias_command)) {
595b9c1b51eSKate Stone       result.AppendErrorWithFormat(
596b9c1b51eSKate Stone           "'%s' is a permanent debugger command and cannot be redefined.\n",
5974574a890SZachary Turner           args[0].c_str());
598844d2303SCaroline Tice       result.SetStatus(eReturnStatusFailed);
599844d2303SCaroline Tice       return false;
600844d2303SCaroline Tice     }
601844d2303SCaroline Tice 
602b9c1b51eSKate Stone     // Get CommandObject that is being aliased. The command name is read from
603a01bccdbSZachary Turner     // the front of raw_command_string. raw_command_string is returned with the
604a01bccdbSZachary Turner     // name of the command object stripped off the front.
605a01bccdbSZachary Turner     llvm::StringRef original_raw_command_string = raw_command_string;
606b9c1b51eSKate Stone     CommandObject *cmd_obj =
607b9c1b51eSKate Stone         m_interpreter.GetCommandObjectForCommand(raw_command_string);
608844d2303SCaroline Tice 
609b9c1b51eSKate Stone     if (!cmd_obj) {
610b9c1b51eSKate Stone       result.AppendErrorWithFormat("invalid command given to 'command alias'. "
611b9c1b51eSKate Stone                                    "'%s' does not begin with a valid command."
612b9c1b51eSKate Stone                                    "  No alias created.",
613a01bccdbSZachary Turner                                    original_raw_command_string.str().c_str());
614844d2303SCaroline Tice       result.SetStatus(eReturnStatusFailed);
615844d2303SCaroline Tice       return false;
616b9c1b51eSKate Stone     } else if (!cmd_obj->WantsRawCommandString()) {
617b9c1b51eSKate Stone       // Note that args was initialized with the original command, and has not
61805097246SAdrian Prantl       // been updated to this point. Therefore can we pass it to the version of
61905097246SAdrian Prantl       // Execute that does not need/expect raw input in the alias.
6205a988416SJim Ingham       return HandleAliasingNormalCommand(args, result);
621b9c1b51eSKate Stone     } else {
622b9c1b51eSKate Stone       return HandleAliasingRawCommand(alias_command, raw_command_string,
623b9c1b51eSKate Stone                                       *cmd_obj, result);
6245a988416SJim Ingham     }
6255a988416SJim Ingham     return result.Succeeded();
6265a988416SJim Ingham   }
6275a988416SJim Ingham 
628a01bccdbSZachary Turner   bool HandleAliasingRawCommand(llvm::StringRef alias_command,
629a01bccdbSZachary Turner                                 llvm::StringRef raw_command_string,
630b9c1b51eSKate Stone                                 CommandObject &cmd_obj,
631b9c1b51eSKate Stone                                 CommandReturnObject &result) {
632844d2303SCaroline Tice     // Verify & handle any options/arguments passed to the alias command
633844d2303SCaroline Tice 
634b9c1b51eSKate Stone     OptionArgVectorSP option_arg_vector_sp =
635b9c1b51eSKate Stone         OptionArgVectorSP(new OptionArgVector);
636844d2303SCaroline Tice 
637b9c1b51eSKate Stone     if (CommandObjectSP cmd_obj_sp =
638b9c1b51eSKate Stone             m_interpreter.GetCommandSPExact(cmd_obj.GetCommandName(), false)) {
639a01bccdbSZachary Turner       if (m_interpreter.AliasExists(alias_command) ||
640a01bccdbSZachary Turner           m_interpreter.UserCommandExists(alias_command)) {
641b9c1b51eSKate Stone         result.AppendWarningWithFormat(
642b9c1b51eSKate Stone             "Overwriting existing definition for '%s'.\n",
643a01bccdbSZachary Turner             alias_command.str().c_str());
644844d2303SCaroline Tice       }
645b9c1b51eSKate Stone       if (CommandAlias *alias = m_interpreter.AddAlias(
646a01bccdbSZachary Turner               alias_command, cmd_obj_sp, raw_command_string)) {
64745d0e238SEnrico Granata         if (m_command_options.m_help.OptionWasSet())
64845d0e238SEnrico Granata           alias->SetHelp(m_command_options.m_help.GetCurrentValue());
64945d0e238SEnrico Granata         if (m_command_options.m_long_help.OptionWasSet())
65045d0e238SEnrico Granata           alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
651844d2303SCaroline Tice         result.SetStatus(eReturnStatusSuccessFinishNoResult);
652b9c1b51eSKate Stone       } else {
653472362e6SCaroline Tice         result.AppendError("Unable to create requested alias.\n");
654472362e6SCaroline Tice         result.SetStatus(eReturnStatusFailed);
655472362e6SCaroline Tice       }
656212130acSEnrico Granata 
657b9c1b51eSKate Stone     } else {
658212130acSEnrico Granata       result.AppendError("Unable to create requested alias.\n");
659212130acSEnrico Granata       result.SetStatus(eReturnStatusFailed);
660212130acSEnrico Granata     }
661212130acSEnrico Granata 
662844d2303SCaroline Tice     return result.Succeeded();
663844d2303SCaroline Tice   }
664ebc09c36SJim Ingham 
665b9c1b51eSKate Stone   bool HandleAliasingNormalCommand(Args &args, CommandReturnObject &result) {
666867b185dSCaroline Tice     size_t argc = args.GetArgumentCount();
667ebc09c36SJim Ingham 
668b9c1b51eSKate Stone     if (argc < 2) {
669d72e412fSEnrico Granata       result.AppendError("'command alias' requires at least two arguments");
670ebc09c36SJim Ingham       result.SetStatus(eReturnStatusFailed);
671ebc09c36SJim Ingham       return false;
672ebc09c36SJim Ingham     }
673ebc09c36SJim Ingham 
6744574a890SZachary Turner     // Save these in std::strings since we're going to shift them off.
6754574a890SZachary Turner     const std::string alias_command(args[0].ref);
6764574a890SZachary Turner     const std::string actual_command(args[1].ref);
677ebc09c36SJim Ingham 
678ebc09c36SJim Ingham     args.Shift(); // Shift the alias command word off the argument vector.
679ebc09c36SJim Ingham     args.Shift(); // Shift the old command word off the argument vector.
680ebc09c36SJim Ingham 
681b9c1b51eSKate Stone     // Verify that the command is alias'able, and get the appropriate command
682b9c1b51eSKate Stone     // object.
683ebc09c36SJim Ingham 
684771ef6d4SMalcolm Parsons     if (m_interpreter.CommandExists(alias_command)) {
685b9c1b51eSKate Stone       result.AppendErrorWithFormat(
686b9c1b51eSKate Stone           "'%s' is a permanent debugger command and cannot be redefined.\n",
687ebc09c36SJim Ingham           alias_command.c_str());
688ebc09c36SJim Ingham       result.SetStatus(eReturnStatusFailed);
6894574a890SZachary Turner       return false;
6904574a890SZachary Turner     }
6914574a890SZachary Turner 
692b9c1b51eSKate Stone     CommandObjectSP command_obj_sp(
693a449698cSZachary Turner         m_interpreter.GetCommandSPExact(actual_command, true));
694ebc09c36SJim Ingham     CommandObjectSP subcommand_obj_sp;
695ebc09c36SJim Ingham     bool use_subcommand = false;
6964574a890SZachary Turner     if (!command_obj_sp) {
6974574a890SZachary Turner       result.AppendErrorWithFormat("'%s' is not an existing command.\n",
6984574a890SZachary Turner                                    actual_command.c_str());
6994574a890SZachary Turner       result.SetStatus(eReturnStatusFailed);
7004574a890SZachary Turner       return false;
7014574a890SZachary Turner     }
702ebc09c36SJim Ingham     CommandObject *cmd_obj = command_obj_sp.get();
7036e3d8e7fSEugene Zelenko     CommandObject *sub_cmd_obj = nullptr;
704b9c1b51eSKate Stone     OptionArgVectorSP option_arg_vector_sp =
705b9c1b51eSKate Stone         OptionArgVectorSP(new OptionArgVector);
706ebc09c36SJim Ingham 
70711eb9c64SZachary Turner     while (cmd_obj->IsMultiwordObject() && !args.empty()) {
7084574a890SZachary Turner       auto sub_command = args[0].ref;
70911eb9c64SZachary Turner       assert(!sub_command.empty());
7104574a890SZachary Turner       subcommand_obj_sp = cmd_obj->GetSubcommandSP(sub_command);
7114574a890SZachary Turner       if (!subcommand_obj_sp) {
712b9c1b51eSKate Stone         result.AppendErrorWithFormat(
713b9c1b51eSKate Stone             "'%s' is not a valid sub-command of '%s'.  "
714f415eeb4SCaroline Tice             "Unable to create alias.\n",
7154574a890SZachary Turner             args[0].c_str(), actual_command.c_str());
716ebc09c36SJim Ingham         result.SetStatus(eReturnStatusFailed);
717ebc09c36SJim Ingham         return false;
718ebc09c36SJim Ingham       }
7194574a890SZachary Turner 
7204574a890SZachary Turner       sub_cmd_obj = subcommand_obj_sp.get();
7214574a890SZachary Turner       use_subcommand = true;
7224574a890SZachary Turner       args.Shift(); // Shift the sub_command word off the argument vector.
7234574a890SZachary Turner       cmd_obj = sub_cmd_obj;
724ebc09c36SJim Ingham     }
725ebc09c36SJim Ingham 
726ebc09c36SJim Ingham     // Verify & handle any options/arguments passed to the alias command
727ebc09c36SJim Ingham 
728212130acSEnrico Granata     std::string args_string;
729212130acSEnrico Granata 
73011eb9c64SZachary Turner     if (!args.empty()) {
731b9c1b51eSKate Stone       CommandObjectSP tmp_sp =
732b9c1b51eSKate Stone           m_interpreter.GetCommandSPExact(cmd_obj->GetCommandName(), false);
733ebc09c36SJim Ingham       if (use_subcommand)
7344574a890SZachary Turner         tmp_sp = m_interpreter.GetCommandSPExact(sub_cmd_obj->GetCommandName(),
7354574a890SZachary Turner                                                  false);
736ca90c47eSCaroline Tice 
737ca90c47eSCaroline Tice       args.GetCommandString(args_string);
738867b185dSCaroline Tice     }
739ebc09c36SJim Ingham 
740771ef6d4SMalcolm Parsons     if (m_interpreter.AliasExists(alias_command) ||
741771ef6d4SMalcolm Parsons         m_interpreter.UserCommandExists(alias_command)) {
742b9c1b51eSKate Stone       result.AppendWarningWithFormat(
7434574a890SZachary Turner           "Overwriting existing definition for '%s'.\n", alias_command.c_str());
744ebc09c36SJim Ingham     }
745ebc09c36SJim Ingham 
746b9c1b51eSKate Stone     if (CommandAlias *alias = m_interpreter.AddAlias(
7474574a890SZachary Turner             alias_command, use_subcommand ? subcommand_obj_sp : command_obj_sp,
748771ef6d4SMalcolm Parsons             args_string)) {
74945d0e238SEnrico Granata       if (m_command_options.m_help.OptionWasSet())
75045d0e238SEnrico Granata         alias->SetHelp(m_command_options.m_help.GetCurrentValue());
75145d0e238SEnrico Granata       if (m_command_options.m_long_help.OptionWasSet())
75245d0e238SEnrico Granata         alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
753ebc09c36SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
754b9c1b51eSKate Stone     } else {
755212130acSEnrico Granata       result.AppendError("Unable to create requested alias.\n");
756212130acSEnrico Granata       result.SetStatus(eReturnStatusFailed);
757212130acSEnrico Granata       return false;
758212130acSEnrico Granata     }
759ebc09c36SJim Ingham 
760ebc09c36SJim Ingham     return result.Succeeded();
761ebc09c36SJim Ingham   }
762ebc09c36SJim Ingham };
763ebc09c36SJim Ingham 
764ebc09c36SJim Ingham #pragma mark CommandObjectCommandsUnalias
765ebc09c36SJim Ingham //-------------------------------------------------------------------------
766ebc09c36SJim Ingham // CommandObjectCommandsUnalias
767ebc09c36SJim Ingham //-------------------------------------------------------------------------
768ebc09c36SJim Ingham 
769b9c1b51eSKate Stone class CommandObjectCommandsUnalias : public CommandObjectParsed {
770ebc09c36SJim Ingham public:
7717428a18cSKate Stone   CommandObjectCommandsUnalias(CommandInterpreter &interpreter)
772b9c1b51eSKate Stone       : CommandObjectParsed(
773b9c1b51eSKate Stone             interpreter, "command unalias",
774b9c1b51eSKate Stone             "Delete one or more custom commands defined by 'command alias'.",
775b9c1b51eSKate Stone             nullptr) {
776405fe67fSCaroline Tice     CommandArgumentEntry arg;
777405fe67fSCaroline Tice     CommandArgumentData alias_arg;
778405fe67fSCaroline Tice 
779405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
780405fe67fSCaroline Tice     alias_arg.arg_type = eArgTypeAliasName;
781405fe67fSCaroline Tice     alias_arg.arg_repetition = eArgRepeatPlain;
782405fe67fSCaroline Tice 
783b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
784b9c1b51eSKate Stone     // argument entry.
785405fe67fSCaroline Tice     arg.push_back(alias_arg);
786405fe67fSCaroline Tice 
787405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
788405fe67fSCaroline Tice     m_arguments.push_back(arg);
789ebc09c36SJim Ingham   }
790ebc09c36SJim Ingham 
7916e3d8e7fSEugene Zelenko   ~CommandObjectCommandsUnalias() override = default;
792ebc09c36SJim Ingham 
7935a988416SJim Ingham protected:
794b9c1b51eSKate Stone   bool DoExecute(Args &args, CommandReturnObject &result) override {
795ebc09c36SJim Ingham     CommandObject::CommandMap::iterator pos;
796ebc09c36SJim Ingham     CommandObject *cmd_obj;
797ebc09c36SJim Ingham 
79811eb9c64SZachary Turner     if (args.empty()) {
79911eb9c64SZachary Turner       result.AppendError("must call 'unalias' with a valid alias");
80011eb9c64SZachary Turner       result.SetStatus(eReturnStatusFailed);
80111eb9c64SZachary Turner       return false;
80211eb9c64SZachary Turner     }
80311eb9c64SZachary Turner 
8044574a890SZachary Turner     auto command_name = args[0].ref;
805a7015092SGreg Clayton     cmd_obj = m_interpreter.GetCommandObject(command_name);
8064574a890SZachary Turner     if (!cmd_obj) {
8074574a890SZachary Turner       result.AppendErrorWithFormat(
8084574a890SZachary Turner           "'%s' is not a known command.\nTry 'help' to see a "
8094574a890SZachary Turner           "current list of commands.\n",
810867e7d17SZachary Turner           args[0].c_str());
8114574a890SZachary Turner       result.SetStatus(eReturnStatusFailed);
8124574a890SZachary Turner       return false;
8134574a890SZachary Turner     }
8144574a890SZachary Turner 
815b9c1b51eSKate Stone     if (m_interpreter.CommandExists(command_name)) {
816b9c1b51eSKate Stone       if (cmd_obj->IsRemovable()) {
817b9c1b51eSKate Stone         result.AppendErrorWithFormat(
818b9c1b51eSKate Stone             "'%s' is not an alias, it is a debugger command which can be "
819b9c1b51eSKate Stone             "removed using the 'command delete' command.\n",
820867e7d17SZachary Turner             args[0].c_str());
821b9c1b51eSKate Stone       } else {
822b9c1b51eSKate Stone         result.AppendErrorWithFormat(
823b9c1b51eSKate Stone             "'%s' is a permanent debugger command and cannot be removed.\n",
824867e7d17SZachary Turner             args[0].c_str());
825b547278cSGreg Clayton       }
826ebc09c36SJim Ingham       result.SetStatus(eReturnStatusFailed);
8274574a890SZachary Turner       return false;
8284574a890SZachary Turner     }
8294574a890SZachary Turner 
830b9c1b51eSKate Stone     if (!m_interpreter.RemoveAlias(command_name)) {
831a7015092SGreg Clayton       if (m_interpreter.AliasExists(command_name))
832b9c1b51eSKate Stone         result.AppendErrorWithFormat(
833867e7d17SZachary Turner             "Error occurred while attempting to unalias '%s'.\n",
834867e7d17SZachary Turner             args[0].c_str());
835ebc09c36SJim Ingham       else
836b9c1b51eSKate Stone         result.AppendErrorWithFormat("'%s' is not an existing alias.\n",
837867e7d17SZachary Turner                                      args[0].c_str());
838ebc09c36SJim Ingham       result.SetStatus(eReturnStatusFailed);
8394574a890SZachary Turner       return false;
840ebc09c36SJim Ingham     }
841ebc09c36SJim Ingham 
8424574a890SZachary Turner     result.SetStatus(eReturnStatusSuccessFinishNoResult);
843ebc09c36SJim Ingham     return result.Succeeded();
844ebc09c36SJim Ingham   }
845ebc09c36SJim Ingham };
846ebc09c36SJim Ingham 
847b547278cSGreg Clayton #pragma mark CommandObjectCommandsDelete
848b547278cSGreg Clayton //-------------------------------------------------------------------------
849b547278cSGreg Clayton // CommandObjectCommandsDelete
850b547278cSGreg Clayton //-------------------------------------------------------------------------
851b547278cSGreg Clayton 
852b9c1b51eSKate Stone class CommandObjectCommandsDelete : public CommandObjectParsed {
853b547278cSGreg Clayton public:
8547428a18cSKate Stone   CommandObjectCommandsDelete(CommandInterpreter &interpreter)
855b9c1b51eSKate Stone       : CommandObjectParsed(
856b9c1b51eSKate Stone             interpreter, "command delete",
857b9c1b51eSKate Stone             "Delete one or more custom commands defined by 'command regex'.",
858b9c1b51eSKate Stone             nullptr) {
859b547278cSGreg Clayton     CommandArgumentEntry arg;
860b547278cSGreg Clayton     CommandArgumentData alias_arg;
861b547278cSGreg Clayton 
862b547278cSGreg Clayton     // Define the first (and only) variant of this arg.
863b547278cSGreg Clayton     alias_arg.arg_type = eArgTypeCommandName;
864b547278cSGreg Clayton     alias_arg.arg_repetition = eArgRepeatPlain;
865b547278cSGreg Clayton 
866b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
867b9c1b51eSKate Stone     // argument entry.
868b547278cSGreg Clayton     arg.push_back(alias_arg);
869b547278cSGreg Clayton 
870b547278cSGreg Clayton     // Push the data for the first argument into the m_arguments vector.
871b547278cSGreg Clayton     m_arguments.push_back(arg);
872b547278cSGreg Clayton   }
873b547278cSGreg Clayton 
8746e3d8e7fSEugene Zelenko   ~CommandObjectCommandsDelete() override = default;
875b547278cSGreg Clayton 
876b547278cSGreg Clayton protected:
877b9c1b51eSKate Stone   bool DoExecute(Args &args, CommandReturnObject &result) override {
878b547278cSGreg Clayton     CommandObject::CommandMap::iterator pos;
879b547278cSGreg Clayton 
88011eb9c64SZachary Turner     if (args.empty()) {
88111eb9c64SZachary Turner       result.AppendErrorWithFormat("must call '%s' with one or more valid user "
88211eb9c64SZachary Turner                                    "defined regular expression command names",
883a449698cSZachary Turner                                    GetCommandName().str().c_str());
88411eb9c64SZachary Turner       result.SetStatus(eReturnStatusFailed);
88511eb9c64SZachary Turner     }
88611eb9c64SZachary Turner 
8874574a890SZachary Turner     auto command_name = args[0].ref;
8884574a890SZachary Turner     if (!m_interpreter.CommandExists(command_name)) {
88946d4aa21SEnrico Granata       StreamString error_msg_stream;
89046d4aa21SEnrico Granata       const bool generate_apropos = true;
89146d4aa21SEnrico Granata       const bool generate_type_lookup = false;
892b9c1b51eSKate Stone       CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
8934574a890SZachary Turner           &error_msg_stream, command_name, llvm::StringRef(), llvm::StringRef(),
8944574a890SZachary Turner           generate_apropos, generate_type_lookup);
895c156427dSZachary Turner       result.AppendError(error_msg_stream.GetString());
896b547278cSGreg Clayton       result.SetStatus(eReturnStatusFailed);
8974574a890SZachary Turner       return false;
898b547278cSGreg Clayton     }
899b547278cSGreg Clayton 
9004574a890SZachary Turner     if (!m_interpreter.RemoveCommand(command_name)) {
9014574a890SZachary Turner       result.AppendErrorWithFormat(
9024574a890SZachary Turner           "'%s' is a permanent debugger command and cannot be removed.\n",
903867e7d17SZachary Turner           args[0].c_str());
9044574a890SZachary Turner       result.SetStatus(eReturnStatusFailed);
9054574a890SZachary Turner       return false;
9064574a890SZachary Turner     }
9074574a890SZachary Turner 
9084574a890SZachary Turner     result.SetStatus(eReturnStatusSuccessFinishNoResult);
9094574a890SZachary Turner     return true;
910b547278cSGreg Clayton   }
911b547278cSGreg Clayton };
912b547278cSGreg Clayton 
913de164aaaSGreg Clayton //-------------------------------------------------------------------------
914de164aaaSGreg Clayton // CommandObjectCommandsAddRegex
915de164aaaSGreg Clayton //-------------------------------------------------------------------------
9161f0f5b5bSZachary Turner 
9171f0f5b5bSZachary Turner static OptionDefinition g_regex_options[] = {
9181f0f5b5bSZachary Turner     // clang-format off
9191f0f5b5bSZachary Turner   { LLDB_OPT_SET_1, false, "help"  , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "The help text to display for this command." },
9201f0f5b5bSZachary Turner   { LLDB_OPT_SET_1, false, "syntax", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "A syntax string showing the typical usage syntax." },
9211f0f5b5bSZachary Turner     // clang-format on
9221f0f5b5bSZachary Turner };
9231f0f5b5bSZachary Turner 
9245a988416SJim Ingham #pragma mark CommandObjectCommandsAddRegex
925de164aaaSGreg Clayton 
926b9c1b51eSKate Stone class CommandObjectCommandsAddRegex : public CommandObjectParsed,
927b9c1b51eSKate Stone                                       public IOHandlerDelegateMultiline {
928de164aaaSGreg Clayton public:
9297428a18cSKate Stone   CommandObjectCommandsAddRegex(CommandInterpreter &interpreter)
930b9c1b51eSKate Stone       : CommandObjectParsed(
931b9c1b51eSKate Stone             interpreter, "command regex", "Define a custom command in terms of "
932b9c1b51eSKate Stone                                           "existing commands by matching "
933b9c1b51eSKate Stone                                           "regular expressions.",
9340e5e5a79SGreg Clayton             "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
935b9c1b51eSKate Stone         IOHandlerDelegateMultiline("",
936b9c1b51eSKate Stone                                    IOHandlerDelegate::Completion::LLDBCommand),
937b9c1b51eSKate Stone         m_options() {
938b9c1b51eSKate Stone     SetHelpLong(
939b9c1b51eSKate Stone         R"(
940b9c1b51eSKate Stone )"
941b9c1b51eSKate Stone         "This command allows the user to create powerful regular expression commands \
942ea671fbdSKate Stone with substitutions. The regular expressions and substitutions are specified \
943b9c1b51eSKate Stone using the regular expression substitution format of:"
944b9c1b51eSKate Stone         R"(
945ea671fbdSKate Stone 
946ea671fbdSKate Stone     s/<regex>/<subst>/
947ea671fbdSKate Stone 
948b9c1b51eSKate Stone )"
949b9c1b51eSKate Stone         "<regex> is a regular expression that can use parenthesis to capture regular \
950ea671fbdSKate Stone expression input and substitute the captured matches in the output using %1 \
951b9c1b51eSKate Stone for the first match, %2 for the second, and so on."
952b9c1b51eSKate Stone         R"(
953ea671fbdSKate Stone 
954b9c1b51eSKate Stone )"
955b9c1b51eSKate Stone         "The regular expressions can all be specified on the command line if more than \
956ea671fbdSKate Stone one argument is provided. If just the command name is provided on the command \
957ea671fbdSKate Stone line, then the regular expressions and substitutions can be entered on separate \
958b9c1b51eSKate Stone lines, followed by an empty line to terminate the command definition."
959b9c1b51eSKate Stone         R"(
960ea671fbdSKate Stone 
961ea671fbdSKate Stone EXAMPLES
962ea671fbdSKate Stone 
963b9c1b51eSKate Stone )"
964b9c1b51eSKate Stone         "The following example will define a regular expression command named 'f' that \
965ea671fbdSKate Stone will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if \
966b9c1b51eSKate Stone a number follows 'f':"
967b9c1b51eSKate Stone         R"(
968ea671fbdSKate Stone 
969b9c1b51eSKate Stone     (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/')");
970de164aaaSGreg Clayton   }
971de164aaaSGreg Clayton 
9726e3d8e7fSEugene Zelenko   ~CommandObjectCommandsAddRegex() override = default;
973de164aaaSGreg Clayton 
9745a988416SJim Ingham protected:
975b9c1b51eSKate Stone   void IOHandlerActivated(IOHandler &io_handler) override {
97644d93782SGreg Clayton     StreamFileSP output_sp(io_handler.GetOutputStreamFile());
977b9c1b51eSKate Stone     if (output_sp) {
978b9c1b51eSKate Stone       output_sp->PutCString("Enter one of more sed substitution commands in "
979b9c1b51eSKate Stone                             "the form: 's/<regex>/<subst>/'.\nTerminate the "
980b9c1b51eSKate Stone                             "substitution list with an empty line.\n");
98144d93782SGreg Clayton       output_sp->Flush();
98244d93782SGreg Clayton     }
98344d93782SGreg Clayton   }
98444d93782SGreg Clayton 
985b9c1b51eSKate Stone   void IOHandlerInputComplete(IOHandler &io_handler,
986b9c1b51eSKate Stone                               std::string &data) override {
98744d93782SGreg Clayton     io_handler.SetIsDone(true);
988b9c1b51eSKate Stone     if (m_regex_cmd_ap) {
98944d93782SGreg Clayton       StringList lines;
990b9c1b51eSKate Stone       if (lines.SplitIntoLines(data)) {
99144d93782SGreg Clayton         const size_t num_lines = lines.GetSize();
99244d93782SGreg Clayton         bool check_only = false;
993b9c1b51eSKate Stone         for (size_t i = 0; i < num_lines; ++i) {
99444d93782SGreg Clayton           llvm::StringRef bytes_strref(lines[i]);
99597206d57SZachary Turner           Status error = AppendRegexSubstitution(bytes_strref, check_only);
996b9c1b51eSKate Stone           if (error.Fail()) {
997b9c1b51eSKate Stone             if (!m_interpreter.GetDebugger()
998b9c1b51eSKate Stone                      .GetCommandInterpreter()
999b9c1b51eSKate Stone                      .GetBatchCommandMode()) {
1000b9c1b51eSKate Stone               StreamSP out_stream =
1001b9c1b51eSKate Stone                   m_interpreter.GetDebugger().GetAsyncOutputStream();
100244d93782SGreg Clayton               out_stream->Printf("error: %s\n", error.AsCString());
100344d93782SGreg Clayton             }
100444d93782SGreg Clayton           }
100544d93782SGreg Clayton         }
100644d93782SGreg Clayton       }
1007b9c1b51eSKate Stone       if (m_regex_cmd_ap->HasRegexEntries()) {
100844d93782SGreg Clayton         CommandObjectSP cmd_sp(m_regex_cmd_ap.release());
100944d93782SGreg Clayton         m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
101044d93782SGreg Clayton       }
101144d93782SGreg Clayton     }
101244d93782SGreg Clayton   }
101344d93782SGreg Clayton 
1014b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
10155a988416SJim Ingham     const size_t argc = command.GetArgumentCount();
1016b9c1b51eSKate Stone     if (argc == 0) {
1017b9c1b51eSKate Stone       result.AppendError("usage: 'command regex <command-name> "
1018b9c1b51eSKate Stone                          "[s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
10190e5e5a79SGreg Clayton       result.SetStatus(eReturnStatusFailed);
102011eb9c64SZachary Turner       return false;
102111eb9c64SZachary Turner     }
102211eb9c64SZachary Turner 
102397206d57SZachary Turner     Status error;
10244574a890SZachary Turner     auto name = command[0].ref;
10254574a890SZachary Turner     m_regex_cmd_ap = llvm::make_unique<CommandObjectRegexCommand>(
10264574a890SZachary Turner         m_interpreter, name, m_options.GetHelp(), m_options.GetSyntax(), 10, 0,
10274574a890SZachary Turner         true);
10280e5e5a79SGreg Clayton 
1029b9c1b51eSKate Stone     if (argc == 1) {
103044d93782SGreg Clayton       Debugger &debugger = m_interpreter.GetDebugger();
1031e30f11d9SKate Stone       bool color_prompt = debugger.GetUseColor();
103244d93782SGreg Clayton       const bool multiple_lines = true; // Get multiple lines
1033b9c1b51eSKate Stone       IOHandlerSP io_handler_sp(new IOHandlerEditline(
1034b9c1b51eSKate Stone           debugger, IOHandler::Type::Other,
103573d80faaSGreg Clayton           "lldb-regex",          // Name of input reader for history
1036514d8cd8SZachary Turner           llvm::StringRef("> "), // Prompt
1037514d8cd8SZachary Turner           llvm::StringRef(),     // Continuation prompt
1038b9c1b51eSKate Stone           multiple_lines, color_prompt,
1039f6913cd7SGreg Clayton           0, // Don't show line numbers
104044d93782SGreg Clayton           *this));
104144d93782SGreg Clayton 
1042b9c1b51eSKate Stone       if (io_handler_sp) {
104344d93782SGreg Clayton         debugger.PushIOHandler(io_handler_sp);
1044de164aaaSGreg Clayton         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1045de164aaaSGreg Clayton       }
1046b9c1b51eSKate Stone     } else {
104797d2c401SZachary Turner       for (auto &entry : command.entries().drop_front()) {
104844d93782SGreg Clayton         bool check_only = false;
104997d2c401SZachary Turner         error = AppendRegexSubstitution(entry.ref, check_only);
10500e5e5a79SGreg Clayton         if (error.Fail())
10510e5e5a79SGreg Clayton           break;
10520e5e5a79SGreg Clayton       }
10530e5e5a79SGreg Clayton 
1054b9c1b51eSKate Stone       if (error.Success()) {
10550e5e5a79SGreg Clayton         AddRegexCommandToInterpreter();
10560e5e5a79SGreg Clayton       }
10570e5e5a79SGreg Clayton     }
1058b9c1b51eSKate Stone     if (error.Fail()) {
10590e5e5a79SGreg Clayton       result.AppendError(error.AsCString());
1060de164aaaSGreg Clayton       result.SetStatus(eReturnStatusFailed);
1061de164aaaSGreg Clayton     }
10620e5e5a79SGreg Clayton 
1063de164aaaSGreg Clayton     return result.Succeeded();
1064de164aaaSGreg Clayton   }
1065de164aaaSGreg Clayton 
106697206d57SZachary Turner   Status AppendRegexSubstitution(const llvm::StringRef &regex_sed,
1067b9c1b51eSKate Stone                                  bool check_only) {
106897206d57SZachary Turner     Status error;
10690e5e5a79SGreg Clayton 
1070b9c1b51eSKate Stone     if (!m_regex_cmd_ap) {
1071b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1072b9c1b51eSKate Stone           "invalid regular expression command object for: '%.*s'",
1073b9c1b51eSKate Stone           (int)regex_sed.size(), regex_sed.data());
10740e5e5a79SGreg Clayton       return error;
1075de164aaaSGreg Clayton     }
10760e5e5a79SGreg Clayton 
10770e5e5a79SGreg Clayton     size_t regex_sed_size = regex_sed.size();
10780e5e5a79SGreg Clayton 
1079b9c1b51eSKate Stone     if (regex_sed_size <= 1) {
1080b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1081b9c1b51eSKate Stone           "regular expression substitution string is too short: '%.*s'",
1082b9c1b51eSKate Stone           (int)regex_sed.size(), regex_sed.data());
10830e5e5a79SGreg Clayton       return error;
10840e5e5a79SGreg Clayton     }
10850e5e5a79SGreg Clayton 
1086b9c1b51eSKate Stone     if (regex_sed[0] != 's') {
1087b9c1b51eSKate Stone       error.SetErrorStringWithFormat("regular expression substitution string "
1088b9c1b51eSKate Stone                                      "doesn't start with 's': '%.*s'",
1089b9c1b51eSKate Stone                                      (int)regex_sed.size(), regex_sed.data());
10900e5e5a79SGreg Clayton       return error;
10910e5e5a79SGreg Clayton     }
10920e5e5a79SGreg Clayton     const size_t first_separator_char_pos = 1;
109305097246SAdrian Prantl     // use the char that follows 's' as the regex separator character so we can
109405097246SAdrian Prantl     // have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
10950e5e5a79SGreg Clayton     const char separator_char = regex_sed[first_separator_char_pos];
1096b9c1b51eSKate Stone     const size_t second_separator_char_pos =
1097b9c1b51eSKate Stone         regex_sed.find(separator_char, first_separator_char_pos + 1);
10980e5e5a79SGreg Clayton 
1099b9c1b51eSKate Stone     if (second_separator_char_pos == std::string::npos) {
1100b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1101b9c1b51eSKate Stone           "missing second '%c' separator char after '%.*s' in '%.*s'",
11020e5e5a79SGreg Clayton           separator_char,
11030e5e5a79SGreg Clayton           (int)(regex_sed.size() - first_separator_char_pos - 1),
1104ea508635SGreg Clayton           regex_sed.data() + (first_separator_char_pos + 1),
1105b9c1b51eSKate Stone           (int)regex_sed.size(), regex_sed.data());
11060e5e5a79SGreg Clayton       return error;
11070e5e5a79SGreg Clayton     }
11080e5e5a79SGreg Clayton 
1109b9c1b51eSKate Stone     const size_t third_separator_char_pos =
1110b9c1b51eSKate Stone         regex_sed.find(separator_char, second_separator_char_pos + 1);
11110e5e5a79SGreg Clayton 
1112b9c1b51eSKate Stone     if (third_separator_char_pos == std::string::npos) {
1113b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1114b9c1b51eSKate Stone           "missing third '%c' separator char after '%.*s' in '%.*s'",
11150e5e5a79SGreg Clayton           separator_char,
11160e5e5a79SGreg Clayton           (int)(regex_sed.size() - second_separator_char_pos - 1),
1117ea508635SGreg Clayton           regex_sed.data() + (second_separator_char_pos + 1),
1118b9c1b51eSKate Stone           (int)regex_sed.size(), regex_sed.data());
11190e5e5a79SGreg Clayton       return error;
11200e5e5a79SGreg Clayton     }
11210e5e5a79SGreg Clayton 
1122b9c1b51eSKate Stone     if (third_separator_char_pos != regex_sed_size - 1) {
112305097246SAdrian Prantl       // Make sure that everything that follows the last regex separator char
1124b9c1b51eSKate Stone       if (regex_sed.find_first_not_of("\t\n\v\f\r ",
1125b9c1b51eSKate Stone                                       third_separator_char_pos + 1) !=
1126b9c1b51eSKate Stone           std::string::npos) {
1127b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
1128b9c1b51eSKate Stone             "extra data found after the '%.*s' regular expression substitution "
1129b9c1b51eSKate Stone             "string: '%.*s'",
1130b9c1b51eSKate Stone             (int)third_separator_char_pos + 1, regex_sed.data(),
11310e5e5a79SGreg Clayton             (int)(regex_sed.size() - third_separator_char_pos - 1),
11320e5e5a79SGreg Clayton             regex_sed.data() + (third_separator_char_pos + 1));
11330e5e5a79SGreg Clayton         return error;
11340e5e5a79SGreg Clayton       }
1135b9c1b51eSKate Stone     } else if (first_separator_char_pos + 1 == second_separator_char_pos) {
1136b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1137b9c1b51eSKate Stone           "<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
1138b9c1b51eSKate Stone           separator_char, separator_char, separator_char, (int)regex_sed.size(),
11390e5e5a79SGreg Clayton           regex_sed.data());
11400e5e5a79SGreg Clayton       return error;
1141b9c1b51eSKate Stone     } else if (second_separator_char_pos + 1 == third_separator_char_pos) {
1142b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1143b9c1b51eSKate Stone           "<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
1144b9c1b51eSKate Stone           separator_char, separator_char, separator_char, (int)regex_sed.size(),
11450e5e5a79SGreg Clayton           regex_sed.data());
11460e5e5a79SGreg Clayton       return error;
11470e5e5a79SGreg Clayton     }
114844d93782SGreg Clayton 
1149b9c1b51eSKate Stone     if (!check_only) {
1150b9c1b51eSKate Stone       std::string regex(regex_sed.substr(first_separator_char_pos + 1,
1151b9c1b51eSKate Stone                                          second_separator_char_pos -
1152b9c1b51eSKate Stone                                              first_separator_char_pos - 1));
1153b9c1b51eSKate Stone       std::string subst(regex_sed.substr(second_separator_char_pos + 1,
1154b9c1b51eSKate Stone                                          third_separator_char_pos -
1155b9c1b51eSKate Stone                                              second_separator_char_pos - 1));
1156b9c1b51eSKate Stone       m_regex_cmd_ap->AddRegexCommand(regex.c_str(), subst.c_str());
115744d93782SGreg Clayton     }
11580e5e5a79SGreg Clayton     return error;
1159de164aaaSGreg Clayton   }
1160de164aaaSGreg Clayton 
1161b9c1b51eSKate Stone   void AddRegexCommandToInterpreter() {
1162b9c1b51eSKate Stone     if (m_regex_cmd_ap) {
1163b9c1b51eSKate Stone       if (m_regex_cmd_ap->HasRegexEntries()) {
1164de164aaaSGreg Clayton         CommandObjectSP cmd_sp(m_regex_cmd_ap.release());
1165de164aaaSGreg Clayton         m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1166de164aaaSGreg Clayton       }
1167de164aaaSGreg Clayton     }
1168de164aaaSGreg Clayton   }
1169de164aaaSGreg Clayton 
1170de164aaaSGreg Clayton private:
11717b0992d9SGreg Clayton   std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_ap;
1172de164aaaSGreg Clayton 
1173b9c1b51eSKate Stone   class CommandOptions : public Options {
1174de164aaaSGreg Clayton   public:
1175b9c1b51eSKate Stone     CommandOptions() : Options() {}
1176de164aaaSGreg Clayton 
11776e3d8e7fSEugene Zelenko     ~CommandOptions() override = default;
1178de164aaaSGreg Clayton 
117997206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1180b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
118197206d57SZachary Turner       Status error;
11823bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
1183de164aaaSGreg Clayton 
1184b9c1b51eSKate Stone       switch (short_option) {
1185de164aaaSGreg Clayton       case 'h':
1186de164aaaSGreg Clayton         m_help.assign(option_arg);
1187de164aaaSGreg Clayton         break;
1188de164aaaSGreg Clayton       case 's':
1189de164aaaSGreg Clayton         m_syntax.assign(option_arg);
1190de164aaaSGreg Clayton         break;
1191de164aaaSGreg Clayton       default:
1192b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
1193b9c1b51eSKate Stone                                        short_option);
1194de164aaaSGreg Clayton         break;
1195de164aaaSGreg Clayton       }
1196de164aaaSGreg Clayton 
1197de164aaaSGreg Clayton       return error;
1198de164aaaSGreg Clayton     }
1199de164aaaSGreg Clayton 
1200b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
1201de164aaaSGreg Clayton       m_help.clear();
1202de164aaaSGreg Clayton       m_syntax.clear();
1203de164aaaSGreg Clayton     }
1204de164aaaSGreg Clayton 
12051f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
120670602439SZachary Turner       return llvm::makeArrayRef(g_regex_options);
12071f0f5b5bSZachary Turner     }
1208de164aaaSGreg Clayton 
120911eb9c64SZachary Turner     // TODO: Convert these functions to return StringRefs.
1210b9c1b51eSKate Stone     const char *GetHelp() {
12116e3d8e7fSEugene Zelenko       return (m_help.empty() ? nullptr : m_help.c_str());
1212de164aaaSGreg Clayton     }
12136e3d8e7fSEugene Zelenko 
1214b9c1b51eSKate Stone     const char *GetSyntax() {
12156e3d8e7fSEugene Zelenko       return (m_syntax.empty() ? nullptr : m_syntax.c_str());
1216de164aaaSGreg Clayton     }
12176e3d8e7fSEugene Zelenko 
1218de164aaaSGreg Clayton   protected:
12196e3d8e7fSEugene Zelenko     // Instance variables to hold the values for command options.
12206e3d8e7fSEugene Zelenko 
1221de164aaaSGreg Clayton     std::string m_help;
1222de164aaaSGreg Clayton     std::string m_syntax;
1223de164aaaSGreg Clayton   };
1224de164aaaSGreg Clayton 
1225b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
1226de164aaaSGreg Clayton 
12275a988416SJim Ingham   CommandOptions m_options;
1228de164aaaSGreg Clayton };
1229de164aaaSGreg Clayton 
1230b9c1b51eSKate Stone class CommandObjectPythonFunction : public CommandObjectRaw {
1231223383edSEnrico Granata public:
1232b9c1b51eSKate Stone   CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name,
1233b9c1b51eSKate Stone                               std::string funct, std::string help,
1234b9c1b51eSKate Stone                               ScriptedCommandSynchronicity synch)
1235a449698cSZachary Turner       : CommandObjectRaw(interpreter, name),
1236b9c1b51eSKate Stone         m_function_name(funct), m_synchro(synch), m_fetched_help_long(false) {
1237735152e3SEnrico Granata     if (!help.empty())
1238442f6530SZachary Turner       SetHelp(help);
1239b9c1b51eSKate Stone     else {
1240735152e3SEnrico Granata       StreamString stream;
1241735152e3SEnrico Granata       stream.Printf("For more information run 'help %s'", name.c_str());
1242c156427dSZachary Turner       SetHelp(stream.GetString());
1243735152e3SEnrico Granata     }
1244223383edSEnrico Granata   }
1245223383edSEnrico Granata 
12466e3d8e7fSEugene Zelenko   ~CommandObjectPythonFunction() override = default;
1247223383edSEnrico Granata 
1248b9c1b51eSKate Stone   bool IsRemovable() const override { return true; }
12495a988416SJim Ingham 
1250b9c1b51eSKate Stone   const std::string &GetFunctionName() { return m_function_name; }
12515a988416SJim Ingham 
1252b9c1b51eSKate Stone   ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
12535a988416SJim Ingham 
1254442f6530SZachary Turner   llvm::StringRef GetHelpLong() override {
1255442f6530SZachary Turner     if (m_fetched_help_long)
1256442f6530SZachary Turner       return CommandObjectRaw::GetHelpLong();
1257442f6530SZachary Turner 
1258fac939e9SEnrico Granata     ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1259442f6530SZachary Turner     if (!scripter)
1260442f6530SZachary Turner       return CommandObjectRaw::GetHelpLong();
1261442f6530SZachary Turner 
1262fac939e9SEnrico Granata     std::string docstring;
1263442f6530SZachary Turner     m_fetched_help_long =
1264442f6530SZachary Turner         scripter->GetDocumentationForItem(m_function_name.c_str(), docstring);
1265fac939e9SEnrico Granata     if (!docstring.empty())
1266442f6530SZachary Turner       SetHelpLong(docstring);
1267fac939e9SEnrico Granata     return CommandObjectRaw::GetHelpLong();
1268fac939e9SEnrico Granata   }
1269fac939e9SEnrico Granata 
12705a988416SJim Ingham protected:
12714d51a902SRaphael Isemann   bool DoExecute(llvm::StringRef raw_command_line,
1272b9c1b51eSKate Stone                  CommandReturnObject &result) override {
1273223383edSEnrico Granata     ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1274223383edSEnrico Granata 
127597206d57SZachary Turner     Status error;
1276223383edSEnrico Granata 
127770f11f88SJim Ingham     result.SetStatus(eReturnStatusInvalid);
127870f11f88SJim Ingham 
1279b9c1b51eSKate Stone     if (!scripter ||
1280b9c1b51eSKate Stone         !scripter->RunScriptBasedCommand(m_function_name.c_str(),
1281b9c1b51eSKate Stone                                          raw_command_line, m_synchro, result,
1282b9c1b51eSKate Stone                                          error, m_exe_ctx)) {
1283223383edSEnrico Granata       result.AppendError(error.AsCString());
1284223383edSEnrico Granata       result.SetStatus(eReturnStatusFailed);
1285b9c1b51eSKate Stone     } else {
128670f11f88SJim Ingham       // Don't change the status if the command already set it...
1287b9c1b51eSKate Stone       if (result.GetStatus() == eReturnStatusInvalid) {
1288c156427dSZachary Turner         if (result.GetOutputData().empty())
1289223383edSEnrico Granata           result.SetStatus(eReturnStatusSuccessFinishNoResult);
129070f11f88SJim Ingham         else
129170f11f88SJim Ingham           result.SetStatus(eReturnStatusSuccessFinishResult);
129270f11f88SJim Ingham       }
129370f11f88SJim Ingham     }
1294223383edSEnrico Granata 
1295223383edSEnrico Granata     return result.Succeeded();
1296223383edSEnrico Granata   }
1297223383edSEnrico Granata 
12986e3d8e7fSEugene Zelenko private:
12996e3d8e7fSEugene Zelenko   std::string m_function_name;
13006e3d8e7fSEugene Zelenko   ScriptedCommandSynchronicity m_synchro;
13016e3d8e7fSEugene Zelenko   bool m_fetched_help_long;
1302223383edSEnrico Granata };
1303223383edSEnrico Granata 
1304b9c1b51eSKate Stone class CommandObjectScriptingObject : public CommandObjectRaw {
13059fe00e52SEnrico Granata public:
13069fe00e52SEnrico Granata   CommandObjectScriptingObject(CommandInterpreter &interpreter,
13079fe00e52SEnrico Granata                                std::string name,
13080641ca1aSZachary Turner                                StructuredData::GenericSP cmd_obj_sp,
1309b9c1b51eSKate Stone                                ScriptedCommandSynchronicity synch)
1310a449698cSZachary Turner       : CommandObjectRaw(interpreter, name),
1311b9c1b51eSKate Stone         m_cmd_obj_sp(cmd_obj_sp), m_synchro(synch), m_fetched_help_short(false),
1312b9c1b51eSKate Stone         m_fetched_help_long(false) {
13139fe00e52SEnrico Granata     StreamString stream;
13149fe00e52SEnrico Granata     stream.Printf("For more information run 'help %s'", name.c_str());
1315c156427dSZachary Turner     SetHelp(stream.GetString());
1316e87764f2SEnrico Granata     if (ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter())
1317e87764f2SEnrico Granata       GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
13189fe00e52SEnrico Granata   }
13199fe00e52SEnrico Granata 
13206e3d8e7fSEugene Zelenko   ~CommandObjectScriptingObject() override = default;
13219fe00e52SEnrico Granata 
1322b9c1b51eSKate Stone   bool IsRemovable() const override { return true; }
13239fe00e52SEnrico Granata 
1324b9c1b51eSKate Stone   StructuredData::GenericSP GetImplementingObject() { return m_cmd_obj_sp; }
13259fe00e52SEnrico Granata 
1326b9c1b51eSKate Stone   ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
13279fe00e52SEnrico Granata 
1328442f6530SZachary Turner   llvm::StringRef GetHelp() override {
1329442f6530SZachary Turner     if (m_fetched_help_short)
1330442f6530SZachary Turner       return CommandObjectRaw::GetHelp();
13316f79bb2dSEnrico Granata     ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1332442f6530SZachary Turner     if (!scripter)
1333442f6530SZachary Turner       return CommandObjectRaw::GetHelp();
13346f79bb2dSEnrico Granata     std::string docstring;
1335b9c1b51eSKate Stone     m_fetched_help_short =
1336b9c1b51eSKate Stone         scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring);
13376f79bb2dSEnrico Granata     if (!docstring.empty())
1338442f6530SZachary Turner       SetHelp(docstring);
1339442f6530SZachary Turner 
13406f79bb2dSEnrico Granata     return CommandObjectRaw::GetHelp();
13416f79bb2dSEnrico Granata   }
13426f79bb2dSEnrico Granata 
1343442f6530SZachary Turner   llvm::StringRef GetHelpLong() override {
1344442f6530SZachary Turner     if (m_fetched_help_long)
1345442f6530SZachary Turner       return CommandObjectRaw::GetHelpLong();
1346442f6530SZachary Turner 
13476f79bb2dSEnrico Granata     ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
1348442f6530SZachary Turner     if (!scripter)
1349442f6530SZachary Turner       return CommandObjectRaw::GetHelpLong();
1350442f6530SZachary Turner 
13516f79bb2dSEnrico Granata     std::string docstring;
1352b9c1b51eSKate Stone     m_fetched_help_long =
1353b9c1b51eSKate Stone         scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring);
13546f79bb2dSEnrico Granata     if (!docstring.empty())
1355442f6530SZachary Turner       SetHelpLong(docstring);
13569fe00e52SEnrico Granata     return CommandObjectRaw::GetHelpLong();
13579fe00e52SEnrico Granata   }
13589fe00e52SEnrico Granata 
13599fe00e52SEnrico Granata protected:
13604d51a902SRaphael Isemann   bool DoExecute(llvm::StringRef raw_command_line,
1361b9c1b51eSKate Stone                  CommandReturnObject &result) override {
13629fe00e52SEnrico Granata     ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
13639fe00e52SEnrico Granata 
136497206d57SZachary Turner     Status error;
13659fe00e52SEnrico Granata 
13669fe00e52SEnrico Granata     result.SetStatus(eReturnStatusInvalid);
13679fe00e52SEnrico Granata 
1368b9c1b51eSKate Stone     if (!scripter ||
1369b9c1b51eSKate Stone         !scripter->RunScriptBasedCommand(m_cmd_obj_sp, raw_command_line,
1370b9c1b51eSKate Stone                                          m_synchro, result, error, m_exe_ctx)) {
13719fe00e52SEnrico Granata       result.AppendError(error.AsCString());
13729fe00e52SEnrico Granata       result.SetStatus(eReturnStatusFailed);
1373b9c1b51eSKate Stone     } else {
13749fe00e52SEnrico Granata       // Don't change the status if the command already set it...
1375b9c1b51eSKate Stone       if (result.GetStatus() == eReturnStatusInvalid) {
1376c156427dSZachary Turner         if (result.GetOutputData().empty())
13779fe00e52SEnrico Granata           result.SetStatus(eReturnStatusSuccessFinishNoResult);
13789fe00e52SEnrico Granata         else
13799fe00e52SEnrico Granata           result.SetStatus(eReturnStatusSuccessFinishResult);
13809fe00e52SEnrico Granata       }
13819fe00e52SEnrico Granata     }
13829fe00e52SEnrico Granata 
13839fe00e52SEnrico Granata     return result.Succeeded();
13849fe00e52SEnrico Granata   }
13859fe00e52SEnrico Granata 
13866e3d8e7fSEugene Zelenko private:
13876e3d8e7fSEugene Zelenko   StructuredData::GenericSP m_cmd_obj_sp;
13886e3d8e7fSEugene Zelenko   ScriptedCommandSynchronicity m_synchro;
13896e3d8e7fSEugene Zelenko   bool m_fetched_help_short : 1;
13906e3d8e7fSEugene Zelenko   bool m_fetched_help_long : 1;
13919fe00e52SEnrico Granata };
13929fe00e52SEnrico Granata 
1393a9dbf432SEnrico Granata //-------------------------------------------------------------------------
1394a9dbf432SEnrico Granata // CommandObjectCommandsScriptImport
1395a9dbf432SEnrico Granata //-------------------------------------------------------------------------
1396a9dbf432SEnrico Granata 
13971f0f5b5bSZachary Turner OptionDefinition g_script_import_options[] = {
13981f0f5b5bSZachary Turner     // clang-format off
13991f0f5b5bSZachary Turner   { LLDB_OPT_SET_1, false, "allow-reload", 'r', OptionParser::eNoArgument, nullptr, nullptr, 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." },
14001f0f5b5bSZachary Turner     // clang-format on
14011f0f5b5bSZachary Turner };
14021f0f5b5bSZachary Turner 
1403b9c1b51eSKate Stone class CommandObjectCommandsScriptImport : public CommandObjectParsed {
14045a988416SJim Ingham public:
1405b9c1b51eSKate Stone   CommandObjectCommandsScriptImport(CommandInterpreter &interpreter)
1406b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command script import",
1407b9c1b51eSKate Stone                             "Import a scripting module in LLDB.", nullptr),
1408b9c1b51eSKate Stone         m_options() {
14095a988416SJim Ingham     CommandArgumentEntry arg1;
14105a988416SJim Ingham     CommandArgumentData cmd_arg;
14115a988416SJim Ingham 
14125a988416SJim Ingham     // Define the first (and only) variant of this arg.
14135a988416SJim Ingham     cmd_arg.arg_type = eArgTypeFilename;
14143b00e35bSEnrico Granata     cmd_arg.arg_repetition = eArgRepeatPlus;
14155a988416SJim Ingham 
1416b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
1417b9c1b51eSKate Stone     // argument entry.
14185a988416SJim Ingham     arg1.push_back(cmd_arg);
14195a988416SJim Ingham 
14205a988416SJim Ingham     // Push the data for the first argument into the m_arguments vector.
14215a988416SJim Ingham     m_arguments.push_back(arg1);
14225a988416SJim Ingham   }
14235a988416SJim Ingham 
14246e3d8e7fSEugene Zelenko   ~CommandObjectCommandsScriptImport() override = default;
14255a988416SJim Ingham 
14262443bbd4SRaphael Isemann   int HandleArgumentCompletion(
14272443bbd4SRaphael Isemann       CompletionRequest &request,
14282443bbd4SRaphael Isemann       OptionElementVector &opt_element_vector) override {
1429b9c1b51eSKate Stone     CommandCompletions::InvokeCommonCompletionCallbacks(
1430b9c1b51eSKate Stone         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
1431*a2e76c0bSRaphael Isemann         request, nullptr);
14322443bbd4SRaphael Isemann     return request.GetMatches().GetSize();
14335a988416SJim Ingham   }
14345a988416SJim Ingham 
1435b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
14365a988416SJim Ingham 
14375a988416SJim Ingham protected:
1438b9c1b51eSKate Stone   class CommandOptions : public Options {
14390a305db7SEnrico Granata   public:
1440b9c1b51eSKate Stone     CommandOptions() : Options() {}
14410a305db7SEnrico Granata 
14426e3d8e7fSEugene Zelenko     ~CommandOptions() override = default;
14430a305db7SEnrico Granata 
144497206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1445b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
144697206d57SZachary Turner       Status error;
14473bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
14480a305db7SEnrico Granata 
1449b9c1b51eSKate Stone       switch (short_option) {
14500a305db7SEnrico Granata       case 'r':
14510a305db7SEnrico Granata         m_allow_reload = true;
14520a305db7SEnrico Granata         break;
14530a305db7SEnrico Granata       default:
1454b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
1455b9c1b51eSKate Stone                                        short_option);
14560a305db7SEnrico Granata         break;
14570a305db7SEnrico Granata       }
14580a305db7SEnrico Granata 
14590a305db7SEnrico Granata       return error;
14600a305db7SEnrico Granata     }
14610a305db7SEnrico Granata 
1462b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
1463e0c70f1bSEnrico Granata       m_allow_reload = true;
14640a305db7SEnrico Granata     }
14650a305db7SEnrico Granata 
14661f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
146770602439SZachary Turner       return llvm::makeArrayRef(g_script_import_options);
14681f0f5b5bSZachary Turner     }
14690a305db7SEnrico Granata 
14700a305db7SEnrico Granata     // Instance variables to hold the values for command options.
14710a305db7SEnrico Granata 
14720a305db7SEnrico Granata     bool m_allow_reload;
14730a305db7SEnrico Granata   };
14740a305db7SEnrico Granata 
1475b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1476b9c1b51eSKate Stone     if (m_interpreter.GetDebugger().GetScriptLanguage() !=
1477b9c1b51eSKate Stone         lldb::eScriptLanguagePython) {
1478b9c1b51eSKate Stone       result.AppendError("only scripting language supported for module "
1479b9c1b51eSKate Stone                          "importing is currently Python");
1480a9dbf432SEnrico Granata       result.SetStatus(eReturnStatusFailed);
1481a9dbf432SEnrico Granata       return false;
1482a9dbf432SEnrico Granata     }
1483a9dbf432SEnrico Granata 
148411eb9c64SZachary Turner     if (command.empty()) {
14853b00e35bSEnrico Granata       result.AppendError("command script import needs one or more arguments");
1486a9dbf432SEnrico Granata       result.SetStatus(eReturnStatusFailed);
1487a9dbf432SEnrico Granata       return false;
1488a9dbf432SEnrico Granata     }
1489a9dbf432SEnrico Granata 
149011eb9c64SZachary Turner     for (auto &entry : command.entries()) {
149197206d57SZachary Turner       Status error;
1492a9dbf432SEnrico Granata 
1493c9d645d3SGreg Clayton       const bool init_session = true;
1494b9c1b51eSKate Stone       // FIXME: this is necessary because CommandObject::CheckRequirements()
149511eb9c64SZachary Turner       // assumes that commands won't ever be recursively invoked, but it's
149611eb9c64SZachary Turner       // actually possible to craft a Python script that does other "command
149705097246SAdrian Prantl       // script imports" in __lldb_init_module the real fix is to have
149805097246SAdrian Prantl       // recursive commands possible with a CommandInvocation object separate
149905097246SAdrian Prantl       // from the CommandObject itself, so that recursive command invocations
150005097246SAdrian Prantl       // won't stomp on each other (wrt to execution contents, options, and
150105097246SAdrian Prantl       // more)
1502078551c7SEnrico Granata       m_exe_ctx.Clear();
1503b9c1b51eSKate Stone       if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule(
150411eb9c64SZachary Turner               entry.c_str(), m_options.m_allow_reload, init_session, error)) {
1505a9dbf432SEnrico Granata         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1506b9c1b51eSKate Stone       } else {
1507b9c1b51eSKate Stone         result.AppendErrorWithFormat("module importing failed: %s",
1508b9c1b51eSKate Stone                                      error.AsCString());
1509a9dbf432SEnrico Granata         result.SetStatus(eReturnStatusFailed);
1510a9dbf432SEnrico Granata       }
15113b00e35bSEnrico Granata     }
1512a9dbf432SEnrico Granata 
1513a9dbf432SEnrico Granata     return result.Succeeded();
1514a9dbf432SEnrico Granata   }
15150a305db7SEnrico Granata 
15165a988416SJim Ingham   CommandOptions m_options;
1517a9dbf432SEnrico Granata };
1518223383edSEnrico Granata 
1519223383edSEnrico Granata //-------------------------------------------------------------------------
1520223383edSEnrico Granata // CommandObjectCommandsScriptAdd
1521223383edSEnrico Granata //-------------------------------------------------------------------------
1522223383edSEnrico Granata 
15231f0f5b5bSZachary Turner static OptionEnumValueElement g_script_synchro_type[] = {
15241f0f5b5bSZachary Turner     {eScriptedCommandSynchronicitySynchronous, "synchronous",
15251f0f5b5bSZachary Turner      "Run synchronous"},
15261f0f5b5bSZachary Turner     {eScriptedCommandSynchronicityAsynchronous, "asynchronous",
15271f0f5b5bSZachary Turner      "Run asynchronous"},
15281f0f5b5bSZachary Turner     {eScriptedCommandSynchronicityCurrentValue, "current",
15291f0f5b5bSZachary Turner      "Do not alter current setting"},
15301f0f5b5bSZachary Turner     {0, nullptr, nullptr}};
15311f0f5b5bSZachary Turner 
15321f0f5b5bSZachary Turner static OptionDefinition g_script_add_options[] = {
15331f0f5b5bSZachary Turner     // clang-format off
15341f0f5b5bSZachary Turner   { LLDB_OPT_SET_1,   false, "function",      'f', OptionParser::eRequiredArgument, nullptr, nullptr,               0, eArgTypePythonFunction,               "Name of the Python function to bind to this command name." },
15351f0f5b5bSZachary Turner   { LLDB_OPT_SET_2,   false, "class",         'c', OptionParser::eRequiredArgument, nullptr, nullptr,               0, eArgTypePythonClass,                  "Name of the Python class to bind to this command name." },
15361f0f5b5bSZachary Turner   { LLDB_OPT_SET_1,   false, "help"  ,        'h', OptionParser::eRequiredArgument, nullptr, nullptr,               0, eArgTypeHelpText,                     "The help text to display for this command." },
15371f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL, false, "synchronicity", 's', OptionParser::eRequiredArgument, nullptr, g_script_synchro_type, 0, eArgTypeScriptedCommandSynchronicity, "Set the synchronicity of this command's executions with regard to LLDB event system." },
15381f0f5b5bSZachary Turner     // clang-format on
15391f0f5b5bSZachary Turner };
15401f0f5b5bSZachary Turner 
1541b9c1b51eSKate Stone class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
1542b9c1b51eSKate Stone                                        public IOHandlerDelegateMultiline {
15435a988416SJim Ingham public:
1544b9c1b51eSKate Stone   CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter)
1545b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command script add",
15465a988416SJim Ingham                             "Add a scripted function as an LLDB command.",
15476e3d8e7fSEugene Zelenko                             nullptr),
1548b9c1b51eSKate Stone         IOHandlerDelegateMultiline("DONE"), m_options() {
15495a988416SJim Ingham     CommandArgumentEntry arg1;
15505a988416SJim Ingham     CommandArgumentData cmd_arg;
15515a988416SJim Ingham 
15525a988416SJim Ingham     // Define the first (and only) variant of this arg.
15535a988416SJim Ingham     cmd_arg.arg_type = eArgTypeCommandName;
15545a988416SJim Ingham     cmd_arg.arg_repetition = eArgRepeatPlain;
15555a988416SJim Ingham 
1556b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
1557b9c1b51eSKate Stone     // argument entry.
15585a988416SJim Ingham     arg1.push_back(cmd_arg);
15595a988416SJim Ingham 
15605a988416SJim Ingham     // Push the data for the first argument into the m_arguments vector.
15615a988416SJim Ingham     m_arguments.push_back(arg1);
15625a988416SJim Ingham   }
15635a988416SJim Ingham 
15646e3d8e7fSEugene Zelenko   ~CommandObjectCommandsScriptAdd() override = default;
15655a988416SJim Ingham 
1566b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
15675a988416SJim Ingham 
15685a988416SJim Ingham protected:
1569b9c1b51eSKate Stone   class CommandOptions : public Options {
1570223383edSEnrico Granata   public:
1571b9c1b51eSKate Stone     CommandOptions()
1572b9c1b51eSKate Stone         : Options(), m_class_name(), m_funct_name(), m_short_help(),
1573b9c1b51eSKate Stone           m_synchronicity(eScriptedCommandSynchronicitySynchronous) {}
1574223383edSEnrico Granata 
15756e3d8e7fSEugene Zelenko     ~CommandOptions() override = default;
1576223383edSEnrico Granata 
157797206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1578b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
157997206d57SZachary Turner       Status error;
15803bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
1581223383edSEnrico Granata 
1582b9c1b51eSKate Stone       switch (short_option) {
1583223383edSEnrico Granata       case 'f':
1584fe11483bSZachary Turner         if (!option_arg.empty())
1585fe11483bSZachary Turner           m_funct_name = option_arg;
1586735152e3SEnrico Granata         break;
15879fe00e52SEnrico Granata       case 'c':
1588fe11483bSZachary Turner         if (!option_arg.empty())
1589fe11483bSZachary Turner           m_class_name = option_arg;
15909fe00e52SEnrico Granata         break;
1591735152e3SEnrico Granata       case 'h':
1592fe11483bSZachary Turner         if (!option_arg.empty())
1593fe11483bSZachary Turner           m_short_help = option_arg;
1594223383edSEnrico Granata         break;
15950a305db7SEnrico Granata       case 's':
1596b9c1b51eSKate Stone         m_synchronicity =
159747cbf4a0SPavel Labath             (ScriptedCommandSynchronicity)OptionArgParser::ToOptionEnum(
1598fe11483bSZachary Turner                 option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
15990a305db7SEnrico Granata         if (!error.Success())
1600b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
1601fe11483bSZachary Turner               "unrecognized value for synchronicity '%s'",
1602fe11483bSZachary Turner               option_arg.str().c_str());
16030a305db7SEnrico Granata         break;
1604223383edSEnrico Granata       default:
1605b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
1606b9c1b51eSKate Stone                                        short_option);
1607223383edSEnrico Granata         break;
1608223383edSEnrico Granata       }
1609223383edSEnrico Granata 
1610223383edSEnrico Granata       return error;
1611223383edSEnrico Granata     }
1612223383edSEnrico Granata 
1613b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
16149fe00e52SEnrico Granata       m_class_name.clear();
1615735152e3SEnrico Granata       m_funct_name.clear();
1616735152e3SEnrico Granata       m_short_help.clear();
161744d93782SGreg Clayton       m_synchronicity = eScriptedCommandSynchronicitySynchronous;
1618223383edSEnrico Granata     }
1619223383edSEnrico Granata 
16201f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
162170602439SZachary Turner       return llvm::makeArrayRef(g_script_add_options);
16221f0f5b5bSZachary Turner     }
1623223383edSEnrico Granata 
1624223383edSEnrico Granata     // Instance variables to hold the values for command options.
1625223383edSEnrico Granata 
16269fe00e52SEnrico Granata     std::string m_class_name;
1627223383edSEnrico Granata     std::string m_funct_name;
1628735152e3SEnrico Granata     std::string m_short_help;
162944d93782SGreg Clayton     ScriptedCommandSynchronicity m_synchronicity;
1630223383edSEnrico Granata   };
1631223383edSEnrico Granata 
1632b9c1b51eSKate Stone   void IOHandlerActivated(IOHandler &io_handler) override {
163344d93782SGreg Clayton     StreamFileSP output_sp(io_handler.GetOutputStreamFile());
1634b9c1b51eSKate Stone     if (output_sp) {
163544d93782SGreg Clayton       output_sp->PutCString(g_python_command_instructions);
163644d93782SGreg Clayton       output_sp->Flush();
1637223383edSEnrico Granata     }
1638223383edSEnrico Granata   }
1639223383edSEnrico Granata 
1640b9c1b51eSKate Stone   void IOHandlerInputComplete(IOHandler &io_handler,
1641b9c1b51eSKate Stone                               std::string &data) override {
164244d93782SGreg Clayton     StreamFileSP error_sp = io_handler.GetErrorStreamFile();
164344d93782SGreg Clayton 
164444d93782SGreg Clayton     ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter();
1645b9c1b51eSKate Stone     if (interpreter) {
164644d93782SGreg Clayton 
164744d93782SGreg Clayton       StringList lines;
164844d93782SGreg Clayton       lines.SplitIntoLines(data);
1649b9c1b51eSKate Stone       if (lines.GetSize() > 0) {
1650a73b7df7SEnrico Granata         std::string funct_name_str;
1651b9c1b51eSKate Stone         if (interpreter->GenerateScriptAliasFunction(lines, funct_name_str)) {
1652b9c1b51eSKate Stone           if (funct_name_str.empty()) {
1653b9c1b51eSKate Stone             error_sp->Printf("error: unable to obtain a function name, didn't "
1654b9c1b51eSKate Stone                              "add python command.\n");
165544d93782SGreg Clayton             error_sp->Flush();
1656b9c1b51eSKate Stone           } else {
1657223383edSEnrico Granata             // everything should be fine now, let's add this alias
1658223383edSEnrico Granata 
1659b9c1b51eSKate Stone             CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(
1660771ef6d4SMalcolm Parsons                 m_interpreter, m_cmd_name, funct_name_str, m_short_help,
166144d93782SGreg Clayton                 m_synchronicity));
1662223383edSEnrico Granata 
1663b9c1b51eSKate Stone             if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp,
1664b9c1b51eSKate Stone                                               true)) {
1665b9c1b51eSKate Stone               error_sp->Printf("error: unable to add selected command, didn't "
1666b9c1b51eSKate Stone                                "add python command.\n");
166744d93782SGreg Clayton               error_sp->Flush();
1668223383edSEnrico Granata             }
1669223383edSEnrico Granata           }
1670b9c1b51eSKate Stone         } else {
1671b9c1b51eSKate Stone           error_sp->Printf(
1672b9c1b51eSKate Stone               "error: unable to create function, didn't add python command.\n");
167344d93782SGreg Clayton           error_sp->Flush();
167444d93782SGreg Clayton         }
1675b9c1b51eSKate Stone       } else {
167644d93782SGreg Clayton         error_sp->Printf("error: empty function, didn't add python command.\n");
167744d93782SGreg Clayton         error_sp->Flush();
167844d93782SGreg Clayton       }
1679b9c1b51eSKate Stone     } else {
1680b9c1b51eSKate Stone       error_sp->Printf(
1681b9c1b51eSKate Stone           "error: script interpreter missing, didn't add python command.\n");
168244d93782SGreg Clayton       error_sp->Flush();
168344d93782SGreg Clayton     }
168444d93782SGreg Clayton 
168544d93782SGreg Clayton     io_handler.SetIsDone(true);
168644d93782SGreg Clayton   }
1687223383edSEnrico Granata 
16885a988416SJim Ingham protected:
1689b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1690b9c1b51eSKate Stone     if (m_interpreter.GetDebugger().GetScriptLanguage() !=
1691b9c1b51eSKate Stone         lldb::eScriptLanguagePython) {
1692b9c1b51eSKate Stone       result.AppendError("only scripting language supported for scripted "
1693b9c1b51eSKate Stone                          "commands is currently Python");
169499f0b8f9SEnrico Granata       result.SetStatus(eReturnStatusFailed);
169599f0b8f9SEnrico Granata       return false;
169699f0b8f9SEnrico Granata     }
169799f0b8f9SEnrico Granata 
169811eb9c64SZachary Turner     if (command.GetArgumentCount() != 1) {
1699223383edSEnrico Granata       result.AppendError("'command script add' requires one argument");
1700223383edSEnrico Granata       result.SetStatus(eReturnStatusFailed);
1701223383edSEnrico Granata       return false;
1702223383edSEnrico Granata     }
1703223383edSEnrico Granata 
1704735152e3SEnrico Granata     // Store the options in case we get multi-line input
17054574a890SZachary Turner     m_cmd_name = command[0].ref;
1706735152e3SEnrico Granata     m_short_help.assign(m_options.m_short_help);
170744d93782SGreg Clayton     m_synchronicity = m_options.m_synchronicity;
1708223383edSEnrico Granata 
1709b9c1b51eSKate Stone     if (m_options.m_class_name.empty()) {
1710b9c1b51eSKate Stone       if (m_options.m_funct_name.empty()) {
1711b9c1b51eSKate Stone         m_interpreter.GetPythonCommandsFromIOHandler(
1712b9c1b51eSKate Stone             "     ",  // Prompt
171344d93782SGreg Clayton             *this,    // IOHandlerDelegate
171444d93782SGreg Clayton             true,     // Run IOHandler in async mode
1715b9c1b51eSKate Stone             nullptr); // Baton for the "io_handler" that will be passed back
1716b9c1b51eSKate Stone                       // into our IOHandlerDelegate functions
1717b9c1b51eSKate Stone       } else {
1718b9c1b51eSKate Stone         CommandObjectSP new_cmd(new CommandObjectPythonFunction(
1719b9c1b51eSKate Stone             m_interpreter, m_cmd_name, m_options.m_funct_name,
1720b9c1b51eSKate Stone             m_options.m_short_help, m_synchronicity));
1721b9c1b51eSKate Stone         if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) {
1722223383edSEnrico Granata           result.SetStatus(eReturnStatusSuccessFinishNoResult);
1723b9c1b51eSKate Stone         } else {
1724223383edSEnrico Granata           result.AppendError("cannot add command");
1725223383edSEnrico Granata           result.SetStatus(eReturnStatusFailed);
1726223383edSEnrico Granata         }
1727223383edSEnrico Granata       }
1728b9c1b51eSKate Stone     } else {
1729b9c1b51eSKate Stone       ScriptInterpreter *interpreter =
1730b9c1b51eSKate Stone           GetCommandInterpreter().GetScriptInterpreter();
1731b9c1b51eSKate Stone       if (!interpreter) {
17329fe00e52SEnrico Granata         result.AppendError("cannot find ScriptInterpreter");
17339fe00e52SEnrico Granata         result.SetStatus(eReturnStatusFailed);
17349fe00e52SEnrico Granata         return false;
17359fe00e52SEnrico Granata       }
17369fe00e52SEnrico Granata 
1737b9c1b51eSKate Stone       auto cmd_obj_sp = interpreter->CreateScriptCommandObject(
1738b9c1b51eSKate Stone           m_options.m_class_name.c_str());
1739b9c1b51eSKate Stone       if (!cmd_obj_sp) {
17409fe00e52SEnrico Granata         result.AppendError("cannot create helper object");
17419fe00e52SEnrico Granata         result.SetStatus(eReturnStatusFailed);
17429fe00e52SEnrico Granata         return false;
17439fe00e52SEnrico Granata       }
17449fe00e52SEnrico Granata 
1745b9c1b51eSKate Stone       CommandObjectSP new_cmd(new CommandObjectScriptingObject(
1746b9c1b51eSKate Stone           m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity));
1747b9c1b51eSKate Stone       if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) {
17489fe00e52SEnrico Granata         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1749b9c1b51eSKate Stone       } else {
17509fe00e52SEnrico Granata         result.AppendError("cannot add command");
17519fe00e52SEnrico Granata         result.SetStatus(eReturnStatusFailed);
17529fe00e52SEnrico Granata       }
17539fe00e52SEnrico Granata     }
1754223383edSEnrico Granata 
1755223383edSEnrico Granata     return result.Succeeded();
1756223383edSEnrico Granata   }
17575a988416SJim Ingham 
17585a988416SJim Ingham   CommandOptions m_options;
175944d93782SGreg Clayton   std::string m_cmd_name;
1760735152e3SEnrico Granata   std::string m_short_help;
176144d93782SGreg Clayton   ScriptedCommandSynchronicity m_synchronicity;
1762223383edSEnrico Granata };
1763223383edSEnrico Granata 
1764223383edSEnrico Granata //-------------------------------------------------------------------------
1765223383edSEnrico Granata // CommandObjectCommandsScriptList
1766223383edSEnrico Granata //-------------------------------------------------------------------------
1767223383edSEnrico Granata 
1768b9c1b51eSKate Stone class CommandObjectCommandsScriptList : public CommandObjectParsed {
1769223383edSEnrico Granata public:
1770b9c1b51eSKate Stone   CommandObjectCommandsScriptList(CommandInterpreter &interpreter)
1771b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command script list",
1772b9c1b51eSKate Stone                             "List defined scripted commands.", nullptr) {}
1773223383edSEnrico Granata 
17746e3d8e7fSEugene Zelenko   ~CommandObjectCommandsScriptList() override = default;
1775223383edSEnrico Granata 
1776b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1777b9c1b51eSKate Stone     m_interpreter.GetHelp(result, CommandInterpreter::eCommandTypesUserDef);
1778223383edSEnrico Granata 
1779223383edSEnrico Granata     result.SetStatus(eReturnStatusSuccessFinishResult);
1780223383edSEnrico Granata 
1781223383edSEnrico Granata     return true;
1782223383edSEnrico Granata   }
1783223383edSEnrico Granata };
1784223383edSEnrico Granata 
1785223383edSEnrico Granata //-------------------------------------------------------------------------
1786223383edSEnrico Granata // CommandObjectCommandsScriptClear
1787223383edSEnrico Granata //-------------------------------------------------------------------------
1788223383edSEnrico Granata 
1789b9c1b51eSKate Stone class CommandObjectCommandsScriptClear : public CommandObjectParsed {
1790223383edSEnrico Granata public:
1791b9c1b51eSKate Stone   CommandObjectCommandsScriptClear(CommandInterpreter &interpreter)
1792b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command script clear",
1793b9c1b51eSKate Stone                             "Delete all scripted commands.", nullptr) {}
1794223383edSEnrico Granata 
17956e3d8e7fSEugene Zelenko   ~CommandObjectCommandsScriptClear() override = default;
1796223383edSEnrico Granata 
17975a988416SJim Ingham protected:
1798b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1799223383edSEnrico Granata     m_interpreter.RemoveAllUser();
1800223383edSEnrico Granata 
1801223383edSEnrico Granata     result.SetStatus(eReturnStatusSuccessFinishResult);
1802223383edSEnrico Granata 
1803223383edSEnrico Granata     return true;
1804223383edSEnrico Granata   }
1805223383edSEnrico Granata };
1806223383edSEnrico Granata 
1807223383edSEnrico Granata //-------------------------------------------------------------------------
1808223383edSEnrico Granata // CommandObjectCommandsScriptDelete
1809223383edSEnrico Granata //-------------------------------------------------------------------------
1810223383edSEnrico Granata 
1811b9c1b51eSKate Stone class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
1812223383edSEnrico Granata public:
1813b9c1b51eSKate Stone   CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter)
1814b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command script delete",
1815b9c1b51eSKate Stone                             "Delete a scripted command.", nullptr) {
1816223383edSEnrico Granata     CommandArgumentEntry arg1;
1817223383edSEnrico Granata     CommandArgumentData cmd_arg;
1818223383edSEnrico Granata 
1819223383edSEnrico Granata     // Define the first (and only) variant of this arg.
1820223383edSEnrico Granata     cmd_arg.arg_type = eArgTypeCommandName;
1821223383edSEnrico Granata     cmd_arg.arg_repetition = eArgRepeatPlain;
1822223383edSEnrico Granata 
1823b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
1824b9c1b51eSKate Stone     // argument entry.
1825223383edSEnrico Granata     arg1.push_back(cmd_arg);
1826223383edSEnrico Granata 
1827223383edSEnrico Granata     // Push the data for the first argument into the m_arguments vector.
1828223383edSEnrico Granata     m_arguments.push_back(arg1);
1829223383edSEnrico Granata   }
1830223383edSEnrico Granata 
18316e3d8e7fSEugene Zelenko   ~CommandObjectCommandsScriptDelete() override = default;
1832223383edSEnrico Granata 
18335a988416SJim Ingham protected:
1834b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1835223383edSEnrico Granata 
183611eb9c64SZachary Turner     if (command.GetArgumentCount() != 1) {
1837223383edSEnrico Granata       result.AppendError("'command script delete' requires one argument");
1838223383edSEnrico Granata       result.SetStatus(eReturnStatusFailed);
1839223383edSEnrico Granata       return false;
1840223383edSEnrico Granata     }
1841223383edSEnrico Granata 
18424574a890SZachary Turner     auto cmd_name = command[0].ref;
1843223383edSEnrico Granata 
18444574a890SZachary Turner     if (cmd_name.empty() || !m_interpreter.HasUserCommands() ||
18454574a890SZachary Turner         !m_interpreter.UserCommandExists(cmd_name)) {
1846867e7d17SZachary Turner       result.AppendErrorWithFormat("command %s not found", command[0].c_str());
1847223383edSEnrico Granata       result.SetStatus(eReturnStatusFailed);
18484574a890SZachary Turner       return false;
1849223383edSEnrico Granata     }
1850223383edSEnrico Granata 
18514574a890SZachary Turner     m_interpreter.RemoveUser(cmd_name);
18524574a890SZachary Turner     result.SetStatus(eReturnStatusSuccessFinishResult);
18534574a890SZachary Turner     return true;
1854223383edSEnrico Granata   }
1855223383edSEnrico Granata };
1856223383edSEnrico Granata 
1857223383edSEnrico Granata #pragma mark CommandObjectMultiwordCommandsScript
1858223383edSEnrico Granata 
1859223383edSEnrico Granata //-------------------------------------------------------------------------
1860223383edSEnrico Granata // CommandObjectMultiwordCommandsScript
1861223383edSEnrico Granata //-------------------------------------------------------------------------
1862223383edSEnrico Granata 
1863b9c1b51eSKate Stone class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword {
1864223383edSEnrico Granata public:
18657428a18cSKate Stone   CommandObjectMultiwordCommandsScript(CommandInterpreter &interpreter)
1866b9c1b51eSKate Stone       : CommandObjectMultiword(
1867b9c1b51eSKate Stone             interpreter, "command script", "Commands for managing custom "
1868b9c1b51eSKate Stone                                            "commands implemented by "
1869b9c1b51eSKate Stone                                            "interpreter scripts.",
1870b9c1b51eSKate Stone             "command script <subcommand> [<subcommand-options>]") {
1871b9c1b51eSKate Stone     LoadSubCommand("add", CommandObjectSP(
1872b9c1b51eSKate Stone                               new CommandObjectCommandsScriptAdd(interpreter)));
1873b9c1b51eSKate Stone     LoadSubCommand(
1874b9c1b51eSKate Stone         "delete",
1875b9c1b51eSKate Stone         CommandObjectSP(new CommandObjectCommandsScriptDelete(interpreter)));
1876b9c1b51eSKate Stone     LoadSubCommand(
1877b9c1b51eSKate Stone         "clear",
1878b9c1b51eSKate Stone         CommandObjectSP(new CommandObjectCommandsScriptClear(interpreter)));
1879b9c1b51eSKate Stone     LoadSubCommand("list", CommandObjectSP(new CommandObjectCommandsScriptList(
1880b9c1b51eSKate Stone                                interpreter)));
1881b9c1b51eSKate Stone     LoadSubCommand(
1882b9c1b51eSKate Stone         "import",
1883b9c1b51eSKate Stone         CommandObjectSP(new CommandObjectCommandsScriptImport(interpreter)));
1884223383edSEnrico Granata   }
1885223383edSEnrico Granata 
18866e3d8e7fSEugene Zelenko   ~CommandObjectMultiwordCommandsScript() override = default;
1887223383edSEnrico Granata };
1888223383edSEnrico Granata 
1889ebc09c36SJim Ingham #pragma mark CommandObjectMultiwordCommands
1890ebc09c36SJim Ingham 
1891ebc09c36SJim Ingham //-------------------------------------------------------------------------
1892ebc09c36SJim Ingham // CommandObjectMultiwordCommands
1893ebc09c36SJim Ingham //-------------------------------------------------------------------------
1894ebc09c36SJim Ingham 
1895b9c1b51eSKate Stone CommandObjectMultiwordCommands::CommandObjectMultiwordCommands(
1896b9c1b51eSKate Stone     CommandInterpreter &interpreter)
1897b9c1b51eSKate Stone     : CommandObjectMultiword(interpreter, "command",
1898b9c1b51eSKate Stone                              "Commands for managing custom LLDB commands.",
1899b9c1b51eSKate Stone                              "command <subcommand> [<subcommand-options>]") {
1900b9c1b51eSKate Stone   LoadSubCommand("source",
1901b9c1b51eSKate Stone                  CommandObjectSP(new CommandObjectCommandsSource(interpreter)));
1902b9c1b51eSKate Stone   LoadSubCommand("alias",
1903b9c1b51eSKate Stone                  CommandObjectSP(new CommandObjectCommandsAlias(interpreter)));
1904b9c1b51eSKate Stone   LoadSubCommand("unalias", CommandObjectSP(
1905b9c1b51eSKate Stone                                 new CommandObjectCommandsUnalias(interpreter)));
1906b9c1b51eSKate Stone   LoadSubCommand("delete",
1907b9c1b51eSKate Stone                  CommandObjectSP(new CommandObjectCommandsDelete(interpreter)));
1908b9c1b51eSKate Stone   LoadSubCommand(
1909b9c1b51eSKate Stone       "regex", CommandObjectSP(new CommandObjectCommandsAddRegex(interpreter)));
1910b9c1b51eSKate Stone   LoadSubCommand("history", CommandObjectSP(
1911b9c1b51eSKate Stone                                 new CommandObjectCommandsHistory(interpreter)));
1912b9c1b51eSKate Stone   LoadSubCommand(
1913b9c1b51eSKate Stone       "script",
1914b9c1b51eSKate Stone       CommandObjectSP(new CommandObjectMultiwordCommandsScript(interpreter)));
1915ebc09c36SJim Ingham }
1916ebc09c36SJim Ingham 
19176e3d8e7fSEugene Zelenko CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands() = default;
1918