blob: e114e5e4e195041d073a7d75749495da6dade63b [file] [log] [blame]
Darin Petkov049dbab2010-02-23 18:16:37 -08001# 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 Petkovd6bf1012010-02-26 13:10:11 -08008# 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 Petkov049dbab2010-02-23 18:16:37 -080034# 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 Petkovd6bf1012010-02-26 13:10:11 -080071 if [ -d ${builddir} ]; then
Darin Petkov049dbab2010-02-23 18:16:37 -080072 echo $(command ls "${builddir}")
73 fi
74}
75
Eric Li3d13cd12010-03-31 11:47:22 -070076_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 Petkov049dbab2010-02-23 18:16:37 -080087# Completion for --board= argument for existing board sysroots
88#
89_board_sysroot()
90{
Darin Petkovd6bf1012010-02-26 13:10:11 -080091 _flag_complete && return 0
Eric Li3d13cd12010-03-31 11:47:22 -070092 _complete_board_flag && return 0
Darin Petkov049dbab2010-02-23 18:16:37 -080093}
94
95complete -o bashdefault -o default -F _board_sysroot \
96 build_autotest.sh \
97 build_image \
98 build_packages \
Darin Petkov726d3662010-02-24 12:05:18 -080099 image_to_usb.sh \
100 mod_image_for_test.sh
Darin Petkov049dbab2010-02-23 18:16:37 -0800101
102
103# echo the existing target board overlays
104#
105_board_overlays()
106{
107 local overlaydir=../overlays
Darin Petkovd6bf1012010-02-26 13:10:11 -0800108 if [ -d ${overlaydir} ]; then
Darin Petkov049dbab2010-02-23 18:16:37 -0800109 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 Petkovd6bf1012010-02-26 13:10:11 -0800117 _flag_complete && return 0
118
Darin Petkov049dbab2010-02-23 18:16:37 -0800119 COMPREPLY=()
120 local arg=$(_argeq --board)
121 if [[ ${arg} == --board=* ]]; then
122 COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) )
Darin Petkov049dbab2010-02-23 18:16:37 -0800123 fi
124}
125
126complete -o bashdefault -o default -F _board_overlay setup_board
127
Eric Li3d13cd12010-03-31 11:47:22 -0700128# 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
154complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
Darin Petkov049dbab2010-02-23 18:16:37 -0800155
156### Local Variables:
157### mode: shell-script
158### End: