aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--Makefile4
-rw-r--r--README.markdown1
-rw-r--r--bin/oii.mi519
-rw-r--r--man/man1/oii.1df21
5 files changed, 48 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index c4a552f8..bf219412 100644
--- a/.gitignore
+++ b/.gitignore
@@ -67,6 +67,9 @@ bin/motd
bin/murl
bin/mw
bin/nlbr
+bin/oii
+bin/oii.sh
+bin/oii.m4
bin/onl
bin/osc
bin/p
diff --git a/Makefile b/Makefile
index 8818e524..2adc814b 100644
--- a/Makefile
+++ b/Makefile
@@ -138,6 +138,7 @@ BINS = bin/ap \
bin/murl \
bin/mw \
bin/nlbr \
+ bin/oii \
bin/onl \
bin/osc \
bin/pa \
@@ -203,6 +204,7 @@ BINS = bin/ap \
BINS_M4 = bin/chn.m4 \
bin/edda.m4 \
+ bin/oii.m4 \
bin/pst.m4 \
bin/rndl.m4 \
bin/swr.m4 \
@@ -212,6 +214,7 @@ BINS_M4 = bin/chn.m4 \
BINS_SH = bin/chn.sh \
bin/edda.sh \
+ bin/oii.sh \
bin/pst.sh \
bin/rndl.sh \
bin/swr.sh \
@@ -277,6 +280,7 @@ clean distclean:
bin/chn.sh: bin/chn.m4 include/mktd.m4
bin/edda.sh: bin/edda.m4 include/mktd.m4
+bin/oii.sh: bin/oii.m4 include/mktd.m4
bin/pst.sh: bin/pst.m4 include/mktd.m4
bin/rndl.sh: bin/rndl.m4 include/mktd.m4
bin/swr.sh: bin/swr.m4 include/mktd.m4
diff --git a/README.markdown b/README.markdown
index bd4d482a..c99dcf6d 100644
--- a/README.markdown
+++ b/README.markdown
@@ -499,6 +499,7 @@ Installed by the `install-bin` target:
* `motd(1df)` shows the system MOTD.
* `mw(1df)` prints alphabetic space-delimited words from the input one per
line.
+* `oii(1df)` runs a command on input only if there is any.
* `onl(1df)` crunches input down to one printable line.
* `osc(1df)` implements a `netcat(1)`-like wrapper for `openssl(1)`'s
`s_client` subcommand.
diff --git a/bin/oii.mi5 b/bin/oii.mi5
new file mode 100644
index 00000000..51f37fb4
--- /dev/null
+++ b/bin/oii.mi5
@@ -0,0 +1,19 @@
+# Only run a command on input if there was at least one byte
+self=oii
+
+# Need at least a command name
+if [ "$#" -eq 0 ] ; then
+ printf >&2 '%s: Need a command\n' "$self"
+ exit 2
+fi
+
+<%
+include(`include/mktd.m4')
+%>
+
+# There is probably a way better way to do this than writing the whole file to
+# disk and then reading it off again, but until I think of something better,
+# this works and is byte-safe.
+cat - > "$td"/in
+[ -s "$td"/in ] || exit
+"$@" < "$td"/in
diff --git a/man/man1/oii.1df b/man/man1/oii.1df
new file mode 100644
index 00000000..f5bb2678
--- /dev/null
+++ b/man/man1/oii.1df
@@ -0,0 +1,21 @@
+.TH OII 1df "June 2017" "Manual page for oii"
+.SH NAME
+.B oii
+\- run a command on input only if there's at least one byte of input
+.SH USAGE
+.B oii
+CMD [ARGS ...] < file
+.br
+program |
+.B oii
+CMD [ARGS ...]
+.SH DESCRIPTION
+Run the given program passing in stdin but only if at least one byte of input
+is actually received, rather like the -E switch to mail(1) behaves on
+bsd-mailx. If no input is received, exit silently with an error status.
+.SH CAVEATS
+It's slow, and doesn't work as a pipe. The entire input is written to disk and
+then tested for filesize before being re-emitted. There's almost certainly a
+more efficient way to do this while still remaining byte-safe.
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>