Handle SIGWINCH in editor(1).

This commit is contained in:
Jonas 'Sortie' Termansen 2024-06-18 17:16:47 +00:00
parent bf1d15957e
commit bab61702f3
2 changed files with 28 additions and 2 deletions

View File

@ -25,6 +25,7 @@
#include <limits.h>
#include <locale.h>
#include <pwd.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@ -270,6 +271,14 @@ bool editor_save_file(struct editor* editor, const char* path)
return fclose(fp) != EOF;
}
volatile sig_atomic_t had_sigwinch = 0;
static void sigwinch(int signum)
{
(void) signum;
had_sigwinch = 1;
}
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "");
@ -279,6 +288,13 @@ int main(int argc, char* argv[])
if ( !isatty(1) )
err(1, "standard output");
sigset_t sigwinch_set;
sigemptyset(&sigwinch_set);
sigaddset(&sigwinch_set, SIGWINCH);
sigprocmask(SIG_BLOCK, &sigwinch_set, NULL);
struct sigaction sa = { .sa_handler = sigwinch };
sigaction(SIGWINCH, &sa, NULL);
struct editor editor;
initialize_editor(&editor);

View File

@ -353,12 +353,22 @@ void editor_input_process_byte(struct editor_input* editor_input,
}
}
extern volatile sig_atomic_t had_sigwinch;
void editor_input_process(struct editor_input* editor_input,
struct editor* editor)
{
sigset_t sigset;
sigemptyset(&sigset);
struct pollfd pfd = { .fd = 0, .events = POLLIN };
do editor_input_process_byte(editor_input, editor);
while ( poll(&pfd, 1, 0) == 1 );
struct timespec timeout = { .tv_sec = 0 };
struct timespec* timeout_ptr = NULL;
while ( !had_sigwinch && ppoll(&pfd, 1, timeout_ptr, &sigset) == 1 )
{
editor_input_process_byte(editor_input, editor);
timeout_ptr = &timeout;
}
had_sigwinch = 0;
}
void editor_input_end(struct editor_input* editor_input)