Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 4 | * 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 Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 27 | #include "status-report.hpp" |
Davide Pesavento | 8b663a9 | 2018-11-21 22:57:20 -0500 | [diff] [blame] | 28 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/net/face-uri.hpp> |
| 30 | #include <ndn-cxx/util/backports.hpp> |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/util/logger.hpp> |
| 32 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 33 | #include <boost/lexical_cast.hpp> |
| 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | namespace nfd::tools::nfdc { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 36 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 37 | NDN_LOG_INIT(nfdc.CommandDefinition); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 38 | |
| 39 | std::ostream& |
| 40 | operator<<(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 Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 47 | case ArgValueType::BOOLEAN: |
| 48 | return os << "boolean"; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 49 | 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 Shi | 8eda682 | 2017-04-12 02:53:14 +0000 | [diff] [blame] | 63 | case ArgValueType::ROUTE_ORIGIN: |
| 64 | return os << "RouteOrigin"; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 65 | } |
| 66 | return os << static_cast<int>(vt); |
| 67 | } |
| 68 | |
| 69 | static std::string |
| 70 | getMetavarFromType(ArgValueType vt) |
| 71 | { |
| 72 | switch (vt) { |
| 73 | case ArgValueType::NONE: |
| 74 | return ""; |
| 75 | case ArgValueType::ANY: |
| 76 | return "args"; |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 77 | case ArgValueType::BOOLEAN: |
| 78 | return "bool"; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 79 | 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 Shi | 8eda682 | 2017-04-12 02:53:14 +0000 | [diff] [blame] | 93 | case ArgValueType::ROUTE_ORIGIN: |
| 94 | return "origin"; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 95 | } |
Davide Pesavento | 5a89769 | 2019-10-31 01:28:43 -0400 | [diff] [blame] | 96 | NDN_CXX_UNREACHABLE; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 99 | CommandDefinition::CommandDefinition(std::string_view noun, std::string_view verb) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 100 | : m_noun(noun) |
| 101 | , m_verb(verb) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | CommandDefinition::~CommandDefinition() = default; |
| 106 | |
| 107 | CommandDefinition& |
| 108 | CommandDefinition::addArg(const std::string& name, ArgValueType valueType, |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 109 | Required isRequired, Positional allowPositional, |
| 110 | const std::string& metavar) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 111 | { |
| 112 | bool isNew = m_args.emplace(name, |
| 113 | Arg{name, valueType, static_cast<bool>(isRequired), |
| 114 | metavar.empty() ? getMetavarFromType(valueType) : metavar}).second; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 115 | BOOST_VERIFY(isNew); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 116 | |
| 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 | |
| 132 | CommandArguments |
| 133 | CommandDefinition::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 Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 148 | NDN_LOG_TRACE(token << " is a no-param argument"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 149 | } |
| 150 | else if (i + 1 >= tokens.size()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 151 | NDN_THROW(Error(arg.name + ": " + arg.metavar + " is missing")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 152 | } |
| 153 | else { |
| 154 | const std::string& valueToken = tokens[++i]; |
| 155 | NDN_LOG_TRACE(arg.name << " has value " << valueToken); |
| 156 | try { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 157 | ca[arg.name] = parseValue(arg.valueType, valueToken); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 158 | } |
| 159 | catch (const std::exception& e) { |
| 160 | NDN_LOG_TRACE(valueToken << " cannot be parsed as " << arg.valueType); |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 161 | NDN_THROW_NESTED(Error(arg.name + ": cannot parse '" + valueToken + "' as " + |
| 162 | arg.metavar + " (" + e.what() + ")")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 163 | } |
| 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 Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 186 | ca[arg.name] = parseValue(arg.valueType, token); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 187 | 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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 193 | NDN_THROW_NESTED(Error("cannot parse '" + token + "' as an argument name or as " + |
| 194 | arg.metavar + " for " + arg.name + " (" + e.what() + ")")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 195 | } |
| 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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 206 | NDN_THROW(Error("cannot parse '" + token + "' as an argument name")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // token is accepted; don't parse as the same positional argument again |
| 210 | ++positionalArgIndex; |
| 211 | } |
| 212 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 213 | for (const auto& argName : m_requiredArgs) { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 214 | if (ca.count(argName) == 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 215 | NDN_THROW(Error(argName + ": required argument is missing")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | return ca; |
| 220 | } |
| 221 | |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 222 | static bool |
| 223 | parseBoolean(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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 231 | NDN_THROW(std::invalid_argument("unrecognized boolean value '" + s + "'")); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 234 | static ReportFormat |
| 235 | parseReportFormat(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 Shi | 215fd0b | 2017-01-28 19:01:04 +0000 | [diff] [blame] | 246 | static FacePersistency |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 247 | parseFacePersistency(const std::string& s) |
| 248 | { |
| 249 | if (s == "persistent") { |
Junxiao Shi | 215fd0b | 2017-01-28 19:01:04 +0000 | [diff] [blame] | 250 | return FacePersistency::FACE_PERSISTENCY_PERSISTENT; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 251 | } |
| 252 | if (s == "permanent") { |
Junxiao Shi | 215fd0b | 2017-01-28 19:01:04 +0000 | [diff] [blame] | 253 | return FacePersistency::FACE_PERSISTENCY_PERMANENT; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 254 | } |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 255 | NDN_THROW(std::invalid_argument("unrecognized FacePersistency '" + s + "'")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 258 | std::any |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 259 | CommandDefinition::parseValue(ArgValueType valueType, const std::string& token) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 260 | { |
| 261 | switch (valueType) { |
| 262 | case ArgValueType::NONE: |
| 263 | case ArgValueType::ANY: |
Davide Pesavento | 5a89769 | 2019-10-31 01:28:43 -0400 | [diff] [blame] | 264 | break; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 265 | |
Davide Pesavento | 8b663a9 | 2018-11-21 22:57:20 -0500 | [diff] [blame] | 266 | case ArgValueType::BOOLEAN: |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 267 | return parseBoolean(token); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 268 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 269 | 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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 273 | NDN_THROW(std::out_of_range("value '" + token + "' is negative")); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 274 | } |
| 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 Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 288 | return ndn::FaceUri(token); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 289 | |
| 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 Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 295 | return ndn::FaceUri(token); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | case ArgValueType::FACE_PERSISTENCY: |
| 299 | return parseFacePersistency(token); |
Junxiao Shi | 8eda682 | 2017-04-12 02:53:14 +0000 | [diff] [blame] | 300 | |
| 301 | case ArgValueType::ROUTE_ORIGIN: |
| 302 | return boost::lexical_cast<RouteOrigin>(token); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Davide Pesavento | 5a89769 | 2019-10-31 01:28:43 -0400 | [diff] [blame] | 305 | NDN_CXX_UNREACHABLE; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 308 | } // namespace nfd::tools::nfdc |