aboutsummaryrefslogtreecommitdiff
path: root/tools/lldb-mi/MICmdArgValOptionLong.cpp
blob: 0c29982e26343a70aedbb880e0d3db56394e4255 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//===-- MICmdArgValOptionLong.cpp -------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

//++
// File:        MICmdArgValOptionLong.cpp
//
// Overview:    CMICmdArgValOptionLong implementation.
//
// Environment: Compilers:  Visual C++ 12.
//                          gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
//              Libraries:  See MIReadmetxt.
//
// Copyright:   None.
//--

// In-house headers:
#include "MICmdArgValOptionLong.h"
#include "MICmdArgContext.h"

//++ ------------------------------------------------------------------------------------
// Details: CMICmdArgValOptionLong constructor.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdArgValOptionLong::CMICmdArgValOptionLong(void)
    : m_nExpectingNOptions(0)
    , m_eExpectingOptionType(eArgValType_invalid)
{
}

//++ ------------------------------------------------------------------------------------
// Details: CMICmdArgValOptionLong constructor.
// Type:    Method.
// Args:    vrArgName       - (R) Argument's name to search by.
//          vbMandatory     - (R) True = Yes must be present, false = optional argument.
//          vbHandleByCmd   - (R) True = Command processes *this option, false = not handled.
// Return:  None.
// Throws:  None.
//--
CMICmdArgValOptionLong::CMICmdArgValOptionLong(const CMIUtilString &vrArgName, const bool vbMandatory, const bool vbHandleByCmd)
    : CMICmdArgValListBase(vrArgName, vbMandatory, vbHandleByCmd)
    , m_nExpectingNOptions(0)
    , m_eExpectingOptionType(eArgValType_invalid)
{
}

//++ ------------------------------------------------------------------------------------
// Details: CMICmdArgValOptionLong constructor.
// Type:    Method.
// Args:    vrArgName           - (R) Argument's name to search by.
//          vbMandatory         - (R) True = Yes must be present, false = optional argument.
//          vbHandleByCmd       - (R) True = Command processes *this option, false = not handled.
//          veType              - (R) The type of argument to look for and create argument object of a certain type.
//          vnExpectingNOptions - (R) The number of options expected to read following *this argument.
// Return:  None.
// Throws:  None.
//--
CMICmdArgValOptionLong::CMICmdArgValOptionLong(const CMIUtilString &vrArgName, const bool vbMandatory, const bool vbHandleByCmd,
                                               const ArgValType_e veType, const MIuint vnExpectingNOptions)
    : CMICmdArgValListBase(vrArgName, vbMandatory, vbHandleByCmd)
    , m_nExpectingNOptions(vnExpectingNOptions)
    , m_eExpectingOptionType(veType)
{
}

//++ ------------------------------------------------------------------------------------
// Details: CMICmdArgValOptionLong destructor.
// Type:    Overridden.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmdArgValOptionLong::~CMICmdArgValOptionLong(void)
{
    // Tidy up
    Destroy();
}

//++ ------------------------------------------------------------------------------------
// Details: Tear down resources used by *this object.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
void
CMICmdArgValOptionLong::Destroy(void)
{
    // Tidy up
    VecArgObjPtr_t::const_iterator it = m_vecArgsExpected.begin();
    while (it != m_vecArgsExpected.end())
    {
        CMICmdArgValBase *pOptionObj = *it;
        delete pOptionObj;

        // Next
        ++it;
    }
    m_vecArgsExpected.clear();
}

//++ ------------------------------------------------------------------------------------
// Details: Parse the command's argument options string and try to extract the long
//          argument *this argument type is looking for.
// Type:    Overridden.
// Args:    vwArgContext    - (RW) The command's argument options string.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool
CMICmdArgValOptionLong::Validate(CMICmdArgContext &vwArgContext)
{
    if (vwArgContext.IsEmpty())
        return MIstatus::success;

    if (vwArgContext.GetNumberArgsPresent() == 1)
    {
        const CMIUtilString &rArg(vwArgContext.GetArgsLeftToParse());
        if (IsArgLongOption(rArg) && ArgNameMatch(rArg))
        {
            m_bFound = true;

            if (!vwArgContext.RemoveArg(rArg))
                return MIstatus::failure;

            if (m_nExpectingNOptions == 0)
            {
                m_bValid = true;
                return MIstatus::success;
            }

            m_bIsMissingOptions = true;
            return MIstatus::failure;
        }
        else
            return MIstatus::failure;
    }

    // More than one option...
    MIuint nArgIndex = 0;
    const CMIUtilString::VecString_t vecOptions(vwArgContext.GetArgs());
    CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
    while (it != vecOptions.end())
    {
        const CMIUtilString &rArg(*it);
        if (IsArgOptionCorrect(rArg) && ArgNameMatch(rArg))
        {
            m_bFound = true;

            if (!vwArgContext.RemoveArg(rArg))
                return MIstatus::failure;

            if (m_nExpectingNOptions != 0)
            {
                if (ExtractExpectedOptions(vwArgContext, nArgIndex))
                {
                    m_bValid = true;
                    return MIstatus::success;
                }

                m_bIsMissingOptions = true;
                return MIstatus::failure;
            }
            else
            {
                m_bValid = true;
                return MIstatus::success;
            }
        }

        // Next
        ++it;
        ++nArgIndex;
    }

    return MIstatus::failure;
}

