diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-09-28 04:22:18 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-28 04:22:18 -0500 |
commit | 813de9f7ac674ad276b01df0f0fac39a7ba8fbdd (patch) | |
tree | 46b8d64aa07a9236594a35598926953c92b4bb01 | |
parent | Merge pull request #2663 from ianyfan/fix-txn-state-leaks (diff) | |
parent | Replace getdelim to make swaybar work on FreeBSD (diff) | |
download | sway-813de9f7ac674ad276b01df0f0fac39a7ba8fbdd.tar.gz sway-813de9f7ac674ad276b01df0f0fac39a7ba8fbdd.tar.zst sway-813de9f7ac674ad276b01df0f0fac39a7ba8fbdd.zip |
Merge pull request #2716 from sghctoma/fix-swaybar-freebsd
Replace getdelim to make swaybar work on FreeBSD
-rw-r--r-- | swaybar/status_line.c | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/swaybar/status_line.c b/swaybar/status_line.c index 48b43248..1442e1a0 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c | |||
@@ -1,5 +1,6 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | 1 | #define _POSIX_C_SOURCE 200809L |
2 | #include <fcntl.h> | 2 | #include <fcntl.h> |
3 | #include <sys/ioctl.h> | ||
3 | #include <json-c/json.h> | 4 | #include <json-c/json.h> |
4 | #include <stdlib.h> | 5 | #include <stdlib.h> |
5 | #include <string.h> | 6 | #include <string.h> |
@@ -34,18 +35,35 @@ bool status_handle_readable(struct status_line *status) { | |||
34 | switch (status->protocol) { | 35 | switch (status->protocol) { |
35 | case PROTOCOL_UNDEF: | 36 | case PROTOCOL_UNDEF: |
36 | errno = 0; | 37 | errno = 0; |
37 | read_bytes = getline(&status->buffer, | 38 | int available_bytes; |
38 | &status->buffer_size, status->read); | 39 | if (ioctl(status->read_fd, FIONREAD, &available_bytes) == -1) { |
39 | if (errno == EAGAIN) { | 40 | wlr_log(WLR_ERROR, "Unable to read status command output size"); |
40 | clearerr(status->read); | ||
41 | } else if (errno) { | ||
42 | status_error(status, "[error reading from status command]"); | 41 | status_error(status, "[error reading from status command]"); |
43 | return true; | 42 | return true; |
44 | } | 43 | } |
45 | 44 | ||
45 | if ((size_t)available_bytes + 1 > status->buffer_size) { | ||
46 | // need room for leading '\0' too | ||
47 | status->buffer_size = available_bytes + 1; | ||
48 | status->buffer = realloc(status->buffer, status->buffer_size); | ||
49 | } | ||
50 | if (status->buffer == NULL) { | ||
51 | wlr_log_errno(WLR_ERROR, "Unable to read status line"); | ||
52 | status_error(status, "[error reading from status command]"); | ||
53 | return true; | ||
54 | } | ||
55 | |||
56 | read_bytes = read(status->read_fd, status->buffer, available_bytes); | ||
57 | if (read_bytes != available_bytes) { | ||
58 | status_error(status, "[error reading from status command]"); | ||
59 | return true; | ||
60 | } | ||
61 | status->buffer[available_bytes] = 0; | ||
62 | |||
46 | // the header must be sent completely the first time round | 63 | // the header must be sent completely the first time round |
64 | char *newline = strchr(status->buffer, '\n'); | ||
47 | json_object *header, *version; | 65 | json_object *header, *version; |
48 | if (status->buffer[read_bytes - 1] == '\n' | 66 | if (newline != NULL |
49 | && (header = json_tokener_parse(status->buffer)) | 67 | && (header = json_tokener_parse(status->buffer)) |
50 | && json_object_object_get_ex(header, "version", &version) | 68 | && json_object_object_get_ex(header, "version", &version) |
51 | && json_object_get_int(version) == 1) { | 69 | && json_object_get_int(version) == 1) { |
@@ -67,13 +85,9 @@ bool status_handle_readable(struct status_line *status) { | |||
67 | 85 | ||
68 | wl_list_init(&status->blocks); | 86 | wl_list_init(&status->blocks); |
69 | status->tokener = json_tokener_new(); | 87 | status->tokener = json_tokener_new(); |
70 | read_bytes = getdelim(&status->buffer, &status->buffer_size, EOF, status->read); | 88 | status->buffer_index = strlen(newline + 1); |
71 | if (read_bytes > 0) { | 89 | memmove(status->buffer, newline + 1, status->buffer_index + 1); |
72 | status->buffer_index = read_bytes; | 90 | return i3bar_handle_readable(status); |
73 | return i3bar_handle_readable(status); | ||
74 | } else { | ||
75 | return false; | ||
76 | } | ||
77 | } | 91 | } |
78 | 92 | ||
79 | wlr_log(WLR_DEBUG, "Using text protocol."); | 93 | wlr_log(WLR_DEBUG, "Using text protocol."); |