aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Ian Fan <ianfan0@gmail.com>2018-12-09 12:09:11 +0000
committerLibravatar Ian Fan <ianfan0@gmail.com>2019-01-01 09:01:25 +0000
commita82b8a3c14e45697708e57f8cb27a8fc6cf31839 (patch)
tree5e30327566fb6f30bd6d319f7b8a96226683e986
parentstringop.c: rewrite strip_whitespace (diff)
downloadsway-a82b8a3c14e45697708e57f8cb27a8fc6cf31839.tar.gz
sway-a82b8a3c14e45697708e57f8cb27a8fc6cf31839.tar.zst
sway-a82b8a3c14e45697708e57f8cb27a8fc6cf31839.zip
Remove readline.c
All occurrences of read_line have been replaced by getline. peek_line has been absorbed into detect_brace.
-rw-r--r--common/ipc-client.c11
-rw-r--r--common/meson.build1
-rw-r--r--common/readline.c72
-rw-r--r--common/util.c4
-rw-r--r--include/readline.h10
-rw-r--r--sway/config.c98
-rw-r--r--sway/main.c68
-rw-r--r--swaybar/status_line.c1
-rw-r--r--swaylock/main.c33
-rw-r--r--swaymsg/main.c1
-rw-r--r--swaynag/config.c62
11 files changed, 116 insertions, 245 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c
index 496fd131..3515ef0a 100644
--- a/common/ipc-client.c
+++ b/common/ipc-client.c
@@ -7,7 +7,6 @@
7#include <sys/un.h> 7#include <sys/un.h>
8#include <unistd.h> 8#include <unistd.h>
9#include "ipc-client.h" 9#include "ipc-client.h"
10#include "readline.h"
11#include "log.h" 10#include "log.h"
12 11
13static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; 12static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
@@ -18,28 +17,30 @@ char *get_socketpath(void) {
18 if (swaysock) { 17 if (swaysock) {
19 return strdup(swaysock); 18 return strdup(swaysock);
20 } 19 }
20 char *line = NULL;
21 size_t line_size = 0;
21 FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r"); 22 FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r");
22 if (fp) { 23 if (fp) {
23 char *line = read_line(fp); 24 getline(&line, &line_size, fp);
24 pclose(fp); 25 pclose(fp);
25 if (line && *line) { 26 if (line && *line) {
26 return line; 27 return line;
27 } 28 }
28 free(line);
29 } 29 }
30 const char *i3sock = getenv("I3SOCK"); 30 const char *i3sock = getenv("I3SOCK");
31 if (i3sock) { 31 if (i3sock) {
32 free(line);
32 return strdup(i3sock); 33 return strdup(i3sock);
33 } 34 }
34 fp = popen("i3 --get-socketpath 2>/dev/null", "r"); 35 fp = popen("i3 --get-socketpath 2>/dev/null", "r");
35 if (fp) { 36 if (fp) {
36 char *line = read_line(fp); 37 getline(&line, &line_size, fp);
37 pclose(fp); 38 pclose(fp);
38 if (line && *line) { 39 if (line && *line) {
39 return line; 40 return line;
40 } 41 }
41 free(line);
42 } 42 }
43 free(line);
43 return NULL; 44 return NULL;
44} 45}
45 46
diff --git a/common/meson.build b/common/meson.build
index 224a9c3f..4ad872d1 100644
--- a/common/meson.build
+++ b/common/meson.build
@@ -8,7 +8,6 @@ lib_sway_common = static_library(
8 'loop.c', 8 'loop.c',
9 'list.c', 9 'list.c',
10 'pango.c', 10 'pango.c',
11 'readline.c',
12 'stringop.c', 11 'stringop.c',
13 'unicode.c', 12 'unicode.c',
14 'util.c' 13 'util.c'
diff --git a/common/readline.c b/common/readline.c
deleted file mode 100644
index 58652429..00000000
--- a/common/readline.c
+++ /dev/null
@@ -1,72 +0,0 @@
1#define _POSIX_C_SOURCE 200809L
2#include "readline.h"
3#include "log.h"
4#include <stdlib.h>
5#include <stdio.h>
6
7char *read_line(FILE *file) {
8 size_t length = 0, size = 128;
9 char *string = malloc(size);
10 char lastChar = '\0';
11 if (!string) {
12 wlr_log(WLR_ERROR, "Unable to allocate memory for read_line");
13 return NULL;
14 }
15 while (1) {
16 int c = getc(file);
17 if (c == '\n' && lastChar == '\\'){
18 --length; // Ignore last character.
19 lastChar = '\0';
20 continue;
21 }
22 if (c == EOF || c == '\n' || c == '\0') {
23 break;
24 }
25 if (c == '\r') {
26 continue;
27 }
28 lastChar = c;
29 if (length == size) {
30 char *new_string = realloc(string, size *= 2);
31 if (!new_string) {
32 free(string);
33 wlr_log(WLR_ERROR, "Unable to allocate memory for read_line");
34 return NULL;
35 }
36 string = new_string;
37 }
38 string[length++] = c;
39 }
40 if (length + 1 == size) {
41 char *new_string = realloc(string, length + 1);
42 if (!new_string) {
43 free(string);
44 return NULL;
45 }
46 string = new_string;
47 }
48 string[length] = '\0';
49 return string;
50}
51
52char *peek_line(FILE *file, int line_offset, long *position) {
53 long pos = ftell(file);
54 size_t length = 0;
55 char *line = NULL;
56 for (int i = 0; i <= line_offset; i++) {
57 ssize_t read = getline(&line, &length, file);
58 if (read < 0) {
59 free(line);
60 line = NULL;
61 break;
62 }
63 if (read > 0 && line[read - 1] == '\n') {
64 line[read - 1] = '\0';
65 }
66 }
67 if (position) {
68 *position = ftell(file);
69 }
70 fseek(file, pos, SEEK_SET);
71 return line;
72}
diff --git a/common/util.c b/common/util.c
index 40c64230..d66058a6 100644
--- a/common/util.c
+++ b/common/util.c
@@ -13,7 +13,6 @@
13#include <xkbcommon/xkbcommon-names.h> 13#include <xkbcommon/xkbcommon-names.h>
14#include <wlr/types/wlr_keyboard.h> 14#include <wlr/types/wlr_keyboard.h>
15#include "log.h" 15#include "log.h"
16#include "readline.h"
17#include "util.h" 16#include "util.h"
18 17
19int wrap(int i, int max) { 18int wrap(int i, int max) {
@@ -87,11 +86,12 @@ pid_t get_parent_pid(pid_t child) {
87 char *token = NULL; 86 char *token = NULL;
88 const char *sep = " "; 87 const char *sep = " ";
89 FILE *stat = NULL; 88 FILE *stat = NULL;
89 size_t buf_size = 0;
90 90
91 sprintf(file_name, "/proc/%d/stat", child); 91 sprintf(file_name, "/proc/%d/stat", child);
92 92
93 if ((stat = fopen(file_name, "r"))) { 93 if ((stat = fopen(file_name, "r"))) {
94 if ((buffer = read_line(stat))) { 94 if (getline(&buffer, &buf_size, stat) != -1) {
95 token = strtok(buffer, sep); // pid 95 token = strtok(buffer, sep); // pid
96 token = strtok(NULL, sep); // executable name 96 token = strtok(NULL, sep); // executable name
97 token = strtok(NULL, sep); // state 97 token = strtok(NULL, sep); // state
diff --git a/include/readline.h b/include/readline.h
deleted file mode 100644
index ee2eba5d..00000000
--- a/include/readline.h
+++ /dev/null
@@ -1,10 +0,0 @@
1#ifndef _SWAY_READLINE_H
2#define _SWAY_READLINE_H
3
4#include <stdio.h>
5
6char *read_line(FILE *file);
7char *peek_line(FILE *file, int line_offset, long *position);
8char *read_line_buffer(FILE *file, char *string, size_t string_len);
9
10#endif
diff --git a/sway/config.c b/sway/config.c
index c71f315a..4afa09b3 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -1,4 +1,4 @@
1#define _XOPEN_SOURCE 600 // for realpath 1#define _XOPEN_SOURCE 700 // for realpath
2#include <stdio.h> 2#include <stdio.h>
3#include <stdbool.h> 3#include <stdbool.h>
4#include <stdlib.h> 4#include <stdlib.h>
@@ -30,7 +30,6 @@
30#include "sway/tree/workspace.h" 30#include "sway/tree/workspace.h"
31#include "cairo.h" 31#include "cairo.h"
32#include "pango.h" 32#include "pango.h"
33#include "readline.h"
34#include "stringop.h" 33#include "stringop.h"
35#include "list.h" 34#include "list.h"
36#include "log.h" 35#include "log.h"
@@ -570,28 +569,23 @@ bool load_include_configs(const char *path, struct sway_config *config,
570 return true; 569 return true;
571} 570}
572 571
573static int detect_brace_on_following_line(FILE *file, char *line, 572static int detect_brace(FILE *file) {
574 int line_number) {
575 int lines = 0; 573 int lines = 0;
576 if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { 574 long pos = ftell(file);
577 char *peeked = NULL; 575 char *line = NULL;
578 long position = 0; 576 size_t line_size = 0;
579 do { 577 while ((getline(&line, &line_size, file)) != -1) {
580 free(peeked); 578 lines++;
581 peeked = peek_line(file, lines, &position); 579 strip_whitespace(line);
582 if (peeked) { 580 if (*line) {
583 strip_whitespace(peeked); 581 if (strcmp(line, "{") != 0) {
582 fseek(file, pos, SEEK_SET);
583 lines = 0;
584 } 584 }
585 lines++; 585 break;
586 } while (peeked && strlen(peeked) == 0);
587
588 if (peeked && strlen(peeked) == 1 && peeked[0] == '{') {
589 fseek(file, position, SEEK_SET);
590 } else {
591 lines = 0;
592 } 586 }
593 free(peeked);
594 } 587 }
588 free(line);
595 return lines; 589 return lines;
596} 590}
597 591
@@ -634,55 +628,47 @@ bool read_config(FILE *file, struct sway_config *config,
634 628
635 bool success = true; 629 bool success = true;
636 int line_number = 0; 630 int line_number = 0;
637 char *line; 631 char *line = NULL;
632 size_t line_size = 0;
633 ssize_t nread;
638 list_t *stack = create_list(); 634 list_t *stack = create_list();
639 size_t read = 0; 635 size_t read = 0;
640 while (!feof(file)) { 636 while ((nread = getline(&line, &line_size, file)) != -1) {
641 char *block = stack->length ? stack->items[0] : NULL;
642 line = read_line(file);
643 if (!line) {
644 continue;
645 }
646 line_number++;
647 wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line);
648
649 if (reading_main_config) { 637 if (reading_main_config) {
650 size_t length = strlen(line); 638 if (read + nread > config_size) {
651
652 if (read + length > config_size) {
653 wlr_log(WLR_ERROR, "Config file changed during reading"); 639 wlr_log(WLR_ERROR, "Config file changed during reading");
654 list_free_items_and_destroy(stack); 640 success = false;
655 free(line); 641 break;
656 return false;
657 } 642 }
658 643
659 strcpy(this_config + read, line); 644 strcpy(&this_config[read], line);
660 if (line_number != 1) { 645 read += nread;
661 this_config[read - 1] = '\n';
662 }
663 read += length + 1;
664 } 646 }
665 647
666 strip_whitespace(line); 648 if (line[nread - 1] == '\n') {
667 if (line[0] == '#') { 649 line[nread - 1] = '\0';
668 free(line);
669 continue;
670 } 650 }
671 if (strlen(line) == 0) { 651
672 free(line); 652 line_number++;
653 wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line);
654
655 strip_whitespace(line);
656 if (!*line || line[0] == '#') {
673 continue; 657 continue;
674 } 658 }
675 int brace_detected = detect_brace_on_following_line(file, line, 659 int brace_detected = 0;
676 line_number); 660 if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') {
677 if (brace_detected > 0) { 661 brace_detected = detect_brace(file);
678 line_number += brace_detected; 662 if (brace_detected > 0) {
679 wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number); 663 line_number += brace_detected;
664 wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number);
665 }
680 } 666 }
667 char *block = stack->length ? stack->items[0] : NULL;
681 char *expanded = expand_line(block, line, brace_detected > 0); 668 char *expanded = expand_line(block, line, brace_detected > 0);
682 if (!expanded) { 669 if (!expanded) {
683 list_free_items_and_destroy(stack); 670 success = false;
684 free(line); 671 break;
685 return false;
686 } 672 }
687 config->current_config_line_number = line_number; 673 config->current_config_line_number = line_number;
688 config->current_config_line = line; 674 config->current_config_line = line;
@@ -742,9 +728,9 @@ bool read_config(FILE *file, struct sway_config *config,
742 default:; 728 default:;
743 } 729 }
744 free(expanded); 730 free(expanded);
745 free(line);
746 free_cmd_results(res); 731 free_cmd_results(res);
747 } 732 }
733 free(line);
748 list_free_items_and_destroy(stack); 734 list_free_items_and_destroy(stack);
749 config->current_config_line_number = 0; 735 config->current_config_line_number = 0;
750 config->current_config_line = NULL; 736 config->current_config_line = NULL;
diff --git a/sway/main.c b/sway/main.c
index f70e751d..d08c0457 100644
--- a/sway/main.c
+++ b/sway/main.c
@@ -3,6 +3,7 @@
3#include <pango/pangocairo.h> 3#include <pango/pangocairo.h>
4#include <signal.h> 4#include <signal.h>
5#include <stdbool.h> 5#include <stdbool.h>
6#include <stdio.h>
6#include <stdlib.h> 7#include <stdlib.h>
7#include <stdio.h> 8#include <stdio.h>
8#include <string.h> 9#include <string.h>
@@ -22,7 +23,6 @@
22#include "sway/ipc-server.h" 23#include "sway/ipc-server.h"
23#include "ipc-client.h" 24#include "ipc-client.h"
24#include "log.h" 25#include "log.h"
25#include "readline.h"
26#include "stringop.h" 26#include "stringop.h"
27#include "util.h" 27#include "util.h"
28 28
@@ -47,31 +47,28 @@ void detect_raspi(void) {
47 if (!f) { 47 if (!f) {
48 return; 48 return;
49 } 49 }
50 char *line; 50 char *line = NULL;
51 while(!feof(f)) { 51 size_t line_size = 0;
52 if (!(line = read_line(f))) { 52 while (getline(&line, &line_size, f) != -1) {
53 break;
54 }
55 if (strstr(line, "Raspberry Pi")) { 53 if (strstr(line, "Raspberry Pi")) {
56 raspi = true; 54 raspi = true;
55 break;
57 } 56 }
58 free(line);
59 } 57 }
60 fclose(f); 58 fclose(f);
61 FILE *g = fopen("/proc/modules", "r"); 59 FILE *g = fopen("/proc/modules", "r");
62 if (!g) { 60 if (!g) {
61 free(line);
63 return; 62 return;
64 } 63 }
65 bool vc4 = false; 64 bool vc4 = false;
66 while (!feof(g)) { 65 while (getline(&line, &line_size, g) != -1) {
67 if (!(line = read_line(g))) {
68 break;
69 }
70 if (strstr(line, "vc4")) { 66 if (strstr(line, "vc4")) {
71 vc4 = true; 67 vc4 = true;
68 break;
72 } 69 }
73 free(line);
74 } 70 }
71 free(line);
75 fclose(g); 72 fclose(g);
76 if (!vc4 && raspi) { 73 if (!vc4 && raspi) {
77 fprintf(stderr, "\x1B[1;31mWarning: You have a " 74 fprintf(stderr, "\x1B[1;31mWarning: You have a "
@@ -86,13 +83,10 @@ void detect_proprietary(int allow_unsupported_gpu) {
86 if (!f) { 83 if (!f) {
87 return; 84 return;
88 } 85 }
89 while (!feof(f)) { 86 char *line = NULL;
90 char *line; 87 size_t line_size = 0;
91 if (!(line = read_line(f))) { 88 while (getline(&line, &line_size, f) != -1) {
92 break;
93 }
94 if (strstr(line, "nvidia")) { 89 if (strstr(line, "nvidia")) {
95 free(line);
96 if (allow_unsupported_gpu) { 90 if (allow_unsupported_gpu) {
97 wlr_log(WLR_ERROR, 91 wlr_log(WLR_ERROR,
98 "!!! Proprietary Nvidia drivers are in use !!!"); 92 "!!! Proprietary Nvidia drivers are in use !!!");
@@ -106,7 +100,6 @@ void detect_proprietary(int allow_unsupported_gpu) {
106 break; 100 break;
107 } 101 }
108 if (strstr(line, "fglrx")) { 102 if (strstr(line, "fglrx")) {
109 free(line);
110 if (allow_unsupported_gpu) { 103 if (allow_unsupported_gpu) {
111 wlr_log(WLR_ERROR, 104 wlr_log(WLR_ERROR,
112 "!!! Proprietary AMD drivers are in use !!!"); 105 "!!! Proprietary AMD drivers are in use !!!");
@@ -118,8 +111,8 @@ void detect_proprietary(int allow_unsupported_gpu) {
118 } 111 }
119 break; 112 break;
120 } 113 }
121 free(line);
122 } 114 }
115 free(line);
123 fclose(f); 116 fclose(f);
124} 117}
125 118
@@ -146,6 +139,19 @@ static void log_env(void) {
146 } 139 }
147} 140}
148 141
142static void log_file(FILE *f) {
143 char *line = NULL;
144 size_t line_size = 0;
145 ssize_t nread;
146 while ((nread = getline(&line, &line_size, f)) != -1) {
147 if (line[nread - 1] == '\n') {
148 line[nread - 1] = '\0';
149 }
150 wlr_log(WLR_INFO, "%s", line);
151 }
152 free(line);
153}
154
149static void log_distro(void) { 155static void log_distro(void) {
150 const char *paths[] = { 156 const char *paths[] = {
151 "/etc/lsb-release", 157 "/etc/lsb-release",
@@ -158,16 +164,7 @@ static void log_distro(void) {
158 FILE *f = fopen(paths[i], "r"); 164 FILE *f = fopen(paths[i], "r");
159 if (f) { 165 if (f) {
160 wlr_log(WLR_INFO, "Contents of %s:", paths[i]); 166 wlr_log(WLR_INFO, "Contents of %s:", paths[i]);
161 while (!feof(f)) { 167 log_file(f);
162 char *line;
163 if (!(line = read_line(f))) {
164 break;
165 }
166 if (*line) {
167 wlr_log(WLR_INFO, "%s", line);
168 }
169 free(line);
170 }
171 fclose(f); 168 fclose(f);
172 } 169 }
173 } 170 }
@@ -179,16 +176,7 @@ static void log_kernel(void) {
179 wlr_log(WLR_INFO, "Unable to determine kernel version"); 176 wlr_log(WLR_INFO, "Unable to determine kernel version");
180 return; 177 return;
181 } 178 }
182 while (!feof(f)) { 179 log_file(f);
183 char *line;
184 if (!(line = read_line(f))) {
185 break;
186 }
187 if (*line) {
188 wlr_log(WLR_INFO, "%s", line);
189 }
190 free(line);
191 }
192 pclose(f); 180 pclose(f);
193} 181}
194 182
diff --git a/swaybar/status_line.c b/swaybar/status_line.c
index 2e6ef173..f0e2c300 100644
--- a/swaybar/status_line.c
+++ b/swaybar/status_line.c
@@ -12,7 +12,6 @@
12#include "swaybar/config.h" 12#include "swaybar/config.h"
13#include "swaybar/i3bar.h" 13#include "swaybar/i3bar.h"
14#include "swaybar/status_line.h" 14#include "swaybar/status_line.h"
15#include "readline.h"
16 15
17static void status_line_close_fds(struct status_line *status) { 16static void status_line_close_fds(struct status_line *status) {
18 if (status->read_fd != -1) { 17 if (status->read_fd != -1) {
diff --git a/swaylock/main.c b/swaylock/main.c
index 9aeb4e64..9a4f3b58 100644
--- a/swaylock/main.c
+++ b/swaylock/main.c
@@ -23,7 +23,6 @@
23#include "cairo.h" 23#include "cairo.h"
24#include "log.h" 24#include "log.h"
25#include "loop.h" 25#include "loop.h"
26#include "readline.h"
27#include "stringop.h" 26#include "stringop.h"
28#include "util.h" 27#include "util.h"
29#include "wlr-input-inhibitor-unstable-v1-client-protocol.h" 28#include "wlr-input-inhibitor-unstable-v1-client-protocol.h"
@@ -808,36 +807,32 @@ static int load_config(char *path, struct swaylock_state *state,
808 wlr_log(WLR_ERROR, "Failed to read config. Running without it."); 807 wlr_log(WLR_ERROR, "Failed to read config. Running without it.");
809 return 0; 808 return 0;
810 } 809 }
811 char *line; 810 char *line = NULL;
811 size_t line_size = 0;
812 ssize_t nread;
812 int line_number = 0; 813 int line_number = 0;
813 while (!feof(config)) { 814 int result = 0;
814 line = read_line(config); 815 while ((nread = getline(&line, &line_size, config)) != -1) {
815 if (!line) {
816 continue;
817 }
818
819 line_number++; 816 line_number++;
820 if (line[0] == '#') { 817
821 free(line); 818 if (line[nread - 1] == '\n') {
822 continue; 819 line[--nread] = '\0';
823 } 820 }
824 if (strlen(line) == 0) { 821
825 free(line); 822 if (!*line || line[0] == '#') {
826 continue; 823 continue;
827 } 824 }
828 825
829 wlr_log(WLR_DEBUG, "Config Line #%d: %s", line_number, line); 826 wlr_log(WLR_DEBUG, "Config Line #%d: %s", line_number, line);
830 char flag[strlen(line) + 3]; 827 char flag[nread + 3];
831 sprintf(flag, "--%s", line); 828 sprintf(flag, "--%s", line);
832 char *argv[] = {"swaylock", flag}; 829 char *argv[] = {"swaylock", flag};
833 int result = parse_options(2, argv, state, line_mode, NULL); 830 result = parse_options(2, argv, state, line_mode, NULL);
834 if (result != 0) { 831 if (result != 0) {
835 free(line); 832 break;
836 fclose(config);
837 return result;
838 } 833 }
839 free(line);
840 } 834 }
835 free(line);
841 fclose(config); 836 fclose(config);
842 return 0; 837 return 0;
843} 838}
diff --git a/swaymsg/main.c b/swaymsg/main.c
index c9c557da..f1bb5e3e 100644
--- a/swaymsg/main.c
+++ b/swaymsg/main.c
@@ -12,7 +12,6 @@
12#include <json-c/json.h> 12#include <json-c/json.h>
13#include "stringop.h" 13#include "stringop.h"
14#include "ipc-client.h" 14#include "ipc-client.h"
15#include "readline.h"
16#include "log.h" 15#include "log.h"
17 16
18void sway_terminate(int exit_code) { 17void sway_terminate(int exit_code) {
diff --git a/swaynag/config.c b/swaynag/config.c
index e724aa0c..85aa380a 100644
--- a/swaynag/config.c
+++ b/swaynag/config.c
@@ -1,10 +1,10 @@
1#define _POSIX_C_SOURCE 200809L 1#define _POSIX_C_SOURCE 200809L
2#include <getopt.h> 2#include <getopt.h>
3#include <stdio.h>
3#include <stdlib.h> 4#include <stdlib.h>
4#include <wordexp.h> 5#include <wordexp.h>
5#include "log.h" 6#include "log.h"
6#include "list.h" 7#include "list.h"
7#include "readline.h"
8#include "swaynag/swaynag.h" 8#include "swaynag/swaynag.h"
9#include "swaynag/types.h" 9#include "swaynag/types.h"
10#include "util.h" 10#include "util.h"
@@ -12,21 +12,19 @@
12 12
13static char *read_from_stdin(void) { 13static char *read_from_stdin(void) {
14 char *buffer = NULL; 14 char *buffer = NULL;
15 while (!feof(stdin)) { 15 size_t buffer_len = 0;
16 char *line = read_line(stdin); 16 char *line = NULL;
17 if (!line) { 17 size_t line_size = 0;
18 continue; 18 ssize_t nread;
19 } 19 while ((nread = getline(&line, &line_size, stdin)) != -1) {
20 20 buffer = realloc(buffer, buffer_len + nread);
21 size_t curlen = buffer ? strlen(buffer) : 0; 21 snprintf(&buffer[buffer_len], nread + 1, "%s", line);
22 buffer = realloc(buffer, curlen + strlen(line) + 2); 22 buffer_len += nread;
23 snprintf(buffer + curlen, strlen(line) + 2, "%s\n", line);
24
25 free(line);
26 } 23 }
24 free(line);
27 25
28 while (buffer && buffer[strlen(buffer) - 1] == '\n') { 26 while (buffer && buffer[buffer_len - 1] == '\n') {
29 buffer[strlen(buffer) - 1] = '\0'; 27 buffer[--buffer_len] = '\0';
30 } 28 }
31 29
32 return buffer; 30 return buffer;
@@ -348,32 +346,24 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
348 type->name = strdup("<config>"); 346 type->name = strdup("<config>");
349 list_add(types, type); 347 list_add(types, type);
350 348
351 char *line; 349 char *line = NULL;
350 size_t line_size = 0;
351 ssize_t nread;
352 int line_number = 0; 352 int line_number = 0;
353 while (!feof(config)) { 353 int result = 0;
354 line = read_line(config); 354 while ((nread = getline(&line, &line_size, config)) != -1) {
355 if (!line) {
356 continue;
357 }
358
359 line_number++; 355 line_number++;
360 if (line[0] == '#') { 356 if (!*line || line[0] == '\n' || line[0] == '#') {
361 free(line);
362 continue;
363 }
364 if (strlen(line) == 0) {
365 free(line);
366 continue; 357 continue;
367 } 358 }
368 359
369 if (line[0] == '[') { 360 if (line[0] == '[') {
370 char *close = strchr(line, ']'); 361 char *close = strchr(line, ']');
371 if (!close) { 362 if (!close) {
372 free(line);
373 fclose(config);
374 fprintf(stderr, "Closing bracket not found on line %d\n", 363 fprintf(stderr, "Closing bracket not found on line %d\n",
375 line_number); 364 line_number);
376 return 1; 365 result = 1;
366 break;
377 } 367 }
378 char *name = calloc(1, close - line); 368 char *name = calloc(1, close - line);
379 strncat(name, line + 1, close - line - 1); 369 strncat(name, line + 1, close - line - 1);
@@ -385,21 +375,17 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
385 } 375 }
386 free(name); 376 free(name);
387 } else { 377 } else {
388 char flag[strlen(line) + 3]; 378 char flag[nread + 3];
389 sprintf(flag, "--%s", line); 379 sprintf(flag, "--%s", line);
390 char *argv[] = {"swaynag", flag}; 380 char *argv[] = {"swaynag", flag};
391 int result;
392 result = swaynag_parse_options(2, argv, swaynag, types, type, 381 result = swaynag_parse_options(2, argv, swaynag, types, type,
393 NULL, NULL); 382 NULL, NULL);
394 if (result != 0) { 383 if (result != 0) {
395 free(line); 384 break;
396 fclose(config);
397 return result;
398 } 385 }
399 } 386 }
400
401 free(line);
402 } 387 }
388 free(line);
403 fclose(config); 389 fclose(config);
404 return 0; 390 return result;
405} 391}