-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminal.c
55 lines (46 loc) · 1.22 KB
/
terminal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "terminal.h"
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <termbox.h>
// Ward against calling tb_shutdown twice. A common case where this would
// happen is if we receive SIGTERM while the editor is in the background.
static bool terminal_is_shut_down = false;
void terminal_resume(void) {
int err = tb_init();
if (err) {
fprintf(stderr, "tb_init() failed with error code %d\n", err);
exit(1);
}
terminal_is_shut_down = false;
}
void terminal_shutdown(void) {
if (!terminal_is_shut_down) {
terminal_is_shut_down = true;
tb_shutdown();
}
}
void terminal_suspend(void) {
terminal_shutdown();
// SIGTSTP is the event that is normally generated by hitting Ctrl-Z.
raise(SIGTSTP);
terminal_resume();
}
static void terminal_sighandler(int signum) {
terminal_shutdown();
raise(signum);
}
void terminal_init(void) {
terminal_resume();
atexit(terminal_shutdown);
struct sigaction sa;
sa.sa_handler = terminal_sighandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESETHAND;
sigaction(SIGABRT, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
}