From 9b7c35a8162ec6adb5a1a5f1498cd4241b20a702 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Sat, 23 Jan 2016 09:37:05 +0100 Subject: [PATCH] replace getopt with boost::program_options --- src/future.cpp | 79 ++++++++++++++++++ src/smrtlink.cpp | 209 ++++++++++++++++------------------------------- 2 files changed, 151 insertions(+), 137 deletions(-) create mode 100644 src/future.cpp diff --git a/src/future.cpp b/src/future.cpp new file mode 100644 index 0000000..409d53b --- /dev/null +++ b/src/future.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include + +void asyncRun() { + std::cout << "Async..." << std::flush; + + boost::asio::io_service io_service; + std::shared_ptr < std::promise > promise(new std::promise()); + std::future future = promise->get_future(); + + io_service.post([promise]() + { + std::chrono::milliseconds dura( 2000 ); + std::this_thread::sleep_for( dura ); + promise->set_value(9); + }); + + std::thread t1([&io_service] {io_service.run();}); + t1.detach(); + + std::cout << "Waiting..." << std::flush; + future.wait(); + std::cout << "Done!\nResults are: " << future.get() << '\n'; + +} + +void nonBlockingRun() { + std::cout << "Non Blocking..." << std::flush; + + std::promise promise; + std::future future = promise.get_future(); + std::thread t1([](std::promise p) + { + std::chrono::milliseconds dura( 2000 ); + std::this_thread::sleep_for( dura ); + p.set_value(9); + }, std::move(promise)); + t1.detach(); + + std::cout << "Waiting...\n" << std::flush; + std::future_status status; + do { + status = future.wait_for(std::chrono::seconds(0)); + + if (status == std::future_status::deferred) { + std::cout << "+"; + } else if (status == std::future_status::timeout) { + std::cout << "."; + } + } while (status != std::future_status::ready); + std::cout << "Done!\nResults are: " << future.get() << '\n'; +} + +void blockingRun() { + std::cout << "Blocking..." << std::flush; + + std::promise promise; + std::future future = promise.get_future(); + std::thread t1([](std::promise p) + { + std::chrono::milliseconds dura( 2000 ); + std::this_thread::sleep_for( dura ); + p.set_value(9); + }, std::move(promise)); + t1.detach(); + + std::cout << "Waiting..." << std::flush; + future.wait(); + std::cout << "Done!\nResults are: " << future.get() << '\n'; +} + +int main2() { + nonBlockingRun(); + blockingRun(); + asyncRun(); +} diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index 3b3ed8f..de126e3 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -6,133 +6,92 @@ // Description : SmrtLink in C++, Ansi-style //============================================================================ -#include -#include -#include - #include +#include #include +#include #include #include -#include +#include #include #include "Constant.h" #include "Host.h" #include "Program.h" #include "Switch.h" -#include "lookup.h" #define no_argument 0 #define required_argument 1 #define optional_argument 2 using namespace std; +namespace po = boost::program_options; -//Options options; +Options options; constexpr unsigned int caseArg(const char* str, int h = 0) { return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h]; } -int main(int argc, char *argv[]) { +int main3(int argc, char *argv[]) { int opt, index; options.user = DEFAULT_USER; options.password = DEFAULT_PASS; - const struct option longopts[] = { { "version", no_argument, 0, 'V' }, { - "verbose", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { - "reverse", no_argument, 0, 'r' }, - { "permanent", no_argument, 0, 's' }, { "debug", optional_argument, - 0, 'd' }, { "password", required_argument, 0, 'p' }, { - "user", required_argument, 0, 'u' }, { "interface", - required_argument, 0, 'i' }, { "header", required_argument, - 0, 'b' }, { "hex", required_argument, 0, 'x' }, { "file", - required_argument, 0, 'f' }, { "timeout", required_argument, - 0, 't' }, { "wait", required_argument, 0, 'w' }, { 0, 0, 0, - 0 }, }; + // Declare the supported options. + po::options_description desc("Option Summary"); + desc.add_options()("help,h", "Display this help text")("version,V", + "Display version of this tool")("reverse,r", + po::bool_switch(&options.flags.REVERSE)->default_value(false), + "switch ports to emulate switch while sniffing")("permanent,s", + po::bool_switch(&options.flags.PERMANENT)->default_value(false), + "Not yet implemented: make changes immediately permanent")("hex,x", + po::bool_switch(&options.flags.HEX)->default_value(false), + "Display Packets as Hex String")("json,j", + po::bool_switch(&options.flags.JSON)->default_value(false), + "Display Packets as JSON")("header,b", + po::bool_switch(&options.flags.HEADER)->default_value(false), + "Show header")("wait,w", + po::bool_switch(&options.flags.WAIT)->default_value(false), + "Not yet implemented: blocking until operation is completed")( + "debug,d", + po::value()->implicit_value(1)->default_value(0)->value_name( + "n"), "Show debugging messages")("password,p", + po::value(&options.password)->value_name("password"), "")( + "user,u", po::value()->value_name("username"), "")( + "interface,i", + po::value(&options.interface)->value_name("iface"), + "only use one Interface")("file,f", + po::value(&options.file)->value_name("path"), + "choose a settings file")("timeout,t", + po::value(&options.timeout)->default_value(180L)->value_name( + "n"), "Timeout in milliseconds")("verbose,v", + po::value()->implicit_value(1)->default_value(0)->value_name( + "n"), "verbosity level"); + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help")) { + cerr << VERSION; + cerr << USAGE; + cerr << desc << "\n"; + cerr << HELP; + exit(EXIT_SUCCESS); + return 1; + } + + if (vm.count("version")) { + cerr << VERSION; + exit(EXIT_SUCCESS); + return 1; + } Program p = Program(); - while ((opt = getopt_long(argc, argv, "bhrvVswxp:u:i:f:t:d::", longopts, - &index)) != -1) { - switch (opt) { - - case 'h': - fprintf(stderr, VERSION); - fprintf(stderr, USAGE, argv[0]); - fprintf(stderr, HELP); - exit(EXIT_SUCCESS); - break; - - case 'V': - fprintf(stderr, VERSION); - exit(EXIT_SUCCESS); - break; - - case 'r': - options.flags.REVERSE = true; - break; - - case 'b': - options.flags.HEADER = true; - break; - - case 'x': - options.flags.HEX = true; - break; - - case 's': - options.flags.PERMANENT = true; - break; - - case 'w': - options.flags.WAIT = true; - break; - - case 'v': - if (optarg != NULL) - options.verbosity = atoi(optarg); - else - options.verbosity++; - break; - - case 'd': - if (optarg != NULL) - options.debug_level = atoi(optarg); - else - options.debug_level++; - break; - - case 't': - options.timeout = atol(optarg); - break; - - case 'f': - options.file = std::string(optarg); - break; - - case 'p': - options.password = std::string(optarg); - break; - - case 'u': - options.user = std::string(optarg); - break; - - case 'i': - options.interface = std::string(optarg); - break; - - default: /* '?' */ - fprintf(stderr, "Unknown option\n"); - fprintf(stderr, USAGE, argv[0]); - exit(EXIT_FAILURE); - } - } - /*//TODO stdin std::ostringstream bucket; bucket << std::cin.rdbuf(); @@ -146,13 +105,21 @@ int main(int argc, char *argv[]) { } p.init(); - std::vector vect; - std::map list; - std::cmatch sm; if (optind < argc) { std::string cmd = std::string(argv[optind++]); + switch (caseArg(cmd.c_str())) { + case caseArg("set"): + while (optind < argc) { + printf("+%s\n", argv[optind]); + optind++; + } + if (!p.setProperty({})) + exit(EXIT_SUCCESS); + fprintf(stderr, "Not yet implemented.\n"); + exit(EXIT_FAILURE); + break; case caseArg("reboot"): if (!p.reboot()) exit(EXIT_SUCCESS); @@ -184,6 +151,11 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); break; + case caseArg("get"): + if (!p.getProperty({})) + exit(EXIT_SUCCESS); + break; + case caseArg("list"): if (!p.list()) exit(EXIT_SUCCESS); @@ -205,44 +177,7 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } break; - case caseArg("set"): - while (optind < argc) { - if (regex_match(argv[optind], sm, - std::regex("^([a-z]+)=(.*)$"))) { - if (!snd_lookup.exists(sm[1])) { - cerr << "Unknown argument " << argv[optind] << endl; - exit(EXIT_FAILURE); - } - list.insert( - std::pair(sm[1], sm[2])); - } else { - cerr << "Invalid Syntax " << argv[optind] << endl; - exit(EXIT_FAILURE); - } - optind++; - } - if (!p.setProperty(list)) - exit(EXIT_SUCCESS); - fprintf(stderr, "Not yet implemented.\n"); - exit(EXIT_FAILURE); - break; - case caseArg("get"): - while (optind < argc) { - if (regex_match(argv[optind], sm, std::regex("^([a-z]+)$"))) { - if (!snd_lookup.exists(sm[1])) { - cerr << "Unknown argument " << argv[optind] << endl; - exit(EXIT_FAILURE); - } - vect.push_back(sm[1]); - } else { - cerr << "Invalid argument " << argv[optind] << endl; - exit(EXIT_FAILURE); - } - optind++; - } - if (!p.getProperty(vect)) - exit(EXIT_SUCCESS); - break; + default: printf("Unknown command: %s\n", cmd.c_str()); exit(EXIT_FAILURE);