Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 1 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 7 | |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 8 | # Echo a list of -- flags that the current command accepts. The |
| 9 | # function assumes that the command supports shflags' --help flag. |
| 10 | # |
| 11 | _flags() |
| 12 | { |
| 13 | echo $(command "${COMP_WORDS[0]}" --help 2>&1 | \ |
| 14 | egrep -o -- --\[^\ \]+: | \ |
| 15 | sed 's/://; s/--\[no\]\(.\+\)/--\1 --no\1/') |
| 16 | } |
| 17 | |
| 18 | |
| 19 | # Complete flags, i.e., current words starting with --. Return 1 if |
| 20 | # the current word doesn't start with --, 0 otherwise. |
| 21 | # |
| 22 | _flag_complete() |
| 23 | { |
| 24 | COMPREPLY=() |
| 25 | local cur="${COMP_WORDS[COMP_CWORD]}" |
| 26 | if [[ "${cur}" == --* ]]; then |
| 27 | COMPREPLY=( $(compgen -W "$(_flags)" -- ${cur}) ) |
| 28 | return 0 |
| 29 | fi |
| 30 | return 1 |
| 31 | } |
| 32 | |
| 33 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 34 | # Look for "--arg=foo" or "--arg foo" (where foo can be an empty string) in the |
| 35 | # word to be completed. If found, echo "--arg=foo". |
| 36 | # |
| 37 | _argeq() |
| 38 | { |
| 39 | local arg=$1 |
| 40 | local w0="${COMP_WORDS[COMP_CWORD]}" |
| 41 | local w1="${COMP_WORDS[COMP_CWORD-1]}" |
| 42 | |
| 43 | # Check for completing "--arg=" |
| 44 | if [ "${w1}" == ${arg} -a "${w0}" == "=" ]; then |
| 45 | echo "${w1}${w0}" |
| 46 | return 0 |
| 47 | fi |
| 48 | |
| 49 | # Check for completing "--arg foo" |
| 50 | if [ "${w1}" == ${arg} ]; then |
| 51 | echo "${w1}=${w0}" |
| 52 | return 0 |
| 53 | fi |
| 54 | |
| 55 | # Check for completing "--arg=foo" |
| 56 | if [ ${COMP_CWORD} -gt 2 ]; then |
| 57 | local w2="${COMP_WORDS[COMP_CWORD-2]}" |
| 58 | if [ "${w2}" == ${arg} -a "${w1}" == "=" ]; then |
| 59 | echo "${w2}${w1}${w0}" |
| 60 | return 0 |
| 61 | fi |
| 62 | fi |
| 63 | } |
| 64 | |
| 65 | |
| 66 | # echo the existing target board sysroots |
| 67 | # |
| 68 | _board_sysroots() |
| 69 | { |
| 70 | local builddir=/build |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 71 | if [ -d ${builddir} ]; then |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 72 | echo $(command ls "${builddir}") |
| 73 | fi |
| 74 | } |
| 75 | |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame^] | 76 | _complete_board_sysroot_flag() |
| 77 | { |
| 78 | COMPREPLY=() |
| 79 | local arg=$(_argeq --board) |
| 80 | if [[ ${arg} == --board=* ]]; then |
| 81 | COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) ) |
| 82 | return 0 |
| 83 | fi |
| 84 | return 1 |
| 85 | } |
| 86 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 87 | # Completion for --board= argument for existing board sysroots |
| 88 | # |
| 89 | _board_sysroot() |
| 90 | { |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 91 | _flag_complete && return 0 |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame^] | 92 | _complete_board_flag && return 0 |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | complete -o bashdefault -o default -F _board_sysroot \ |
| 96 | build_autotest.sh \ |
| 97 | build_image \ |
| 98 | build_packages \ |
Darin Petkov | 726d366 | 2010-02-24 12:05:18 -0800 | [diff] [blame] | 99 | image_to_usb.sh \ |
| 100 | mod_image_for_test.sh |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 101 | |
| 102 | |
| 103 | # echo the existing target board overlays |
| 104 | # |
| 105 | _board_overlays() |
| 106 | { |
| 107 | local overlaydir=../overlays |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 108 | if [ -d ${overlaydir} ]; then |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 109 | echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,) |
| 110 | fi |
| 111 | } |
| 112 | |
| 113 | # Completion for --board= argument for existing board overlays |
| 114 | # |
| 115 | _board_overlay() |
| 116 | { |
Darin Petkov | d6bf101 | 2010-02-26 13:10:11 -0800 | [diff] [blame] | 117 | _flag_complete && return 0 |
| 118 | |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 119 | COMPREPLY=() |
| 120 | local arg=$(_argeq --board) |
| 121 | if [[ ${arg} == --board=* ]]; then |
| 122 | COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) ) |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 123 | fi |
| 124 | } |
| 125 | |
| 126 | complete -o bashdefault -o default -F _board_overlay setup_board |
| 127 | |
Eric Li | 3d13cd1 | 2010-03-31 11:47:22 -0700 | [diff] [blame^] | 128 | # Completion for -c and -s argument for autotest script |
| 129 | _ls_autotest() { |
| 130 | local autotest_dir=../third_party/autotest/files |
| 131 | ls --color=never -dBFH ${autotest_dir}/$1* 2>/dev/null |\ |
| 132 | sed s/"..\/third_party\/autotest\/files\/"//g |
| 133 | } |
| 134 | |
| 135 | _autotest_complete() |
| 136 | { |
| 137 | _flag_complete && return 0 |
| 138 | |
| 139 | local arg=$(_argeq -c) |
| 140 | if [[ ${arg} == -c=* ]]; then |
| 141 | COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-c=})")) |
| 142 | return 0 |
| 143 | fi |
| 144 | |
| 145 | arg=$(_argeq -s) |
| 146 | if [[ ${arg} == -s=* ]]; then |
| 147 | COMPREPLY=($(compgen -W "$(_ls_autotest ${arg#-s=})")) |
| 148 | return 0 |
| 149 | fi |
| 150 | |
| 151 | _complete_board_sysroot_flag && return 0 |
| 152 | } |
| 153 | |
| 154 | complete -o bashdefault -o default -o nospace -F _autotest_complete autotest |
Darin Petkov | 049dbab | 2010-02-23 18:16:37 -0800 | [diff] [blame] | 155 | |
| 156 | ### Local Variables: |
| 157 | ### mode: shell-script |
| 158 | ### End: |