| 1 |
_cond() |
| 2 |
{ |
| 3 |
initctl -pt cond dump | awk '{print $4}' | sed 's/<\(.*\)>/\1/' |
| 4 |
} |
| 5 |
|
| 6 |
_ident() |
| 7 |
{ |
| 8 |
if [ -n "$1" ]; then |
| 9 |
initctl ident | grep -q $1 |
| 10 |
else |
| 11 |
initctl ident |
| 12 |
fi |
| 13 |
} |
| 14 |
|
| 15 |
_svc() |
| 16 |
{ |
| 17 |
initctl ls -pt | grep $1 | sed "s/.*\/\(.*\)/\1/g" | sort -u |
| 18 |
} |
| 19 |
|
| 20 |
_enabled() |
| 21 |
{ |
| 22 |
echo "$(_svc enabled)" |
| 23 |
} |
| 24 |
|
| 25 |
_available() |
| 26 |
{ |
| 27 |
all=$(mktemp) |
| 28 |
ena=$(mktemp) |
| 29 |
echo "$(_svc available)" >$all |
| 30 |
echo "$(_svc enabled)" >$ena |
| 31 |
grep -v -f $ena $all |
| 32 |
rm $all $ena |
| 33 |
} |
| 34 |
|
| 35 |
# Determine first non-option word. Usually the command |
| 36 |
_firstword() { |
| 37 |
local firstword i |
| 38 |
|
| 39 |
firstword= |
| 40 |
for ((i = 1; i < ${#COMP_WORDS[@]}; ++i)); do |
| 41 |
if [[ ${COMP_WORDS[i]} != -* ]]; then |
| 42 |
firstword=${COMP_WORDS[i]} |
| 43 |
break |
| 44 |
fi |
| 45 |
done |
| 46 |
|
| 47 |
echo $firstword |
| 48 |
} |
| 49 |
|
| 50 |
# Determine last non-option word. Uusally a sub-command |
| 51 |
_lastword() { |
| 52 |
local lastword i |
| 53 |
|
| 54 |
lastword= |
| 55 |
for ((i = 1; i < ${#COMP_WORDS[@]}; ++i)); do |
| 56 |
if [[ ${COMP_WORDS[i]} != -* ]] && [[ -n ${COMP_WORDS[i]} ]] && [[ ${COMP_WORDS[i]} != $cur ]]; then |
| 57 |
lastword=${COMP_WORDS[i]} |
| 58 |
fi |
| 59 |
done |
| 60 |
|
| 61 |
echo $lastword |
| 62 |
} |
| 63 |
|
| 64 |
_initctl() |
| 65 |
{ |
| 66 |
local cur command |
| 67 |
|
| 68 |
cur=${COMP_WORDS[COMP_CWORD]} |
| 69 |
prev="${COMP_WORDS[COMP_CWORD-1]}" |
| 70 |
firstword=$(_firstword) |
| 71 |
lastword=$(_lastword) |
| 72 |
|
| 73 |
commands="status cond debug help kill ls log version list enable \ |
| 74 |
disable touch show cat edit create delete reload start \ |
| 75 |
stop restart signal cgroup ps top plugins runlevel reboot \ |
| 76 |
halt poweroff suspend utmp" |
| 77 |
cond_cmds="set get clear status dump" |
| 78 |
cond_types="hook net pid service task usr" |
| 79 |
signals="int term hup stop tstp cont usr1 usr2 pwr" |
| 80 |
options="-b --batch \ |
| 81 |
-c --create \ |
| 82 |
-d --debug \ |
| 83 |
-f --force \ |
| 84 |
-h --help \ |
| 85 |
-j --json \ |
| 86 |
-n --noerr \ |
| 87 |
-1 --once \ |
| 88 |
-p --plain \ |
| 89 |
-q --quiet \ |
| 90 |
-t --no-heading \ |
| 91 |
-v --verbose \ |
| 92 |
-V --version" |
| 93 |
|
| 94 |
case "${firstword}" in |
| 95 |
enable) |
| 96 |
COMPREPLY=($(compgen -W "$(_available)" -- $cur)) |
| 97 |
;; |
| 98 |
disable|touch) |
| 99 |
COMPREPLY=($(compgen -W "$(_enabled)" -- $cur)) |
| 100 |
;; |
| 101 |
show|cat|edit|delete) |
| 102 |
COMPREPLY=($(compgen -W "$(_svc .)" -- $cur)) |
| 103 |
;; |
| 104 |
start|stop|restart|log|status) |
| 105 |
COMPREPLY=($(compgen -W "$(_ident)" -- $cur)) |
| 106 |
;; |
| 107 |
signal|kill) |
| 108 |
if $(_ident "${prev}"); then |
| 109 |
COMPREPLY=($(compgen -W "$signals" -- $cur)) |
| 110 |
else |
| 111 |
COMPREPLY=($(compgen -W "$(_ident)" -- $cur)) |
| 112 |
fi |
| 113 |
;; |
| 114 |
cond) |
| 115 |
case "${lastword}" in |
| 116 |
set|clear) |
| 117 |
compopt -o nospace |
| 118 |
COMPREPLY=($(compgen -W "usr/" -- $cur)) |
| 119 |
;; |
| 120 |
get) |
| 121 |
COMPREPLY=($(compgen -W "$(_cond)" -- $cur)) |
| 122 |
;; |
| 123 |
dump) |
| 124 |
COMPREPLY=($(compgen -W "$cond_types" -- $cur)) |
| 125 |
;; |
| 126 |
*) |
| 127 |
COMPREPLY=($(compgen -W "$cond_cmds" -- $cur)) |
| 128 |
;; |
| 129 |
esac |
| 130 |
;; |
| 131 |
*) |
| 132 |
COMPREPLY=($(compgen -W "$commands" -- $cur)) |
| 133 |
;; |
| 134 |
esac |
| 135 |
|
| 136 |
if [[ $cur == -* ]]; then |
| 137 |
COMPREPLY=($(compgen -W "$options" -- $cur)) |
| 138 |
fi |
| 139 |
} |
| 140 |
|
| 141 |
complete -F _initctl initctl |