Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 15902ef | 2017-08-11 22:58:35 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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-parser.hpp" |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame^] | 27 | |
| 28 | #include "cs-module.hpp" |
| 29 | #include "face-module.hpp" |
| 30 | #include "rib-module.hpp" |
| 31 | #include "status.hpp" |
| 32 | #include "strategy-choice-module.hpp" |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 33 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 34 | #include <ndn-cxx/util/logger.hpp> |
| 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::tools::nfdc { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 37 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 38 | NDN_LOG_INIT(nfdc.CommandParser); |
| 39 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 40 | static_assert(std::is_same_v<std::underlying_type_t<AvailableIn>, |
| 41 | std::underlying_type_t<ParseMode>>, |
| 42 | "AvailableIn and ParseMode must be declared with the same underlying type"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 43 | |
| 44 | std::ostream& |
| 45 | operator<<(std::ostream& os, AvailableIn modes) |
| 46 | { |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 47 | text::Separator sep("|"); |
| 48 | if ((modes & AVAILABLE_IN_ONE_SHOT) != 0) { |
| 49 | os << sep << "one-shot"; |
| 50 | } |
| 51 | if ((modes & AVAILABLE_IN_BATCH) != 0) { |
| 52 | os << sep << "batch"; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 53 | } |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 54 | if ((modes & AVAILABLE_IN_HELP) == 0) { |
| 55 | os << sep << "hidden"; |
| 56 | } |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 57 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 58 | if (sep.getCount() == 0) { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 59 | os << "none"; |
| 60 | } |
| 61 | return os; |
| 62 | } |
| 63 | |
| 64 | std::ostream& |
| 65 | operator<<(std::ostream& os, ParseMode mode) |
| 66 | { |
| 67 | switch (mode) { |
| 68 | case ParseMode::ONE_SHOT: |
| 69 | return os << "one-shot"; |
| 70 | case ParseMode::BATCH: |
| 71 | return os << "batch"; |
| 72 | } |
| 73 | return os << static_cast<int>(mode); |
| 74 | } |
| 75 | |
| 76 | CommandParser& |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 77 | CommandParser::addCommand(const CommandDefinition& def, const ExecuteCommand& execute, |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 78 | std::underlying_type_t<AvailableIn> modes) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 79 | { |
| 80 | BOOST_ASSERT(modes != AVAILABLE_IN_NONE); |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 81 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 82 | m_commands[{def.getNoun(), def.getVerb()}].reset( |
| 83 | new Command{def, execute, static_cast<AvailableIn>(modes)}); |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 84 | |
| 85 | if ((modes & AVAILABLE_IN_HELP) != 0) { |
| 86 | m_commandOrder.push_back(m_commands.find({def.getNoun(), def.getVerb()})); |
| 87 | } |
| 88 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | CommandParser& |
| 93 | CommandParser::addAlias(const std::string& noun, const std::string& verb, const std::string& verb2) |
| 94 | { |
| 95 | m_commands[{noun, verb2}] = m_commands.at({noun, verb}); |
| 96 | return *this; |
| 97 | } |
| 98 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 99 | std::vector<const CommandDefinition*> |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 100 | CommandParser::listCommands(std::string_view noun, ParseMode mode) const |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 101 | { |
| 102 | std::vector<const CommandDefinition*> results; |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 103 | for (auto i : m_commandOrder) { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 104 | const auto& command = *i->second; |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 105 | if ((command.modes & static_cast<AvailableIn>(mode)) != 0 && |
| 106 | (noun.empty() || noun == command.def.getNoun())) { |
| 107 | results.push_back(&command.def); |
| 108 | } |
| 109 | } |
| 110 | return results; |
| 111 | } |
| 112 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 113 | std::tuple<std::string, std::string, CommandArguments, ExecuteCommand> |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 114 | CommandParser::parse(const std::vector<std::string>& tokens, ParseMode mode) const |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 115 | { |
| 116 | BOOST_ASSERT(mode == ParseMode::ONE_SHOT); |
| 117 | |
| 118 | const std::string& noun = tokens.size() > 0 ? tokens[0] : ""; |
| 119 | const std::string& verb = tokens.size() > 1 ? tokens[1] : ""; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 120 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 121 | NDN_LOG_TRACE("parse mode=" << mode << " noun=" << noun << " verb=" << verb); |
| 122 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 123 | auto i = m_commands.find({noun, verb}); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 124 | if (i == m_commands.end() || (i->second->modes & static_cast<AvailableIn>(mode)) == 0) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 125 | NDN_THROW(NoSuchCommandError(noun, verb)); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | const CommandDefinition& def = i->second->def; |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 129 | NDN_LOG_TRACE("found command noun=" << def.getNoun() << " verb=" << def.getVerb()); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 130 | |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 131 | size_t nConsumed = std::min<size_t>(2, tokens.size()); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 132 | return {def.getNoun(), def.getVerb(), def.parse(tokens, nConsumed), i->second->execute}; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame^] | 135 | void |
| 136 | registerCommands(CommandParser& parser) |
| 137 | { |
| 138 | registerStatusCommands(parser); |
| 139 | FaceModule::registerCommands(parser); |
| 140 | RibModule::registerCommands(parser); |
| 141 | CsModule::registerCommands(parser); |
| 142 | StrategyChoiceModule::registerCommands(parser); |
| 143 | } |
| 144 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 145 | } // namespace nfd::tools::nfdc |