Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 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" |
| 27 | #include <ndn-cxx/util/logger.hpp> |
| 28 | |
| 29 | namespace nfd { |
| 30 | namespace tools { |
| 31 | namespace nfdc { |
| 32 | |
| 33 | NDN_LOG_INIT(CommandParser); |
| 34 | |
| 35 | std::ostream& |
| 36 | operator<<(std::ostream& os, AvailableIn modes) |
| 37 | { |
| 38 | int count = 0; |
| 39 | |
| 40 | #define PRINT_BIT(bit, str) \ |
| 41 | if ((modes & bit) != 0) { \ |
| 42 | if (++count > 1) { \ |
| 43 | os << '|'; \ |
| 44 | } \ |
| 45 | os << str; \ |
| 46 | } |
| 47 | |
| 48 | PRINT_BIT(AVAILABLE_IN_ONE_SHOT, "one-shot") |
| 49 | PRINT_BIT(AVAILABLE_IN_BATCH, "batch") |
| 50 | |
| 51 | #undef PRINT_BIT |
| 52 | |
| 53 | if (count == 0) { |
| 54 | os << "none"; |
| 55 | } |
| 56 | return os; |
| 57 | } |
| 58 | |
| 59 | std::ostream& |
| 60 | operator<<(std::ostream& os, ParseMode mode) |
| 61 | { |
| 62 | switch (mode) { |
| 63 | case ParseMode::ONE_SHOT: |
| 64 | return os << "one-shot"; |
| 65 | case ParseMode::BATCH: |
| 66 | return os << "batch"; |
| 67 | } |
| 68 | return os << static_cast<int>(mode); |
| 69 | } |
| 70 | |
| 71 | CommandParser& |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 72 | CommandParser::addCommand(const CommandDefinition& def, const ExecuteCommand& execute, AvailableIn modes) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 73 | { |
| 74 | BOOST_ASSERT(modes != AVAILABLE_IN_NONE); |
| 75 | m_commands[{def.getNoun(), def.getVerb()}].reset(new Command{def, execute, modes}); |
| 76 | return *this; |
| 77 | } |
| 78 | |
| 79 | CommandParser& |
| 80 | CommandParser::addAlias(const std::string& noun, const std::string& verb, const std::string& verb2) |
| 81 | { |
| 82 | m_commands[{noun, verb2}] = m_commands.at({noun, verb}); |
| 83 | return *this; |
| 84 | } |
| 85 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 86 | std::tuple<std::string, std::string, CommandArguments, ExecuteCommand> |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 87 | CommandParser::parse(const std::vector<std::string>& tokens, ParseMode mode) const |
| 88 | { |
| 89 | BOOST_ASSERT(mode == ParseMode::ONE_SHOT); |
| 90 | |
| 91 | const std::string& noun = tokens.size() > 0 ? tokens[0] : ""; |
| 92 | const std::string& verb = tokens.size() > 1 ? tokens[1] : ""; |
| 93 | size_t nameLen = std::min<size_t>(2, tokens.size()); |
| 94 | |
| 95 | auto i = m_commands.find({noun, verb}); |
| 96 | if (i == m_commands.end()) { |
| 97 | if (verb.empty()) { |
| 98 | i = m_commands.find({noun, "list"}); |
| 99 | } |
| 100 | else { |
| 101 | // help, exit, quit, legacy nfdc commands |
| 102 | i = m_commands.find({noun, ""}); |
| 103 | } |
| 104 | nameLen = std::min<size_t>(1, tokens.size()); |
| 105 | } |
| 106 | if (i == m_commands.end() || (i->second->modes & static_cast<AvailableIn>(mode)) == 0) { |
| 107 | BOOST_THROW_EXCEPTION(Error("no such command: " + noun + " " + verb)); |
| 108 | } |
| 109 | |
| 110 | const CommandDefinition& def = i->second->def; |
| 111 | NDN_LOG_TRACE("found command " << def.getNoun() << " " << def.getVerb()); |
| 112 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 113 | 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] | 114 | } |
| 115 | |
| 116 | } // namespace nfdc |
| 117 | } // namespace tools |
| 118 | } // namespace nfd |