diff options
-rw-r--r-- | sway/criteria.c | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/sway/criteria.c b/sway/criteria.c index 9a04b48e..f5fe40cb 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 | |||
114 | 114 | ||
115 | char **argv = *buf = calloc(max_tokens, sizeof(char*)); | 115 | char **argv = *buf = calloc(max_tokens, sizeof(char*)); |
116 | argv[0] = base; // this needs to be freed by caller | 116 | argv[0] = base; // this needs to be freed by caller |
117 | bool quoted = true; | ||
117 | 118 | ||
118 | *argc = 1; // uneven = name, even = value | 119 | *argc = 1; // uneven = name, even = value |
119 | while (*head && *argc < max_tokens) { | 120 | while (*head && *argc < max_tokens) { |
@@ -134,7 +135,8 @@ static char *crit_tokens(int *argc, char ***buf, const char * const criteria_str | |||
134 | if (*(namep) == ' ') { | 135 | if (*(namep) == ' ') { |
135 | namep = strrchr(namep, ' ') + 1; | 136 | namep = strrchr(namep, ' ') + 1; |
136 | } | 137 | } |
137 | argv[(*argc)++] = namep; | 138 | argv[*argc] = namep; |
139 | *argc += 1; | ||
138 | } | 140 | } |
139 | } else if (*head == '"') { | 141 | } else if (*head == '"') { |
140 | if (*argc % 2 != 0) { | 142 | if (*argc % 2 != 0) { |
@@ -143,21 +145,38 @@ static char *crit_tokens(int *argc, char ***buf, const char * const criteria_str | |||
143 | "Found quoted value where it was not expected"); | 145 | "Found quoted value where it was not expected"); |
144 | } else if (!valp) { // value starts here | 146 | } else if (!valp) { // value starts here |
145 | valp = head + 1; | 147 | valp = head + 1; |
148 | quoted = true; | ||
146 | } else { | 149 | } else { |
147 | // value ends here | 150 | // value ends here |
148 | argv[(*argc)++] = valp; | 151 | argv[*argc] = valp; |
152 | *argc += 1; | ||
149 | *head = '\0'; | 153 | *head = '\0'; |
150 | valp = NULL; | 154 | valp = NULL; |
151 | namep = head + 1; | 155 | namep = head + 1; |
152 | } | 156 | } |
153 | } else if (*argc % 2 == 0 && !valp && *head != ' ') { | 157 | } else if (*argc % 2 == 0 && *head != ' ') { |
154 | // We're expecting a quoted value, haven't found one yet, and this | 158 | // parse unquoted values |
155 | // is not an empty space. | 159 | if (!valp) { |
156 | return strdup("Unable to parse criteria: " | 160 | quoted = false; |
157 | "Names must be unquoted, values must be quoted"); | 161 | valp = head; // value starts here |
162 | } | ||
163 | } else if (valp && !quoted && *head == ' ') { | ||
164 | // value ends here | ||
165 | argv[*argc] = valp; | ||
166 | *argc += 1; | ||
167 | *head = '\0'; | ||
168 | valp = NULL; | ||
169 | namep = head + 1; | ||
158 | } | 170 | } |
159 | head++; | 171 | head++; |
160 | } | 172 | } |
173 | |||
174 | // catch last unquoted value if needed | ||
175 | if (valp && !quoted && !*head) { | ||
176 | argv[*argc] = valp; | ||
177 | *argc += 1; | ||
178 | } | ||
179 | |||
161 | return NULL; | 180 | return NULL; |
162 | } | 181 | } |
163 | 182 | ||