aboutsummaryrefslogtreecommitdiffstats
path: root/common
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 /common
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 'common')
-rw-r--r--common/ipc-client.c11
-rw-r--r--common/meson.build1
-rw-r--r--common/readline.c72
-rw-r--r--common/util.c4
4 files changed, 8 insertions, 80 deletions
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 @@
7#include <sys/un.h> 7#include <sys/un.h>
8#include <unistd.h> 8#include <unistd.h>
9#include "ipc-client.h" 9#include "ipc-client.h"
10#include "readline.h"
11#include "log.h" 10#include "log.h"
12 11
13static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; 12static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
@@ -18,28 +17,30 @@ char *get_socketpath(void) {
18 if (swaysock) { 17 if (swaysock) {
19 return strdup(swaysock); 18 return strdup(swaysock);
20 } 19 }
20 char *line = NULL;
21 size_t line_size = 0;
21 FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r"); 22 FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r");
22 if (fp) { 23 if (fp) {
23 char *line = read_line(fp); 24 getline(&line, &line_size, fp);
24 pclose(fp); 25 pclose(fp);
25 if (line && *line) { 26 if (line && *line) {
26 return line; 27 return line;
27 } 28 }
28 free(line);
29 } 29 }
30 const char *i3sock = getenv("I3SOCK"); 30 const char *i3sock = getenv("I3SOCK");
31 if (i3sock) { 31 if (i3sock) {
32 free(line);
32 return strdup(i3sock); 33 return strdup(i3sock);
33 } 34 }
34 fp = popen("i3 --get-socketpath 2>/dev/null", "r"); 35 fp = popen("i3 --get-socketpath 2>/dev/null", "r");
35 if (fp) { 36 if (fp) {
36 char *line = read_line(fp); 37 getline(&line, &line_size, fp);
37 pclose(fp); 38 pclose(fp);
38 if (line && *line) { 39 if (line && *line) {
39 return line; 40 return line;
40 } 41 }
41 free(line);
42 } 42 }
43 free(line);
43 return NULL; 44 return NULL;
44} 45}
45 46
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(
8 'loop.c', 8 'loop.c',
9 'list.c', 9 'list.c',
10 'pango.c', 10 'pango.c',
11 'readline.c',
12 'stringop.c', 11 'stringop.c',
13 'unicode.c', 12 'unicode.c',
14 'util.c' 13 '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 @@
1#define _POSIX_C_SOURCE 200809L
2#include "readline.h"
3#include "log.h"
4#include <stdlib.h>
5#include <stdio.h>
6
7char *read_line(FILE *file) {
8 size_t length = 0, size = 128;
9 char *string = malloc(size);
10 char lastChar = '\0';
11 if (!string) {
12 wlr_log(WLR_ERROR, "Unable to allocate memory for read_line");
13 return NULL;
14 }
15 while (1) {
16 int c = getc(file);
17 if (c == '\n' && lastChar == '\\'){
18 --length; // Ignore last character.
19 lastChar = '\0';
20 continue;
21 }
22 if (c == EOF || c == '\n' || c == '\0') {
23 break;
24 }
25 if (c == '\r') {
26 continue;
27 }
28 lastChar = c;
29 if (length == size) {
30 char *new_string = realloc(string, size *= 2);
31 if (!new_string) {
32 free(string);
33 wlr_log(WLR_ERROR, "Unable to allocate memory for read_line");
34 return NULL;
35 }
36 string = new_string;
37 }
38 string[length++] = c;
39 }
40 if (length + 1 == size) {
41 char *new_string = realloc(string, length + 1);
42 if (!new_string) {
43 free(string);
44 return NULL;
45 }
46 string = new_string;
47 }
48 string[length] = '\0';
49 return string;
50}
51
52char *peek_line(FILE *file, int line_offset, long *position) {
53 long pos = ftell(file);
54 size_t length = 0;
55 char *line = NULL;
56 for (int i = 0; i <= line_offset; i++) {
57 ssize_t read = getline(&line, &length, file);
58 if (read < 0) {
59 free(line);
60 line = NULL;
61 break;
62 }
63 if (read > 0 && line[read - 1] == '\n') {
64 line[read - 1] = '\0';
65 }
66 }
67 if (position) {
68 *position = ftell(file);
69 }
70 fseek(file, pos, SEEK_SET);
71 return line;
72}
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 @@
13#include <xkbcommon/xkbcommon-names.h> 13#include <xkbcommon/xkbcommon-names.h>
14#include <wlr/types/wlr_keyboard.h> 14#include <wlr/types/wlr_keyboard.h>
15#include "log.h" 15#include "log.h"
16#include "readline.h"
17#include "util.h" 16#include "util.h"
18 17
19int wrap(int i, int max) { 18int wrap(int i, int max) {
@@ -87,11 +86,12 @@ pid_t get_parent_pid(pid_t child) {
87 char *token = NULL; 86 char *token = NULL;
88 const char *sep = " "; 87 const char *sep = " ";
89 FILE *stat = NULL; 88 FILE *stat = NULL;
89 size_t buf_size = 0;
90 90
91 sprintf(file_name, "/proc/%d/stat", child); 91 sprintf(file_name, "/proc/%d/stat", child);
92 92
93 if ((stat = fopen(file_name, "r"))) { 93 if ((stat = fopen(file_name, "r"))) {
94 if ((buffer = read_line(stat))) { 94 if (getline(&buffer, &buf_size, stat) != -1) {
95 token = strtok(buffer, sep); // pid 95 token = strtok(buffer, sep); // pid
96 token = strtok(NULL, sep); // executable name 96 token = strtok(NULL, sep); // executable name
97 token = strtok(NULL, sep); // state 97 token = strtok(NULL, sep); // state