2015-08-06 12:40:38 +10:00
|
|
|
#include <xkbcommon/xkbcommon.h>
|
|
|
|
#include <xkbcommon/xkbcommon-names.h>
|
2015-08-09 09:24:18 +10:00
|
|
|
#include <wlc/wlc.h>
|
2016-04-24 09:47:57 +10:00
|
|
|
#include <wlc/wlc-render.h>
|
2015-08-06 12:10:56 +10:00
|
|
|
#include <stdio.h>
|
2015-08-06 07:30:40 +10:00
|
|
|
#include <stdlib.h>
|
2015-08-19 07:53:57 +10:00
|
|
|
#include <errno.h>
|
2015-08-06 07:30:40 +10:00
|
|
|
#include <string.h>
|
2015-09-18 21:27:35 +10:00
|
|
|
#include <strings.h>
|
2015-08-10 02:26:32 +10:00
|
|
|
#include <unistd.h>
|
2015-08-06 07:30:40 +10:00
|
|
|
#include <ctype.h>
|
2015-11-20 10:25:15 +11:00
|
|
|
#include <wordexp.h>
|
2016-05-08 17:20:28 +10:00
|
|
|
#include <libgen.h>
|
2015-09-18 21:16:20 +10:00
|
|
|
#include <sys/types.h>
|
2015-10-09 02:12:31 +11:00
|
|
|
#include <sys/wait.h>
|
2016-01-17 21:53:37 +11:00
|
|
|
#include <limits.h>
|
|
|
|
#include <float.h>
|
|
|
|
#include <libinput.h>
|
2016-09-01 22:18:37 +10:00
|
|
|
#include "sway/layout.h"
|
|
|
|
#include "sway/focus.h"
|
|
|
|
#include "sway/workspace.h"
|
|
|
|
#include "sway/commands.h"
|
|
|
|
#include "sway/container.h"
|
|
|
|
#include "sway/output.h"
|
|
|
|
#include "sway/handlers.h"
|
|
|
|
#include "sway/input_state.h"
|
|
|
|
#include "sway/criteria.h"
|
|
|
|
#include "sway/ipc-server.h"
|
2016-12-03 00:10:03 +11:00
|
|
|
#include "sway/security.h"
|
2016-09-01 22:18:37 +10:00
|
|
|
#include "sway/input.h"
|
|
|
|
#include "sway/border.h"
|
2015-08-06 07:30:40 +10:00
|
|
|
#include "stringop.h"
|
2015-08-20 23:12:34 +10:00
|
|
|
#include "sway.h"
|
2016-09-01 22:18:37 +10:00
|
|
|
#include "util.h"
|
2016-01-17 21:53:37 +11:00
|
|
|
#include "list.h"
|
2016-09-01 22:18:37 +10:00
|
|
|
#include "log.h"
|
2015-08-06 07:30:40 +10:00
|
|
|
|
2016-09-02 12:39:08 +10:00
|
|
|
struct cmd_handler {
|
|
|
|
char *command;
|
|
|
|
sway_cmd *handle;
|
|
|
|
};
|
2015-12-15 10:38:26 +11:00
|
|
|
|
2016-09-02 12:39:08 +10:00
|
|
|
int sp_index = 0;
|
2015-12-15 10:38:26 +11:00
|
|
|
|
2016-09-02 12:39:08 +10:00
|
|
|
// Returns error object, or NULL if check succeeds.
|
|
|
|
struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) {
|
2016-04-25 02:57:35 +10:00
|
|
|
struct cmd_results *error = NULL;
|
2016-09-02 12:39:08 +10:00
|
|
|
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;
|
2016-04-25 02:57:35 +10:00
|
|
|
}
|
2016-09-02 12:39:08 +10:00
|
|
|
return error;
|
2016-04-25 02:57:35 +10:00
|
|
|
}
|
|
|
|
|
2016-09-02 12:39:08 +10:00
|
|
|
void hide_view_in_scratchpad(swayc_t *sp_view) {
|
|
|
|
if (sp_view == NULL) {
|
|
|
|
return;
|
2015-12-15 00:56:28 +11:00
|
|
|
}
|
|
|
|
|
2016-09-02 12:39:08 +10:00
|
|
|
wlc_view_set_mask(sp_view->handle, 0);
|
|
|
|
sp_view->visible = false;
|
|
|
|
swayc_t *ws = sp_view->parent;
|
|
|
|
remove_child(sp_view);
|
|
|
|
if (swayc_active_workspace() != ws && ws->floating->length == 0 && ws->children->length == 0) {
|
|
|
|
destroy_workspace(ws);
|
2015-12-15 00:56:28 +11:00
|
|
|
} else {
|
2016-09-02 12:39:08 +10:00
|
|
|
arrange_windows(ws, -1, -1);
|
2015-12-19 09:49:50 +11:00
|
|
|
}
|
2016-09-02 12:39:08 +10:00
|
|
|
set_focused_container(container_under_pointer());
|
2015-12-15 00:52:34 +11:00
|
|
|
}
|
|
|
|
|
2016-09-02 12:39:08 +10:00
|
|
|
void input_cmd_apply(struct input_config *input) {
|
|
|
|
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);
|
2015-12-14 10:00:11 +11:00
|
|
|
}
|
|
|
|
|
2016-09-02 12:39:08 +10:00
|
|
|
current_input_config = input;
|
2015-12-14 10:00:11 +11:00
|
|
|
|
2016-09-02 12:39:08 +10:00
|
|
|
if (input->identifier) {
|
|
|
|
// Try to find the input device and apply configuration now. If
|
|
|
|
// this is during startup then there will be no container and config
|
|
|
|
// will be applied during normal "new input" event from wlc.
|
|
|
|
struct libinput_device *device = NULL;
|
|
|
|
for (int i = 0; i < input_devices->length; ++i) {
|
|
|
|
device = input_devices->items[i];
|
|
|
|
char* dev_identifier = libinput_dev_unique_id(device);
|
2016-12-16 10:03:59 +11:00
|
|
|
if (!dev_identifier) {
|
|
|
|
break;
|
|
|
|
}
|
2016-09-02 12:39:08 +10:00
|
|
|
int match = dev_identifier && strcmp(dev_identifier, input->identifier) == 0;
|
|
|
|
free(dev_identifier);
|
|
|
|
if (match) {
|
|
|
|
apply_input_config(input, device);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 10:00:11 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 12:39:08 +10:00
|
|
|
void remove_view_from_scratchpad(swayc_t *view) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < scratchpad->length; i++) {
|
|
|
|
if (scratchpad->items[i] == view) {
|
|
|
|
if (sp_index == 0) {
|
|
|
|
sp_index = scratchpad->length - 1;
|
|
|
|
} else {
|
|
|
|
sp_index--;
|
|
|
|
}
|
|
|
|
list_del(scratchpad, sp_index);
|
|
|
|
sp_view = NULL;
|
|
|
|
}
|
2016-07-18 01:26:38 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 12:39:08 +10:00
|
|
|
/* Keep alphabetized */
|
|
|
|
static struct cmd_handler handlers[] = {
|
|
|
|
{ "assign", cmd_assign },
|
|
|
|
{ "bar", cmd_bar },
|
|
|
|
{ "bindcode", cmd_bindcode },
|
|
|
|
{ "bindsym", cmd_bindsym },
|
|
|
|
{ "border", cmd_border },
|
|
|
|
{ "client.background", cmd_client_background },
|
|
|
|
{ "client.focused", cmd_client_focused },
|
|
|
|
{ "client.focused_inactive", cmd_client_focused_inactive },
|
|
|
|
{ "client.placeholder", cmd_client_placeholder },
|
|
|
|
{ "client.unfocused", cmd_client_unfocused },
|
|
|
|
{ "client.urgent", cmd_client_urgent },
|
2016-12-03 00:10:03 +11:00
|
|
|
{ "commands", cmd_commands },
|
2016-09-02 12:39:08 +10:00
|
|
|
{ "debuglog", cmd_debuglog },
|
|
|
|
{ "default_orientation", cmd_orientation },
|
|
|
|
{ "exec", cmd_exec },
|
|
|
|
{ "exec_always", cmd_exec_always },
|
|
|
|
{ "exit", cmd_exit },
|
|
|
|
{ "floating", cmd_floating },
|
|
|
|
{ "floating_maximum_size", cmd_floating_maximum_size },
|
|
|
|
{ "floating_minimum_size", cmd_floating_minimum_size },
|
|
|
|
{ "floating_modifier", cmd_floating_mod },
|
|
|
|
{ "floating_scroll", cmd_floating_scroll },
|
|
|
|
{ "focus", cmd_focus },
|
|
|
|
{ "focus_follows_mouse", cmd_focus_follows_mouse },
|
|
|
|
{ "font", cmd_font },
|
|
|
|
{ "for_window", cmd_for_window },
|
2016-10-08 20:12:45 +11:00
|
|
|
{ "force_focus_wrapping", cmd_force_focus_wrapping },
|
2016-09-02 12:39:08 +10:00
|
|
|
{ "fullscreen", cmd_fullscreen },
|
|
|
|
{ "gaps", cmd_gaps },
|
|
|
|
{ "hide_edge_borders", cmd_hide_edge_borders },
|
|
|
|
{ "include", cmd_include },
|
|
|
|
{ "input", cmd_input },
|
2016-12-03 09:34:26 +11:00
|
|
|
{ "ipc", cmd_ipc },
|
2016-09-02 12:39:08 +10:00
|
|
|
{ "kill", cmd_kill },
|
|
|
|
{ "layout", cmd_layout },
|
|
|
|
{ "log_colors", cmd_log_colors },
|
|
|
|
{ "mode", cmd_mode },
|
|
|
|
{ "mouse_warping", cmd_mouse_warping },
|
|
|
|
{ "move", cmd_move },
|
|
|
|
{ "new_float", cmd_new_float },
|
|
|
|
{ "new_window", cmd_new_window },
|
|
|
|
{ "output", cmd_output },
|
2016-12-02 13:36:43 +11:00
|
|
|
{ "permit", cmd_permit },
|
|
|
|
{ "reject", cmd_reject },
|
2016-09-02 12:39:08 +10:00
|
|
|
{ "reload", cmd_reload },
|
|
|
|
{ "resize", cmd_resize },
|
|
|
|
{ "scratchpad", cmd_scratchpad },
|
|
|
|
{ "seamless_mouse", cmd_seamless_mouse },
|
|
|
|
{ "set", cmd_set },
|
|
|
|
{ "smart_gaps", cmd_smart_gaps },
|
|
|
|
{ "split", cmd_split },
|
|
|
|
{ "splith", cmd_splith },
|
|
|
|
{ "splitt", cmd_splitt },
|
|
|
|
{ "splitv", cmd_splitv },
|
|
|
|
{ "sticky", cmd_sticky },
|
|
|
|
{ "workspace", cmd_workspace },
|
|
|
|
{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth },
|
|
|
|
{ "workspace_layout", cmd_workspace_layout },
|
|
|
|
};
|
2015-12-14 09:33:54 +11:00
|
|
|
|
2015-11-30 09:20:27 +11:00
|
|
|
static struct cmd_handler bar_handlers[] = {
|
2015-12-16 09:38:17 +11:00
|
|
|
{ "binding_mode_indicator", bar_cmd_binding_mode_indicator },
|
2015-12-15 09:43:52 +11:00
|
|
|
{ "bindsym", bar_cmd_bindsym },
|
2015-12-15 23:12:08 +11:00
|
|
|
{ "colors", bar_cmd_colors },
|
2015-12-16 09:24:37 +11:00
|
|
|
{ "font", bar_cmd_font },
|
2015-12-16 00:05:42 +11:00
|
|
|
{ "height", bar_cmd_height },
|
2015-12-15 05:52:25 +11:00
|
|
|
{ "hidden_state", bar_cmd_hidden_state },
|
2015-12-15 04:29:26 +11:00
|
|
|
{ "id", bar_cmd_id },
|
2015-12-15 06:15:58 +11:00
|
|
|
{ "mode", bar_cmd_mode },
|
2015-12-15 11:01:01 +11:00
|
|
|
{ "modifier", bar_cmd_modifier },
|
2015-12-16 07:55:20 +11:00
|
|
|
{ "output", bar_cmd_output },
|
2016-04-25 03:35:21 +10:00
|
|
|
{ "pango_markup", bar_cmd_pango_markup },
|
2015-12-15 01:12:05 +11:00
|
|
|
{ "position", bar_cmd_position },
|
2015-12-16 22:58:45 +11:00
|
|
|
{ "separator_symbol", bar_cmd_separator_symbol },
|
2015-12-15 10:38:26 +11:00
|
|
|
{ "status_command", bar_cmd_status_command },
|
2015-12-15 00:56:28 +11:00
|
|
|
{ "strip_workspace_numbers", bar_cmd_strip_workspace_numbers },
|
2015-12-19 09:49:50 +11:00
|
|
|
{ "swaybar_command", bar_cmd_swaybar_command },
|
2015-12-15 00:52:34 +11:00
|
|
|
{ "tray_output", bar_cmd_tray_output },
|
2015-12-14 10:00:11 +11:00
|
|
|
{ "tray_padding", bar_cmd_tray_padding },
|
2015-12-14 09:33:54 +11:00
|
|
|
{ "workspace_buttons", bar_cmd_workspace_buttons },
|
2016-10-08 20:12:45 +11:00
|
|
|
{ "wrap_scroll", bar_cmd_wrap_scroll },
|
2015-11-30 09:20:27 +11:00
|
|
|
};
|
|
|
|
|
2015-12-15 23:12:08 +11:00
|
|
|
/**
|
|
|
|
* Check and add color to buffer.
|
|
|
|
*
|
|
|
|
* return error object, or NULL if color is valid.
|
|
|
|
*/
|
2016-09-02 12:39:08 +10:00
|
|
|
struct cmd_results *add_color(const char *name, char *buffer, const char *color) {
|
2015-12-15 23:45:04 +11:00
|
|
|
int len = strlen(color);
|
|
|
|
if (len != 7 && len != 9 ) {
|
2015-12-15 23:12:08 +11:00
|
|
|
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;
|
2015-12-15 23:45:04 +11:00
|
|
|
for (i = 1; i < len; ++i) {
|
2015-12-15 23:12:08 +11:00
|
|
|
if (!isxdigit(color[i])) {
|
|
|
|
return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy color to buffer
|
2015-12-15 23:45:04 +11:00
|
|
|
strncpy(buffer, color, len);
|
|
|
|
// add default alpha channel if color was defined without it
|
|
|
|
if (len == 7) {
|
|
|
|
buffer[7] = 'f';
|
|
|
|
buffer[8] = 'f';
|
|
|
|
}
|
2016-11-03 07:07:04 +11:00
|
|
|
buffer[9] = '\0';
|
2015-12-15 23:12:08 +11:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:53:37 +11:00
|
|
|
static struct cmd_handler input_handlers[] = {
|
2016-05-01 20:02:44 +10:00
|
|
|
{ "accel_profile", input_cmd_accel_profile },
|
2016-01-17 21:53:37 +11:00
|
|
|
{ "click_method", input_cmd_click_method },
|
|
|
|
{ "drag_lock", input_cmd_drag_lock },
|
|
|
|
{ "dwt", input_cmd_dwt },
|
|
|
|
{ "events", input_cmd_events },
|
2016-10-26 07:03:58 +11:00
|
|
|
{ "left_handed", input_cmd_left_handed },
|
2016-01-17 21:53:37 +11:00
|
|
|
{ "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 },
|
|
|
|
};
|
|
|
|
|
2015-12-15 23:12:08 +11:00
|
|
|
static struct cmd_handler bar_colors_handlers[] = {
|
|
|
|
{ "active_workspace", bar_colors_cmd_active_workspace },
|
|
|
|
{ "background", bar_colors_cmd_background },
|
|
|
|
{ "binding_mode", bar_colors_cmd_binding_mode },
|
2016-11-03 04:48:43 +11:00
|
|
|
{ "focused_background", bar_colors_cmd_focused_background },
|
|
|
|
{ "focused_separator", bar_colors_cmd_focused_separator },
|
|
|
|
{ "focused_statusline", bar_colors_cmd_focused_statusline },
|
2015-12-15 23:12:08 +11:00
|
|
|
{ "focused_workspace", bar_colors_cmd_focused_workspace },
|
|
|
|
{ "inactive_workspace", bar_colors_cmd_inactive_workspace },
|
|
|
|
{ "separator", bar_colors_cmd_separator },
|
|
|
|
{ "statusline", bar_colors_cmd_statusline },
|
|
|
|
{ "urgent_workspace", bar_colors_cmd_urgent_workspace },
|
|
|
|
};
|
|
|
|
|
2016-12-03 09:34:26 +11:00
|
|
|
static struct cmd_handler ipc_handlers[] = {
|
2017-02-20 22:48:33 +11:00
|
|
|
{ "*", cmd_ipc_cmd },
|
2016-12-03 09:34:26 +11:00
|
|
|
{ "bar-config", cmd_ipc_cmd },
|
|
|
|
{ "command", cmd_ipc_cmd },
|
|
|
|
{ "events", cmd_ipc_events },
|
|
|
|
{ "inputs", cmd_ipc_cmd },
|
|
|
|
{ "marks", cmd_ipc_cmd },
|
|
|
|
{ "outputs", cmd_ipc_cmd },
|
|
|
|
{ "tree", cmd_ipc_cmd },
|
|
|
|
{ "workspaces", cmd_ipc_cmd },
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct cmd_handler ipc_event_handlers[] = {
|
2017-02-20 22:48:33 +11:00
|
|
|
{ "*", cmd_ipc_event_cmd },
|
2016-12-03 09:34:26 +11:00
|
|
|
{ "binding", cmd_ipc_event_cmd },
|
|
|
|
{ "input", cmd_ipc_event_cmd },
|
|
|
|
{ "mode", cmd_ipc_event_cmd },
|
|
|
|
{ "output", cmd_ipc_event_cmd },
|
|
|
|
{ "window", cmd_ipc_event_cmd },
|
|
|
|
{ "workspace", cmd_ipc_event_cmd },
|
|
|
|
};
|
|
|
|
|
2015-08-13 14:51:38 +10:00
|
|
|
static int handler_compare(const void *_a, const void *_b) {
|
2015-08-06 07:30:40 +10:00
|
|
|
const struct cmd_handler *a = _a;
|
|
|
|
const struct cmd_handler *b = _b;
|
|
|
|
return strcasecmp(a->command, b->command);
|
|
|
|
}
|
|
|
|
|
2015-11-30 09:20:27 +11:00
|
|
|
static struct cmd_handler *find_handler(char *line, enum cmd_status block) {
|
2015-08-06 07:30:40 +10:00
|
|
|
struct cmd_handler d = { .command=line };
|
2015-12-14 12:39:24 +11:00
|
|
|
struct cmd_handler *res = NULL;
|
2016-01-17 21:53:37 +11:00
|
|
|
sway_log(L_DEBUG, "find_handler(%s) %d", line, block == CMD_BLOCK_INPUT);
|
2015-12-14 12:39:24 +11:00
|
|
|
if (block == CMD_BLOCK_BAR) {
|
|
|
|
res = bsearch(&d, bar_handlers,
|
|
|
|
sizeof(bar_handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
2015-12-15 23:12:08 +11:00
|
|
|
} else if (block == CMD_BLOCK_BAR_COLORS){
|
|
|
|
res = bsearch(&d, bar_colors_handlers,
|
|
|
|
sizeof(bar_colors_handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
2016-01-17 21:53:37 +11:00
|
|
|
} else if (block == CMD_BLOCK_INPUT) {
|
|
|
|
res = bsearch(&d, input_handlers,
|
|
|
|
sizeof(input_handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
2016-12-03 09:34:26 +11:00
|
|
|
} else if (block == CMD_BLOCK_IPC) {
|
|
|
|
res = bsearch(&d, ipc_handlers,
|
|
|
|
sizeof(ipc_handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
|
|
|
} else if (block == CMD_BLOCK_IPC_EVENTS) {
|
|
|
|
res = bsearch(&d, ipc_event_handlers,
|
|
|
|
sizeof(ipc_event_handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
2015-12-14 12:39:24 +11:00
|
|
|
} else {
|
|
|
|
res = bsearch(&d, handlers,
|
2015-09-05 09:57:03 +10:00
|
|
|
sizeof(handlers) / sizeof(struct cmd_handler),
|
|
|
|
sizeof(struct cmd_handler), handler_compare);
|
2015-12-14 12:39:24 +11:00
|
|
|
}
|
2015-08-06 07:30:40 +10:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-12-03 00:17:45 +11:00
|
|
|
struct cmd_results *handle_command(char *_exec, enum command_context context) {
|
2015-10-22 23:14:13 +11: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
|
|
|
|
// error string we could e.g. concatonate all errors there.)
|
|
|
|
struct cmd_results *results = NULL;
|
2015-09-15 12:59:25 +10:00
|
|
|
char *exec = strdup(_exec);
|
|
|
|
char *head = exec;
|
|
|
|
char *cmdlist;
|
|
|
|
char *cmd;
|
|
|
|
char *criteria __attribute__((unused));
|
|
|
|
|
|
|
|
head = exec;
|
|
|
|
do {
|
2015-11-19 05:03:24 +11:00
|
|
|
// Extract criteria (valid for this command list only).
|
|
|
|
criteria = NULL;
|
2015-09-15 12:59:25 +10:00
|
|
|
if (*head == '[') {
|
2015-11-19 05:03:24 +11:00
|
|
|
++head;
|
2015-09-15 12:59:25 +10:00
|
|
|
criteria = argsep(&head, "]");
|
|
|
|
if (head) {
|
|
|
|
++head;
|
|
|
|
// TODO handle criteria
|
|
|
|
} else {
|
2015-11-19 05:03:24 +11:00
|
|
|
if (!results) {
|
|
|
|
results = cmd_results_new(CMD_INVALID, criteria, "Unmatched [");
|
|
|
|
}
|
|
|
|
goto cleanup;
|
2015-09-15 12:59:25 +10:00
|
|
|
}
|
|
|
|
// Skip leading whitespace
|
|
|
|
head += strspn(head, whitespace);
|
2015-11-19 05:03:24 +11:00
|
|
|
|
|
|
|
// TODO: it will yield unexpected results to execute commands
|
|
|
|
// (on any view) that where meant for certain views only.
|
|
|
|
if (!results) {
|
|
|
|
int len = strlen(criteria) + strlen(head) + 4;
|
|
|
|
char *tmp = malloc(len);
|
2016-12-16 09:39:09 +11:00
|
|
|
if (tmp) {
|
|
|
|
snprintf(tmp, len, "[%s] %s", criteria, head);
|
|
|
|
} else {
|
|
|
|
sway_log(L_DEBUG, "Unable to allocate criteria string for cmd result");
|
|
|
|
}
|
2015-11-19 05:03:24 +11:00
|
|
|
results = cmd_results_new(CMD_INVALID, tmp,
|
|
|
|
"Can't handle criteria string: Refusing to execute command");
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
goto cleanup;
|
2015-09-15 12:59:25 +10:00
|
|
|
}
|
|
|
|
// Split command list
|
|
|
|
cmdlist = argsep(&head, ";");
|
|
|
|
cmdlist += strspn(cmdlist, whitespace);
|
|
|
|
do {
|
|
|
|
// Split commands
|
|
|
|
cmd = argsep(&cmdlist, ",");
|
|
|
|
cmd += strspn(cmd, whitespace);
|
2015-10-20 04:42:49 +11:00
|
|
|
if (strcmp(cmd, "") == 0) {
|
|
|
|
sway_log(L_INFO, "Ignoring empty command.");
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-15 12:59:25 +10:00
|
|
|
sway_log(L_INFO, "Handling command '%s'", cmd);
|
|
|
|
//TODO better handling of argv
|
|
|
|
int argc;
|
|
|
|
char **argv = split_args(cmd, &argc);
|
2016-03-18 19:34:45 +11:00
|
|
|
if (strcmp(argv[0], "exec") != 0) {
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (*argv[i] == '\"' || *argv[i] == '\'') {
|
|
|
|
strip_quotes(argv[i]);
|
|
|
|
}
|
2016-01-11 13:39:34 +11:00
|
|
|
}
|
2015-09-15 12:59:25 +10:00
|
|
|
}
|
2015-11-30 09:20:27 +11:00
|
|
|
struct cmd_handler *handler = find_handler(argv[0], CMD_BLOCK_END);
|
2015-10-22 23:14:13 +11:00
|
|
|
if (!handler) {
|
|
|
|
if (results) {
|
|
|
|
free_cmd_results(results);
|
|
|
|
}
|
2015-10-23 22:03:14 +11:00
|
|
|
results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command");
|
2015-10-22 23:14:13 +11:00
|
|
|
free_argv(argc, argv);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-12-03 00:17:45 +11:00
|
|
|
if (!(get_command_policy(argv[0]) & context)) {
|
|
|
|
if (results) {
|
|
|
|
free_cmd_results(results);
|
|
|
|
}
|
|
|
|
results = cmd_results_new(CMD_INVALID, cmd,
|
|
|
|
"Permission denied for %s via %s", cmd,
|
|
|
|
command_policy_str(context));
|
|
|
|
free_argv(argc, argv);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2015-10-22 23:14:13 +11:00
|
|
|
struct cmd_results *res = handler->handle(argc-1, argv+1);
|
|
|
|
if (res->status != CMD_SUCCESS) {
|
2015-09-15 12:59:25 +10:00
|
|
|
free_argv(argc, argv);
|
2015-10-22 23:14:13 +11:00
|
|
|
if (results) {
|
|
|
|
free_cmd_results(results);
|
|
|
|
}
|
|
|
|
results = res;
|
2015-09-15 12:59:25 +10:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
free_argv(argc, argv);
|
2015-10-22 23:14:13 +11:00
|
|
|
free_cmd_results(res);
|
2015-09-15 12:59:25 +10:00
|
|
|
} while(cmdlist);
|
|
|
|
} while(head);
|
|
|
|
cleanup:
|
|
|
|
free(exec);
|
2015-10-22 23:14:13 +11:00
|
|
|
if (!results) {
|
|
|
|
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
}
|
|
|
|
return results;
|
2015-08-06 07:30:40 +10:00
|
|
|
}
|
2015-09-09 03:27:09 +10:00
|
|
|
|
2015-11-18 09:30:45 +11:00
|
|
|
// this is like handle_command above, except:
|
|
|
|
// 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)
|
|
|
|
// 4) handle_command handles all state internally while config_command has some
|
|
|
|
// state handled outside (notably the block mode, in read_config)
|
2015-11-30 09:20:27 +11:00
|
|
|
struct cmd_results *config_command(char *exec, enum cmd_status block) {
|
2015-10-22 23:14:13 +11:00
|
|
|
struct cmd_results *results = NULL;
|
2015-09-09 03:27:09 +10:00
|
|
|
int argc;
|
|
|
|
char **argv = split_args(exec, &argc);
|
2015-10-22 23:14:13 +11:00
|
|
|
if (!argc) {
|
|
|
|
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2015-09-19 01:33:28 +10:00
|
|
|
|
|
|
|
sway_log(L_INFO, "handling config command '%s'", exec);
|
2015-09-15 12:59:25 +10:00
|
|
|
// Endblock
|
|
|
|
if (**argv == '}') {
|
2015-10-22 23:14:13 +11:00
|
|
|
results = cmd_results_new(CMD_BLOCK_END, NULL, NULL);
|
2015-09-09 03:27:09 +10:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2015-11-30 09:20:27 +11:00
|
|
|
struct cmd_handler *handler = find_handler(argv[0], block);
|
2015-09-15 12:59:25 +10:00
|
|
|
if (!handler) {
|
2015-10-23 22:03:14 +11:00
|
|
|
char *input = argv[0] ? argv[0] : "(empty)";
|
2015-10-22 23:14:13 +11:00
|
|
|
results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");
|
2015-09-15 12:59:25 +10:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
// Var replacement, for all but first argument of set
|
|
|
|
for (i = handler->handle == cmd_set ? 2 : 1; i < argc; ++i) {
|
|
|
|
argv[i] = do_var_replacement(argv[i]);
|
2016-01-09 09:16:57 +11:00
|
|
|
unescape_string(argv[i]);
|
2015-09-15 12:59:25 +10:00
|
|
|
}
|
|
|
|
/* Strip quotes for first argument.
|
|
|
|
* TODO This part needs to be handled much better */
|
|
|
|
if (argc>1 && (*argv[1] == '\"' || *argv[1] == '\'')) {
|
|
|
|
strip_quotes(argv[1]);
|
2015-09-09 03:27:09 +10:00
|
|
|
}
|
2015-12-15 03:11:29 +11:00
|
|
|
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");
|
|
|
|
}
|
2016-12-03 00:10:03 +11:00
|
|
|
|
|
|
|
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);
|
2016-12-04 04:38:42 +11:00
|
|
|
if (!handler && strcmp(cmd, "*") != 0) {
|
2016-12-03 00:10:03 +11:00
|
|
|
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;
|
2016-12-03 10:08:15 +11:00
|
|
|
for (j = 0; j < sizeof(context_names) / sizeof(context_names[0]); ++j) {
|
2016-12-03 00:10:03 +11:00
|
|
|
if (strcmp(context_names[j].name, argv[i]) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-12-03 10:08:15 +11:00
|
|
|
if (j == sizeof(context_names) / sizeof(context_names[0])) {
|
2016-12-03 00:10:03 +11:00
|
|
|
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);
|
2016-12-16 10:10:29 +11:00
|
|
|
if (!policy) {
|
|
|
|
sway_abort("Unable to allocate security policy");
|
|
|
|
}
|
2016-12-03 00:10:03 +11:00
|
|
|
list_add(config->command_policies, policy);
|
|
|
|
}
|
|
|
|
policy->context = context;
|
|
|
|
|
|
|
|
sway_log(L_INFO, "Set command policy for %s to %d",
|
|
|
|
policy->command, policy->context);
|
|
|
|
|
|
|
|
results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
|
|
|
|
cleanup:
|
2015-09-09 03:27:09 +10:00
|
|
|
free_argv(argc, argv);
|
2015-10-22 23:14:13 +11:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2016-12-16 09:39:09 +11:00
|
|
|
if (!results) {
|
|
|
|
sway_log(L_ERROR, "Unable to allocate command results");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-10-22 23:14:13 +11:00
|
|
|
results->status = status;
|
2015-10-23 22:03:14 +11:00
|
|
|
if (input) {
|
|
|
|
results->input = strdup(input); // input is the command name
|
|
|
|
} else {
|
|
|
|
results->input = NULL;
|
|
|
|
}
|
2015-10-22 23:14:13 +11:00
|
|
|
if (format) {
|
|
|
|
char *error = malloc(256);
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2016-12-16 09:39:09 +11:00
|
|
|
if (error) {
|
|
|
|
vsnprintf(error, 256, format, args);
|
|
|
|
}
|
2015-10-22 23:14:13 +11:00
|
|
|
va_end(args);
|
|
|
|
results->error = error;
|
|
|
|
} else {
|
|
|
|
results->error = NULL;
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_cmd_results(struct cmd_results *results) {
|
2015-10-23 22:03:14 +11:00
|
|
|
if (results->input) {
|
|
|
|
free(results->input);
|
|
|
|
}
|
2015-10-23 21:50:30 +11:00
|
|
|
if (results->error) {
|
2015-10-22 23:14:13 +11:00
|
|
|
free(results->error);
|
2015-10-23 21:50:30 +11:00
|
|
|
}
|
2015-10-22 23:14:13 +11:00
|
|
|
free(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *cmd_results_to_json(struct cmd_results *results) {
|
2016-07-24 13:13:09 +10:00
|
|
|
json_object *result_array = json_object_new_array();
|
2015-10-22 23:14:13 +11:00
|
|
|
json_object *root = json_object_new_object();
|
|
|
|
json_object_object_add(root, "success", json_object_new_boolean(results->status == CMD_SUCCESS));
|
2015-10-23 21:50:30 +11:00
|
|
|
if (results->input) {
|
2015-10-22 23:14:13 +11:00
|
|
|
json_object_object_add(root, "input", json_object_new_string(results->input));
|
2015-10-23 21:50:30 +11:00
|
|
|
}
|
|
|
|
if (results->error) {
|
2015-10-22 23:14:13 +11:00
|
|
|
json_object_object_add(root, "error", json_object_new_string(results->error));
|
2015-10-23 21:50:30 +11:00
|
|
|
}
|
2016-07-24 13:13:09 +10:00
|
|
|
json_object_array_add(result_array, root);
|
|
|
|
const char *json = json_object_to_json_string(result_array);
|
|
|
|
free(result_array);
|
2015-10-22 23:14:13 +11:00
|
|
|
free(root);
|
|
|
|
return json;
|
2015-09-09 03:27:09 +10:00
|
|
|
}
|