Skip to content

Commit

Permalink
koekeishiya#307 properly parse key-value pairs in rules and signals w…
Browse files Browse the repository at this point in the history
…hen the value contains =
  • Loading branch information
koekeishiya authored and brorbw committed Jan 28, 2020
1 parent 90f3d59 commit 7acfb5f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [Unreleased]
### Changed
- Remove buffer-size restriction when reading data from socket [#221](https://github.com/koekeishiya/yabai/issues/221)
- Replace strtok with custom function to parse key-value arguments in rules and signals [#307](https://github.com/koekeishiya/yabai/issues/307)

## [2.1.0] - 2019-11-09
### Added
Expand Down
31 changes: 27 additions & 4 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,25 @@ static struct token get_token(char **message)
return token;
}

static void get_key_value_pair(char *token, char **key, char **value)
{
*key = token;

while (*token && *token != '=') {
++token;
}

if (*token != '=') {
*key = NULL;
*value = NULL;
} else if (token[1]) {
*token = '\0';
*value = token+1;
} else {
*value = NULL;
}
}

static void daemon_fail(FILE *rsp, char *fmt, ...)
{
if (!rsp) return;
Expand Down Expand Up @@ -1730,8 +1749,10 @@ static void handle_domain_rule(FILE *rsp, struct token domain, char *message)
struct rule *rule = rule_create();
struct token token = get_token(&message);
while (token.text && token.length > 0) {
char *key = strtok(token.text, "=");
char *value = strtok(NULL, "=");
char *key = NULL;
char *value = NULL;
get_key_value_pair(token.text, &key, &value);

if (!key || !value) {
daemon_fail(rsp, "invalid key-value pair '%s'\n", token.text);
return;
Expand Down Expand Up @@ -1835,8 +1856,10 @@ static void handle_domain_signal(FILE *rsp, struct token domain, char *message)

struct token token = get_token(&message);
while (token.text && token.length > 0) {
char *key = strtok(token.text, "=");
char *value = strtok(NULL, "=");
char *key = NULL;
char *value = NULL;
get_key_value_pair(token.text, &key, &value);

if (!key || !value) {
daemon_fail(rsp, "invalid key-value pair '%s'\n", token.text);
return;
Expand Down

0 comments on commit 7acfb5f

Please sign in to comment.