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 | /* |
| 3 | * Copyright (c) 2014-2017, 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" |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 27 | #include "format-helpers.hpp" |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/util/logger.hpp> |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace tools { |
| 32 | namespace nfdc { |
| 33 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 34 | NDN_LOG_INIT(nfdc.CommandParser); |
| 35 | |
| 36 | static_assert(std::is_same<std::underlying_type<AvailableIn>::type, |
| 37 | std::underlying_type<ParseMode>::type>::value, |
| 38 | "AvailableIn and ParseMode must be declared with same underlying type"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 39 | |
| 40 | std::ostream& |
| 41 | operator<<(std::ostream& os, AvailableIn modes) |
| 42 | { |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 43 | text::Separator sep("|"); |
| 44 | if ((modes & AVAILABLE_IN_ONE_SHOT) != 0) { |
| 45 | os << sep << "one-shot"; |
| 46 | } |
| 47 | if ((modes & AVAILABLE_IN_BATCH) != 0) { |
| 48 | os << sep << "batch"; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 49 | } |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 50 | if ((modes & AVAILABLE_IN_HELP) == 0) { |
| 51 | os << sep << "hidden"; |
| 52 | } |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 53 | |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 54 | if (sep.getCount() == 0) { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 55 | os << "none"; |
| 56 | } |
| 57 | return os; |
| 58 | } |
| 59 | |
| 60 | std::ostream& |
| 61 | operator<<(std::ostream& os, ParseMode mode) |
| 62 | { |
| 63 | switch (mode) { |
| 64 | case ParseMode::ONE_SHOT: |
| 65 | return os << "one-shot"; |
| 66 | case ParseMode::BATCH: |
| 67 | return os << "batch"; |
| 68 | } |
| 69 | return os << static_cast<int>(mode); |
| 70 | } |
| 71 | |
| 72 | CommandParser& |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 73 | CommandParser::addCommand(const CommandDefinition& def, const ExecuteCommand& execute, |
| 74 | std::underlying_type<AvailableIn>::type modes) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 75 | { |
| 76 | BOOST_ASSERT(modes != AVAILABLE_IN_NONE); |
Junxiao Shi | c143afe | 2016-09-20 13:04:51 +0000 | [diff] [blame] | 77 | m_commands[{def.getNoun(), def.getVerb()}].reset( |
| 78 | new Command{def, execute, static_cast<AvailableIn>(modes)}); |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 79 | |
| 80 | if ((modes & AVAILABLE_IN_HELP) != 0) { |
| 81 | m_commandOrder.push_back(m_commands.find({def.getNoun(), def.getVerb()})); |
| 82 | } |
| 83 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | CommandParser& |
| 88 | CommandParser::addAlias(const std::string& noun, const std::string& verb, const std::string& verb2) |
| 89 | { |
| 90 | m_commands[{noun, verb2}] = m_commands.at({noun, verb}); |
| 91 | return *this; |
| 92 | } |
| 93 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 94 | std::vector<const CommandDefinition*> |
| 95 | CommandParser::listCommands(const std::string& noun, ParseMode mode) const |
| 96 | { |
| 97 | std::vector<const CommandDefinition*> results; |
| 98 | for (CommandContainer::const_iterator i : m_commandOrder) { |
| 99 | const Command& command = *i->second; |
| 100 | if ((command.modes & static_cast<AvailableIn>(mode)) != 0 && |
| 101 | (noun.empty() || noun == command.def.getNoun())) { |
| 102 | results.push_back(&command.def); |
| 103 | } |
| 104 | } |
| 105 | return results; |
| 106 | } |
| 107 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 108 | std::tuple<std::string, std::string, CommandArguments, ExecuteCommand> |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 109 | CommandParser::parse(const std::vector<std::string>& tokens, ParseMode mode) const |
| 110 | { |
| 111 | BOOST_ASSERT(mode == ParseMode::ONE_SHOT); |
| 112 | |
| 113 | const std::string& noun = tokens.size() > 0 ? tokens[0] : ""; |
| 114 | const std::string& verb = tokens.size() > 1 ? tokens[1] : ""; |
| 115 | size_t nameLen = std::min<size_t>(2, tokens.size()); |
| 116 | |
| 117 | auto i = m_commands.find({noun, verb}); |
| 118 | if (i == m_commands.end()) { |
| 119 | if (verb.empty()) { |
| 120 | i = m_commands.find({noun, "list"}); |
| 121 | } |
| 122 | else { |
Junxiao Shi | 15902ef | 2017-08-11 22:58:35 +0000 | [diff] [blame^] | 123 | // help, exit, quit commands |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 124 | i = m_commands.find({noun, ""}); |
| 125 | } |
| 126 | nameLen = std::min<size_t>(1, tokens.size()); |
| 127 | } |
| 128 | if (i == m_commands.end() || (i->second->modes & static_cast<AvailableIn>(mode)) == 0) { |
| 129 | BOOST_THROW_EXCEPTION(Error("no such command: " + noun + " " + verb)); |
| 130 | } |
| 131 | |
| 132 | const CommandDefinition& def = i->second->def; |
| 133 | NDN_LOG_TRACE("found command " << def.getNoun() << " " << def.getVerb()); |
| 134 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 135 | return std::make_tuple(def.getNoun(), def.getVerb(), def.parse(tokens, nameLen), i->second->execute); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | } // namespace nfdc |
| 139 | } // namespace tools |
| 140 | } // namespace nfd |