Chris Sosa | c1bd111 | 2011-08-26 15:22:10 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 2 | # 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 Hesling | 8c3e5f1 | 2019-10-31 10:34:08 -0700 | [diff] [blame] | 7 | # Declare one cache for all bash complete operations to |
| 8 | # allow it to be easiley cleared if need be. |
| 9 | # > unset _comp_cache |
| 10 | declare -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 Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 24 | |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 25 | # Echo a list of -- flags that the current command accepts. The |
| 26 | # function assumes that the command supports shflags' --help flag. |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 27 | _flags() { |
| 28 | echo $(command "${COMP_WORDS[0]}" --help 2>&1 \ |
Craig Hesling | f241ea9 | 2019-10-31 10:18:58 -0700 | [diff] [blame] | 29 | | egrep -o -- '--[[:alnum:]=_-]+' \ |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 30 | | sed 's/://; s/--\[no\]\(.\+\)/--\1 --no\1/') |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 33 | # Complete flags, i.e., current words starting with --. Return 1 if |
| 34 | # the current word doesn't start with --, 0 otherwise. |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 35 | _flag_complete() { |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 36 | 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 Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 45 | # 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 47 | _argeq() { |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 48 | 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 76 | _board_sysroots() { |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 77 | local builddir=/build |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 78 | if [ -d ${builddir} ]; then |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 79 | echo $(command ls "${builddir}") |
| 80 | fi |
| 81 | } |
| 82 | |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 83 | _complete_board_sysroot_flag() { |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 84 | 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 Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 93 | # Completion for --board= argument for existing board sysroots |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 94 | _board_sysroot() { |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 95 | _flag_complete && return 0 |
Eric Li | cad2b49 | 2010-04-01 13:07:10 -0700 | [diff] [blame] | 96 | _complete_board_sysroot_flag && return 0 |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 99 | # echo the existing target board overlays |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 100 | _board_overlays() { |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 101 | local overlaydir=../overlays |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 102 | if [ -d ${overlaydir} ]; then |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 103 | echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,) |
| 104 | fi |
| 105 | } |
| 106 | |
| 107 | # Completion for --board= argument for existing board overlays |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 108 | _board_overlay() { |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 109 | _flag_complete && return 0 |
| 110 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 111 | COMPREPLY=() |
| 112 | local arg=$(_argeq --board) |
| 113 | if [[ ${arg} == --board=* ]]; then |
| 114 | COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) ) |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 115 | fi |
| 116 | } |
| 117 | |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 118 | # Completion for -c and -s argument for autotest script |
| 119 | _ls_autotest() { |
| 120 | local autotest_dir=../third_party/autotest/files |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 121 | ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null \ |
| 122 | | sed s/"..\/third_party\/autotest\/files\/"//g |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 125 | _autotest_complete() { |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 126 | _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 Petkov | 5ad2cb5 | 2010-08-18 14:01:56 -0700 | [diff] [blame] | 143 | # Complete cros_workon's <command> argument. |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 144 | # |
| 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 Baines | 93da99b | 2011-02-05 11:42:19 -0800 | [diff] [blame] | 151 | # "cros_workon --all list". |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 152 | _complete_cros_workon_command() { |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 153 | [ ${COMP_CWORD} -eq 1 ] || return 1 |
| 154 | local command="${COMP_WORDS[1]}" |
Craig Hesling | 1f5f5a3 | 2019-10-31 13:19:45 -0700 | [diff] [blame] | 155 | |
| 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 167 | return 0 |
| 168 | } |
| 169 | |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 170 | # 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 Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 180 | # 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 Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 184 | local cros_workon=$(_cros_workon_executable) |
Craig Hesling | 33b6021 | 2019-11-01 14:16:49 -0700 | [diff] [blame] | 185 | ${cros_workon} $1 list | sed 's,\(.\+\)/\(.\+\),\1/\2 \2,' |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | # Completes the current cros_workon argument assuming it's a |
| 189 | # package/ebuild name. |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 190 | _complete_cros_workon_package() { |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 191 | [ ${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 |
Craig Hesling | 6db16b5 | 2019-11-01 14:17:43 -0700 | [diff] [blame^] | 196 | local key="pkgs/all" |
| 197 | if [[ -z "${_comp_cache[${key}]}" ]]; then |
| 198 | _comp_cache[${key}]="$(_cros_workon_list --all)" |
| 199 | fi |
| 200 | COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${package}")) |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 201 | return 0 |
| 202 | fi |
| 203 | # If "stop" or "iterate", complete based on all live packages. |
| 204 | if [[ ${command} == "stop" ]] || [[ ${command} == "iterate" ]]; then |
| 205 | COMPREPLY=($(compgen -W "$(_cros_workon_list)" -- "$package")) |
| 206 | return 0 |
| 207 | fi |
| 208 | return 1 |
| 209 | } |
| 210 | |
Darin Petkov | 5ad2cb5 | 2010-08-18 14:01:56 -0700 | [diff] [blame] | 211 | # Complete cros_workon arguments. |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 212 | _cros_workon() { |
| 213 | COMPREPLY=() |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 214 | _flag_complete && return 0 |
| 215 | _complete_board_sysroot_flag && return 0 |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 216 | _complete_cros_workon_command && return 0 |
| 217 | _complete_cros_workon_package && return 0 |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 218 | return 0 |
| 219 | } |
| 220 | |
Craig Hesling | 8c3e5f1 | 2019-10-31 10:34:08 -0700 | [diff] [blame] | 221 | _complete_cros_command() { |
| 222 | local command="${COMP_WORDS[COMP_CWORD]}" |
| 223 | if [ ${COMP_CWORD} -ne 1 ]; then |
| 224 | COMPREPLY=($(compgen -o default -- "${command}")) |
| 225 | return 0 |
| 226 | fi |
| 227 | |
| 228 | local key="subcmds/cros" |
| 229 | if [[ -z "${_comp_cache[${key}]}" ]]; then |
| 230 | _comp_cache[${key}]="$(command cros --help | _subcmds_from_help)" |
| 231 | fi |
| 232 | COMPREPLY=($(compgen -W "${_comp_cache[${key}]}" -- "${command}")) |
| 233 | return 0 |
| 234 | } |
| 235 | |
| 236 | # Complete cros arguments. |
| 237 | _cros() { |
| 238 | COMPREPLY=() |
| 239 | _flag_complete && return 0 |
| 240 | _complete_board_sysroot_flag && return 0 |
| 241 | _complete_cros_command && return 0 |
| 242 | # TODO(hesling): Add package completion like cros_workon. |
| 243 | return 0 |
| 244 | } |
| 245 | |
Aviv Keshet | b08a89a | 2013-03-12 12:27:49 -0700 | [diff] [blame] | 246 | # Complete equery's <module-name> argument. |
| 247 | _complete_equery_module_name() { |
| 248 | [ ${COMP_CWORD} -eq 1 ] || return 1 |
| 249 | local command="${COMP_WORDS[1]}" |
| 250 | COMPREPLY=($(compgen -W "belongs changes check depends depgraph files has \ |
| 251 | hasuse keywords list meta size uses which" \ |
| 252 | -- "$command")) |
| 253 | return 0 |
| 254 | } |
| 255 | |
| 256 | # Complete equery arguments. |
| 257 | _complete_equery() { |
| 258 | COMPREPLY=() |
| 259 | _complete_equery_module_name && return 0 |
| 260 | return 0 |
| 261 | } |
| 262 | |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 263 | complete -o bashdefault -o default -F _board_sysroot \ |
| 264 | build_autotest.sh \ |
| 265 | build_image \ |
| 266 | build_packages \ |
Darin Petkov | 593e796 | 2010-08-13 14:45:02 -0700 | [diff] [blame] | 267 | mod_image_for_test.sh |
| 268 | complete -o bashdefault -o default -F _board_overlay setup_board |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame] | 269 | complete -o bashdefault -o default -o nospace -F _autotest_complete autotest |
Craig Hesling | 8c3e5f1 | 2019-10-31 10:34:08 -0700 | [diff] [blame] | 270 | complete -F _cros cros |
Darin Petkov | 6944c5d | 2010-08-18 09:57:34 -0700 | [diff] [blame] | 271 | complete -F _cros_workon cros_workon |
Aviv Keshet | 7af8fe3 | 2013-03-14 16:03:31 -0700 | [diff] [blame] | 272 | complete -o bashdefault -o default -F _complete_equery equery |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 273 | |
Aviv Keshet | 7e743ed | 2013-03-14 18:37:54 -0700 | [diff] [blame] | 274 | # Use equery completion for equery-$board for known boards |
| 275 | _boardlist=$(cros_list_overlays | egrep "^.*/overlays/overlay-.*$" | |
| 276 | sed -n "s/.*overlay-//p") |
| 277 | for board in $_boardlist; do |
| 278 | complete -o bashdefault -o default -F _complete_equery equery-$board |
| 279 | done |
| 280 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 281 | ### Local Variables: |
| 282 | ### mode: shell-script |
| 283 | ### End: |