180814287SRaphael Isemann //===-- CommandObjectCommands.cpp -----------------------------------------===//
2ebc09c36SJim Ingham //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ebc09c36SJim Ingham //
7ebc09c36SJim Ingham //===----------------------------------------------------------------------===//
8ebc09c36SJim Ingham 
90e5e5a79SGreg Clayton #include "llvm/ADT/StringRef.h"
100e5e5a79SGreg Clayton 
116e3d8e7fSEugene Zelenko #include "CommandObjectCommands.h"
1246d4aa21SEnrico Granata #include "CommandObjectHelp.h"
13ebc09c36SJim Ingham #include "lldb/Core/Debugger.h"
1444d93782SGreg Clayton #include "lldb/Core/IOHandler.h"
157594f14fSEnrico Granata #include "lldb/Interpreter/CommandHistory.h"
16ebc09c36SJim Ingham #include "lldb/Interpreter/CommandInterpreter.h"
17de164aaaSGreg Clayton #include "lldb/Interpreter/CommandObjectRegexCommand.h"
18ebc09c36SJim Ingham #include "lldb/Interpreter/CommandReturnObject.h"
1947cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h"
20012d4fcaSEnrico Granata #include "lldb/Interpreter/OptionValueBoolean.h"
2145d0e238SEnrico Granata #include "lldb/Interpreter/OptionValueString.h"
227594f14fSEnrico Granata #include "lldb/Interpreter/OptionValueUInt64.h"
23ebc09c36SJim Ingham #include "lldb/Interpreter/Options.h"
2499f0b8f9SEnrico Granata #include "lldb/Interpreter/ScriptInterpreter.h"
25145d95c9SPavel Labath #include "lldb/Utility/Args.h"
26573ab909SZachary Turner #include "lldb/Utility/StringList.h"
27ebc09c36SJim Ingham 
28ebc09c36SJim Ingham using namespace lldb;
29ebc09c36SJim Ingham using namespace lldb_private;
30ebc09c36SJim Ingham 
31ebc09c36SJim Ingham // CommandObjectCommandsSource
32ebc09c36SJim Ingham 
3364becc11SRaphael Isemann #define LLDB_OPTIONS_history
3464becc11SRaphael Isemann #include "CommandOptions.inc"
351f0f5b5bSZachary Turner 
36b9c1b51eSKate Stone class CommandObjectCommandsHistory : public CommandObjectParsed {
375a988416SJim Ingham public:
38b9c1b51eSKate Stone   CommandObjectCommandsHistory(CommandInterpreter &interpreter)
39b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command history",
40aab5be05SJim Ingham                             "Dump the history of commands in this session.\n"
41aab5be05SJim Ingham                             "Commands in the history list can be run again "
42aab5be05SJim Ingham                             "using \"!<INDEX>\".   \"!-<OFFSET>\" will re-run "
43aab5be05SJim Ingham                             "the command that is <OFFSET> commands from the end"
44aab5be05SJim Ingham                             " of the list (counting the current command).",
456e3d8e7fSEugene Zelenko                             nullptr),
46b9c1b51eSKate Stone         m_options() {}
475a988416SJim Ingham 
486e3d8e7fSEugene Zelenko   ~CommandObjectCommandsHistory() override = default;
495a988416SJim Ingham 
50b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
515a988416SJim Ingham 
525a988416SJim Ingham protected:
53b9c1b51eSKate Stone   class CommandOptions : public Options {
54a5a97ebeSJim Ingham   public:
55b9c1b51eSKate Stone     CommandOptions()
56b9c1b51eSKate Stone         : Options(), m_start_idx(0), m_stop_idx(0), m_count(0), m_clear(false) {
57a5a97ebeSJim Ingham     }
58a5a97ebeSJim Ingham 
596e3d8e7fSEugene Zelenko     ~CommandOptions() override = default;
60a5a97ebeSJim Ingham 
6197206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
62b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
6397206d57SZachary Turner       Status error;
643bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
65a5a97ebeSJim Ingham 
66b9c1b51eSKate Stone       switch (short_option) {
67a5a97ebeSJim Ingham       case 'c':
68fe11483bSZachary Turner         error = m_count.SetValueFromString(option_arg, eVarSetOperationAssign);
69a5a97ebeSJim Ingham         break;
70a5a97ebeSJim Ingham       case 's':
71fe11483bSZachary Turner         if (option_arg == "end") {
727594f14fSEnrico Granata           m_start_idx.SetCurrentValue(UINT64_MAX);
737594f14fSEnrico Granata           m_start_idx.SetOptionWasSet();
74b9c1b51eSKate Stone         } else
75fe11483bSZachary Turner           error = m_start_idx.SetValueFromString(option_arg,
76b9c1b51eSKate Stone                                                  eVarSetOperationAssign);
777594f14fSEnrico Granata         break;
787594f14fSEnrico Granata       case 'e':
79fe11483bSZachary Turner         error =
80fe11483bSZachary Turner             m_stop_idx.SetValueFromString(option_arg, eVarSetOperationAssign);
817594f14fSEnrico Granata         break;
8263123b64SEnrico Granata       case 'C':
8363123b64SEnrico Granata         m_clear.SetCurrentValue(true);
8463123b64SEnrico Granata         m_clear.SetOptionWasSet();
85a5a97ebeSJim Ingham         break;
86a5a97ebeSJim Ingham       default:
8736162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
88a5a97ebeSJim Ingham       }
89a5a97ebeSJim Ingham 
90a5a97ebeSJim Ingham       return error;
91a5a97ebeSJim Ingham     }
92a5a97ebeSJim Ingham 
93b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
947594f14fSEnrico Granata       m_start_idx.Clear();
957594f14fSEnrico Granata       m_stop_idx.Clear();
967594f14fSEnrico Granata       m_count.Clear();
9763123b64SEnrico Granata       m_clear.Clear();
98a5a97ebeSJim Ingham     }
99a5a97ebeSJim Ingham 
1001f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
10170602439SZachary Turner       return llvm::makeArrayRef(g_history_options);
1021f0f5b5bSZachary Turner     }
103a5a97ebeSJim Ingham 
104a5a97ebeSJim Ingham     // Instance variables to hold the values for command options.
105a5a97ebeSJim Ingham 
1067594f14fSEnrico Granata     OptionValueUInt64 m_start_idx;
1077594f14fSEnrico Granata     OptionValueUInt64 m_stop_idx;
1087594f14fSEnrico Granata     OptionValueUInt64 m_count;
10963123b64SEnrico Granata     OptionValueBoolean m_clear;
110a5a97ebeSJim Ingham   };
111a5a97ebeSJim Ingham 
112b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
113b9c1b51eSKate Stone     if (m_options.m_clear.GetCurrentValue() &&
114b9c1b51eSKate Stone         m_options.m_clear.OptionWasSet()) {
1157594f14fSEnrico Granata       m_interpreter.GetCommandHistory().Clear();
1167594f14fSEnrico Granata       result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
117b9c1b51eSKate Stone     } else {
118b9c1b51eSKate Stone       if (m_options.m_start_idx.OptionWasSet() &&
119b9c1b51eSKate Stone           m_options.m_stop_idx.OptionWasSet() &&
120b9c1b51eSKate Stone           m_options.m_count.OptionWasSet()) {
121b9c1b51eSKate Stone         result.AppendError("--count, --start-index and --end-index cannot be "
122b9c1b51eSKate Stone                            "all specified in the same invocation");
1237594f14fSEnrico Granata         result.SetStatus(lldb::eReturnStatusFailed);
124b9c1b51eSKate Stone       } else {
125b9c1b51eSKate Stone         std::pair<bool, uint64_t> start_idx(
126b9c1b51eSKate Stone             m_options.m_start_idx.OptionWasSet(),
127b9c1b51eSKate Stone             m_options.m_start_idx.GetCurrentValue());
128b9c1b51eSKate Stone         std::pair<bool, uint64_t> stop_idx(
129b9c1b51eSKate Stone             m_options.m_stop_idx.OptionWasSet(),
130b9c1b51eSKate Stone             m_options.m_stop_idx.GetCurrentValue());
131b9c1b51eSKate Stone         std::pair<bool, uint64_t> count(m_options.m_count.OptionWasSet(),
132b9c1b51eSKate Stone                                         m_options.m_count.GetCurrentValue());
133a5a97ebeSJim Ingham 
1347594f14fSEnrico Granata         const CommandHistory &history(m_interpreter.GetCommandHistory());
1357594f14fSEnrico Granata 
136b9c1b51eSKate Stone         if (start_idx.first && start_idx.second == UINT64_MAX) {
137b9c1b51eSKate Stone           if (count.first) {
1387594f14fSEnrico Granata             start_idx.second = history.GetSize() - count.second;
1397594f14fSEnrico Granata             stop_idx.second = history.GetSize() - 1;
140b9c1b51eSKate Stone           } else if (stop_idx.first) {
1417594f14fSEnrico Granata             start_idx.second = stop_idx.second;
1427594f14fSEnrico Granata             stop_idx.second = history.GetSize() - 1;
143b9c1b51eSKate Stone           } else {
1447594f14fSEnrico Granata             start_idx.second = 0;
1457594f14fSEnrico Granata             stop_idx.second = history.GetSize() - 1;
1467594f14fSEnrico Granata           }
147b9c1b51eSKate Stone         } else {
148b9c1b51eSKate Stone           if (!start_idx.first && !stop_idx.first && !count.first) {
1497594f14fSEnrico Granata             start_idx.second = 0;
1507594f14fSEnrico Granata             stop_idx.second = history.GetSize() - 1;
151b9c1b51eSKate Stone           } else if (start_idx.first) {
152b9c1b51eSKate Stone             if (count.first) {
1537594f14fSEnrico Granata               stop_idx.second = start_idx.second + count.second - 1;
154b9c1b51eSKate Stone             } else if (!stop_idx.first) {
1557594f14fSEnrico Granata               stop_idx.second = history.GetSize() - 1;
1567594f14fSEnrico Granata             }
157b9c1b51eSKate Stone           } else if (stop_idx.first) {
158b9c1b51eSKate Stone             if (count.first) {
1597594f14fSEnrico Granata               if (stop_idx.second >= count.second)
1607594f14fSEnrico Granata                 start_idx.second = stop_idx.second - count.second + 1;
1617594f14fSEnrico Granata               else
1627594f14fSEnrico Granata                 start_idx.second = 0;
1637594f14fSEnrico Granata             }
164b9c1b51eSKate Stone           } else /* if (count.first) */
1657594f14fSEnrico Granata           {
1667594f14fSEnrico Granata             start_idx.second = 0;
1677594f14fSEnrico Granata             stop_idx.second = count.second - 1;
1687594f14fSEnrico Granata           }
1697594f14fSEnrico Granata         }
170b9c1b51eSKate Stone         history.Dump(result.GetOutputStream(), start_idx.second,
171b9c1b51eSKate Stone                      stop_idx.second);
1727594f14fSEnrico Granata       }
1737594f14fSEnrico Granata     }
174a5a97ebeSJim Ingham     return result.Succeeded();
175a5a97ebeSJim Ingham   }
1765a988416SJim Ingham 
1775a988416SJim Ingham   CommandOptions m_options;
178a5a97ebeSJim Ingham };
179a5a97ebeSJim Ingham 
180a5a97ebeSJim Ingham // CommandObjectCommandsSource
181a5a97ebeSJim Ingham 
18264becc11SRaphael Isemann #define LLDB_OPTIONS_source
18364becc11SRaphael Isemann #include "CommandOptions.inc"
1841f0f5b5bSZachary Turner 
185b9c1b51eSKate Stone class CommandObjectCommandsSource : public CommandObjectParsed {
1865a988416SJim Ingham public:
1877428a18cSKate Stone   CommandObjectCommandsSource(CommandInterpreter &interpreter)
188b9c1b51eSKate Stone       : CommandObjectParsed(
189b9c1b51eSKate Stone             interpreter, "command source",
190b9c1b51eSKate Stone             "Read and execute LLDB commands from the file <filename>.",
1916e3d8e7fSEugene Zelenko             nullptr),
192b9c1b51eSKate Stone         m_options() {
1935a988416SJim Ingham     CommandArgumentEntry arg;
1945a988416SJim Ingham     CommandArgumentData file_arg;
1955a988416SJim Ingham 
1965a988416SJim Ingham     // Define the first (and only) variant of this arg.
1975a988416SJim Ingham     file_arg.arg_type = eArgTypeFilename;
1985a988416SJim Ingham     file_arg.arg_repetition = eArgRepeatPlain;
1995a988416SJim Ingham 
200b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
201b9c1b51eSKate Stone     // argument entry.
2025a988416SJim Ingham     arg.push_back(file_arg);
2035a988416SJim Ingham 
2045a988416SJim Ingham     // Push the data for the first argument into the m_arguments vector.
2055a988416SJim Ingham     m_arguments.push_back(arg);
2065a988416SJim Ingham   }
2075a988416SJim Ingham 
2086e3d8e7fSEugene Zelenko   ~CommandObjectCommandsSource() override = default;
2095a988416SJim Ingham 
210b9c1b51eSKate Stone   const char *GetRepeatCommand(Args &current_command_args,
211b9c1b51eSKate Stone                                uint32_t index) override {
2125a988416SJim Ingham     return "";
2135a988416SJim Ingham   }
2145a988416SJim Ingham 
215ae34ed2cSRaphael Isemann   void
216ae34ed2cSRaphael Isemann   HandleArgumentCompletion(CompletionRequest &request,
2172443bbd4SRaphael Isemann                            OptionElementVector &opt_element_vector) override {
218b9c1b51eSKate Stone     CommandCompletions::InvokeCommonCompletionCallbacks(
219b9c1b51eSKate Stone         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
220a2e76c0bSRaphael Isemann         request, nullptr);
2215a988416SJim Ingham   }
2225a988416SJim Ingham 
223b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
2245a988416SJim Ingham 
2255a988416SJim Ingham protected:
226b9c1b51eSKate Stone   class CommandOptions : public Options {
227e16c50a1SJim Ingham   public:
228b9c1b51eSKate Stone     CommandOptions()
229b9c1b51eSKate Stone         : Options(), m_stop_on_error(true), m_silent_run(false),
230b9c1b51eSKate Stone           m_stop_on_continue(true) {}
231e16c50a1SJim Ingham 
2326e3d8e7fSEugene Zelenko     ~CommandOptions() override = default;
233e16c50a1SJim Ingham 
23497206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
235b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
23697206d57SZachary Turner       Status error;
2373bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
238e16c50a1SJim Ingham 
239b9c1b51eSKate Stone       switch (short_option) {
240e16c50a1SJim Ingham       case 'e':
241fe11483bSZachary Turner         error = m_stop_on_error.SetValueFromString(option_arg);
242e16c50a1SJim Ingham         break;
243340b0309SGreg Clayton 
244e16c50a1SJim Ingham       case 'c':
245fe11483bSZachary Turner         error = m_stop_on_continue.SetValueFromString(option_arg);
246e16c50a1SJim Ingham         break;
247340b0309SGreg Clayton 
24860986174SMichael Sartain       case 's':
249fe11483bSZachary Turner         error = m_silent_run.SetValueFromString(option_arg);
25060986174SMichael Sartain         break;
251340b0309SGreg Clayton 
252e16c50a1SJim Ingham       default:
25336162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
254e16c50a1SJim Ingham       }
255e16c50a1SJim Ingham 
256e16c50a1SJim Ingham       return error;
257e16c50a1SJim Ingham     }
258e16c50a1SJim Ingham 
259b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
260012d4fcaSEnrico Granata       m_stop_on_error.Clear();
261340b0309SGreg Clayton       m_silent_run.Clear();
262340b0309SGreg Clayton       m_stop_on_continue.Clear();
263e16c50a1SJim Ingham     }
264e16c50a1SJim Ingham 
2651f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
26670602439SZachary Turner       return llvm::makeArrayRef(g_source_options);
2671f0f5b5bSZachary Turner     }
268e16c50a1SJim Ingham 
269e16c50a1SJim Ingham     // Instance variables to hold the values for command options.
270e16c50a1SJim Ingham 
271012d4fcaSEnrico Granata     OptionValueBoolean m_stop_on_error;
272340b0309SGreg Clayton     OptionValueBoolean m_silent_run;
273340b0309SGreg Clayton     OptionValueBoolean m_stop_on_continue;
274e16c50a1SJim Ingham   };
275e16c50a1SJim Ingham 
276b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
2774574a890SZachary Turner     if (command.GetArgumentCount() != 1) {
2784574a890SZachary Turner       result.AppendErrorWithFormat(
2794574a890SZachary Turner           "'%s' takes exactly one executable filename argument.\n",
2804574a890SZachary Turner           GetCommandName().str().c_str());
2814574a890SZachary Turner       result.SetStatus(eReturnStatusFailed);
2824574a890SZachary Turner       return false;
2834574a890SZachary Turner     }
284ebc09c36SJim Ingham 
2850d9a201eSRaphael Isemann     FileSpec cmd_file(command[0].ref());
2868f3be7a3SJonas Devlieghere     FileSystem::Instance().Resolve(cmd_file);
2876e3d8e7fSEugene Zelenko     ExecutionContext *exe_ctx = nullptr; // Just use the default context.
288ebc09c36SJim Ingham 
289340b0309SGreg Clayton     // If any options were set, then use them
290340b0309SGreg Clayton     if (m_options.m_stop_on_error.OptionWasSet() ||
291340b0309SGreg Clayton         m_options.m_silent_run.OptionWasSet() ||
292b9c1b51eSKate Stone         m_options.m_stop_on_continue.OptionWasSet()) {
293340b0309SGreg Clayton       // Use user set settings
29426c7bf93SJim Ingham       CommandInterpreterRunOptions options;
2951c19b74cSJonas Devlieghere 
2961c19b74cSJonas Devlieghere       if (m_options.m_stop_on_continue.OptionWasSet())
2971c19b74cSJonas Devlieghere         options.SetStopOnContinue(
2981c19b74cSJonas Devlieghere             m_options.m_stop_on_continue.GetCurrentValue());
2991c19b74cSJonas Devlieghere 
3001c19b74cSJonas Devlieghere       if (m_options.m_stop_on_error.OptionWasSet())
30126c7bf93SJim Ingham         options.SetStopOnError(m_options.m_stop_on_error.GetCurrentValue());
302c678ed77SStefan Granitz 
303c678ed77SStefan Granitz       // Individual silent setting is override for global command echo settings.
304c678ed77SStefan Granitz       if (m_options.m_silent_run.GetCurrentValue()) {
305c678ed77SStefan Granitz         options.SetSilent(true);
306c678ed77SStefan Granitz       } else {
307c678ed77SStefan Granitz         options.SetPrintResults(true);
308c0b48ab6SJonas Devlieghere         options.SetPrintErrors(true);
309c678ed77SStefan Granitz         options.SetEchoCommands(m_interpreter.GetEchoCommands());
310c678ed77SStefan Granitz         options.SetEchoCommentCommands(m_interpreter.GetEchoCommentCommands());
311c678ed77SStefan Granitz       }
31226c7bf93SJim Ingham 
3134574a890SZachary Turner       m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, result);
314b9c1b51eSKate Stone     } else {
31505097246SAdrian Prantl       // No options were set, inherit any settings from nested "command source"
31605097246SAdrian Prantl       // commands, or set to sane default settings...
31726c7bf93SJim Ingham       CommandInterpreterRunOptions options;
3184574a890SZachary Turner       m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, result);
319ebc09c36SJim Ingham     }
320ebc09c36SJim Ingham     return result.Succeeded();
321ebc09c36SJim Ingham   }
3226e3d8e7fSEugene Zelenko 
3235a988416SJim Ingham   CommandOptions m_options;
324ebc09c36SJim Ingham };
325ebc09c36SJim Ingham 
326ebc09c36SJim Ingham #pragma mark CommandObjectCommandsAlias
327ebc09c36SJim Ingham // CommandObjectCommandsAlias
328ebc09c36SJim Ingham 
32964becc11SRaphael Isemann #define LLDB_OPTIONS_alias
33064becc11SRaphael Isemann #include "CommandOptions.inc"
3311f0f5b5bSZachary Turner 
332b9c1b51eSKate Stone static const char *g_python_command_instructions =
333b9c1b51eSKate Stone     "Enter your Python command(s). Type 'DONE' to end.\n"
334be93a35aSEnrico Granata     "You must define a Python function with this signature:\n"
33544d93782SGreg Clayton     "def my_command_impl(debugger, args, result, internal_dict):\n";
336be93a35aSEnrico Granata 
337b9c1b51eSKate Stone class CommandObjectCommandsAlias : public CommandObjectRaw {
33845d0e238SEnrico Granata protected:
339b9c1b51eSKate Stone   class CommandOptions : public OptionGroup {
340ebc09c36SJim Ingham   public:
341b9c1b51eSKate Stone     CommandOptions() : OptionGroup(), m_help(), m_long_help() {}
34245d0e238SEnrico Granata 
34345d0e238SEnrico Granata     ~CommandOptions() override = default;
34445d0e238SEnrico Granata 
3451f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
34670602439SZachary Turner       return llvm::makeArrayRef(g_alias_options);
3471f0f5b5bSZachary Turner     }
34845d0e238SEnrico Granata 
34997206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_value,
350b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
35197206d57SZachary Turner       Status error;
35245d0e238SEnrico Granata 
3531f0f5b5bSZachary Turner       const int short_option = GetDefinitions()[option_idx].short_option;
3548cef4b0bSZachary Turner       std::string option_str(option_value);
35545d0e238SEnrico Granata 
356b9c1b51eSKate Stone       switch (short_option) {
35745d0e238SEnrico Granata       case 'h':
3588cef4b0bSZachary Turner         m_help.SetCurrentValue(option_str);
35945d0e238SEnrico Granata         m_help.SetOptionWasSet();
36045d0e238SEnrico Granata         break;
36145d0e238SEnrico Granata 
36245d0e238SEnrico Granata       case 'H':
3638cef4b0bSZachary Turner         m_long_help.SetCurrentValue(option_str);
36445d0e238SEnrico Granata         m_long_help.SetOptionWasSet();
36545d0e238SEnrico Granata         break;
36645d0e238SEnrico Granata 
36745d0e238SEnrico Granata       default:
36836162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
36945d0e238SEnrico Granata       }
37045d0e238SEnrico Granata 
37145d0e238SEnrico Granata       return error;
37245d0e238SEnrico Granata     }
37345d0e238SEnrico Granata 
374b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
37545d0e238SEnrico Granata       m_help.Clear();
37645d0e238SEnrico Granata       m_long_help.Clear();
37745d0e238SEnrico Granata     }
37845d0e238SEnrico Granata 
37945d0e238SEnrico Granata     OptionValueString m_help;
38045d0e238SEnrico Granata     OptionValueString m_long_help;
38145d0e238SEnrico Granata   };
38245d0e238SEnrico Granata 
38345d0e238SEnrico Granata   OptionGroupOptions m_option_group;
38445d0e238SEnrico Granata   CommandOptions m_command_options;
38545d0e238SEnrico Granata 
38645d0e238SEnrico Granata public:
387b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
38845d0e238SEnrico Granata 
3897428a18cSKate Stone   CommandObjectCommandsAlias(CommandInterpreter &interpreter)
390b9c1b51eSKate Stone       : CommandObjectRaw(
391b9c1b51eSKate Stone             interpreter, "command alias",
392a449698cSZachary Turner             "Define a custom command in terms of an existing command."),
393b9c1b51eSKate Stone         m_option_group(), m_command_options() {
39445d0e238SEnrico Granata     m_option_group.Append(&m_command_options);
39545d0e238SEnrico Granata     m_option_group.Finalize();
39645d0e238SEnrico Granata 
397ebc09c36SJim Ingham     SetHelpLong(
398ea671fbdSKate Stone         "'alias' allows the user to create a short-cut or abbreviation for long \
399ea671fbdSKate Stone commands, multi-word commands, and commands that take particular options.  \
400b9c1b51eSKate Stone Below are some simple examples of how one might use the 'alias' command:"
401b9c1b51eSKate Stone         R"(
402ea671fbdSKate Stone 
403ea671fbdSKate Stone (lldb) command alias sc script
404ea671fbdSKate Stone 
405ea671fbdSKate Stone     Creates the abbreviation 'sc' for the 'script' command.
406ea671fbdSKate Stone 
407ea671fbdSKate Stone (lldb) command alias bp breakpoint
408ea671fbdSKate Stone 
409b9c1b51eSKate Stone )"
410b9c1b51eSKate Stone         "    Creates the abbreviation 'bp' for the 'breakpoint' command.  Since \
411ea671fbdSKate Stone breakpoint commands are two-word commands, the user would still need to \
412b9c1b51eSKate Stone enter the second word after 'bp', e.g. 'bp enable' or 'bp delete'."
413b9c1b51eSKate Stone         R"(
414ea671fbdSKate Stone 
415ea671fbdSKate Stone (lldb) command alias bpl breakpoint list
416ea671fbdSKate Stone 
417ea671fbdSKate Stone     Creates the abbreviation 'bpl' for the two-word command 'breakpoint list'.
418ea671fbdSKate Stone 
419b9c1b51eSKate Stone )"
420b9c1b51eSKate Stone         "An alias can include some options for the command, with the values either \
421ea671fbdSKate Stone filled in at the time the alias is created, or specified as positional \
422ea671fbdSKate Stone arguments, to be filled in when the alias is invoked.  The following example \
423b9c1b51eSKate Stone shows how to create aliases with options:"
424b9c1b51eSKate Stone         R"(
425ea671fbdSKate Stone 
426ea671fbdSKate Stone (lldb) command alias bfl breakpoint set -f %1 -l %2
427ea671fbdSKate Stone 
428b9c1b51eSKate Stone )"
429b9c1b51eSKate Stone         "    Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \
430ea671fbdSKate Stone options already part of the alias.  So if the user wants to set a breakpoint \
431ea671fbdSKate Stone by file and line without explicitly having to use the -f and -l options, the \
432ea671fbdSKate Stone user can now use 'bfl' instead.  The '%1' and '%2' are positional placeholders \
433ea671fbdSKate Stone for the actual arguments that will be passed when the alias command is used.  \
434ea671fbdSKate Stone The number in the placeholder refers to the position/order the actual value \
435ea671fbdSKate Stone occupies when the alias is used.  All the occurrences of '%1' in the alias \
436ea671fbdSKate Stone will be replaced with the first argument, all the occurrences of '%2' in the \
437ea671fbdSKate Stone alias will be replaced with the second argument, and so on.  This also allows \
438ea671fbdSKate Stone actual arguments to be used multiple times within an alias (see 'process \
439b9c1b51eSKate Stone launch' example below)."
440b9c1b51eSKate Stone         R"(
441ea671fbdSKate Stone 
442b9c1b51eSKate Stone )"
443b9c1b51eSKate Stone         "Note: the positional arguments must substitute as whole words in the resultant \
444ea671fbdSKate Stone command, so you can't at present do something like this to append the file extension \
445b9c1b51eSKate Stone \".cpp\":"
446b9c1b51eSKate Stone         R"(
447ea671fbdSKate Stone 
448ea671fbdSKate Stone (lldb) command alias bcppfl breakpoint set -f %1.cpp -l %2
449ea671fbdSKate Stone 
450b9c1b51eSKate Stone )"
451b9c1b51eSKate Stone         "For more complex aliasing, use the \"command regex\" command instead.  In the \
452ea671fbdSKate Stone 'bfl' case above, the actual file value will be filled in with the first argument \
453ea671fbdSKate Stone following 'bfl' and the actual line number value will be filled in with the second \
454b9c1b51eSKate Stone argument.  The user would use this alias as follows:"
455b9c1b51eSKate Stone         R"(
456ea671fbdSKate Stone 
457ea671fbdSKate Stone (lldb) command alias bfl breakpoint set -f %1 -l %2
458ea671fbdSKate Stone (lldb) bfl my-file.c 137
459ea671fbdSKate Stone 
460ea671fbdSKate Stone This would be the same as if the user had entered 'breakpoint set -f my-file.c -l 137'.
461ea671fbdSKate Stone 
462ea671fbdSKate Stone Another example:
463ea671fbdSKate Stone 
464ea671fbdSKate Stone (lldb) command alias pltty process launch -s -o %1 -e %1
465ea671fbdSKate Stone (lldb) pltty /dev/tty0
466ea671fbdSKate Stone 
467ea671fbdSKate Stone     Interpreted as 'process launch -s -o /dev/tty0 -e /dev/tty0'
468ea671fbdSKate Stone 
469b9c1b51eSKate Stone )"
470b9c1b51eSKate Stone         "If the user always wanted to pass the same value to a particular option, the \
471ea671fbdSKate Stone alias could be defined with that value directly in the alias as a constant, \
472b9c1b51eSKate Stone rather than using a positional placeholder:"
473b9c1b51eSKate Stone         R"(
474ea671fbdSKate Stone 
475ea671fbdSKate Stone (lldb) command alias bl3 breakpoint set -f %1 -l 3
476ea671fbdSKate Stone 
477b9c1b51eSKate Stone     Always sets a breakpoint on line 3 of whatever file is indicated.)");
478ebc09c36SJim Ingham 
479405fe67fSCaroline Tice     CommandArgumentEntry arg1;
480405fe67fSCaroline Tice     CommandArgumentEntry arg2;
481405fe67fSCaroline Tice     CommandArgumentEntry arg3;
482405fe67fSCaroline Tice     CommandArgumentData alias_arg;
483405fe67fSCaroline Tice     CommandArgumentData cmd_arg;
484405fe67fSCaroline Tice     CommandArgumentData options_arg;
485405fe67fSCaroline Tice 
486405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
487405fe67fSCaroline Tice     alias_arg.arg_type = eArgTypeAliasName;
488405fe67fSCaroline Tice     alias_arg.arg_repetition = eArgRepeatPlain;
489405fe67fSCaroline Tice 
490b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
491b9c1b51eSKate Stone     // argument entry.
492405fe67fSCaroline Tice     arg1.push_back(alias_arg);
493405fe67fSCaroline Tice 
494405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
495405fe67fSCaroline Tice     cmd_arg.arg_type = eArgTypeCommandName;
496405fe67fSCaroline Tice     cmd_arg.arg_repetition = eArgRepeatPlain;
497405fe67fSCaroline Tice 
498b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
499b9c1b51eSKate Stone     // argument entry.
500405fe67fSCaroline Tice     arg2.push_back(cmd_arg);
501405fe67fSCaroline Tice 
502405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
503405fe67fSCaroline Tice     options_arg.arg_type = eArgTypeAliasOptions;
504405fe67fSCaroline Tice     options_arg.arg_repetition = eArgRepeatOptional;
505405fe67fSCaroline Tice 
506b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
507b9c1b51eSKate Stone     // argument entry.
508405fe67fSCaroline Tice     arg3.push_back(options_arg);
509405fe67fSCaroline Tice 
510405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
511405fe67fSCaroline Tice     m_arguments.push_back(arg1);
512405fe67fSCaroline Tice     m_arguments.push_back(arg2);
513405fe67fSCaroline Tice     m_arguments.push_back(arg3);
514ebc09c36SJim Ingham   }
515ebc09c36SJim Ingham 
5166e3d8e7fSEugene Zelenko   ~CommandObjectCommandsAlias() override = default;
517ebc09c36SJim Ingham 
5185a988416SJim Ingham protected:
5194d51a902SRaphael Isemann   bool DoExecute(llvm::StringRef raw_command_line,
520b9c1b51eSKate Stone                  CommandReturnObject &result) override {
5214d51a902SRaphael Isemann     if (raw_command_line.empty()) {
522d72e412fSEnrico Granata       result.AppendError("'command alias' requires at least two arguments");
52345d0e238SEnrico Granata       return false;
52445d0e238SEnrico Granata     }
52545d0e238SEnrico Granata 
526e1cfbc79STodd Fiala     ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext();
527e1cfbc79STodd Fiala     m_option_group.NotifyOptionParsingStarting(&exe_ctx);
52845d0e238SEnrico Granata 
5293a0e1270SRaphael Isemann     OptionsWithRaw args_with_suffix(raw_command_line);
53045d0e238SEnrico Granata 
5313a0e1270SRaphael Isemann     if (args_with_suffix.HasArgs())
5323a0e1270SRaphael Isemann       if (!ParseOptionsAndNotify(args_with_suffix.GetArgs(), result,
5333a0e1270SRaphael Isemann                                  m_option_group, exe_ctx))
53445d0e238SEnrico Granata         return false;
53545d0e238SEnrico Granata 
536daed98e5SShivam Mittal     llvm::StringRef raw_command_string = args_with_suffix.GetRawPart();
537a01bccdbSZachary Turner     Args args(raw_command_string);
538844d2303SCaroline Tice 
53911eb9c64SZachary Turner     if (args.GetArgumentCount() < 2) {
540d72e412fSEnrico Granata       result.AppendError("'command alias' requires at least two arguments");
541844d2303SCaroline Tice       result.SetStatus(eReturnStatusFailed);
542844d2303SCaroline Tice       return false;
543844d2303SCaroline Tice     }
544844d2303SCaroline Tice 
545844d2303SCaroline Tice     // Get the alias command.
546844d2303SCaroline Tice 
5470d9a201eSRaphael Isemann     auto alias_command = args[0].ref();
5484574a890SZachary Turner     if (alias_command.startswith("-")) {
549d72e412fSEnrico Granata       result.AppendError("aliases starting with a dash are not supported");
550b9c1b51eSKate Stone       if (alias_command == "--help" || alias_command == "--long-help") {
551b9c1b51eSKate Stone         result.AppendWarning("if trying to pass options to 'command alias' add "
552b9c1b51eSKate Stone                              "a -- at the end of the options");
553d72e412fSEnrico Granata       }
554d72e412fSEnrico Granata       result.SetStatus(eReturnStatusFailed);
555d72e412fSEnrico Granata       return false;
556d72e412fSEnrico Granata     }
557844d2303SCaroline Tice 
558b9c1b51eSKate Stone     // Strip the new alias name off 'raw_command_string'  (leave it on args,
55905097246SAdrian Prantl     // which gets passed to 'Execute', which does the stripping itself.
560844d2303SCaroline Tice     size_t pos = raw_command_string.find(alias_command);
561b9c1b51eSKate Stone     if (pos == 0) {
562844d2303SCaroline Tice       raw_command_string = raw_command_string.substr(alias_command.size());
563844d2303SCaroline Tice       pos = raw_command_string.find_first_not_of(' ');
564844d2303SCaroline Tice       if ((pos != std::string::npos) && (pos > 0))
565844d2303SCaroline Tice         raw_command_string = raw_command_string.substr(pos);
566b9c1b51eSKate Stone     } else {
567844d2303SCaroline Tice       result.AppendError("Error parsing command string.  No alias created.");
568844d2303SCaroline Tice       result.SetStatus(eReturnStatusFailed);
569844d2303SCaroline Tice       return false;
570844d2303SCaroline Tice     }
571844d2303SCaroline Tice 
572844d2303SCaroline Tice     // Verify that the command is alias-able.
573771ef6d4SMalcolm Parsons     if (m_interpreter.CommandExists(alias_command)) {
574b9c1b51eSKate Stone       result.AppendErrorWithFormat(
575b9c1b51eSKate Stone           "'%s' is a permanent debugger command and cannot be redefined.\n",
5764574a890SZachary Turner           args[0].c_str());
577844d2303SCaroline Tice       result.SetStatus(eReturnStatusFailed);
578844d2303SCaroline Tice       return false;
579844d2303SCaroline Tice     }
580844d2303SCaroline Tice 
581b9c1b51eSKate Stone     // Get CommandObject that is being aliased. The command name is read from
582a01bccdbSZachary Turner     // the front of raw_command_string. raw_command_string is returned with the
583a01bccdbSZachary Turner     // name of the command object stripped off the front.
584a01bccdbSZachary Turner     llvm::StringRef original_raw_command_string = raw_command_string;
585b9c1b51eSKate Stone     CommandObject *cmd_obj =
586b9c1b51eSKate Stone         m_interpreter.GetCommandObjectForCommand(raw_command_string);
587844d2303SCaroline Tice 
588b9c1b51eSKate Stone     if (!cmd_obj) {
589b9c1b51eSKate Stone       result.AppendErrorWithFormat("invalid command given to 'command alias'. "
590b9c1b51eSKate Stone                                    "'%s' does not begin with a valid command."
591b9c1b51eSKate Stone                                    "  No alias created.",
592a01bccdbSZachary Turner                                    original_raw_command_string.str().c_str());
593844d2303SCaroline Tice       result.SetStatus(eReturnStatusFailed);
594844d2303SCaroline Tice       return false;
595b9c1b51eSKate Stone     } else if (!cmd_obj->WantsRawCommandString()) {
596b9c1b51eSKate Stone       // Note that args was initialized with the original command, and has not
59705097246SAdrian Prantl       // been updated to this point. Therefore can we pass it to the version of
59805097246SAdrian Prantl       // Execute that does not need/expect raw input in the alias.
5995a988416SJim Ingham       return HandleAliasingNormalCommand(args, result);
600b9c1b51eSKate Stone     } else {
601b9c1b51eSKate Stone       return HandleAliasingRawCommand(alias_command, raw_command_string,
602b9c1b51eSKate Stone                                       *cmd_obj, result);
6035a988416SJim Ingham     }
6045a988416SJim Ingham     return result.Succeeded();
6055a988416SJim Ingham   }
6065a988416SJim Ingham 
607a01bccdbSZachary Turner   bool HandleAliasingRawCommand(llvm::StringRef alias_command,
608a01bccdbSZachary Turner                                 llvm::StringRef raw_command_string,
609b9c1b51eSKate Stone                                 CommandObject &cmd_obj,
610b9c1b51eSKate Stone                                 CommandReturnObject &result) {
611844d2303SCaroline Tice     // Verify & handle any options/arguments passed to the alias command
612844d2303SCaroline Tice 
613b9c1b51eSKate Stone     OptionArgVectorSP option_arg_vector_sp =
614b9c1b51eSKate Stone         OptionArgVectorSP(new OptionArgVector);
615844d2303SCaroline Tice 
616b9c1b51eSKate Stone     if (CommandObjectSP cmd_obj_sp =
617b9c1b51eSKate Stone             m_interpreter.GetCommandSPExact(cmd_obj.GetCommandName(), false)) {
618a01bccdbSZachary Turner       if (m_interpreter.AliasExists(alias_command) ||
619a01bccdbSZachary Turner           m_interpreter.UserCommandExists(alias_command)) {
620b9c1b51eSKate Stone         result.AppendWarningWithFormat(
621b9c1b51eSKate Stone             "Overwriting existing definition for '%s'.\n",
622a01bccdbSZachary Turner             alias_command.str().c_str());
623844d2303SCaroline Tice       }
624b9c1b51eSKate Stone       if (CommandAlias *alias = m_interpreter.AddAlias(
625a01bccdbSZachary Turner               alias_command, cmd_obj_sp, raw_command_string)) {
62645d0e238SEnrico Granata         if (m_command_options.m_help.OptionWasSet())
62745d0e238SEnrico Granata           alias->SetHelp(m_command_options.m_help.GetCurrentValue());
62845d0e238SEnrico Granata         if (m_command_options.m_long_help.OptionWasSet())
62945d0e238SEnrico Granata           alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
630844d2303SCaroline Tice         result.SetStatus(eReturnStatusSuccessFinishNoResult);
631b9c1b51eSKate Stone       } else {
632472362e6SCaroline Tice         result.AppendError("Unable to create requested alias.\n");
633472362e6SCaroline Tice         result.SetStatus(eReturnStatusFailed);
634472362e6SCaroline Tice       }
635212130acSEnrico Granata 
636b9c1b51eSKate Stone     } else {
637212130acSEnrico Granata       result.AppendError("Unable to create requested alias.\n");
638212130acSEnrico Granata       result.SetStatus(eReturnStatusFailed);
639212130acSEnrico Granata     }
640212130acSEnrico Granata 
641844d2303SCaroline Tice     return result.Succeeded();
642844d2303SCaroline Tice   }
643ebc09c36SJim Ingham 
644b9c1b51eSKate Stone   bool HandleAliasingNormalCommand(Args &args, CommandReturnObject &result) {
645867b185dSCaroline Tice     size_t argc = args.GetArgumentCount();
646ebc09c36SJim Ingham 
647b9c1b51eSKate Stone     if (argc < 2) {
648d72e412fSEnrico Granata       result.AppendError("'command alias' requires at least two arguments");
649ebc09c36SJim Ingham       result.SetStatus(eReturnStatusFailed);
650ebc09c36SJim Ingham       return false;
651ebc09c36SJim Ingham     }
652ebc09c36SJim Ingham 
6534574a890SZachary Turner     // Save these in std::strings since we're going to shift them off.
654adcd0268SBenjamin Kramer     const std::string alias_command(std::string(args[0].ref()));
655adcd0268SBenjamin Kramer     const std::string actual_command(std::string(args[1].ref()));
656ebc09c36SJim Ingham 
657ebc09c36SJim Ingham     args.Shift(); // Shift the alias command word off the argument vector.
658ebc09c36SJim Ingham     args.Shift(); // Shift the old command word off the argument vector.
659ebc09c36SJim Ingham 
660b9c1b51eSKate Stone     // Verify that the command is alias'able, and get the appropriate command
661b9c1b51eSKate Stone     // object.
662ebc09c36SJim Ingham 
663771ef6d4SMalcolm Parsons     if (m_interpreter.CommandExists(alias_command)) {
664b9c1b51eSKate Stone       result.AppendErrorWithFormat(
665b9c1b51eSKate Stone           "'%s' is a permanent debugger command and cannot be redefined.\n",
666ebc09c36SJim Ingham           alias_command.c_str());
667ebc09c36SJim Ingham       result.SetStatus(eReturnStatusFailed);
6684574a890SZachary Turner       return false;
6694574a890SZachary Turner     }
6704574a890SZachary Turner 
671b9c1b51eSKate Stone     CommandObjectSP command_obj_sp(
672a449698cSZachary Turner         m_interpreter.GetCommandSPExact(actual_command, true));
673ebc09c36SJim Ingham     CommandObjectSP subcommand_obj_sp;
674ebc09c36SJim Ingham     bool use_subcommand = false;
6754574a890SZachary Turner     if (!command_obj_sp) {
6764574a890SZachary Turner       result.AppendErrorWithFormat("'%s' is not an existing command.\n",
6774574a890SZachary Turner                                    actual_command.c_str());
6784574a890SZachary Turner       result.SetStatus(eReturnStatusFailed);
6794574a890SZachary Turner       return false;
6804574a890SZachary Turner     }
681ebc09c36SJim Ingham     CommandObject *cmd_obj = command_obj_sp.get();
6826e3d8e7fSEugene Zelenko     CommandObject *sub_cmd_obj = nullptr;
683b9c1b51eSKate Stone     OptionArgVectorSP option_arg_vector_sp =
684b9c1b51eSKate Stone         OptionArgVectorSP(new OptionArgVector);
685ebc09c36SJim Ingham 
68611eb9c64SZachary Turner     while (cmd_obj->IsMultiwordObject() && !args.empty()) {
6870d9a201eSRaphael Isemann       auto sub_command = args[0].ref();
68811eb9c64SZachary Turner       assert(!sub_command.empty());
6894574a890SZachary Turner       subcommand_obj_sp = cmd_obj->GetSubcommandSP(sub_command);
6904574a890SZachary Turner       if (!subcommand_obj_sp) {
691b9c1b51eSKate Stone         result.AppendErrorWithFormat(
692b9c1b51eSKate Stone             "'%s' is not a valid sub-command of '%s'.  "
693f415eeb4SCaroline Tice             "Unable to create alias.\n",
6944574a890SZachary Turner             args[0].c_str(), actual_command.c_str());
695ebc09c36SJim Ingham         result.SetStatus(eReturnStatusFailed);
696ebc09c36SJim Ingham         return false;
697ebc09c36SJim Ingham       }
6984574a890SZachary Turner 
6994574a890SZachary Turner       sub_cmd_obj = subcommand_obj_sp.get();
7004574a890SZachary Turner       use_subcommand = true;
7014574a890SZachary Turner       args.Shift(); // Shift the sub_command word off the argument vector.
7024574a890SZachary Turner       cmd_obj = sub_cmd_obj;
703ebc09c36SJim Ingham     }
704ebc09c36SJim Ingham 
705ebc09c36SJim Ingham     // Verify & handle any options/arguments passed to the alias command
706ebc09c36SJim Ingham 
707212130acSEnrico Granata     std::string args_string;
708212130acSEnrico Granata 
70911eb9c64SZachary Turner     if (!args.empty()) {
710b9c1b51eSKate Stone       CommandObjectSP tmp_sp =
711b9c1b51eSKate Stone           m_interpreter.GetCommandSPExact(cmd_obj->GetCommandName(), false);
712ebc09c36SJim Ingham       if (use_subcommand)
7134574a890SZachary Turner         tmp_sp = m_interpreter.GetCommandSPExact(sub_cmd_obj->GetCommandName(),
7144574a890SZachary Turner                                                  false);
715ca90c47eSCaroline Tice 
716ca90c47eSCaroline Tice       args.GetCommandString(args_string);
717867b185dSCaroline Tice     }
718ebc09c36SJim Ingham 
719771ef6d4SMalcolm Parsons     if (m_interpreter.AliasExists(alias_command) ||
720771ef6d4SMalcolm Parsons         m_interpreter.UserCommandExists(alias_command)) {
721b9c1b51eSKate Stone       result.AppendWarningWithFormat(
7224574a890SZachary Turner           "Overwriting existing definition for '%s'.\n", alias_command.c_str());
723ebc09c36SJim Ingham     }
724ebc09c36SJim Ingham 
725b9c1b51eSKate Stone     if (CommandAlias *alias = m_interpreter.AddAlias(
7264574a890SZachary Turner             alias_command, use_subcommand ? subcommand_obj_sp : command_obj_sp,
727771ef6d4SMalcolm Parsons             args_string)) {
72845d0e238SEnrico Granata       if (m_command_options.m_help.OptionWasSet())
72945d0e238SEnrico Granata         alias->SetHelp(m_command_options.m_help.GetCurrentValue());
73045d0e238SEnrico Granata       if (m_command_options.m_long_help.OptionWasSet())
73145d0e238SEnrico Granata         alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
732ebc09c36SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
733b9c1b51eSKate Stone     } else {
734212130acSEnrico Granata       result.AppendError("Unable to create requested alias.\n");
735212130acSEnrico Granata       result.SetStatus(eReturnStatusFailed);
736212130acSEnrico Granata       return false;
737212130acSEnrico Granata     }
738ebc09c36SJim Ingham 
739ebc09c36SJim Ingham     return result.Succeeded();
740ebc09c36SJim Ingham   }
741ebc09c36SJim Ingham };
742ebc09c36SJim Ingham 
743ebc09c36SJim Ingham #pragma mark CommandObjectCommandsUnalias
744ebc09c36SJim Ingham // CommandObjectCommandsUnalias
745ebc09c36SJim Ingham 
746b9c1b51eSKate Stone class CommandObjectCommandsUnalias : public CommandObjectParsed {
747ebc09c36SJim Ingham public:
7487428a18cSKate Stone   CommandObjectCommandsUnalias(CommandInterpreter &interpreter)
749b9c1b51eSKate Stone       : CommandObjectParsed(
750b9c1b51eSKate Stone             interpreter, "command unalias",
751b9c1b51eSKate Stone             "Delete one or more custom commands defined by 'command alias'.",
752b9c1b51eSKate Stone             nullptr) {
753405fe67fSCaroline Tice     CommandArgumentEntry arg;
754405fe67fSCaroline Tice     CommandArgumentData alias_arg;
755405fe67fSCaroline Tice 
756405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
757405fe67fSCaroline Tice     alias_arg.arg_type = eArgTypeAliasName;
758405fe67fSCaroline Tice     alias_arg.arg_repetition = eArgRepeatPlain;
759405fe67fSCaroline Tice 
760b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
761b9c1b51eSKate Stone     // argument entry.
762405fe67fSCaroline Tice     arg.push_back(alias_arg);
763405fe67fSCaroline Tice 
764405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
765405fe67fSCaroline Tice     m_arguments.push_back(arg);
766ebc09c36SJim Ingham   }
767ebc09c36SJim Ingham 
7686e3d8e7fSEugene Zelenko   ~CommandObjectCommandsUnalias() override = default;
769ebc09c36SJim Ingham 
7705a988416SJim Ingham protected:
771b9c1b51eSKate Stone   bool DoExecute(Args &args, CommandReturnObject &result) override {
772ebc09c36SJim Ingham     CommandObject::CommandMap::iterator pos;
773ebc09c36SJim Ingham     CommandObject *cmd_obj;
774ebc09c36SJim Ingham 
77511eb9c64SZachary Turner     if (args.empty()) {
77611eb9c64SZachary Turner       result.AppendError("must call 'unalias' with a valid alias");
77711eb9c64SZachary Turner       result.SetStatus(eReturnStatusFailed);
77811eb9c64SZachary Turner       return false;
77911eb9c64SZachary Turner     }
78011eb9c64SZachary Turner 
7810d9a201eSRaphael Isemann     auto command_name = args[0].ref();
782a7015092SGreg Clayton     cmd_obj = m_interpreter.GetCommandObject(command_name);
7834574a890SZachary Turner     if (!cmd_obj) {
7844574a890SZachary Turner       result.AppendErrorWithFormat(
7854574a890SZachary Turner           "'%s' is not a known command.\nTry 'help' to see a "
7864574a890SZachary Turner           "current list of commands.\n",
787867e7d17SZachary Turner           args[0].c_str());
7884574a890SZachary Turner       result.SetStatus(eReturnStatusFailed);
7894574a890SZachary Turner       return false;
7904574a890SZachary Turner     }
7914574a890SZachary Turner 
792b9c1b51eSKate Stone     if (m_interpreter.CommandExists(command_name)) {
793b9c1b51eSKate Stone       if (cmd_obj->IsRemovable()) {
794b9c1b51eSKate Stone         result.AppendErrorWithFormat(
795b9c1b51eSKate Stone             "'%s' is not an alias, it is a debugger command which can be "
796b9c1b51eSKate Stone             "removed using the 'command delete' command.\n",
797867e7d17SZachary Turner             args[0].c_str());
798b9c1b51eSKate Stone       } else {
799b9c1b51eSKate Stone         result.AppendErrorWithFormat(
800b9c1b51eSKate Stone             "'%s' is a permanent debugger command and cannot be removed.\n",
801867e7d17SZachary Turner             args[0].c_str());
802b547278cSGreg Clayton       }
803ebc09c36SJim Ingham       result.SetStatus(eReturnStatusFailed);
8044574a890SZachary Turner       return false;
8054574a890SZachary Turner     }
8064574a890SZachary Turner 
807b9c1b51eSKate Stone     if (!m_interpreter.RemoveAlias(command_name)) {
808a7015092SGreg Clayton       if (m_interpreter.AliasExists(command_name))
809b9c1b51eSKate Stone         result.AppendErrorWithFormat(
810867e7d17SZachary Turner             "Error occurred while attempting to unalias '%s'.\n",
811867e7d17SZachary Turner             args[0].c_str());
812ebc09c36SJim Ingham       else
813b9c1b51eSKate Stone         result.AppendErrorWithFormat("'%s' is not an existing alias.\n",
814867e7d17SZachary Turner                                      args[0].c_str());
815ebc09c36SJim Ingham       result.SetStatus(eReturnStatusFailed);
8164574a890SZachary Turner       return false;
817ebc09c36SJim Ingham     }
818ebc09c36SJim Ingham 
8194574a890SZachary Turner     result.SetStatus(eReturnStatusSuccessFinishNoResult);
820ebc09c36SJim Ingham     return result.Succeeded();
821ebc09c36SJim Ingham   }
822ebc09c36SJim Ingham };
823ebc09c36SJim Ingham 
824b547278cSGreg Clayton #pragma mark CommandObjectCommandsDelete
825b547278cSGreg Clayton // CommandObjectCommandsDelete
826b547278cSGreg Clayton 
827b9c1b51eSKate Stone class CommandObjectCommandsDelete : public CommandObjectParsed {
828b547278cSGreg Clayton public:
8297428a18cSKate Stone   CommandObjectCommandsDelete(CommandInterpreter &interpreter)
830b9c1b51eSKate Stone       : CommandObjectParsed(
831b9c1b51eSKate Stone             interpreter, "command delete",
832b9c1b51eSKate Stone             "Delete one or more custom commands defined by 'command regex'.",
833b9c1b51eSKate Stone             nullptr) {
834b547278cSGreg Clayton     CommandArgumentEntry arg;
835b547278cSGreg Clayton     CommandArgumentData alias_arg;
836b547278cSGreg Clayton 
837b547278cSGreg Clayton     // Define the first (and only) variant of this arg.
838b547278cSGreg Clayton     alias_arg.arg_type = eArgTypeCommandName;
839b547278cSGreg Clayton     alias_arg.arg_repetition = eArgRepeatPlain;
840b547278cSGreg Clayton 
841b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
842b9c1b51eSKate Stone     // argument entry.
843b547278cSGreg Clayton     arg.push_back(alias_arg);
844b547278cSGreg Clayton 
845b547278cSGreg Clayton     // Push the data for the first argument into the m_arguments vector.
846b547278cSGreg Clayton     m_arguments.push_back(arg);
847b547278cSGreg Clayton   }
848b547278cSGreg Clayton 
8496e3d8e7fSEugene Zelenko   ~CommandObjectCommandsDelete() override = default;
850b547278cSGreg Clayton 
851b547278cSGreg Clayton protected:
852b9c1b51eSKate Stone   bool DoExecute(Args &args, CommandReturnObject &result) override {
853b547278cSGreg Clayton     CommandObject::CommandMap::iterator pos;
854b547278cSGreg Clayton 
85511eb9c64SZachary Turner     if (args.empty()) {
85611eb9c64SZachary Turner       result.AppendErrorWithFormat("must call '%s' with one or more valid user "
85711eb9c64SZachary Turner                                    "defined regular expression command names",
858a449698cSZachary Turner                                    GetCommandName().str().c_str());
85911eb9c64SZachary Turner       result.SetStatus(eReturnStatusFailed);
860d77ea5b2SRaphael Isemann       return false;
86111eb9c64SZachary Turner     }
86211eb9c64SZachary Turner 
8630d9a201eSRaphael Isemann     auto command_name = args[0].ref();
8644574a890SZachary Turner     if (!m_interpreter.CommandExists(command_name)) {
86546d4aa21SEnrico Granata       StreamString error_msg_stream;
866d5b44036SJonas Devlieghere       const bool generate_upropos = true;
86746d4aa21SEnrico Granata       const bool generate_type_lookup = false;
868b9c1b51eSKate Stone       CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
8694574a890SZachary Turner           &error_msg_stream, command_name, llvm::StringRef(), llvm::StringRef(),
870d5b44036SJonas Devlieghere           generate_upropos, generate_type_lookup);
871c156427dSZachary Turner       result.AppendError(error_msg_stream.GetString());
872b547278cSGreg Clayton       result.SetStatus(eReturnStatusFailed);
8734574a890SZachary Turner       return false;
874b547278cSGreg Clayton     }
875b547278cSGreg Clayton 
8764574a890SZachary Turner     if (!m_interpreter.RemoveCommand(command_name)) {
8774574a890SZachary Turner       result.AppendErrorWithFormat(
8784574a890SZachary Turner           "'%s' is a permanent debugger command and cannot be removed.\n",
879867e7d17SZachary Turner           args[0].c_str());
8804574a890SZachary Turner       result.SetStatus(eReturnStatusFailed);
8814574a890SZachary Turner       return false;
8824574a890SZachary Turner     }
8834574a890SZachary Turner 
8844574a890SZachary Turner     result.SetStatus(eReturnStatusSuccessFinishNoResult);
8854574a890SZachary Turner     return true;
886b547278cSGreg Clayton   }
887b547278cSGreg Clayton };
888b547278cSGreg Clayton 
889de164aaaSGreg Clayton // CommandObjectCommandsAddRegex
8901f0f5b5bSZachary Turner 
89164becc11SRaphael Isemann #define LLDB_OPTIONS_regex
89264becc11SRaphael Isemann #include "CommandOptions.inc"
8931f0f5b5bSZachary Turner 
8945a988416SJim Ingham #pragma mark CommandObjectCommandsAddRegex
895de164aaaSGreg Clayton 
896b9c1b51eSKate Stone class CommandObjectCommandsAddRegex : public CommandObjectParsed,
897b9c1b51eSKate Stone                                       public IOHandlerDelegateMultiline {
898de164aaaSGreg Clayton public:
8997428a18cSKate Stone   CommandObjectCommandsAddRegex(CommandInterpreter &interpreter)
900b9c1b51eSKate Stone       : CommandObjectParsed(
901a925974bSAdrian Prantl             interpreter, "command regex",
902a925974bSAdrian Prantl             "Define a custom command in terms of "
903b9c1b51eSKate Stone             "existing commands by matching "
904b9c1b51eSKate Stone             "regular expressions.",
9050e5e5a79SGreg Clayton             "command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
906b9c1b51eSKate Stone         IOHandlerDelegateMultiline("",
907b9c1b51eSKate Stone                                    IOHandlerDelegate::Completion::LLDBCommand),
908b9c1b51eSKate Stone         m_options() {
909b9c1b51eSKate Stone     SetHelpLong(
910b9c1b51eSKate Stone         R"(
911b9c1b51eSKate Stone )"
912b9c1b51eSKate Stone         "This command allows the user to create powerful regular expression commands \
913ea671fbdSKate Stone with substitutions. The regular expressions and substitutions are specified \
914b9c1b51eSKate Stone using the regular expression substitution format of:"
915b9c1b51eSKate Stone         R"(
916ea671fbdSKate Stone 
917ea671fbdSKate Stone     s/<regex>/<subst>/
918ea671fbdSKate Stone 
919b9c1b51eSKate Stone )"
920b9c1b51eSKate Stone         "<regex> is a regular expression that can use parenthesis to capture regular \
921ea671fbdSKate Stone expression input and substitute the captured matches in the output using %1 \
922b9c1b51eSKate Stone for the first match, %2 for the second, and so on."
923b9c1b51eSKate Stone         R"(
924ea671fbdSKate Stone 
925b9c1b51eSKate Stone )"
926b9c1b51eSKate Stone         "The regular expressions can all be specified on the command line if more than \
927ea671fbdSKate Stone one argument is provided. If just the command name is provided on the command \
928ea671fbdSKate Stone line, then the regular expressions and substitutions can be entered on separate \
929b9c1b51eSKate Stone lines, followed by an empty line to terminate the command definition."
930b9c1b51eSKate Stone         R"(
931ea671fbdSKate Stone 
932ea671fbdSKate Stone EXAMPLES
933ea671fbdSKate Stone 
934b9c1b51eSKate Stone )"
935b9c1b51eSKate Stone         "The following example will define a regular expression command named 'f' that \
936ea671fbdSKate Stone will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if \
937b9c1b51eSKate Stone a number follows 'f':"
938b9c1b51eSKate Stone         R"(
939ea671fbdSKate Stone 
940b9c1b51eSKate Stone     (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/')");
941de164aaaSGreg Clayton   }
942de164aaaSGreg Clayton 
9436e3d8e7fSEugene Zelenko   ~CommandObjectCommandsAddRegex() override = default;
944de164aaaSGreg Clayton 
9455a988416SJim Ingham protected:
9460affb582SDave Lee   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
9477ca15ba7SLawrence D'Anna     StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
9480affb582SDave Lee     if (output_sp && interactive) {
9490affb582SDave Lee       output_sp->PutCString("Enter one or more sed substitution commands in "
950b9c1b51eSKate Stone                             "the form: 's/<regex>/<subst>/'.\nTerminate the "
951b9c1b51eSKate Stone                             "substitution list with an empty line.\n");
95244d93782SGreg Clayton       output_sp->Flush();
95344d93782SGreg Clayton     }
95444d93782SGreg Clayton   }
95544d93782SGreg Clayton 
956b9c1b51eSKate Stone   void IOHandlerInputComplete(IOHandler &io_handler,
957b9c1b51eSKate Stone                               std::string &data) override {
95844d93782SGreg Clayton     io_handler.SetIsDone(true);
959d5b44036SJonas Devlieghere     if (m_regex_cmd_up) {
96044d93782SGreg Clayton       StringList lines;
961b9c1b51eSKate Stone       if (lines.SplitIntoLines(data)) {
96244d93782SGreg Clayton         bool check_only = false;
9634c78b788SRaphael Isemann         for (const std::string &line : lines) {
9644c78b788SRaphael Isemann           Status error = AppendRegexSubstitution(line, check_only);
965b9c1b51eSKate Stone           if (error.Fail()) {
96657179860SJonas Devlieghere             if (!GetDebugger().GetCommandInterpreter().GetBatchCommandMode()) {
96757179860SJonas Devlieghere               StreamSP out_stream = GetDebugger().GetAsyncOutputStream();
96844d93782SGreg Clayton               out_stream->Printf("error: %s\n", error.AsCString());
96944d93782SGreg Clayton             }
97044d93782SGreg Clayton           }
97144d93782SGreg Clayton         }
97244d93782SGreg Clayton       }
973d5b44036SJonas Devlieghere       if (m_regex_cmd_up->HasRegexEntries()) {
974d5b44036SJonas Devlieghere         CommandObjectSP cmd_sp(m_regex_cmd_up.release());
97544d93782SGreg Clayton         m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
97644d93782SGreg Clayton       }
97744d93782SGreg Clayton     }
97844d93782SGreg Clayton   }
97944d93782SGreg Clayton 
980b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
9815a988416SJim Ingham     const size_t argc = command.GetArgumentCount();
982b9c1b51eSKate Stone     if (argc == 0) {
983b9c1b51eSKate Stone       result.AppendError("usage: 'command regex <command-name> "
984b9c1b51eSKate Stone                          "[s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
9850e5e5a79SGreg Clayton       result.SetStatus(eReturnStatusFailed);
98611eb9c64SZachary Turner       return false;
98711eb9c64SZachary Turner     }
98811eb9c64SZachary Turner 
98997206d57SZachary Turner     Status error;
9900d9a201eSRaphael Isemann     auto name = command[0].ref();
991a8f3ae7cSJonas Devlieghere     m_regex_cmd_up = std::make_unique<CommandObjectRegexCommand>(
9924574a890SZachary Turner         m_interpreter, name, m_options.GetHelp(), m_options.GetSyntax(), 10, 0,
9934574a890SZachary Turner         true);
9940e5e5a79SGreg Clayton 
995b9c1b51eSKate Stone     if (argc == 1) {
99657179860SJonas Devlieghere       Debugger &debugger = GetDebugger();
997e30f11d9SKate Stone       bool color_prompt = debugger.GetUseColor();
99844d93782SGreg Clayton       const bool multiple_lines = true; // Get multiple lines
999b9c1b51eSKate Stone       IOHandlerSP io_handler_sp(new IOHandlerEditline(
1000b9c1b51eSKate Stone           debugger, IOHandler::Type::Other,
100173d80faaSGreg Clayton           "lldb-regex",          // Name of input reader for history
1002514d8cd8SZachary Turner           llvm::StringRef("> "), // Prompt
1003514d8cd8SZachary Turner           llvm::StringRef(),     // Continuation prompt
1004b9c1b51eSKate Stone           multiple_lines, color_prompt,
1005f6913cd7SGreg Clayton           0, // Don't show line numbers
1006d77c2e09SJonas Devlieghere           *this, nullptr));
100744d93782SGreg Clayton 
1008b9c1b51eSKate Stone       if (io_handler_sp) {
10097ce2de2cSJonas Devlieghere         debugger.RunIOHandlerAsync(io_handler_sp);
1010de164aaaSGreg Clayton         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1011de164aaaSGreg Clayton       }
1012b9c1b51eSKate Stone     } else {
101397d2c401SZachary Turner       for (auto &entry : command.entries().drop_front()) {
101444d93782SGreg Clayton         bool check_only = false;
10150d9a201eSRaphael Isemann         error = AppendRegexSubstitution(entry.ref(), check_only);
10160e5e5a79SGreg Clayton         if (error.Fail())
10170e5e5a79SGreg Clayton           break;
10180e5e5a79SGreg Clayton       }
10190e5e5a79SGreg Clayton 
1020b9c1b51eSKate Stone       if (error.Success()) {
10210e5e5a79SGreg Clayton         AddRegexCommandToInterpreter();
10220e5e5a79SGreg Clayton       }
10230e5e5a79SGreg Clayton     }
1024b9c1b51eSKate Stone     if (error.Fail()) {
10250e5e5a79SGreg Clayton       result.AppendError(error.AsCString());
1026de164aaaSGreg Clayton       result.SetStatus(eReturnStatusFailed);
1027de164aaaSGreg Clayton     }
10280e5e5a79SGreg Clayton 
1029de164aaaSGreg Clayton     return result.Succeeded();
1030de164aaaSGreg Clayton   }
1031de164aaaSGreg Clayton 
103297206d57SZachary Turner   Status AppendRegexSubstitution(const llvm::StringRef &regex_sed,
1033b9c1b51eSKate Stone                                  bool check_only) {
103497206d57SZachary Turner     Status error;
10350e5e5a79SGreg Clayton 
1036d5b44036SJonas Devlieghere     if (!m_regex_cmd_up) {
1037b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1038b9c1b51eSKate Stone           "invalid regular expression command object for: '%.*s'",
1039b9c1b51eSKate Stone           (int)regex_sed.size(), regex_sed.data());
10400e5e5a79SGreg Clayton       return error;
1041de164aaaSGreg Clayton     }
10420e5e5a79SGreg Clayton 
10430e5e5a79SGreg Clayton     size_t regex_sed_size = regex_sed.size();
10440e5e5a79SGreg Clayton 
1045b9c1b51eSKate Stone     if (regex_sed_size <= 1) {
1046b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1047b9c1b51eSKate Stone           "regular expression substitution string is too short: '%.*s'",
1048b9c1b51eSKate Stone           (int)regex_sed.size(), regex_sed.data());
10490e5e5a79SGreg Clayton       return error;
10500e5e5a79SGreg Clayton     }
10510e5e5a79SGreg Clayton 
1052b9c1b51eSKate Stone     if (regex_sed[0] != 's') {
1053b9c1b51eSKate Stone       error.SetErrorStringWithFormat("regular expression substitution string "
1054b9c1b51eSKate Stone                                      "doesn't start with 's': '%.*s'",
1055b9c1b51eSKate Stone                                      (int)regex_sed.size(), regex_sed.data());
10560e5e5a79SGreg Clayton       return error;
10570e5e5a79SGreg Clayton     }
10580e5e5a79SGreg Clayton     const size_t first_separator_char_pos = 1;
105905097246SAdrian Prantl     // use the char that follows 's' as the regex separator character so we can
106005097246SAdrian Prantl     // have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
10610e5e5a79SGreg Clayton     const char separator_char = regex_sed[first_separator_char_pos];
1062b9c1b51eSKate Stone     const size_t second_separator_char_pos =
1063b9c1b51eSKate Stone         regex_sed.find(separator_char, first_separator_char_pos + 1);
10640e5e5a79SGreg Clayton 
1065b9c1b51eSKate Stone     if (second_separator_char_pos == std::string::npos) {
1066b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1067b9c1b51eSKate Stone           "missing second '%c' separator char after '%.*s' in '%.*s'",
10680e5e5a79SGreg Clayton           separator_char,
10690e5e5a79SGreg Clayton           (int)(regex_sed.size() - first_separator_char_pos - 1),
1070ea508635SGreg Clayton           regex_sed.data() + (first_separator_char_pos + 1),
1071b9c1b51eSKate Stone           (int)regex_sed.size(), regex_sed.data());
10720e5e5a79SGreg Clayton       return error;
10730e5e5a79SGreg Clayton     }
10740e5e5a79SGreg Clayton 
1075b9c1b51eSKate Stone     const size_t third_separator_char_pos =
1076b9c1b51eSKate Stone         regex_sed.find(separator_char, second_separator_char_pos + 1);
10770e5e5a79SGreg Clayton 
1078b9c1b51eSKate Stone     if (third_separator_char_pos == std::string::npos) {
1079b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1080b9c1b51eSKate Stone           "missing third '%c' separator char after '%.*s' in '%.*s'",
10810e5e5a79SGreg Clayton           separator_char,
10820e5e5a79SGreg Clayton           (int)(regex_sed.size() - second_separator_char_pos - 1),
1083ea508635SGreg Clayton           regex_sed.data() + (second_separator_char_pos + 1),
1084b9c1b51eSKate Stone           (int)regex_sed.size(), regex_sed.data());
10850e5e5a79SGreg Clayton       return error;
10860e5e5a79SGreg Clayton     }
10870e5e5a79SGreg Clayton 
1088b9c1b51eSKate Stone     if (third_separator_char_pos != regex_sed_size - 1) {
108905097246SAdrian Prantl       // Make sure that everything that follows the last regex separator char
1090b9c1b51eSKate Stone       if (regex_sed.find_first_not_of("\t\n\v\f\r ",
1091b9c1b51eSKate Stone                                       third_separator_char_pos + 1) !=
1092b9c1b51eSKate Stone           std::string::npos) {
1093b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
1094b9c1b51eSKate Stone             "extra data found after the '%.*s' regular expression substitution "
1095b9c1b51eSKate Stone             "string: '%.*s'",
1096b9c1b51eSKate Stone             (int)third_separator_char_pos + 1, regex_sed.data(),
10970e5e5a79SGreg Clayton             (int)(regex_sed.size() - third_separator_char_pos - 1),
10980e5e5a79SGreg Clayton             regex_sed.data() + (third_separator_char_pos + 1));
10990e5e5a79SGreg Clayton         return error;
11000e5e5a79SGreg Clayton       }
1101b9c1b51eSKate Stone     } else if (first_separator_char_pos + 1 == second_separator_char_pos) {
1102b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1103b9c1b51eSKate Stone           "<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
1104b9c1b51eSKate Stone           separator_char, separator_char, separator_char, (int)regex_sed.size(),
11050e5e5a79SGreg Clayton           regex_sed.data());
11060e5e5a79SGreg Clayton       return error;
1107b9c1b51eSKate Stone     } else if (second_separator_char_pos + 1 == third_separator_char_pos) {
1108b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
1109b9c1b51eSKate Stone           "<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
1110b9c1b51eSKate Stone           separator_char, separator_char, separator_char, (int)regex_sed.size(),
11110e5e5a79SGreg Clayton           regex_sed.data());
11120e5e5a79SGreg Clayton       return error;
11130e5e5a79SGreg Clayton     }
111444d93782SGreg Clayton 
1115b9c1b51eSKate Stone     if (!check_only) {
1116adcd0268SBenjamin Kramer       std::string regex(std::string(regex_sed.substr(
1117adcd0268SBenjamin Kramer           first_separator_char_pos + 1,
1118adcd0268SBenjamin Kramer           second_separator_char_pos - first_separator_char_pos - 1)));
1119adcd0268SBenjamin Kramer       std::string subst(std::string(regex_sed.substr(
1120adcd0268SBenjamin Kramer           second_separator_char_pos + 1,
1121adcd0268SBenjamin Kramer           third_separator_char_pos - second_separator_char_pos - 1)));
1122d5b44036SJonas Devlieghere       m_regex_cmd_up->AddRegexCommand(regex.c_str(), subst.c_str());
112344d93782SGreg Clayton     }
11240e5e5a79SGreg Clayton     return error;
1125de164aaaSGreg Clayton   }
1126de164aaaSGreg Clayton 
1127b9c1b51eSKate Stone   void AddRegexCommandToInterpreter() {
1128d5b44036SJonas Devlieghere     if (m_regex_cmd_up) {
1129d5b44036SJonas Devlieghere       if (m_regex_cmd_up->HasRegexEntries()) {
1130d5b44036SJonas Devlieghere         CommandObjectSP cmd_sp(m_regex_cmd_up.release());
1131de164aaaSGreg Clayton         m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
1132de164aaaSGreg Clayton       }
1133de164aaaSGreg Clayton     }
1134de164aaaSGreg Clayton   }
1135de164aaaSGreg Clayton 
1136de164aaaSGreg Clayton private:
1137d5b44036SJonas Devlieghere   std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_up;
1138de164aaaSGreg Clayton 
1139b9c1b51eSKate Stone   class CommandOptions : public Options {
1140de164aaaSGreg Clayton   public:
1141b9c1b51eSKate Stone     CommandOptions() : Options() {}
1142de164aaaSGreg Clayton 
11436e3d8e7fSEugene Zelenko     ~CommandOptions() override = default;
1144de164aaaSGreg Clayton 
114597206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1146b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
114797206d57SZachary Turner       Status error;
11483bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
1149de164aaaSGreg Clayton 
1150b9c1b51eSKate Stone       switch (short_option) {
1151de164aaaSGreg Clayton       case 'h':
1152adcd0268SBenjamin Kramer         m_help.assign(std::string(option_arg));
1153de164aaaSGreg Clayton         break;
1154de164aaaSGreg Clayton       case 's':
1155adcd0268SBenjamin Kramer         m_syntax.assign(std::string(option_arg));
1156de164aaaSGreg Clayton         break;
1157de164aaaSGreg Clayton       default:
115836162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
1159de164aaaSGreg Clayton       }
1160de164aaaSGreg Clayton 
1161de164aaaSGreg Clayton       return error;
1162de164aaaSGreg Clayton     }
1163de164aaaSGreg Clayton 
1164b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
1165de164aaaSGreg Clayton       m_help.clear();
1166de164aaaSGreg Clayton       m_syntax.clear();
1167de164aaaSGreg Clayton     }
1168de164aaaSGreg Clayton 
11691f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
117070602439SZachary Turner       return llvm::makeArrayRef(g_regex_options);
11711f0f5b5bSZachary Turner     }
1172de164aaaSGreg Clayton 
1173daed98e5SShivam Mittal     llvm::StringRef GetHelp() { return m_help; }
11746e3d8e7fSEugene Zelenko 
1175daed98e5SShivam Mittal     llvm::StringRef GetSyntax() { return m_syntax; }
11766e3d8e7fSEugene Zelenko 
1177de164aaaSGreg Clayton   protected:
11786e3d8e7fSEugene Zelenko     // Instance variables to hold the values for command options.
11796e3d8e7fSEugene Zelenko 
1180de164aaaSGreg Clayton     std::string m_help;
1181de164aaaSGreg Clayton     std::string m_syntax;
1182de164aaaSGreg Clayton   };
1183de164aaaSGreg Clayton 
1184b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
1185de164aaaSGreg Clayton 
11865a988416SJim Ingham   CommandOptions m_options;
1187de164aaaSGreg Clayton };
1188de164aaaSGreg Clayton 
1189b9c1b51eSKate Stone class CommandObjectPythonFunction : public CommandObjectRaw {
1190223383edSEnrico Granata public:
1191b9c1b51eSKate Stone   CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name,
1192b9c1b51eSKate Stone                               std::string funct, std::string help,
1193b9c1b51eSKate Stone                               ScriptedCommandSynchronicity synch)
1194a925974bSAdrian Prantl       : CommandObjectRaw(interpreter, name), m_function_name(funct),
1195a925974bSAdrian Prantl         m_synchro(synch), m_fetched_help_long(false) {
1196735152e3SEnrico Granata     if (!help.empty())
1197442f6530SZachary Turner       SetHelp(help);
1198b9c1b51eSKate Stone     else {
1199735152e3SEnrico Granata       StreamString stream;
1200735152e3SEnrico Granata       stream.Printf("For more information run 'help %s'", name.c_str());
1201c156427dSZachary Turner       SetHelp(stream.GetString());
1202735152e3SEnrico Granata     }
1203223383edSEnrico Granata   }
1204223383edSEnrico Granata 
12056e3d8e7fSEugene Zelenko   ~CommandObjectPythonFunction() override = default;
1206223383edSEnrico Granata 
1207b9c1b51eSKate Stone   bool IsRemovable() const override { return true; }
12085a988416SJim Ingham 
1209b9c1b51eSKate Stone   const std::string &GetFunctionName() { return m_function_name; }
12105a988416SJim Ingham 
1211b9c1b51eSKate Stone   ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
12125a988416SJim Ingham 
1213442f6530SZachary Turner   llvm::StringRef GetHelpLong() override {
1214442f6530SZachary Turner     if (m_fetched_help_long)
1215442f6530SZachary Turner       return CommandObjectRaw::GetHelpLong();
1216442f6530SZachary Turner 
12172b29b432SJonas Devlieghere     ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
1218442f6530SZachary Turner     if (!scripter)
1219442f6530SZachary Turner       return CommandObjectRaw::GetHelpLong();
1220442f6530SZachary Turner 
1221fac939e9SEnrico Granata     std::string docstring;
1222442f6530SZachary Turner     m_fetched_help_long =
1223442f6530SZachary Turner         scripter->GetDocumentationForItem(m_function_name.c_str(), docstring);
1224fac939e9SEnrico Granata     if (!docstring.empty())
1225442f6530SZachary Turner       SetHelpLong(docstring);
1226fac939e9SEnrico Granata     return CommandObjectRaw::GetHelpLong();
1227fac939e9SEnrico Granata   }
1228fac939e9SEnrico Granata 
12295a988416SJim Ingham protected:
12304d51a902SRaphael Isemann   bool DoExecute(llvm::StringRef raw_command_line,
1231b9c1b51eSKate Stone                  CommandReturnObject &result) override {
12322b29b432SJonas Devlieghere     ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
1233223383edSEnrico Granata 
123497206d57SZachary Turner     Status error;
1235223383edSEnrico Granata 
123670f11f88SJim Ingham     result.SetStatus(eReturnStatusInvalid);
123770f11f88SJim Ingham 
1238a925974bSAdrian Prantl     if (!scripter || !scripter->RunScriptBasedCommand(
1239a925974bSAdrian Prantl                          m_function_name.c_str(), raw_command_line, m_synchro,
1240a925974bSAdrian Prantl                          result, error, m_exe_ctx)) {
1241223383edSEnrico Granata       result.AppendError(error.AsCString());
1242223383edSEnrico Granata       result.SetStatus(eReturnStatusFailed);
1243b9c1b51eSKate Stone     } else {
124470f11f88SJim Ingham       // Don't change the status if the command already set it...
1245b9c1b51eSKate Stone       if (result.GetStatus() == eReturnStatusInvalid) {
1246c156427dSZachary Turner         if (result.GetOutputData().empty())
1247223383edSEnrico Granata           result.SetStatus(eReturnStatusSuccessFinishNoResult);
124870f11f88SJim Ingham         else
124970f11f88SJim Ingham           result.SetStatus(eReturnStatusSuccessFinishResult);
125070f11f88SJim Ingham       }
125170f11f88SJim Ingham     }
1252223383edSEnrico Granata 
1253223383edSEnrico Granata     return result.Succeeded();
1254223383edSEnrico Granata   }
1255223383edSEnrico Granata 
12566e3d8e7fSEugene Zelenko private:
12576e3d8e7fSEugene Zelenko   std::string m_function_name;
12586e3d8e7fSEugene Zelenko   ScriptedCommandSynchronicity m_synchro;
12596e3d8e7fSEugene Zelenko   bool m_fetched_help_long;
1260223383edSEnrico Granata };
1261223383edSEnrico Granata 
1262b9c1b51eSKate Stone class CommandObjectScriptingObject : public CommandObjectRaw {
12639fe00e52SEnrico Granata public:
12649fe00e52SEnrico Granata   CommandObjectScriptingObject(CommandInterpreter &interpreter,
12659fe00e52SEnrico Granata                                std::string name,
12660641ca1aSZachary Turner                                StructuredData::GenericSP cmd_obj_sp,
1267b9c1b51eSKate Stone                                ScriptedCommandSynchronicity synch)
1268a925974bSAdrian Prantl       : CommandObjectRaw(interpreter, name), m_cmd_obj_sp(cmd_obj_sp),
1269a925974bSAdrian Prantl         m_synchro(synch), m_fetched_help_short(false),
1270b9c1b51eSKate Stone         m_fetched_help_long(false) {
12719fe00e52SEnrico Granata     StreamString stream;
12729fe00e52SEnrico Granata     stream.Printf("For more information run 'help %s'", name.c_str());
1273c156427dSZachary Turner     SetHelp(stream.GetString());
12742b29b432SJonas Devlieghere     if (ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter())
1275e87764f2SEnrico Granata       GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
12769fe00e52SEnrico Granata   }
12779fe00e52SEnrico Granata 
12786e3d8e7fSEugene Zelenko   ~CommandObjectScriptingObject() override = default;
12799fe00e52SEnrico Granata 
1280b9c1b51eSKate Stone   bool IsRemovable() const override { return true; }
12819fe00e52SEnrico Granata 
1282b9c1b51eSKate Stone   ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
12839fe00e52SEnrico Granata 
1284442f6530SZachary Turner   llvm::StringRef GetHelp() override {
1285442f6530SZachary Turner     if (m_fetched_help_short)
1286442f6530SZachary Turner       return CommandObjectRaw::GetHelp();
12872b29b432SJonas Devlieghere     ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
1288442f6530SZachary Turner     if (!scripter)
1289442f6530SZachary Turner       return CommandObjectRaw::GetHelp();
12906f79bb2dSEnrico Granata     std::string docstring;
1291b9c1b51eSKate Stone     m_fetched_help_short =
1292b9c1b51eSKate Stone         scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring);
12936f79bb2dSEnrico Granata     if (!docstring.empty())
1294442f6530SZachary Turner       SetHelp(docstring);
1295442f6530SZachary Turner 
12966f79bb2dSEnrico Granata     return CommandObjectRaw::GetHelp();
12976f79bb2dSEnrico Granata   }
12986f79bb2dSEnrico Granata 
1299442f6530SZachary Turner   llvm::StringRef GetHelpLong() override {
1300442f6530SZachary Turner     if (m_fetched_help_long)
1301442f6530SZachary Turner       return CommandObjectRaw::GetHelpLong();
1302442f6530SZachary Turner 
13032b29b432SJonas Devlieghere     ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
1304442f6530SZachary Turner     if (!scripter)
1305442f6530SZachary Turner       return CommandObjectRaw::GetHelpLong();
1306442f6530SZachary Turner 
13076f79bb2dSEnrico Granata     std::string docstring;
1308b9c1b51eSKate Stone     m_fetched_help_long =
1309b9c1b51eSKate Stone         scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring);
13106f79bb2dSEnrico Granata     if (!docstring.empty())
1311442f6530SZachary Turner       SetHelpLong(docstring);
13129fe00e52SEnrico Granata     return CommandObjectRaw::GetHelpLong();
13139fe00e52SEnrico Granata   }
13149fe00e52SEnrico Granata 
13159fe00e52SEnrico Granata protected:
13164d51a902SRaphael Isemann   bool DoExecute(llvm::StringRef raw_command_line,
1317b9c1b51eSKate Stone                  CommandReturnObject &result) override {
13182b29b432SJonas Devlieghere     ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
13199fe00e52SEnrico Granata 
132097206d57SZachary Turner     Status error;
13219fe00e52SEnrico Granata 
13229fe00e52SEnrico Granata     result.SetStatus(eReturnStatusInvalid);
13239fe00e52SEnrico Granata 
1324b9c1b51eSKate Stone     if (!scripter ||
1325b9c1b51eSKate Stone         !scripter->RunScriptBasedCommand(m_cmd_obj_sp, raw_command_line,
1326b9c1b51eSKate Stone                                          m_synchro, result, error, m_exe_ctx)) {
13279fe00e52SEnrico Granata       result.AppendError(error.AsCString());
13289fe00e52SEnrico Granata       result.SetStatus(eReturnStatusFailed);
1329b9c1b51eSKate Stone     } else {
13309fe00e52SEnrico Granata       // Don't change the status if the command already set it...
1331b9c1b51eSKate Stone       if (result.GetStatus() == eReturnStatusInvalid) {
1332c156427dSZachary Turner         if (result.GetOutputData().empty())
13339fe00e52SEnrico Granata           result.SetStatus(eReturnStatusSuccessFinishNoResult);
13349fe00e52SEnrico Granata         else
13359fe00e52SEnrico Granata           result.SetStatus(eReturnStatusSuccessFinishResult);
13369fe00e52SEnrico Granata       }
13379fe00e52SEnrico Granata     }
13389fe00e52SEnrico Granata 
13399fe00e52SEnrico Granata     return result.Succeeded();
13409fe00e52SEnrico Granata   }
13419fe00e52SEnrico Granata 
13426e3d8e7fSEugene Zelenko private:
13436e3d8e7fSEugene Zelenko   StructuredData::GenericSP m_cmd_obj_sp;
13446e3d8e7fSEugene Zelenko   ScriptedCommandSynchronicity m_synchro;
13456e3d8e7fSEugene Zelenko   bool m_fetched_help_short : 1;
13466e3d8e7fSEugene Zelenko   bool m_fetched_help_long : 1;
13479fe00e52SEnrico Granata };
13489fe00e52SEnrico Granata 
1349a9dbf432SEnrico Granata // CommandObjectCommandsScriptImport
135064becc11SRaphael Isemann #define LLDB_OPTIONS_script_import
135164becc11SRaphael Isemann #include "CommandOptions.inc"
13521f0f5b5bSZachary Turner 
1353b9c1b51eSKate Stone class CommandObjectCommandsScriptImport : public CommandObjectParsed {
13545a988416SJim Ingham public:
1355b9c1b51eSKate Stone   CommandObjectCommandsScriptImport(CommandInterpreter &interpreter)
1356b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command script import",
1357b9c1b51eSKate Stone                             "Import a scripting module in LLDB.", nullptr),
1358b9c1b51eSKate Stone         m_options() {
13595a988416SJim Ingham     CommandArgumentEntry arg1;
13605a988416SJim Ingham     CommandArgumentData cmd_arg;
13615a988416SJim Ingham 
13625a988416SJim Ingham     // Define the first (and only) variant of this arg.
13635a988416SJim Ingham     cmd_arg.arg_type = eArgTypeFilename;
13643b00e35bSEnrico Granata     cmd_arg.arg_repetition = eArgRepeatPlus;
13655a988416SJim Ingham 
1366b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
1367b9c1b51eSKate Stone     // argument entry.
13685a988416SJim Ingham     arg1.push_back(cmd_arg);
13695a988416SJim Ingham 
13705a988416SJim Ingham     // Push the data for the first argument into the m_arguments vector.
13715a988416SJim Ingham     m_arguments.push_back(arg1);
13725a988416SJim Ingham   }
13735a988416SJim Ingham 
13746e3d8e7fSEugene Zelenko   ~CommandObjectCommandsScriptImport() override = default;
13755a988416SJim Ingham 
1376ae34ed2cSRaphael Isemann   void
1377ae34ed2cSRaphael Isemann   HandleArgumentCompletion(CompletionRequest &request,
13782443bbd4SRaphael Isemann                            OptionElementVector &opt_element_vector) override {
1379b9c1b51eSKate Stone     CommandCompletions::InvokeCommonCompletionCallbacks(
1380b9c1b51eSKate Stone         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
1381a2e76c0bSRaphael Isemann         request, nullptr);
13825a988416SJim Ingham   }
13835a988416SJim Ingham 
1384b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
13855a988416SJim Ingham 
13865a988416SJim Ingham protected:
1387b9c1b51eSKate Stone   class CommandOptions : public Options {
13880a305db7SEnrico Granata   public:
1389b9c1b51eSKate Stone     CommandOptions() : Options() {}
13900a305db7SEnrico Granata 
13916e3d8e7fSEugene Zelenko     ~CommandOptions() override = default;
13920a305db7SEnrico Granata 
139397206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1394b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
139597206d57SZachary Turner       Status error;
13963bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
13970a305db7SEnrico Granata 
1398b9c1b51eSKate Stone       switch (short_option) {
13990a305db7SEnrico Granata       case 'r':
140015625112SJonas Devlieghere         // NO-OP
14010a305db7SEnrico Granata         break;
14020a305db7SEnrico Granata       default:
140336162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
14040a305db7SEnrico Granata       }
14050a305db7SEnrico Granata 
14060a305db7SEnrico Granata       return error;
14070a305db7SEnrico Granata     }
14080a305db7SEnrico Granata 
1409b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
14100a305db7SEnrico Granata     }
14110a305db7SEnrico Granata 
14121f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
141370602439SZachary Turner       return llvm::makeArrayRef(g_script_import_options);
14141f0f5b5bSZachary Turner     }
14150a305db7SEnrico Granata   };
14160a305db7SEnrico Granata 
1417b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
141811eb9c64SZachary Turner     if (command.empty()) {
14193b00e35bSEnrico Granata       result.AppendError("command script import needs one or more arguments");
1420a9dbf432SEnrico Granata       result.SetStatus(eReturnStatusFailed);
1421a9dbf432SEnrico Granata       return false;
1422a9dbf432SEnrico Granata     }
1423a9dbf432SEnrico Granata 
142411eb9c64SZachary Turner     for (auto &entry : command.entries()) {
142597206d57SZachary Turner       Status error;
1426a9dbf432SEnrico Granata 
1427c9d645d3SGreg Clayton       const bool init_session = true;
1428b9c1b51eSKate Stone       // FIXME: this is necessary because CommandObject::CheckRequirements()
142911eb9c64SZachary Turner       // assumes that commands won't ever be recursively invoked, but it's
143011eb9c64SZachary Turner       // actually possible to craft a Python script that does other "command
143105097246SAdrian Prantl       // script imports" in __lldb_init_module the real fix is to have
143205097246SAdrian Prantl       // recursive commands possible with a CommandInvocation object separate
143305097246SAdrian Prantl       // from the CommandObject itself, so that recursive command invocations
143405097246SAdrian Prantl       // won't stomp on each other (wrt to execution contents, options, and
143505097246SAdrian Prantl       // more)
1436078551c7SEnrico Granata       m_exe_ctx.Clear();
14372b29b432SJonas Devlieghere       if (GetDebugger().GetScriptInterpreter()->LoadScriptingModule(
143815625112SJonas Devlieghere               entry.c_str(), init_session, error)) {
1439a9dbf432SEnrico Granata         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1440b9c1b51eSKate Stone       } else {
1441b9c1b51eSKate Stone         result.AppendErrorWithFormat("module importing failed: %s",
1442b9c1b51eSKate Stone                                      error.AsCString());
1443a9dbf432SEnrico Granata         result.SetStatus(eReturnStatusFailed);
1444a9dbf432SEnrico Granata       }
14453b00e35bSEnrico Granata     }
1446a9dbf432SEnrico Granata 
1447a9dbf432SEnrico Granata     return result.Succeeded();
1448a9dbf432SEnrico Granata   }
14490a305db7SEnrico Granata 
14505a988416SJim Ingham   CommandOptions m_options;
1451a9dbf432SEnrico Granata };
1452223383edSEnrico Granata 
1453223383edSEnrico Granata // CommandObjectCommandsScriptAdd
14548fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_script_synchro_type[] = {
1455e063ecccSJonas Devlieghere     {
1456e063ecccSJonas Devlieghere         eScriptedCommandSynchronicitySynchronous,
1457e063ecccSJonas Devlieghere         "synchronous",
1458e063ecccSJonas Devlieghere         "Run synchronous",
1459e063ecccSJonas Devlieghere     },
1460e063ecccSJonas Devlieghere     {
1461e063ecccSJonas Devlieghere         eScriptedCommandSynchronicityAsynchronous,
1462e063ecccSJonas Devlieghere         "asynchronous",
1463e063ecccSJonas Devlieghere         "Run asynchronous",
1464e063ecccSJonas Devlieghere     },
1465e063ecccSJonas Devlieghere     {
1466e063ecccSJonas Devlieghere         eScriptedCommandSynchronicityCurrentValue,
1467e063ecccSJonas Devlieghere         "current",
1468e063ecccSJonas Devlieghere         "Do not alter current setting",
1469e063ecccSJonas Devlieghere     },
1470e063ecccSJonas Devlieghere };
14711f0f5b5bSZachary Turner 
14728fe53c49STatyana Krasnukha static constexpr OptionEnumValues ScriptSynchroType() {
14738fe53c49STatyana Krasnukha   return OptionEnumValues(g_script_synchro_type);
14748fe53c49STatyana Krasnukha }
14758fe53c49STatyana Krasnukha 
147664becc11SRaphael Isemann #define LLDB_OPTIONS_script_add
147764becc11SRaphael Isemann #include "CommandOptions.inc"
14781f0f5b5bSZachary Turner 
1479b9c1b51eSKate Stone class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
1480b9c1b51eSKate Stone                                        public IOHandlerDelegateMultiline {
14815a988416SJim Ingham public:
1482b9c1b51eSKate Stone   CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter)
1483b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command script add",
14845a988416SJim Ingham                             "Add a scripted function as an LLDB command.",
14856e3d8e7fSEugene Zelenko                             nullptr),
1486b9c1b51eSKate Stone         IOHandlerDelegateMultiline("DONE"), m_options() {
14875a988416SJim Ingham     CommandArgumentEntry arg1;
14885a988416SJim Ingham     CommandArgumentData cmd_arg;
14895a988416SJim Ingham 
14905a988416SJim Ingham     // Define the first (and only) variant of this arg.
14915a988416SJim Ingham     cmd_arg.arg_type = eArgTypeCommandName;
14925a988416SJim Ingham     cmd_arg.arg_repetition = eArgRepeatPlain;
14935a988416SJim Ingham 
1494b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
1495b9c1b51eSKate Stone     // argument entry.
14965a988416SJim Ingham     arg1.push_back(cmd_arg);
14975a988416SJim Ingham 
14985a988416SJim Ingham     // Push the data for the first argument into the m_arguments vector.
14995a988416SJim Ingham     m_arguments.push_back(arg1);
15005a988416SJim Ingham   }
15015a988416SJim Ingham 
15026e3d8e7fSEugene Zelenko   ~CommandObjectCommandsScriptAdd() override = default;
15035a988416SJim Ingham 
1504b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
15055a988416SJim Ingham 
15065a988416SJim Ingham protected:
1507b9c1b51eSKate Stone   class CommandOptions : public Options {
1508223383edSEnrico Granata   public:
1509b9c1b51eSKate Stone     CommandOptions()
1510b9c1b51eSKate Stone         : Options(), m_class_name(), m_funct_name(), m_short_help(),
1511b9c1b51eSKate Stone           m_synchronicity(eScriptedCommandSynchronicitySynchronous) {}
1512223383edSEnrico Granata 
15136e3d8e7fSEugene Zelenko     ~CommandOptions() override = default;
1514223383edSEnrico Granata 
151597206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1516b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
151797206d57SZachary Turner       Status error;
15183bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
1519223383edSEnrico Granata 
1520b9c1b51eSKate Stone       switch (short_option) {
1521223383edSEnrico Granata       case 'f':
1522fe11483bSZachary Turner         if (!option_arg.empty())
1523adcd0268SBenjamin Kramer           m_funct_name = std::string(option_arg);
1524735152e3SEnrico Granata         break;
15259fe00e52SEnrico Granata       case 'c':
1526fe11483bSZachary Turner         if (!option_arg.empty())
1527adcd0268SBenjamin Kramer           m_class_name = std::string(option_arg);
15289fe00e52SEnrico Granata         break;
1529735152e3SEnrico Granata       case 'h':
1530fe11483bSZachary Turner         if (!option_arg.empty())
1531adcd0268SBenjamin Kramer           m_short_help = std::string(option_arg);
1532223383edSEnrico Granata         break;
15330a305db7SEnrico Granata       case 's':
1534b9c1b51eSKate Stone         m_synchronicity =
153547cbf4a0SPavel Labath             (ScriptedCommandSynchronicity)OptionArgParser::ToOptionEnum(
1536fe11483bSZachary Turner                 option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
15370a305db7SEnrico Granata         if (!error.Success())
1538b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
1539fe11483bSZachary Turner               "unrecognized value for synchronicity '%s'",
1540fe11483bSZachary Turner               option_arg.str().c_str());
15410a305db7SEnrico Granata         break;
1542223383edSEnrico Granata       default:
154336162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
1544223383edSEnrico Granata       }
1545223383edSEnrico Granata 
1546223383edSEnrico Granata       return error;
1547223383edSEnrico Granata     }
1548223383edSEnrico Granata 
1549b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
15509fe00e52SEnrico Granata       m_class_name.clear();
1551735152e3SEnrico Granata       m_funct_name.clear();
1552735152e3SEnrico Granata       m_short_help.clear();
155344d93782SGreg Clayton       m_synchronicity = eScriptedCommandSynchronicitySynchronous;
1554223383edSEnrico Granata     }
1555223383edSEnrico Granata 
15561f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
155770602439SZachary Turner       return llvm::makeArrayRef(g_script_add_options);
15581f0f5b5bSZachary Turner     }
1559223383edSEnrico Granata 
1560223383edSEnrico Granata     // Instance variables to hold the values for command options.
1561223383edSEnrico Granata 
15629fe00e52SEnrico Granata     std::string m_class_name;
1563223383edSEnrico Granata     std::string m_funct_name;
1564735152e3SEnrico Granata     std::string m_short_help;
156544d93782SGreg Clayton     ScriptedCommandSynchronicity m_synchronicity;
1566223383edSEnrico Granata   };
1567223383edSEnrico Granata 
15680affb582SDave Lee   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
15697ca15ba7SLawrence D'Anna     StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
15700affb582SDave Lee     if (output_sp && interactive) {
157144d93782SGreg Clayton       output_sp->PutCString(g_python_command_instructions);
157244d93782SGreg Clayton       output_sp->Flush();
1573223383edSEnrico Granata     }
1574223383edSEnrico Granata   }
1575223383edSEnrico Granata 
1576b9c1b51eSKate Stone   void IOHandlerInputComplete(IOHandler &io_handler,
1577b9c1b51eSKate Stone                               std::string &data) override {
15787ca15ba7SLawrence D'Anna     StreamFileSP error_sp = io_handler.GetErrorStreamFileSP();
157944d93782SGreg Clayton 
15802b29b432SJonas Devlieghere     ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
1581b9c1b51eSKate Stone     if (interpreter) {
158244d93782SGreg Clayton 
158344d93782SGreg Clayton       StringList lines;
158444d93782SGreg Clayton       lines.SplitIntoLines(data);
1585b9c1b51eSKate Stone       if (lines.GetSize() > 0) {
1586a73b7df7SEnrico Granata         std::string funct_name_str;
1587b9c1b51eSKate Stone         if (interpreter->GenerateScriptAliasFunction(lines, funct_name_str)) {
1588b9c1b51eSKate Stone           if (funct_name_str.empty()) {
1589b9c1b51eSKate Stone             error_sp->Printf("error: unable to obtain a function name, didn't "
1590b9c1b51eSKate Stone                              "add python command.\n");
159144d93782SGreg Clayton             error_sp->Flush();
1592b9c1b51eSKate Stone           } else {
1593223383edSEnrico Granata             // everything should be fine now, let's add this alias
1594223383edSEnrico Granata 
1595b9c1b51eSKate Stone             CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(
1596771ef6d4SMalcolm Parsons                 m_interpreter, m_cmd_name, funct_name_str, m_short_help,
159744d93782SGreg Clayton                 m_synchronicity));
1598223383edSEnrico Granata 
1599b9c1b51eSKate Stone             if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp,
1600b9c1b51eSKate Stone                                               true)) {
1601b9c1b51eSKate Stone               error_sp->Printf("error: unable to add selected command, didn't "
1602b9c1b51eSKate Stone                                "add python command.\n");
160344d93782SGreg Clayton               error_sp->Flush();
1604223383edSEnrico Granata             }
1605223383edSEnrico Granata           }
1606b9c1b51eSKate Stone         } else {
1607b9c1b51eSKate Stone           error_sp->Printf(
1608b9c1b51eSKate Stone               "error: unable to create function, didn't add python command.\n");
160944d93782SGreg Clayton           error_sp->Flush();
161044d93782SGreg Clayton         }
1611b9c1b51eSKate Stone       } else {
161244d93782SGreg Clayton         error_sp->Printf("error: empty function, didn't add python command.\n");
161344d93782SGreg Clayton         error_sp->Flush();
161444d93782SGreg Clayton       }
1615b9c1b51eSKate Stone     } else {
1616b9c1b51eSKate Stone       error_sp->Printf(
1617b9c1b51eSKate Stone           "error: script interpreter missing, didn't add python command.\n");
161844d93782SGreg Clayton       error_sp->Flush();
161944d93782SGreg Clayton     }
162044d93782SGreg Clayton 
162144d93782SGreg Clayton     io_handler.SetIsDone(true);
162244d93782SGreg Clayton   }
1623223383edSEnrico Granata 
16245a988416SJim Ingham protected:
1625b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
162657179860SJonas Devlieghere     if (GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython) {
1627b9c1b51eSKate Stone       result.AppendError("only scripting language supported for scripted "
1628b9c1b51eSKate Stone                          "commands is currently Python");
162999f0b8f9SEnrico Granata       result.SetStatus(eReturnStatusFailed);
163099f0b8f9SEnrico Granata       return false;
163199f0b8f9SEnrico Granata     }
163299f0b8f9SEnrico Granata 
163311eb9c64SZachary Turner     if (command.GetArgumentCount() != 1) {
1634223383edSEnrico Granata       result.AppendError("'command script add' requires one argument");
1635223383edSEnrico Granata       result.SetStatus(eReturnStatusFailed);
1636223383edSEnrico Granata       return false;
1637223383edSEnrico Granata     }
1638223383edSEnrico Granata 
1639735152e3SEnrico Granata     // Store the options in case we get multi-line input
1640adcd0268SBenjamin Kramer     m_cmd_name = std::string(command[0].ref());
1641735152e3SEnrico Granata     m_short_help.assign(m_options.m_short_help);
164244d93782SGreg Clayton     m_synchronicity = m_options.m_synchronicity;
1643223383edSEnrico Granata 
1644b9c1b51eSKate Stone     if (m_options.m_class_name.empty()) {
1645b9c1b51eSKate Stone       if (m_options.m_funct_name.empty()) {
1646b9c1b51eSKate Stone         m_interpreter.GetPythonCommandsFromIOHandler(
1647b9c1b51eSKate Stone             "     ", // Prompt
1648a6faf851SJonas Devlieghere             *this);  // IOHandlerDelegate
1649b9c1b51eSKate Stone       } else {
1650b9c1b51eSKate Stone         CommandObjectSP new_cmd(new CommandObjectPythonFunction(
1651b9c1b51eSKate Stone             m_interpreter, m_cmd_name, m_options.m_funct_name,
1652b9c1b51eSKate Stone             m_options.m_short_help, m_synchronicity));
1653b9c1b51eSKate Stone         if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) {
1654223383edSEnrico Granata           result.SetStatus(eReturnStatusSuccessFinishNoResult);
1655b9c1b51eSKate Stone         } else {
1656223383edSEnrico Granata           result.AppendError("cannot add command");
1657223383edSEnrico Granata           result.SetStatus(eReturnStatusFailed);
1658223383edSEnrico Granata         }
1659223383edSEnrico Granata       }
1660b9c1b51eSKate Stone     } else {
16612b29b432SJonas Devlieghere       ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
1662b9c1b51eSKate Stone       if (!interpreter) {
16639fe00e52SEnrico Granata         result.AppendError("cannot find ScriptInterpreter");
16649fe00e52SEnrico Granata         result.SetStatus(eReturnStatusFailed);
16659fe00e52SEnrico Granata         return false;
16669fe00e52SEnrico Granata       }
16679fe00e52SEnrico Granata 
1668b9c1b51eSKate Stone       auto cmd_obj_sp = interpreter->CreateScriptCommandObject(
1669b9c1b51eSKate Stone           m_options.m_class_name.c_str());
1670b9c1b51eSKate Stone       if (!cmd_obj_sp) {
16719fe00e52SEnrico Granata         result.AppendError("cannot create helper object");
16729fe00e52SEnrico Granata         result.SetStatus(eReturnStatusFailed);
16739fe00e52SEnrico Granata         return false;
16749fe00e52SEnrico Granata       }
16759fe00e52SEnrico Granata 
1676b9c1b51eSKate Stone       CommandObjectSP new_cmd(new CommandObjectScriptingObject(
1677b9c1b51eSKate Stone           m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity));
1678b9c1b51eSKate Stone       if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) {
16799fe00e52SEnrico Granata         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1680b9c1b51eSKate Stone       } else {
16819fe00e52SEnrico Granata         result.AppendError("cannot add command");
16829fe00e52SEnrico Granata         result.SetStatus(eReturnStatusFailed);
16839fe00e52SEnrico Granata       }
16849fe00e52SEnrico Granata     }
1685223383edSEnrico Granata 
1686223383edSEnrico Granata     return result.Succeeded();
1687223383edSEnrico Granata   }
16885a988416SJim Ingham 
16895a988416SJim Ingham   CommandOptions m_options;
169044d93782SGreg Clayton   std::string m_cmd_name;
1691735152e3SEnrico Granata   std::string m_short_help;
169244d93782SGreg Clayton   ScriptedCommandSynchronicity m_synchronicity;
1693223383edSEnrico Granata };
1694223383edSEnrico Granata 
1695223383edSEnrico Granata // CommandObjectCommandsScriptList
1696223383edSEnrico Granata 
1697b9c1b51eSKate Stone class CommandObjectCommandsScriptList : public CommandObjectParsed {
1698223383edSEnrico Granata public:
1699b9c1b51eSKate Stone   CommandObjectCommandsScriptList(CommandInterpreter &interpreter)
1700b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command script list",
1701b9c1b51eSKate Stone                             "List defined scripted commands.", nullptr) {}
1702223383edSEnrico Granata 
17036e3d8e7fSEugene Zelenko   ~CommandObjectCommandsScriptList() override = default;
1704223383edSEnrico Granata 
1705b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1706d77ea5b2SRaphael Isemann     if (command.GetArgumentCount() != 0) {
1707d77ea5b2SRaphael Isemann       result.AppendError("'command script list' doesn't take any arguments");
1708d77ea5b2SRaphael Isemann       result.SetStatus(eReturnStatusFailed);
1709d77ea5b2SRaphael Isemann       return false;
1710d77ea5b2SRaphael Isemann     }
1711d77ea5b2SRaphael Isemann 
1712b9c1b51eSKate Stone     m_interpreter.GetHelp(result, CommandInterpreter::eCommandTypesUserDef);
1713223383edSEnrico Granata 
1714223383edSEnrico Granata     result.SetStatus(eReturnStatusSuccessFinishResult);
1715223383edSEnrico Granata 
1716223383edSEnrico Granata     return true;
1717223383edSEnrico Granata   }
1718223383edSEnrico Granata };
1719223383edSEnrico Granata 
1720223383edSEnrico Granata // CommandObjectCommandsScriptClear
1721223383edSEnrico Granata 
1722b9c1b51eSKate Stone class CommandObjectCommandsScriptClear : public CommandObjectParsed {
1723223383edSEnrico Granata public:
1724b9c1b51eSKate Stone   CommandObjectCommandsScriptClear(CommandInterpreter &interpreter)
1725b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command script clear",
1726b9c1b51eSKate Stone                             "Delete all scripted commands.", nullptr) {}
1727223383edSEnrico Granata 
17286e3d8e7fSEugene Zelenko   ~CommandObjectCommandsScriptClear() override = default;
1729223383edSEnrico Granata 
17305a988416SJim Ingham protected:
1731b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1732d77ea5b2SRaphael Isemann     if (command.GetArgumentCount() != 0) {
1733d77ea5b2SRaphael Isemann       result.AppendError("'command script clear' doesn't take any arguments");
1734d77ea5b2SRaphael Isemann       result.SetStatus(eReturnStatusFailed);
1735d77ea5b2SRaphael Isemann       return false;
1736d77ea5b2SRaphael Isemann     }
1737d77ea5b2SRaphael Isemann 
1738223383edSEnrico Granata     m_interpreter.RemoveAllUser();
1739223383edSEnrico Granata 
1740223383edSEnrico Granata     result.SetStatus(eReturnStatusSuccessFinishResult);
1741223383edSEnrico Granata 
1742223383edSEnrico Granata     return true;
1743223383edSEnrico Granata   }
1744223383edSEnrico Granata };
1745223383edSEnrico Granata 
1746223383edSEnrico Granata // CommandObjectCommandsScriptDelete
1747223383edSEnrico Granata 
1748b9c1b51eSKate Stone class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
1749223383edSEnrico Granata public:
1750b9c1b51eSKate Stone   CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter)
1751b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "command script delete",
1752b9c1b51eSKate Stone                             "Delete a scripted command.", nullptr) {
1753223383edSEnrico Granata     CommandArgumentEntry arg1;
1754223383edSEnrico Granata     CommandArgumentData cmd_arg;
1755223383edSEnrico Granata 
1756223383edSEnrico Granata     // Define the first (and only) variant of this arg.
1757223383edSEnrico Granata     cmd_arg.arg_type = eArgTypeCommandName;
1758223383edSEnrico Granata     cmd_arg.arg_repetition = eArgRepeatPlain;
1759223383edSEnrico Granata 
1760b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
1761b9c1b51eSKate Stone     // argument entry.
1762223383edSEnrico Granata     arg1.push_back(cmd_arg);
1763223383edSEnrico Granata 
1764223383edSEnrico Granata     // Push the data for the first argument into the m_arguments vector.
1765223383edSEnrico Granata     m_arguments.push_back(arg1);
1766223383edSEnrico Granata   }
1767223383edSEnrico Granata 
17686e3d8e7fSEugene Zelenko   ~CommandObjectCommandsScriptDelete() override = default;
1769223383edSEnrico Granata 
17702e8f304fSGongyu Deng   void
17712e8f304fSGongyu Deng   HandleArgumentCompletion(CompletionRequest &request,
17722e8f304fSGongyu Deng                            OptionElementVector &opt_element_vector) override {
17732e8f304fSGongyu Deng     if (!m_interpreter.HasCommands() || request.GetCursorIndex() != 0)
17742e8f304fSGongyu Deng       return;
17752e8f304fSGongyu Deng 
1776*2ebe30c6SRaphael Isemann     for (const auto &c : m_interpreter.GetUserCommands())
1777*2ebe30c6SRaphael Isemann       request.TryCompleteCurrentArg(c.first, c.second->GetHelp());
17782e8f304fSGongyu Deng   }
17792e8f304fSGongyu Deng 
17805a988416SJim Ingham protected:
1781b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1782223383edSEnrico Granata 
178311eb9c64SZachary Turner     if (command.GetArgumentCount() != 1) {
1784223383edSEnrico Granata       result.AppendError("'command script delete' requires one argument");
1785223383edSEnrico Granata       result.SetStatus(eReturnStatusFailed);
1786223383edSEnrico Granata       return false;
1787223383edSEnrico Granata     }
1788223383edSEnrico Granata 
17890d9a201eSRaphael Isemann     auto cmd_name = command[0].ref();
1790223383edSEnrico Granata 
17914574a890SZachary Turner     if (cmd_name.empty() || !m_interpreter.HasUserCommands() ||
17924574a890SZachary Turner         !m_interpreter.UserCommandExists(cmd_name)) {
1793867e7d17SZachary Turner       result.AppendErrorWithFormat("command %s not found", command[0].c_str());
1794223383edSEnrico Granata       result.SetStatus(eReturnStatusFailed);
17954574a890SZachary Turner       return false;
1796223383edSEnrico Granata     }
1797223383edSEnrico Granata 
17984574a890SZachary Turner     m_interpreter.RemoveUser(cmd_name);
17994574a890SZachary Turner     result.SetStatus(eReturnStatusSuccessFinishResult);
18004574a890SZachary Turner     return true;
1801223383edSEnrico Granata   }
1802223383edSEnrico Granata };
1803223383edSEnrico Granata 
1804223383edSEnrico Granata #pragma mark CommandObjectMultiwordCommandsScript
1805223383edSEnrico Granata 
1806223383edSEnrico Granata // CommandObjectMultiwordCommandsScript
1807223383edSEnrico Granata 
1808b9c1b51eSKate Stone class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword {
1809223383edSEnrico Granata public:
18107428a18cSKate Stone   CommandObjectMultiwordCommandsScript(CommandInterpreter &interpreter)
1811b9c1b51eSKate Stone       : CommandObjectMultiword(
1812a925974bSAdrian Prantl             interpreter, "command script",
1813a925974bSAdrian Prantl             "Commands for managing custom "
1814b9c1b51eSKate Stone             "commands implemented by "
1815b9c1b51eSKate Stone             "interpreter scripts.",
1816b9c1b51eSKate Stone             "command script <subcommand> [<subcommand-options>]") {
1817b9c1b51eSKate Stone     LoadSubCommand("add", CommandObjectSP(
1818b9c1b51eSKate Stone                               new CommandObjectCommandsScriptAdd(interpreter)));
1819b9c1b51eSKate Stone     LoadSubCommand(
1820b9c1b51eSKate Stone         "delete",
1821b9c1b51eSKate Stone         CommandObjectSP(new CommandObjectCommandsScriptDelete(interpreter)));
1822b9c1b51eSKate Stone     LoadSubCommand(
1823b9c1b51eSKate Stone         "clear",
1824b9c1b51eSKate Stone         CommandObjectSP(new CommandObjectCommandsScriptClear(interpreter)));
1825b9c1b51eSKate Stone     LoadSubCommand("list", CommandObjectSP(new CommandObjectCommandsScriptList(
1826b9c1b51eSKate Stone                                interpreter)));
1827b9c1b51eSKate Stone     LoadSubCommand(
1828b9c1b51eSKate Stone         "import",
1829b9c1b51eSKate Stone         CommandObjectSP(new CommandObjectCommandsScriptImport(interpreter)));
1830223383edSEnrico Granata   }
1831223383edSEnrico Granata 
18326e3d8e7fSEugene Zelenko   ~CommandObjectMultiwordCommandsScript() override = default;
1833223383edSEnrico Granata };
1834223383edSEnrico Granata 
1835ebc09c36SJim Ingham #pragma mark CommandObjectMultiwordCommands
1836ebc09c36SJim Ingham 
1837ebc09c36SJim Ingham // CommandObjectMultiwordCommands
1838ebc09c36SJim Ingham 
1839b9c1b51eSKate Stone CommandObjectMultiwordCommands::CommandObjectMultiwordCommands(
1840b9c1b51eSKate Stone     CommandInterpreter &interpreter)
1841b9c1b51eSKate Stone     : CommandObjectMultiword(interpreter, "command",
1842b9c1b51eSKate Stone                              "Commands for managing custom LLDB commands.",
1843b9c1b51eSKate Stone                              "command <subcommand> [<subcommand-options>]") {
1844b9c1b51eSKate Stone   LoadSubCommand("source",
1845b9c1b51eSKate Stone                  CommandObjectSP(new CommandObjectCommandsSource(interpreter)));
1846b9c1b51eSKate Stone   LoadSubCommand("alias",
1847b9c1b51eSKate Stone                  CommandObjectSP(new CommandObjectCommandsAlias(interpreter)));
1848b9c1b51eSKate Stone   LoadSubCommand("unalias", CommandObjectSP(
1849b9c1b51eSKate Stone                                 new CommandObjectCommandsUnalias(interpreter)));
1850b9c1b51eSKate Stone   LoadSubCommand("delete",
1851b9c1b51eSKate Stone                  CommandObjectSP(new CommandObjectCommandsDelete(interpreter)));
1852b9c1b51eSKate Stone   LoadSubCommand(
1853b9c1b51eSKate Stone       "regex", CommandObjectSP(new CommandObjectCommandsAddRegex(interpreter)));
1854b9c1b51eSKate Stone   LoadSubCommand("history", CommandObjectSP(
1855b9c1b51eSKate Stone                                 new CommandObjectCommandsHistory(interpreter)));
1856b9c1b51eSKate Stone   LoadSubCommand(
1857b9c1b51eSKate Stone       "script",
1858b9c1b51eSKate Stone       CommandObjectSP(new CommandObjectMultiwordCommandsScript(interpreter)));
1859ebc09c36SJim Ingham }
1860ebc09c36SJim Ingham 
18616e3d8e7fSEugene Zelenko CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands() = default;
1862