Remove readline.c

All occurrences of read_line have been replaced by getline.
peek_line has been absorbed into detect_brace.
This commit is contained in:
Ian Fan 2018-12-09 12:09:11 +00:00
parent 967566e37f
commit a82b8a3c14
11 changed files with 118 additions and 247 deletions

View file

@ -7,7 +7,6 @@
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include "ipc-client.h" #include "ipc-client.h"
#include "readline.h"
#include "log.h" #include "log.h"
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
@ -18,28 +17,30 @@ char *get_socketpath(void) {
if (swaysock) { if (swaysock) {
return strdup(swaysock); return strdup(swaysock);
} }
char *line = NULL;
size_t line_size = 0;
FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r"); FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r");
if (fp) { if (fp) {
char *line = read_line(fp); getline(&line, &line_size, fp);
pclose(fp); pclose(fp);
if (line && *line) { if (line && *line) {
return line; return line;
} }
free(line);
} }
const char *i3sock = getenv("I3SOCK"); const char *i3sock = getenv("I3SOCK");
if (i3sock) { if (i3sock) {
free(line);
return strdup(i3sock); return strdup(i3sock);
} }
fp = popen("i3 --get-socketpath 2>/dev/null", "r"); fp = popen("i3 --get-socketpath 2>/dev/null", "r");
if (fp) { if (fp) {
char *line = read_line(fp); getline(&line, &line_size, fp);
pclose(fp); pclose(fp);
if (line && *line) { if (line && *line) {
return line; return line;
} }
free(line);
} }
free(line);
return NULL; return NULL;
} }

View file

@ -8,7 +8,6 @@ lib_sway_common = static_library(
'loop.c', 'loop.c',
'list.c', 'list.c',
'pango.c', 'pango.c',
'readline.c',
'stringop.c', 'stringop.c',
'unicode.c', 'unicode.c',
'util.c' 'util.c'

View file

@ -1,72 +0,0 @@
#define _POSIX_C_SOURCE 200809L
#include "readline.h"
#include "log.h"
#include <stdlib.h>
#include <stdio.h>
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;
}

View file

