blob: a7d1e3e8914f90d492d2e45daa30ad64da658b27 [file] [log] [blame]
Junxiao Shi78c78cc2015-06-19 15:53:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoda85e252019-03-18 11:42:01 -04002/*
Davide Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2014-2021, Regents of the University of California.
Junxiao Shi78c78cc2015-06-19 15:53:53 -07004 *
5 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
7 *
8 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "ndn-dissect.hpp"
21#include "core/version.hpp"
22
23#include <boost/program_options/options_description.hpp>
Junxiao Shi78c78cc2015-06-19 15:53:53 -070024#include <boost/program_options/parsers.hpp>
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050025#include <boost/program_options/variables_map.hpp>
Junxiao Shi78c78cc2015-06-19 15:53:53 -070026
Davide Pesaventoc0702702017-08-24 22:04:00 -040027#include <fstream>
28
Junxiao Shi78c78cc2015-06-19 15:53:53 -070029namespace ndn {
30namespace dissect {
31
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050032namespace po = boost::program_options;
33
Davide Pesaventoc0702702017-08-24 22:04:00 -040034static void
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050035usage(std::ostream& os, const std::string& programName, const po::options_description& options)
Junxiao Shi78c78cc2015-06-19 15:53:53 -070036{
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050037 os << "Usage: " << programName << " [options] [input-file]\n"
Junxiao Shi78c78cc2015-06-19 15:53:53 -070038 << "\n"
39 << options;
40}
41
Davide Pesaventoc0702702017-08-24 22:04:00 -040042static int
Junxiao Shi78c78cc2015-06-19 15:53:53 -070043main(int argc, char* argv[])
44{
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050045 std::string inputFileName;
46
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050047 po::options_description visibleOptions("Options");
Junxiao Shi78c78cc2015-06-19 15:53:53 -070048 visibleOptions.add_options()
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050049 ("help,h", "print help and exit")
50 ("version,V", "print version and exit")
Junxiao Shi78c78cc2015-06-19 15:53:53 -070051 ;
52
Junxiao Shi78c78cc2015-06-19 15:53:53 -070053 po::options_description hiddenOptions;
54 hiddenOptions.add_options()
55 ("input-file", po::value<std::string>(&inputFileName));
Junxiao Shi78c78cc2015-06-19 15:53:53 -070056
57 po::options_description allOptions;
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050058 allOptions.add(visibleOptions).add(hiddenOptions);
59
60 po::positional_options_description pos;
61 pos.add("input-file", -1);
Junxiao Shi78c78cc2015-06-19 15:53:53 -070062
63 po::variables_map vm;
64 try {
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050065 po::store(po::command_line_parser(argc, argv).options(allOptions).positional(pos).run(), vm);
Junxiao Shi78c78cc2015-06-19 15:53:53 -070066 po::notify(vm);
67 }
Junxiao Shi3ebb9cb2016-11-23 14:58:55 +000068 catch (const po::error& e) {
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050069 std::cerr << "ERROR: " << e.what() << "\n";
Junxiao Shi78c78cc2015-06-19 15:53:53 -070070 return 2;
71 }
72
73 if (vm.count("help") > 0) {
74 usage(std::cout, argv[0], visibleOptions);
75 return 0;
76 }
77
78 if (vm.count("version") > 0) {
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050079 std::cout << "ndn-dissect " << tools::VERSION << "\n";
Junxiao Shi78c78cc2015-06-19 15:53:53 -070080 return 0;
81 }
82
83 std::ifstream inputFile;
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050084 std::istream* inputStream = &std::cin;
Junxiao Shi78c78cc2015-06-19 15:53:53 -070085
86 if (vm.count("input-file") > 0 && inputFileName != "-") {
87 inputFile.open(inputFileName);
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050088 if (!inputFile) {
89 std::cerr << argv[0] << ": " << inputFileName << ": File does not exist or is unreadable\n";
Junxiao Shi3ebb9cb2016-11-23 14:58:55 +000090 return 3;
91 }
Junxiao Shi78c78cc2015-06-19 15:53:53 -070092 inputStream = &inputFile;
93 }
Junxiao Shi78c78cc2015-06-19 15:53:53 -070094
95 NdnDissect program;
96 program.dissect(std::cout, *inputStream);
97
98 return 0;
99}
100
101} // namespace dissect
102} // namespace ndn
103
104int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400105main(int argc, char* argv[])
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700106{
107 return ndn::dissect::main(argc, argv);
108}