aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlbonn <bonnans.l@gmail.com>2017-10-08 01:05:40 +0200
committerlbonn <bonnans.l@gmail.com>2017-10-08 11:54:46 +0200
commit514eed7e4b256565d85c63014500d1252fec2928 (patch)
tree88c0cdfc09ac13100c8425ef72258cca71b64a91
parentd879e5b15d146a04ed7585b6c78653c4ac56b6dd (diff)
downloadsway-514eed7e4b256565d85c63014500d1252fec2928.zip
sway-514eed7e4b256565d85c63014500d1252fec2928.tar.gz
sway-514eed7e4b256565d85c63014500d1252fec2928.tar.bz2
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
-rw-r--r--sway/criteria.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/sway/criteria.c b/sway/criteria.c
index 9a04b48..f5fe40c 100644
--- a/sway/criteria.c
+++ b/sway/criteria.c
@@ -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*));
argv[0] = base; // this needs to be freed by caller
+ bool quoted = true;
*argc = 1; // uneven = name, even = value
while (*head && *argc < max_tokens) {
@@ -134,7 +135,8 @@ static char *crit_tokens(int *argc, char ***buf, const char * const criteria_str
if (*(namep) == ' ') {
namep = strrchr(namep, ' ') + 1;
}
- argv[(*argc)++] = namep;
+ argv[*argc] = namep;
+ *argc += 1;
}
} else if (*head == '"') {
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");
} else if (!valp) { // value starts here
valp = head + 1;
+ quoted = true;
} else {
// value ends here
- argv[(*argc)++] = valp;
+ argv[*argc] = valp;
+ *argc += 1;
*head = '\0';
valp = NULL;
namep = head + 1;
}
- } else if (*argc % 2 == 0 && !valp && *head != ' ') {
- // We're expecting a quoted value, haven't found one yet, and this
- // is not an empty space.
- return strdup("Unable to parse criteria: "
- "Names must be unquoted, values must be quoted");
+ } else if (*argc % 2 == 0 && *head != ' ') {
+ // parse unquoted values
+ if (!valp) {
+ quoted = false;
+ 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++;
}
+
+ // catch last unquoted value if needed
+ if (valp && !quoted && !*head) {
+ argv[*argc] = valp;
+ *argc += 1;
+ }
+
return NULL;
}