diff options
author | Zandr Martin <zandrmartin@gmail.com> | 2016-09-01 21:39:08 -0500 |
---|---|---|
committer | Zandr Martin <zandrmartin@gmail.com> | 2016-09-01 21:39:08 -0500 |
commit | b374c35758777f98e5ddbe4b0dc43bd7c80f36d7 (patch) | |
tree | 04bb4cfc3da7d2e0de7fbc38db42f65c66d2df4c /sway/commands/assign.c | |
parent | 050704ab23b698b00ee2ded8dbd4df9afceb8844 (diff) |
refactor commands.c
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 @@ +#include <stdio.h> +#include <string.h> +#include "commands.h" +#include "criteria.h" +#include "list.h" +#include "log.h" + +struct cmd_results *cmd_assign(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "assign", EXPECTED_AT_LEAST, 2))) { + return error; + } + + char *criteria = *argv++; + + if (strncmp(*argv, "→", strlen("→")) == 0) { + if (argc < 3) { + return cmd_results_new(CMD_INVALID, "assign", "Missing workspace"); + } + argv++; + } + + char *movecmd = "move container to workspace "; + int arglen = strlen(movecmd) + strlen(*argv) + 1; + char *cmdlist = calloc(1, arglen); + + snprintf(cmdlist, arglen, "%s%s", movecmd, *argv); + + struct criteria *crit = malloc(sizeof(struct criteria)); + crit->crit_raw = strdup(criteria); + crit->cmdlist = cmdlist; + crit->tokens = create_list(); + char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); + + if (err_str) { + error = cmd_results_new(CMD_INVALID, "assign", err_str); + free(err_str); + free_criteria(crit); + } else if (crit->tokens->length == 0) { + error = cmd_results_new(CMD_INVALID, "assign", "Found no name/value pairs in criteria"); + free_criteria(crit); + } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) { + sway_log(L_DEBUG, "assign: Duplicate, skipping."); + free_criteria(crit); + } else { + sway_log(L_DEBUG, "assign: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist); + list_add(config->criteria, crit); + } + return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + |