blob: 93be92826c57ff54f18ef0d6266e0c67d939c5bf [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
39 COMPREPLY=( $(compgen -W "$(_flags)" -- ${cur}) )
40 return 0
41 fi
42 return 1
43}
44
Darin Petkov049dbab2010-02-23 18:16:37 -080045# Look for "--arg=foo" or "--arg foo" (where foo can be an empty string) in the
46# word to be completed. If found, echo "--arg=foo".
Darin Petkov593e7962010-08-13 14:45:02 -070047_argeq() {
Darin Petkov049dbab2010-02-23 18:16:37 -080048 local arg=$1
49 local w0="${COMP_WORDS[COMP_CWORD]}"
50 local w1="${COMP_WORDS[COMP_CWORD-1]}"
51
52 # Check for completing "--arg="
53 if [ "${w1}" == ${arg} -a "${w0}" == "=" ]; then
54 echo "${w1}${w0}"
55 return 0
56 fi
57
58 # Check for completing "--arg foo"
59 if [ "${w1}" == ${arg} ]; then
60 echo "${w1}=${w0}"
61 return 0
62 fi
63
64 # Check for completing "--arg=foo"
65 if [ ${COMP_CWORD} -gt 2 ]; then
66 local w2="${COMP_WORDS[COMP_CWORD-2]}"
67 if [ "${w2}" == ${arg} -a "${w1}" == "=" ]; then
68 echo "${w2}${w1}${w0}"
69 return 0
70 fi
71 fi
72}
73
74
75# echo the existing target board sysroots
Darin Petkov593e7962010-08-13 14:45:02 -070076_board_sysroots() {
Darin Petkov049dbab2010-02-23 18:16:37 -080077 local builddir=/build
Darin Petkovd6bf1012010-02-26 13:10:11 -080078 if [ -d ${builddir} ]; then
Darin Petkov049dbab2010-02-23 18:16:37 -080079 echo $(command ls "${builddir}")
80 fi
81}
82
Darin Petkov593e7962010-08-13 14:45:02 -070083_complete_board_sysroot_flag() {
Eric Li3d13cd12010-03-31 11:47:22 -070084 COMPREPLY=()
85 local arg=$(_argeq --board)
86 if [[ ${arg} == --board=* ]]; then
87 COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) )
88 return 0
89 fi
90 return 1
91}
92
Darin Petkov049dbab2010-02-23 18:16:37 -080093# Completion for --board= argument for existing board sysroots
Darin Petkov593e7962010-08-13 14:45:02 -070094_board_sysroot() {
Darin Petkovd6bf1012010-02-26 13:10:11 -080095 _flag_complete && return 0
Eric Licad2b492010-04-01 13:07:10 -070096 _complete_board_sysroot_flag && return 0
Darin Petkov049dbab2010-02-23 18:16:37 -080097}
98
Darin Petkov049dbab2010-02-23 18:16:37 -080099# echo the existing target board overlays
Darin Petkov593e7962010-08-13 14:45:02 -0700100_board_overlays() {
Darin Petkov049dbab2010-02-23 18:16:37 -0800101 local overlaydir=../overlays
Darin Petkovd6bf1012010-02-26 13:10:11 -0800102 if [ -d ${overlaydir} ]; then
Darin Petkov049dbab2010-02-23 18:16:37 -0800103 echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,)
104 fi
105}
106
107# Completion for --board= argument for existing board overlays
Darin Petkov593e7962010-08-13 14:45:02 -0700108_board_overlay() {
Darin Petkovd6bf1012010-02-26 13:10:11 -0800109 _flag_complete && return 0
110
Darin Petkov049dbab2010-02-23 18:16:37 -0800111 COMPREPLY=()
112 local arg=$(_argeq --board)
113 if [[ ${arg} == --board=* ]]; then
114 COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) )
Darin Petkov049dbab2010-02-23 18:16:37 -0800115 fi
116}
117
Eric Li3d13cd12010-03-31 11:47:22 -0700118# Completion for -c and -s argument for autotest script
119_ls_autotest() {
120 local autotest_dir=../third_party/autotest/files
Darin Petkov593e7962010-08-13 14:45:02 -0700121 ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null \
122 | sed s/"..\/third_party\/autotest\/files\/"//g
Eric Li3d13cd12010-03-31 11:47:22 -0700123}
124
Darin Petkov593e7962010-08-13 14:45:02 -0700125_autotest_complete() {
Eric Li3d13cd12010-03-31 11:47:22 -0700126 _flag_complete && return 0
127
128 local arg=$(_argeq -c)
129 if [[ ${arg} == -c=* ]]; then
130 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-c=})"))
131 return 0
132 fi
133
134 arg=$(_argeq -s)
135 if [[ ${arg} == -s=* ]]; then
136 COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-s=})"))
137 return 0
138 fi
139
140 _complete_board_sysroot_flag && return 0
141}
142
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700143# Complete cros_workon's <command> argument.
Darin Petkov593e7962010-08-13 14:45:02 -0700144#
145# TODO(petkov): We should probably extract the list of commands from
146# cros_workon --help, just like we do for flags (see _flag_complete).
147#
148# TODO(petkov): Currently, this assumes that the command is the first
149# argument. In practice, the command is the first non-flag
150# argument. I.e., this should be fixed to support something like
Mandeep Singh Baines93da99b2011-02-05 11:42:19 -0800151# "cros_workon --all list".
Darin Petkov6944c5d2010-08-18 09:57:34 -0700152_complete_cros_workon_command() {
Darin Petkov593e7962010-08-13 14:45:02 -0700153 [ ${COMP_CWORD} -eq 1 ] || return 1
154 local command="${COMP_WORDS[1]}"
155 COMPREPLY=($(compgen -W "start stop list iterate" -- "$command"))
156 return 0
157}
158
Darin Petkov6944c5d2010-08-18 09:57:34 -0700159# Prints the full path to the cros_workon executable, handling tilde
160# expansion for the current user.
161_cros_workon_executable() {
162 local cros_workon="${COMP_WORDS[0]}"
163 if [[ "$cros_workon" == '~/'* ]]; then
164 cros_workon="$HOME/${cros_workon#'~/'}"
165 fi
166 echo "$cros_workon"
167}
168
Darin Petkov593e7962010-08-13 14:45:02 -0700169# Lists the workon (or live, if --all is passed in) ebuilds. Lists
170# both the full names (e.g., chromeos-base/metrics) as well as just
171# the ebuild names (e.g., metrics).
172_cros_workon_list() {
Darin Petkov6944c5d2010-08-18 09:57:34 -0700173 local cros_workon=$(_cros_workon_executable)
Darin Petkov593e7962010-08-13 14:45:02 -0700174 ${cros_workon} list $1 | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,'
175}
176
177# Completes the current cros_workon argument assuming it's a
178# package/ebuild name.
Darin Petkov6944c5d2010-08-18 09:57:34 -0700179_complete_cros_workon_package() {
Darin Petkov593e7962010-08-13 14:45:02 -0700180 [ ${COMP_CWORD} -gt 1 ] || return 1
181 local package="${COMP_WORDS[COMP_CWORD]}"
182 local command="${COMP_WORDS[1]}"
183 # If "start", complete based on all workon packages.
184 if [[ ${command} == "start" ]]; then
185 COMPREPLY=($(compgen -W "$(_cros_workon_list --all)" -- "$package"))
186 return 0
187 fi
188 # If "stop" or "iterate", complete based on all live packages.
189 if [[ ${command} == "stop" ]] || [[ ${command} == "iterate" ]]; then
190 COMPREPLY=($(compgen -W "$(_cros_workon_list)" -- "$package"))
191 return 0
192 fi
193 return 1
194}
195
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700196# Complete cros_workon arguments.
Darin Petkov593e7962010-08-13 14:45:02 -0700197_cros_workon() {
198 COMPREPLY=()
Darin Petkov593e7962010-08-13 14:45:02 -0700199 _flag_complete && return 0
200 _complete_board_sysroot_flag && return 0
Darin Petkov6944c5d2010-08-18 09:57:34 -0700201 _complete_cros_workon_command && return 0
202 _complete_cros_workon_package && return 0
Darin Petkov593e7962010-08-13 14:45:02 -0700203 return 0
204}
205
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700206_complete_cros_command() {
207 local command="${COMP_WORDS[COMP_CWORD]}"
208 if [ ${COMP_CWORD} -ne 1 ]; then
209 COMPREPLY=($(compgen -o default -- "${command}"))
210 return 0
211 fi
212
213 local key="subcmds/cros"
214 if [[ -z "${_comp_cache[${key}]}" ]]; then
215 _comp_cache[${key}]="$(command cros --help | _subcmds_from_help)"
216 fi
217 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${command}"))
218 return 0
219}
220
221# Complete cros arguments.
222_cros() {
223 COMPREPLY=()
224 _flag_complete && return 0
225 _complete_board_sysroot_flag && return 0
226 _complete_cros_command && return 0
227 # TODO(hesling): Add package completion like cros_workon.
228 return 0
229}
230
Aviv Keshetb08a89a2013-03-12 12:27:49 -0700231# Complete equery's <module-name> argument.
232_complete_equery_module_name() {
233 [ ${COMP_CWORD} -eq 1 ] || return 1
234 local command="${COMP_WORDS[1]}"
235 COMPREPLY=($(compgen -W "belongs changes check depends depgraph files has \
236 hasuse keywords list meta size uses which" \
237 -- "$command"))
238 return 0
239}
240
241# Complete equery arguments.
242_complete_equery() {
243 COMPREPLY=()
244 _complete_equery_module_name && return 0
245 return 0
246}
247
Darin Petkov593e7962010-08-13 14:45:02 -0700248complete -o bashdefault -o default -F _board_sysroot \
249 build_autotest.sh \
250 build_image \
251 build_packages \
Darin Petkov593e7962010-08-13 14:45:02 -0700252 mod_image_for_test.sh
253complete -o bashdefault -o default -F _board_overlay setup_board
Eric Li3d13cd12010-03-31 11:47:22 -0700254complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700255complete -F _cros cros
Darin Petkov6944c5d2010-08-18 09:57:34 -0700256complete -F _cros_workon cros_workon
Aviv Keshet7af8fe32013-03-14 16:03:31 -0700257complete -o bashdefault -o default -F _complete_equery equery
Darin Petkov049dbab2010-02-23 18:16:37 -0800258
Aviv Keshet7e743ed2013-03-14 18:37:54 -0700259# Use equery completion for equery-$board for known boards
260_boardlist=$(cros_list_overlays | egrep "^.*/overlays/overlay-.*$" |
261 sed -n "s/.*overlay-//p")
262for board in $_boardlist; do
263 complete -o bashdefault -o default -F _complete_equery equery-$board
264done
265
Darin Petkov049dbab2010-02-23 18:16:37 -0800266### Local Variables:
267### mode: shell-script
268### End: