OLD | NEW |
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # Add programmable completion to some Chromium OS build scripts | 5 # Add programmable completion to some Chromium OS build scripts |
6 | 6 |
7 | 7 |
| 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 |
8 # Look for "--arg=foo" or "--arg foo" (where foo can be an empty string) in the | 34 # Look for "--arg=foo" or "--arg foo" (where foo can be an empty string) in the |
9 # word to be completed. If found, echo "--arg=foo". | 35 # word to be completed. If found, echo "--arg=foo". |
10 # | 36 # |
11 _argeq() | 37 _argeq() |
12 { | 38 { |
13 local arg=$1 | 39 local arg=$1 |
14 local w0="${COMP_WORDS[COMP_CWORD]}" | 40 local w0="${COMP_WORDS[COMP_CWORD]}" |
15 local w1="${COMP_WORDS[COMP_CWORD-1]}" | 41 local w1="${COMP_WORDS[COMP_CWORD-1]}" |
16 | 42 |
17 # Check for completing "--arg=" | 43 # Check for completing "--arg=" |
(...skipping 17 matching lines...) Expand all Loading... |
35 fi | 61 fi |
36 fi | 62 fi |
37 } | 63 } |
38 | 64 |
39 | 65 |
40 # echo the existing target board sysroots | 66 # echo the existing target board sysroots |
41 # | 67 # |
42 _board_sysroots() | 68 _board_sysroots() |
43 { | 69 { |
44 local builddir=/build | 70 local builddir=/build |
45 if [ ! -d ${builddir} ]; then | 71 if [ -d ${builddir} ]; then |
46 echo "" | |
47 else | |
48 echo $(command ls "${builddir}") | 72 echo $(command ls "${builddir}") |
49 fi | 73 fi |
50 } | 74 } |
51 | 75 |
52 # Completion for --board= argument for existing board sysroots | 76 # Completion for --board= argument for existing board sysroots |
53 # | 77 # |
54 _board_sysroot() | 78 _board_sysroot() |
55 { | 79 { |
| 80 _flag_complete && return 0 |
| 81 |
56 COMPREPLY=() | 82 COMPREPLY=() |
57 local arg=$(_argeq --board) | 83 local arg=$(_argeq --board) |
58 if [[ ${arg} == --board=* ]]; then | 84 if [[ ${arg} == --board=* ]]; then |
59 COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) ) | 85 COMPREPLY=( $(compgen -W "$(_board_sysroots)" -- ${arg#--board=}) ) |
60 return 0 | |
61 fi | 86 fi |
62 } | 87 } |
63 | 88 |
64 complete -o bashdefault -o default -F _board_sysroot \ | 89 complete -o bashdefault -o default -F _board_sysroot \ |
65 build_autotest.sh \ | 90 build_autotest.sh \ |
66 build_image \ | 91 build_image \ |
67 build_packages \ | 92 build_packages \ |
68 image_to_usb.sh \ | 93 image_to_usb.sh \ |
69 mod_image_for_test.sh | 94 mod_image_for_test.sh |
70 | 95 |
71 | 96 |
72 # echo the existing target board overlays | 97 # echo the existing target board overlays |
73 # | 98 # |
74 _board_overlays() | 99 _board_overlays() |
75 { | 100 { |
76 local overlaydir=../overlays | 101 local overlaydir=../overlays |
77 if [ ! -d ${overlaydir} ]; then | 102 if [ -d ${overlaydir} ]; then |
78 echo "" | |
79 else | |
80 echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,) | 103 echo $(command ls $overlaydir | grep overlay- | sed s,overlay-,,) |
81 fi | 104 fi |
82 } | 105 } |
83 | 106 |
84 # Completion for --board= argument for existing board overlays | 107 # Completion for --board= argument for existing board overlays |
85 # | 108 # |
86 _board_overlay() | 109 _board_overlay() |
87 { | 110 { |
| 111 _flag_complete && return 0 |
| 112 |
88 COMPREPLY=() | 113 COMPREPLY=() |
89 local arg=$(_argeq --board) | 114 local arg=$(_argeq --board) |
90 if [[ ${arg} == --board=* ]]; then | 115 if [[ ${arg} == --board=* ]]; then |
91 COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) ) | 116 COMPREPLY=( $(compgen -W "$(_board_overlays)" -- ${arg#--board=}) ) |
92 return 0 | |
93 fi | 117 fi |
94 } | 118 } |
95 | 119 |
96 complete -o bashdefault -o default -F _board_overlay setup_board | 120 complete -o bashdefault -o default -F _board_overlay setup_board |
97 | 121 |
98 | 122 |
99 ### Local Variables: | 123 ### Local Variables: |
100 ### mode: shell-script | 124 ### mode: shell-script |
101 ### End: | 125 ### End: |
OLD | NEW |