summaryrefslogtreecommitdiffstats
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, 32 insertions, 33 deletions
diff --git a/include/stringop.h b/include/stringop.h
index a5346829..03387345 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
5char *strip_whitespace(char *str, int *trimmed_start); 5void strip_whitespace(char *str);
6char *strip_comments(char *str); 6void 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 444e6159..642fa3ce 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -490,7 +490,7 @@ static char **split_directive(char *line, int *argc) {
490 if (!*line) return parts; 490 if (!*line) return parts;
491 491
492 int in_string = 0, in_character = 0; 492 int in_string = 0, in_character = 0;
493 int i, j, _; 493 int i, j;
494 for (i = 0, j = 0; line[i]; ++i) { 494 for (i = 0, j = 0; line[i]; ++i) {
495 if (line[i] == '\\') { 495 if (line[i] == '\\') {
496 ++i; 496 ++i;
@@ -503,7 +503,7 @@ static char **split_directive(char *line, int *argc) {
503 char *item = malloc(i - j + 1); 503 char *item = malloc(i - j + 1);
504 strncpy(item, line + j, i - j); 504 strncpy(item, line + j, i - j);
505 item[i - j] = '\0'; 505 item[i - j] = '\0';
506 item = strip_whitespace(item, &_); 506 strip_whitespace(item);
507 if (item[0] == '\0') { 507 if (item[0] == '\0') {
508 free(item); 508 free(item);
509 } else { 509 } else {
@@ -521,7 +521,7 @@ static char **split_directive(char *line, int *argc) {
521 char *item = malloc(i - j + 1); 521 char *item = malloc(i - j + 1);
522 strncpy(item, line + j, i - j); 522 strncpy(item, line + j, i - j);
523 item[i - j] = '\0'; 523 item[i - j] = '\0';
524 item = strip_whitespace(item, &_); 524 strip_whitespace(item);
525 if (*argc == capacity) { 525 if (*argc == capacity) {
526 capacity++; 526 capacity++;
527 parts = realloc(parts, sizeof(char *) * capacity); 527 parts = realloc(parts, sizeof(char *) * capacity);
diff --git a/sway/config.c b/sway/config.c
index 44943769..bde65614 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -186,10 +186,9 @@ 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 _;
190 char *line = read_line(file); 189 char *line = read_line(file);
191 line = strip_comments(line); 190 strip_comments(line);
192 line = strip_whitespace(line, &_); 191 strip_whitespace(line);
193 if (!line[0]) { 192 if (!line[0]) {
194 goto _continue; 193 goto _continue;
195 } 194 }
diff --git a/sway/stringop.c b/sway/stringop.c
index cbaa69b9..00cc32b8 100644
--- a/sway/stringop.c
+++ b/sway/stringop.c
@@ -1,37 +1,38 @@
1#include "stringop.h"
2#include <stdlib.h> 1#include <stdlib.h>
3#include <stdio.h> 2#include <stdio.h>
3#include <strings.h>
4#include <ctype.h>
5#include "stringop.h"
4#include "string.h" 6#include "string.h"
5#include "list.h" 7#include "list.h"
6#include <strings.h>
7 8
8/* Note: This returns 8 characters for trimmed_start per tab character. */ 9/* Note: This returns 8 characters for trimmed_start per tab character. */
9char *strip_whitespace(char *_str, int *trimmed_start) { 10void strip_whitespace(char *str) {
10 *trimmed_start = 0; 11 int shift = 0;
11 if (*_str == '\0') 12 int bpair = 1;
12 return _str; 13 int in_str = 0, in_ch = 0;
13 char *strold = _str; 14 while (*str) {
14 while (*_str == ' ' || *_str == '\t') { 15 str[-shift] = str[0];
15 if (*_str == '\t') { 16 if (*str == '"' && !in_ch) {
16 *trimmed_start += 8; 17 in_str = !in_str;
17 } else { 18 } else if (*str == '\'' && !in_str) {
18 *trimmed_start += 1; 19 in_ch = !in_ch;
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 }
19 } 29 }
20 _str++; 30 ++str;
21 } 31 }
22 char *str = malloc(strlen(_str) + 1); 32 str[-shift-bpair] = 0;
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;
32} 33}
33 34
34char *strip_comments(char *str) { 35void strip_comments(char *str) {
35 int in_string = 0, in_character = 0; 36 int in_string = 0, in_character = 0;
36 int i = 0; 37 int i = 0;
37 while (str[i] != '\0') { 38 while (str[i] != '\0') {
@@ -47,7 +48,6 @@ char *strip_comments(char *str) {
47 } 48 }
48 ++i; 49 ++i;
49 } 50 }
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) {