blob: 4dc43b27b0371288c93f3611b4742352b2349bb9 [file] [log] [blame]
Chris Sosac1bd1112011-08-26 15:22:10 -07001# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Darin Petkov049dbab2010-02-23 18:16:37 -08002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# Add programmable completion to some Chromium OS build scripts
6
Craig Hesling8c3e5f12019-10-31 10:34:08 -07007# Declare one cache for all bash complete operations to
8# allow it to be easiley cleared if need be.
9# > unset _comp_cache
10declare -A _comp_cache
11
12# Usage: cros --help | _subcmds_from_help
13# Parse subcommands from a commands's help message.
14# Trims the help outputs after it see any token with "command"
15# in it. Then, it searches for a no-space bracketed token,
16# similar to the following:
17# {subcmd1,subcmd2,subcmd3}
18_subcmds_from_help() {
19 sed -n -e '/commands:/,$p' \
20 | egrep -o '\{[[:alnum:]_-,]+\}' | sort -u \
21 | tr -d '{}' | tr ',' ' '
22 return ${PIPESTATUS[0]}
23}
Darin Petkov049dbab2010-02-23 18:16:37 -080024
Darin Petkovd6bf1012010-02-26 13:10:11 -080025# Echo a list of -- flags that the current command accepts. The
26# function assumes that the command supports shflags' --help flag.
Darin Petkov593e7962010-08-13 14:45:02 -070027_flags() {
28 echo $(command "${COMP_WORDS[0]}" --help 2>&1 \
Craig Heslingf241ea92019-10-31 10:18:58 -070029 | egrep -o -- '--[[:alnum:]=_-]+' \
Darin Petkov593e7962010-08-13 14:45:02 -070030 | sed 's/://; s/--\[no\]\(.\+\)/--\1 --no\1/')
Darin Petkovd6bf1012010-02-26 13:10:11 -080031}
32
Darin Petkovd6bf1012010-02-26 13:10:11 -080033# Complete flags, i.e., current words starting with --. Return 1 if
34# the current word doesn't start with --, 0 otherwise.
Darin Petkov593e7962010-08-13 14:45:02 -070035_flag_complete() {
Darin Petkovd6bf1012010-02-26 13:10:11 -080036 COMPREPLY=()
37 local cur="${COMP_WORDS[COMP_CWORD]}"
38 if [[ "${cur}" == --* ]]; then
Craig Hesling3b06f212019-11-01 14:18:10 -070039 local key="flags/${COMP_WORDS[0]}"
40 if [[ -z "${_comp_cache[${key}]}" ]]; then
41 _comp_cache[${key}]="$(_flags)"
42 fi
43 COMPREPLY=( $(compgen -W "${_comp_cache[${key}]}" -- "${cur}") )
Darin Petkovd6bf1012010-02-26 13:10:11 -080044 return 0
45 fi
46 return 1
47}
48
Darin Petkov049dbab2010-02-23 18:16:37 -080049# Look for "--arg=foo" or "--arg foo" (where foo can be an empty string) in the
50# word to be completed. If found, echo "--arg=foo".
Darin Petkov593e7962010-08-13 14:45:02 -070051_argeq() {
Darin Petkov049dbab2010-02-23 18:16:37 -080052 local arg=$1
53 local w0="${COMP_WORDS[COMP_CWORD]}"
54 local w1="${COMP_WORDS[COMP_CWORD-1]}"
55
56 # Check for completing "--arg="
57 if [ "${w1}" == ${arg} -a "${w0}" == "=" ]; then
58 echo "${w1}${w0}"
59 return 0
60 fi
61
62 # Check for completing "--arg foo"
63 if [ "${w1}" == ${arg} ]; then
64 echo "${w1}=${w0}"
65 return 0
66 fi
67
68 # Check for completing "--arg=foo"
69 if [ ${COMP_CWORD} -gt 2 ]; then
70 local w2="${COMP_WORDS[COMP_CWORD-2]}"
71 if [ "${w2}" == ${arg} -a "${w1}" == "=" ]; then
72 echo "${w2}${w1}${w0}"
73 return 0
74 fi
75 fi
76}
77
78
79# echo the existing target board sysroots
Darin Petkov593e7962010-08-13 14:45:02 -070080_board_sysroots() {
Darin Petkov049dbab2010-02-23 18:16:37 -080081 local builddir=/build
Darin Petkovd6bf1012010-02-26 13:10:11 -080082 if [ -d ${builddir} ]; then
Darin Petkov049dbab2010-02-23 18:16:37 -080083 echo $(command ls "${builddir}")
84 fi
85}
86
Darin Petkov593e7962010-08-13 14:45:02 -070087_complete_board_sysroot_flag() {
Eric Li3d13cd12010-03-31 11:47:22 -070088 COMPREPLY=()
89 local arg=$(_argeq --board)
90 if [[ ${arg} == --board=* ]]; then
91 COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) )
92 return 0
93 fi
94 return 1
95}
96
Darin Petkov049dbab2010-02-23 18:16:37 -080097# Completion for --board= argument for existing board sysroots
Darin Petkov593e7962010-08-13 14:45:02 -070098_board_sysroot() {
Darin Petkovd6bf1012010-02-26 13:10:11 -080099 _flag_complete && return 0
Eric Licad2b492010-04-01 13:07:10 -0700100 _complete_board_sysroot_flag && return 0
Darin Petkov049dbab2010-02-23 18:16:37 -0800101}
102
Darin Petkov049dbab2010-02-23 18:16:37 -0800103# echo the existing target board overlays
Darin Petkov593e7962010-08-13 14:45:02 -0700104_board_overlays() {
Darin Petkov049dbab2010-02-23 18:16:37 -0800105 local overlaydir=../overlays
Darin Petkovd6bf1012010-02-26 13:10:11 -0800106 if [ -d ${overlaydir} ]; then
Darin Petkov049dbab2010-02-23 18:16:37 -0800107 echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,)
108 fi
109}
110
111# Completion for --board= argument for existing board overlays
Darin Petkov593e7962010-08-13 14:45:02 -0700112_board_overlay() {
Darin Petkovd6bf1012010-02-26 13:10:11 -0800113 _flag_complete && return 0
114
Darin Petkov049dbab2010-02-23 18:16:37 -0800115 COMPREPLY=()
116 local arg=$(_argeq --board)
117 if [[ ${arg} == --board=* ]]; then
118 COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) )
Darin Petkov049dbab2010-02-23 18:16:37 -0800119 fi
120}
121
Eric Li3d13cd12010-03-31 11:47:22 -0700122# Completion for -c and -s argument for autotest script
123_ls_autotest() {
124 local autotest_dir=../third_party/autotest/files
Darin Petkov593e7962010-08-13 14:45:02 -0700125 ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null \
126 | sed s/"..\/third_party\/autotest\/files\/"//g
Eric Li3d13cd12010-03-31 11:47:22 -0700127}
128
Darin Petkov593e7962010-08-13 14:45:02 -0700129_autotest_complete() {
Eric Li3d13cd12010-03-31 11:47:22 -0700130 _flag_complete && return 0
131
132 local arg=$(_argeq -c)
133 if [[ ${arg} == -c=* ]]; then
134 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-c=})"))
135 return 0
136 fi
137
138 arg=$(_argeq -s)
139 if [[ ${arg} == -s=* ]]; then
140 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-s=})"))
141 return 0
142 fi
143
144 _complete_board_sysroot_flag && return 0
145}
146
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700147# Complete cros_workon's <command> argument.
Darin Petkov593e7962010-08-13 14:45:02 -0700148#
149# TODO(petkov): We should probably extract the list of commands from
150# cros_workon --help, just like we do for flags (see _flag_complete).
151#
152# TODO(petkov): Currently, this assumes that the command is the first
153# argument. In practice, the command is the first non-flag
154# argument. I.e., this should be fixed to support something like
Mandeep Singh Baines93da99b2011-02-05 11:42:19 -0800155# "cros_workon --all list".
Darin Petkov6944c5d2010-08-18 09:57:34 -0700156_complete_cros_workon_command() {
Darin Petkov593e7962010-08-13 14:45:02 -0700157 [ ${COMP_CWORD} -eq 1 ] || return 1
158 local command="${COMP_WORDS[1]}"
Craig Hesling1f5f5a32019-10-31 13:19:45 -0700159
160 # TODO(hesling): Local scoped references to associative arrays
161 # seems to be broken in bash version 4.3.48, but working in version 5.
162 # We can beautify this by using the following command, when cros bash
163 # is updated.
164 # local -n cache="_comp_cache[subcmds/cros_workon]"
165
166 local key="subcmds/cros_workon"
167 if [[ -z "${_comp_cache[${key}]}" ]]; then
168 _comp_cache[${key}]="$(command cros_workon --help | _subcmds_from_help)"
169 fi
170 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${command}"))
Darin Petkov593e7962010-08-13 14:45:02 -0700171 return 0
172}
173
Darin Petkov6944c5d2010-08-18 09:57:34 -0700174# Prints the full path to the cros_workon executable, handling tilde
175# expansion for the current user.
176_cros_workon_executable() {
177 local cros_workon="${COMP_WORDS[0]}"
178 if [[ "$cros_workon" == '~/'* ]]; then
179 cros_workon="$HOME/${cros_workon#'~/'}"
180 fi
181 echo "$cros_workon"
182}
183
Darin Petkov593e7962010-08-13 14:45:02 -0700184# Lists the workon (or live, if --all is passed in) ebuilds. Lists
185# both the full names (e.g., chromeos-base/metrics) as well as just
186# the ebuild names (e.g., metrics).
187_cros_workon_list() {
Darin Petkov6944c5d2010-08-18 09:57:34 -0700188 local cros_workon=$(_cros_workon_executable)
Craig Hesling33b60212019-11-01 14:16:49 -0700189 ${cros_workon} $1 list | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,'
Darin Petkov593e7962010-08-13 14:45:02 -0700190}
191
192# Completes the current cros_workon argument assuming it's a
193# package/ebuild name.
Darin Petkov6944c5d2010-08-18 09:57:34 -0700194_complete_cros_workon_package() {
Darin Petkov593e7962010-08-13 14:45:02 -0700195 [ ${COMP_CWORD} -gt 1 ] || return 1
196 local package="${COMP_WORDS[COMP_CWORD]}"
197 local command="${COMP_WORDS[1]}"
198 # If "start", complete based on all workon packages.
199 if [[ ${command} == "start" ]]; then
Craig Hesling6db16b52019-11-01 14:17:43 -0700200 local key="pkgs/all"
201 if [[ -z "${_comp_cache[${key}]}" ]]; then
202 _comp_cache[${key}]="$(_cros_workon_list --all)"
203 fi
204 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${package}"))
Darin Petkov593e7962010-08-13 14:45:02 -0700205 return 0
206 fi
207 # If "stop" or "iterate", complete based on all live packages.
208 if [[ ${command} == "stop" ]] || [[ ${command} == "iterate" ]]; then
209 COMPREPLY=($(compgen -W "$(_cros_workon_list)" -- "$package"))
210 return 0
211 fi
212 return 1
213}
214
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700215# Complete cros_workon arguments.
Darin Petkov593e7962010-08-13 14:45:02 -0700216_cros_workon() {
217 COMPREPLY=()
Darin Petkov593e7962010-08-13 14:45:02 -0700218 _flag_complete && return 0
219 _complete_board_sysroot_flag && return 0
Darin Petkov6944c5d2010-08-18 09:57:34 -0700220 _complete_cros_workon_command && return 0
221 _complete_cros_workon_package && return 0
Darin Petkov593e7962010-08-13 14:45:02 -0700222 return 0
223}
224
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700225_complete_cros_command() {
226 local command="${COMP_WORDS[COMP_CWORD]}"
227 if [ ${COMP_CWORD} -ne 1 ]; then
228 COMPREPLY=($(compgen -o default -- "${command}"))
229 return 0
230 fi
231
232 local key="subcmds/cros"
233 if [[ -z "${_comp_cache[${key}]}" ]]; then
234 _comp_cache[${key}]="$(command cros --help | _subcmds_from_help)"
235 fi
236 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${command}"))
237 return 0
238}
239
240# Complete cros arguments.
241_cros() {
242 COMPREPLY=()
243 _flag_complete && return 0
244 _complete_board_sysroot_flag && return 0
245 _complete_cros_command && return 0
246 # TODO(hesling): Add package completion like cros_workon.
247 return 0
248}
249
Aviv Keshetb08a89a2013-03-12 12:27:49 -0700250# Complete equery's <module-name> argument.
251_complete_equery_module_name() {
252 [ ${COMP_CWORD} -eq 1 ] || return 1
253 local command="${COMP_WORDS[1]}"
254 COMPREPLY=($(compgen -W "belongs changes check depends depgraph files has \
255 hasuse keywords list meta size uses which" \
256 -- "$command"))
257 return 0
258}
259
260# Complete equery arguments.
261_complete_equery() {
262 COMPREPLY=()
263 _complete_equery_module_name && return 0
264 return 0
265}
266
Darin Petkov593e7962010-08-13 14:45:02 -0700267complete -o bashdefault -o default -F _board_sysroot \
268 build_autotest.sh \
269 build_image \
270 build_packages \
Darin Petkov593e7962010-08-13 14:45:02 -0700271 mod_image_for_test.sh
272complete -o bashdefault -o default -F _board_overlay setup_board
Eric Li3d13cd12010-03-31 11:47:22 -0700273complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700274complete -F _cros cros
Darin Petkov6944c5d2010-08-18 09:57:34 -0700275complete -F _cros_workon cros_workon
Aviv Keshet7af8fe32013-03-14 16:03:31 -0700276complete -o bashdefault -o default -F _complete_equery equery
Darin Petkov049dbab2010-02-23 18:16:37 -0800277
Aviv Keshet7e743ed2013-03-14 18:37:54 -0700278# Use equery completion for equery-$board for known boards
279_boardlist=$(cros_list_overlays | egrep "^.*/overlays/overlay-.*$" |
280 sed -n "s/.*overlay-//p")
281for board in $_boardlist; do
282 complete -o bashdefault -o default -F _complete_equery equery-$board
283done
284
Darin Petkov049dbab2010-02-23 18:16:37 -0800285### Local Variables:
286### mode: shell-script
287### End: