Browse Source

Add sandman

June 1 week ago
parent
commit
deec27410a
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
4 changed files with 119 additions and 8 deletions
  1. 3
    1
      Darwin.mk
  2. 10
    7
      Makefile
  3. 34
    0
      sandman.1
  4. 72
    0
      sandman.m

+ 3
- 1
Darwin.mk View File

@@ -1,2 +1,4 @@
1 1
 LIBRESSL_PREFIX = /usr/local/opt/libressl
2
-LDLIBS = -lcurses -ltls
2
+LDLIBS = -lcurses -ltls -framework Cocoa
3
+BINS += sandman
4
+MANS += sandman.1

+ 10
- 7
Makefile View File

@@ -9,6 +9,9 @@ CFLAGS += -I$(LIBRESSL_PREFIX)/include
9 9
 LDFLAGS += -L$(LIBRESSL_PREFIX)/lib
10 10
 LDLIBS = -lcursesw -ltls
11 11
 
12
+BINS = catgirl
13
+MANS = catgirl.1
14
+
12 15
 -include config.mk
13 16
 
14 17
 OBJS += chat.o
@@ -29,7 +32,7 @@ OBJS += url.o
29 32
 TESTS += format.t
30 33
 TESTS += term.t
31 34
 
32
-all: tags catgirl test
35
+all: tags $(BINS) test
33 36
 
34 37
 $(OBJS): chat.h
35 38
 
@@ -47,14 +50,14 @@ test: $(TESTS)
47 50
 tags: *.h *.c
48 51
 	ctags -w *.h *.c
49 52
 
50
-install: catgirl catgirl.1
53
+install: $(BINS) $(MANS)
51 54
 	install -d $(PREFIX)/bin $(MANDIR)1
52
-	install catgirl $(PREFIX)/bin/catgirl
53
-	install -m 644 catgirl.1 $(MANDIR)1/catgirl.1
55
+	install $(BINS) $(PREFIX)/bin
56
+	install -m 644 $(MANS) $(MANDIR)1
54 57
 
55 58
 uninstall:
56
-	rm -f $(PREFIX)/bin/catgirl
57
-	rm -f $(MANDIR)1/catgirl.1
59
+	rm -f $(BINS:%=$(PREFIX)/bin/%)
60
+	rm -f $(MAN:%=%(MANDIR)1/%)
58 61
 
59 62
 chroot.tar: catgirl catgirl.1 man.sh
60 63
 	install -d -o root -g wheel \
@@ -92,7 +95,7 @@ install-chroot: chroot.tar
92 95
 	tar -xf chroot.tar -C /home/$(CHROOT_USER)
93 96
 
94 97
 clean:
95
-	rm -fr $(OBJS) catgirl $(TESTS) tags root chroot.tar
98
+	rm -fr $(OBJS) $(BINS) $(TESTS) tags root chroot.tar
96 99
 
97 100
 README: catgirl.7
98 101
 	mandoc catgirl.7 | col -bx > README

+ 34
- 0
sandman.1 View File

@@ -0,0 +1,34 @@
1
+.Dd November 30, 2018
2
+.Dt SANDMAN 1
3
+.Os "Causal Agency"
4
+.
5
+.Sh NAME
6
+.Nm sandman
7
+.Nd signal sleep
8
+.
9
+.Sh SYNOPSIS
10
+.Nm
11
+.Ar command ...
12
+.
13
+.Sh DESCRIPTION
14
+.Nm
15
+is a utility for Darwin systems.
16
+.Nm
17
+runs the
18
+.Ar command
19
+in a child process
20
+and sends it
21
+.Dv SIGTSTP
22
+and
23
+.Dv SIGCONT
24
+signals
25
+when the computer goes to sleep
26
+and wakes from sleep,
27
+respectively.
28
+.
29
+.Sh EXIT STATUS
30
+.Nm
31
+exits with the exit status of the child process.
32
+.
33
+.Sh SEE ALSO
34
+.Xr signal 3

+ 72
- 0
sandman.m View File

@@ -0,0 +1,72 @@
1
+/* Copyright (C) 2018  Curtis McEnroe <june@causal.agency>
2
+ *
3
+ * This program is free software: you can redistribute it and/or modify
4
+ * it under the terms of the GNU Affero General Public License as published by
5
+ * the Free Software Foundation, either version 3 of the License, or
6
+ * (at your option) any later version.
7
+ *
8
+ * This program is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+ * GNU Affero General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Affero General Public License
14
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+ */
16
+
17
+#import <Cocoa/Cocoa.h>
18
+#import <err.h>
19
+#import <signal.h>
20
+#import <stdlib.h>
21
+#import <sysexits.h>
22
+#import <unistd.h>
23
+
24
+void sigchld(int sig) {
25
+	(void)sig;
26
+	int status;
27
+	pid_t pid = wait(&status);
28
+	if (pid < 0) err(EX_OSERR, "wait");
29
+	if (WIFSIGNALED(status)) {
30
+		_exit(128 + WTERMSIG(status));
31
+	} else {
32
+		_exit(WEXITSTATUS(status));
33
+	}
34
+}
35
+
36
+int main(int argc, char *argv[]) {
37
+	if (argc < 2) return EX_USAGE;
38
+
39
+	signal(SIGCHLD, sigchld);
40
+
41
+	pid_t pid = fork();
42
+	if (pid < 0) err(EX_OSERR, "fork");
43
+
44
+	if (!pid) {
45
+		execvp(argv[1], &argv[1]);
46
+		err(EX_NOINPUT, "%s", argv[1]);
47
+	}
48
+
49
+	NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
50
+	NSNotificationCenter *center = [workspace notificationCenter];
51
+	NSOperationQueue *main = [NSOperationQueue mainQueue];
52
+
53
+	[center addObserverForName:NSWorkspaceWillSleepNotification
54
+						object:nil
55
+						 queue:main
56
+					usingBlock:^(NSNotification *note) {
57
+						(void)note;
58
+						int error = kill(pid, SIGTSTP);
59
+						if (error) err(EX_UNAVAILABLE, "kill %d", pid);
60
+					}];
61
+
62
+	[center addObserverForName:NSWorkspaceDidWakeNotification
63
+						object:nil
64
+						 queue:main
65
+					usingBlock:^(NSNotification *note) {
66
+						(void)note;
67
+						int error = kill(pid, SIGCONT);
68
+						if (error) err(EX_UNAVAILABLE, "kill %d", pid);
69
+					}];
70
+
71
+	[[NSApplication sharedApplication] run];
72
+}

Loading…
Cancel
Save