blob: a9d9c6fad0303b7e493666a5c751faf6ddd25eb5 [file] [log] [blame]
Junxiao Shid243d712016-08-19 06:45:31 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa997d292017-08-24 20:16:59 -04002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shid243d712016-08-19 06:45:31 +00004 * 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 Shi64567bb2016-09-04 16:00:27 +000026#include "available-commands.hpp"
Junxiao Shid243d712016-08-19 06:45:31 +000027#include "core/version.hpp"
Alexander Afanasyevc414ca52021-06-09 12:15:19 -040028#include "help.hpp"
Junxiao Shid243d712016-08-19 06:45:31 +000029
Alexander Afanasyevc414ca52021-06-09 12:15:19 -040030#include <boost/tokenizer.hpp>
31#include <fstream>
Davide Pesaventoa997d292017-08-24 20:16:59 -040032#include <iostream>
33
Junxiao Shid243d712016-08-19 06:45:31 +000034namespace nfd {
35namespace tools {
36namespace nfdc {
37
Junxiao Shid243d712016-08-19 06:45:31 +000038static int
39main(int argc, char** argv)
40{
Junxiao Shi64567bb2016-09-04 16:00:27 +000041 std::vector<std::string> args(argv + 1, argv + argc);
42
Junxiao Shi6c135622016-11-21 14:30:33 +000043 CommandParser parser;
44 registerCommands(parser);
45
Davide Pesavento2a588152018-02-19 18:10:03 -050046 if (args.empty()) {
Junxiao Shi6c135622016-11-21 14:30:33 +000047 helpList(std::cout, parser);
Junxiao Shid243d712016-08-19 06:45:31 +000048 return 0;
49 }
50
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050051 if (args[0] == "-V" || args[0] == "--version") {
Junxiao Shid243d712016-08-19 06:45:31 +000052 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
53 return 0;
54 }
55
Alexander Afanasyevc414ca52021-06-09 12:15:19 -040056 struct Command
57 {
58 std::string noun, verb;
59 CommandArguments ca;
60 ExecuteCommand execute;
61 };
62
63 auto processLine = [&parser] (const std::vector<std::string>& line) -> Command {
64 try {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040065 auto [noun, verb, ca, execute] = parser.parse(line, ParseMode::ONE_SHOT);
66 return {noun, verb, ca, execute};
Alexander Afanasyevc414ca52021-06-09 12:15:19 -040067 }
68 catch (const std::invalid_argument& e) {
69 int ret = help(std::cout, parser, line);
70 if (ret == 2)
71 std::cerr << e.what() << std::endl;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040072 return {};
Alexander Afanasyevc414ca52021-06-09 12:15:19 -040073 }
74 };
75
76 std::list<Command> commands;
77
78 if (args[0] == "-f" || args[0] == "--batch") {
79 if (args.size() != 2) {
80 std::cerr << "ERROR: Invalid command line arguments: " << args[0] << " should follow with batch-file."
81 << " Use -h for more detail." << std::endl;
82 return 2;
83 }
84
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040085 auto processIstream = [&commands, &processLine] (std::istream& is, const std::string& inputFile) {
Alexander Afanasyevc414ca52021-06-09 12:15:19 -040086 std::string line;
87 size_t lineCounter = 0;
88 while (std::getline(is, line)) {
89 ++lineCounter;
90
91 auto hasEscapeSlash = [] (const std::string& str) {
92 auto count = std::count(str.rbegin(), str.rend(), '\\');
93 return (count % 2) == 1;
94 };
95 while (!line.empty() && hasEscapeSlash(line)) {
96 std::string extraLine;
97 const auto& hasMore = std::getline(is, extraLine);
98 ++lineCounter;
99 line = line.substr(0, line.size() - 1) + extraLine;
100 if (!hasMore) {
101 break;
102 }
103 }
104 boost::tokenizer<boost::escaped_list_separator<char>> tokenizer(
105 line,
106 boost::escaped_list_separator<char>("\\", " \t", "\"'"));
107
108 auto firstNonEmptyToken = tokenizer.begin();
109 while (firstNonEmptyToken != tokenizer.end() && firstNonEmptyToken->empty()) {
110 ++firstNonEmptyToken;
111 }
112
113 // Ignore empty lines (or lines with just spaces) and lines that start with #
114 // Non empty lines with trailing comment are not allowed and may trigger syntax error
115 if (firstNonEmptyToken == tokenizer.end() || (*firstNonEmptyToken)[0] == '#') {
116 continue;
117 }
118
119 std::vector<std::string> lineArgs;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400120 std::copy_if(firstNonEmptyToken, tokenizer.end(), std::back_inserter(lineArgs),
121 [] (const auto& t) { return !t.empty(); });
Alexander Afanasyevc414ca52021-06-09 12:15:19 -0400122
123 auto cmd = processLine(lineArgs);
124 if (cmd.noun.empty()) {
125 std::cerr << " >> Syntax error on line " << lineCounter << " of the batch in "
126 << inputFile << std::endl;
127 return 2; // not exactly correct, but should be indication of an error, which already shown
128 }
129 commands.push_back(std::move(cmd));
130 }
131 return 0;
132 };
133
134 if (args[1] == "-") {
135 auto retval = processIstream(std::cin, "standard input");
136 if (retval != 0) {
137 return retval;
138 }
139 }
140 else {
141 std::ifstream iff(args[1]);
142 if (!iff) {
143 std::cerr << "ERROR: Could not open `" << args[1] << "` batch file "
144 << "(" << strerror(errno) << ")" << std::endl;
145 return 2;
146 }
147 auto retval = processIstream(iff, args[1]);
148 if (retval != 0) {
149 return retval;
150 }
151 }
Junxiao Shi64567bb2016-09-04 16:00:27 +0000152 }
Alexander Afanasyevc414ca52021-06-09 12:15:19 -0400153 else {
154 commands.push_back(processLine(args));
Junxiao Shi331ade72016-08-19 14:07:19 +0000155 }
156
Junxiao Shi737c43c2016-09-14 02:51:44 +0000157 try {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000158 Face face;
159 KeyChain keyChain;
160 Controller controller(face, keyChain);
Alexander Afanasyevc414ca52021-06-09 12:15:19 -0400161 size_t commandCounter = 0;
162 for (auto& command : commands) {
163 ++commandCounter;
164 ExecuteContext ctx{command.noun, command.verb, command.ca, 0,
165 std::cout, std::cerr, face, keyChain, controller};
166 command.execute(ctx);
167
168 if (ctx.exitCode != 0) {
169 if (commands.size() > 1) {
170 std::cerr << " >> Failed to execute command on line " << commandCounter
171 << " of the batch file " << args[1] << std::endl;
172 std::cerr << " Note that nfdc has executed all commands on previous lines and "
173 << "stopped processing at this line" << std::endl;
174 }
175
176 return ctx.exitCode;
177 }
178 }
179 return 0;
Junxiao Shi737c43c2016-09-14 02:51:44 +0000180 }
181 catch (const std::exception& e) {
182 std::cerr << e.what() << std::endl;
183 return 1;
184 }
Junxiao Shid243d712016-08-19 06:45:31 +0000185}
186
187} // namespace nfdc
188} // namespace tools
189} // namespace nfd
190
191int
192main(int argc, char** argv)
193{
194 return nfd::tools::nfdc::main(argc, argv);
195}