From b1bdb6496c07fc4fcf3f0feae69b5ef89ae557d9 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 9 Sep 2016 16:20:05 +0900 Subject: bash-completion: systemctl: do not pass masked or not-found units to filter Also, add new function __filter_units_by_properties() for filtering units by multiple properties, and make __get_startable_units() use it. fixes #4114 --- shell-completion/bash/systemctl.in | 43 ++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'shell-completion') diff --git a/shell-completion/bash/systemctl.in b/shell-completion/bash/systemctl.in index 2a45dcbba0..e62cfa57a9 100644 --- a/shell-completion/bash/systemctl.in +++ b/shell-completion/bash/systemctl.in @@ -51,6 +51,37 @@ __filter_units_by_property () { done } +__filter_units_by_properties () { + local mode=$1 properties=$2 values=$3 ; shift 3 + local units=("$@") + local props + IFS=$'\n' read -rd '' -a props < \ + <(__systemctl $mode show --property "$properties" -- "${units[@]}") + IFS=$',' read -r -a properties < <(echo $properties) + IFS=$',' read -r -a values < <(echo $values) + local conditions=() + for ((i=0; i < ${#properties[*]}; i++)); do + for ((j=0; j < ${#properties[*]}; j++)); do + if [[ ${props[i]%%=*} == ${properties[j]} ]]; then + conditions+=( "${properties[j]}=${values[j]}" ) + fi + done + done + local flag + for ((i=0; i < ${#units[*]}; i++)); do + flag=1 + for ((j=0; j < ${#conditions[*]}; j++)); do + if [[ "${props[ i * ${#conditions[*]} + j]}" != "${conditions[j]}" ]]; then + flag= + break + fi + done + if [[ -n $flag ]]; then + echo " ${units[i]}" + fi + done +} + __get_all_units () { { __systemctl $1 list-unit-files; __systemctl $1 list-units --all; } \ | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; } __get_template_names () { __systemctl $1 list-unit-files \ @@ -60,12 +91,12 @@ __get_active_units () { __systemctl $1 list-units \ | { while read -r a b; do echo " $a"; done; }; } __get_startable_units () { # find startable inactive units - __filter_units_by_property $mode ActiveState inactive $( - __filter_units_by_property $mode CanStart yes $( - __systemctl $mode list-unit-files --state enabled,disabled,static | \ - { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; } - __systemctl $mode list-units --state inactive,failed | \ - { while read -r a b; do echo " $a"; done; } )) + __filter_units_by_properties $mode ActiveState,CanStart inactive,yes $( + { __systemctl $mode list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient | \ + { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; } + __systemctl $mode list-units --state inactive,failed | \ + { while read -r a b c; do [[ $b == "loaded" ]] && echo " $a"; done; } + } | sort -u ) } __get_restartable_units () { # filter out masked and not-found -- cgit v1.2.3-54-g00ecf From c07c80bc969fdb745593a86c8a34758722eeb29a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 14 Sep 2016 15:22:45 +0900 Subject: bash-completion: systemctl: use local variables --- shell-completion/bash/systemctl.in | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'shell-completion') diff --git a/shell-completion/bash/systemctl.in b/shell-completion/bash/systemctl.in index e62cfa57a9..dcf71a1f51 100644 --- a/shell-completion/bash/systemctl.in +++ b/shell-completion/bash/systemctl.in @@ -41,7 +41,7 @@ __contains_word () { __filter_units_by_property () { local mode=$1 property=$2 value=$3 ; shift 3 local units=("$@") - local props + local props i IFS=$'\n' read -rd '' -a props < \ <(__systemctl $mode show --property "$property" -- "${units[@]}") for ((i=0; $i < ${#units[*]}; i++)); do @@ -54,12 +54,11 @@ __filter_units_by_property () { __filter_units_by_properties () { local mode=$1 properties=$2 values=$3 ; shift 3 local units=("$@") - local props + local props i j conditions=() IFS=$'\n' read -rd '' -a props < \ <(__systemctl $mode show --property "$properties" -- "${units[@]}") IFS=$',' read -r -a properties < <(echo $properties) IFS=$',' read -r -a values < <(echo $values) - local conditions=() for ((i=0; i < ${#properties[*]}; i++)); do for ((j=0; j < ${#properties[*]}; j++)); do if [[ ${props[i]%%=*} == ${properties[j]} ]]; then @@ -67,16 +66,13 @@ __filter_units_by_properties () { fi done done - local flag for ((i=0; i < ${#units[*]}; i++)); do - flag=1 for ((j=0; j < ${#conditions[*]}; j++)); do if [[ "${props[ i * ${#conditions[*]} + j]}" != "${conditions[j]}" ]]; then - flag= break fi done - if [[ -n $flag ]]; then + if (( j == ${#conditions[*]} )); then echo " ${units[i]}" fi done -- cgit v1.2.3-54-g00ecf