@ -13,7 +13,6 @@
#include <xkbcommon/xkbcommon-names.h> #include <xkbcommon/xkbcommon-names.h>
#include <wlr/types/wlr_keyboard.h> #include <wlr/types/wlr_keyboard.h>
#include "log.h" #include "log.h"
#include "readline.h"
#include "util.h" #include "util.h"
int wrap(int i, int max) { int wrap(int i, int max) {
@ -87,11 +86,12 @@ pid_t get_parent_pid(pid_t child) {
char *token = NULL; char *token = NULL;
const char *sep = " "; const char *sep = " ";
FILE *stat = NULL; FILE *stat = NULL;
size_t buf_size = 0;
sprintf(file_name, "/proc/%d/stat", child); sprintf(file_name, "/proc/%d/stat", child);
if ((stat = fopen(file_name, "r"))) { 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(buffer, sep); // pid
token = strtok(NULL, sep); // executable name token = strtok(NULL, sep); // executable name
token = strtok(NULL, sep); // state token = strtok(NULL, sep); // state

View file

@ -1,10 +0,0 @@
#ifndef _SWAY_READLINE_H
#define _SWAY_READLINE_H
#include <stdio.h>
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

View file

@ -1,4 +1,4 @@
#define _XOPEN_SOURCE 600 // for realpath #define _XOPEN_SOURCE 700 // for realpath
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
@ -30,7 +30,6 @@
#include "sway/tree/workspace.h" #include "sway/tree/workspace.h"
#include "cairo.h" #include "cairo.h"
#include "pango.h" #include "pango.h"
#include "readline.h"
#include "stringop.h" #include "stringop.h"
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
@ -570,28 +569,23 @@ bool load_include_configs(const char *path, struct sway_config *config,
return true; return true;
} }
static int detect_brace_on_following_line(FILE *file, char *line, static int detect_brace(FILE *file) {
int line_number) {
int lines = 0; int lines = 0;
if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') { long pos = ftell(file);
char *peeked = NULL; char *line = NULL;
long position = 0; size_t line_size = 0;
do { while ((getline(&line, &line_size, file)) != -1) {
free(peeked); lines++;
peeked = peek_line(file, lines, &position); strip_whitespace(line);
if (peeked) { if (*line) {
strip_whitespace(peeked); if (strcmp(line, "{") != 0) {
fseek(file, pos, SEEK_SET);
lines = 0;
} }
lines++; break;
} while (peeked && strlen(peeked) == 0);
if (peeked && strlen(peeked) == 1 && peeked[0] == '{') {
fseek(file, position, SEEK_SET);
} else {
lines = 0;
} }
free(peeked);
} }
free(line);
return lines; return lines;
} }
@ -634,55 +628,47 @@ bool read_config(FILE *file, struct sway_config *config,
bool success = true; bool success = true;
int line_number = 0; int line_number = 0;
char *line; char *line = NULL;
size_t line_size = 0;
ssize_t nread;
list_t *stack = create_list(); list_t *stack = create_list();
size_t read = 0; size_t read = 0;
while (!feof(file)) { while ((nread = getline(&line, &line_size, file)) != -1) {
char *block = stack->length ? stack->items[0] : NULL; if (reading_main_config) {
line = read_line(file); if (read + nread > config_size) {
if (!line) { wlr_log(WLR_ERROR, "Config file changed during reading");
continue; success = false;
break;
}
strcpy(&this_config[read], line);
read += nread;
} }
if (line[nread - 1] == '\n') {
line[nread - 1] = '\0';
}
line_number++; line_number++;
wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line); wlr_log(WLR_DEBUG, "Read line %d: %s", line_number, line);
if (reading_main_config) {
size_t length = strlen(line);
if (read + length > config_size) {
wlr_log(WLR_ERROR, "Config file changed during reading");
list_free_items_and_destroy(stack);
free(line);
return false;
}
strcpy(this_config + read, line);
if (line_number != 1) {
this_config[read - 1] = '\n';
}
read += length + 1;
}
strip_whitespace(line); strip_whitespace(line);
if (line[0] == '#') { if (!*line || line[0] == '#') {
free(line);
continue; continue;
} }
if (strlen(line) == 0) { int brace_detected = 0;
free(line); if (line[strlen(line) - 1] != '{' && line[strlen(line) - 1] != '}') {
continue; brace_detected = detect_brace(file);
} if (brace_detected > 0) {
int brace_detected = detect_brace_on_following_line(file, line, line_number += brace_detected;
line_number); wlr_log(WLR_DEBUG, "Detected open brace on line %d", line_number);
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); char *expanded = expand_line(block, line, brace_detected > 0);
if (!expanded) { if (!expanded) {
list_free_items_and_destroy(stack); success = false;
free(line); break;
return false;
} }
config->current_config_line_number = line_number; config->current_config_line_number = line_number;
config->current_config_line = line; config->current_config_line = line;
@ -742,9 +728,9 @@ bool read_config(FILE *file, struct sway_config *config,
default:; default:;
} }
free(expanded); free(expanded);
free(line);
free_cmd_results(res); free_cmd_results(res);
} }
free(line);
list_free_items_and_destroy(stack); list_free_items_and_destroy(stack);
config->current_config_line_number = 0; config->current_config_line_number = 0;
config->current_config_line = NULL; config->current_config_line = NULL;

View file

@ -3,6 +3,7 @@
#include <pango/pangocairo.h> #include <pango/pangocairo.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -22,7 +23,6 @@
#include "sway/ipc-server.h" #include "sway/ipc-server.h"
#include "ipc-client.h" #include "ipc-client.h"
#include "log.h" #include "log.h"
#include "readline.h"
#include "stringop.h" #include "stringop.h"
#include "util.h" #include "util.h"
@ -47,31 +47,28 @@ void detect_raspi(void) {
if (!f) { if (!f) {
return; return;
} }
char *line; char *line = NULL;
while(!feof(f)) { size_t line_size = 0;
if (!(line = read_line(f))) { while (getline(&line, &line_size, f) != -1) {
break;
}
if (strstr(line, "Raspberry Pi")) { if (strstr(line, "Raspberry Pi")) {
raspi = true; raspi = true;
break;
} }
free(line);
} }
fclose(f); fclose(f);
FILE *g = fopen("/proc/modules", "r"); FILE *g = fopen("/proc/modules", "r");
if (!g) { if (!g) {
free(line);
return; return;
} }
bool vc4 = false; bool vc4 = false;
while (!feof(g)) { while (getline(&line, &line_size, g) != -1) {
if (!(line = read_line(g))) {
break;
}
if (strstr(line, "vc4")) { if (strstr(line, "vc4")) {
vc4 = true; vc4 = true;
break;
} }
free(line);
} }
free(line);
fclose(g); fclose(g);
if (!vc4 && raspi) { if (!vc4 && raspi) {
fprintf(stderr, "\x1B[1;31mWarning: You have a " fprintf(stderr, "\x1B[1;31mWarning: You have a "
@ -86,13 +83,10 @@ void detect_proprietary(int allow_unsupported_gpu) {
if (!f) { if (!f) {
return; return;
} }
while (!feof(f)) { char *line = NULL;
char *line; size_t line_size = 0;
if (!(line = read_line(f))) { while (getline(&line, &line_size, f) != -1) {
break;
}
if (strstr(line, "nvidia")) { if (strstr(line, "nvidia")) {
free(line);
if (allow_unsupported_gpu) { if (allow_unsupported_gpu) {
wlr_log(WLR_ERROR, wlr_log(WLR_ERROR,
"!!! Proprietary Nvidia drivers are in use !!!"); "!!! Proprietary Nvidia drivers are in use !!!");
@ -106,7 +100,6 @@ void detect_proprietary(int allow_unsupported_gpu) {
break; break;
} }
if (strstr(line, "fglrx")) { if (strstr(line, "fglrx")) {
free(line);
if (allow_unsupported_gpu) { if (allow_unsupported_gpu) {
wlr_log(WLR_ERROR, wlr_log(WLR_ERROR,
"!!! Proprietary AMD drivers are in use !!!"); "!!! Proprietary AMD drivers are in use !!!");
@ -118,8 +111,8 @@ void detect_proprietary(int allow_unsupported_gpu) {
} }
break; break;
} }
free(line);
} }
free(line);
fclose(f); 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) { static void log_distro(void) {
const char *paths[] = { const char *paths[] = {
"/etc/lsb-release", "/etc/lsb-release",
@ -158,16 +164,7 @@ static void log_distro(void) {
FILE *f = fopen(paths[i], "r"); FILE *f = fopen(paths[i], "r");
if (f) { if (f) {
wlr_log(WLR_INFO, "Contents of %s:", paths[i]); wlr_log(WLR_INFO, "Contents of %s:", paths[i]);
while (!feof(f)) { log_file(f);
char *line;
if (!(line = read_line(f))) {
break;
}
if (*line) {
wlr_log(WLR_INFO, "%s", line);
}
free(line);
}
fclose(f); fclose(f);
} }
} }
@ -179,16 +176,7 @@ static void log_kernel(void) {
wlr_log(WLR_INFO, "Unable to determine kernel version"); wlr_log(WLR_INFO, "Unable to determine kernel version");
return; return;
} }
while (!feof(f)) { log_file(f);
char *line;
if (!(line = read_line(f))) {
break;
}
if (*line) {
wlr_log(WLR_INFO, "%s", line);
}
free(line);
}
pclose(f); pclose(f);
} }

View file

@ -12,7 +12,6 @@
#include "swaybar/config.h" #include "swaybar/config.h"
#include "swaybar/i3bar.h" #include "swaybar/i3bar.h"
#include "swaybar/status_line.h" #include "swaybar/status_line.h"
#include "readline.h"
static void status_line_close_fds(struct status_line *status) { static void status_line_close_fds(struct status_line *status) {
if (status->read_fd != -1) { if (status->read_fd != -1) {

View file

@ -23,7 +23,6 @@
#include "cairo.h" #include "cairo.h"
#include "log.h" #include "log.h"
#include "loop.h" #include "loop.h"
#include "readline.h"
#include "stringop.h" #include "stringop.h"
#include "util.h" #include "util.h"
#include "wlr-input-inhibitor-unstable-v1-client-protocol.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."); wlr_log(WLR_ERROR, "Failed to read config. Running without it.");
return 0; return 0;
} }
char *line; char *line = NULL;
size_t line_size = 0;
ssize_t nread;
int line_number = 0; int line_number = 0;
while (!feof(config)) { int result = 0;
line = read_line(config); while ((nread = getline(&line, &line_size, config)) != -1) {
if (!line) { line_number++;
continue;
if (line[nread - 1] == '\n') {
line[--nread] = '\0';
} }
line_number++; if (!*line || line[0] == '#') {
if (line[0] == '#') {
free(line);
continue;
}
if (strlen(line) == 0) {
free(line);
continue; continue;
} }
wlr_log(WLR_DEBUG, "Config Line #%d: %s", line_number, line); wlr_log(WLR_DEBUG, "Config Line #%d: %s", line_number, line);
char flag[strlen(line) + 3]; char flag[nread + 3];
sprintf(flag, "--%s", line); sprintf(flag, "--%s", line);
char *argv[] = {"swaylock", flag}; 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) { if (result != 0) {
free(line); break;
fclose(config);
return result;
} }
free(line);
} }
free(line);
fclose(config); fclose(config);
return 0; return 0;
} }

View file

@ -12,7 +12,6 @@
#include <json-c/json.h> #include <json-c/json.h>
#include "stringop.h" #include "stringop.h"
#include "ipc-client.h" #include "ipc-client.h"
#include "readline.h"
#include "log.h" #include "log.h"
void sway_terminate(int exit_code) { void sway_terminate(int exit_code) {

View file

@ -1,10 +1,10 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include <getopt.h> #include <getopt.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <wordexp.h> #include <wordexp.h>
#include "log.h" #include "log.h"
#include "list.h" #include "list.h"
#include "readline.h"
#include "swaynag/swaynag.h" #include "swaynag/swaynag.h"
#include "swaynag/types.h" #include "swaynag/types.h"
#include "util.h" #include "util.h"
@ -12,21 +12,19 @@
static char *read_from_stdin(void) { static char *read_from_stdin(void) {
char *buffer = NULL; char *buffer = NULL;
while (!feof(stdin)) { size_t buffer_len = 0;
char *line = read_line(stdin); char *line = NULL;
if (!line) { size_t line_size = 0;
continue; ssize_t nread;
} while ((nread = getline(&line, &line_size, stdin)) != -1) {
buffer = realloc(buffer, buffer_len + nread);
size_t curlen = buffer ? strlen(buffer) : 0; snprintf(&buffer[buffer_len], nread + 1, "%s", line);
buffer = realloc(buffer, curlen + strlen(line) + 2); buffer_len += nread;
snprintf(buffer + curlen, strlen(line) + 2, "%s\n", line);
free(line);
} }
free(line);
while (buffer && buffer[strlen(buffer) - 1] == '\n') { while (buffer && buffer[buffer_len - 1] == '\n') {
buffer[strlen(buffer) - 1] = '\0'; buffer[--buffer_len] = '\0';
} }
return buffer; return buffer;
@ -348,32 +346,24 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
type->name = strdup("<config>"); type->name = strdup("<config>");
list_add(types, type); list_add(types, type);
char *line; char *line = NULL;
size_t line_size = 0;
ssize_t nread;
int line_number = 0; int line_number = 0;
while (!feof(config)) { int result = 0;
line = read_line(config); while ((nread = getline(&line, &line_size, config)) != -1) {
if (!line) {
continue;
}
line_number++; line_number++;
if (line[0] == '#') { if (!*line || line[0] == '\n' || line[0] == '#') {
free(line);
continue;
}
if (strlen(line) == 0) {
free(line);
continue; continue;
} }
if (line[0] == '[') { if (line[0] == '[') {
char *close = strchr(line, ']'); char *close = strchr(line, ']');
if (!close) { if (!close) {
free(line);
fclose(config);
fprintf(stderr, "Closing bracket not found on line %d\n", fprintf(stderr, "Closing bracket not found on line %d\n",
line_number); line_number);
return 1; result = 1;
break;
} }
char *name = calloc(1, close - line); char *name = calloc(1, close - line);
strncat(name, line + 1, close - line - 1); 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); free(name);
} else { } else {
char flag[strlen(line) + 3]; char flag[nread + 3];
sprintf(flag, "--%s", line); sprintf(flag, "--%s", line);
char *argv[] = {"swaynag", flag}; char *argv[] = {"swaynag", flag};
int result;
result = swaynag_parse_options(2, argv, swaynag, types, type, result = swaynag_parse_options(2, argv, swaynag, types, type,
NULL, NULL); NULL, NULL);
if (result != 0) { if (result != 0) {
free(line); break;
fclose(config);
return result;
} }
} }
free(line);
} }
free(line);
fclose(config); fclose(config);
return 0; return result;
} }