blob: 22b4729730ca9f3a07e40f19f373c3800a9629a7 [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]}"
Craig Hesling1f5f5a32019-10-31 13:19:45 -0700155
156 # TODO(hesling): Local scoped references to associative arrays
157 # seems to be broken in bash version 4.3.48, but working in version 5.
158 # We can beautify this by using the following command, when cros bash
159 # is updated.
160 # local -n cache="_comp_cache[subcmds/cros_workon]"
161
162 local key="subcmds/cros_workon"
163 if [[ -z "${_comp_cache[${key}]}" ]]; then
164 _comp_cache[${key}]="$(command cros_workon --help | _subcmds_from_help)"
165 fi
166 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${command}"))
Darin Petkov593e7962010-08-13 14:45:02 -0700167 return 0
168}
169
Darin Petkov6944c5d2010-08-18 09:57:34 -0700170# Prints the full path to the cros_workon executable, handling tilde
171# expansion for the current user.
172_cros_workon_executable() {
173 local cros_workon="${COMP_WORDS[0]}"
174 if [[ "$cros_workon" == '~/'* ]]; then
175 cros_workon="$HOME/${cros_workon#'~/'}"
176 fi
177 echo "$cros_workon"
178}
179
Darin Petkov593e7962010-08-13 14:45:02 -0700180# Lists the workon (or live, if --all is passed in) ebuilds. Lists
181# both the full names (e.g., chromeos-base/metrics) as well as just
182# the ebuild names (e.g., metrics).
183_cros_workon_list() {
Darin Petkov6944c5d2010-08-18 09:57:34 -0700184 local cros_workon=$(_cros_workon_executable)
Craig Hesling33b60212019-11-01 14:16:49 -0700185 ${cros_workon} $1 list | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,'
Darin Petkov593e7962010-08-13 14:45:02 -0700186}
187
188# Completes the current cros_workon argument assuming it's a
189# package/ebuild name.
Darin Petkov6944c5d2010-08-18 09:57:34 -0700190_complete_cros_workon_package() {
Darin Petkov593e7962010-08-13 14:45:02 -0700191 [ ${COMP_CWORD} -gt 1 ] || return 1
192 local package="${COMP_WORDS[COMP_CWORD]}"
193 local command="${COMP_WORDS[1]}"
194 # If "start", complete based on all workon packages.
195 if [[ ${command} == "start" ]]; then
196 COMPREPLY=($(compgen -W "$(_cros_workon_list --all)" -- "$package"))
197 return 0
198 fi
199 # If "stop" or "iterate", complete based on all live packages.
200 if [[ ${command} == "stop" ]] || [[ ${command} == "iterate" ]]; then
201 COMPREPLY=($(compgen -W "$(_cros_workon_list)" -- "$package"))
202 return 0
203 fi
204 return 1
205}
206
Darin Petkov5ad2cb52010-08-18 14:01:56 -0700207# Complete cros_workon arguments.
Darin Petkov593e7962010-08-13 14:45:02 -0700208_cros_workon() {
209 COMPREPLY=()
Darin Petkov593e7962010-08-13 14:45:02 -0700210 _flag_complete && return 0
211 _complete_board_sysroot_flag && return 0
Darin Petkov6944c5d2010-08-18 09:57:34 -0700212 _complete_cros_workon_command && return 0
213 _complete_cros_workon_package && return 0
Darin Petkov593e7962010-08-13 14:45:02 -0700214 return 0
215}
216
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700217_complete_cros_command() {
218 local command="${COMP_WORDS[COMP_CWORD]}"
219 if [ ${COMP_CWORD} -ne 1 ]; then
220 COMPREPLY=($(compgen -o default -- "${command}"))
221 return 0
222 fi
223
224 local key="subcmds/cros"
225 if [[ -z "${_comp_cache[${key}]}" ]]; then
226 _comp_cache[${key}]="$(command cros --help | _subcmds_from_help)"
227 fi
228 COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${command}"))
229 return 0
230}
231
232# Complete cros arguments.
233_cros() {
234 COMPREPLY=()
235 _flag_complete && return 0
236 _complete_board_sysroot_flag && return 0
237 _complete_cros_command && return 0
238 # TODO(hesling): Add package completion like cros_workon.
239 return 0
240}
241
Aviv Keshetb08a89a2013-03-12 12:27:49 -0700242# Complete equery's <module-name> argument.
243_complete_equery_module_name() {
244 [ ${COMP_CWORD} -eq 1 ] || return 1
245 local command="${COMP_WORDS[1]}"
246 COMPREPLY=($(compgen -W "belongs changes check depends depgraph files has \
247 hasuse keywords list meta size uses which" \
248 -- "$command"))
249 return 0
250}
251
252# Complete equery arguments.
253_complete_equery() {
254 COMPREPLY=()
255 _complete_equery_module_name && return 0
256 return 0
257}
258
Darin Petkov593e7962010-08-13 14:45:02 -0700259complete -o bashdefault -o default -F _board_sysroot \
260 build_autotest.sh \
261 build_image \
262 build_packages \
Darin Petkov593e7962010-08-13 14:45:02 -0700263 mod_image_for_test.sh
264complete -o bashdefault -o default -F _board_overlay setup_board
Eric Li3d13cd12010-03-31 11:47:22 -0700265complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
Craig Hesling8c3e5f12019-10-31 10:34:08 -0700266complete -F _cros cros
Darin Petkov6944c5d2010-08-18 09:57:34 -0700267complete -F _cros_workon cros_workon
Aviv Keshet7af8fe32013-03-14 16:03:31 -0700268complete -o bashdefault -o default -F _complete_equery equery
Darin Petkov049dbab2010-02-23 18:16:37 -0800269
Aviv Keshet7e743ed2013-03-14 18:37:54 -0700270# Use equery completion for equery-$board for known boards
271_boardlist=$(cros_list_overlays | egrep "^.*/overlays/overlay-.*$" |
272 sed -n "s/.*overlay-//p")
273for board in $_boardlist; do
274 complete -o bashdefault -o default -F _complete_equery equery-$board
275done
276
Darin Petkov049dbab2010-02-23 18:16:37 -0800277### Local Variables:
278### mode: shell-script
279### End: