diff options
Diffstat (limited to 'sway/commands/assign.c')
-rw-r--r-- | sway/commands/assign.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/sway/commands/assign.c b/sway/commands/assign.c new file mode 100644 index 00000000..31e949fb --- /dev/null +++ b/sway/commands/assign.c | |||
@@ -0,0 +1,51 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <string.h> | ||
3 | #include "commands.h" | ||
4 | #include "criteria.h" | ||
5 | #include "list.h" | ||
6 | #include "log.h" | ||
7 | |||
8 | struct cmd_results *cmd_assign(int argc, char **argv) { | ||
9 | struct cmd_results *error = NULL; | ||
10 | if ((error = checkarg(argc, "assign", EXPECTED_AT_LEAST, 2))) { | ||
11 | return error; | ||
12 | } | ||
13 | |||
14 | char *criteria = *argv++; | ||
15 | |||
16 | if (strncmp(*argv, "→", strlen("→")) == 0) { | ||
17 | if (argc < 3) { | ||
18 | return cmd_results_new(CMD_INVALID, "assign", "Missing workspace"); | ||
19 | } | ||
20 | argv++; | ||
21 | } | ||
22 | |||
23 | char *movecmd = "move container to workspace "; | ||
24 | int arglen = strlen(movecmd) + strlen(*argv) + 1; | ||
25 | char *cmdlist = calloc(1, arglen); | ||
26 | |||
27 | snprintf(cmdlist, arglen, "%s%s", movecmd, *argv); | ||
28 | |||
29 | struct criteria *crit = malloc(sizeof(struct criteria)); | ||
30 | crit->crit_raw = strdup(criteria); | ||
31 | crit->cmdlist = cmdlist; | ||
32 | crit->tokens = create_list(); | ||
33 | char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); | ||
34 | |||
35 | if (err_str) { | ||
36 | error = cmd_results_new(CMD_INVALID, "assign", err_str); | ||
37 | free(err_str); | ||
38 | free_criteria(crit); | ||
39 | } else if (crit->tokens->length == 0) { | ||
40 | error = cmd_results_new(CMD_INVALID, "assign", "Found no name/value pairs in criteria"); | ||
41 | free_criteria(crit); | ||
42 | } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) { | ||
43 | sway_log(L_DEBUG, "assign: Duplicate, skipping."); | ||
44 | free_criteria(crit); | ||
45 | } else { | ||
46 | sway_log(L_DEBUG, "assign: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist); | ||
47 | list_add(config->criteria, crit); | ||
48 | } | ||
49 | return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
50 | } | ||
51 | |||