diff mbox series

setsebool: improve bash-completion script

Message ID 20230706142126.59907-1-cgzones@googlemail.com (mailing list archive)
State Accepted
Commit 4c6a339eee37
Delegated to: Petr Lautrbach
Headers show
Series setsebool: improve bash-completion script | expand

Commit Message

Christian Göttsche July 6, 2023, 2:21 p.m. UTC
Support the usage format of multiple booleans to change, e.g.:

    setsebool bool1=on bool2=off

Support the options -N and -V.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 .../setsebool/setsebool-bash-completion.sh    | 54 ++++++++++++++-----
 1 file changed, 42 insertions(+), 12 deletions(-)

Comments

James Carter Aug. 2, 2023, 7:20 p.m. UTC | #1
On Thu, Jul 6, 2023 at 10:42 AM Christian Göttsche
<cgzones@googlemail.com> wrote:
>
> Support the usage format of multiple booleans to change, e.g.:
>
>     setsebool bool1=on bool2=off
>
> Support the options -N and -V.
>
> Signed-off-by: Christian Göttsche <cgzones@googlemail.com>

Acked-by: James Carter <jwcart2@gmail.com>

> ---
>  .../setsebool/setsebool-bash-completion.sh    | 54 ++++++++++++++-----
>  1 file changed, 42 insertions(+), 12 deletions(-)
>
> diff --git a/policycoreutils/setsebool/setsebool-bash-completion.sh b/policycoreutils/setsebool/setsebool-bash-completion.sh
> index d0f3b424..f7fe0c0c 100644
> --- a/policycoreutils/setsebool/setsebool-bash-completion.sh
> +++ b/policycoreutils/setsebool/setsebool-bash-completion.sh
> @@ -15,26 +15,56 @@
>  # You should have received a copy of the GNU General Public License
>  # along with systemd; If not, see <http://www.gnu.org/licenses/>.
>
> -__contains_word () {
> -        local word=$1; shift
> -        for w in $*; do [[ $w = $word ]] && return 0; done
> -        return 1
> -}
> -
>  __get_all_booleans () {
>      getsebool -a | cut -f1 -d' '
>  }
>
>  _setsebool () {
> -        local command=${COMP_WORDS[1]}
>          local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
> -        local verb comps
>
> -       if   [ "$verb" = "" -a "$prev" = "setsebool" -o "$prev" = "-P" ]; then
> -               COMPREPLY=( $(compgen -W "-P $( __get_all_booleans ) " -- "$cur") )
> -               return 0
> +        if [ "$prev" = '=' ]; then
> +                COMPREPLY=( $(compgen -W "on off" -- "$cur") )
> +                return 0
>          fi
> -        COMPREPLY=( $(compgen -W "0 1 -P" -- "$cur") )
> +
> +        case "$cur" in
> +        '0')
> +                COMPREPLY=( $(compgen -W "0 1" -- "$cur") )
> +                return 0
> +        ;;
> +        '1')
> +                COMPREPLY=( $(compgen -W "0 1" -- "$cur") )
> +                return 0
> +        ;;
> +        =)
> +                COMPREPLY=( $(compgen -W "on off" -- "") )
> +                return 0
> +        ;;
> +        -*)
> +                COMPREPLY=( $(compgen -W "-N -P -V" -- "$cur") )
> +                return 0
> +        ;;
> +        '')
> +                if [ "$prev" = '0' ] || [ "$prev" = '1' ]; then
> +                        COMPREPLY=( $(compgen -W "-N -P -V" -- "$cur") )
> +                        return 0
> +                fi
> +                if getsebool "$prev" > /dev/null 2>&1; then
> +                        COMPREPLY=( $(compgen -W "0 1" -- "$cur") )
> +                        return 0
> +                fi
> +        ;;
> +        *)
> +                if getsebool "$cur" > /dev/null 2>&1; then
> +                        COMPREPLY=( $(compgen -W '$( __get_all_booleans ) "$cur=on " "$cur=off "' -- "$cur") )
> +                        compopt -o nospace
> +                        return 0
> +                fi
> +        ;;
> +        esac
> +
> +        COMPREPLY=( $(compgen -W '"-N " "-P " "-V " $( __get_all_booleans ) ' -- "$cur") )
> +        compopt -o nospace
>          return 0
>  }
>
> --
> 2.40.1
>
James Carter Aug. 4, 2023, 6:35 p.m. UTC | #2
On Wed, Aug 2, 2023 at 3:20 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Thu, Jul 6, 2023 at 10:42 AM Christian Göttsche
> <cgzones@googlemail.com> wrote:
> >
> > Support the usage format of multiple booleans to change, e.g.:
> >
> >     setsebool bool1=on bool2=off
> >
> > Support the options -N and -V.
> >
> > Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
>
> Acked-by: James Carter <jwcart2@gmail.com>
>

Merged.
Thanks,
Jim

