Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 33932 - Bash completion for "rc" and "rc-status"
Summary: Bash completion for "rc" and "rc-status"
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All All
: High minor (vote)
Assignee: Christian Birchinger (RETIRED)
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-11-20 06:51 UTC by TGL
Modified: 2003-12-13 09:47 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description TGL 2003-11-20 06:51:28 UTC
Hi,

Here is a small bash completion function that lists existing runlevels.
It's almost nothing, but I think it eases usage of /sbin/rc and 
/bin/rc-status, hence I suggest its inclusion in
/etc/bash_completion.d/gentoo:

#
# rc & rc-status completion command
#
_runlevels()
{
	local cur runlvdir origdir
	origdir=${PWD}
	runlvdir=/etc/runlevels
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	if [ ${#COMP_WORDS[*]} -le 2 ]; then
		cd ${runlvdir} 
		COMPREPLY=( $( compgen -G "${cur}*" ) )
		cd ${origdir}
	fi
	return 0
}
complete -F _runlevels rc rc-status
Comment 1 Christian Birchinger (RETIRED) gentoo-dev 2003-11-27 03:32:38 UTC
rc-status doesn't seem to care about the runlevel argument:

joker@milka ~ $ rc-status nonetwork
Runlevel: default
  atd                                      [ started ]
  dcron                                    [ started ]

It always outputs "default".

The completion for "rc" is fine. But rc-status also supports arguments. And completion should also complete those arguments like with other commands.
Comment 2 TGL 2003-11-27 03:59:26 UTC
Strange, it works for me: rc-status displays the status of services
for the right runlevel, the one I ask on command line. I'm using 
baselayout-1.8.6.12, maybe it is your version that has a bug?

That said, I've just realized that it also support a few options 
other than the runlevel name, so I will write a different 
completion function for it.
Comment 3 Christian Birchinger (RETIRED) gentoo-dev 2003-11-27 04:15:33 UTC
Would this be ok? It completes the rc-status options too.

#
# rc & rc-status completion command
#
 
_rc()
{
        local cur
        COMPREPLY=()
        cur=${COMP_WORDS[COMP_CWORD]}
        if [ ${#COMP_WORDS[*]} -le 2 ]; then
                COMPREPLY=( $( compgen -W "$(for i in /etc/runlevels/*; do echo ${i##*/}; done)" | grep ^$cur ) )

        fi
        return 0
}
complete -F _rc rc

_rc-status()
{
        local cur opts
        COMPREPLY=()
        cur=${COMP_WORDS[COMP_CWORD]}
        if [ ${#COMP_WORDS[*]} -le 2 ]; then
                opts="-a -all -l --list -u --unused"
                COMPREPLY=( $( compgen -W "${opts} $(for i in /etc/runlevels/*; do echo ${i##*/}; done) " | grep ^$cur ) )

        fi
        return 0
}
 
complete -F _rc-status rc-status

Comment 4 Christian Birchinger (RETIRED) gentoo-dev 2003-11-27 04:32:22 UTC
" -- $cur" ofcourse ... not "| grep ^$cur"
Comment 5 TGL 2003-11-27 05:38:08 UTC
Yes it would be okay. I had not seen your reply at first so I had also
written my version, which does just about the same. The only difference
is that I only display the options list when the arg begin with a "-",
but show only runlevels otherwise.:

#
# /sbin/rc completion command
#
_runlevels()
{
	local cur numwords runlvdir origdir
	origdir=${PWD}
	runlvdir=/etc/runlevels
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	if [ ${#COMP_WORDS[*]} -le 2 ]; then
		cd ${runlvdir} 
		COMPREPLY=( $( compgen -G "${cur}*" ) )
		cd ${origdir}
	fi
	return 0
}
complete -F _runlevels rc

#
# /bin/rc-status completion command
#
_rcstatus()
{
	local cur numwords runlvdir origdir options
	origdir=${PWD}
	runlvdir=/etc/runlevels
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	if [ ${#COMP_WORDS[*]} -le 2 ]; then
		if [[ "$cur" == -* ]]; then
			options="--all --list --unused"
			COMPREPLY=( $( compgen -W "$options" -- $cur ) )
		else
			cd ${runlvdir} 
			COMPREPLY=( $( compgen -G "${cur}*" ) )
			cd ${origdir}
		fi
	fi
	return 0
}
complete -F _rcstatus rc-status


I let you decide what you prefer, I don't really care :)
Comment 6 Christian Birchinger (RETIRED) gentoo-dev 2003-12-13 09:47:10 UTC
Added bash-completion-20031125-r1 to cvs with KEYWORDS ~arch.