commands: allow criterion values to be unquoted

Sometimes it doesn't really make sense to quote them (numeric values for
example)

In that case, the value is parsed until the next space or the end of the
whole criteria expression
This commit is contained in:
lbonn 2017-10-08 01:05:40 +02:00
parent d879e5b15d
commit 514eed7e4b

View file

@ -114,6 +114,7 @@ static char *crit_tokens(int *argc, char ***buf, const char * const criteria_str
char **argv = *buf = calloc(max_tokens, sizeof(char*)); char **argv = *buf = calloc(max_tokens, sizeof(char*));
argv[0] = base; // this needs to be freed by caller argv[0] = base; // this needs to be freed by caller
bool quoted = true;
*argc = 1; // uneven = name, even = value *argc = 1; // uneven = name, even = value
while (*head && *argc < max_tokens) { while (*head && *argc < max_tokens) {
@ -134,7 +135,8 @@ static char *crit_tokens(int *argc, char ***buf, const char * const criteria_str
if (*(namep) == ' ') { if (*(namep) == ' ') {
namep = strrchr(namep, ' ') + 1; namep = strrchr(namep, ' ') + 1;
} }
argv[(*argc)++] = namep; argv[*argc] = namep;
*argc += 1;
} }
} else if (*head == '"') { } else if (*head == '"') {
if (*argc % 2 != 0) { if (*argc % 2 != 0) {
@ -143,21 +145,38 @@ static char *crit_tokens(int *argc, char ***buf, const char * const criteria_str
"Found quoted value where it was not expected"); "Found quoted value where it was not expected");
} else if (!valp) { // value starts here } else if (!valp) { // value starts here
valp = head + 1; valp = head + 1;
quoted = true;
} else { } else {
// value ends here // value ends here
argv[(*argc)++] = valp; argv[*argc] = valp;
*argc += 1;
*head = '\0'; *head = '\0';
valp = NULL; valp = NULL;
namep = head + 1; namep = head + 1;
} }
} else if (*argc % 2 == 0 && !valp && *head != ' ') { } else if (*argc % 2 == 0 && *head != ' ') {
// We're expecting a quoted value, haven't found one yet, and this // parse unquoted values
// is not an empty space. if (!valp) {
return strdup("Unable to parse criteria: " quoted = false;
"Names must be unquoted, values must be quoted"); valp = head; // value starts here
}
} else if (valp && !quoted && *head == ' ') {
// value ends here
argv[*argc] = valp;
*argc += 1;
*head = '\0';
valp = NULL;
namep = head + 1;
} }
head++; head++;
} }
// catch last unquoted value if needed
if (valp && !quoted && !*head) {
argv[*argc] = valp;
*argc += 1;
}
return NULL; return NULL;
} }