blob: 5e59382d7b94300fc55826da8b0733138b4b4468 [file] [log] [blame]
Junxiao Shi64567bb2016-09-04 16:00:27 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry84d3adc2017-08-09 23:31:40 -04002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Junxiao Shi64567bb2016-09-04 16:00:27 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "command-definition.hpp"
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040027#include "status-report.hpp"
Davide Pesavento8b663a92018-11-21 22:57:20 -050028
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050029#include <ndn-cxx/net/face-uri.hpp>
30#include <ndn-cxx/util/backports.hpp>
Junxiao Shi64567bb2016-09-04 16:00:27 +000031#include <ndn-cxx/util/logger.hpp>
32
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050033#include <boost/lexical_cast.hpp>
34
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::tools::nfdc {
Junxiao Shi64567bb2016-09-04 16:00:27 +000036
Junxiao Shic143afe2016-09-20 13:04:51 +000037NDN_LOG_INIT(nfdc.CommandDefinition);
Junxiao Shi64567bb2016-09-04 16:00:27 +000038
39std::ostream&
40operator<<(std::ostream& os, ArgValueType vt)
41{
42 switch (vt) {
43 case ArgValueType::NONE:
44 return os << "none";
45 case ArgValueType::ANY:
46 return os << "any";
Eric Newberry84d3adc2017-08-09 23:31:40 -040047 case ArgValueType::BOOLEAN:
48 return os << "boolean";
Junxiao Shi64567bb2016-09-04 16:00:27 +000049 case ArgValueType::UNSIGNED:
50 return os << "non-negative integer";
51 case ArgValueType::STRING:
52 return os << "string";
53 case ArgValueType::REPORT_FORMAT:
54 return os << "ReportFormat";
55 case ArgValueType::NAME:
56 return os << "Name";
57 case ArgValueType::FACE_URI:
58 return os << "FaceUri";
59 case ArgValueType::FACE_ID_OR_URI:
60 return os << "FaceId or FaceUri";
61 case ArgValueType::FACE_PERSISTENCY:
62 return os << "FacePersistency";
Junxiao Shi8eda6822017-04-12 02:53:14 +000063 case ArgValueType::ROUTE_ORIGIN:
64 return os << "RouteOrigin";
Junxiao Shi64567bb2016-09-04 16:00:27 +000065 }
66 return os << static_cast<int>(vt);
67}
68
69static std::string
70getMetavarFromType(ArgValueType vt)
71{
72 switch (vt) {
73 case ArgValueType::NONE:
74 return "";
75 case ArgValueType::ANY:
76 return "args";
Eric Newberry84d3adc2017-08-09 23:31:40 -040077 case ArgValueType::BOOLEAN:
78 return "bool";
Junxiao Shi64567bb2016-09-04 16:00:27 +000079 case ArgValueType::UNSIGNED:
80 return "uint";
81 case ArgValueType::STRING:
82 return "str";
83 case ArgValueType::REPORT_FORMAT:
84 return "fmt";
85 case ArgValueType::NAME:
86 return "name";
87 case ArgValueType::FACE_URI:
88 return "uri";
89 case ArgValueType::FACE_ID_OR_URI:
90 return "face";
91 case ArgValueType::FACE_PERSISTENCY:
92 return "persistency";
Junxiao Shi8eda6822017-04-12 02:53:14 +000093 case ArgValueType::ROUTE_ORIGIN:
94 return "origin";
Junxiao Shi64567bb2016-09-04 16:00:27 +000095 }
Davide Pesavento5a897692019-10-31 01:28:43 -040096 NDN_CXX_UNREACHABLE;
Junxiao Shi64567bb2016-09-04 16:00:27 +000097}
98
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040099CommandDefinition::CommandDefinition(std::string_view noun, std::string_view verb)
Junxiao Shi64567bb2016-09-04 16:00:27 +0000100 : m_noun(noun)
101 , m_verb(verb)
102{
103}
104
105CommandDefinition::~CommandDefinition() = default;
106
107CommandDefinition&
108CommandDefinition::addArg(const std::string& name, ArgValueType valueType,
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400109 Required isRequired, Positional allowPositional,
110 const std::string& metavar)
Junxiao Shi64567bb2016-09-04 16:00:27 +0000111{
112 bool isNew = m_args.emplace(name,
113 Arg{name, valueType, static_cast<bool>(isRequired),
114 metavar.empty() ? getMetavarFromType(valueType) : metavar}).second;
Junxiao Shi737c43c2016-09-14 02:51:44 +0000115 BOOST_VERIFY(isNew);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000116
117 if (static_cast<bool>(isRequired)) {
118 m_requiredArgs.insert(name);
119 }
120
121 if (static_cast<bool>(allowPositional)) {
122 BOOST_ASSERT(valueType != ArgValueType::NONE);
123 m_positionalArgs.push_back(name);
124 }
125 else {
126 BOOST_ASSERT(valueType != ArgValueType::ANY);
127 }
128
129 return *this;
130}
131
132CommandArguments
133CommandDefinition::parse(const std::vector<std::string>& tokens, size_t start) const
134{
135 CommandArguments ca;
136
137 size_t positionalArgIndex = 0;
138 for (size_t i = start; i < tokens.size(); ++i) {
139 const std::string& token = tokens[i];
140
141 // try to parse as named argument
142 auto namedArg = m_args.find(token);
143 if (namedArg != m_args.end() && namedArg->second.valueType != ArgValueType::ANY) {
144 NDN_LOG_TRACE(token << " is a named argument");
145 const Arg& arg = namedArg->second;
146 if (arg.valueType == ArgValueType::NONE) {
147 ca[arg.name] = true;
Eric Newberry84d3adc2017-08-09 23:31:40 -0400148 NDN_LOG_TRACE(token << " is a no-param argument");
Junxiao Shi64567bb2016-09-04 16:00:27 +0000149 }
150 else if (i + 1 >= tokens.size()) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500151 NDN_THROW(Error(arg.name + ": " + arg.metavar + " is missing"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000152 }
153 else {
154 const std::string& valueToken = tokens[++i];
155 NDN_LOG_TRACE(arg.name << " has value " << valueToken);
156 try {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400157 ca[arg.name] = parseValue(arg.valueType, valueToken);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000158 }
159 catch (const std::exception& e) {
160 NDN_LOG_TRACE(valueToken << " cannot be parsed as " << arg.valueType);
Davide Pesavento19779d82019-02-14 13:40:04 -0500161 NDN_THROW_NESTED(Error(arg.name + ": cannot parse '" + valueToken + "' as " +
162 arg.metavar + " (" + e.what() + ")"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000163 }
164 NDN_LOG_TRACE(valueToken << " is parsed as " << arg.valueType);
165 }
166
167 // disallow positional arguments after named argument
168 positionalArgIndex = m_positionalArgs.size();
169 continue;
170 }
171
172 // try to parse as positional argument
173 for (; positionalArgIndex < m_positionalArgs.size(); ++positionalArgIndex) {
174 const Arg& arg = m_args.at(m_positionalArgs[positionalArgIndex]);
175
176 if (arg.valueType == ArgValueType::ANY) {
177 std::vector<std::string> values;
178 std::copy(tokens.begin() + i, tokens.end(), std::back_inserter(values));
179 ca[arg.name] = values;
180 NDN_LOG_TRACE((tokens.size() - i) << " tokens are consumed for " << arg.name);
181 i = tokens.size();
182 break;
183 }
184
185 try {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400186 ca[arg.name] = parseValue(arg.valueType, token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000187 NDN_LOG_TRACE(token << " is parsed as value for " << arg.name);
188 break;
189 }
190 catch (const std::exception& e) {
191 if (arg.isRequired) { // the current token must be parsed as the value for arg
192 NDN_LOG_TRACE(token << " cannot be parsed as value for " << arg.name);
Davide Pesavento19779d82019-02-14 13:40:04 -0500193 NDN_THROW_NESTED(Error("cannot parse '" + token + "' as an argument name or as " +
194 arg.metavar + " for " + arg.name + " (" + e.what() + ")"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000195 }
196 else {
197 // the current token may be a value for next positional argument
198 NDN_LOG_TRACE(token << " cannot be parsed as value for " << arg.name);
199 }
200 }
201 }
202
203 if (positionalArgIndex >= m_positionalArgs.size()) {
204 // for loop has reached the end without finding a match,
205 // which means token is not accepted as a value for positional argument
Davide Pesavento19779d82019-02-14 13:40:04 -0500206 NDN_THROW(Error("cannot parse '" + token + "' as an argument name"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000207 }
208
209 // token is accepted; don't parse as the same positional argument again
210 ++positionalArgIndex;
211 }
212
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400213 for (const auto& argName : m_requiredArgs) {
Junxiao Shi64567bb2016-09-04 16:00:27 +0000214 if (ca.count(argName) == 0) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500215 NDN_THROW(Error(argName + ": required argument is missing"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000216 }
217 }
218
219 return ca;
220}
221
Eric Newberry84d3adc2017-08-09 23:31:40 -0400222static bool
223parseBoolean(const std::string& s)
224{
225 if (s == "on" || s == "true" || s == "enabled" || s == "yes" || s == "1") {
226 return true;
227 }
228 if (s == "off" || s == "false" || s == "disabled" || s == "no" || s == "0") {
229 return false;
230 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500231 NDN_THROW(std::invalid_argument("unrecognized boolean value '" + s + "'"));
Eric Newberry84d3adc2017-08-09 23:31:40 -0400232}
233
Davide Pesavento990620a2022-06-05 00:25:53 -0400234static ReportFormat
235parseReportFormat(const std::string& s)
236{
237 if (s == "xml") {
238 return ReportFormat::XML;
239 }
240 if (s == "text") {
241 return ReportFormat::TEXT;
242 }
243 NDN_THROW(std::invalid_argument("unrecognized ReportFormat '" + s + "'"));
244}
245
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000246static FacePersistency
Junxiao Shi64567bb2016-09-04 16:00:27 +0000247parseFacePersistency(const std::string& s)
248{
249 if (s == "persistent") {
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000250 return FacePersistency::FACE_PERSISTENCY_PERSISTENT;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000251 }
252 if (s == "permanent") {
Junxiao Shi215fd0b2017-01-28 19:01:04 +0000253 return FacePersistency::FACE_PERSISTENCY_PERMANENT;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000254 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500255 NDN_THROW(std::invalid_argument("unrecognized FacePersistency '" + s + "'"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000256}
257
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400258std::any
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400259CommandDefinition::parseValue(ArgValueType valueType, const std::string& token)
Junxiao Shi64567bb2016-09-04 16:00:27 +0000260{
261 switch (valueType) {
262 case ArgValueType::NONE:
263 case ArgValueType::ANY:
Davide Pesavento5a897692019-10-31 01:28:43 -0400264 break;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000265
Davide Pesavento8b663a92018-11-21 22:57:20 -0500266 case ArgValueType::BOOLEAN:
Eric Newberry84d3adc2017-08-09 23:31:40 -0400267 return parseBoolean(token);
Eric Newberry84d3adc2017-08-09 23:31:40 -0400268
Junxiao Shi64567bb2016-09-04 16:00:27 +0000269 case ArgValueType::UNSIGNED: {
270 // boost::lexical_cast<uint64_t> will accept negative number
271 int64_t v = boost::lexical_cast<int64_t>(token);
272 if (v < 0) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500273 NDN_THROW(std::out_of_range("value '" + token + "' is negative"));
Junxiao Shi64567bb2016-09-04 16:00:27 +0000274 }
275 return static_cast<uint64_t>(v);
276 }
277
278 case ArgValueType::STRING:
279 return token;
280
281 case ArgValueType::REPORT_FORMAT:
282 return parseReportFormat(token);
283
284 case ArgValueType::NAME:
285 return Name(token);
286
287 case ArgValueType::FACE_URI:
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500288 return ndn::FaceUri(token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000289
290 case ArgValueType::FACE_ID_OR_URI:
291 try {
292 return boost::lexical_cast<uint64_t>(token);
293 }
294 catch (const boost::bad_lexical_cast&) {
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500295 return ndn::FaceUri(token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000296 }
297
298 case ArgValueType::FACE_PERSISTENCY:
299 return parseFacePersistency(token);
Junxiao Shi8eda6822017-04-12 02:53:14 +0000300
301 case ArgValueType::ROUTE_ORIGIN:
302 return boost::lexical_cast<RouteOrigin>(token);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000303 }
304
Davide Pesavento5a897692019-10-31 01:28:43 -0400305 NDN_CXX_UNREACHABLE;
Junxiao Shi64567bb2016-09-04 16:00:27 +0000306}
307
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400308} // namespace nfd::tools::nfdc