Skip to content

Commit

Permalink
Remove readline.c
Browse files Browse the repository at this point in the history
All occurrences of read_line have been replaced by getline.
peek_line has been absorbed into detect_brace.
  • Loading branch information
ianyfan committed Dec 9, 2018
1 parent cdfa733 commit b1e90e5
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 246 deletions.
11 changes: 6 additions & 5 deletions common/ipc-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <sys/un.h>
#include <unistd.h>
#include "ipc-client.h"
#include "readline.h"
#include "log.h"

static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
Expand All @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion common/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ lib_sway_common = static_library(
'loop.c',
'list.c',
'pango.c',
'readline.c',
'stringop.c',
'unicode.c',
'util.c'
Expand Down
72 changes: 0 additions & 72 deletions common/readline.c

This file was deleted.

4 changes: 2 additions & 2 deletions common/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <xkbcommon/xkbcommon-names.h>
#include <wlr/types/wlr_keyboard.h>
#include "log.h"
#include "readline.h"
#include "util.h"

int wrap(int i, int max) {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions include/readline.h

This file was deleted.

2 changes: 1 addition & 1 deletion sway/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
}
// Split command list
cmdlist = argsep(&head, ";");
for (; isspace(*cmdlist); ++head) {}
for (; isspace(*cmdlist); ++cmdlist) {}
do {
// Split commands
cmd = argsep(&cmdlist, ",");
Expand Down
98 changes: 42 additions & 56 deletions sway/config.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define _XOPEN_SOURCE 600 // for realpath
#define _XOPEN_SOURCE 700 // for realpath
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -571,28 +570,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;
}

Expand Down Expand Up @@ -635,55 +629,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;
Expand Down Expand Up @@ -743,9 +729,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;
Expand Down
Loading

0 comments on commit b1e90e5

Please sign in to comment.