aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-07-01 21:49:27 +1200
committerTom Ryder <tom@sanctum.geek.nz>2017-07-01 22:00:07 +1200
commit7adef2006aaff89db93c87fd1470a688fb1d4907 (patch)
tree25e967034dad315ca11b50b51b2d1088c65eaf00
parentdbc1be0317ad5e70afb895648f4d6c8f0fbc1080 (diff)
downloaddotfiles-7adef2006aaff89db93c87fd1470a688fb1d4907.tar.gz
dotfiles-7adef2006aaff89db93c87fd1470a688fb1d4907.zip
Reimplement rndl(1df) in Awk
Removes the need for the temporary file. Also refactor pks(6df) to accommodate it.
-rw-r--r--.gitignore2
-rw-r--r--Makefile3
-rw-r--r--bin/rndl.awk29
-rw-r--r--bin/rndl.mi538
-rw-r--r--games/pks.awk10
-rw-r--r--man/man1/rndl.1df2
6 files changed, 35 insertions, 49 deletions
diff --git a/.gitignore b/.gitignore
index ce1910c3..7e2ae706 100644
--- a/.gitignore
+++ b/.gitignore
@@ -95,8 +95,6 @@ bin/rnda
bin/rndf
bin/rndi
bin/rndl
-bin/rndl.sh
-bin/rndl.m4
bin/rnds
bin/sd2u
bin/sec
diff --git a/Makefile b/Makefile
index 28a58805..a5af63a9 100644
--- a/Makefile
+++ b/Makefile
@@ -205,7 +205,6 @@ BINS_M4 = bin/chn.m4 \
bin/edda.m4 \
bin/oii.m4 \
bin/pst.m4 \
- bin/rndl.m4 \
bin/swr.m4 \
bin/tlcs.m4 \
bin/try.m4 \
@@ -215,7 +214,6 @@ BINS_SH = bin/chn.sh \
bin/edda.sh \
bin/oii.sh \
bin/pst.sh \
- bin/rndl.sh \
bin/swr.sh \
bin/tlcs.sh \
bin/try.sh \
@@ -282,7 +280,6 @@ 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
bin/tlcs.sh: bin/tlcs.m4 include/mktd.m4
bin/try.sh: bin/try.m4 include/mktd.m4
diff --git a/bin/rndl.awk b/bin/rndl.awk
new file mode 100644
index 00000000..8359af90
--- /dev/null
+++ b/bin/rndl.awk
@@ -0,0 +1,29 @@
+# Print a random line from input
+
+# Process arguments
+BEGIN {
+
+ # Name self
+ self = "rndl"
+
+ # Seed the random number generator
+ "rnds 2>/dev/null" | getline seed
+ srand(seed)
+}
+
+# Iterate over the lines, randomly assigning the first field of each one with a
+# decreasing probability
+rand() * NR < 1 { ln = $0 }
+
+# Check and print
+END {
+
+ # Check that we processed at least one line
+ if (!NR) {
+ printf "%s: No lines found on input\n", self | "cat >&2"
+ exit(1)
+ }
+
+ # Print the line
+ print ln
+}
diff --git a/bin/rndl.mi5 b/bin/rndl.mi5
deleted file mode 100644
index f99ccbea..00000000
--- a/bin/rndl.mi5
+++ /dev/null
@@ -1,38 +0,0 @@
-# Print a random line from input
-self=rndl
-
-# If there are no arguments, we're checking stdin; this is more complicated
-# than checking file arguments because we have to count the lines in order to
-# correctly choose a random one, and two passes means we require a temporary
-# file if we don't want to read all of the input into memory (!)
-if [ "$#" -eq 0 ] ; then
-
-<%
-include(`include/mktd.m4')
-%>
-
- # We'll operate on stdin in the temp directory; write the script's stdin to
- # it with cat(1)
- set -- "$td"/stdin
- cat >"$td"/stdin
-fi
-
-# Count the number of lines in the input
-lc=$(sed -- '$=;d' "$@") || exit
-
-# If there were none, bail
-case $lc in
- ''|0)
- printf >&2 'rndl: No lines found on input\n'
- exit 2
- ;;
-esac
-
-# Try to get a random seed from rnds(1df) for rndi(1df)
-seed=$(rnds)
-
-# Get a random line number from rndi(1df)
-ri=$(rndi 1 "$lc" "$seed") || exit
-
-# Print the line using sed(1)
-sed -- "$ri"'!d' "$@"
diff --git a/games/pks.awk b/games/pks.awk
index c490e8dc..c7ff320d 100644
--- a/games/pks.awk
+++ b/games/pks.awk
@@ -13,12 +13,13 @@ BEGIN {
}
# Seed the random number generator
- srand()
+ "rnds 2>/dev/null" | getline seed
+ srand(seed)
}
# Iterate over the lines, randomly assigning the first field of each one with a
# decreasing probability; this method
-rand() * NR < 1 { wr = $1 }
+$1 ~ /[[:alpha:]]/ && rand() * ++n < 1 { wr = $1 }
# Ha, ha, ha! Incompetent!
END {
@@ -27,11 +28,10 @@ END {
if (!NR)
exit 1
- # Strip trailing possessives
- sub(/'s*$/, "", wr)
+ # Strip trailing possessives and punctuation
+ sub(/[^[:alpha:]]+s*$/, "", wr)
# Two or three "has"? Important decisions here folks
- srand()
hr = int(rand()*2+1)
for (ha = "Ha"; hi < hr; hi++)
ha = ha ", ha"
diff --git a/man/man1/rndl.1df b/man/man1/rndl.1df
index ec44564a..0e952724 100644
--- a/man/man1/rndl.1df
+++ b/man/man1/rndl.1df
@@ -13,7 +13,7 @@ command |
.B rndl
.SH DESCRIPTION
.B rndl
-prints a random line from its input, using rndi(1df) to choose it. This is
+prints a random line from its input, using rnds(1df) as a seed. This is
probably not a high-quality source, but should differ within seconds and
between runs on most systems.
.SH SEE ALSO