Skip to content

Commit

Permalink
Fix Ctrl+D on FreeBSD
Browse files Browse the repository at this point in the history
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 56bb182.

Signed-off-by: Gavin D. Howard <[email protected]>
  • Loading branch information
gavinhoward committed Sep 16, 2024
1 parent c5b7724 commit 89d6c34
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
24 changes: 24 additions & 0 deletions include/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

2 comments on commit 89d6c34

@dag-erling
Copy link

@dag-erling dag-erling commented on 89d6c34 Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug is in bc, not in macOS. You are calling el_resize() from a signal handler, which isn't supported. The solution is to revert this and 56bb182, remove any mention of SIGWINCH in bc, and instead add the following line in an appropriate location in bc_history_init():

        el_set(h->el, EL_SIGNAL, 1);

This will make libedit handle SIGWINCH on its own and everything will be fine.

Edit: see #86.

@gavinhoward
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment on #86; your solution will not work for bc, unfortunately.

Please sign in to comment.