Added better generic support for connector options.

This commit is contained in:
Thulinma 2013-08-07 16:04:57 +02:00
parent 0daa757e92
commit 0c059f0ca3
2 changed files with 86 additions and 20 deletions

View file

@ -21,6 +21,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <fstream> #include <fstream>
#include <dirent.h> //for getMyExec
bool Util::Config::is_active = false; bool Util::Config::is_active = false;
std::string Util::Config::libver = PACKAGE_VERSION; std::string Util::Config::libver = PACKAGE_VERSION;
@ -167,7 +168,7 @@ void Util::Config::printHelp(std::ostream & output){
/// Parses commandline arguments. /// Parses commandline arguments.
/// Calls exit if an unknown option is encountered, printing a help message. /// Calls exit if an unknown option is encountered, printing a help message.
void Util::Config::parseArgs(int & argc, char ** & argv){ bool Util::Config::parseArgs(int & argc, char ** & argv){
int opt = 0; int opt = 0;
std::string shortopts; std::string shortopts;
struct option * longOpts = (struct option*)calloc(long_count + 1, sizeof(struct option)); struct option * longOpts = (struct option*)calloc(long_count + 1, sizeof(struct option));
@ -250,10 +251,9 @@ void Util::Config::parseArgs(int & argc, char ** & argv){
long_i++; long_i++;
} }
if (long_i <= arg_count){ if (long_i <= arg_count){
std::cerr << "Usage error: missing argument(s)." << std::endl; return false;
printHelp(std::cout);
exit(1);
} }
return true;
} }
/// Returns a reference to the current value of an option or default if none was set. /// Returns a reference to the current value of an option or default if none was set.
@ -334,22 +334,67 @@ void Util::Config::signal_handler(int signum){
} }
} //signal_handler } //signal_handler
/// Adds the default connector options to this Util::Config object. /// Adds the default connector options. Also updates the capabilities structure with the default options.
void Util::Config::addConnectorOptions(int port){ /// Besides the options addBasicConnectorOptions adds, this function also adds port and interface options.
JSON::Value stored_port = JSON::fromString("{\"long\":\"port\", \"short\":\"p\", \"arg\":\"integer\", \"help\":\"TCP port to listen on.\"}"); void Util::Config::addConnectorOptions(int port, JSON::Value & capabilities){
stored_port["value"].append((long long int)port); JSON::Value option;
addOption("listen_port", stored_port); option.null();
addOption("listen_interface", option["long"] = "port";
JSON::fromString( option["short"] = "p";
"{\"long\":\"interface\", \"value\":[\"0.0.0.0\"], \"short\":\"i\", \"arg\":\"string\", \"help\":\"Interface address to listen on, or 0.0.0.0 for all available interfaces.\"}")); option["arg"] = "integer";
addOption("username", option["help"] = "TCP port to listen on";
JSON::fromString( option["value"].append((long long)port);
"{\"long\":\"username\", \"value\":[\"root\"], \"short\":\"u\", \"arg\":\"string\", \"help\":\"Username to drop privileges to, or root to not drop provileges.\"}")); addOption("listen_port", option);
addOption("daemonize", capabilities["optional"]["port"]["name"] = "TCP port";
JSON::fromString( capabilities["optional"]["port"]["help"] = "TCP port to listen on - default if unprovided is "+option["value"][0u].asString();
"{\"long\":\"daemon\", \"short\":\"d\", \"value\":[1], \"long_off\":\"nodaemon\", \"short_off\":\"n\", \"help\":\"Whether or not to daemonize the process after starting.\"}")); capabilities["optional"]["port"]["type"] = "uint";
capabilities["optional"]["port"]["default"] = option["value"][0u];
option.null();
option["long"] = "interface";
option["short"] = "i";
option["arg"] = "string";
option["help"] = "Interface address to listen on, or 0.0.0.0 for all available interfaces.";
option["value"].append("0.0.0.0");
addOption("listen_interface", option);
capabilities["optional"]["interface"]["name"] = "Interface";
capabilities["optional"]["interface"]["help"] = "Address of the interface to listen on - default if unprovided is all interfaces";
capabilities["optional"]["interface"]["type"] = "str";
addBasicConnectorOptions(capabilities);
} //addConnectorOptions } //addConnectorOptions
/// Adds the default connector options. Also updates the capabilities structure with the default options.
void Util::Config::addBasicConnectorOptions(JSON::Value & capabilities){
JSON::Value option;
option.null();
option["long"] = "username";
option["short"] = "u";
option["arg"] = "string";
option["help"] = "Username to drop privileges to, or root to not drop provileges.";
option["value"].append("root");
addOption("username", option);
capabilities["optional"]["username"]["name"] = "Username";
capabilities["optional"]["username"]["help"] = "Username to drop privileges to - default if unprovided means do not drop privileges";
capabilities["optional"]["username"]["type"] = "str";
option.null();
option["long"] = "daemon";
option["short"] = "d";
option["long_off"] = "nodaemon";
option["short_off"] = "n";
option["help"] = "Whether or not to daemonize the process after starting.";
option["value"].append(1ll);
addOption("daemonize", option);
option.null();
option["long"] = "json";
option["short"] = "j";
option["help"] = "Output connector info in JSON format, then exit.";
option["value"].append(0ll);
addOption("json", option);
}
/// Gets directory the current executable is stored in. /// Gets directory the current executable is stored in.
std::string Util::getMyPath(){ std::string Util::getMyPath(){
char mypath[500]; char mypath[500];
@ -371,6 +416,23 @@ std::string Util::getMyPath(){
return tPath; return tPath;
} }
/// Gets all executables in getMyPath that start with "Mist".
void Util::getMyExec(std::deque<std::string> & execs){
std::string path = Util::getMyPath();
DIR * d = opendir(path.c_str());
if (!d){return;}
struct dirent *dp;
do {
errno = 0;
if (dp = readdir(d)){
if (strncmp(dp->d_name, "Mist", 4) == 0){
execs.push_back(dp->d_name);
}
}
} while (dp != NULL);
closedir(d);
}
/// Sets the current process' running user /// Sets the current process' running user
void Util::setUser(std::string username){ void Util::setUser(std::string username){
if (username != "root"){ if (username != "root"){

View file

@ -27,18 +27,22 @@ namespace Util {
Config(std::string cmd, std::string version); Config(std::string cmd, std::string version);
void addOption(std::string optname, JSON::Value option); void addOption(std::string optname, JSON::Value option);
void printHelp(std::ostream & output); void printHelp(std::ostream & output);
void parseArgs(int & argc, char ** & argv); bool parseArgs(int & argc, char ** & argv);
JSON::Value & getOption(std::string optname, bool asArray = false); JSON::Value & getOption(std::string optname, bool asArray = false);
std::string getString(std::string optname); std::string getString(std::string optname);
long long int getInteger(std::string optname); long long int getInteger(std::string optname);
bool getBool(std::string optname); bool getBool(std::string optname);
void activate(); void activate();
void addConnectorOptions(int port); void addBasicConnectorOptions(JSON::Value & capabilities);
void addConnectorOptions(int port, JSON::Value & capabilities);
}; };
/// Gets directory the current executable is stored in. /// Gets directory the current executable is stored in.
std::string getMyPath(); std::string getMyPath();
/// Gets all executables in getMyPath that start with "Mist".
void getMyExec(std::deque<std::string> & execs);
/// Will set the active user to the named username. /// Will set the active user to the named username.
void setUser(std::string user); void setUser(std::string user);