aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-21 17:43:58 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-21 17:43:58 +1200
commit2d392c27d78cce8c7b663a17abbd1c699e997b54 (patch)
tree4055783425860cb192f83ac8af0ac82a895ef320
parent06eae861a4abf6d72ac1cb1afff23276044963b7 (diff)
downloaddotfiles-2d392c27d78cce8c7b663a17abbd1c699e997b54.tar.gz
dotfiles-2d392c27d78cce8c7b663a17abbd1c699e997b54.zip
Cache --quiet option for bc(1) in flag file
As done for grep(1) and ls(1); ed(1) is next
-rw-r--r--sh/profile.d/bc.sh22
-rw-r--r--sh/shrc.d/bc.sh15
2 files changed, 33 insertions, 4 deletions
diff --git a/sh/profile.d/bc.sh b/sh/profile.d/bc.sh
new file mode 100644
index 00000000..16200b33
--- /dev/null
+++ b/sh/profile.d/bc.sh
@@ -0,0 +1,22 @@
+# Test that we have metadata about what options this system's bc(1) supports,
+# and try to create it if not
+(
+ # Create a directory to hold metadata about bc
+ bcd=$HOME/.cache/bc
+ if ! [ -d "$bcd" ] ; then
+ mkdir -p -- "$bcd" || exit
+ fi
+
+ # Write bc(1)'s --help output to a file, even if it's empty
+ if ! [ -f "$bcd"/help ] ; then
+ bc --help </dev/null >"$bcd"/help 2>/dev/null || exit
+
+ # Iterate through some useful options and create files to show they're
+ # available
+ set -- quiet
+ for opt ; do
+ grep -q -- --"$opt" "$bcd"/help || continue
+ touch -- "$bcd"/"$opt" || exit
+ done
+ fi
+)
diff --git a/sh/shrc.d/bc.sh b/sh/shrc.d/bc.sh
index 643678ac..41331ff9 100644
--- a/sh/shrc.d/bc.sh
+++ b/sh/shrc.d/bc.sh
@@ -1,7 +1,14 @@
-# This function is only applicable if bc(1) has the non-POSIX -q option
-command bc -q </dev/null >&0 2>&0 || return
+# Our ~/.profile should already have made a directory with the supported
+# options for us; if not, we won't be wrapping bc(1) with a function at all
+[ -d "$HOME"/.cache/bc ] || return
-# Don't print the bc(1) welcome message
+# Define function proper
bc() {
- command bc -q "$@"
+
+ # Add --quiet to stop the annoying welcome banner
+ [ -e "$HOME"/.cache/bc/quiet ] &&
+ set -- --quiet "$@"
+
+ # Run bc(1) with the concluded arguments
+ command bc "$@"
}