[arch-general] rc.d bash completion

Auguste Pop auguste at gmail.com
Wed May 18 12:13:29 EDT 2011


i wrote a very simple rc.d bash completion script. is there any chance
it can be used upstream?

the script is uploaded to http://paste.pocoo.org/show/391283/

it is also copy-pasted here for your convenience:

#!/bin/bash

_rc.d_getdaemons ()
{
    /usr/bin/find "$1" -mindepth 1 -maxdepth 1 -type f -printf '%f\n'
}

_rc.d_arraydiff ()
{
    local arr
    declare -A arr

    for elem in "${!1}"
    do
        arr[$elem]=1
    done
    for elem in "${!2}"
    do
        unset arr[$elem]
    done
    echo "${!arr[@]}"
}

_rc.d ()
{
    local command commands available started
    local cur words cword

    commands="list help start stop restart"
    available=($(_rc.d_getdaemons /etc/rc.d))
    started=($(_rc.d_getdaemons /run/daemons))

    _get_comp_words_by_ref -n =: cur words cword
    [ $cword -gt 1 ] && command=${words[1]}

    if [ -z "$command" ]
    then
        COMPREPLY=($(compgen -W "$commands" -- $cur))
    else
        case "$command" in
            start)
                COMPREPLY=($(compgen -W "$(_rc.d_arraydiff \
                    available[@] started[@])" -- $cur))
                ;;
            stop|restart)
                COMPREPLY=($(compgen -W "$(echo ${started[@]})" -- $cur))
                ;;
            list|help|*)
                COMPREPLY=()
                ;;
        esac
    fi
}

complete -F _rc.d rc.d


More information about the arch-general mailing list