From 967566e37f93890bd5255725129c929aeeac709e Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Sun, 9 Dec 2018 11:52:55 +0000 Subject: stringop.c: rewrite strip_whitespace --- sway/commands.c | 6 +++--- sway/config.c | 4 ++-- sway/input/input-manager.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'sway') diff --git a/sway/commands.c b/sway/commands.c index 927434bc..cd595b03 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -236,15 +236,15 @@ list_t *execute_command(char *_exec, struct sway_seat *seat, criteria_destroy(criteria); config->handler_context.using_criteria = true; // Skip leading whitespace - head += strspn(head, whitespace); + for (; isspace(*head); ++head) {} } // Split command list cmdlist = argsep(&head, ";"); - cmdlist += strspn(cmdlist, whitespace); + for (; isspace(*cmdlist); ++cmdlist) {} do { // Split commands cmd = argsep(&cmdlist, ","); - cmd += strspn(cmd, whitespace); + for (; isspace(*cmd); ++cmd) {} if (strcmp(cmd, "") == 0) { wlr_log(WLR_INFO, "Ignoring empty command."); continue; diff --git a/sway/config.c b/sway/config.c index bb7f796d..c71f315a 100644 --- a/sway/config.c +++ b/sway/config.c @@ -580,7 +580,7 @@ static int detect_brace_on_following_line(FILE *file, char *line, free(peeked); peeked = peek_line(file, lines, &position); if (peeked) { - peeked = strip_whitespace(peeked); + strip_whitespace(peeked); } lines++; } while (peeked && strlen(peeked) == 0); @@ -663,7 +663,7 @@ bool read_config(FILE *file, struct sway_config *config, read += length + 1; } - line = strip_whitespace(line); + strip_whitespace(line); if (line[0] == '#') { free(line); continue; 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) { int vendor = device->vendor; int product = device->product; char *name = strdup(device->name); - name = strip_whitespace(name); + strip_whitespace(name); char *p = name; for (; *p; ++p) { -- cgit v1.2.3 From a82b8a3c14e45697708e57f8cb27a8fc6cf31839 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Sun, 9 Dec 2018 12:09:11 +0000 Subject: Remove readline.c All occurrences of read_line have been replaced by getline. peek_line has been absorbed into detect_brace. --- common/ipc-client.c | 11 +++--- common/meson.build | 1 - common/readline.c | 72 ------------------------------------- common/util.c | 4 +-- include/readline.h | 10 ------ sway/config.c | 98 ++++++++++++++++++++++----------------------------- sway/main.c | 68 +++++++++++++++-------------------- swaybar/status_line.c | 1 - swaylock/main.c | 33 ++++++++--------- swaymsg/main.c | 1 - swaynag/config.c | 62 +++++++++++++------------------- 11 files changed, 116 insertions(+), 245 deletions(-) delete mode 100644 common/readline.c delete mode 100644 include/readline.h (limited to 'sway') 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 @@ #include #include #include "ipc-client.h" -#include "readline.h" #include "log.h" static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; @@ -18,28 +17,30 @@ char *get_socketpath(void) { if (swaysock) { return strdup(swaysock); } + char *line = NULL; + size_t line_size = 0; FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r"); if (fp) { - char *line = read_line(fp); + getline(&line, &line_size, fp); pclose(fp); if (line && *line) { return line; } - free(line); } const char *i3sock = getenv("I3SOCK"); if (i3sock) { + free(line); return strdup(i3sock); } fp = popen("i3 --get-socketpath 2>/dev/null", "r"); if (fp) { - char *line = read_line(fp); + getline(&line, &line_size, fp); pclose(fp); if (line && *line) { return line; } - free(line); } + free(line); return NULL; } 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( 'loop.c', 'list.c', 'pango.c', - 'readline.c', 'stringop.c', 'unicode.c', '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 @@ -#define _POSIX_C_SOURCE 200809L -#include "readline.h" -#include "log.h" -#include -#include - -char *read_line(FILE *file) { - size_t length = 0, size = 128; - char *string = malloc(size); - char lastChar = '\0'; - if (!string) { - wlr_log(WLR_ERROR, "Unable to allocate memory for read_line"); - return NULL; - } - while (1) { - int c = getc(file); - if (c == '\n' && lastChar == '\\'){ - --length; // Ignore last character. - lastChar = '\0'; - continue; - } - if (c == EOF || c == '\n' || c == '\0') { - break; - } - if (c == '\r') { - continue; - } - lastChar = c; - if (length == size) { - char *new_string = realloc(string, size *= 2); - if (!new_string) { - free(string); - wlr_log(WLR_ERROR, "Unable to allocate memory for read_line"); - return NULL; - } - string = new_string; - } - string[length++] = c; - } - if (length + 1 == size) { - char *new_string = realloc(string, length + 1); - if (!new_string) { - free(string); - return NULL; - } - string = new_string; - } - string[length] = '\0'; - return string; -} - -char *peek_line(FILE *file, int line_offset, long *position) { - long pos = ftell(file); - size_t length = 0; - char *line = NULL; - for (int i = 0; i <= line_offset; i++) { - ssize_t read = getline(&line, &length, file); - if (read < 0) { - free(line); - line = NULL; - break; - } - if (read > 0 && line[read - 1] == '\n') { - line[read - 1] = '\0'; - } - } - if (position) { - *position = ftell(file); - } - fseek(file, pos, SEEK_SET); - return line; -} 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 @@ #include #include #include "log.h" -#include "readline.h" #include "util.h" int wrap(int i, int max) { @@ -87,11 +86,12 @@ pid_t get_parent_pid(pid_t child) { char *token = NULL; const char *sep = " "; FILE *stat = NULL; + size_t buf_size = 0; sprintf(file_name, "/proc/%d/stat", child); if ((stat = fopen(file_name, "r"))) { - if ((buffer = read_line(stat))) { + if (getline(&buffer, &buf_size, stat) != -1) { token = strtok(buffer, sep); // pid token = strtok(NULL, sep); // executable name 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 @@ -#ifndef _SWAY_READLINE_H -#define _SWAY_READLINE_H - -#include - -char *read_line(FILE *file); -char *peek_line(FILE *file, int line_offset, long *position); -char *read_line_buffer(FILE *file, char *string, size_t string_len); - -#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 @@ -#define _XOPEN_SOURCE 600 // for realpath +#define _XOPEN_SOURCE 700 // for realpath #include #include #include @@ -30,7 +30,6 @@ #include "sway/tree/workspace.h" #include "cairo.h" #include "pango.h" -#include "readline.h" #include "stringop.h" #include "list.h" #include "log.h" @@ -570,28 +569,23 @@ bool load_include_configs(const char *path, struct sway_config *config, return true; } -static int detect_brace_on_following_line(FILE *file, char *line, - int line_number) { +static int detect_brace(FILE *file) { int lines = 0; - if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { - char *peeked = NULL; - long position = 0; - do { - free(peeked); - peeked = peek_line(file, lines, &position); - if (peeked) { - strip_whitespace(peeked); + long pos = ftell(file); + char *line = NULL; + size_t line_size = 0; + while ((getline(&line, &line_size, file)) != -1) { + lines++; + strip_whitespace(line); + if (*line) { + if (strcmp(line, "{") != 0) { + fseek(file, pos, SEEK_SET); + lines = 0; } - lines++; - } while (peeked && strlen(peeked) == 0); - - if (peeked && strlen(peeked) == 1 && peeked[0] == '{') { - fseek(file, position, SEEK_SET); - } else { - lines = 0; + break; } - free(peeked); } + free(line); return lines; } @@ -634,55 +628,47 @@ bool read_config(FILE *file, struct sway_config *config, bool success = true; int line_number = 0; - char *line; + char *line = NULL; + size_t line_size = 0; + ssize_t nread; list_t *stack = create_list(); size_t read = 0; - while (!feof(file)) { - char *block = stack->length ? stack->items[0] : NULL; - line = read_line(file); - if (!line) { - continue; - } - line_number++; - wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); - + while ((nread = getline(&line, &line_size, file)) != -1) { if (reading_main_config) { - size_t length = strlen(line); - - if (read + length > config_size) { + if (read + nread > config_size) { wlr_log(WLR_ERROR, "Config file changed during reading"); - list_free_items_and_destroy(stack); - free(line); - return false; + success = false; + break; } - strcpy(this_config + read, line); - if (line_number != 1) { - this_config[read - 1] = '\n'; - } - read += length + 1; + strcpy(&this_config[read], line); + read += nread; } - strip_whitespace(line); - if (line[0] == '#') { - free(line); - continue; + if (line[nread - 1] == '\n') { + line[nread - 1] = '\0'; } - if (strlen(line) == 0) { - free(line); + + line_number++; + wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); + + strip_whitespace(line); + if (!*line || line[0] == '#') { continue; } - int brace_detected = detect_brace_on_following_line(file, line, - line_number); - if (brace_detected > 0) { - line_number += brace_detected; - wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number); + int brace_detected = 0; + if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { + brace_detected = detect_brace(file); + if (brace_detected > 0) { + line_number += brace_detected; + wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number); + } } + char *block = stack->length ? stack->items[0] : NULL; char *expanded = expand_line(block, line, brace_detected > 0); if (!expanded) { - list_free_items_and_destroy(stack); - free(line); - return false; + success = false; + break; } config->current_config_line_number = line_number; config->current_config_line = line; @@ -742,9 +728,9 @@ bool read_config(FILE *file, struct sway_config *config, default:; } free(expanded); - free(line); free_cmd_results(res); } + free(line); list_free_items_and_destroy(stack); config->current_config_line_number = 0; 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 @@ #include #include #include +#include #include #include #include @@ -22,7 +23,6 @@ #include "sway/ipc-server.h" #include "ipc-client.h" #include "log.h" -#include "readline.h" #include "stringop.h" #include "util.h" @@ -47,31 +47,28 @@ void detect_raspi(void) { if (!f) { return; } - char *line; - while(!feof(f)) { - if (!(line = read_line(f))) { - break; - } + char *line = NULL; + size_t line_size = 0; + while (getline(&line, &line_size, f) != -1) { if (strstr(line, "Raspberry Pi")) { raspi = true; + break; } - free(line); } fclose(f); FILE *g = fopen("/proc/modules", "r"); if (!g) { + free(line); return; } bool vc4 = false; - while (!feof(g)) { - if (!(line = read_line(g))) { - break; - } + while (getline(&line, &line_size, g) != -1) { if (strstr(line, "vc4")) { vc4 = true; + break; } - free(line); } + free(line); fclose(g); if (!vc4 && raspi) { fprintf(stderr, "\x1B[1;31mWarning: You have a " @@ -86,13 +83,10 @@ void detect_proprietary(int allow_unsupported_gpu) { if (!f) { return; } - while (!feof(f)) { - char *line; - if (!(line = read_line(f))) { - break; - } + char *line = NULL; + size_t line_size = 0; + while (getline(&line, &line_size, f) != -1) { if (strstr(line, "nvidia")) { - free(line); if (allow_unsupported_gpu) { wlr_log(WLR_ERROR, "!!! Proprietary Nvidia drivers are in use !!!"); @@ -106,7 +100,6 @@ void detect_proprietary(int allow_unsupported_gpu) { break; } if (strstr(line, "fglrx")) { - free(line); if (allow_unsupported_gpu) { wlr_log(WLR_ERROR, "!!! Proprietary AMD drivers are in use !!!"); @@ -118,8 +111,8 @@ void detect_proprietary(int allow_unsupported_gpu) { } break; } - free(line); } + free(line); fclose(f); } @@ -146,6 +139,19 @@ static void log_env(void) { } } +static void log_file(FILE *f) { + char *line = NULL; + size_t line_size = 0; + ssize_t nread; + while ((nread = getline(&line, &line_size, f)) != -1) { + if (line[nread - 1] == '\n') { + line[nread - 1] = '\0'; + } + wlr_log(WLR_INFO, "%s", line); + } + free(line); +} + static void log_distro(void) { const char *paths[] = { "/etc/lsb-release", @@ -158,16 +164,7 @@ static void log_distro(void) { FILE *f = fopen(paths[i], "r"); if (f) { wlr_log(WLR_INFO, "Contents of %s:", paths[i]); - while (!feof(f)) { - char *line; - if (!(line = read_line(f))) { - break; - } - if (*line) { - wlr_log(WLR_INFO, "%s", line); - } - free(line); - } + log_file(f); fclose(f); } } @@ -179,16 +176,7 @@ static void log_kernel(void) { wlr_log(WLR_INFO, "Unable to determine kernel version"); return; } - while (!feof(f)) { - char *line; - if (!(line = read_line(f))) { - break; - } - if (*line) { - wlr_log(WLR_INFO, "%s", line); - } - free(line); - } + log_file(f); pclose(f); } 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 @@ #include "swaybar/config.h" #include "swaybar/i3bar.h" #include "swaybar/status_line.h" -#include "readline.h" static void status_line_close_fds(struct status_line *status) { 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 @@ #include "cairo.h" #include "log.h" #include "loop.h" -#include "readline.h" #include "stringop.h" #include "util.h" #include "wlr-input-inhibitor-unstable-v1-client-protocol.h" @@ -808,36 +807,32 @@ static int load_config(char *path, struct swaylock_state *state, wlr_log(WLR_ERROR, "Failed to read config. Running without it."); return 0; } - char *line; + char *line = NULL; + size_t line_size = 0; + ssize_t nread; int line_number = 0; - while (!feof(config)) { - line = read_line(config); - if (!line) { - continue; - } - + int result = 0; + while ((nread = getline(&line, &line_size, config)) != -1) { line_number++; - if (line[0] == '#') { - free(line); - continue; + + if (line[nread - 1] == '\n') { + line[--nread] = '\0'; } - if (strlen(line) == 0) { - free(line); + + if (!*line || line[0] == '#') { continue; } wlr_log(WLR_DEBUG, "Config Line #%d: %s", line_number, line); - char flag[strlen(line) + 3]; + char flag[nread + 3]; sprintf(flag, "--%s", line); char *argv[] = {"swaylock", flag}; - int result = parse_options(2, argv, state, line_mode, NULL); + result = parse_options(2, argv, state, line_mode, NULL); if (result != 0) { - free(line); - fclose(config); - return result; + break; } - free(line); } + free(line); fclose(config); return 0; } 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 @@ #include #include "stringop.h" #include "ipc-client.h" -#include "readline.h" #include "log.h" void 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 @@ #define _POSIX_C_SOURCE 200809L #include +#include #include #include #include "log.h" #include "list.h" -#include "readline.h" #include "swaynag/swaynag.h" #include "swaynag/types.h" #include "util.h" @@ -12,21 +12,19 @@ static char *read_from_stdin(void) { char *buffer = NULL; - while (!feof(stdin)) { - char *line = read_line(stdin); - if (!line) { - continue; - } - - size_t curlen = buffer ? strlen(buffer) : 0; - buffer = realloc(buffer, curlen + strlen(line) + 2); - snprintf(buffer + curlen, strlen(line) + 2, "%s\n", line); - - free(line); + size_t buffer_len = 0; + char *line = NULL; + size_t line_size = 0; + ssize_t nread; + while ((nread = getline(&line, &line_size, stdin)) != -1) { + buffer = realloc(buffer, buffer_len + nread); + snprintf(&buffer[buffer_len], nread + 1, "%s", line); + buffer_len += nread; } + free(line); - while (buffer && buffer[strlen(buffer) - 1] == '\n') { - buffer[strlen(buffer) - 1] = '\0'; + while (buffer && buffer[buffer_len - 1] == '\n') { + buffer[--buffer_len] = '\0'; } return buffer; @@ -348,32 +346,24 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { type->name = strdup(""); list_add(types, type); - char *line; + char *line = NULL; + size_t line_size = 0; + ssize_t nread; int line_number = 0; - while (!feof(config)) { - line = read_line(config); - if (!line) { - continue; - } - + int result = 0; + while ((nread = getline(&line, &line_size, config)) != -1) { line_number++; - if (line[0] == '#') { - free(line); - continue; - } - if (strlen(line) == 0) { - free(line); + if (!*line || line[0] == '\n' || line[0] == '#') { continue; } if (line[0] == '[') { char *close = strchr(line, ']'); if (!close) { - free(line); - fclose(config); fprintf(stderr, "Closing bracket not found on line %d\n", line_number); - return 1; + result = 1; + break; } char *name = calloc(1, close - line); strncat(name, line + 1, close - line - 1); @@ -385,21 +375,17 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { } free(name); } else { - char flag[strlen(line) + 3]; + char flag[nread + 3]; sprintf(flag, "--%s", line); char *argv[] = {"swaynag", flag}; - int result; result = swaynag_parse_options(2, argv, swaynag, types, type, NULL, NULL); if (result != 0) { - free(line); - fclose(config); - return result; + break; } } - - free(line); } + free(line); fclose(config); - return 0; + return result; } -- cgit v1.2.3 From a9c5158a77cc53a526ea0b16d99e476bd960e4a5 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Mon, 31 Dec 2018 23:00:49 +0000 Subject: config.c: re-enable backslash continuation in config file --- sway/config.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'sway') diff --git a/sway/config.c b/sway/config.c index 4afa09b3..4177ea10 100644 --- a/sway/config.c +++ b/sway/config.c @@ -569,6 +569,36 @@ bool load_include_configs(const char *path, struct sway_config *config, return true; } +// get line, with backslash continuation +static ssize_t getline_with_cont(char **lineptr, size_t *line_size, FILE *file) { + char *next_line = NULL; + size_t next_line_size = 0; + ssize_t nread = getline(lineptr, line_size, file); + while (nread >= 2) { + if (strcmp(&(*lineptr)[nread - 2], "\\\n") != 0) { + break; + } + + ssize_t next_nread = getline(&next_line, &next_line_size, file); + if (next_nread == -1) { + break; + } + + nread += next_nread - 2; + if ((ssize_t) *line_size < nread + 1) { + *line_size = nread + 1; + *lineptr = realloc(*lineptr, *line_size); + if (!lineptr) { + nread = -1; + break; + } + } + strcpy(&(*lineptr)[nread - next_nread], next_line); + } + free(next_line); + return nread; +} + static int detect_brace(FILE *file) { int lines = 0; long pos = ftell(file); @@ -633,7 +663,7 @@ bool read_config(FILE *file, struct sway_config *config, ssize_t nread; list_t *stack = create_list(); size_t read = 0; - while ((nread = getline(&line, &line_size, file)) != -1) { + while ((nread = getline_with_cont(&line, &line_size, file)) != -1) { if (reading_main_config) { if (read + nread > config_size) { wlr_log(WLR_ERROR, "Config file changed during reading"); -- cgit v1.2.3 From bd35879e1cfda51accc68c9890980dce59eb006a Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Tue, 1 Jan 2019 09:01:09 +0000 Subject: fixup! config.c: re-enable backslash continuation in config file --- sway/config.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'sway') diff --git a/sway/config.c b/sway/config.c index 4177ea10..9f32d44c 100644 --- a/sway/config.c +++ b/sway/config.c @@ -574,11 +574,7 @@ static ssize_t getline_with_cont(char **lineptr, size_t *line_size, FILE *file) char *next_line = NULL; size_t next_line_size = 0; ssize_t nread = getline(lineptr, line_size, file); - while (nread >= 2) { - if (strcmp(&(*lineptr)[nread - 2], "\\\n") != 0) { - break; - } - + while (nread >= 2 && strcmp(&(*lineptr)[nread - 2], "\\\n") == 0) { ssize_t next_nread = getline(&next_line, &next_line_size, file); if (next_nread == -1) { break; @@ -588,7 +584,7 @@ static ssize_t getline_with_cont(char **lineptr, size_t *line_size, FILE *file) if ((ssize_t) *line_size < nread + 1) { *line_size = nread + 1; *lineptr = realloc(*lineptr, *line_size); - if (!lineptr) { + if (!*lineptr) { nread = -1; break; } -- cgit v1.2.3