sway-patched-tray-menu/sway/commands/assign.c

59 lines
1.7 KiB
C
Raw Normal View History

2017-03-11 05:41:24 +01:00
#define _XOPEN_SOURCE 500
2016-09-02 04:39:08 +02:00
#include <stdio.h>
#include <string.h>
2016-09-02 21:11:48 +02:00
#include "sway/commands.h"
#include "sway/criteria.h"
2016-09-02 04:39:08 +02:00
#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);
2016-12-16 00:26:53 +01:00
if (!cmdlist) {
return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate command list");
}
2016-09-02 04:39:08 +02:00
snprintf(cmdlist, arglen, "%s%s", movecmd, *argv);
struct criteria *crit = malloc(sizeof(struct criteria));
2016-12-15 23:39:09 +01:00
if (!crit) {
2016-12-16 00:26:53 +01:00
free(cmdlist);
2016-12-15 23:39:09 +01:00
return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate criteria");
}
2016-09-02 04:39:08 +02:00
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);
}