Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up argument parsing in most commands #1882

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 68 additions & 34 deletions common/rfb/Configuration.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,12 @@ Configuration* Configuration::viewer() {

// -=- Configuration implementation

bool Configuration::set(const char* n, const char* v, bool immutable) {
return set(n, strlen(n), v, immutable);
}

bool Configuration::set(const char* paramName, int len,
const char* val, bool immutable)
bool Configuration::set(const char* paramName, const char* val,
bool immutable)
{
VoidParameter* current = head;
while (current) {
if ((int)strlen(current->getName()) == len &&
strncasecmp(current->getName(), paramName, len) == 0)
if (strcasecmp(current->getName(), paramName) == 0)
{
bool b = current->setParam(val);
if (b && immutable)
Expand All @@ -92,32 +87,7 @@ bool Configuration::set(const char* paramName, int len,
}
current = current->_next;
}
return _next ? _next->set(paramName, len, val, immutable) : false;
}

bool Configuration::set(const char* config, bool immutable) {
bool hyphen = false;
if (config[0] == '-') {
hyphen = true;
config++;
if (config[0] == '-') config++; // allow gnu-style --<option>
}
const char* equal = strchr(config, '=');
if (equal) {
return set(config, equal-config, equal+1, immutable);
} else if (hyphen) {
VoidParameter* current = head;
while (current) {
if (strcasecmp(current->getName(), config) == 0) {
bool b = current->setParam();
if (b && immutable)
current->setImmutable();
return b;
}
current = current->_next;
}
}
return _next ? _next->set(config, immutable) : false;
return _next ? _next->set(paramName, val, immutable) : false;
}

VoidParameter* Configuration::get(const char* param)
Expand Down Expand Up @@ -189,6 +159,70 @@ bool Configuration::remove(const char* param) {
return false;
}

int Configuration::handleArg(int argc, char* argv[], int index)
{
std::string param, val;
const char* equal = strchr(argv[index], '=');

if (equal == argv[index])
return 0;

if (equal) {
param.assign(argv[index], equal-argv[index]);
val.assign(equal+1);
} else {
param.assign(argv[index]);
}

if ((param.length() > 0) && (param[0] == '-')) {
// allow gnu-style --<option>
if ((param.length() > 1) && (param[1] == '-'))
param = param.substr(2);
else
param = param.substr(1);
} else {
// All command line arguments need either an initial '-', or an '='
if (val.empty())
return 0;
}

if (!val.empty())
return set(param.c_str(), val.c_str()) ? 1 : 0;

VoidParameter* current = head;
while (current) {
if (strcasecmp(current->getName(), param.c_str()) != 0) {
current = current->_next;
continue;
}

// We need to resolve an ambiguity for booleans
if (dynamic_cast<BoolParameter*>(current) != nullptr) {
if (index+1 < argc) {
// FIXME: Should not duplicate the list of values here
if ((strcasecmp(argv[index+1], "0") == 0) ||
(strcasecmp(argv[index+1], "1") == 0) ||
(strcasecmp(argv[index+1], "true") == 0) ||
(strcasecmp(argv[index+1], "false") == 0) ||
(strcasecmp(argv[index+1], "yes") == 0) ||
(strcasecmp(argv[index+1], "no") == 0)) {
return current->setParam(argv[index+1]) ? 2 : 0;
}
}
}

if (current->setParam())
return 1;

if (index+1 >= argc)
return 0;

return current->setParam(argv[index+1]) ? 2 : 0;
}

return _next ? _next->handleArg(argc, argv, index) : 0;
}


// -=- VoidParameter

Expand Down
29 changes: 7 additions & 22 deletions common/rfb/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,6 @@ namespace rfb {
// - Set named parameter to value
bool set(const char* param, const char* value, bool immutable=false);

// - Set parameter to value (separated by "=")
bool set(const char* config, bool immutable=false);

// - Set named parameter to value, with name truncated at len
bool set(const char* name, int len,
const char* val, bool immutable);

// - Get named parameter
VoidParameter* get(const char* param);

Expand All @@ -89,14 +82,10 @@ namespace rfb {
// - Remove a parameter from this Configuration group
bool remove(const char* param);

// - readFromFile
// Read configuration parameters from the specified file.
void readFromFile(const char* filename);

// - writeConfigToFile
// Write a new configuration parameters file, then mv it
// over the old file.
void writeToFile(const char* filename);
// - handleArg
// Parse a command line argument into a parameter, returning how
// many arguments were consumed
int handleArg(int argc, char* argv[], int index);


// - Get the Global Configuration object
Expand All @@ -114,20 +103,16 @@ namespace rfb {
static bool setParam(const char* param, const char* value, bool immutable=false) {
return global()->set(param, value, immutable);
}
static bool setParam(const char* config, bool immutable=false) {
return global()->set(config, immutable);
}
static bool setParam(const char* name, int len,
const char* val, bool immutable) {
return global()->set(name, len, val, immutable);
}
static VoidParameter* getParam(const char* param) { return global()->get(param); }
static void listParams(int width=79, int nameWidth=10) {
global()->list(width, nameWidth);
}
static bool removeParam(const char* param) {
return global()->remove(param);
}
static int handleParamArg(int argc, char* argv[], int index) {
return global()->handleArg(argc, argv, index);
}

private:
friend class VoidParameter;
Expand Down
16 changes: 7 additions & 9 deletions tests/perf/encperf.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -433,18 +433,16 @@ int main(int argc, char **argv)

fn = nullptr;
for (i = 1; i < argc; i++) {
if (rfb::Configuration::setParam(argv[i]))
int ret;

ret = rfb::Configuration::handleParamArg(argc, argv, i);
if (ret > 0) {
i += ret;
continue;
}

if (argv[i][0] == '-') {
if (i + 1 < argc) {
if (rfb::Configuration::setParam(&argv[i][1], argv[i + 1])) {
i++;
continue;
}
}
if (argv[i][0] == '-')
usage(argv[0]);
}

if (fn != nullptr)
usage(argv[0]);
Expand Down
10 changes: 7 additions & 3 deletions unix/vncconfig/vncExt.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,26 @@ Bool XVncExtQueryExtension(Display* dpy, int* event_basep, int* error_basep)
return True;
}

Bool XVncExtSetParam(Display* dpy, const char* param)
Bool XVncExtSetParam(Display* dpy, const char* param, const char* value)
{
xVncExtSetParamReq* req;
xVncExtSetParamReply rep;

int paramLen = strlen(param);
if (paramLen > 255) return False;
if (paramLen > 65535) return False;
int valueLen = strlen(value);
if (valueLen > 65535) return False;
if (!checkExtension(dpy)) return False;

LockDisplay(dpy);
GetReq(VncExtSetParam, req);
req->reqType = codes->major_opcode;
req->vncExtReqType = X_VncExtSetParam;
req->length += (paramLen + 3) >> 2;
req->length += ((paramLen + 3) >> 2) + ((valueLen + 3) >> 2);
req->paramLen = paramLen;
req->valueLen = valueLen;
Data(dpy, param, paramLen);
Data(dpy, value, valueLen);
if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
UnlockDisplay(dpy);
SyncHandle();
Expand Down
Loading
Loading