[common] options: trim whitespace from option values

This closes #1071
This commit is contained in:
Geoffrey McRae 2023-05-02 12:27:17 +10:00
parent dab5618a6d
commit 07bcc54732

View File

@ -346,8 +346,18 @@ bool option_parse(int argc, char * argv[])
o = state.options[i]; o = state.options[i];
if (o->type != OPTION_TYPE_BOOL && a < argc - 1) if (o->type != OPTION_TYPE_BOOL && a < argc - 1)
{ {
++a; char * v = argv[a];
value = strdup(argv[a]);
//ltrim
while(*v && isspace(*v))
++v;
int valueLen = strlen(v);
//rtrim
while (valueLen > 1 && isspace(v[valueLen-1]))
--valueLen;
value = strndup(v, valueLen);
} }
break; break;
} }
@ -369,7 +379,18 @@ bool option_parse(int argc, char * argv[])
o = option_get(module, name); o = option_get(module, name);
if (value) if (value)
value = strdup(value); {
//ltrim
while(*value && isspace(*value))
++value;
int valueLen = strlen(value);
//rtrim
while (valueLen > 1 && isspace(value[valueLen-1]))
--valueLen;
value = strndup(value, valueLen);
}
free(arg); free(arg);
} }
@ -458,7 +479,13 @@ static bool process_option_line(const char * module, const char * name,
else else
{ {
if (value) if (value)
{
//rtrim
while (valueLen > 1 && isspace(name[valueLen-1]))
--valueLen;
value[valueLen] = '\0'; value[valueLen] = '\0';
}
if (!option_set(o, value)) if (!option_set(o, value))
DEBUG_ERROR("Failed to set the option value"); DEBUG_ERROR("Failed to set the option value");