Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, The University of Memphis, |
Vince Lehman | 9dce0c9 | 2015-02-09 12:53:41 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | **/ |
Alexander Afanasyev | b669f9c | 2014-11-14 12:41:54 -0800 | [diff] [blame] | 21 | |
Vince Lehman | c57c64b | 2015-08-10 12:21:31 -0500 | [diff] [blame] | 22 | #include "nlsr-runner.hpp" |
Vince Lehman | b722b10 | 2014-08-24 16:33:49 -0500 | [diff] [blame] | 23 | #include "version.hpp" |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 7afe22f | 2018-02-13 16:17:40 -0500 | [diff] [blame] | 25 | #include <boost/exception/get_error_info.hpp> |
| 26 | #include <sstream> |
| 27 | |
| 28 | template<typename E> |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 29 | static std::string |
Alexander Afanasyev | 7afe22f | 2018-02-13 16:17:40 -0500 | [diff] [blame] | 30 | getExtendedErrorMessage(const E& exception) |
| 31 | { |
| 32 | std::ostringstream errorMessage; |
| 33 | errorMessage << exception.what(); |
| 34 | |
| 35 | const char* const* file = boost::get_error_info<boost::throw_file>(exception); |
| 36 | const int* line = boost::get_error_info<boost::throw_line>(exception); |
| 37 | const char* const* func = boost::get_error_info<boost::throw_function>(exception); |
| 38 | if (file && line) { |
| 39 | errorMessage << " [from " << *file << ":" << *line; |
| 40 | if (func) { |
| 41 | errorMessage << " in " << *func; |
| 42 | } |
| 43 | errorMessage << "]"; |
| 44 | } |
| 45 | |
| 46 | return errorMessage.str(); |
| 47 | } |
| 48 | |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 49 | static void |
| 50 | printUsage(std::ostream& os, const std::string& programName) |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 51 | { |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 52 | os << "Usage: " << programName << " [OPTIONS...]\n" |
| 53 | << "\n" |
| 54 | << "Options:\n" |
| 55 | << " -f <FILE> Path to configuration file\n" |
| 56 | << " -h Display this help message\n" |
| 57 | << " -V Display version information\n" |
| 58 | << std::endl; |
| 59 | } |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 60 | |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 61 | int |
| 62 | main(int argc, char** argv) |
| 63 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 64 | std::string programName(argv[0]); |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 65 | std::string configFileName("nlsr.conf"); |
Vince Lehman | c57c64b | 2015-08-10 12:21:31 -0500 | [diff] [blame] | 66 | |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 67 | int opt; |
| 68 | while ((opt = getopt(argc, argv, "hf:V")) != -1) { |
Vince Lehman | c57c64b | 2015-08-10 12:21:31 -0500 | [diff] [blame] | 69 | switch (opt) { |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 70 | case 'h': |
| 71 | printUsage(std::cout, programName); |
| 72 | return 0; |
| 73 | case 'f': |
| 74 | configFileName = optarg; |
| 75 | break; |
| 76 | case 'V': |
| 77 | std::cout << NLSR_VERSION_BUILD_STRING << std::endl; |
| 78 | return 0; |
| 79 | default: |
| 80 | printUsage(std::cerr, programName); |
| 81 | return 2; |
Vince Lehman | c57c64b | 2015-08-10 12:21:31 -0500 | [diff] [blame] | 82 | } |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 83 | } |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 84 | |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 85 | nlsr::NlsrRunner runner(configFileName); |
Vince Lehman | c57c64b | 2015-08-10 12:21:31 -0500 | [diff] [blame] | 86 | |
| 87 | try { |
| 88 | runner.run(); |
| 89 | } |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 90 | catch (const nlsr::NlsrRunner::ConfFileError& e) { |
| 91 | std::cerr << e.what() << std::endl; |
| 92 | return 2; |
| 93 | } |
Vince Lehman | c57c64b | 2015-08-10 12:21:31 -0500 | [diff] [blame] | 94 | catch (const std::exception& e) { |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 95 | std::cerr << "FATAL: " << getExtendedErrorMessage(e) << std::endl; |
| 96 | return 1; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 97 | } |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 98 | |
Davide Pesavento | e3c741d | 2019-01-29 20:28:15 -0500 | [diff] [blame^] | 99 | return 0; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 100 | } |