//++ ------------------------------------------------------------------------------------
// Details: Parse the text following *this argument and extract the options the values of
//          CMICmdArgValListBase::m_eArgType forming argument objects for each of those
//          options extracted.
// Type:    Method.
// Args:    vrwTxt      - (RW)  The command's argument options string.
//          nArgIndex   - (R)   The Nth arg position in argument context from the left.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool
CMICmdArgValOptionLong::ExtractExpectedOptions(CMICmdArgContext &vrwTxt, const MIuint nArgIndex)
{
    CMIUtilString::VecString_t vecOptions;
    MIuint nOptionsPresent = 0;
    if ((m_eExpectingOptionType != eArgValType_StringQuoted) && (m_eExpectingOptionType != eArgValType_StringQuotedNumber) &&
        (m_eExpectingOptionType != eArgValType_StringQuotedNumberPath))
        nOptionsPresent = vrwTxt.GetArgsLeftToParse().Split(" ", vecOptions);
    else
        nOptionsPresent = vrwTxt.GetArgsLeftToParse().SplitConsiderQuotes(" ", vecOptions);
    if (nOptionsPresent == 0)
        return MIstatus::failure;

    MIuint nArgIndexCnt = 0;
    MIuint nTypeCnt = 0;
    MIuint nTypeCnt2 = 0;
    MIuint nFoundNOptionsCnt = 0;
    CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
    while (it != vecOptions.end())
    {
        // Move to the Nth argument position from left before do validation/checking
        if (nArgIndexCnt++ == nArgIndex)
        {
            nTypeCnt++;
            const CMIUtilString &rOption(*it);
            if (IsExpectedCorrectType(rOption, m_eExpectingOptionType))
            {
                nTypeCnt2++;
                CMICmdArgValBase *pOptionObj = CreationObj(rOption, m_eExpectingOptionType);
                if ((pOptionObj != nullptr) && vrwTxt.RemoveArgAtPos(rOption, nArgIndex))
                {
                    nFoundNOptionsCnt++;
                    m_vecArgsExpected.push_back(pOptionObj);
                }
            }

            // Is the sequence 'options' of same type broken. Expecting the same type until the
            // next argument.
            if (nTypeCnt != nTypeCnt2)
                return MIstatus::failure;

            if (nFoundNOptionsCnt == m_nExpectingNOptions)
                return MIstatus::success;
        }

        // Next
        ++it;
    }
    if (nFoundNOptionsCnt != m_nExpectingNOptions)
        return MIstatus::failure;

    return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details: Examine the string and determine if it is a valid long type option argument.
//          Long type argument looks like --someLongOption.
// Type:    Method.
// Args:    vrTxt   - (R) Some text.
// Return:  bool    - True = yes valid arg, false = no.
// Throws:  None.
//--
bool
CMICmdArgValOptionLong::IsArgLongOption(const CMIUtilString &vrTxt) const
{
    const bool bHavePosSlash = (vrTxt.find_first_of("/") != std::string::npos);
    const bool bHaveBckSlash = (vrTxt.find_first_of("\\") != std::string::npos);
    if (bHavePosSlash || bHaveBckSlash)
        return false;

    const MIint nPos = vrTxt.find_first_of("--");
    if (nPos != 0)
        return false;

    if (vrTxt.length() < 3)
        return false;

    const CMIUtilString strArg = vrTxt.substr(2).c_str();
    if (strArg.IsNumber())
        return false;

    return true;
}

//++ ------------------------------------------------------------------------------------
// Details: Examine the string and determine if it is a valid long type option argument.
//          Long type argument looks like --someLongOption.
// Type:    Overideable.
// Args:    vrTxt   - (R) Some text.
// Return:  bool    - True = yes valid arg, false = no.
// Throws:  None.
//--
bool
CMICmdArgValOptionLong::IsArgOptionCorrect(const CMIUtilString &vrTxt) const
{
    return IsArgLongOption(vrTxt);
}

//++ ------------------------------------------------------------------------------------
// Details: Does the argument name of the argument being parsed ATM match the name of
//          *this argument object.
// Type:    Method.
// Args:    vrTxt   - (R) Some text.
// Return:  bool    - True = yes arg name matched, false = no.
// Throws:  None.
//--
bool
CMICmdArgValOptionLong::ArgNameMatch(const CMIUtilString &vrTxt) const
{
    const CMIUtilString strArg = vrTxt.substr(2).c_str();
    return (strArg == GetName());
}

//++ ------------------------------------------------------------------------------------
// Details: Retrieve the list of CMICmdArgValBase derived option objects found following
//          *this long option argument. For example "list-thread-groups [ --recurse 1 ]"
//          where 1 is the list of expected option to follow.
// Type:    Method.
// Args:    None.
// Return:  CMICmdArgValListBase::VecArgObjPtr_t & - List of options.
// Throws:  None.
//--
const CMICmdArgValListBase::VecArgObjPtr_t &
CMICmdArgValOptionLong::GetExpectedOptions(void) const
{
    return m_vecArgsExpected;
}