sway/sway/config.c

743 lines
19 KiB
C
Raw Normal View History

2017-12-05 09:40:55 +00:00
#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <wordexp.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <signal.h>
#include <libinput.h>
#include <limits.h>
#include <dirent.h>
#include <strings.h>
#ifdef __linux__
#include <linux/input-event-codes.h>
#elif __FreeBSD__
#include <dev/evdev/input-event-codes.h>
#endif
#include <wlr/types/wlr_output.h>
2017-12-12 14:02:30 +00:00
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
2017-12-05 09:40:55 +00:00
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/tree/layout.h"
2018-05-02 13:07:52 +00:00
#include "cairo.h"
#include "pango.h"
2017-12-05 09:40:55 +00:00
#include "readline.h"
#include "stringop.h"
#include "list.h"
#include "log.h"
struct sway_config *config = NULL;
static void free_mode(struct sway_mode *mode) {
int i;
if (!mode) {
return;
}
free(mode->name);
if (mode->keysym_bindings) {
for (i = 0; i < mode->keysym_bindings->length; i++) {
free_sway_binding(mode->keysym_bindings->items[i]);
}
list_free(mode->keysym_bindings);
}
if (mode->keycode_bindings) {
for (i = 0; i < mode->keycode_bindings->length; i++) {
free_sway_binding(mode->keycode_bindings->items[i]);
}
list_free(mode->keycode_bindings);
}
free(mode);
}
2017-12-05 09:40:55 +00:00
void free_config(struct sway_config *config) {
2018-01-20 16:32:07 +00:00
config_clear_handler_context(config);
if (!config) {
return;
}
// TODO: handle all currently unhandled lists as we add implementations
2017-12-29 14:31:04 +00:00
if (config->symbols) {
2018-03-31 04:35:06 +00:00
for (int i = 0; i < config->symbols->length; ++i) {
2017-12-29 14:31:04 +00:00
free_sway_variable(config->symbols->items[i]);
}
list_free(config->symbols);
}
if (config->modes) {
2018-03-31 04:35:06 +00:00
for (int i = 0; i < config->modes->length; ++i) {
free_mode(config->modes->items[i]);
}
list_free(config->modes);
}
2018-03-31 04:35:06 +00:00
if (config->bars) {
for (int i = 0; i < config->bars->length; ++i) {
free_bar_config(config->bars->items[i]);
}
list_free(config->bars);
}
list_free(config->cmd_queue);
list_free(config->workspace_outputs);
list_free(config->pid_workspaces);
list_free(config->output_configs);
if (config->input_configs) {
2018-03-31 04:35:06 +00:00
for (int i = 0; i < config->input_configs->length; i++) {
free_input_config(config->input_configs->items[i]);
}
list_free(config->input_configs);
}
if (config->seat_configs) {
2018-03-31 04:35:06 +00:00
for (int i = 0; i < config->seat_configs->length; i++) {
free_seat_config(config->seat_configs->items[i]);
}
list_free(config->seat_configs);
}
list_free(config->criteria);
list_free(config->no_focus);
list_free(config->active_bar_modifiers);
list_free(config->config_chain);
list_free(config->command_policies);
list_free(config->feature_policies);
list_free(config->ipc_policies);
free(config->current_bar);
free(config->floating_scroll_up_cmd);
free(config->floating_scroll_down_cmd);
free(config->floating_scroll_left_cmd);
free(config->floating_scroll_right_cmd);
free(config->font);
free((char *)config->current_config);
free(config);
2017-12-05 09:40:55 +00:00
}
static void destroy_removed_seats(struct sway_config *old_config,
struct sway_config *new_config) {
struct seat_config *seat_config;
struct sway_seat *seat;
int i;
for (i = 0; i < old_config->seat_configs->length; i++) {
seat_config = old_config->seat_configs->items[i];
/* Also destroy seats that aren't present in new config */
if (new_config && list_seq_find(new_config->seat_configs,
seat_name_cmp, seat_config->name) < 0) {
seat = input_manager_get_seat(input_manager,
seat_config->name);
2018-04-02 12:45:37 +00:00
seat_destroy(seat);
}
}
}
2018-05-02 13:07:52 +00:00
int get_font_text_height(char *font) {
cairo_t *cairo = cairo_create(NULL);
int text_height;
get_text_size(cairo, font, NULL, &text_height, 1, false,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"!@#$%^&*([{|");
cairo_destroy(cairo);
return text_height;
}
2018-05-01 12:48:57 +00:00
static void set_color(float dest[static 4], uint32_t color) {
dest[0] = ((color >> 16) & 0xff) / 255.0;
dest[1] = ((color >> 8) & 0xff) / 255.0;
dest[2] = (color & 0xff) / 255.0;
dest[3] = 1.0;
}
2017-12-05 09:40:55 +00:00
static void config_defaults(struct sway_config *config) {
if (!(config->symbols = create_list())) goto cleanup;
if (!(config->modes = create_list())) goto cleanup;
if (!(config->bars = create_list())) goto cleanup;
if (!(config->workspace_outputs = create_list())) goto cleanup;
if (!(config->pid_workspaces = create_list())) goto cleanup;
if (!(config->criteria = create_list())) goto cleanup;
if (!(config->no_focus = create_list())) goto cleanup;
if (!(config->input_configs = create_list())) goto cleanup;
2017-12-14 16:11:56 +00:00
if (!(config->seat_configs = create_list())) goto cleanup;
2017-12-05 09:40:55 +00:00
if (!(config->output_configs = create_list())) goto cleanup;
if (!(config->cmd_queue = create_list())) goto cleanup;
2017-12-12 14:02:30 +00:00
if (!(config->current_mode = malloc(sizeof(struct sway_mode))))
goto cleanup;
2017-12-05 09:40:55 +00:00
if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup;
strcpy(config->current_mode->name, "default");
2017-12-27 15:08:18 +00:00
if (!(config->current_mode->keysym_bindings = create_list())) goto cleanup;
if (!(config->current_mode->keycode_bindings = create_list())) goto cleanup;
2017-12-05 09:40:55 +00:00
list_add(config->modes, config->current_mode);
config->floating_mod = 0;
config->dragging_key = BTN_LEFT;
config->resizing_key = BTN_RIGHT;
if (!(config->floating_scroll_up_cmd = strdup(""))) goto cleanup;
if (!(config->floating_scroll_down_cmd = strdup(""))) goto cleanup;
if (!(config->floating_scroll_left_cmd = strdup(""))) goto cleanup;
if (!(config->floating_scroll_right_cmd = strdup(""))) goto cleanup;
config->default_layout = L_NONE;
config->default_orientation = L_NONE;
if (!(config->font = strdup("monospace 10"))) goto cleanup;
2018-05-02 13:07:52 +00:00
config->font_height = get_font_text_height(config->font);
2017-12-05 09:40:55 +00:00
// floating view
config->floating_maximum_width = 0;
config->floating_maximum_height = 0;
config->floating_minimum_width = 75;
config->floating_minimum_height = 50;
// Flags
config->focus_follows_mouse = true;
config->mouse_warping = true;
config->reloading = false;
config->active = false;
config->failed = false;
config->auto_back_and_forth = false;
config->reading = false;
config->show_marks = true;
config->edge_gaps = true;
config->smart_gaps = false;
config->gaps_inner = 0;
config->gaps_outer = 0;
if (!(config->active_bar_modifiers = create_list())) goto cleanup;
if (!(config->config_chain = create_list())) goto cleanup;
config->current_config = NULL;
// borders
config->border = B_NORMAL;
config->floating_border = B_NORMAL;
config->border_thickness = 2;
config->floating_border_thickness = 2;
config->hide_edge_borders = E_NONE;
// border colors
2018-05-01 12:48:57 +00:00
set_color(config->border_colors.focused.border, 0x4C7899);
set_color(config->border_colors.focused.border, 0x4C7899);
set_color(config->border_colors.focused.background, 0x285577);
2018-05-02 13:07:52 +00:00
config->border_colors.focused.text = 0xFFFFFFFF;
2018-05-01 12:48:57 +00:00
set_color(config->border_colors.focused.indicator, 0x2E9EF4);
set_color(config->border_colors.focused.child_border, 0x285577);
set_color(config->border_colors.focused_inactive.border, 0x333333);
set_color(config->border_colors.focused_inactive.background, 0x5F676A);
2018-05-02 13:07:52 +00:00
config->border_colors.focused_inactive.text = 0xFFFFFFFF;
2018-05-01 12:48:57 +00:00
set_color(config->border_colors.focused_inactive.indicator, 0x484E50);
set_color(config->border_colors.focused_inactive.child_border, 0x5F676A);
set_color(config->border_colors.unfocused.border, 0x333333);
set_color(config->border_colors.unfocused.background, 0x222222);
2018-05-02 13:07:52 +00:00
config->border_colors.unfocused.text = 0x888888FF;
2018-05-01 12:48:57 +00:00
set_color(config->border_colors.unfocused.indicator, 0x292D2E);
set_color(config->border_colors.unfocused.child_border, 0x222222);
set_color(config->border_colors.urgent.border, 0x2F343A);
set_color(config->border_colors.urgent.background, 0x900000);
2018-05-02 13:07:52 +00:00
config->border_colors.urgent.text = 0xFFFFFFFF;
2018-05-01 12:48:57 +00:00
set_color(config->border_colors.urgent.indicator, 0x900000);
set_color(config->border_colors.urgent.child_border, 0x900000);
set_color(config->border_colors.placeholder.border, 0x000000);
set_color(config->border_colors.placeholder.background, 0x0C0C0C);
2018-05-02 13:07:52 +00:00
config->border_colors.placeholder.text = 0xFFFFFFFF;
2018-05-01 12:48:57 +00:00
set_color(config->border_colors.placeholder.indicator, 0x000000);
set_color(config->border_colors.placeholder.child_border, 0x0C0C0C);
set_color(config->border_colors.background, 0xFFFFFF);
2017-12-05 09:40:55 +00:00
// Security
if (!(config->command_policies = create_list())) goto cleanup;
if (!(config->feature_policies = create_list())) goto cleanup;
if (!(config->ipc_policies = create_list())) goto cleanup;
return;
cleanup:
sway_abort("Unable to allocate config structures");
}
static bool file_exists(const char *path) {
return path && access(path, R_OK) != -1;
}
static char *get_config_path(void) {
static const char *config_paths[] = {
"$HOME/.sway/config",
"$XDG_CONFIG_HOME/sway/config",
"$HOME/.i3/config",
"$XDG_CONFIG_HOME/i3/config",
SYSCONFDIR "/sway/config",
SYSCONFDIR "/i3/config",
};
if (!getenv("XDG_CONFIG_HOME")) {
char *home = getenv("HOME");
char *config_home = malloc(strlen(home) + strlen("/.config") + 1);
if (!config_home) {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Unable to allocate $HOME/.config");
2017-12-05 09:40:55 +00:00
} else {
strcpy(config_home, home);
strcat(config_home, "/.config");
setenv("XDG_CONFIG_HOME", config_home, 1);
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home);
2017-12-05 09:40:55 +00:00
free(config_home);
}
}
wordexp_t p;
char *path;
int i;
for (i = 0; i < (int)(sizeof(config_paths) / sizeof(char *)); ++i) {
if (wordexp(config_paths[i], &p, 0) == 0) {
path = strdup(p.we_wordv[0]);
wordfree(&p);
if (file_exists(path)) {
return path;
}
free(path);
2017-12-05 09:40:55 +00:00
}
}
return NULL; // Not reached
}
const char *current_config_path;
static bool load_config(const char *path, struct sway_config *config) {
2018-01-05 21:32:51 +00:00
wlr_log(L_INFO, "Loading config from %s", path);
2017-12-05 09:40:55 +00:00
current_config_path = path;
struct stat sb;
if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
return false;
}
if (path == NULL) {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Unable to find a config file!");
2017-12-05 09:40:55 +00:00
return false;
}
FILE *f = fopen(path, "r");
if (!f) {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Unable to open %s for reading", path);
2017-12-05 09:40:55 +00:00
return false;
}
bool config_load_success = read_config(f, config);
fclose(f);
if (!config_load_success) {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Error(s) loading config!");
2017-12-05 09:40:55 +00:00
}
current_config_path = NULL;
return true;
}
bool load_main_config(const char *file, bool is_active) {
char *path;
if (file != NULL) {
path = strdup(file);
} else {
path = get_config_path();
}
struct sway_config *old_config = config;
config = calloc(1, sizeof(struct sway_config));
if (!config) {
sway_abort("Unable to allocate config");
}
config_defaults(config);
if (is_active) {
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "Performing configuration file reload");
2017-12-05 09:40:55 +00:00
config->reloading = true;
config->active = true;
}
config->current_config = path;
list_add(config->config_chain, path);
config->reading = true;
// Read security configs
2018-01-31 04:09:21 +00:00
// TODO: Security
2017-12-05 09:40:55 +00:00
bool success = true;
2018-01-31 04:09:21 +00:00
/*
2017-12-05 09:40:55 +00:00
DIR *dir = opendir(SYSCONFDIR "/sway/security.d");
if (!dir) {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR,
2017-12-12 14:02:30 +00:00
"%s does not exist, sway will have no security configuration"
" and will probably be broken", SYSCONFDIR "/sway/security.d");
2017-12-05 09:40:55 +00:00
} else {
list_t *secconfigs = create_list();
char *base = SYSCONFDIR "/sway/security.d/";
struct dirent *ent = readdir(dir);
struct stat s;
while (ent != NULL) {
char *_path = malloc(strlen(ent->d_name) + strlen(base) + 1);
strcpy(_path, base);
strcat(_path, ent->d_name);
lstat(_path, &s);
if (S_ISREG(s.st_mode) && ent->d_name[0] != '.') {
list_add(secconfigs, _path);
}
else {
free(_path);
}
ent = readdir(dir);
}
closedir(dir);
list_qsort(secconfigs, qstrcmp);
for (int i = 0; i < secconfigs->length; ++i) {
char *_path = secconfigs->items[i];
2017-12-12 14:02:30 +00:00
if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 ||
(((s.st_mode & 0777) != 0644) &&
2017-12-14 16:11:56 +00:00
(s.st_mode & 0777) != 0444)) {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR,
2017-12-12 14:02:30 +00:00
"Refusing to load %s - it must be owned by root "
"and mode 644 or 444", _path);
2017-12-05 09:40:55 +00:00
success = false;
} else {
success = success && load_config(_path, config);
}
}
free_flat_list(secconfigs);
}
2018-01-31 04:09:21 +00:00
*/
2017-12-05 09:40:55 +00:00
success = success && load_config(path, config);
if (is_active) {
config->reloading = false;
}
if (old_config) {
destroy_removed_seats(old_config, config);
2017-12-05 09:40:55 +00:00
free_config(old_config);
}
config->reading = false;
return success;
}
2017-12-12 14:02:30 +00:00
static bool load_include_config(const char *path, const char *parent_dir,
struct sway_config *config) {
2017-12-05 17:47:57 +00:00
// save parent config
const char *parent_config = config->current_config;
char *full_path = strdup(path);
int len = strlen(path);
if (len >= 1 && path[0] != '/') {
len = len + strlen(parent_dir) + 2;
full_path = malloc(len * sizeof(char));
if (!full_path) {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR,
2017-12-12 14:02:30 +00:00
"Unable to allocate full path to included config");
2017-12-05 17:47:57 +00:00
return false;
}
snprintf(full_path, len, "%s/%s", parent_dir, path);
}
char *real_path = realpath(full_path, NULL);
free(full_path);
if (real_path == NULL) {
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "%s not found.", path);
2017-12-05 17:47:57 +00:00
return false;
}
// check if config has already been included
int j;
for (j = 0; j < config->config_chain->length; ++j) {
char *old_path = config->config_chain->items[j];
if (strcmp(real_path, old_path) == 0) {
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG,
2017-12-12 14:02:30 +00:00
"%s already included once, won't be included again.",
real_path);
2017-12-05 17:47:57 +00:00
free(real_path);
return false;
}
}
config->current_config = real_path;
list_add(config->config_chain, real_path);
int index = config->config_chain->length - 1;
if (!load_config(real_path, config)) {
free(real_path);
config->current_config = parent_config;
list_del(config->config_chain, index);
return false;
}
// restore current_config
config->current_config = parent_config;
return true;
}
bool load_include_configs(const char *path, struct sway_config *config) {
char *wd = getcwd(NULL, 0);
char *parent_path = strdup(config->current_config);
const char *parent_dir = dirname(parent_path);
if (chdir(parent_dir) < 0) {
free(parent_path);
free(wd);
return false;
}
wordexp_t p;
if (wordexp(path, &p, 0) < 0) {
free(parent_path);
free(wd);
return false;
}
char **w = p.we_wordv;
size_t i;
for (i = 0; i < p.we_wordc; ++i) {
load_include_config(w[i], parent_dir, config);
}
free(parent_path);
wordfree(&p);
// restore wd
if (chdir(wd) < 0) {
free(wd);
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "failed to restore working directory");
2017-12-05 17:47:57 +00:00
return false;
}
free(wd);
return true;
}
2018-01-20 16:32:07 +00:00
void config_clear_handler_context(struct sway_config *config) {
free_input_config(config->handler_context.input_config);
2018-01-20 16:44:34 +00:00
free_seat_config(config->handler_context.seat_config);
2018-01-20 16:32:07 +00:00
memset(&config->handler_context, 0, sizeof(config->handler_context));
}
2017-12-12 13:29:37 +00:00
2017-12-05 09:40:55 +00:00
bool read_config(FILE *file, struct sway_config *config) {
bool success = true;
enum cmd_status block = CMD_BLOCK_END;
int line_number = 0;
char *line;
while (!feof(file)) {
line = read_line(file);
if (!line) {
continue;
}
line_number++;
line = strip_whitespace(line);
if (line[0] == '#') {
free(line);
continue;
}
struct cmd_results *res;
if (block == CMD_BLOCK_COMMANDS) {
// Special case
res = config_commands_command(line);
} else {
res = config_command(line, block);
}
switch(res->status) {
case CMD_FAILURE:
case CMD_INVALID:
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Error on line %i '%s': %s (%s)", line_number,
2017-12-12 14:02:30 +00:00
line, res->error, config->current_config);
2017-12-05 09:40:55 +00:00
success = false;
break;
case CMD_DEFER:
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "Deferring command `%s'", line);
2017-12-05 09:40:55 +00:00
list_add(config->cmd_queue, strdup(line));
break;
case CMD_BLOCK_MODE:
if (block == CMD_BLOCK_END) {
block = CMD_BLOCK_MODE;
} else {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Invalid block '%s'", line);
2017-12-05 09:40:55 +00:00
}
break;
case CMD_BLOCK_INPUT:
if (block == CMD_BLOCK_END) {
block = CMD_BLOCK_INPUT;
} else {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Invalid block '%s'", line);
2017-12-05 09:40:55 +00:00
}
break;
2017-12-14 16:11:56 +00:00
case CMD_BLOCK_SEAT:
if (block == CMD_BLOCK_END) {
block = CMD_BLOCK_SEAT;
} else {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Invalid block '%s'", line);
2017-12-14 16:11:56 +00:00
}
break;
2017-12-05 09:40:55 +00:00
case CMD_BLOCK_BAR:
if (block == CMD_BLOCK_END) {
block = CMD_BLOCK_BAR;
} else {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Invalid block '%s'", line);
2017-12-05 09:40:55 +00:00
}
break;
case CMD_BLOCK_BAR_COLORS:
if (block == CMD_BLOCK_BAR) {
block = CMD_BLOCK_BAR_COLORS;
} else {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Invalid block '%s'", line);
2017-12-05 09:40:55 +00:00
}
break;
case CMD_BLOCK_COMMANDS:
if (block == CMD_BLOCK_END) {
block = CMD_BLOCK_COMMANDS;
} else {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Invalid block '%s'", line);
2017-12-05 09:40:55 +00:00
}
break;
case CMD_BLOCK_IPC:
if (block == CMD_BLOCK_END) {
block = CMD_BLOCK_IPC;
} else {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Invalid block '%s'", line);
2017-12-05 09:40:55 +00:00
}
break;
case CMD_BLOCK_IPC_EVENTS:
if (block == CMD_BLOCK_IPC) {
block = CMD_BLOCK_IPC_EVENTS;
} else {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Invalid block '%s'", line);
2017-12-05 09:40:55 +00:00
}
break;
case CMD_BLOCK_END:
switch(block) {
case CMD_BLOCK_MODE:
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "End of mode block");
2017-12-05 09:40:55 +00:00
config->current_mode = config->modes->items[0];
block = CMD_BLOCK_END;
break;
case CMD_BLOCK_INPUT:
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "End of input block");
2017-12-05 09:40:55 +00:00
block = CMD_BLOCK_END;
break;
2017-12-14 16:11:56 +00:00
case CMD_BLOCK_SEAT:
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "End of seat block");
2017-12-14 16:11:56 +00:00
block = CMD_BLOCK_END;
break;
2017-12-05 09:40:55 +00:00
case CMD_BLOCK_BAR:
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "End of bar block");
2017-12-05 09:40:55 +00:00
config->current_bar = NULL;
block = CMD_BLOCK_END;
break;
case CMD_BLOCK_BAR_COLORS:
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "End of bar colors block");
2017-12-05 09:40:55 +00:00
block = CMD_BLOCK_BAR;
break;
case CMD_BLOCK_COMMANDS:
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "End of commands block");
2017-12-05 09:40:55 +00:00
block = CMD_BLOCK_END;
break;
case CMD_BLOCK_IPC:
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "End of IPC block");
2017-12-05 09:40:55 +00:00
block = CMD_BLOCK_END;
break;
case CMD_BLOCK_IPC_EVENTS:
2018-01-05 21:32:51 +00:00
wlr_log(L_DEBUG, "End of IPC events block");
2017-12-05 09:40:55 +00:00
block = CMD_BLOCK_IPC;
break;
case CMD_BLOCK_END:
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR, "Unmatched }");
2017-12-05 09:40:55 +00:00
break;
default:;
}
2018-01-20 16:32:07 +00:00
config_clear_handler_context(config);
2017-12-05 09:40:55 +00:00
default:;
}
free(line);
free_cmd_results(res);
}
return success;
}
char *do_var_replacement(char *str) {
int i;
char *find = str;
while ((find = strchr(find, '$'))) {
// Skip if escaped.
if (find > str && find[-1] == '\\') {
if (find == str + 1 || !(find > str + 1 && find[-2] == '\\')) {
++find;
continue;
}
}
// Find matching variable
for (i = 0; i < config->symbols->length; ++i) {
struct sway_variable *var = config->symbols->items[i];
int vnlen = strlen(var->name);
if (strncmp(find, var->name, vnlen) == 0) {
int vvlen = strlen(var->value);
char *newstr = malloc(strlen(str) - vnlen + vvlen + 1);
if (!newstr) {
2018-01-05 21:32:51 +00:00
wlr_log(L_ERROR,
2017-12-12 14:02:30 +00:00
"Unable to allocate replacement "
"during variable expansion");
2017-12-05 09:40:55 +00:00
break;
}
char *newptr = newstr;
int offset = find - str;
strncpy(newptr, str, offset);
newptr += offset;
strncpy(newptr, var->value, vvlen);
newptr += vvlen;
strcpy(newptr, find + vnlen);
free(str);
str = newstr;
find = str + offset + vvlen;
break;
}
}
if (i == config->symbols->length) {
++find;
}
}
return str;
}
2018-01-31 04:09:21 +00:00
// the naming is intentional (albeit long): a workspace_output_cmp function
// would compare two structs in full, while this method only compares the
// workspace.
int workspace_output_cmp_workspace(const void *a, const void *b) {
const struct workspace_output *wsa = a, *wsb = b;
return lenient_strcmp(wsa->workspace, wsb->workspace);
}