diff options
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands.c | 6 | ||||
-rw-r--r-- | sway/config.c | 9 | ||||
-rw-r--r-- | sway/readline.c | 13 | ||||
-rw-r--r-- | sway/stringop.c | 52 |
4 files changed, 39 insertions, 41 deletions
diff --git a/sway/commands.c b/sway/commands.c index 548cb8ed..7e9169e8 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -504,7 +504,7 @@ static char **split_directive(char *line, int *argc) { if (!*line) return parts; int in_string = 0, in_character = 0; - int i, j, _; + int i, j; for (i = 0, j = 0; line[i]; ++i) { if (line[i] == '\\') { ++i; @@ -517,7 +517,7 @@ static char **split_directive(char *line, int *argc) { char *item = malloc(i - j + 1); strncpy(item, line + j, i - j); item[i - j] = '\0'; - item = strip_whitespace(item, &_); + strip_whitespace(item); if (item[0] == '\0') { free(item); } else { @@ -535,7 +535,7 @@ static char **split_directive(char *line, int *argc) { char *item = malloc(i - j + 1); strncpy(item, line + j, i - j); item[i - j] = '\0'; - item = strip_whitespace(item, &_); + strip_whitespace(item); if (*argc == capacity) { capacity++; parts = realloc(parts, sizeof(char *) * capacity); diff --git a/sway/config.c b/sway/config.c index 4125f4cd..95d605a3 100644 --- a/sway/config.c +++ b/sway/config.c @@ -40,7 +40,7 @@ static char* get_config_path() { } else { name = "/sway/config"; temp = malloc(strlen(xdg_config_home) + strlen(name) + 1); - strcpy(temp, home); + strcpy(xdg_config_home, home); strcat(temp, name); } if (exists(temp)) { @@ -93,7 +93,7 @@ static char* get_config_path() { } else { name = "/i3/config"; temp = malloc(strlen(xdg_config_home) + strlen(name) + 1); - strcpy(temp, home); + strcpy(xdg_config_home, home); strcat(temp, name); } if (exists(temp)) { @@ -186,10 +186,9 @@ bool read_config(FILE *file, bool is_active) { int temp_depth = 0; // Temporary: skip all config sections with depth while (!feof(file)) { - int _; char *line = read_line(file); - line = strip_whitespace(line, &_); - line = strip_comments(line); + strip_comments(line); + strip_whitespace(line); if (!line[0]) { goto _continue; } diff --git a/sway/readline.c b/sway/readline.c index be8c35cc..dfdc3fe8 100644 --- a/sway/readline.c +++ b/sway/readline.c @@ -3,7 +3,7 @@ #include <stdio.h> char *read_line(FILE *file) { - int i = 0, length = 0, size = 128; + int length = 0, size = 128; char *string = malloc(size); if (!string) { return NULL; @@ -16,21 +16,20 @@ char *read_line(FILE *file) { if (c == '\r') { continue; } - if (i == size) { - string = realloc(string, length *= 2); + if (length == size) { + string = realloc(string, size *= 2); if (!string) { return NULL; } } - string[i++] = (char)c; - length++; + string[length++] = c; } - if (i + 1 != size) { + if (length + 1 == size) { string = realloc(string, length + 1); if (!string) { return NULL; } } - string[i] = '\0'; + string[length] = '\0'; return string; } diff --git a/sway/stringop.c b/sway/stringop.c index 1dff97bf..00cc32b8 100644 --- a/sway/stringop.c +++ b/sway/stringop.c @@ -1,37 +1,38 @@ -#include "stringop.h" #include <stdlib.h> #include <stdio.h> +#include <strings.h> +#include <ctype.h> +#include "stringop.h" #include "string.h" #include "list.h" -#include <strings.h> /* Note: This returns 8 characters for trimmed_start per tab character. */ -char *strip_whitespace(char *_str, int *trimmed_start) { - *trimmed_start = 0; - if (*_str == '\0') - return _str; - char *strold = _str; - while (*_str == ' ' || *_str == '\t') { - if (*_str == '\t') { - *trimmed_start += 8; - } else { - *trimmed_start += 1; +void strip_whitespace(char *str) { + int shift = 0; + int bpair = 1; + int in_str = 0, in_ch = 0; + while (*str) { + str[-shift] = str[0]; + if (*str == '"' && !in_ch) { + in_str = !in_str; + } else if (*str == '\'' && !in_str) { + in_ch = !in_ch; + } else if (!in_ch && !in_str) { + if (isblank(*str)) { + if (bpair) { + ++shift; + } + bpair=1; + } else { + bpair = 0; + } } - _str++; + ++str; } - char *str = malloc(strlen(_str) + 1); - strcpy(str, _str); - free(strold); - int i; - for (i = 0; str[i] != '\0'; ++i); - do { - i--; - } while (i >= 0 && (str[i] == ' ' || str[i] == '\t')); - str[i + 1] = '\0'; - return str; + str[-shift-bpair] = 0; } -char *strip_comments(char *str) { +void strip_comments(char *str) { int in_string = 0, in_character = 0; int i = 0; while (str[i] != '\0') { @@ -40,14 +41,13 @@ char *strip_comments(char *str) { } else if (str[i] == '\'' && !in_string) { in_character = !in_character; } else if (!in_character && !in_string) { - if (str[i] == '#' && i == 0) { + if (str[i] == '#') { str[i] = '\0'; break; } } ++i; } - return str; } list_t *split_string(const char *str, const char *delims) { |