2017-11-23 03:20:41 +01:00
|
|
|
#define _XOPEN_SOURCE 500
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <json-c/json.h>
|
|
|
|
#include "sway/commands.h"
|
2017-12-05 10:40:55 +01:00
|
|
|
#include "sway/config.h"
|
2018-01-20 22:21:45 +01:00
|
|
|
#include "sway/criteria.h"
|
2017-12-05 10:40:55 +01:00
|
|
|
#include "sway/security.h"
|
2017-12-11 10:17:14 +01:00
|
|
|
#include "sway/input/input-manager.h"
|
2018-01-21 14:46:31 +01:00
|
|
|
#include "sway/input/seat.h"
|
2017-11-23 03:20:41 +01:00
|
|
|
#include "stringop.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
struct cmd_handler {
|
|
|
|
char *command;
|
|
|
|
sway_cmd *handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns error object, or NULL if check succeeds.
|
|
|
|
struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) {
|
|
|
|
struct cmd_results *error = NULL;
|
|
|
|
switch (type) {
|
|
|
|
case EXPECTED_MORE_THAN:
|
|
|
|
if (argc > val) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
|
|
|
"(expected more than %d argument%s, got %d)",
|
|
|
|
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
|
|
|
break;
|
|
|
|
case EXPECTED_AT_LEAST:
|
|
|
|
if (argc >= val) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
|
|
|
"(expected at least %d argument%s, got %d)",
|
|
|
|
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
|
|
|
break;
|
|
|
|
case EXPECTED_LESS_THAN:
|
|
|
|
if (argc < val) {
|
|
|
|
return NULL;
|
|
|
|
};
|
|
|
|
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
|
|
|
"(expected less than %d argument%s, got %d)",
|
|
|
|
name, val, (char*[2]){"s", ""}[argc==1], argc);
|
|
|
|
break;
|
|
|
|
case EXPECTED_EQUAL_TO:
|
|
|
|
if (argc == val) {
|
|
|
|
return NULL;
|
|
|
|
};
|
|
|
|
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
|
|
|
|
"(expected %d arguments, got %d)", name, val, argc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-12-16 18:14:24 +01:00
|
|
|
void apply_input_config(struct input_config *input) {
|
2017-12-11 10:17:14 +01:00
|
|
|
int i;
|
|
|
|
i = list_seq_find(config->input_configs, input_identifier_cmp, input->identifier);
|
|
|
|
if (i >= 0) {
|
|
|
|
// merge existing config
|
|
|
|
struct input_config *ic = config->input_configs->items[i];
|
|
|
|
merge_input_config(ic, input);
|
|
|
|
free_input_config(input);
|
|
|
|
input = ic;
|
|
|
|
} else {
|
|
|
|
list_add(config->input_configs, input);
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:11:56 +01:00
|
|
|
sway_input_manager_apply_input_config(input_manager, input);
|
|
|
|
}
|
|
|
|
|
2017-12-16 18:14:24 +01:00
|
|
|
void apply_seat_config(struct seat_config *seat) {
|
2017-12-14 17:11:56 +01:00
|
|
|
int i;
|
|
|
|
i = list_seq_find(config->seat_configs, seat_name_cmp, seat->name);
|
|
|
|
if (i >= 0) {
|
|
|
|
// merge existing config
|
|
|
|
struct seat_config *sc = config->seat_configs->items[i];
|
|
|
|
merge_seat_config(sc, seat);
|
|
|
|
free_seat_config(seat);
|
|
|
|
seat = sc;
|
|
|
|
} else {
|
|
|
|
list_add(config->seat_configs, seat);
|
|
|
|
}
|
|
|
|
|
|
|
|
sway_input_manager_apply_seat_config(input_manager, seat);
|
2017-12-11 10:17:14 +01:00
|
|
|
}
|
|
|
|
|
2017-11-23 03:20:41 +01:00
|
|
|
/**
|
|
|
|
* Check and add color to buffer.
|
|
|
|
*
|
|
|
|
* return error object, or NULL if color is valid.
|
|
|
|
*/
|
|
|
|
struct cmd_results *add_color(const char *name, char *buffer, const char *color) {
|
|
|
|
int len = strlen(color);
|
|
|
|
if (len != 7 && len != 9) {
|
|
|
|
return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (color[0] != '#') {
|
|
|
|
return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < len; ++i) {
|
|
|
|
if (!isxdigit(color[i])) {
|
|
|
|
return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy color to buffer
|
|
|
|
strncpy(buffer, color, len);
|
|
|
|
// add default alpha channel if color was defined without it
|
|
|
|
if (len == 7) {
|
|
|
|
buffer[7] = 'f';
|
|
|
|
buffer[8] = 'f';
|
|
|
|
}
|
|
|
|
buffer[9] = '\0';
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-24 19:20:34 +01:00
|
|
|
/**
|
|
|
|
* handlers that can run in either config or command context
|
|
|
|
* Keep alphabetized
|
|
|
|
*/
|
2017-11-23 03:20:41 +01:00
|
|
|
static struct cmd_handler handlers[] = {
|
2017-12-27 16:08:18 +01:00
|
|
|
{ "bindcode", cmd_bindcode },
|
|
|
|
{ "bindsym", cmd_bindsym },
|
2017-12-04 22:43:49 +01:00
|
|
|
{ "exec", cmd_exec },
|
|
|
|
{ "exec_always", cmd_exec_always },
|
2017-12-05 18:47:57 +01:00
|
|
|
{ "include", cmd_include },
|
2017-12-11 10:17:14 +01:00
|
|
|
{ "input", cmd_input },
|
2017-12-06 12:36:06 +01:00
|
|
|
{ "output", cmd_output },
|
2017-12-14 17:11:56 +01:00
|
|
|
{ "seat", cmd_seat },
|
2018-01-31 05:09:21 +01:00
|
|
|
{ "workspace", cmd_workspace },
|
2017-11-23 03:20:41 +01:00
|
|
|
};
|
|
|
|
|
2018-02-24 19:34:47 +01:00
|
|
|
/**
|
|
|
|
* Commands that can *only* run in the config loading context
|
|
|
|
* Keep alphabetized
|
|
|
|
*/
|
|
|
|
static struct cmd_handler config_handlers[] = {
|
|
|
|
{ "set", cmd_set },
|
|
|
|
};
|
|
|
|
|
2018-02-24 19:20:34 +01:00
|
|
|
/**
|
|
|
|
* Commands that can *not* run in the config loading context
|
|
|
|
* Keep alphabetized
|
|
|
|
*/
|
|
|
|
static struct cmd_handler command_handlers[] = {
|
|
|
|
{ "exit", cmd_exit },
|
|
|
|
{ "focus", cmd_focus },
|
|
|
|
{ "kill", cmd_kill },
|
|
|
|
{ "layout", cmd_layout },
|
|
|
|
{ "reload", cmd_reload },
|
|
|
|
};
|
|
|
|
|
2017-11-23 03:20:41 +01:00
|
|
|
static int handler_compare(const void *_a, const void *_b) {
|
|
|
|
const struct cmd_handler *a = _a;
|
|
|
|
const struct cmd_handler *b = _b;
|
|
|
|
return strcasecmp(a->command, b->command);
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:55:20 +01:00
|
|
|
// must be in order for the bsearch
|
2017-12-11 10:17:14 +01:00
|
|
|
static struct cmd_handler input_handlers[] = {
|
|
|
|
{ "accel_profile", input_cmd_accel_profile },
|
|
|
|
{ "click_method", input_cmd_click_method },
|
|
|
|
{ "drag_lock", input_cmd_drag_lock },
|
|
|
|
{ "dwt", input_cmd_dwt },
|
|
|
|
{ "events", input_cmd_events },
|
|
|
|
{ "left_handed", input_cmd_left_handed },
|
|
|
|
{ "middle_emulation", input_cmd_middle_emulation },
|
|
|
|
{ "natural_scroll", input_cmd_natural_scroll },
|
|
|
|
{ "pointer_accel", input_cmd_pointer_accel },
|
|
|
|
{ "scroll_method", input_cmd_scroll_method },
|
|
|
|
{ "tap", input_cmd_tap },
|
2017-12-15 11:22:51 +01:00
|
|
|
{ "xkb_layout", input_cmd_xkb_layout },
|
|
|
|
{ "xkb_model", input_cmd_xkb_model },
|
|
|
|
{ "xkb_options", input_cmd_xkb_options },
|
|
|
|
{ "xkb_rules", input_cmd_xkb_rules },
|
|
|
|
{ "xkb_variant", input_cmd_xkb_variant },
|
2017-12-11 10:17:14 +01:00
|
|
|
};
|
|
|
|
|
2017-12-14 17:11:56 +01:00
|
|
|
// must be in order for the bsearch
|
|
|
|
static struct cmd_handler seat_handlers[] = {
|
|
|
|
{ "attach", seat_cmd_attach },
|
2017-12-17 16:39:22 +01:00
|
|
|
{ "fallback", seat_cmd_fallback },
|
2017-12-14 17:11:56 +01:00
|
|
|
};
|
|
|
|
|
2017-11-23 03:20:41 +01:00
|
|
|
static struct cmd_handler *find_handler(char *line, enum cmd_status block) {
|
|
|
|
struct cmd_handler d = { .command=line };
|
|
|
|
struct cmd_handler *res = NULL;
|
2018-01-05 22:32:51 +01:00
|
|
|
wlr_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_SEAT);
|
2017-12-11 10:17:14 +01:00
|
|
|
|
2018-02-24 19:20:34 +01:00
|
|
|
bool config_loading = config->reading || !config->active;
|
|
|
|
|
2017-12-11 10:17:14 +01:00
|
|
|
if (block == CMD_BLOCK_INPUT) {
|
2018-02-24 19:20:34 +01:00
|
|
|
// input commands can run in either context
|
|
|
|
return bsearch(&d, input_handlers,
|
2017-12-11 10:17:14 +01:00
|
|
|
sizeof(input_handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
2017-12-14 17:11:56 +01:00
|
|
|
} else if (block == CMD_BLOCK_SEAT) {
|
2018-02-24 19:20:34 +01:00
|
|
|
// seat commands can run in either context
|
|
|
|
return bsearch(&d, seat_handlers,
|
2017-12-14 17:11:56 +01:00
|
|
|
sizeof(seat_handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
2018-02-24 19:20:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!config_loading) {
|
|
|
|
res = bsearch(&d, command_handlers,
|
|
|
|
sizeof(command_handlers) / sizeof(struct cmd_handler),
|
2017-12-11 10:17:14 +01:00
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
2018-02-24 19:20:34 +01:00
|
|
|
|
|
|
|
if (res) {
|
|
|
|
return res;
|
|
|
|
}
|
2017-12-11 10:17:14 +01:00
|
|
|
}
|
|
|
|
|
2018-02-24 19:34:47 +01:00
|
|
|
if (config->reading) {
|
|
|
|
res = bsearch(&d, config_handlers,
|
|
|
|
sizeof(config_handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 19:20:34 +01:00
|
|
|
res = bsearch(&d, handlers,
|
|
|
|
sizeof(handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
|
|
|
|
2017-11-23 03:20:41 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-02-24 18:50:24 +01:00
|
|
|
struct cmd_results *execute_command(char *_exec, struct sway_seat *seat) {
|
2017-11-23 03:20:41 +01:00
|
|
|
// Even though this function will process multiple commands we will only
|
|
|
|
// return the last error, if any (for now). (Since we have access to an
|
2017-12-29 11:29:13 +01:00
|
|
|
// error string we could e.g. concatenate all errors there.)
|
2017-11-23 03:20:41 +01:00
|
|
|
struct cmd_results *results = NULL;
|
|
|
|
char *exec = strdup(_exec);
|
|
|
|
char *head = exec;
|
|
|
|
char *cmdlist;
|
|
|
|
char *cmd;
|
2018-01-20 22:21:45 +01:00
|
|
|
list_t *containers = NULL;
|
2017-11-23 03:20:41 +01:00
|
|
|
|
2018-02-24 18:50:24 +01:00
|
|
|
if (seat == NULL) {
|
|
|
|
// passing a NULL seat means we just pick the default seat
|
|
|
|
seat = sway_input_manager_get_default_seat(input_manager);
|
|
|
|
if (!sway_assert(seat, "could not find a seat to run the command on")) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config->handler_context.seat = seat;
|
|
|
|
|
2017-11-23 03:20:41 +01:00
|
|
|
head = exec;
|
|
|
|
do {
|
2018-01-20 22:21:45 +01:00
|
|
|
// Extract criteria (valid for this command list only).
|
|
|
|
bool has_criteria = false;
|
|
|
|
if (*head == '[') {
|
|
|
|
has_criteria = true;
|
|
|
|
++head;
|
|
|
|
char *criteria_string = argsep(&head, "]");
|
|
|
|
if (head) {
|
|
|
|
++head;
|
|
|
|
list_t *tokens = create_list();
|
|
|
|
char *error;
|
|
|
|
|
|
|
|
if ((error = extract_crit_tokens(tokens, criteria_string))) {
|
|
|
|
wlr_log(L_DEBUG, "criteria string parse error: %s", error);
|
|
|
|
results = cmd_results_new(CMD_INVALID, criteria_string,
|
|
|
|
"Can't parse criteria string: %s", error);
|
|
|
|
free(error);
|
|
|
|
free(tokens);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-01-21 20:11:41 +01:00
|
|
|
containers = container_for_crit_tokens(tokens);
|
2018-01-20 22:21:45 +01:00
|
|
|
|
|
|
|
free(tokens);
|
|
|
|
} else {
|
|
|
|
if (!results) {
|
|
|
|
results = cmd_results_new(CMD_INVALID, criteria_string, "Unmatched [");
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
// Skip leading whitespace
|
|
|
|
head += strspn(head, whitespace);
|
|
|
|
}
|
2017-11-23 03:20:41 +01:00
|
|
|
// Split command list
|
|
|
|
cmdlist = argsep(&head, ";");
|
|
|
|
cmdlist += strspn(cmdlist, whitespace);
|
|
|
|
do {
|
|
|
|
// Split commands
|
|
|
|
cmd = argsep(&cmdlist, ",");
|
|
|
|
cmd += strspn(cmd, whitespace);
|
|
|
|
if (strcmp(cmd, "") == 0) {
|
2018-01-05 22:32:51 +01:00
|
|
|
wlr_log(L_INFO, "Ignoring empty command.");
|
2017-11-23 03:20:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-05 22:32:51 +01:00
|
|
|
wlr_log(L_INFO, "Handling command '%s'", cmd);
|
2017-11-23 03:20:41 +01:00
|
|
|
//TODO better handling of argv
|
|
|
|
int argc;
|
|
|
|
char **argv = split_args(cmd, &argc);
|
|
|
|
if (strcmp(argv[0], "exec") != 0) {
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (*argv[i] == '\"' || *argv[i] == '\'') {
|
|
|
|
strip_quotes(argv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct cmd_handler *handler = find_handler(argv[0], CMD_BLOCK_END);
|
|
|
|
if (!handler) {
|
|
|
|
if (results) {
|
|
|
|
free_cmd_results(results);
|
|
|
|
}
|
|
|
|
results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command");
|
|
|
|
free_argv(argc, argv);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-01-20 22:21:45 +01:00
|
|
|
|
|
|
|
if (!has_criteria) {
|
2018-01-21 14:46:31 +01:00
|
|
|
// without criteria, the command acts upon the focused
|
|
|
|
// container
|
2018-02-24 18:50:24 +01:00
|
|
|
config->handler_context.current_container =
|
|
|
|
sway_seat_get_focus_inactive(seat, &root_container);
|
|
|
|
if (!sway_assert(config->handler_context.current_container,
|
|
|
|
"could not get focus-inactive for root container")) {
|
|
|
|
return NULL;
|
2018-01-21 14:46:31 +01:00
|
|
|
}
|
2018-02-24 18:50:24 +01:00
|
|
|
struct cmd_results *res = handler->handle(argc-1, argv+1);
|
|
|
|
if (res->status != CMD_SUCCESS) {
|
|
|
|
free_argv(argc, argv);
|
|
|
|
if (results) {
|
|
|
|
free_cmd_results(results);
|
2018-01-20 22:21:45 +01:00
|
|
|
}
|
2018-02-24 18:50:24 +01:00
|
|
|
results = res;
|
|
|
|
goto cleanup;
|
2018-01-20 22:21:45 +01:00
|
|
|
}
|
2018-02-24 18:50:24 +01:00
|
|
|
free_cmd_results(res);
|
2018-01-20 22:21:45 +01:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < containers->length; ++i) {
|
|
|
|
config->handler_context.current_container = containers->items[i];
|
|
|
|
struct cmd_results *res = handler->handle(argc-1, argv+1);
|
|
|
|
if (res->status != CMD_SUCCESS) {
|
|
|
|
free_argv(argc, argv);
|
|
|
|
if (results) {
|
|
|
|
free_cmd_results(results);
|
|
|
|
}
|
|
|
|
results = res;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
free_cmd_results(res);
|
2017-11-23 03:39:35 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-23 03:20:41 +01:00
|
|
|
free_argv(argc, argv);
|
|
|
|
} while(cmdlist);
|
|
|
|
} while(head);
|
|
|
|
cleanup:
|
|
|
|
free(exec);
|
|
|
|
if (!results) {
|
|
|
|
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2018-02-24 18:50:24 +01:00
|
|
|
// this is like execute_command above, except:
|
2017-12-05 10:40:55 +01:00
|
|
|
// 1) it ignores empty commands (empty lines)
|
|
|
|
// 2) it does variable substitution
|
|
|
|
// 3) it doesn't split commands (because the multiple commands are supposed to
|
|
|
|
// be chained together)
|
2018-02-24 18:50:24 +01:00
|
|
|
// 4) execute_command handles all state internally while config_command has
|
|
|
|
// some state handled outside (notably the block mode, in read_config)
|
2017-12-05 10:40:55 +01:00
|
|
|
struct cmd_results *config_command(char *exec, enum cmd_status block) {
|
|
|
|
struct cmd_results *results = NULL;
|
|
|
|
int argc;
|
|
|
|
char **argv = split_args(exec, &argc);
|
|
|
|
if (!argc) {
|
|
|
|
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-01-05 22:32:51 +01:00
|
|
|
wlr_log(L_INFO, "handling config command '%s'", exec);
|
2017-12-05 10:40:55 +01:00
|
|
|
// Endblock
|
|
|
|
if (**argv == '}') {
|
|
|
|
results = cmd_results_new(CMD_BLOCK_END, NULL, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
struct cmd_handler *handler = find_handler(argv[0], block);
|
|
|
|
if (!handler) {
|
|
|
|
char *input = argv[0] ? argv[0] : "(empty)";
|
|
|
|
results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
// Var replacement, for all but first argument of set
|
|
|
|
// TODO commands
|
2017-12-29 15:31:04 +01:00
|
|
|
for (i = handler->handle == cmd_set ? 2 : 1; i < argc; ++i) {
|
2017-12-05 10:40:55 +01:00
|
|
|
argv[i] = do_var_replacement(argv[i]);
|
|
|
|
unescape_string(argv[i]);
|
|
|
|
}
|
2017-12-11 10:17:14 +01:00
|
|
|
// Strip quotes for first argument.
|
|
|
|
// TODO This part needs to be handled much better
|
2017-12-05 10:40:55 +01:00
|
|
|
if (argc>1 && (*argv[1] == '\"' || *argv[1] == '\'')) {
|
|
|
|
strip_quotes(argv[1]);
|
|
|
|
}
|
|
|
|
if (handler->handle) {
|
|
|
|
results = handler->handle(argc-1, argv+1);
|
|
|
|
} else {
|
|
|
|
results = cmd_results_new(CMD_INVALID, argv[0], "This command is shimmed, but unimplemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free_argv(argc, argv);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cmd_results *config_commands_command(char *exec) {
|
|
|
|
struct cmd_results *results = NULL;
|
|
|
|
int argc;
|
|
|
|
char **argv = split_args(exec, &argc);
|
|
|
|
if (!argc) {
|
|
|
|
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find handler for the command this is setting a policy for
|
|
|
|
char *cmd = argv[0];
|
|
|
|
|
|
|
|
if (strcmp(cmd, "}") == 0) {
|
|
|
|
results = cmd_results_new(CMD_BLOCK_END, NULL, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cmd_handler *handler = find_handler(cmd, CMD_BLOCK_END);
|
|
|
|
if (!handler && strcmp(cmd, "*") != 0) {
|
|
|
|
char *input = cmd ? cmd : "(empty)";
|
|
|
|
results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum command_context context = 0;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
char *name;
|
|
|
|
enum command_context context;
|
|
|
|
} context_names[] = {
|
|
|
|
{ "config", CONTEXT_CONFIG },
|
|
|
|
{ "binding", CONTEXT_BINDING },
|
|
|
|
{ "ipc", CONTEXT_IPC },
|
|
|
|
{ "criteria", CONTEXT_CRITERIA },
|
|
|
|
{ "all", CONTEXT_ALL },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
size_t j;
|
|
|
|
for (j = 0; j < sizeof(context_names) / sizeof(context_names[0]); ++j) {
|
|
|
|
if (strcmp(context_names[j].name, argv[i]) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j == sizeof(context_names) / sizeof(context_names[0])) {
|
|
|
|
results = cmd_results_new(CMD_INVALID, cmd,
|
|
|
|
"Invalid command context %s", argv[i]);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
context |= context_names[j].context;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct command_policy *policy = NULL;
|
|
|
|
for (int i = 0; i < config->command_policies->length; ++i) {
|
|
|
|
struct command_policy *p = config->command_policies->items[i];
|
|
|
|
if (strcmp(p->command, cmd) == 0) {
|
|
|
|
policy = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!policy) {
|
|
|
|
policy = alloc_command_policy(cmd);
|
|
|
|
sway_assert(policy, "Unable to allocate security policy");
|
|
|
|
if (policy) {
|
|
|
|
list_add(config->command_policies, policy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
policy->context = context;
|
|
|
|
|
2018-01-05 22:32:51 +01:00
|
|
|
wlr_log(L_INFO, "Set command policy for %s to %d",
|
2017-12-05 10:40:55 +01:00
|
|
|
policy->command, policy->context);
|
|
|
|
|
|
|
|
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free_argv(argc, argv);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2017-11-23 03:20:41 +01:00
|
|
|
struct cmd_results *cmd_results_new(enum cmd_status status,
|
|
|
|
const char *input, const char *format, ...) {
|
|
|
|
struct cmd_results *results = malloc(sizeof(struct cmd_results));
|
|
|
|
if (!results) {
|
2018-01-05 22:32:51 +01:00
|
|
|
wlr_log(L_ERROR, "Unable to allocate command results");
|
2017-11-23 03:20:41 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
results->status = status;
|
|
|
|
if (input) {
|
|
|
|
results->input = strdup(input); // input is the command name
|
|
|
|
} else {
|
|
|
|
results->input = NULL;
|
|
|
|
}
|
|
|
|
if (format) {
|
|
|
|
char *error = malloc(256);
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
if (error) {
|
|
|
|
vsnprintf(error, 256, format, args);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
results->error = error;
|
|
|
|
} else {
|
|
|
|
results->error = NULL;
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_cmd_results(struct cmd_results *results) {
|
|
|
|
if (results->input) {
|
|
|
|
free(results->input);
|
|
|
|
}
|
|
|
|
if (results->error) {
|
|
|
|
free(results->error);
|
|
|
|
}
|
|
|
|
free(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *cmd_results_to_json(struct cmd_results *results) {
|
|
|
|
json_object *result_array = json_object_new_array();
|
|
|
|
json_object *root = json_object_new_object();
|
|
|
|
json_object_object_add(root, "success",
|
|
|
|
json_object_new_boolean(results->status == CMD_SUCCESS));
|
|
|
|
if (results->input) {
|
|
|
|
json_object_object_add(
|
|
|
|
root, "input", json_object_new_string(results->input));
|
|
|
|
}
|
|
|
|
if (results->error) {
|
|
|
|
json_object_object_add(
|
|
|
|
root, "error", json_object_new_string(results->error));
|
|
|
|
}
|
|
|
|
json_object_array_add(result_array, root);
|
|
|
|
const char *json = json_object_to_json_string(result_array);
|
|
|
|
free(result_array);
|
|
|
|
free(root);
|
|
|
|
return json;
|
|
|
|
}
|