Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, Regents of the University of California. |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame^] | 20 | #include "dissector.hpp" |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 21 | #include "core/version.hpp" |
| 22 | |
| 23 | #include <boost/program_options/options_description.hpp> |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 24 | #include <boost/program_options/parsers.hpp> |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 25 | #include <boost/program_options/variables_map.hpp> |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 27 | #include <fstream> |
| 28 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 29 | namespace ndn { |
| 30 | namespace dissect { |
| 31 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 32 | namespace po = boost::program_options; |
| 33 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 34 | static void |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 35 | usage(std::ostream& os, const std::string& programName, const po::options_description& options) |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 36 | { |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 37 | os << "Usage: " << programName << " [options] [input-file]\n" |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 38 | << "\n" |
| 39 | << options; |
| 40 | } |
| 41 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 42 | static int |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 43 | main(int argc, char* argv[]) |
| 44 | { |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame^] | 45 | Options options; |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 46 | std::string inputFileName; |
| 47 | |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 48 | po::options_description visibleOptions("Options"); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 49 | visibleOptions.add_options() |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame^] | 50 | ("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 Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 53 | ; |
| 54 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 55 | po::options_description hiddenOptions; |
| 56 | hiddenOptions.add_options() |
| 57 | ("input-file", po::value<std::string>(&inputFileName)); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 58 | |
| 59 | po::options_description allOptions; |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 60 | allOptions.add(visibleOptions).add(hiddenOptions); |
| 61 | |
| 62 | po::positional_options_description pos; |
| 63 | pos.add("input-file", -1); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 64 | |
| 65 | po::variables_map vm; |
| 66 | try { |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 67 | po::store(po::command_line_parser(argc, argv).options(allOptions).positional(pos).run(), vm); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 68 | po::notify(vm); |
| 69 | } |
Junxiao Shi | 3ebb9cb | 2016-11-23 14:58:55 +0000 | [diff] [blame] | 70 | catch (const po::error& e) { |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 71 | std::cerr << "ERROR: " << e.what() << "\n"; |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 72 | 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 Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 81 | std::cout << "ndn-dissect " << tools::VERSION << "\n"; |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | std::ifstream inputFile; |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 86 | std::istream* inputStream = &std::cin; |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 87 | if (vm.count("input-file") > 0 && inputFileName != "-") { |
| 88 | inputFile.open(inputFileName); |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 89 | if (!inputFile) { |
| 90 | std::cerr << argv[0] << ": " << inputFileName << ": File does not exist or is unreadable\n"; |
Junxiao Shi | 3ebb9cb | 2016-11-23 14:58:55 +0000 | [diff] [blame] | 91 | return 3; |
| 92 | } |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 93 | inputStream = &inputFile; |
| 94 | } |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 95 | |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame^] | 96 | Dissector d(*inputStream, std::cout, options); |
| 97 | d.dissect(); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | } // namespace dissect |
| 103 | } // namespace ndn |
| 104 | |
| 105 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 106 | main(int argc, char* argv[]) |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 107 | { |
| 108 | return ndn::dissect::main(argc, argv); |
| 109 | } |