-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptrkeys.c
72 lines (62 loc) · 1.26 KB
/
ptrkeys.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#include <string.h>
#include "pk.h"
#include "jot.h"
#define USAGE "usage: ptrkeys [-d|--debug] [-h|--help] [--version]\n"
static void onsigint();
static void setsighandler();
int jottrace = 0;
static sigjmp_buf jmpbuf;
static volatile sig_atomic_t canjump;
static void
onsigint()
{
if (!canjump) return;
canjump = 0;
siglongjmp(jmpbuf, 1);
}
static void
setsighandler()
{
struct sigaction sa = {.sa_handler = onsigint};
sigemptyset(&sa.sa_mask);
if (sigaction(SIGINT, &sa, NULL) == -1) {
dief("set sigint handler: %s", strerror(errno));
}
}
static void
parseargs(int argc, char *argv[])
{
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
jottrace = 1;
} else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
fprintf(stdout, USAGE);
exit(0);
} else if (!strcmp(argv[i], "--version")) {
fprintf(stdout, VERSION "\n");
exit(0);
} else {
fprintf(stderr, USAGE);
exit(1);
}
}
}
int
main(int argc, char *argv[])
{
parseargs(argc, argv);
dieifbadbindings();
setup();
setsighandler();
if (sigsetjmp(jmpbuf, 0)) {
// Jumped from signal handler.
exit(130);
}
canjump = 1;
runeventloop();
exit(0);
}