2015-08-05 03:30:40 +02:00
|
|
|
#include <stdio.h>
|
2015-08-05 23:30:40 +02:00
|
|
|
#include <stdbool.h>
|
2015-08-05 03:30:40 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "readline.h"
|
|
|
|
#include "stringop.h"
|
|
|
|
#include "list.h"
|
2015-08-09 01:24:18 +02:00
|
|
|
#include "log.h"
|
2015-08-05 23:30:40 +02:00
|
|
|
#include "commands.h"
|
2015-08-05 03:30:40 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2015-08-09 15:23:10 +02:00
|
|
|
void config_defaults(struct sway_config *config) {
|
2015-08-05 03:30:40 +02:00
|
|
|
config->symbols = create_list();
|
|
|
|
config->modes = create_list();
|
2015-08-06 04:10:56 +02:00
|
|
|
config->current_mode = malloc(sizeof(struct sway_mode));
|
|
|
|
config->current_mode->name = NULL;
|
|
|
|
config->current_mode->bindings = create_list();
|
|
|
|
list_add(config->modes, config->current_mode);
|
2015-08-09 15:23:10 +02:00
|
|
|
// Flags
|
|
|
|
config->focus_follows_mouse = true;
|
|
|
|
config->mouse_warping = true;
|
2015-08-10 21:09:51 +02:00
|
|
|
config->reloading = false;
|
2015-08-09 15:23:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 20:53:43 +02:00
|
|
|
struct sway_config *read_config(FILE *file, bool is_active) {
|
2015-08-09 15:23:10 +02:00
|
|
|
struct sway_config *config = malloc(sizeof(struct sway_config));
|
|
|
|
config_defaults(config);
|
2015-08-05 03:30:40 +02:00
|
|
|
|
2015-08-10 21:09:51 +02:00
|
|
|
if (is_active) {
|
|
|
|
config->reloading = true;
|
|
|
|
}
|
2015-08-10 20:53:43 +02:00
|
|
|
|
2015-08-06 04:10:56 +02:00
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
int temp_depth = 0; // Temporary: skip all config sections with depth
|
2015-08-05 23:30:40 +02:00
|
|
|
|
2015-08-05 03:30:40 +02:00
|
|
|
while (!feof(file)) {
|
|
|
|
int _;
|
|
|
|
char *line = read_line(file);
|
|
|
|
line = strip_whitespace(line, &_);
|
|
|
|
line = strip_comments(line);
|
|
|
|
if (!line[0]) {
|
|
|
|
goto _continue;
|
|
|
|
}
|
2015-08-06 04:10:56 +02:00
|
|
|
if (temp_depth && line[0] == '}') {
|
|
|
|
temp_depth--;
|
2015-08-05 23:30:40 +02:00
|
|
|
goto _continue;
|
|
|
|
}
|
|
|
|
|
2015-08-09 01:24:18 +02:00
|
|
|
if (!temp_depth && handle_command(config, line) != 0) {
|
2015-08-06 04:10:56 +02:00
|
|
|
success = false;
|
|
|
|
}
|
2015-08-10 21:00:10 +02:00
|
|
|
|
2015-08-05 03:30:40 +02:00
|
|
|
_continue:
|
2015-08-05 23:30:40 +02:00
|
|
|
if (line && line[strlen(line) - 1] == '{') {
|
2015-08-06 04:10:56 +02:00
|
|
|
temp_depth++;
|
2015-08-05 23:30:40 +02:00
|
|
|
}
|
2015-08-05 03:30:40 +02:00
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
2015-08-06 04:10:56 +02:00
|
|
|
if (!success) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-08-10 21:09:51 +02:00
|
|
|
if (is_active) {
|
|
|
|
config->reloading = true;
|
|
|
|
}
|
2015-08-10 20:53:43 +02:00
|
|
|
|
2015-08-05 03:30:40 +02:00
|
|
|
return config;
|
|
|
|
}
|
2015-08-06 04:40:38 +02:00
|
|
|
|
|
|
|
char *do_var_replacement(struct sway_config *config, char *str) {
|
|
|
|
// TODO: Handle escaping $ and using $ in string literals
|
|
|
|
int i;
|
|
|
|
for (i = 0; str[i]; ++i) {
|
|
|
|
if (str[i] == '$') {
|
|
|
|
// Try for match (note: this could be faster)
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < config->symbols->length; ++j) {
|
|
|
|
struct sway_variable *var = config->symbols->items[j];
|
|
|
|
if (strstr(str + i, var->name) == str + i) {
|
|
|
|
// Match, do replacement
|
|
|
|
char *new_string = malloc(
|
|
|
|
strlen(str) -
|
|
|
|
strlen(var->name) +
|
|
|
|
strlen(var->value) + 1);
|
|
|
|
strncpy(new_string, str, i);
|
|
|
|
new_string[i] = 0;
|
|
|
|
strcat(new_string, var->value);
|
|
|
|
strcat(new_string, str + i + strlen(var->name));
|
|
|
|
free(str);
|
|
|
|
str = new_string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|