> > ---
> >  .../setsebool/setsebool-bash-completion.sh    | 54 ++++++++++++++-----
> >  1 file changed, 42 insertions(+), 12 deletions(-)
> >
> > diff --git a/policycoreutils/setsebool/setsebool-bash-completion.sh b/policycoreutils/setsebool/setsebool-bash-completion.sh
> > index d0f3b424..f7fe0c0c 100644
> > --- a/policycoreutils/setsebool/setsebool-bash-completion.sh
> > +++ b/policycoreutils/setsebool/setsebool-bash-completion.sh
> > @@ -15,26 +15,56 @@
> >  # You should have received a copy of the GNU General Public License
> >  # along with systemd; If not, see <http://www.gnu.org/licenses/>.
> >
> > -__contains_word () {
> > -        local word=$1; shift
> > -        for w in $*; do [[ $w = $word ]] && return 0; done
> > -        return 1
> > -}
> > -
> >  __get_all_booleans () {
> >      getsebool -a | cut -f1 -d' '
> >  }
> >
> >  _setsebool () {
> > -        local command=${COMP_WORDS[1]}
> >          local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
> > -        local verb comps
> >
> > -       if   [ "$verb" = "" -a "$prev" = "setsebool" -o "$prev" = "-P" ]; then
> > -               COMPREPLY=( $(compgen -W "-P $( __get_all_booleans ) " -- "$cur") )
> > -               return 0
> > +        if [ "$prev" = '=' ]; then
> > +                COMPREPLY=( $(compgen -W "on off" -- "$cur") )
> > +                return 0
> >          fi
> > -        COMPREPLY=( $(compgen -W "0 1 -P" -- "$cur") )
> > +
> > +        case "$cur" in
> > +        '0')
> > +                COMPREPLY=( $(compgen -W "0 1" -- "$cur") )
> > +                return 0
> > +        ;;
> > +        '1')
> > +                COMPREPLY=( $(compgen -W "0 1" -- "$cur") )
> > +                return 0
> > +        ;;
> > +        =)
> > +                COMPREPLY=( $(compgen -W "on off" -- "") )
> > +                return 0
> > +        ;;
> > +        -*)
> > +                COMPREPLY=( $(compgen -W "-N -P -V" -- "$cur") )
> > +                return 0
> > +        ;;
> > +        '')
> > +                if [ "$prev" = '0' ] || [ "$prev" = '1' ]; then
> > +                        COMPREPLY=( $(compgen -W "-N -P -V" -- "$cur") )
> > +                        return 0
> > +                fi
> > +                if getsebool "$prev" > /dev/null 2>&1; then
> > +                        COMPREPLY=( $(compgen -W "0 1" -- "$cur") )
> > +                        return 0
> > +                fi
> > +        ;;
> > +        *)
> > +                if getsebool "$cur" > /dev/null 2>&1; then
> > +                        COMPREPLY=( $(compgen -W '$( __get_all_booleans ) "$cur=on " "$cur=off "' -- "$cur") )
> > +                        compopt -o nospace
> > +                        return 0
> > +                fi
> > +        ;;
> > +        esac
> > +
> > +        COMPREPLY=( $(compgen -W '"-N " "-P " "-V " $( __get_all_booleans ) ' -- "$cur") )
> > +        compopt -o nospace
> >          return 0
> >  }
> >
> > --
> > 2.40.1
> >
diff mbox series

Patch

diff --git a/policycoreutils/setsebool/setsebool-bash-completion.sh b/policycoreutils/setsebool/setsebool-bash-completion.sh
index d0f3b424..f7fe0c0c 100644
--- a/policycoreutils/setsebool/setsebool-bash-completion.sh
+++ b/policycoreutils/setsebool/setsebool-bash-completion.sh
@@ -15,26 +15,56 @@ 
 # You should have received a copy of the GNU General Public License
 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
 
-__contains_word () {
-        local word=$1; shift
-        for w in $*; do [[ $w = $word ]] && return 0; done
-        return 1
-}
-
 __get_all_booleans () {
     getsebool -a | cut -f1 -d' '
 }
 
 _setsebool () {
-        local command=${COMP_WORDS[1]}
         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
-        local verb comps
 
-	if   [ "$verb" = "" -a "$prev" = "setsebool" -o "$prev" = "-P" ]; then
-	        COMPREPLY=( $(compgen -W "-P $( __get_all_booleans ) " -- "$cur") )
-		return 0
+        if [ "$prev" = '=' ]; then
+                COMPREPLY=( $(compgen -W "on off" -- "$cur") )
+                return 0
         fi
-        COMPREPLY=( $(compgen -W "0 1 -P" -- "$cur") )
+
+        case "$cur" in
+        '0')
+                COMPREPLY=( $(compgen -W "0 1" -- "$cur") )
+                return 0
+        ;;
+        '1')
+                COMPREPLY=( $(compgen -W "0 1" -- "$cur") )
+                return 0
+        ;;
+        =)
+                COMPREPLY=( $(compgen -W "on off" -- "") )
+                return 0
+        ;;
+        -*)
+                COMPREPLY=( $(compgen -W "-N -P -V" -- "$cur") )
+                return 0
+        ;;
+        '')
+                if [ "$prev" = '0' ] || [ "$prev" = '1' ]; then
+                        COMPREPLY=( $(compgen -W "-N -P -V" -- "$cur") )
+                        return 0
+                fi
+                if getsebool "$prev" > /dev/null 2>&1; then
+                        COMPREPLY=( $(compgen -W "0 1" -- "$cur") )
+                        return 0
+                fi
+        ;;
+        *)
+                if getsebool "$cur" > /dev/null 2>&1; then
+                        COMPREPLY=( $(compgen -W '$( __get_all_booleans ) "$cur=on " "$cur=off "' -- "$cur") )
+                        compopt -o nospace
+                        return 0
+                fi
+        ;;
+        esac
+
+        COMPREPLY=( $(compgen -W '"-N " "-P " "-V " $( __get_all_booleans ) ' -- "$cur") )
+        compopt -o nospace
         return 0
 }