replace getopt with boost::program_options
This commit is contained in:
parent
8200103514
commit
9b7c35a816
2 changed files with 151 additions and 137 deletions
79
src/future.cpp
Normal file
79
src/future.cpp
Normal file
|
@ -0,0 +1,79 @@
|
|||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <future>
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
void asyncRun() {
|
||||
std::cout << "Async..." << std::flush;
|
||||
|
||||
boost::asio::io_service io_service;
|
||||
std::shared_ptr < std::promise<int> > promise(new std::promise<int>());
|
||||
std::future<int> 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<int> promise;
|
||||
std::future<int> future = promise.get_future();
|
||||
std::thread t1([](std::promise<int> 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<int> promise;
|
||||
std::future<int> future = promise.get_future();
|
||||
std::thread t1([](std::promise<int> 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();
|
||||
}
|
209
src/smrtlink.cpp
209
src/smrtlink.cpp
|
@ -6,133 +6,92 @@
|
|||
// Description : SmrtLink in C++, Ansi-style
|
||||
//============================================================================
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <regex>
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
|
||||
#include <getopt.h>
|
||||
#include <boost/program_options.hpp>
|
||||
#include <unistd.h>
|
||||
|
||||
#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<int>()->implicit_value(1)->default_value(0)->value_name(
|
||||
"n"), "Show debugging messages")("password,p",
|
||||
po::value<string>(&options.password)->value_name("password"), "")(
|
||||
"user,u", po::value<string>()->value_name("username"), "")(
|
||||
"interface,i",
|
||||
po::value<string>(&options.interface)->value_name("iface"),
|
||||
"only use one Interface")("file,f",
|
||||
po::value<string>(&options.file)->value_name("path"),
|
||||
"choose a settings file")("timeout,t",
|
||||
po::value<long>(&options.timeout)->default_value(180L)->value_name(
|
||||
"n"), "Timeout in milliseconds")("verbose,v",
|
||||
po::value<int>()->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<std::string> vect;
|
||||
std::map<std::string, std::string> 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<std::string, std::string>(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);
|
||||
|
|
Loading…
Reference in a new issue