Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +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 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 26 | #include "available-commands.hpp" |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 27 | #include "core/version.hpp" |
Alexander Afanasyev | c414ca5 | 2021-06-09 12:15:19 -0400 | [diff] [blame] | 28 | #include "help.hpp" |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 29 | |
Alexander Afanasyev | c414ca5 | 2021-06-09 12:15:19 -0400 | [diff] [blame] | 30 | #include <boost/tokenizer.hpp> |
| 31 | #include <fstream> |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 32 | #include <iostream> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 34 | namespace nfd::tools::nfdc { |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 35 | |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 36 | static int |
| 37 | main(int argc, char** argv) |
| 38 | { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 39 | std::vector<std::string> args(argv + 1, argv + argc); |
| 40 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 41 | CommandParser parser; |
| 42 | registerCommands(parser); |
| 43 | |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 44 | if (args.empty()) { |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 45 | helpList(std::cout, parser); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 49 | if (args[0] == "-V" || args[0] == "--version") { |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 50 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 51 | return 0; |
| 52 | } |
| 53 | |
Alexander Afanasyev | c414ca5 | 2021-06-09 12:15:19 -0400 | [diff] [blame] | 54 | struct Command |
| 55 | { |
| 56 | std::string noun, verb; |
| 57 | CommandArguments ca; |
| 58 | ExecuteCommand execute; |
| 59 | }; |
| 60 | |
| 61 | auto processLine = [&parser] (const std::vector<std::string>& line) -> Command { |
| 62 | try { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 63 | auto [noun, verb, ca, execute] = parser.parse(line, ParseMode::ONE_SHOT); |
| 64 | return {noun, verb, ca, execute}; |
Alexander Afanasyev | c414ca5 | 2021-06-09 12:15:19 -0400 | [diff] [blame] | 65 | } |
| 66 | catch (const std::invalid_argument& e) { |
| 67 | int ret = help(std::cout, parser, line); |
| 68 | if (ret == 2) |
| 69 | std::cerr << e.what() << std::endl; |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 70 | return {}; |
Alexander Afanasyev | c414ca5 | 2021-06-09 12:15:19 -0400 | [diff] [blame] | 71 | } |
| 72 | }; |
| 73 | |
| 74 | std::list<Command> commands; |
| 75 | |
| 76 | if (args[0] == "-f" || args[0] == "--batch") { |
| 77 | if (args.size() != 2) { |
| 78 | std::cerr << "ERROR: Invalid command line arguments: " << args[0] << " should follow with batch-file." |
| 79 | << " Use -h for more detail." << std::endl; |
| 80 | return 2; |
| 81 | } |
| 82 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 83 | auto processIstream = [&commands, &processLine] (std::istream& is, const std::string& inputFile) { |
Alexander Afanasyev | c414ca5 | 2021-06-09 12:15:19 -0400 | [diff] [blame] | 84 | std::string line; |
| 85 | size_t lineCounter = 0; |
| 86 | while (std::getline(is, line)) { |
| 87 | ++lineCounter; |
| 88 | |
| 89 | auto hasEscapeSlash = [] (const std::string& str) { |
| 90 | auto count = std::count(str.rbegin(), str.rend(), '\\'); |
| 91 | return (count % 2) == 1; |
| 92 | }; |
| 93 | while (!line.empty() && hasEscapeSlash(line)) { |
| 94 | std::string extraLine; |
| 95 | const auto& hasMore = std::getline(is, extraLine); |
| 96 | ++lineCounter; |
| 97 | line = line.substr(0, line.size() - 1) + extraLine; |
| 98 | if (!hasMore) { |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | boost::tokenizer<boost::escaped_list_separator<char>> tokenizer( |
| 103 | line, |
| 104 | boost::escaped_list_separator<char>("\\", " \t", "\"'")); |
| 105 | |
| 106 | auto firstNonEmptyToken = tokenizer.begin(); |
| 107 | while (firstNonEmptyToken != tokenizer.end() && firstNonEmptyToken->empty()) { |
| 108 | ++firstNonEmptyToken; |
| 109 | } |
| 110 | |
| 111 | // Ignore empty lines (or lines with just spaces) and lines that start with # |
| 112 | // Non empty lines with trailing comment are not allowed and may trigger syntax error |
| 113 | if (firstNonEmptyToken == tokenizer.end() || (*firstNonEmptyToken)[0] == '#') { |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | std::vector<std::string> lineArgs; |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 118 | std::copy_if(firstNonEmptyToken, tokenizer.end(), std::back_inserter(lineArgs), |
| 119 | [] (const auto& t) { return !t.empty(); }); |
Alexander Afanasyev | c414ca5 | 2021-06-09 12:15:19 -0400 | [diff] [blame] | 120 | |
| 121 | auto cmd = processLine(lineArgs); |
| 122 | if (cmd.noun.empty()) { |
| 123 | std::cerr << " >> Syntax error on line " << lineCounter << " of the batch in " |
| 124 | << inputFile << std::endl; |
| 125 | return 2; // not exactly correct, but should be indication of an error, which already shown |
| 126 | } |
| 127 | commands.push_back(std::move(cmd)); |
| 128 | } |
| 129 | return 0; |
| 130 | }; |
| 131 | |
| 132 | if (args[1] == "-") { |
| 133 | auto retval = processIstream(std::cin, "standard input"); |
| 134 | if (retval != 0) { |
| 135 | return retval; |
| 136 | } |
| 137 | } |
| 138 | else { |
| 139 | std::ifstream iff(args[1]); |
| 140 | if (!iff) { |
| 141 | std::cerr << "ERROR: Could not open `" << args[1] << "` batch file " |
| 142 | << "(" << strerror(errno) << ")" << std::endl; |
| 143 | return 2; |
| 144 | } |
| 145 | auto retval = processIstream(iff, args[1]); |
| 146 | if (retval != 0) { |
| 147 | return retval; |
| 148 | } |
| 149 | } |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 150 | } |
Alexander Afanasyev | c414ca5 | 2021-06-09 12:15:19 -0400 | [diff] [blame] | 151 | else { |
| 152 | commands.push_back(processLine(args)); |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 155 | try { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 156 | Face face; |
| 157 | KeyChain keyChain; |
| 158 | Controller controller(face, keyChain); |
Alexander Afanasyev | c414ca5 | 2021-06-09 12:15:19 -0400 | [diff] [blame] | 159 | size_t commandCounter = 0; |
| 160 | for (auto& command : commands) { |
| 161 | ++commandCounter; |
| 162 | ExecuteContext ctx{command.noun, command.verb, command.ca, 0, |
| 163 | std::cout, std::cerr, face, keyChain, controller}; |
| 164 | command.execute(ctx); |
| 165 | |
| 166 | if (ctx.exitCode != 0) { |
| 167 | if (commands.size() > 1) { |
| 168 | std::cerr << " >> Failed to execute command on line " << commandCounter |
| 169 | << " of the batch file " << args[1] << std::endl; |
| 170 | std::cerr << " Note that nfdc has executed all commands on previous lines and " |
| 171 | << "stopped processing at this line" << std::endl; |
| 172 | } |
| 173 | |
| 174 | return ctx.exitCode; |
| 175 | } |
| 176 | } |
| 177 | return 0; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 178 | } |
| 179 | catch (const std::exception& e) { |
| 180 | std::cerr << e.what() << std::endl; |
| 181 | return 1; |
| 182 | } |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 185 | } // namespace nfd::tools::nfdc |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 186 | |
| 187 | int |
| 188 | main(int argc, char** argv) |
| 189 | { |
| 190 | return nfd::tools::nfdc::main(argc, argv); |
| 191 | } |