File: initctl

package info (click to toggle)
finit 4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,216 kB
  • sloc: ansic: 17,060; sh: 6,281; makefile: 532
file content (141 lines) | stat: -rw-r--r-- 2,934 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
_cond()
{
    initctl -pt cond dump |  awk '{print $4}' | sed 's/<\(.*\)>/\1/'
}

_ident()
{
    if [ -n "$1" ]; then
	initctl ident | grep -q $1
    else
	initctl ident
    fi
}

_svc()
{
    initctl ls -pt | grep $1 | sed "s/.*\/\(.*\)/\1/g" | sort -u
}

_enabled()
{
    echo "$(_svc enabled)"
}

_available()
{
    all=$(mktemp)
    ena=$(mktemp)
    echo "$(_svc available)" >$all
    echo "$(_svc enabled)"   >$ena
    grep -v -f $ena $all
    rm $all $ena
}

# Determine first non-option word. Usually the command
_firstword() {
	local firstword i

	firstword=
	for ((i = 1; i < ${#COMP_WORDS[@]}; ++i)); do
		if [[ ${COMP_WORDS[i]} != -* ]]; then
			firstword=${COMP_WORDS[i]}
			break
		fi
	done

	echo $firstword
}

# Determine last non-option word. Uusally a sub-command
_lastword() {
	local lastword i

	lastword=
	for ((i = 1; i < ${#COMP_WORDS[@]}; ++i)); do
		if [[ ${COMP_WORDS[i]} != -* ]] && [[ -n ${COMP_WORDS[i]} ]] && [[ ${COMP_WORDS[i]} != $cur ]]; then
			lastword=${COMP_WORDS[i]}
		fi
	done

	echo $lastword
}

_initctl()
{
    local cur command

    cur=${COMP_WORDS[COMP_CWORD]}
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    firstword=$(_firstword)
    lastword=$(_lastword)

    commands="status cond debug help kill ls log version  list enable	\
	      disable touch show cat edit create delete reload start	\
	      stop restart signal cgroup ps top plugins runlevel reboot	\
	      halt poweroff suspend utmp"
    cond_cmds="set get clear status dump"
    cond_types="hook net pid service task usr"
    signals="int term hup stop tstp cont usr1 usr2 pwr"
    options="-b --batch				\
	     -c --create			\
	     -d --debug				\
	     -f --force				\
	     -h --help				\
	     -j --json				\
	     -n --noerr				\
	     -1 --once				\
	     -p --plain				\
	     -q --quiet				\
	     -t --no-heading			\
	     -v --verbose			\
	     -V --version"

    case "${firstword}" in
	enable)
	    COMPREPLY=($(compgen -W "$(_available)" -- $cur))
	    ;;
	disable|touch)
	    COMPREPLY=($(compgen -W "$(_enabled)" -- $cur))
	    ;;
	show|cat|edit|delete)
	    COMPREPLY=($(compgen -W "$(_svc .)" -- $cur))
	    ;;
	start|stop|restart|log|status)
	    COMPREPLY=($(compgen -W "$(_ident)" -- $cur))
	    ;;
	signal|kill)
	    if $(_ident "${prev}"); then
		COMPREPLY=($(compgen -W "$signals" -- $cur))
	    else
		COMPREPLY=($(compgen -W "$(_ident)" -- $cur))
	    fi
	    ;;
	cond)
	    case "${lastword}" in
		set|clear)
		    compopt -o nospace
		    COMPREPLY=($(compgen -W "usr/" -- $cur))
		    ;;
		get)
		    COMPREPLY=($(compgen -W "$(_cond)" -- $cur))
		    ;;
		dump)
		    COMPREPLY=($(compgen -W "$cond_types" -- $cur))
		    ;;
		*)
		    COMPREPLY=($(compgen -W "$cond_cmds" -- $cur))
		    ;;
	    esac
	    ;;
	*)
	    COMPREPLY=($(compgen -W "$commands" -- $cur))
	    ;;
    esac

    if [[ $cur == -* ]]; then
	COMPREPLY=($(compgen -W "$options" -- $cur))
    fi
}

complete -F _initctl initctl