mirror of
https://github.com/swaywm/sway.git
synced 2024-11-29 11:21:28 +00:00
Support braces on next line for config blocks
This commit is contained in:
parent
7c810dc344
commit
51bb9d8573
|
@ -48,6 +48,20 @@ char *read_line(FILE *file) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *peek_line(FILE *file, int offset) {
|
||||||
|
int pos = ftell(file);
|
||||||
|
char *line = NULL;
|
||||||
|
for (int i = 0; i <= offset; i++) {
|
||||||
|
free(line);
|
||||||
|
line = read_line(file);
|
||||||
|
if (!line) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fseek(file, pos, SEEK_SET);
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
|
||||||
char *read_line_buffer(FILE *file, char *string, size_t string_len) {
|
char *read_line_buffer(FILE *file, char *string, size_t string_len) {
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
if (!string) {
|
if (!string) {
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
char *read_line(FILE *file);
|
char *read_line(FILE *file);
|
||||||
|
char *peek_line(FILE *file, int offset);
|
||||||
char *read_line_buffer(FILE *file, char *string, size_t string_len);
|
char *read_line_buffer(FILE *file, char *string, size_t string_len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -513,6 +513,49 @@ bool load_include_configs(const char *path, struct sway_config *config) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int detect_brace_on_following_line(FILE *file, char *line,
|
||||||
|
int line_number) {
|
||||||
|
int lines = 0;
|
||||||
|
if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') {
|
||||||
|
char *peeked = NULL;
|
||||||
|
do {
|
||||||
|
wlr_log(L_DEBUG, "Peeking line %d", line_number + lines + 1);
|
||||||
|
free(peeked);
|
||||||
|
peeked = peek_line(file, lines);
|
||||||
|
if (peeked) {
|
||||||
|
peeked = strip_whitespace(peeked);
|
||||||
|
}
|
||||||
|
lines++;
|
||||||
|
} while (peeked && strlen(peeked) == 0);
|
||||||
|
|
||||||
|
if (peeked && strlen(peeked) == 1 && peeked[0] == '{') {
|
||||||
|
for (int i = 0; i < lines; i++) {
|
||||||
|
free(peeked);
|
||||||
|
peeked = read_line(file);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
lines = 0;
|
||||||
|
}
|
||||||
|
free(peeked);
|
||||||
|
}
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *expand_line(char *block, char *line, bool add_brace) {
|
||||||
|
int size = (block ? strlen(block) + 1 : 0) + strlen(line)
|
||||||
|
+ (add_brace ? 2 : 0) + 1;
|
||||||
|
char *expanded = calloc(1, size);
|
||||||
|
if (!expanded) {
|
||||||
|
wlr_log(L_ERROR, "Cannot allocate expanded line buffer");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
strcat(expanded, block ? block : "");
|
||||||
|
strcat(expanded, block ? " " : "");
|
||||||
|
strcat(expanded, line);
|
||||||
|
strcat(expanded, add_brace ? " {" : "");
|
||||||
|
return expanded;
|
||||||
|
}
|
||||||
|
|
||||||
bool read_config(FILE *file, struct sway_config *config) {
|
bool read_config(FILE *file, struct sway_config *config) {
|
||||||
bool success = true;
|
bool success = true;
|
||||||
int line_number = 0;
|
int line_number = 0;
|
||||||
|
@ -535,19 +578,27 @@ bool read_config(FILE *file, struct sway_config *config) {
|
||||||
free(line);
|
free(line);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
char *full = calloc(strlen(block ? block : "") + strlen(line) + 2, 1);
|
int brace_detected = detect_brace_on_following_line(file, line,
|
||||||
strcat(full, block ? block : "");
|
line_number);
|
||||||
strcat(full, block ? " " : "");
|
if (brace_detected > 0) {
|
||||||
strcat(full, line);
|
line_number += brace_detected;
|
||||||
wlr_log(L_DEBUG, "Expanded line: %s", full);
|
wlr_log(L_DEBUG, "Detected open brace on line %d", line_number);
|
||||||
|
}
|
||||||
|
char *expanded = expand_line(block, line, brace_detected > 0);
|
||||||
|
if (!expanded) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
wlr_log(L_DEBUG, "Expanded line: %s", expanded);
|
||||||
struct cmd_results *res;
|
struct cmd_results *res;
|
||||||
if (block && strcmp(block, "<commands>") == 0) {
|
if (block && strcmp(block, "<commands>") == 0) {
|
||||||
// Special case
|
// Special case
|
||||||
res = config_commands_command(full);
|
res = config_commands_command(expanded);
|
||||||
} else {
|
} else {
|
||||||
res = config_command(full);
|
wlr_log(L_DEBUG, "Entering c_c");
|
||||||
|
res = config_command(expanded);
|
||||||
|
wlr_log(L_DEBUG, "Exiting c_c");
|
||||||
}
|
}
|
||||||
free(full);
|
free(expanded);
|
||||||
switch(res->status) {
|
switch(res->status) {
|
||||||
case CMD_FAILURE:
|
case CMD_FAILURE:
|
||||||
case CMD_INVALID:
|
case CMD_INVALID:
|
||||||
|
|
Loading…
Reference in a new issue