From 895fa4ed2acabaaf2c2a98d1af4033c8de1eb7e5 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Wed, 28 Aug 2019 17:51:55 -0400 Subject: Rename wat to ever --- bin/.gitignore | 2 +- bin/Makefile | 2 +- bin/README | 2 +- bin/bin.7 | 6 ++-- bin/ever.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/man1/ever.1 | 35 +++++++++++++++++++++ bin/man1/wat.1 | 35 --------------------- bin/wat.c | 97 --------------------------------------------------------- 8 files changed, 138 insertions(+), 138 deletions(-) create mode 100644 bin/ever.c create mode 100644 bin/man1/ever.1 delete mode 100644 bin/man1/wat.1 delete mode 100644 bin/wat.c (limited to 'bin') diff --git a/bin/.gitignore b/bin/.gitignore index d30ffdac..3bf8cbc0 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -6,6 +6,7 @@ bit bri config.mk dtch +ever fbatt fbclock glitch @@ -30,6 +31,5 @@ shotty tags ttpre up -wat when xx diff --git a/bin/Makefile b/bin/Makefile index de4fda51..6f7f89dc 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -36,7 +36,7 @@ LINKS_ANY += open LINKS_ANY += pbcopy LINKS_ANY += pbpaste -BINS_BSD += wat +BINS_BSD += ever BINS_LINUX += bri BINS_LINUX += fbatt diff --git a/bin/README b/bin/README index 5b398bff..ddca3e0b 100644 --- a/bin/README +++ b/bin/README @@ -12,6 +12,7 @@ DESCRIPTION bit(1) calculator bri(1) backlight brightness control dtch(1) detached sessions + ever(1) watch files fbatt(1) framebuffer battery indicator fbclock(1) framebuffer clock glitch(1) PNG glitcher @@ -29,7 +30,6 @@ DESCRIPTION shotty(1) terminal capture ttpre(1) man output to HTML up(1) upload file - wat(1) watch files when(1) date calculator xx(1) hexdump diff --git a/bin/bin.7 b/bin/bin.7 index c1f53ab8..af3bf225 100644 --- a/bin/bin.7 +++ b/bin/bin.7 @@ -31,6 +31,9 @@ backlight brightness control .It Xr dtch 1 detached sessions . +.It Xr ever 1 +watch files +. .It Xr fbatt 1 framebuffer battery indicator . @@ -82,9 +85,6 @@ man output to HTML .It Xr up 1 upload file . -.It Xr wat 1 -watch files -. .It Xr when 1 date calculator . diff --git a/bin/ever.c b/bin/ever.c new file mode 100644 index 00000000..9fd48f96 --- /dev/null +++ b/bin/ever.c @@ -0,0 +1,97 @@ +/* Copyright (C) 2017 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +static void watch(int kq, char *path) { + int fd = open(path, O_CLOEXEC); + if (fd < 0) err(EX_NOINPUT, "%s", path); + + struct kevent event; + EV_SET( + &event, + fd, + EVFILT_VNODE, + EV_ADD | EV_CLEAR, + NOTE_WRITE | NOTE_DELETE, + 0, + path + ); + int nevents = kevent(kq, &event, 1, NULL, 0, NULL); + if (nevents < 0) err(EX_OSERR, "kevent"); +} + +static void exec(char *const argv[]) { + pid_t pid = fork(); + if (pid < 0) err(EX_OSERR, "fork"); + + if (!pid) { + execvp(*argv, argv); + err(EX_NOINPUT, "%s", *argv); + } + + int status; + pid = wait(&status); + if (pid < 0) err(EX_OSERR, "wait"); + + if (WIFEXITED(status)) { + warnx("exit %d\n", WEXITSTATUS(status)); + } else if (WIFSIGNALED(status)) { + warnx("signal %d\n", WTERMSIG(status)); + } else { + warnx("status %d\n", status); + } +} + +int main(int argc, char *argv[]) { + if (argc < 3) return EX_USAGE; + + int kq = kqueue(); + if (kq < 0) err(EX_OSERR, "kqueue"); + + int i; + for (i = 1; i < argc - 1; ++i) { + if (argv[i][0] == '-') { + i++; + break; + } + watch(kq, argv[i]); + } + + exec(&argv[i]); + + for (;;) { + struct kevent event; + int nevents = kevent(kq, NULL, 0, &event, 1, NULL); + if (nevents < 0) err(EX_OSERR, "kevent"); + + if (event.fflags & NOTE_DELETE) { + close(event.ident); + sleep(1); + watch(kq, (char *)event.udata); + } + + exec(&argv[i]); + } +} diff --git a/bin/man1/ever.1 b/bin/man1/ever.1 new file mode 100644 index 00000000..cde6d9b9 --- /dev/null +++ b/bin/man1/ever.1 @@ -0,0 +1,35 @@ +.Dd August 28, 2019 +.Dt EVER 1 +.Os +. +.Sh NAME +.Nm ever +.Nd watch files +. +.Sh SYNOPSIS +.Nm +.Ar +.Ar command +.Nm +.Ar +.Fl - +.Ar command +.Op Ar argument ... +. +.Sh DESCRIPTION +.Nm +executes the +.Ar command +whenever +.Ar file +is modified. +. +.Sh EXAMPLES +.Dl ever ever.c make +.Dl ever when.y ever.c -- make when ever +. +.Sh CAVEATS +.Nm +does not support Linux +since it uses +.Xr kqueue 2 . diff --git a/bin/man1/wat.1 b/bin/man1/wat.1 deleted file mode 100644 index f4ef4ed6..00000000 --- a/bin/man1/wat.1 +++ /dev/null @@ -1,35 +0,0 @@ -.Dd January 1, 2018 -.Dt WAT 1 -.Os -. -.Sh NAME -.Nm wat -.Nd watch files -. -.Sh SYNOPSIS -.Nm -.Ar -.Ar command -.Nm -.Ar -.Fl - -.Ar command -.Op Ar argument ... -. -.Sh DESCRIPTION -.Nm -executes the -.Ar command -each time a -.Ar file -is modified. -. -.Sh EXAMPLES -.Dl wat wat.c make -.Dl wat wat.c wake.c -- make wat wake -. -.Sh CAVEATS -.Nm -does not support Linux -since it uses -.Xr kqueue 2 . diff --git a/bin/wat.c b/bin/wat.c deleted file mode 100644 index 9fd48f96..00000000 --- a/bin/wat.c +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright (C) 2017 C. McEnroe - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero 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 Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#include - -#include -#include -#include -#include -#include -#include -#include - -static void watch(int kq, char *path) { - int fd = open(path, O_CLOEXEC); - if (fd < 0) err(EX_NOINPUT, "%s", path); - - struct kevent event; - EV_SET( - &event, - fd, - EVFILT_VNODE, - EV_ADD | EV_CLEAR, - NOTE_WRITE | NOTE_DELETE, - 0, - path - ); - int nevents = kevent(kq, &event, 1, NULL, 0, NULL); - if (nevents < 0) err(EX_OSERR, "kevent"); -} - -static void exec(char *const argv[]) { - pid_t pid = fork(); - if (pid < 0) err(EX_OSERR, "fork"); - - if (!pid) { - execvp(*argv, argv); - err(EX_NOINPUT, "%s", *argv); - } - - int status; - pid = wait(&status); - if (pid < 0) err(EX_OSERR, "wait"); - - if (WIFEXITED(status)) { - warnx("exit %d\n", WEXITSTATUS(status)); - } else if (WIFSIGNALED(status)) { - warnx("signal %d\n", WTERMSIG(status)); - } else { - warnx("status %d\n", status); - } -} - -int main(int argc, char *argv[]) { - if (argc < 3) return EX_USAGE; - - int kq = kqueue(); - if (kq < 0) err(EX_OSERR, "kqueue"); - - int i; - for (i = 1; i < argc - 1; ++i) { - if (argv[i][0] == '-') { - i++; - break; - } - watch(kq, argv[i]); - } - - exec(&argv[i]); - - for (;;) { - struct kevent event; - int nevents = kevent(kq, NULL, 0, &event, 1, NULL); - if (nevents < 0) err(EX_OSERR, "kevent"); - - if (event.fflags & NOTE_DELETE) { - close(event.ident); - sleep(1); - watch(kq, (char *)event.udata); - } - - exec(&argv[i]); - } -} -- cgit 1.4.1