From 0ac59f6294003c1f6c0342a132e81259b1cd6218 Mon Sep 17 00:00:00 2001 From: cptpcrd <31829097+cptpcrd@users.noreply.github.com> Date: Thu, 12 Nov 2020 21:14:21 -0500 Subject: [PATCH] Add -a/--all option to `brl which` Prints the list of strata in which the given binary can be found. --- src/slash-bedrock/libexec/brl-which | 132 ++++++++++++++---- src/slash-bedrock/share/bash/completion/brl | 1 + .../share/fish/completion/brl.fish | 2 + src/slash-bedrock/share/zsh/completion/_brl | 1 + 4 files changed, 111 insertions(+), 25 deletions(-) diff --git a/src/slash-bedrock/libexec/brl-which b/src/slash-bedrock/libexec/brl-which index 5e2cf2b3..1ad4d994 100755 --- a/src/slash-bedrock/libexec/brl-which +++ b/src/slash-bedrock/libexec/brl-which @@ -24,6 +24,7 @@ Options: ${color_cmd}-f${color_norm}, ${color_cmd}--file ${color_norm}which ${color_term}stratum${color_norm} provides a given file path ${color_cmd}-p${color_norm}, ${color_cmd}--pid ${color_norm}which ${color_term}stratum${color_norm} provides a given process ID ${color_cmd}-x${color_norm}, ${color_cmd}--xwindow ${color_norm}which ${color_term}stratum${color_norm} provides a given X11 window (requires xprop) + ${color_cmd}-a${color_norm}, ${color_cmd}--all ${color_norm}search in all ${color_term}strata${color_norm} (files and binaries only) ${color_cmd}-h${color_norm}, ${color_cmd}--help ${color_norm}print this message Guessing reasoning for ${color_sub}${color_norm} based on identifier: @@ -67,6 +68,19 @@ which_bin() { done } +which_bin_all() ( + min_args "${#}" "1" + + while [ -n "${1:-}" ]; do + for stratum in $(list_strata); do + if path="$(strat -r "${stratum}" /bedrock/libexec/busybox which "$1" 2>/dev/null)"; then + which_file "/bedrock/strata/${stratum}/${path}" + fi + done + shift + done +) + which_file() { while [ -n "${1:-}" ]; do if ! [ -e "${1}" ]; then @@ -90,6 +104,19 @@ which_file() { done } +which_file_all() ( + min_args "${#}" "1" + + while [ -n "${1:-}" ]; do + for stratum in $(list_strata); do + if [ -e "/bedrock/strata/${stratum}/$1" ]; then + which_file "/bedrock/strata/${stratum}/$1" + fi + done + shift + done +) + which_pid() { while [ -n "${1:-}" ]; do # Pick a mount point visible to the process. @@ -135,30 +162,12 @@ which_xwindow() { fi } -handle_help "${@:-}" +which_auto() { + if [ "${#}" -eq 0 ]; then + which_current + return + fi -case "${1:-}" in -"-c" | "--current" | "") - [ -n "${1:-}" ] && shift - which_current "${@}" - ;; -"-b" | "--bin") - shift - which_bin "${@}" - ;; -"-f" | "--file") - shift - which_file "${@}" - ;; -"-p" | "--pid") - shift - which_pid "${@}" - ;; -"-x" | "--xwindow") - shift - which_xwindow - ;; -*) while [ -n "${1:-}" ]; do if echo "${1}" | grep -q '^[0-9][0-9]*$'; then which_pid "${1}" @@ -169,7 +178,80 @@ case "${1:-}" in fi shift done - ;; -esac +} + +which_auto_all() { + min_args "${#}" "1" + + while [ -n "${1:-}" ]; do + if echo "${1}" | grep -q '/'; then + which_file_all "${1}" + else + which_bin_all "${1}" + fi + shift + done +} + +handle_help "${@:-}" + +OPTL="all,bin,file,current,pid,xwindow" +OPTO="abcfpx" +eval set -- "$(getopt -q -l "${OPTL}" -- "${OPTO}" "${@}")" || true + +list_all=false +list_type=auto + +while [ -n "${1:-}" ]; do + case "${1:-}" in + "-c" | "--current") + shift + list_type=current + ;; + "-b" | "--bin") + shift + list_type=bin + ;; + "-a" | "--all") + shift + list_all=true + ;; + "-f" | "--file") + shift + list_type=file + ;; + "-p" | "--pid") + shift + list_type=pid + ;; + "-x" | "--xwindow") + shift + list_type=xwindow + ;; + --) + shift + break + ;; + -*) + abort "Unrecognized argument: ${1}" + ;; + *) + break + ;; + esac +done + +if [ "${list_all}" = true ]; then + case "${list_type}" in + auto|file|bin) + list_type="${list_type}_all" + ;; + *) + abort "-a/--all can only be used with -f/--file or -b/--bin" + ;; + esac +fi + +which_${list_type} "$@" exit_success diff --git a/src/slash-bedrock/share/bash/completion/brl b/src/slash-bedrock/share/bash/completion/brl index 35500e3f..458877ad 100755 --- a/src/slash-bedrock/share/bash/completion/brl +++ b/src/slash-bedrock/share/bash/completion/brl @@ -87,6 +87,7 @@ _brl() { opts="${opts} $(_brl_unused_all "-f --file" "${COMP_WORDS[@]}")" opts="${opts} $(_brl_unused_all "-p --pid" "${COMP_WORDS[@]}")" opts="${opts} $(_brl_unused_all "-x --xwindow" "${COMP_WORDS[@]}")" + opts="${opts} $(_brl_unused_all "-a --all" "${COMP_WORDS[@]}")" fi case "${COMP_WORDS[2]}" in "-c" | "--current") ;; diff --git a/src/slash-bedrock/share/fish/completion/brl.fish b/src/slash-bedrock/share/fish/completion/brl.fish index f768ab89..87513a8c 100644 --- a/src/slash-bedrock/share/fish/completion/brl.fish +++ b/src/slash-bedrock/share/fish/completion/brl.fish @@ -83,6 +83,8 @@ complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -o 'p' -d 'which complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -l 'pid' -d 'which stratum provides a given process ID' complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -o 'x' -d 'which stratum provides a given process ID' complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -l 'xwindow' -d 'which stratum provides a given X11 window' +complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -o 'a' -d 'search in all strata (files and binaries only)' +complete -f -c brl -n '_brl_arg_is 2 which; and _brl_argnum 2' -l 'all' -d 'search in all strata (files and binaries only)' complete -f -c brl -n '_brl_arg_is 2 which; and _brl_arg_is 3 -b' -a '(_brl_which_bin)' -d 'binary' complete -f -c brl -n '_brl_arg_is 2 which; and _brl_arg_is 3 --bin' -a '(_brl_which_bin)' -d 'binary' complete -f -c brl -n '_brl_arg_is 2 which; and _brl_arg_is 3 -f' -a '(__fish_complete_path)' -d 'file' diff --git a/src/slash-bedrock/share/zsh/completion/_brl b/src/slash-bedrock/share/zsh/completion/_brl index 3f564e05..fbf59420 100755 --- a/src/slash-bedrock/share/zsh/completion/_brl +++ b/src/slash-bedrock/share/zsh/completion/_brl @@ -84,6 +84,7 @@ case "${words[2]}" in args+=('(-f --file)'{-f,--file}'[which stratum provides a given file path]') args+=('(-p --pid)'{-p,--pid}'[which stratum provides a given process ID]') args+=('(-x --xwindow)'{-x,--xwindow}'[which stratum provides a given X11 window]') + args+=('(-a --all)'{-a,--all}'[search in all strata (files and binaries only)]') fi case "${words[3]}" in "-c"|"--current")