aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/stringop.h4
-rw-r--r--sway/commands.c6
-rw-r--r--sway/config.c5
-rw-r--r--sway/stringop.c50
4 files changed, 33 insertions, 32 deletions
diff --git a/include/stringop.h b/include/stringop.h
index 03387345..a5346829 100644
--- a/include/stringop.h
+++ b/include/stringop.h
@@ -2,8 +2,8 @@
2#define _SWAY_STRINGOP_H 2#define _SWAY_STRINGOP_H
3#include "list.h" 3#include "list.h"
4 4
5void strip_whitespace(char *str); 5char *strip_whitespace(char *str, int *trimmed_start);
6void strip_comments(char *str); 6char *strip_comments(char *str);
7list_t *split_string(const char *str, const char *delims); 7list_t *split_string(const char *str, const char *delims);
8void free_flat_list(list_t *list); 8void free_flat_list(list_t *list);
9char *code_strchr(const char *string, char delimiter); 9char *code_strchr(const char *string, char delimiter);
diff --git a/sway/commands.c b/sway/commands.c
index 51de7a50..ae0bdbe4 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -506,7 +506,7 @@ static char **split_directive(char *line, int *argc) {
506 if (!*line) return parts; 506 if (!*line) return parts;
507 507
508 int in_string = 0, in_character = 0; 508 int in_string = 0, in_character = 0;
509 int i, j; 509 int i, j, _;
510 for (i = 0, j = 0; line[i]; ++i) { 510 for (i = 0, j = 0; line[i]; ++i) {
511 if (line[i] == '\\') { 511 if (line[i] == '\\') {
512 ++i; 512 ++i;
@@ -519,7 +519,7 @@ static char **split_directive(char *line, int *argc) {
519 char *item = malloc(i - j + 1); 519 char *item = malloc(i - j + 1);
520 strncpy(item, line + j, i - j); 520 strncpy(item, line + j, i - j);
521 item[i - j] = '\0'; 521 item[i - j] = '\0';
522 strip_whitespace(item); 522 item = strip_whitespace(item, &_);
523 if (item[0] == '\0') { 523 if (item[0] == '\0') {
524 free(item); 524 free(item);
525 } else { 525 } else {
@@ -537,7 +537,7 @@ static char **split_directive(char *line, int *argc) {
537 char *item = malloc(i - j + 1); 537 char *item = malloc(i - j + 1);
538 strncpy(item, line + j, i - j); 538 strncpy(item, line + j, i - j);
539 item[i - j] = '\0'; 539 item[i - j] = '\0';
540 strip_whitespace(item); 540 item = strip_whitespace(item, &_);
541 if (*argc == capacity) { 541 if (*argc == capacity) {
542 capacity++; 542 capacity++;
543 parts = realloc(parts, sizeof(char *) * capacity); 543 parts = realloc(parts, sizeof(char *) * capacity);
diff --git a/sway/config.c b/sway/config.c
index 6d39839d..f1de6080 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -186,9 +186,10 @@ bool read_config(FILE *file, bool is_active) {
186 int temp_depth = 0; // Temporary: skip all config sections with depth 186 int temp_depth = 0; // Temporary: skip all config sections with depth
187 187
188 while (!feof(file)) { 188 while (!feof(file)) {
189 int _;
189 char *line = read_line(file); 190 char *line = read_line(file);
190 strip_comments(line); 191 line = strip_comments(line);
191 strip_whitespace(line); 192 line = strip_whitespace(line, &_);
192 if (!line[0]) { 193 if (!line[0]) {
193 goto _continue; 194 goto _continue;
194 } 195 }
diff --git a/sway/stringop.c b/sway/stringop.c
index 624c8401..1dff97bf 100644
--- a/sway/stringop.c
+++ b/sway/stringop.c
@@ -1,38 +1,37 @@
1#include "stringop.h"
1#include <stdlib.h> 2#include <stdlib.h>
2#include <stdio.h> 3#include <stdio.h>
3#include <strings.h>
4#include <ctype.h>
5#include "stringop.h"
6#include "string.h" 4#include "string.h"
7#include "list.h" 5#include "list.h"
6#include <strings.h>
8 7
9/* Note: This returns 8 characters for trimmed_start per tab character. */ 8/* Note: This returns 8 characters for trimmed_start per tab character. */
10void strip_whitespace(char *str) { 9char *strip_whitespace(char *_str, int *trimmed_start) {
11 int shift = 0; 10 *trimmed_start = 0;
12 int bpair = 1; 11 if (*_str == '\0')
13 int in_str = 0, in_ch = 0; 12 return _str;
14 while (*str) { 13 char *strold = _str;
15 str[-shift] = str[0]; 14 while (*_str == ' ' || *_str == '\t') {
16 if (*str == '"' && !in_ch) { 15 if (*_str == '\t') {
17 in_str = !in_str; 16 *trimmed_start += 8;
18 } else if (*str == '\'' && !in_str) { 17 } else {
19 in_ch = !in_ch; 18 *trimmed_start += 1;
20 } else if (!in_ch && !in_str) {
21 if (isblank(*str)) {
22 if (bpair) {
23 ++shift;
24 }
25 bpair=1;
26 } else {
27 bpair = 0;
28 }
29 } 19 }
30 ++str; 20 _str++;
31 } 21 }
32 str[-shift-bpair] = 0; 22 char *str = malloc(strlen(_str) + 1);
23 strcpy(str, _str);
24 free(strold);
25 int i;
26 for (i = 0; str[i] != '\0'; ++i);
27 do {
28 i--;
29 } while (i >= 0 && (str[i] == ' ' || str[i] == '\t'));
30 str[i + 1] = '\0';
31 return str;
33} 32}
34 33
35void strip_comments(char *str) { 34char *strip_comments(char *str) {
36 int in_string = 0, in_character = 0; 35 int in_string = 0, in_character = 0;
37 int i = 0; 36 int i = 0;
38 while (str[i] != '\0') { 37 while (str[i] != '\0') {
@@ -48,6 +47,7 @@ void strip_comments(char *str) {
48 } 47 }
49 ++i; 48 ++i;
50 } 49 }
50 return str;
51} 51}
52 52
53list_t *split_string(const char *str, const char *delims) { 53list_t *split_string(const char *str, const char *delims) {