blob: b6bf6baf53287d5ed91b566de63e325ca1864c23 [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 Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2014-2024, 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
Davide Pesaventoa420e972021-02-25 00:56:25 -050020#include "dissector.hpp"
Junxiao Shi78c78cc2015-06-19 15:53:53 -070021#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>
Davide Pesavento5748e822024-01-26 18:40:22 -050028#include <iostream>
Davide Pesaventoc0702702017-08-24 22:04:00 -040029
Davide Pesaventob3570c62022-02-19 19:19:00 -050030namespace ndn::dissect {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070031
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050032namespace po = boost::program_options;
33
Davide Pesaventoc0702702017-08-24 22:04:00 -040034static void
Davide Pesaventob3570c62022-02-19 19:19:00 -050035usage(std::ostream& os, std::string_view 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 Pesaventoa420e972021-02-25 00:56:25 -050045 Options options;
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050046 std::string inputFileName;
47
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050048 po::options_description visibleOptions("Options");
Junxiao Shi78c78cc2015-06-19 15:53:53 -070049 visibleOptions.add_options()
Davide Pesaventoa420e972021-02-25 00:56:25 -050050 ("help,h", "print this help message and exit")
51 ("content,c", po::bool_switch(&options.dissectContent), "dissect the value of Content elements")
52 ("version,V", "print program version and exit")
Junxiao Shi78c78cc2015-06-19 15:53:53 -070053 ;
54
Junxiao Shi78c78cc2015-06-19 15:53:53 -070055 po::options_description hiddenOptions;
56 hiddenOptions.add_options()
57 ("input-file", po::value<std::string>(&inputFileName));
Junxiao Shi78c78cc2015-06-19 15:53:53 -070058
59 po::options_description allOptions;
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050060 allOptions.add(visibleOptions).add(hiddenOptions);
61
62 po::positional_options_description pos;
63 pos.add("input-file", -1);
Junxiao Shi78c78cc2015-06-19 15:53:53 -070064
65 po::variables_map vm;
66 try {
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050067 po::store(po::command_line_parser(argc, argv).options(allOptions).positional(pos).run(), vm);
Junxiao Shi78c78cc2015-06-19 15:53:53 -070068 po::notify(vm);
69 }
Junxiao Shi3ebb9cb2016-11-23 14:58:55 +000070 catch (const po::error& e) {
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050071 std::cerr << "ERROR: " << e.what() << "\n";
Junxiao Shi78c78cc2015-06-19 15:53:53 -070072 return 2;
73 }
74
75 if (vm.count("help") > 0) {
76 usage(std::cout, argv[0], visibleOptions);
77 return 0;
78 }
79
80 if (vm.count("version") > 0) {
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050081 std::cout << "ndn-dissect " << tools::VERSION << "\n";
Junxiao Shi78c78cc2015-06-19 15:53:53 -070082 return 0;
83 }
84
85 std::ifstream inputFile;
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050086 std::istream* inputStream = &std::cin;
Junxiao Shi78c78cc2015-06-19 15:53:53 -070087 if (vm.count("input-file") > 0 && inputFileName != "-") {
88 inputFile.open(inputFileName);
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -050089 if (!inputFile) {
90 std::cerr << argv[0] << ": " << inputFileName << ": File does not exist or is unreadable\n";
Junxiao Shi3ebb9cb2016-11-23 14:58:55 +000091 return 3;
92 }
Junxiao Shi78c78cc2015-06-19 15:53:53 -070093 inputStream = &inputFile;
94 }
Junxiao Shi78c78cc2015-06-19 15:53:53 -070095
Davide Pesaventoa420e972021-02-25 00:56:25 -050096 Dissector d(*inputStream, std::cout, options);
97 d.dissect();
Junxiao Shi78c78cc2015-06-19 15:53:53 -070098
99 return 0;
100}
101
Davide Pesaventob3570c62022-02-19 19:19:00 -0500102} // namespace ndn::dissect
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700103
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}