From 89d6c3451a605cbf26fc4a0a3e3082633a8b1ae3 Mon Sep 17 00:00:00 2001 From: "Gavin D. Howard" Date: Mon, 16 Sep 2024 15:21:38 -0600 Subject: [PATCH] Fix Ctrl+D on FreeBSD This was broken by the macOS fix because apparently, editline acts differently on those platforms. I have tested that Linux, macOS, and FreeBSD all work with this one, both SIGWINCH and Ctrl+D. Well, Ctrl+D doesn't work on macOS, but that is actually a limitation I *can't* get around. Fixes FreeBSD being broken by 56bb18255a24b60f785560ab882447d9c959bd93. Signed-off-by: Gavin D. Howard --- include/history.h | 24 ++++++++++++++++++++++++ src/history.c | 13 ++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/include/history.h b/include/history.h index 460524bd..13f6dc6e 100644 --- a/include/history.h +++ b/include/history.h @@ -120,6 +120,30 @@ typedef struct BcHistory extern const char bc_history_editrc[]; extern const size_t bc_history_editrc_len; +#ifdef __APPLE__ + +/** + * Returns true if the line is a valid line, false otherwise. + * @param line The line. + * @param len The length of the line. + * @return True if the line is valid, false otherwise. + */ +#define BC_HISTORY_INVALID_LINE(line, len) \ + ((line) == NULL && ((len) == -1 || errno == EINTR)) + +#else // __APPLE__ + +/** + * Returns true if the line is a valid line, false otherwise. + * @param line The line. + * @param len The length of the line. + * @return True if the line is valid, false otherwise. + */ +#define BC_HISTORY_INVALID_LINE(line, len) \ + ((line) == NULL && (len) == -1 && errno == EINTR) + +#endif // __APPLE__ + #else // BC_ENABLE_EDITLINE #if BC_ENABLE_READLINE diff --git a/src/history.c b/src/history.c index 6ae9785d..32a19f71 100644 --- a/src/history.c +++ b/src/history.c @@ -264,7 +264,18 @@ bc_history_line(BcHistory* h, BcVec* vec, const char* prompt) errno = EINTR; // Get the line. - while (line == NULL && (len == -1 || errno == EINTR)) + // + // XXX: Why have a macro here? Because macOS needs to be special. Honestly, + // it's starting to feel special like Windows at this point. Anyway, the + // second SIGWINCH signal of multiple will return a valid line length on + // macOS, so we need to allow for that on macOS. However, FreeBSD's editline + // is different and will mess up the terminal if we do it that way. + // + // There is one limitation with this, however: Ctrl+D won't work on macOS. + // But it's because of macOS that this problem exists, and I can't really do + // anything about it. So macOS should fix their broken editline; once they + // do, I'll fix Ctrl+D on macOS. + while (BC_HISTORY_INVALID_LINE(line, len)) { line = el_gets(h->el, &len); bc_history_use_prompt = false;