]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/Interpreter/CommandObjectRegexCommand.cpp
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / Interpreter / CommandObjectRegexCommand.cpp
1 //===-- CommandObjectRegexCommand.cpp ---------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Interpreter/CommandObjectRegexCommand.h"
10
11 #include "lldb/Interpreter/CommandInterpreter.h"
12 #include "lldb/Interpreter/CommandReturnObject.h"
13
14 using namespace lldb;
15 using namespace lldb_private;
16
17 // CommandObjectRegexCommand constructor
18 CommandObjectRegexCommand::CommandObjectRegexCommand(
19     CommandInterpreter &interpreter, llvm::StringRef name, llvm::StringRef help,
20   llvm::StringRef syntax, uint32_t max_matches, uint32_t completion_type_mask,
21     bool is_removable)
22     : CommandObjectRaw(interpreter, name, help, syntax),
23       m_max_matches(max_matches), m_completion_type_mask(completion_type_mask),
24       m_entries(), m_is_removable(is_removable) {}
25
26 // Destructor
27 CommandObjectRegexCommand::~CommandObjectRegexCommand() {}
28
29 bool CommandObjectRegexCommand::DoExecute(llvm::StringRef command,
30                                           CommandReturnObject &result) {
31   EntryCollection::const_iterator pos, end = m_entries.end();
32   for (pos = m_entries.begin(); pos != end; ++pos) {
33     RegularExpression::Match regex_match(m_max_matches);
34
35     if (pos->regex.Execute(command, &regex_match)) {
36       std::string new_command(pos->command);
37       std::string match_str;
38       char percent_var[8];
39       size_t idx, percent_var_idx;
40       for (uint32_t match_idx = 1; match_idx <= m_max_matches; ++match_idx) {
41         if (regex_match.GetMatchAtIndex(command, match_idx, match_str)) {
42           const int percent_var_len =
43               ::snprintf(percent_var, sizeof(percent_var), "%%%u", match_idx);
44           for (idx = 0; (percent_var_idx = new_command.find(
45                              percent_var, idx)) != std::string::npos;) {
46             new_command.erase(percent_var_idx, percent_var_len);
47             new_command.insert(percent_var_idx, match_str);
48             idx += percent_var_idx + match_str.size();
49           }
50         }
51       }
52       // Interpret the new command and return this as the result!
53       if (m_interpreter.GetExpandRegexAliases())
54         result.GetOutputStream().Printf("%s\n", new_command.c_str());
55       // Pass in true for "no context switching".  The command that called us
56       // should have set up the context appropriately, we shouldn't have to
57       // redo that.
58       return m_interpreter.HandleCommand(
59           new_command.c_str(), eLazyBoolCalculate, result, nullptr, true, true);
60     }
61   }
62   result.SetStatus(eReturnStatusFailed);
63   if (!GetSyntax().empty())
64     result.AppendError(GetSyntax());
65   else
66     result.GetOutputStream() << "Command contents '" << command
67                              << "' failed to match any "
68                                 "regular expression in the '"
69                              << m_cmd_name << "' regex ";
70   return false;
71 }
72
73 bool CommandObjectRegexCommand::AddRegexCommand(const char *re_cstr,
74                                                 const char *command_cstr) {
75   m_entries.resize(m_entries.size() + 1);
76   // Only add the regular expression if it compiles
77   if (m_entries.back().regex.Compile(
78           llvm::StringRef::withNullAsEmpty(re_cstr))) {
79     m_entries.back().command.assign(command_cstr);
80     return true;
81   }
82   // The regex didn't compile...
83   m_entries.pop_back();
84   return false;
85 }
86
87 int CommandObjectRegexCommand::HandleCompletion(CompletionRequest &request) {
88   if (m_completion_type_mask) {
89     CommandCompletions::InvokeCommonCompletionCallbacks(
90         GetCommandInterpreter(), m_completion_type_mask, request, nullptr);
91     return request.GetNumberOfMatches();
92   } else {
93     request.SetWordComplete(false);
94   }
95   return 0;
96 }