From 8274be77a0b01f3e29bd1137be77d82077c038a3 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sun, 9 Jul 2023 21:08:01 -0400 Subject: Move sandman to top-level, add --enable-sandman --- .gitignore | 1 + Makefile | 22 ++++++++++---- README.7 | 15 +++++----- configure | 1 + sandman.1 | 36 ++++++++++++++++++++++ sandman.m | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/.gitignore | 1 - scripts/Makefile | 22 -------------- scripts/sandman.1 | 36 ---------------------- scripts/sandman.m | 87 ------------------------------------------------------ 10 files changed, 149 insertions(+), 159 deletions(-) create mode 100644 sandman.1 create mode 100644 sandman.m delete mode 100644 scripts/.gitignore delete mode 100644 scripts/Makefile delete mode 100644 scripts/sandman.1 delete mode 100644 scripts/sandman.m diff --git a/.gitignore b/.gitignore index b31d1c5..519791d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ catgirl chroot.tar config.mk root/ +sandman tags diff --git a/Makefile b/Makefile index 3abba03..66fb408 100644 --- a/Makefile +++ b/Makefile @@ -8,9 +8,13 @@ CFLAGS += ${CEXTS:%=-Wno-%} LDADD.libtls = -ltls LDADD.ncursesw = -lncursesw +BINS = catgirl +MANS = ${BINS:=.1} + -include config.mk LDLIBS = ${LDADD.libtls} ${LDADD.ncursesw} +LDLIBS.sandman = -framework Cocoa OBJS += buffer.o OBJS += chat.o @@ -28,11 +32,13 @@ OBJS += url.o OBJS += window.o OBJS += xdg.o +OBJS.sandman = sandman.o + TESTS += edit.t dev: tags all check -all: catgirl +all: ${BINS} catgirl: ${OBJS} ${CC} ${LDFLAGS} ${OBJS} ${LDLIBS} -o $@ @@ -41,6 +47,9 @@ ${OBJS}: chat.h edit.o edit.t input.o: edit.h +sandman: ${OBJS.sandman} + ${CC} ${LDFLAGS} ${OBJS.$@} ${LDLIBS.$@} -o $@ + check: ${TESTS} .SUFFIXES: .t @@ -53,15 +62,16 @@ tags: *.[ch] ctags -w *.[ch] clean: - rm -f catgirl ${OBJS} ${TESTS} tags + rm -f ${BINS} ${OBJS} ${OBJS.sandman} ${TESTS} tags -install: catgirl catgirl.1 +install: ${BINS} ${MANS} install -d ${DESTDIR}${BINDIR} ${DESTDIR}${MANDIR}/man1 - install catgirl ${DESTDIR}${BINDIR} - install -m 644 catgirl.1 ${DESTDIR}${MANDIR}/man1 + install ${BINS} ${DESTDIR}${BINDIR} + install -m 644 ${MANS} ${DESTDIR}${MANDIR}/man1 uninstall: - rm -f ${DESTDIR}${BINDIR}/catgirl ${DESTDIR}${MANDIR}/man1/catgirl.1 + rm -f ${BINS:%=${DESTDIR}${BINDIR}/%} + rm -f ${MANS:%=${DESTDIR}${MANDIR}/man1/%} CHROOT_USER = chat CHROOT_GROUP = ${CHROOT_USER} diff --git a/README.7 b/README.7 index 6c3a536..a26d270 100644 --- a/README.7 +++ b/README.7 @@ -1,5 +1,5 @@ .\" To view this file: $ man ./README.7 -.Dd July 30, 2022 +.Dd July 9, 2023 .Dt README 7 .Os "Causal Agency" . @@ -173,10 +173,10 @@ wrapper is provided for macOS to stop and start .Nm on system sleep and wake. -Install it as follows: +To enable it, +configure with: .Bd -literal -offset indent -$ make -C scripts sandman -# make -C scripts install +$ ./configure --enable-sandman .Ed . .Sh FILES @@ -213,6 +213,8 @@ chat logging configuration parsing .It Pa xdg.c XDG base directories +.It Pa sandman.m +sleep/wake wrapper for macOS .El . .Pp @@ -222,8 +224,6 @@ example .Xr tmux 1 configuration for multiple networks and automatic reconnects -.It Pa scripts/sandman.m -sleep/wake wrapper for macOS .It Pa scripts/notify-send.scpt .Xr notify-send 1 in AppleScript @@ -257,7 +257,8 @@ Monetary contributions can be .Lk https://liberapay.com/june/donate "donated via Liberapay" . . .Sh SEE ALSO -.Xr catgirl 1 +.Xr catgirl 1 , +.Xr sandman 1 . .Pp IRC bouncer: diff --git a/configure b/configure index 9465b77..07e3245 100755 --- a/configure +++ b/configure @@ -29,6 +29,7 @@ for opt; do (--prefix=*) echo "PREFIX = ${opt#*=}" ;; (--bindir=*) echo "BINDIR = ${opt#*=}" ;; (--mandir=*) echo "MANDIR = ${opt#*=}" ;; + (--enable-sandman) echo 'BINS += sandman' ;; (*) echo "warning: unsupported option ${opt}" >&2 ;; esac done diff --git a/sandman.1 b/sandman.1 new file mode 100644 index 0000000..92828c0 --- /dev/null +++ b/sandman.1 @@ -0,0 +1,36 @@ +.Dd February 12, 2020 +.Dt SANDMAN 1 +.Os +. +.Sh NAME +.Nm sandman +.Nd sleep wrapper +. +.Sh SYNOPSIS +.Nm +.Op Fl t Ar secs +.Ar command ... +. +.Sh DESCRIPTION +The +.Nm +utility wraps a +.Ar command , +sending it +.Dv SIGHUP +when the system goes to sleep, +and restarting it +when the system wakes. +. +.Pp +The arguments are as follows: +.Bl -tag -width Ds +.It Fl t Ar secs +Wait for +.Ar secs +before restarting the process. +The default is 8 seconds. +.El +. +.Sh AUTHORS +.An June McEnroe Aq Mt june@causal.agency diff --git a/sandman.m b/sandman.m new file mode 100644 index 0000000..2e5c4db --- /dev/null +++ b/sandman.m @@ -0,0 +1,87 @@ +/* Copyright (C) 2019, 2020 June McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#import +#import +#import +#import +#import +#import +#import + +typedef unsigned uint; + +static pid_t pid; +static void spawn(char *argv[]) { + pid = fork(); + if (pid < 0) err(EX_OSERR, "fork"); + if (pid) return; + execvp(argv[0], argv); + err(EX_CONFIG, "%s", argv[0]); +} + +static void handler(int signal) { + (void)signal; + int status; + pid_t pid = wait(&status); + if (pid < 0) _exit(EX_OSERR); + _exit(status); +} + +int main(int argc, char *argv[]) { + uint delay = 8; + + for (int opt; 0 < (opt = getopt(argc, argv, "t:"));) { + switch (opt) { + break; case 't': delay = strtoul(optarg, NULL, 10); + break; default: return EX_USAGE; + } + } + argc -= optind; + argv += optind; + if (!argc) errx(EX_USAGE, "command required"); + + NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; + NSNotificationCenter *notifCenter = [workspace notificationCenter]; + + [notifCenter addObserverForName:NSWorkspaceWillSleepNotification + object:nil + queue:nil + usingBlock:^(NSNotification *notif) { + (void)notif; + signal(SIGCHLD, SIG_IGN); + int error = kill(pid, SIGHUP); + if (error) err(EX_UNAVAILABLE, "kill"); + int status; + wait(&status); + }]; + + [notifCenter addObserverForName:NSWorkspaceDidWakeNotification + object:nil + queue:nil + usingBlock:^(NSNotification *notif) { + (void)notif; + warnx("waiting %u seconds...", delay); + sleep(delay); + signal(SIGCHLD, handler); + spawn(argv); + }]; + + signal(SIGCHLD, handler); + spawn(argv); + + [[NSApplication sharedApplication] run]; +} diff --git a/scripts/.gitignore b/scripts/.gitignore deleted file mode 100644 index f6dc107..0000000 --- a/scripts/.gitignore +++ /dev/null @@ -1 +0,0 @@ -sandman diff --git a/scripts/Makefile b/scripts/Makefile deleted file mode 100644 index 179a2d3..0000000 --- a/scripts/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -PREFIX ?= /usr/local -BINDIR ?= ${PREFIX}/bin -MANDIR ?= ${PREFIX}/man - -CFLAGS += -Wall -Wextra - --include ../config.mk - -LDLIBS = -framework Cocoa - -all: sandman - -clean: - rm -f sandman - -install: sandman sandman.1 - install -d ${DESTDIR}${BINDIR} ${DESTDIR}${MANDIR}/man1 - install sandman ${DESTDIR}${BINDIR} - install -m 644 sandman.1 ${DESTDIR}${MANDIR}/man1 - -uninstall: - rm -f ${DESTDIR}${BINDIR}/sandman ${DESTDIR}/man/man1/sandman.1 diff --git a/scripts/sandman.1 b/scripts/sandman.1 deleted file mode 100644 index 92828c0..0000000 --- a/scripts/sandman.1 +++ /dev/null @@ -1,36 +0,0 @@ -.Dd February 12, 2020 -.Dt SANDMAN 1 -.Os -. -.Sh NAME -.Nm sandman -.Nd sleep wrapper -. -.Sh SYNOPSIS -.Nm -.Op Fl t Ar secs -.Ar command ... -. -.Sh DESCRIPTION -The -.Nm -utility wraps a -.Ar command , -sending it -.Dv SIGHUP -when the system goes to sleep, -and restarting it -when the system wakes. -. -.Pp -The arguments are as follows: -.Bl -tag -width Ds -.It Fl t Ar secs -Wait for -.Ar secs -before restarting the process. -The default is 8 seconds. -.El -. -.Sh AUTHORS -.An June McEnroe Aq Mt june@causal.agency diff --git a/scripts/sandman.m b/scripts/sandman.m deleted file mode 100644 index 2e5c4db..0000000 --- a/scripts/sandman.m +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright (C) 2019, 2020 June McEnroe - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#import -#import -#import -#import -#import -#import -#import - -typedef unsigned uint; - -static pid_t pid; -static void spawn(char *argv[]) { - pid = fork(); - if (pid < 0) err(EX_OSERR, "fork"); - if (pid) return; - execvp(argv[0], argv); - err(EX_CONFIG, "%s", argv[0]); -} - -static void handler(int signal) { - (void)signal; - int status; - pid_t pid = wait(&status); - if (pid < 0) _exit(EX_OSERR); - _exit(status); -} - -int main(int argc, char *argv[]) { - uint delay = 8; - - for (int opt; 0 < (opt = getopt(argc, argv, "t:"));) { - switch (opt) { - break; case 't': delay = strtoul(optarg, NULL, 10); - break; default: return EX_USAGE; - } - } - argc -= optind; - argv += optind; - if (!argc) errx(EX_USAGE, "command required"); - - NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; - NSNotificationCenter *notifCenter = [workspace notificationCenter]; - - [notifCenter addObserverForName:NSWorkspaceWillSleepNotification - object:nil - queue:nil - usingBlock:^(NSNotification *notif) { - (void)notif; - signal(SIGCHLD, SIG_IGN); - int error = kill(pid, SIGHUP); - if (error) err(EX_UNAVAILABLE, "kill"); - int status; - wait(&status); - }]; - - [notifCenter addObserverForName:NSWorkspaceDidWakeNotification - object:nil - queue:nil - usingBlock:^(NSNotification *notif) { - (void)notif; - warnx("waiting %u seconds...", delay); - sleep(delay); - signal(SIGCHLD, handler); - spawn(argv); - }]; - - signal(SIGCHLD, handler); - spawn(argv); - - [[NSApplication sharedApplication] run]; -} -- cgit 1.4.1