aboutsummaryrefslogtreecommitdiff
path: root/sh/profile.d
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-21 23:02:35 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-21 23:02:35 +1200
commitcc7a77804b12d86251e71303d1887b48c43f13b2 (patch)
treebf3747118cc0b96e78b2a615aea70bb71c54fc20 /sh/profile.d
parentDistill/fix keychain handling a bit (diff)
downloaddotfiles-cc7a77804b12d86251e71303d1887b48c43f13b2.tar.gz
dotfiles-cc7a77804b12d86251e71303d1887b48c43f13b2.zip
Consolidate ed,bc,grep,ls.sh into one function
Diffstat (limited to 'sh/profile.d')
-rw-r--r--sh/profile.d/bc.sh22
-rw-r--r--sh/profile.d/ed.sh22
-rw-r--r--sh/profile.d/grep.sh27
-rw-r--r--sh/profile.d/ls.sh27
-rw-r--r--sh/profile.d/options.sh55
5 files changed, 55 insertions, 98 deletions
diff --git a/sh/profile.d/bc.sh b/sh/profile.d/bc.sh
deleted file mode 100644
index 16200b33..00000000
--- a/sh/profile.d/bc.sh
+++ /dev/null
@@ -1,22 +0,0 @@
-# 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/profile.d/ed.sh b/sh/profile.d/ed.sh
deleted file mode 100644
index abbd75c5..00000000
--- a/sh/profile.d/ed.sh
+++ /dev/null
@@ -1,22 +0,0 @@
-# Test that we have metadata about what options this system's ed(1) supports,
-# and try to create it if not
-(
- # Create a directory to hold metadata about ed
- ecd=$HOME/.cache/ed
- if ! [ -d "$ecd" ] ; then
- mkdir -p -- "$ecd" || exit
- fi
-
- # Write ed(1)'s --help output to a file, even if it's empty
- if ! [ -f "$ecd"/help ] ; then
- ed --help </dev/null >"$ecd"/help 2>/dev/null || exit
-
- # Iterate through some useful options and create files to show they're
- # available
- set -- verbose
- for opt ; do
- grep -q -- --"$opt" "$ecd"/help || continue
- touch -- "$ecd"/"$opt" || exit
- done
- fi
-)
diff --git a/sh/profile.d/grep.sh b/sh/profile.d/grep.sh
deleted file mode 100644
index d6f9c36b..00000000
--- a/sh/profile.d/grep.sh
+++ /dev/null
@@ -1,27 +0,0 @@
-# Test that we have metadata about what options this system's grep(1) supports,
-# and try to create it if not
-(
- # Create a directory to hold metadata about grep
- gcd=$HOME/.cache/grep
- if ! [ -d "$gcd" ] ; then
- mkdir -p -- "$gcd" || exit
- fi
-
- # Write grep(1)'s --help output to a file, even if it's empty
- if ! [ -f "$gcd"/help ] ; then
- grep --help </dev/null >"$gcd"/help 2>/dev/null || exit
-
- # Iterate through some useful options and create files to show they're
- # available
- set -- binary-files \
- color \
- devices \
- directories \
- exclude \
- exclude-dir
- for opt ; do
- grep -q -- --"$opt" "$gcd"/help || continue
- touch -- "$gcd"/"$opt" || exit
- done
- fi
-)
diff --git a/sh/profile.d/ls.sh b/sh/profile.d/ls.sh
deleted file mode 100644
index 2b0f507d..00000000
--- a/sh/profile.d/ls.sh
+++ /dev/null
@@ -1,27 +0,0 @@
-# Test that we have metadata about what options this system's ls(1) supports,
-# and try to create it if not
-(
- # Create a directory to hold metadata about ls(1)
- lcd=$HOME/.cache/ls
- if ! [ -d "$lcd" ] ; then
- mkdir -p -- "$lcd" || exit
- fi
-
- # Write ls(1)'s --help output to a file, even if it's empty
- if ! [ -f "$lcd"/help ] ; then
- ls --help >"$lcd"/help 2>/dev/null || exit
-
- # Iterate through some useful options and create files to show they're
- # available
- set -- almost-all \
- block-size \
- classify \
- color \
- human-readable \
- time-style
- for opt ; do
- grep -q -- --"$opt" "$lcd"/help || continue
- touch -- "$lcd"/"$opt" || exit
- done
- fi
-)
diff --git a/sh/profile.d/options.sh b/sh/profile.d/options.sh
new file mode 100644
index 00000000..aa6bea06
--- /dev/null
+++ b/sh/profile.d/options.sh
@@ -0,0 +1,55 @@
+# Cache the options available to certain programs. Run all this in a subshell
+# (none of its state needs to endure in the session)
+(
+options() { (
+
+ # Check or create the directory to cache the options
+ dir=$HOME/.cache/$1
+
+ # Directory already exists; bail out
+ [ -d "$dir" ] && exit
+
+ # Create the directory
+ mkdir -p -- "$dir" || exit
+ cd -- "$dir" || exit
+
+ # Write grep(1)'s --help output to a file, even if it's empty
+ "$1" --help </dev/null >help 2>/dev/null || exit
+
+ # Shift the program name off; remaining arguments are the options to check
+ shift
+
+ # Iterate through some useful options and create files to show they're
+ # available if found in the help output
+ for opt ; do
+ grep -q -- '[^[:alnum:]]--'"$opt"'[^[:alnum:]]' help &&
+ touch -- "$opt"
+ done
+) ; }
+
+# Cache options for bc(1)
+options bc \
+ quiet
+
+# Cache options for ed(1)
+options ed \
+ verbose
+
+# Cache options for grep(1)
+options grep \
+ binary-files \
+ color \
+ devices \
+ directories \
+ exclude \
+ exclude-dir
+
+# Cache options for ls(1)
+options ls \
+ almost-all \
+ block-size \
+ classify \
+ color \
+ human-readable \
+ time-style
+)