updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / svc / _svc
blob09d7922efae57191e9ffae25201df113782cd746
1 #compdef svc
3 services() {
4         local -a inits xinetds alls
5         local expl ret=1
7         if chkconfig --list > /dev/null 2>&1; then
8                 alls=( ${(f)"$(LANGUAGE=C LANG=C LC_ALL=C chkconfig --list)"} )
9                 inits=( ${${${alls[1,(r)xinetd based*]}[1,-2]}/%[[:space:]]*/} )
10                 xinetds=( ${${${${alls[(r)xinetd based*,-1]}[2,-1]}/#[[:space:]]#}/%:*} )
12          _alternative \
13                 'init:init service:compadd -a inits' \
14             'xinetd:xinetd service:compadd -a xinetds' && ret=0
15         else
16                 services_directory=`[ -d /etc/init.d ] && echo /etc/init.d || echo /etc/rc.d`;
17                 _wanted services expl service compadd "$@" - $services_directory/*(-*:t) && ret=0
18         fi
20         return ret
23 init_d() {
24         local magic cmds what script
26         _compskip=all
28         script=$words[1]
29         [[ $script = */* ]] || script=`[ -d /etc/init.d ] && echo /etc/init.d || echo /etc/rc.d`/$script
31         what='(st(art|op|atus)|(force-|)re(start|load)|debug_(up|down)|dump(|_stats)|add|delete|clean|list|config(|ure))'
33         read -u0 -k2 magic < $script && [[ $magic = '#!' ]] &&
34                 cmds=( ${${(j:|:s:|:)${(M)${(f)"$(< $script)"}:#[[:blank:]]#(\'|)${~what}([[:blank:]]#\|[[:blank:]]#${~what})#(\'|)\)}}//[^-a-z_]} )
36         (( $#cmds )) || zstyle -a ":completion:${curcontext}:commands" commands cmds ||
37                 cmds=(start stop)
39         _sub_commands $cmds
42 _svc() {
43         case $CURRENT in
44                 2) # Show services under /etc/rc.d
45                         services
46                         ;;
47                 3) # Show subcommands for current service
49                         # This is here so that _init_d works on the service name rather
50                         # than on the actual function name "svc"
51                         (( CURRENT-- )) 
52                         shift words
54                         # Append enable and disable as service subcommands
55                         compadd enable disable
57                         # Append available subcommands for current service
58                         init_d 
59                         ;;
60         esac
64 _svc "$@"