blob: bd4e3cf177995dcf8f75cd399a2ff373feee6b18 [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() {
Craig Heslinge6bbc552019-11-13 09:46:46 -080028 command "$@" --help 2>&1 \
29 | egrep -o -- '--(\[no\])?[[:alnum:]=_-]+' \
30 | sed -E -e '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
Craig Heslinge6bbc552019-11-13 09:46:46 -080041 _comp_cache[${key}]="$(_flags "${COMP_WORDS[0]}")"
Craig Hesling3b06f212019-11-01 14:18:10 -070042 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
Craig Heslingd89d5982019-11-01 16:10:03 -0700147_test_that_complete() {
148 _flag_complete && return 0
149 return 0
150}
151
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700152# Complete cros_workon's <command> argument.
Darin Petkov593e7962010-08-13 14:45:02 -0700153#
154# TODO(petkov): We should probably extract the list of commands from
155# cros_workon --help, just like we do for flags (see _flag_complete).
156#
157# TODO(petkov): Currently, this assumes that the command is the first
158# argument. In practice, the command is the first non-flag
159# argument. I.e., this should be fixed to support something like
Mandeep Singh Baines93da99b2011-02-05 11:42:19 -0800160# "cros_workon --all list".
Darin Petkov6944c5d2010-08-18 09:57:34 -0700161_complete_cros_workon_command() {
Darin Petkov593e7962010-08-13 14:45:02 -0700162 [ ${COMP_CWORD} -eq 1 ] || return 1
163 local command="${COMP_WORDS[1]}"
Craig Hesling1f5f5a32019-10-31 13:19:45 -0700164
165 # TODO(hesling): Local scoped references to associative arrays
166 # seems to be broken in bash version 4.3.48, but working in version 5.
167 # We can beautify this by using the following command, when cros bash
168 # is updated.
169 # local -n cache="_comp_cache[subcmds/cros_workon]"
170
171 local key="subcmds/cros_workon"
172 if [[ -z "${_comp_cache[${key}]}" ]]; then
173 _comp_cache[${key}]="$(command cros_workon --help | _subcmds_from_help)"
174 fi
175 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${command}"))
Darin Petkov593e7962010-08-13 14:45:02 -0700176 return 0
177}
178
Darin Petkov6944c5d2010-08-18 09:57:34 -0700179# Prints the full path to the cros_workon executable, handling tilde
180# expansion for the current user.
181_cros_workon_executable() {
182 local cros_workon="${COMP_WORDS[0]}"
183 if [[ "$cros_workon" == '~/'* ]]; then
184 cros_workon="$HOME/${cros_workon#'~/'}"
185 fi
186 echo "$cros_workon"
187}
188
Darin Petkov593e7962010-08-13 14:45:02 -0700189# Lists the workon (or live, if --all is passed in) ebuilds. Lists
190# both the full names (e.g., chromeos-base/metrics) as well as just
191# the ebuild names (e.g., metrics).
192_cros_workon_list() {
Darin Petkov6944c5d2010-08-18 09:57:34 -0700193 local cros_workon=$(_cros_workon_executable)
Craig Hesling33b60212019-11-01 14:16:49 -0700194 ${cros_workon} $1 list | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,'
Darin Petkov593e7962010-08-13 14:45:02 -0700195}
196
197# Completes the current cros_workon argument assuming it's a
198# package/ebuild name.
Darin Petkov6944c5d2010-08-18 09:57:34 -0700199_complete_cros_workon_package() {
Darin Petkov593e7962010-08-13 14:45:02 -0700200 [ ${COMP_CWORD} -gt 1 ] || return 1
201 local package="${COMP_WORDS[COMP_CWORD]}"
202 local command="${COMP_WORDS[1]}"
203 # If "start", complete based on all workon packages.
204 if [[ ${command} == "start" ]]; then
Craig Hesling6db16b52019-11-01 14:17:43 -0700205 local key="pkgs/all"
206 if [[ -z "${_comp_cache[${key}]}" ]]; then
207 _comp_cache[${key}]="$(_cros_workon_list --all)"
208 fi
209 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${package}"))
Darin Petkov593e7962010-08-13 14:45:02 -0700210 return 0
211 fi
212 # If "stop" or "iterate", complete based on all live packages.
213 if [[ ${command} == "stop" ]] || [[ ${command} == "iterate" ]]; then
214 COMPREPLY=($(compgen -W "$(_cros_workon_list)" -- "$package"))
215 return 0
216 fi
217 return 1
218}
219
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700220# Complete cros_workon arguments.
Darin Petkov593e7962010-08-13 14:45:02 -0700221_cros_workon() {
222 COMPREPLY=()
Darin Petkov593e7962010-08-13 14:45:02 -0700223 _flag_complete && return 0
224 _complete_board_sysroot_flag && return 0
Darin Petkov6944c5d2010-08-18 09:57:34 -0700225 _complete_cros_workon_command && return 0
226 _complete_cros_workon_package && return 0
Darin Petkov593e7962010-08-13 14:45:02 -0700227 return 0
228}
229
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700230_complete_cros_command() {
231 local command="${COMP_WORDS[COMP_CWORD]}"
232 if [ ${COMP_CWORD} -ne 1 ]; then
233 COMPREPLY=($(compgen -o default -- "${command}"))
234 return 0
235 fi
236
237 local key="subcmds/cros"
238 if [[ -z "${_comp_cache[${key}]}" ]]; then
239 _comp_cache[${key}]="$(command cros --help | _subcmds_from_help)"
240 fi
241 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${command}"))
242 return 0
243}
244
245# Complete cros arguments.
246_cros() {
247 COMPREPLY=()
248 _flag_complete && return 0
249 _complete_board_sysroot_flag && return 0
250 _complete_cros_command && return 0
251 # TODO(hesling): Add package completion like cros_workon.
252 return 0
253}
254
Craig Hesling74511202019-11-07 11:38:20 -0800255_cros_portage_upgrade() {
256 COMPREPLY=()
257 _flag_complete && return 0
258 _complete_board_sysroot_flag && return 0
259 return 0
260}
261
Aviv Keshetb08a89a2013-03-12 12:27:49 -0700262# Complete equery's <module-name> argument.
263_complete_equery_module_name() {
264 [ ${COMP_CWORD} -eq 1 ] || return 1
265 local command="${COMP_WORDS[1]}"
266 COMPREPLY=($(compgen -W "belongs changes check depends depgraph files has \
267 hasuse keywords list meta size uses which" \
268 -- "$command"))
269 return 0
270}
271
272# Complete equery arguments.
273_complete_equery() {
274 COMPREPLY=()
275 _complete_equery_module_name && return 0
276 return 0
277}
278
Darin Petkov593e7962010-08-13 14:45:02 -0700279complete -o bashdefault -o default -F _board_sysroot \
280 build_autotest.sh \
281 build_image \
282 build_packages \
Darin Petkov593e7962010-08-13 14:45:02 -0700283 mod_image_for_test.sh
284complete -o bashdefault -o default -F _board_overlay setup_board
Eric Li3d13cd12010-03-31 11:47:22 -0700285complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
Craig Heslingd89d5982019-11-01 16:10:03 -0700286complete -o bashdefault -o default -o nospace -F _test_that_complete test_that
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700287complete -F _cros cros
Darin Petkov6944c5d2010-08-18 09:57:34 -0700288complete -F _cros_workon cros_workon
Craig Hesling74511202019-11-07 11:38:20 -0800289complete -o bashdefault -o default -o nospace -F _cros_portage_upgrade \
290 cros_portage_upgrade
Aviv Keshet7af8fe32013-03-14 16:03:31 -0700291complete -o bashdefault -o default -F _complete_equery equery
Darin Petkov049dbab2010-02-23 18:16:37 -0800292
Aviv Keshet7e743ed2013-03-14 18:37:54 -0700293# Use equery completion for equery-$board for known boards
294_boardlist=$(cros_list_overlays | egrep "^.*/overlays/overlay-.*$" |
295 sed -n "s/.*overlay-//p")
296for board in $_boardlist; do
297 complete -o bashdefault -o default -F _complete_equery equery-$board
298done
299
Darin Petkov049dbab2010-02-23 18:16:37 -0800300### Local Variables:
301### mode: shell-script
302### End: