blob: 5b74b1f131bbd48803e2f65b025d9cbc469f8c9a [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
76# Completion for --board= argument for existing board sysroots
77#
78_board_sysroot()
79{
Darin Petkovd6bf1012010-02-26 13:10:11 -080080 _flag_complete && return 0
81
Darin Petkov049dbab2010-02-23 18:16:37 -080082 COMPREPLY=()
83 local arg=$(_argeq --board)
84 if [[ ${arg} == --board=* ]]; then
85 COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) )
Darin Petkov049dbab2010-02-23 18:16:37 -080086 fi
87}
88
89complete -o bashdefault -o default -F _board_sysroot \
90 build_autotest.sh \
91 build_image \
92 build_packages \
Darin Petkov726d3662010-02-24 12:05:18 -080093 image_to_usb.sh \
94 mod_image_for_test.sh
Darin Petkov049dbab2010-02-23 18:16:37 -080095
96
97# echo the existing target board overlays
98#
99_board_overlays()
100{
101 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
108#
109_board_overlay()
110{
Darin Petkovd6bf1012010-02-26 13:10:11 -0800111 _flag_complete && return 0
112
Darin Petkov049dbab2010-02-23 18:16:37 -0800113 COMPREPLY=()
114 local arg=$(_argeq --board)
115 if [[ ${arg} == --board=* ]]; then
116 COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) )
Darin Petkov049dbab2010-02-23 18:16:37 -0800117 fi
118}
119
120complete -o bashdefault -o default -F _board_overlay setup_board
121
122
123### Local Variables:
124### mode: shell-script
125### End: