mirror of
https://github.com/swaywm/sway.git
synced 2025-11-24 06:59:51 -05:00
Merge pull request #3275 from ianyfan/remove-readline
Rewrite strip_whitespace and remove readline.c
This commit is contained in:
commit
140bc2dd5b
15 changed files with 161 additions and 274 deletions
|
|
@ -237,15 +237,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;
|
||||
|
|
|
|||
134
sway/config.c
134
sway/config.c
|
|
@ -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>
|
||||
|
|
@ -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"
|
||||
|
|
@ -571,28 +570,49 @@ 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) {
|
||||
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) {
|
||||
peeked = strip_whitespace(peeked);
|
||||
}
|
||||
lines++;
|
||||
} while (peeked && strlen(peeked) == 0);
|
||||
|
||||
if (peeked && strlen(peeked) == 1 && peeked[0] == '{') {
|
||||
fseek(file, position, SEEK_SET);
|
||||
} else {
|
||||
lines = 0;
|
||||
// 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 && strcmp(&(*lineptr)[nread - 2], "\\\n") == 0) {
|
||||
ssize_t next_nread = getline(&next_line, &next_line_size, file);
|
||||
if (next_nread == -1) {
|
||||
break;
|
||||
}
|
||||
free(peeked);
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(line);
|
||||
return lines;
|
||||
}
|
||||
|
||||
|
|
@ -635,55 +655,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;
|
||||
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");
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
strcpy(&this_config[read], line);
|
||||
read += nread;
|
||||
}
|
||||
|
||||
if (line[nread - 1] == '\n') {
|
||||
line[nread - 1] = '\0';
|
||||
}
|
||||
|
||||
line_number++;
|
||||
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;
|
||||
}
|
||||
|
||||
line = strip_whitespace(line);
|
||||
if (line[0] == '#') {
|
||||
free(line);
|
||||
strip_whitespace(line);
|
||||
if (!*line || line[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
if (strlen(line) == 0) {
|
||||
free(line);
|
||||
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;
|
||||
|
|
@ -743,9 +755,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;
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
68
sway/main.c
68
sway/main.c
|
|
@ -3,6 +3,7 @@
|
|||
#include <pango/pangocairo.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue