From b6dcf69a979a6502b97f4b18837d5889d8d31ab6 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 1 Jul 2017 20:11:44 +1200 Subject: First attempt at pks(6df) I've got a better idea, though --- .gitignore | 1 + Makefile | 1 + README.markdown | 1 + games/pks.awk | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ man/man6/pks.6df | 21 +++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 games/pks.awk create mode 100644 man/man6/pks.6df diff --git a/.gitignore b/.gitignore index 21d294c4..ce1910c3 100644 --- a/.gitignore +++ b/.gitignore @@ -151,6 +151,7 @@ games/chkl games/dr games/drakon games/kvlt +games/pks games/rndn games/rot13 games/squ diff --git a/Makefile b/Makefile index 22d5ac8e..28a58805 100644 --- a/Makefile +++ b/Makefile @@ -228,6 +228,7 @@ GAMES = games/aaf \ games/dr \ games/drakon \ games/kvlt \ + games/pks \ games/rndn \ games/rot13 \ games/squ \ diff --git a/README.markdown b/README.markdown index 30e94d10..830ca51d 100644 --- a/README.markdown +++ b/README.markdown @@ -554,6 +554,7 @@ There's some silly stuff in `install-games`: * `squ(6df)` makes a reduced Latin square out of each line of input. * `kvlt(6df)` translates input to emulate a style of typing unique to black metal communities on the internet. +* `pks(6df)` laughs at a randomly selected word. * `rndn(6df)` implements an esoteric random number generation algorithm. * `strik(6df)` outputs s̶t̶r̶i̶k̶e̶d̶ ̶o̶u̶t̶ struck out text. * `rot13(6df)` rotates the Latin letters in its input. diff --git a/games/pks.awk b/games/pks.awk new file mode 100644 index 00000000..ff316fb9 --- /dev/null +++ b/games/pks.awk @@ -0,0 +1,81 @@ +# Ha, ha, ha! Awk! + +# Print either two or three "has" and the given word, capitalized +function haha(wr) { + + # Two or three "has"? Important decisions here folks + srand() + hr = int(rand()*2+1) + for (ha = "Ha"; hi < hr; hi++) + ha = ha ", ha" + + # Capitalise the word + wr = toupper(substr(wr,1,1)) substr(wr,2) + + # Return the laughter and the word + return ha "! " wr "!" +} + +# Ha, ha! Failure! +function fail(str, ex) { + af = 1 + print haha(str) | "cat >&2" + exit(ex) +} + +# Process arguments +BEGIN { + + # If no arguments, assume a dictionary file + if (ARGC == 1) { + ARGC = 2 + if ("DICT" in ENVIRON) + ARGV[1] = ENVIRON["DICT"] + else + ARGV[1] = "/usr/share/dict/words" + } + + # Check the user hasn't tried stdin + for (ai = 1; ai < ARGC; ai++) + if (ARGV[ai] == "-") + fail("standard input", 2) + + # Count the number of lines in the files (pass 1) + for (ai = 1; ai < ARGC; ai++) + while (getline < ARGV[ai]) + lc++ + + # If all files were empty, we can't go on + if (lc == 0) + fail("no data", 1) + + # Pick a random line number + srand() + lr = int(lc*rand()+1) +} + +# Iterate over the file until we get to the selected line (pass 2) +NR >= lr { + + # Find the first word-looking thing + for (i = 1; !wr && i <= NF; i++) + if (tolower($i) ~ /[[:lower:]]/) + wr = $i + + # Strip trailing possessives + sub(/'s*$/, "", wr) + + # No word? Uh, better keep going + if (!length(wr)) + next + + # Ha, ha! Suboptimal! + print haha(wr) + exit(0) +} + +# Ha, ha, ha! Incompetent! +END { + if (!af && !length(wr)) + fail("error", 1) +} diff --git a/man/man6/pks.6df b/man/man6/pks.6df new file mode 100644 index 00000000..03ab7251 --- /dev/null +++ b/man/man6/pks.6df @@ -0,0 +1,21 @@ +.TH PKS 6df "July 2017" "Manual page for pks" +.SH NAME +.B pks +\- select and laugh at a random word from a system dictionary or fileset +.SH USAGE +.B pks +.br +.B pks +FILE1 FILE2 +.br +DICT=$HOME/dict +.B pks +.SH DESCRIPTION +.B pks +picks a random word from a set of files and laughs at it. If no files are +given, it defaults to /usr/share/dict/words, or the value of DICT (ha, ha!) if +specified in the environment. +.SH SEE ALSO + +.SH AUTHOR +Tom Ryder -- cgit v1.2.3