summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar emersion <contact@emersion.fr>2019-01-08 10:05:37 +0100
committerLibravatar GitHub <noreply@github.com>2019-01-08 10:05:37 +0100
commit140bc2dd5b81205df58bf06e695788e689fae397 (patch)
tree6a88913630734736763b12ec0b10da68ef413256
parentMerge pull request #3337 from RedSoxFan/fix-seat-cmd-cursor (diff)
parentfixup! stringop.c: rewrite strip_whitespace (diff)
downloadsway-140bc2dd5b81205df58bf06e695788e689fae397.tar.gz
sway-140bc2dd5b81205df58bf06e695788e689fae397.tar.zst
sway-140bc2dd5b81205df58bf06e695788e689fae397.zip
Merge pull request #3275 from ianyfan/remove-readline
Rewrite strip_whitespace and remove readline.c
-rw-r--r--common/ipc-client.c11
-rw-r--r--common/meson.build1
-rw-r--r--common/readline.c72
-rw-r--r--common/stringop.c25
-rw-r--r--common/util.c4
-rw-r--r--include/readline.h10
-rw-r--r--include/stringop.h5
-rw-r--r--sway/commands.c6
-rw-r--r--sway/config.c124
-rw-r--r--sway/input/input-manager.c2
-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
15 files changed, 156 insertions, 269 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/stringop.c b/common/stringop.c
index 4b8c9a38..8af0d60f 100644
--- a/common/stringop.c
+++ b/common/stringop.c
@@ -9,24 +9,17 @@
9#include "string.h" 9#include "string.h"
10#include "list.h" 10#include "list.h"
11 11
12const char whitespace[] = " \f\n\r\t\v"; 12static const char whitespace[] = " \f\n\r\t\v";
13 13
14char *strip_whitespace(char *_str) { 14void strip_whitespace(char *str) {
15 if (*_str == '\0') 15 size_t len = strlen(str);
16 return _str; 16 size_t start = strspn(str, whitespace);
17 char *strold = _str; 17 memmove(str, &str[start], len + 1 - start);
18 while (*_str == ' ' || *_str == '\t') { 18
19 _str++; 19 if (*str) {
20 for (len -= start + 1; isspace(str[len]); --len) {}
21 str[len + 1] = '\0';
20 } 22 }
21 char *str = strdup(_str);
22 free(strold);
23 int i;
24 for (i = 0; str[i] != '\0'; ++i);
25 do {
26 i--;
27 } while (i >= 0 && (str[i] == ' ' || str[i] == '\t'));
28 str[i + 1] = '\0';
29 return str;
30} 23}
31 24
32void strip_quotes(char *str) { 25void strip_quotes(char *str) {
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/include/stringop.h b/include/stringop.h
index d1bfa29d..f7ca60a5 100644
--- a/include/stringop.h
+++ b/include/stringop.h
@@ -3,10 +3,7 @@
3 3
4#include "list.h" 4#include "list.h"
5 5
6// array of whitespace characters to use for delims 6void strip_whitespace(char *str);
7extern const char whitespace[];
8
9char *strip_whitespace(char *str);
10char *strip_comments(char *str); 7char *strip_comments(char *str);
11void strip_quotes(char *str); 8void strip_quotes(char *str);
12 9
diff --git a/sway/commands.c b/sway/commands.c
index 0883b57b..4e524a88 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -237,15 +237,15 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
237 criteria_destroy(criteria); 237 criteria_destroy(criteria);
238 config->handler_context.using_criteria = true; 238 config->handler_context.using_criteria = true;
239 // Skip leading whitespace 239 // Skip leading whitespace
240 head += strspn(head, whitespace); 240 for (; isspace(*head); ++head) {}
241 } 241 }
242 // Split command list 242 // Split command list
243 cmdlist = argsep(&head, ";"); 243 cmdlist = argsep(&head, ";");
244 cmdlist += strspn(cmdlist, whitespace); 244 for (; isspace(*cmdlist); ++cmdlist) {}
245 do { 245 do {
246 // Split commands 246 // Split commands
247 cmd = argsep(&cmdlist, ","); 247 cmd = argsep(&cmdlist, ",");
248 cmd += strspn(cmd, whitespace); 248 for (; isspace(*cmd); ++cmd) {}
249 if (strcmp(cmd, "") == 0) { 249 if (strcmp(cmd, "") == 0) {
250 wlr_log(WLR_INFO, "Ignoring empty command."); 250 wlr_log(WLR_INFO, "Ignoring empty command.");
251 continue; 251 continue;
diff --git a/sway/config.c b/sway/config.c
index 5d631b7e..8a0b293c 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"
@@ -571,28 +570,49 @@ bool load_include_configs(const char *path, struct sway_config *config,
571 return true; 570 return true;
572} 571}
573 572
574static int detect_brace_on_following_line(FILE *file, char *line, 573// get line, with backslash continuation
575 int line_number) { 574static ssize_t getline_with_cont(char **lineptr, size_t *line_size, FILE *file) {
576 int lines = 0; 575 char *next_line = NULL;
577 if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { 576 size_t next_line_size = 0;
578 char *peeked = NULL; 577 ssize_t nread = getline(lineptr, line_size, file);
579 long position = 0; 578 while (nread >= 2 && strcmp(&(*lineptr)[nread - 2], "\\\n") == 0) {
580 do { 579 ssize_t next_nread = getline(&next_line, &next_line_size, file);
581 free(peeked); 580 if (next_nread == -1) {
582 peeked = peek_line(file, lines, &position); 581 break;
583 if (peeked) { 582 }
584 peeked = strip_whitespace(peeked); 583
584 nread += next_nread - 2;
585 if ((ssize_t) *line_size < nread + 1) {
586 *line_size = nread + 1;
587 *lineptr = realloc(*lineptr, *line_size);
588 if (!*lineptr) {
589 nread = -1;
590 break;
585 } 591 }
586 lines++; 592 }
587 } while (peeked && strlen(peeked) == 0); 593 strcpy(&(*lineptr)[nread - next_nread], next_line);
594 }
595 free(next_line);
596 return nread;
597}
588 598
589 if (peeked && strlen(peeked) == 1 && peeked[0] == '{') { 599static int detect_brace(FILE *file) {
590 fseek(file, position, SEEK_SET); 600 int lines = 0;
591 } else { 601 long pos = ftell(file);
592 lines = 0; 602 char *line = NULL;
603 size_t line_size = 0;
604 while ((getline(&line, &line_size, file)) != -1) {
605 lines++;
606 strip_whitespace(line);
607 if (*line) {
608 if (strcmp(line, "{") != 0) {
609 fseek(file, pos, SEEK_SET);
610 lines = 0;
611 }
612 break;
593 } 613 }
594 free(peeked);
595 } 614 }
615 free(line);
596 return lines; 616 return lines;
597} 617}
598 618
@@ -635,55 +655,47 @@ bool read_config(FILE *file, struct sway_config *config,
635 655
636 bool success = true; 656 bool success = true;
637 int line_number = 0; 657 int line_number = 0;
638 char *line; 658 char *line = NULL;
659 size_t line_size = 0;
660 ssize_t nread;
639 list_t *stack = create_list(); 661 list_t *stack = create_list();
640 size_t read = 0; 662 size_t read = 0;
641 while (!feof(file)) { 663 while ((nread = getline_with_cont(&line, &line_size, file)) != -1) {
642 char *block = stack->length ? stack->items[0] : NULL;
643 line = read_line(file);
644 if (!line) {
645 continue;
646 }
647 line_number++;
648 wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line);
649
650 if (reading_main_config) { 664 if (reading_main_config) {
651 size_t length = strlen(line); 665 if (read + nread > config_size) {
652
653 if (read + length > config_size) {
654 wlr_log(WLR_ERROR, "Config file changed during reading"); 666 wlr_log(WLR_ERROR, "Config file changed during reading");
655 list_free_items_and_destroy(stack); 667 success = false;
656 free(line); 668 break;
657 return false;
658 } 669 }
659 670
660 strcpy(this_config + read, line); 671 strcpy(&this_config[read], line);
661 if (line_number != 1) { 672 read += nread;
662 this_config[read - 1] = '\n';
663 }
664 read += length + 1;
665 } 673 }
666 674
667 line = strip_whitespace(line); 675 if (line[nread - 1] == '\n') {
668 if (line[0] == '#') { 676 line[nread - 1] = '\0';
669 free(line);
670 continue;
671 } 677 }
672 if (strlen(line) == 0) { 678
673 free(line); 679 line_number++;
680 wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line);
681
682 strip_whitespace(line);
683 if (!*line || line[0] == '#') {
674 continue; 684 continue;
675 } 685 }
676 int brace_detected = detect_brace_on_following_line(file, line, 686 int brace_detected = 0;
677 line_number); 687 if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') {
678 if (brace_detected > 0) { 688 brace_detected = detect_brace(file);
679 line_number += brace_detected; 689 if (brace_detected > 0) {
680 wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number); 690 line_number += brace_detected;
691 wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number);
692 }
681 } 693 }
694 char *block = stack->length ? stack->items[0] : NULL;
682 char *expanded = expand_line(block, line, brace_detected > 0); 695 char *expanded = expand_line(block, line, brace_detected > 0);
683 if (!expanded) { 696 if (!expanded) {
684 list_free_items_and_destroy(stack); 697 success = false;
685 free(line); 698 break;
686 return false;
687 } 699 }
688 config->current_config_line_number = line_number; 700 config->current_config_line_number = line_number;
689 config->current_config_line = line; 701 config->current_config_line = line;
@@ -743,9 +755,9 @@ bool read_config(FILE *file, struct sway_config *config,
743 default:; 755 default:;
744 } 756 }
745 free(expanded); 757 free(expanded);
746 free(line);
747 free_cmd_results(res); 758 free_cmd_results(res);
748 } 759 }
760 free(line);
749 list_free_items_and_destroy(stack); 761 list_free_items_and_destroy(stack);
750 config->current_config_line_number = 0; 762 config->current_config_line_number = 0;
751 config->current_config_line = NULL; 763 config->current_config_line = NULL;
diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c
index 61087733..04e14355 100644
--- a/sway/input/input-manager.c
+++ b/sway/input/input-manager.c
@@ -49,7 +49,7 @@ char *input_device_get_identifier(struct wlr_input_device *device) {
49 int vendor = device->vendor; 49 int vendor = device->vendor;
50 int product = device->product; 50 int product = device->product;
51 char *name = strdup(device->name); 51 char *name = strdup(device->name);
52 name = strip_whitespace(name); 52 strip_whitespace(name);
53 53
54 char *p = name; 54 char *p = name;
55 for (; *p; ++p) { 55 for (; *p; ++p) {
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}