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 | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 29 | namespace ndn::dissect { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 31 | namespace po = boost::program_options; |
| 32 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 33 | static void |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 34 | usage(std::ostream& os, std::string_view programName, const po::options_description& options) |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 35 | { |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 36 | os << "Usage: " << programName << " [options] [input-file]\n" |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 37 | << "\n" |
| 38 | << options; |
| 39 | } |
| 40 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 41 | static int |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 42 | main(int argc, char* argv[]) |
| 43 | { |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 44 | Options options; |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 45 | std::string inputFileName; |
| 46 | |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 47 | po::options_description visibleOptions("Options"); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 48 | visibleOptions.add_options() |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 49 | ("help,h", "print this help message and exit") |
| 50 | ("content,c", po::bool_switch(&options.dissectContent), "dissect the value of Content elements") |
| 51 | ("version,V", "print program version and exit") |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 52 | ; |
| 53 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 54 | po::options_description hiddenOptions; |
| 55 | hiddenOptions.add_options() |
| 56 | ("input-file", po::value<std::string>(&inputFileName)); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 57 | |
| 58 | po::options_description allOptions; |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 59 | allOptions.add(visibleOptions).add(hiddenOptions); |
| 60 | |
| 61 | po::positional_options_description pos; |
| 62 | pos.add("input-file", -1); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 63 | |
| 64 | po::variables_map vm; |
| 65 | try { |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 66 | 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] | 67 | po::notify(vm); |
| 68 | } |
Junxiao Shi | 3ebb9cb | 2016-11-23 14:58:55 +0000 | [diff] [blame] | 69 | catch (const po::error& e) { |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 70 | std::cerr << "ERROR: " << e.what() << "\n"; |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 71 | return 2; |
| 72 | } |
| 73 | |
| 74 | if (vm.count("help") > 0) { |
| 75 | usage(std::cout, argv[0], visibleOptions); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | if (vm.count("version") > 0) { |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 80 | std::cout << "ndn-dissect " << tools::VERSION << "\n"; |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | std::ifstream inputFile; |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 85 | std::istream* inputStream = &std::cin; |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 86 | if (vm.count("input-file") > 0 && inputFileName != "-") { |
| 87 | inputFile.open(inputFileName); |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 88 | if (!inputFile) { |
| 89 | 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] | 90 | return 3; |
| 91 | } |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 92 | inputStream = &inputFile; |
| 93 | } |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 94 | |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 95 | Dissector d(*inputStream, std::cout, options); |
| 96 | d.dissect(); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 101 | } // namespace ndn::dissect |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 102 | |
| 103 | int |
Davide Pesavento | da85e25 | 2019-03-18 11:42:01 -0400 | [diff] [blame] | 104 | main(int argc, char* argv[]) |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 105 | { |
| 106 | return ndn::dissect::main(argc, argv); |
| 107 | } |