]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdArgContext.cpp
Merge lldb trunk r366426, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdArgContext.cpp
1 //===-- MICmdArgContext.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 // In-house headers:
10 #include "MICmdArgContext.h"
11
12 //++
13 // Details: CMICmdArgContext constructor.
14 // Type:    Method.
15 // Args:    None.
16 // Return:  None.
17 // Throws:  None.
18 //--
19 CMICmdArgContext::CMICmdArgContext() {}
20
21 //++
22 // Details: CMICmdArgContext constructor.
23 // Type:    Method.
24 // Args:    vrCmdLineArgsRaw    - (R) The text description of the arguments
25 // options.
26 // Return:  None.
27 // Throws:  None.
28 //--
29 CMICmdArgContext::CMICmdArgContext(const CMIUtilString &vrCmdLineArgsRaw)
30     : m_strCmdArgsAndOptions(vrCmdLineArgsRaw) {}
31
32 //++
33 // Details: CMICmdArgContext destructor.
34 // Type:    Method.
35 // Args:    None.
36 // Return:  None.
37 // Throws:  None.
38 //--
39 CMICmdArgContext::~CMICmdArgContext() {}
40
41 //++
42 // Details: Retrieve the remainder of the command's argument options left to
43 // parse.
44 // Type:    Method.
45 // Args:    None.
46 // Return:  CMIUtilString & - Argument options text.
47 // Throws:  None.
48 //--
49 const CMIUtilString &CMICmdArgContext::GetArgsLeftToParse() const {
50   return m_strCmdArgsAndOptions;
51 }
52
53 //++
54 // Details: Ask if this arguments string has any arguments.
55 // Type:    Method.
56 // Args:    None.
57 // Return:  bool    - True = Has one or more arguments present, false = no
58 // arguments.
59 // Throws:  None.
60 //--
61 bool CMICmdArgContext::IsEmpty() const {
62   return m_strCmdArgsAndOptions.empty();
63 }
64
65 //++
66 // Details: Remove the argument from the options text and any space after the
67 // argument
68 //          if applicable.
69 // Type:    Method.
70 // Args:    vArg    - (R) The name of the argument.
71 // Return:  MIstatus::success - Functional succeeded.
72 //          MIstatus::failure - Functional failed.
73 // Throws:  None.
74 //--
75 bool CMICmdArgContext::RemoveArg(const CMIUtilString &vArg) {
76   if (vArg.empty())
77     return MIstatus::success;
78
79   const size_t nLen = vArg.length();
80   const size_t nLenCntxt = m_strCmdArgsAndOptions.length();
81   if (nLen > nLenCntxt)
82     return MIstatus::failure;
83
84   size_t nExtraSpace = 0;
85   size_t nPos = m_strCmdArgsAndOptions.find(vArg);
86   while (1) {
87     if (nPos == std::string::npos)
88       return MIstatus::success;
89
90     bool bPass1 = false;
91     if (nPos != 0) {
92       if (m_strCmdArgsAndOptions[nPos - 1] == ' ')
93         bPass1 = true;
94     } else
95       bPass1 = true;
96
97     const size_t nEnd = nPos + nLen;
98
99     if (bPass1) {
100       bool bPass2 = false;
101       if (nEnd < nLenCntxt) {
102         if (m_strCmdArgsAndOptions[nEnd] == ' ') {
103           bPass2 = true;
104           nExtraSpace = 1;
105         }
106       } else
107         bPass2 = true;
108
109       if (bPass2)
110         break;
111     }
112
113     nPos = m_strCmdArgsAndOptions.find(vArg, nEnd);
114   }
115
116   const size_t nPosEnd = nLen + nExtraSpace;
117   m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.replace(nPos, nPosEnd, "");
118   m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
119
120   return MIstatus::success;
121 }
122
123 //++
124 // Details: Remove the argument at the Nth word position along in the context
125 // string.
126 //          Any space after the argument is removed if applicable. A search is
127 //          not
128 //          performed as there may be more than one vArg with the same 'name' in
129 //          the
130 //          context string.
131 // Type:    Method.
132 // Args:    vArg        - (R) The name of the argument.
133 //          nArgIndex   - (R) The word count position to which to remove the
134 //          vArg word.
135 // Return:  MIstatus::success - Functional succeeded.
136 //          MIstatus::failure - Functional failed.
137 // Throws:  None.
138 //--
139 bool CMICmdArgContext::RemoveArgAtPos(const CMIUtilString &vArg,
140                                       size_t nArgIndex) {
141   size_t nWordIndex = 0;
142   CMIUtilString strBuildContextUp;
143   const CMIUtilString::VecString_t vecWords(GetArgs());
144   const bool bSpaceRequired(GetNumberArgsPresent() > 2);
145
146   CMIUtilString::VecString_t::const_iterator it = vecWords.begin();
147   const CMIUtilString::VecString_t::const_iterator itEnd = vecWords.end();
148   while (it != itEnd) {
149     const CMIUtilString &rWord(*it);
150     if (nWordIndex++ != nArgIndex) {
151       // Single words
152       strBuildContextUp += rWord;
153       if (bSpaceRequired)
154         strBuildContextUp += " ";
155     } else {
156       // If quoted loose quoted text
157       if (++it != itEnd) {
158         CMIUtilString words = rWord;
159         while (vArg != words) {
160           if (bSpaceRequired)
161             words += " ";
162           words += *it;
163           if (++it == itEnd)
164             break;
165         }
166         if (it != itEnd)
167           --it;
168       }
169     }
170
171     // Next
172     if (it != itEnd)
173       ++it;
174   }
175
176   m_strCmdArgsAndOptions = strBuildContextUp;
177   m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
178
179   return MIstatus::success;
180 }
181
182 //++
183 // Details: Retrieve number of arguments or options present in the command's
184 // option text.
185 // Type:    Method.
186 // Args:    None.
187 // Return:  size_t  - 0 to n arguments present.
188 // Throws:  None.
189 //--
190 size_t CMICmdArgContext::GetNumberArgsPresent() const {
191   CMIUtilString::VecString_t vecOptions;
192   return m_strCmdArgsAndOptions.SplitConsiderQuotes(" ", vecOptions);
193 }
194
195 //++
196 // Details: Retrieve all the arguments or options remaining in *this context.
197 // Type:    Method.
198 // Args:    None.
199 // Return:  MIUtilString::VecString_t   - List of args remaining.
200 // Throws:  None.
201 //--
202 CMIUtilString::VecString_t CMICmdArgContext::GetArgs() const {
203   CMIUtilString::VecString_t vecOptions;
204   m_strCmdArgsAndOptions.SplitConsiderQuotes(" ", vecOptions);
205   return vecOptions;
206 }
207
208 //++
209 // Details: Copy assignment operator.
210 // Type:    Method.
211 // Args:    vOther  - (R) The variable to copy from.
212 // Return:  CMIUtilString & - this object.
213 // Throws:  None.
214 //--
215 CMICmdArgContext &CMICmdArgContext::operator=(const CMICmdArgContext &vOther) {
216   if (this != &vOther) {
217     m_strCmdArgsAndOptions = vOther.m_strCmdArgsAndOptions;
218   }
219
220   return *this;
221 }