From b192dfcd75d587711e3ec5701d08a1fec68cc9f2 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 25 Dec 2023 16:21:31 +0100 Subject: [PATCH 1/5] Address warning -Wunused-result MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Symptom was (with GCC 12 on Linux): > # CFLAGS=-O1 make clean all > rm -f ttyplot stresstest > cc -O1 -Wall -Wextra `pkg-config --cflags ncursesw` ttyplot.c `pkg-config --libs ncursesw` -lm -o ttyplot > ttyplot.c: In function ‘signal_handler’: > ttyplot.c:285:5: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result] > 285 | write(signal_write_fd, &signal_number, 1); > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > cc -O1 -Wall -Wextra `pkg-config --cflags ncursesw` stresstest.c `pkg-config --libs ncursesw` -lm -o stresstest > stresstest.c: In function ‘main’: > stresstest.c:86:17: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result] > 86 | write(STDOUT_FILENO, buffer + send_pos, bytes_to_send); > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > stresstest.c:94:13: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result] > 94 | write(STDOUT_FILENO, buffer, buffer_pos); > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Co-authored-by: Edgar Bonet --- stresstest.c | 12 ++++++++---- ttyplot.c | 5 ++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/stresstest.c b/stresstest.c index d56ba33..23bc44a 100644 --- a/stresstest.c +++ b/stresstest.c @@ -83,16 +83,20 @@ int main(int argc, char *argv[]) { size_t send_pos = 0; while (buffer_pos - send_pos >= 16) { const size_t bytes_to_send = 1 + rand() % 16; // 1..16 - write(STDOUT_FILENO, buffer + send_pos, bytes_to_send); + const ssize_t bytes_sent = write(STDOUT_FILENO, buffer + send_pos, bytes_to_send); usleep(50); // let ttyplot read this before proceeding - send_pos += bytes_to_send; + if (bytes_sent > 0) + send_pos += bytes_sent; } if (send_pos > 0 && send_pos < buffer_pos) memmove(buffer, buffer + send_pos, buffer_pos - send_pos); buffer_pos -= send_pos; } else { - write(STDOUT_FILENO, buffer, buffer_pos); - buffer_pos = 0; + const ssize_t bytes_sent = write(STDOUT_FILENO, buffer, buffer_pos); + if ((bytes_sent > 0) && ((size_t)bytes_sent < buffer_pos)) + memmove(buffer, buffer + bytes_sent, buffer_pos - bytes_sent); + if (bytes_sent > 0) + buffer_pos -= bytes_sent; } usleep(delay); } diff --git a/ttyplot.c b/ttyplot.c index 5e1fccc..ab1deb3 100644 --- a/ttyplot.c +++ b/ttyplot.c @@ -282,7 +282,10 @@ static void paint_plot(void) { // (Related: https://stackoverflow.com/q/62315082) static void signal_handler(int signum) { const unsigned char signal_number = (unsigned char) signum; // signum is either 2 (SIGINT) or 28 (SIGWINCH) - write(signal_write_fd, &signal_number, 1); + ssize_t write_res; + do { + write_res = write(signal_write_fd, &signal_number, 1); + } while ((write_res == -1) && (errno == EINTR)); } static void redraw_screen(const char * errstr) { From 68f55545fc2149ef1950117a661cd9753b78e737 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 25 Dec 2023 16:24:28 +0100 Subject: [PATCH 2/5] linux_and_macos.yml: Compile with -O1 --- .github/workflows/linux_and_macos.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linux_and_macos.yml b/.github/workflows/linux_and_macos.yml index 5d8a762..87db19f 100644 --- a/.github/workflows/linux_and_macos.yml +++ b/.github/workflows/linux_and_macos.yml @@ -124,7 +124,7 @@ jobs: ${MAKE} --version 2>/dev/null || true - CFLAGS="-std=gnu99 -pedantic -Werror ${sanitizer}" LDFLAGS="${as_needed} ${sanitizer}" ${MAKE} + CFLAGS="-std=gnu99 -pedantic -Werror ${sanitizer} -O1" LDFLAGS="${as_needed} ${sanitizer}" ${MAKE} - name: 'Install' env: From e94848425f77d05ecc59f3edcd17b4159409b9fc Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 25 Dec 2023 16:26:39 +0100 Subject: [PATCH 3/5] ttyplot.c: Fix crash with -O1 on Ctrl+C To reproduce: 1. Run "ttyplot" (as is) 2. Hit Ctrl+C 3. See message "*** bit out of range 0 - FD_SETSIZE on fd_set ***: terminate" and "Aborted" --- ttyplot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ttyplot.c b/ttyplot.c index ab1deb3..7ee54f5 100644 --- a/ttyplot.c +++ b/ttyplot.c @@ -481,7 +481,7 @@ static int wait_for_events(int signal_read_fd, int tty, bool stdin_is_open, stru ret |= EVENT_SIGNAL_READABLE; } - if (FD_ISSET(tty, &read_fds)) { + if ((tty != -1) && FD_ISSET(tty, &read_fds)) { ret |= EVENT_TTY_READABLE; } From a47053fe1e8b535f662e51ff248ff1a98da6d540 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 25 Dec 2023 16:31:08 +0100 Subject: [PATCH 4/5] Bump version to 1.6.1 --- recordings/expected.txt | 6 +++--- snap/snapcraft.yaml | 2 +- ttyplot.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/recordings/expected.txt b/recordings/expected.txt index 562cbef..2421eec 100644 --- a/recordings/expected.txt +++ b/recordings/expected.txt @@ -19,7 +19,7 @@ | │ | | └─────────────────────────────────────────────────────────────────────────────────────> | | X Thu Jan 1 00:00:00 1970 | -|   https://github.com/tenox7/ttyplot 1.6.0 | +|   https://github.com/tenox7/ttyplot 1.6.1 | +------------------------------------------------------------------------------------------+ [90x20] Frame 2: @@ -43,7 +43,7 @@ | │ XX | | └─────────────────────────────────────────────────────────────────────────────────────> | | X last=3.0 min=1.0 max=3.0 avg=2.0 Thu Jan 1 00:00:00 1970 | -|   last=4.0 min=2.0 max=4.0 avg=3.0 https://github.com/tenox7/ttyplot 1.6.0 | +|   last=4.0 min=2.0 max=4.0 avg=3.0 https://github.com/tenox7/ttyplot 1.6.1 | +------------------------------------------------------------------------------------------+ [90x20] Frame 3: @@ -67,6 +67,6 @@ | │ XX | | └─────────────────────────────────────────────────────────────────────────────────────> | | X last=3.0 min=1.0 max=3.0 avg=2.0 Thu Jan 1 00:00:00 1970 | -|   last=4.0 min=2.0 max=4.0 avg=3.0 https://github.com/tenox7/ttyplot 1.6.0 | +|   last=4.0 min=2.0 max=4.0 avg=3.0 https://github.com/tenox7/ttyplot 1.6.1 | +------------------------------------------------------------------------------------------+ diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 5257fb5..4bff377 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: ttyplot -version: 1.6.0 +version: 1.6.1 summary: realtime plotting utility for terminal/console with data input from stdin description: | takes data from standard input / unix pipeline, diff --git a/ttyplot.c b/ttyplot.c index 7ee54f5..6cf5ee8 100644 --- a/ttyplot.c +++ b/ttyplot.c @@ -39,7 +39,7 @@ #define STR(x) STR_(x) #define VERSION_MAJOR 1 #define VERSION_MINOR 6 -#define VERSION_PATCH 0 +#define VERSION_PATCH 1 #define VERSION_STR STR(VERSION_MAJOR) "." STR(VERSION_MINOR) "." STR(VERSION_PATCH) #define T_RARR '>' From aa919c9e3ae207eec3d3dcd3a403542a40076f31 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 25 Dec 2023 16:31:38 +0100 Subject: [PATCH 5/5] ttyplot.1: Set date to today --- ttyplot.1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ttyplot.1 b/ttyplot.1 index afc0006..ff9b0dc 100644 --- a/ttyplot.1 +++ b/ttyplot.1 @@ -1,4 +1,4 @@ -.Dd November 27, 2023 +.Dd December 25, 2023 .Dt TTYPLOT 1 .Os .Sh NAME