From 64ef1eddfd4fa79a0720945e189cf7c3a44bde9c Mon Sep 17 00:00:00 2001 From: dana Date: Mon, 4 Jun 2018 10:04:27 -0500 Subject: 42931: completion for several utilities especially for checksums across a variety of systems --- Completion/Unix/Command/_base64 | 59 ++++++++++++++++++++ Completion/Unix/Command/_chroot | 53 ++++++++++++++++++ Completion/Unix/Command/_cksum | 112 ++++++++++++++++++++++++++++++++++++++ Completion/Unix/Command/_lz4 | 103 +++++++++++++++++++++++++++++++++++ Completion/Unix/Command/_md5sum | 63 ++++++++++++++++----- Completion/Unix/Command/_readlink | 48 ++++++++++++++++ Completion/Unix/Command/_shasum | 24 ++++++++ Completion/Unix/Command/_tee | 32 +++++++++++ 8 files changed, 479 insertions(+), 15 deletions(-) create mode 100644 Completion/Unix/Command/_base64 create mode 100644 Completion/Unix/Command/_chroot create mode 100644 Completion/Unix/Command/_cksum create mode 100644 Completion/Unix/Command/_lz4 create mode 100644 Completion/Unix/Command/_readlink create mode 100644 Completion/Unix/Command/_shasum create mode 100644 Completion/Unix/Command/_tee (limited to 'Completion/Unix/Command') diff --git a/Completion/Unix/Command/_base64 b/Completion/Unix/Command/_base64 new file mode 100644 index 000000000..47a650343 --- /dev/null +++ b/Completion/Unix/Command/_base64 @@ -0,0 +1,59 @@ +#compdef base64 gbase64 base32 gbase32 + +# Note: This does NOT cover `uuencode`/`uudecode`, which on some BSD systems +# have the aliases `b64encode`/`b64decode` — those are sufficiently different +# that they belong in another function. It DOES cover GNU's `base32`, since it +# is essentially the same program as their `base64`. + +local variant type=base${service//[^2346]/} + +_pick_variant -r variant \ + gnu='Free Soft' fourmilab=fourmi darwin=-D unix --version + +case $variant in + gnu) + _arguments -s -S : \ + '(: -)--help[display help information]' \ + '(: -)--version[display version information]' \ + + dec \ + '(enc -d --decode)'{-d,--decode}"[decode input from $type]" \ + '(enc -i --ignore-garbage)'{-i,--ignore-garbage}'[ignore irrelevant characters when decoding]' \ + + '(enc)' \ + '(dec)'{-w+,--wrap=}'[wrap encoded lines at specified number of columns]:number of columns' \ + + in \ + '1:input file:_files' + return + ;; + darwin) + _arguments -s -S : \ + '(: -)'{-h,--help}'[display help information]' \ + + '(dec)' \ + '(enc)'{-D,--decode}"[decode input from $type]" \ + + '(enc)' \ + '(dec)'{-b+,--break=}'[wrap encoded lines at specified number of columns]:number of columns' \ + + '(out)' \ + {-o+,--output=}'[specify output file]:output file:_files' \ + + '(in)' \ + {-i+,--input=}'[specify input file]:input file:_files' \ + '1:input file:_files' + return + ;; + fourmilab) + _arguments -s -S : \ + '(: -)--copyright[display copyright information]' \ + '(: -)'{-u,--help}'[display help information]' \ + '(: -)--version[display version information]' \ + + dec \ + '(enc -d --decode)'{-d,--decode}"[decode input from $type]" \ + '(enc -n --noerrcheck)'{-n,--noerrcheck}'[ignore errors when decoding]' \ + + '(enc)' \ + '(dec)'{-e,--encode}"[encode input to $type]" \ + + io \ + '1:input file:_files' \ + '2:output file:_files' + return + ;; +esac + +# A few other implementations exist, though they are rarely encountered +_default diff --git a/Completion/Unix/Command/_chroot b/Completion/Unix/Command/_chroot new file mode 100644 index 000000000..516992694 --- /dev/null +++ b/Completion/Unix/Command/_chroot @@ -0,0 +1,53 @@ +#compdef chroot gchroot + +local variant ret=1 +local -a context line state state_descr args +local -A opt_args + +_pick_variant -r variant gnu='Free Soft' unix --version + +variant+=-$OSTYPE + +case $variant in + gnu-*) + args=( + '(: -)--help[display help information]' + '(: -)--version[display version information]' + '--groups=[specify supplemental group memberships]: :_sequence -s , _groups' + '--userspec=[specify user and group to run process as]: :->userspecs' + '--skip-chdir[do not change working directory to /]' + ) + ;; + *-openbsd*) + args=( + '-u+[specify user to run process as]: :_users' + '-g+[specify group to run process as, and supplemental group memberships]: :_sequence -s , _groups' + ) + ;; + *-(darwin|dragonfly|freebsd|netbsd)*) + args=( + '-u+[specify user to run process as]: :_users' + '-g+[specify group to run process as]: :_groups' + '-G+[specify supplemental group memberships]: :_sequence -s , _groups' + ) + ;; +esac + +args+=( '1:new root directory:_directories' '*:::command:_normal' ) + +_arguments -s -S : $args && ret=0 + +# @todo user:group specs are probably used often enough to justify making a type +# function for this (see also `chown`, `cpio`, `rsync`, ...) +[[ $state == userspecs ]] && +if compset -P '*:*:'; then + ret=1 +elif compset -P '*:'; then + _groups && ret=0 +elif compset -S ':*'; then + _users && ret=0 +else + _users -qS : && ret=0 +fi + +return ret diff --git a/Completion/Unix/Command/_cksum b/Completion/Unix/Command/_cksum new file mode 100644 index 000000000..bb2f60ac7 --- /dev/null +++ b/Completion/Unix/Command/_cksum @@ -0,0 +1,112 @@ +#compdef cksum gcksum sum gsum md2 md4 md5 rmd160 sha1 sha256 sha384 sha512 sha512t256 skein256 skein512 skein1024 + +# This function covers mostly the BSD `cksum`, `sum`, and digest utilities. It +# also covers the GNU Coreutils `cksum` and `sum`. For the GNU digest utilities, +# see `_md5sum`. For the `shasum` Perl script, see `_shasum`. +# +# Notes: +# - We really don't do a very good job of handling option exclusivity here. In +# particular, OpenBSD treats -t and -x as exclusive of each other and most +# other options — but there are issues with other variants too (@todo) +# - Although only OpenBSD's documentation mentions it, -t can be supplied +# multiple times with all variants to run additional rounds of testing +# - All digest variants on a given platform share code, but not all variants are +# found on all platforms (e.g., Darwin only has `md5`). Some BSDs are actively +# purging legacy/insecure digest tools +# - The documentation for Dragonfly/FreeBSD `sum` says that it's 'identical to' +# `cksum`, but, as the synopsis confirms, it doesn't take any options +# - FreeBSD's -c is never useful, and Dragonfly's -b/-e are *almost* never +# useful, with multiple input files +# - NetBSD's -n isn't useful with `sum` and `cksum` unless a digest is specified +# with -a. Similarly, OpenBSD's -b isn't useful with `cksum` without -a +# - OpenBSD's -a option allows you to add a b/x suffix to each algorithm name; +# we don't handle that. Also, only one -a option can be used in conjunction +# with -c; we don't handle that either + +local -a args + +_pick_variant gnu='Free Soft' unix --version && { + args=( + '*: :_files' + '(: -)--help[display help information]' + '(: -)--version[display version information]' + ) + [[ $service == *cksum* ]] || args+=( + '(-s --sysv)-r[use BSD algorithm (1 KiB blocks)]' + '(-r -s --sysv)'{-s,--sysv}'[use System V algorithm (512 B blocks)]' + ) + _arguments -s -S : $args + return +} + +[[ $OSTYPE == (darwin|dragonfly|freebsd|openbsd)* && $service == sum ]] && { + _default + return +} + +[[ $OSTYPE == (darwin|dragonfly|freebsd)* && $service == cksum ]] && { + _arguments -s -S -A '-*' \ + '-o[use specified historic algorithm]:historic algorithm:(1 2 3)' \ + '*: :_files' + return +} + +case $OSTYPE in + darwin*|dragonfly*|freebsd*|netbsd*) + args+=( + '(-n -p -r)-q[output checksums only]' + ) + ;| # MATCH AGAIN + darwin*|dragonfly*|freebsd*|openbsd*) + args+=( + '(-p -q)-r[reverse output format]' + ) + ;| # MATCH AGAIN + netbsd*|openbsd*) + args+=( + '-c[verify checksums from input files]' + ) + ;| # MATCH AGAIN + dragonfly*) + args+=( + '-b+[begin processing files at specified offset]:begin offset (bytes)' + '-e+[end processing files at specified offset]:end offset (bytes)' + ) + ;; + freebsd*) + args+=( + '-c+[verify input against specified digest string]:digest string' + ) + ;; + netbsd*) + args+=( + '(-p -q)-n[reverse output format]' + '(-p)-w[warn on malformed checksum files]' + ) + [[ $service == (|ck)sum ]] && args+=( + '(-o)-a+[use specified algorithm]:algorithm:(crc md2 md4 md5 old1 old2 rmd160 sha1 sha256 sha384 sha512)' + '(-a)-o+[use specified historic algorithm]:historic algorithm:(1 2)' + ) + ;; + openbsd*) + args+=( + '-b[output in base64]' + '(-h)-C+[verify input files against checksums in specifed file]:checksum file:_files' + '(-C)-h+[output checksums to specified file]:checksum file:_files' + '(-n -r)-q[output checksum only, or suppress check success messages]' + ) + [[ $service == cksum ]] && args+=( + '*-a+[use specified algorithm(s)]:algorithm:_values -s , algorithm cksum md5 rmd160 sha1 sha224 sha256 sha384 sha512/256 sha512' + ) + ;; +esac + +args+=( + '-p[output stdin along with checksum]' + '*-s+[checksum specified string]:string' + '*-t[run built-in time trial(s)]' + '-x[run built-in tests]' + '*: :_files' +) + +_arguments -s -S -A '-*' : $args diff --git a/Completion/Unix/Command/_lz4 b/Completion/Unix/Command/_lz4 new file mode 100644 index 000000000..d69091d00 --- /dev/null +++ b/Completion/Unix/Command/_lz4 @@ -0,0 +1,103 @@ +#compdef lz4 lz4c lz4c32 lz4cat unlz4 + +# Notes: +# - All lz4 CLI tools take the same options — you can do `unlz4 --compress` if +# you want — and we complete accordingly. One can make a reasonable argument +# that we shouldn't, but...? +# - The only exceptions to the above are the legacy compression options (-c0, +# -hc, and so on) — only lz4c accepts these. Each of these options is +# interpreted separately otherwise (e.g., -c0 becomes equivalent to -c -0) +# - All these tools use a non-standard option-handling method that we don't +# fully support. For example, the tool will let you do things like `-b1e3i3` +# instead of `-b1 -e3 -i3` — we won't + +local ret=1 +local -a context line state state_descr expl args levels=( -{1..16} ) +local -A opt_args val_args + +args=( + + excl # Fully exclusive options + '(: -)'{-h,--help}'[display help information]' + '(: -)-H[display long help information]' + '(: -)'{-V,--version}'[display version information]' + + misc # Misc. arguments + '(-q -v --quiet --verbose)*'{-q,--quiet}'[reduce output verbosity]' + '(-q -v --quiet --verbose)*'{-v,--verbose}'[increase output verbosity]' + '*::: :->files' + + B # Benchmark/compress-mode options (not allowed with legacy format) + '(d t -l)*-B-[specify block property]: :->block-props' + '(d t -l --no-content-size)--content-size[record original uncompressed size]' + '(d t -l --no-frame-crc)--frame-crc[enable content checksum]' + '(d t -l --content-size)--no-content-size[do not record original uncompressed size]' + '(d t -l --frame-crc)--no-frame-crc[disable content checksum]' + '(d t -l --sparse)--no-sparse[disable sparse-file support]' + '(d t -l --no-sparse)--sparse[enable sparse-file support]' + + C # Compress/decompress-mode options + '(b t -c --stdout --to-stdout)'{-c,--stdout}'[write on standard output]' + '(b t -y)'{-f,--force}'[overwrite target without prompting, or cat on standard output]' + '(b t -k --keep --rm)'{-k,--keep}'[keep source file]' + '(b t -m -r --multiple)'{-m,--multiple}'[take multiple input files]' + '!(b -t -f -y --force)--no-force' + '(b t -m --multiple)-r[operate recursively on directories]' + '(b t -k --keep)--rm[remove source file]' + '!(b t -c --stdout)--to-stdout' + + b # Benchmark-mode options + "(C c d t)-b-[benchmark file using specified compression level]::compression level:(${(j< >)levels//-/})" + "(C c d t)-e-[specify upper compression level limit (with -b)]:compression level:(${(j< >)levels//-/})" + '(C c d t)-i-[specifiy minimum evaluation time (with -b)]:evaluation time (seconds