-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathCliParm.cpp
More file actions
356 lines (262 loc) · 9.3 KB
/
Copy pathCliParm.cpp
File metadata and controls
356 lines (262 loc) · 9.3 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//==============================================================================
//
// CliParm.cpp
//
// Copyright (C) 2013-2025 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the Lesser GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the Lesser GNU General Public License
// along with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "CliParm.h"
#include <cstring>
#include <sstream>
#include "Algorithms.h"
#include "CliBuffer.h"
#include "CliThread.h"
#include "Debug.h"
#include "Formatters.h"
using std::ostream;
using std::string;
//------------------------------------------------------------------------------
namespace NodeBase
{
fixed_string CliParm::ParmExplPrefix = " : ";
fixed_string CliParm::AnyStringParm = "<str>";
const col_t CliParm::ParmWidth = 17;
const char CliParm::MandParmBegin = '(';
const char CliParm::MandParmEnd = ')';
const char CliParm::OptParmBegin = '[';
const char CliParm::OptParmEnd = ']';
//------------------------------------------------------------------------------
//
// Invoked if trying to obtain a parameter whose type differs from
// what the current parameter in the parse tree actually supports.
// TYPE is the type of incorrect parameter.
//
static CliParm::Rc Mismatch(const CliThread& cli, const string& type)
{
Debug::ft("NodeBase.Mismatch");
auto s = "Internal error: parameter mismatch when looking for " + type;
cli.ibuf->ErrorAtPos(cli, s);
return CliParm::Error;
}
//------------------------------------------------------------------------------
fn_name CliParm_ctor = "CliParm.ctor";
CliParm::CliParm(c_string help, bool opt, c_string tag) :
help_(help),
opt_(opt),
tag_(tag)
{
Debug::ft(CliParm_ctor);
Debug::Assert(help_ != nullptr);
auto size = strlen(help_);
auto total = ParmWidth + strlen(ParmExplPrefix) + size;
if(size == 0)
Debug::SwLog(CliParm_ctor, "help string empty", size);
else if(total >= COUT_LENGTH_MAX)
Debug::SwLog
(CliParm_ctor, "help string too long", total - COUT_LENGTH_MAX + 1);
}
//------------------------------------------------------------------------------
fn_name CliParm_dtor = "CliParm.dtor";
CliParm::~CliParm()
{
Debug::ftnt(CliParm_dtor);
Debug::SwLog(CliParm_dtor, UnexpectedInvocation, 0);
}
//------------------------------------------------------------------------------
CliParm* CliParm::AccessParm(CliCookie& cookie, uint32_t depth) const
{
Debug::ft("CliParm.AccessParm");
// AccessParm essentially finds subparameters. A basic parameter (an int,
// bool, char, or pointer) has no subparameters, so return nullptr. Only
// strings (CliText and its subclasses) support subparameters.
//
return nullptr;
}
//------------------------------------------------------------------------------
ptrdiff_t CliParm::CellDiff()
{
uintptr_t local;
auto fake = reinterpret_cast<const CliParm*>(&local);
return ptrdiff(&fake->pid_, fake);
}
//------------------------------------------------------------------------------
void CliParm::Display(ostream& stream,
const string& prefix, const Flags& options) const
{
Immutable::Display(stream, prefix, options);
stream << prefix << "pid : " << pid_.to_str() << CRLF;
stream << prefix << "opt : " << opt_ << CRLF;
if(tag_ == nullptr) return;
stream << prefix << "tag : " << tag_ << CRLF;
}
//------------------------------------------------------------------------------
void CliParm::Explain(ostream& stream, col_t indent) const
{
Debug::ft("CliParm.Explain");
std::ostringstream buff;
string values;
auto mand = ShowValues(values);
auto opt = IsOptional();
auto tag = Tag();
// Display the parameter's legal values, surrounded by indicators
// that specify whether it is mandatory or optional, and followed
// by an explanation of its purpose.
//
buff << spaces(indent);
if(opt)
{
if(tag != nullptr) buff << tag << CliBuffer::OptTagChar;
buff << OptParmBegin;
}
else if(mand)
{
buff << MandParmBegin;
}
buff << values;
if(opt)
buff << OptParmEnd;
else if(mand)
buff << MandParmEnd;
stream << buff.str() << spaces(ParmWidth - buff.str().size());
stream << ParmExplPrefix << Help() << CRLF;
}
//------------------------------------------------------------------------------
bool CliParm::GetBoolParm(bool& b, CliThread& cli) const
{
Debug::ft("CliParm.GetBoolParm");
return (GetBoolParmRc(b, cli) == Ok);
}
//------------------------------------------------------------------------------
CliParm::Rc CliParm::GetBoolParmRc(bool& b, CliThread& cli) const
{
Debug::ft("CliParm.GetBoolParmRc");
return Mismatch(cli, "boolean");
}
//------------------------------------------------------------------------------
bool CliParm::GetCharParm(char& c, CliThread& cli) const
{
Debug::ft("CliParm.GetCharParm");
return (GetCharParmRc(c, cli) == Ok);
}
//------------------------------------------------------------------------------
CliParm::Rc CliParm::GetCharParmRc(char& c, CliThread& cli) const
{
Debug::ft("CliParm.GetCharParmRc");
return Mismatch(cli, "character");
}
//------------------------------------------------------------------------------
bool CliParm::GetFileName(string& s, CliThread& cli) const
{
Debug::ft("CliParm.GetFileName");
return (GetFileNameRc(s, cli) == Ok);
}
//------------------------------------------------------------------------------
CliParm::Rc CliParm::GetFileNameRc(string& s, CliThread& cli) const
{
Debug::ft("CliParm.GetFileNameRc");
return Mismatch(cli, "filename");
}
//------------------------------------------------------------------------------
bool CliParm::GetIdentifier(string& s, CliThread& cli,
const string& valid, const string& exclude) const
{
Debug::ft("CliParm.GetIdentifier");
return (GetIdentifierRc(s, cli, valid, exclude) == Ok);
}
//------------------------------------------------------------------------------
CliParm::Rc CliParm::GetIdentifierRc(string& s, CliThread& cli,
const string& valid, const string& exclude) const
{
Debug::ft("CliParm.GetIdentifierRc");
return Mismatch(cli, "identifier");
}
//------------------------------------------------------------------------------
bool CliParm::GetIntParm(word& n, CliThread& cli) const
{
Debug::ft("CliParm.GetIntParm");
return (GetIntParmRc(n, cli) == Ok);
}
//------------------------------------------------------------------------------
CliParm::Rc CliParm::GetIntParmRc(word& n, CliThread& cli) const
{
Debug::ft("CliParm.GetIntParmRc");
return Mismatch(cli, "integer");
}
//------------------------------------------------------------------------------
bool CliParm::GetPtrParm(void*& p, CliThread& cli) const
{
Debug::ft("CliParm.GetPtrParm");
return (GetPtrParmRc(p, cli) == Ok);
}
//------------------------------------------------------------------------------
CliParm::Rc CliParm::GetPtrParmRc(void*& p, CliThread& cli) const
{
Debug::ft("CliParm.GetPtrParmRc");
return Mismatch(cli, "pointer");
}
//------------------------------------------------------------------------------
bool CliParm::GetString(string& s, CliThread& cli) const
{
Debug::ft("CliParm.GetString");
return (GetStringRc(s, cli) == Ok);
}
//------------------------------------------------------------------------------
CliParm::Rc CliParm::GetStringRc(string& s, CliThread& cli) const
{
Debug::ft("CliParm.GetStringRc");
return Mismatch(cli, "string");
}
//------------------------------------------------------------------------------
bool CliParm::GetTextIndex(id_t& i, CliThread& cli) const
{
Debug::ft("CliParm.GetTextIndex");
string s;
return (GetTextParmRc(i, s, cli) == Ok);
}
//------------------------------------------------------------------------------
CliParm::Rc CliParm::GetTextIndexRc(id_t& i, CliThread& cli) const
{
Debug::ft("CliParm.GetTextIndexRc");
string s;
return GetTextParmRc(i, s, cli);
}
//------------------------------------------------------------------------------
bool CliParm::GetTextParm(id_t& i, string& s, CliThread& cli) const
{
Debug::ft("CliParm.GetTextParm");
return (GetTextParmRc(i, s, cli) == Ok);
}
//------------------------------------------------------------------------------
CliParm::Rc CliParm::GetTextParmRc(id_t& i, string& s, CliThread& cli) const
{
Debug::ft("CliParm.GetTextParmRc");
return Mismatch(cli, "text");
}
//------------------------------------------------------------------------------
void CliParm::Patch(sel_t selector, void* arguments)
{
Immutable::Patch(selector, arguments);
}
//------------------------------------------------------------------------------
fn_name CliParm_ShowValues = "CliParm.ShowValues";
bool CliParm::ShowValues(string& values) const
{
Debug::ft(CliParm_ShowValues);
Debug::SwLog(CliParm_ShowValues, strOver(this), 0);
return false;
}
}