-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathCliCommand.cpp
241 lines (192 loc) · 7.16 KB
/
CliCommand.cpp
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
//==============================================================================
//
// CliCommand.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 "CliCommand.h"
#include <cstring>
#include <ostream>
#include <string>
#include "CliBuffer.h"
#include "CliThread.h"
#include "Debug.h"
#include "Formatters.h"
using std::ostream;
using std::string;
//------------------------------------------------------------------------------
namespace NodeBase
{
const int CliCommand::CommandWidth = 12;
const char CliCommand::CommandSeparator = '.';
fixed_string CliCommand::UnexpectedIndex = "unexpected index";
//------------------------------------------------------------------------------
//
// Invoked if trying to obtain another parameter when the parse tree
// has been exhausted. TYPE is the type of parameter that could not
// be obtained.
//
static CliParm::Rc Exhausted(const CliThread& cli, const string& type)
{
Debug::ft("NodeBase.Exhausted");
auto s = "Internal error: parameters exhausted before looking for " + type;
cli.ibuf->ErrorAtPos(cli, s);
return CliParm::Error;
}
//------------------------------------------------------------------------------
fn_name CliCommand_ctor = "CliCommand.ctor";
CliCommand::CliCommand(c_string comm, c_string help, uint32_t size) :
CliText(help, comm, false, size)
{
Debug::ft(CliCommand_ctor);
if((comm != nullptr) && (strlen(comm) > CommandWidth))
{
Debug::SwLog(CliCommand_ctor, "command name length", strlen(comm));
}
}
//------------------------------------------------------------------------------
CliCommand::~CliCommand()
{
Debug::ftnt("CliCommand.dtor");
}
//------------------------------------------------------------------------------
word CliCommand::ExplainCommand(ostream& stream, bool verbose) const
{
Debug::ft("CliCommand.ExplainCommand");
if(verbose)
{
// Provide full help by invoking Explain (on our base class) to
// display the purpose of the command and each of its parameters.
//
Explain(stream, 0);
}
else
{
// We are listing all of the commands in the increment. Display
// only each command's name and its purpose.
//
stream << spaces(CommandWidth - strlen(Text()));
stream << Text() << ParmExplPrefix << Help() << CRLF;
}
return 0;
}
//------------------------------------------------------------------------------
CliParm::Rc CliCommand::GetBoolParmRc(bool& b, CliThread& cli) const
{
Debug::ft("CliCommand.GetBoolParmRc");
// Return the next parameter, which should be a boolean.
//
auto parm = AccessParm(cli.Cookie(), 0);
if(parm == nullptr) return Exhausted(cli, "boolean");
return parm->GetBoolParmRc(b, cli);
}
//------------------------------------------------------------------------------
CliParm::Rc CliCommand::GetCharParmRc(char& c, CliThread& cli) const
{
Debug::ft("CliCommand.GetCharParmRc");
// Return the next parameter, which should be a character.
//
auto parm = AccessParm(cli.Cookie(), 0);
if(parm == nullptr) return Exhausted(cli, "character");
return parm->GetCharParmRc(c, cli);
}
//------------------------------------------------------------------------------
CliCommand::Rc CliCommand::GetFileNameRc(string& s, CliThread& cli) const
{
Debug::ft("CliCommand.GetFileNameRc");
// Return the next parameter, which should be a filename.
//
auto parm = AccessParm(cli.Cookie(), 0);
if(parm == nullptr) return Exhausted(cli, "filename");
return parm->GetFileNameRc(s, cli);
}
//------------------------------------------------------------------------------
CliParm::Rc CliCommand::GetIdentifierRc(string& s, CliThread& cli,
const string& valid, const string& exclude) const
{
Debug::ft("CliCommand.GetIdentifierRc");
// Return the next parameter, which should be an identifier.
//
auto parm = AccessParm(cli.Cookie(), 0);
if(parm == nullptr) return Exhausted(cli, "identifier");
return parm->GetIdentifierRc(s, cli, valid, exclude);
}
//------------------------------------------------------------------------------
CliParm::Rc CliCommand::GetIntParmRc(word& n, CliThread& cli) const
{
Debug::ft("CliCommand.GetIntParmRc");
// Return the next parameter, which should be an integer.
//
auto parm = AccessParm(cli.Cookie(), 0);
if(parm == nullptr) return Exhausted(cli, "integer");
return parm->GetIntParmRc(n, cli);
}
//------------------------------------------------------------------------------
CliParm::Rc CliCommand::GetPtrParmRc(void*& p, CliThread& cli) const
{
Debug::ft("CliCommand.GetPtrParmRc");
// Return the next parameter, which should be a pointer.
//
auto parm = AccessParm(cli.Cookie(), 0);
if(parm == nullptr) return Exhausted(cli, "pointer");
return parm->GetPtrParmRc(p, cli);
}
//------------------------------------------------------------------------------
CliParm::Rc CliCommand::GetStringRc(string& s, CliThread& cli) const
{
Debug::ft("CliCommand.GetStringRc");
// Return the next parameter, which can be any string.
//
auto parm = AccessParm(cli.Cookie(), 0);
if(parm == nullptr) return Exhausted(cli, "string");
return parm->GetStringRc(s, cli);
}
//------------------------------------------------------------------------------
CliParm::Rc CliCommand::GetTextParmRc(id_t& i, string& s, CliThread& cli) const
{
Debug::ft("CliCommand.GetTextParmRc");
// Return the next parameter, which should be a string in a specified list.
//
auto parm = AccessParm(cli.Cookie(), 0);
if(parm == nullptr) return Exhausted(cli, "text");
return parm->GetTextParmRc(i, s, cli);
}
//------------------------------------------------------------------------------
void CliCommand::Patch(sel_t selector, void* arguments)
{
CliText::Patch(selector, arguments);
}
//------------------------------------------------------------------------------
fn_name CliCommand_ProcessCommand = "CliCommand.ProcessCommand";
word CliCommand::ProcessCommand(CliThread& cli) const
{
Debug::ft(CliCommand_ProcessCommand);
Debug::SwLog(CliCommand_ProcessCommand, strOver(this), 0);
return -1;
}
//------------------------------------------------------------------------------
fn_name CliCommand_ProcessSubcommand = "CliCommand.ProcessSubcommand";
word CliCommand::ProcessSubcommand(CliThread& cli, id_t index) const
{
Debug::ft(CliCommand_ProcessSubcommand);
// This can be invoked to generate a log.
//
Debug::SwLog(CliCommand_ProcessSubcommand, UnexpectedIndex, index);
return -1;
}
}