Skip to content
Myr

dmenu: don't use getdelim() to read lines from stdin

Before adoption: this commit came in with the history the project adopted, so no landing records it.

We mark stdin as non-blocking. This causes getdelim() to fail if we receive input on stdin, but no delimiter. For example: printf("a line without a newline"); sleep(5); printf("\n"); printf("part of the"); sleep(5); printf(" next line\n"); Solution: roll our own getdelim() that reads directly from the stdin file descriptor, and buffers data locally. In short: * When poll() says there's data to read, try to fill up the local buffer. If the local buffer is already full, increase its size. * Consume as many lines as possible from the buffer. * Any remaining data (i.e. a trailing partial line) is memmoved() to the beginning of the buffer, and we then wait for poll() to signal input availability. * If stdin is closed, treat all remaining data as a line, and consume it.

authored by Daniel Eklöf · committed by Daniel Eklöf · parent c079ac8 · Browse files · 2 files+109 added−30 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+109 added−30 removed

  • other:+109 added−30 removed

2 files+109 added−30 removed

modified:CHANGELOG.md+2 added
61 unmodified lines
62* Crash when `tabs` (in `fuzzel.ini`) is set to `0` ([#348][348]).63* Crash while loading the cache, when `--list-executables-in-path` is64 used ([#356][356])65* Rounding of window size when fractional scaling is used.66* Dmenu mode failing with _"failed to read from stdin: Resource67 temporarily unavailable"_.6869[348]: https://codeberg.org/dnkl/fuzzel/issues/34870[356]: https://codeberg.org/dnkl/fuzzel/issues/35671
651 unmodified lines
modified:dmenu.c+107 added-30 removed
21 unmodified lines
22 int abort_fd)23{24 tll(struct application) entries = tll_init();2526 char *line = NULL;27 size_t alloc_size = 0;26 size_t size = 0;27 size_t alloc_size = 256;28 char *buffer = malloc(alloc_size);2930 if (buffer == NULL) {31 LOG_ERRNO("failed to allocate input buffer");32 return;33 }3435 int flags;36 if ((flags = fcntl(STDIN_FILENO, F_GETFL)) < 0 ||37 fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) < 0)38 {
22 unmodified lines
6162 if (fds[1].revents & (POLLIN | POLLHUP))63 break;6459 ssize_t len = getdelim(&line, &alloc_size, delim, stdin);65 /* Increase size of input buffer, if necessary */66 if (size >= alloc_size - 2) {67 LOG_DBG("increasing input buffer size %zu -> %zu",68 alloc_size, alloc_size * 2);6961 if (len < 0) {62 if (errno != 0)63 LOG_ERRNO("failed to read from stdin");70 alloc_size *= 2;7172 char *new_buf = realloc(buffer, alloc_size);73 if (new_buf == NULL) {74 LOG_ERRNO("failed to reallocate input buffer");75 break;76 }7778 buffer = new_buf;79 }8081 ssize_t bytes_read = read(82 STDIN_FILENO, &buffer[size], alloc_size - size - 1);8384 if (bytes_read < 0) {85 if (errno == EINTR)86 continue;87 LOG_ERRNO("failed to read from stdin");88 break;89 } else if (bytes_read == 0 && size == 0) {90 /* No more data on stdin, and all buffered data consumed */91 break;92 }9367 while (len > 0 && line[len - 1] == '\n')68 line[--len] = '\0';94 size += bytes_read;9596 /*71 * Support Rofi’s extended dmenu protocol. One can specify an72 * icon by appending ‘\0icon\x1f<icon-name>’ to the entry:97 * Consume as much as possible from the input buffer98 *74 * “hello world\0icon\x1ffirefox”99 * Input buffer may contain many lines, resulting in multiple100 * applications being added.101 *102 * Input buffer may not even contain a single line, in which103 * case we can't consume anything at all.104 *105 * Input buffer may contain one or more full lines, followed106 * by a final partial line. If stdin has been closed107 * (i.e. there's no more input to read), then treat the last,108 * partial line as a complete line (i.e. pretend it's followed109 * by a delimiter). But, if stdin is still open, don't consume110 * the last, partial line. Instead, wait for more input.111 */76 const char *icon_name = NULL;77 const char *extra = strchr(line, '\0');78 if (extra != NULL && strncmp(extra + 1, "icon", 4) == 0) {79 char *separator = strchr(extra + 1, '\x1f');80 if (separator != NULL) {81 *separator = '\0';82 icon_name = separator + 1;112 while (size > 0) {113 char *delim_at = memchr(buffer, delim, size);114 if (delim_at == NULL) {115 if (bytes_read > 0) {116 /* No delimiter yet, wait for more data */117 LOG_DBG("no delimiter found, waiting for more data...");118 break;119 } else {120 /* No delimiter, but stdin has been closed. Treat121 the last chunk of data as a complete line. */122 LOG_DBG("last line: pointing delim_at to the end of the buffer");123 delim_at = &buffer[size];124 size++;125 }126 }84 }12786 LOG_DBG("%s (icon=%s)", line, icon_name);128 const size_t entry_len = delim_at - buffer;129 *delim_at = '\0';13088 char32_t *wline = ambstoc32(line);89 if (wline == NULL)90 continue;131 /*132 * Support Rofi’s extended dmenu protocol. One can specify133 * an icon by appending ‘\0icon\x1f<icon-name>’ to the134 * entry:135 *136 * “hello world\0icon\x1ffirefox”137 */138 char *icon_name = NULL;139 const char *extra = memchr(buffer, '\0', entry_len);14092 struct application app = {93 .title = wline,94 .icon = {.name = icon_name != NULL ? strdup(icon_name) : NULL},95 .visible = true,96 };141 if (extra != NULL) {142 const size_t extra_len = delim_at - extra;143144 /*145 * 'extra' is "\0icon\x1f" - 6 characters. Require146 * *more* than 6, since icon *name* cannot be empty147 */148 if (extra_len > 6 && memcmp(extra, "\0icon\x1f", 6) == 0)149 icon_name = strndup(extra + 6, delim_at - (extra + 6));150 }151152 LOG_DBG("%s (icon=%s)", buffer, icon_name);153154 char32_t *wline = ambstoc32(buffer);155156 /* Consume entry from input buffer */157 const size_t consume = delim_at + 1 - buffer;158 assert(consume <= size);159160 memmove(buffer, delim_at + 1, size - consume);161 size -= consume;162163 if (wline == NULL) {164 free(icon_name);165 continue;166 }167168 struct application app = {169 .title = wline,170 .icon = {.name = icon_name},171 .visible = true,172 };17398 tll_push_back(entries, app);174 tll_push_back(entries, app);175 }176 }177101 free(line);178 free(buffer);179180out:181182 applications->v = calloc(tll_length(entries), sizeof(applications->v[0]));
32 unmodified lines