aboutsummaryrefslogtreecommitdiffstats
path: root/sway
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 /sway
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.
Diffstat (limited to 'sway')
-rw-r--r--sway/config.c98
-rw-r--r--sway/main.c68
2 files changed, 70 insertions, 96 deletions
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