--set - handle spaces on either side of '='

Discard spaces when provided as part of --set around the '='. For
example, "val=key", "val = key", "val= key" and "val =key" are
all equivalent now.
pull/1523/head
Jason Ish 10 years ago committed by Victor Julien
parent d9fe95bc8a
commit ae23144b67

@ -232,6 +232,56 @@ int ConfSet(const char *name, char *val)
return 1;
}
/**
* \brief Set a configuration parameter from a string.
*
* Where the input string is something like:
* stream.midstream=true
*
* \param input the input string to be parsed.
*
* \retval 1 if the value of set, otherwise 0.
*/
int ConfSetFromString(const char *input, int final)
{
int retval = 0;
char *name = SCStrdup(input), *val = NULL;
if (unlikely(name == NULL)) {
goto done;
}
val = strchr(name, '=');
if (val == NULL) {
goto done;
}
*val++ = '\0';
while (isspace((int)name[strlen(name) - 1])) {
name[strlen(name) - 1] = '\0';
}
while (isspace((int)*val)) {
val++;
}
if (final) {
if (!ConfSetFinal(name, val)) {
goto done;
}
}
else {
if (!ConfSet(name, val)) {
goto done;
}
}
retval = 1;
done:
if (name != NULL) {
SCFree(name);
}
return retval;
}
/**
* \brief Set a final configuration value.
*
@ -1399,6 +1449,65 @@ end:
return retval;
}
static int ConfSetFromStringTest(void)
{
int retval = 0;
ConfNode *n;
ConfCreateContextBackup();
ConfInit();
if (!ConfSetFromString("stream.midstream=true", 0)) {
goto end;
}
n = ConfGetNode("stream.midstream");
if (n == NULL) {
goto end;
}
if (n->val == NULL || strcmp("true", n->val)) {
goto end;
}
if (!ConfSetFromString("stream.midstream =false", 0)) {
goto end;
}
n = ConfGetNode("stream.midstream");
if (n == NULL) {
goto end;
}
if (n->val == NULL || strcmp("false", n->val)) {
goto end;
}
if (!ConfSetFromString("stream.midstream= true", 0)) {
goto end;
}
n = ConfGetNode("stream.midstream");
if (n == NULL) {
goto end;
}
if (n->val == NULL || strcmp("true", n->val)) {
goto end;
}
if (!ConfSetFromString("stream.midstream = false", 0)) {
goto end;
}
n = ConfGetNode("stream.midstream");
if (n == NULL) {
goto end;
}
if (n->val == NULL || strcmp("false", n->val)) {
goto end;
}
retval = 1;
end:
ConfDeInit();
ConfRestoreContextBackup();
return retval;
}
void ConfRegisterTests(void)
{
UtRegisterTest("ConfTestGetNonExistant", ConfTestGetNonExistant, 1);
@ -1417,6 +1526,7 @@ void ConfRegisterTests(void)
UtRegisterTest("ConfGetNodeOrCreateTest", ConfGetNodeOrCreateTest, 1);
UtRegisterTest("ConfNodePruneTest", ConfNodePruneTest, 1);
UtRegisterTest("ConfNodeIsSequenceTest", ConfNodeIsSequenceTest, 1);
UtRegisterTest("ConfSetFromStringTest", ConfSetFromStringTest, 1);
}
#endif /* UNITTESTS */

@ -62,6 +62,7 @@ int ConfGetBool(const char *name, int *val);
int ConfGetDouble(const char *name, double *val);
int ConfGetFloat(const char *name, float *val);
int ConfSet(const char *name, char *val);
int ConfSetFromString(const char *input, int final);
int ConfSetFinal(const char *name, char *val);
void ConfDump(void);
void ConfNodeDump(const ConfNode *node, const char *prefix);

@ -1479,14 +1479,14 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
#endif
else if (strcmp((long_opts[option_index]).name, "set") == 0) {
if (optarg != NULL) {
/* Quick validation. */
char *val = strchr(optarg, '=');
if (val == NULL) {
SCLogError(SC_ERR_CMD_LINE,
"Invalid argument for --set, must be key=val.");
exit(EXIT_FAILURE);
}
*val++ = '\0';
if (ConfSetFinal(optarg, val) != 1) {
if (!ConfSetFromString(optarg, 1)) {
fprintf(stderr, "Failed to set configuration value %s.",
optarg);
exit(EXIT_FAILURE);

Loading…
Cancel
Save