3 # A bash command completion script for ovs-appctl.
6 # Right now, the script can do the following:
8 # - display available completion or complete on unfinished user input
9 # (long option, subcommand, and argument).
11 # - once the subcommand (e.g. ofproto/trace) has been given, the
12 # script will print the subcommand format.
14 # - the script can convert between keywords like 'bridge/port/interface/dp'
15 # and the available record in ovsdb.
19 # - only support small set of important keywords
20 # (dp, datapath, bridge, switch, port, interface, iface).
22 # - does not support parsing of nested option
23 # (e.g. ovsdb-tool create [db [schema]]).
25 # - does not support expansion on repeatitive argument
26 # (e.g. ovs-dpctl show [dp...]).
28 # - only support matching on long options, and only in the format
29 # (--option [arg], i.e. should not use --option=[arg]).
38 # Expandable keywords.
39 _KWORDS
=(bridge switch port interface iface dp_name dp
)
46 # Output to the compgen.
50 # For ovs-appctl command only.
52 # Target in the current completion, default ovs-vswitchd.
55 _POSSIBLE_TARGETS
="ovs-vswitchd ovsdb-server ovs-ofctl"
62 # Extracts all subcommands of 'command'.
63 # If fails, returns nothing.
65 local command=$_COMMAND
69 if [ -n "$_APPCTL_TARGET" ]; then
70 target
="--target $_APPCTL_TARGET"
73 subcmds
="$($command $target list-commands 2>/dev/null | tail -n +2 | cut -c3- \
74 | cut -d ' ' -f1)" || error
="TRUE"
76 if [ -z "$error" ]; then
81 # Extracts all long options of ovs-appctl.
82 # If fails, returns nothing.
84 local command=$_COMMAND
87 options
="$($command --option 2>/dev/null | sort | sed -n '/^--.*/p' | cut -d '=' -f1)" \
90 if [ -z "$error" ]; then
95 # Returns the option format, if the option asks for an argument.
96 # If fails, returns nothing.
97 option_require_arg
() {
98 local command=$_COMMAND
100 local require_arg error
102 require_arg
="$($command --option | sort | sed -n '/^--.*/p' | grep -- "$option" | grep -- "=")" \
105 if [ -z "$error" ]; then
110 # Combination Discovery
111 # =====================
115 # Given the subcommand formats, finds all possible completions
116 # at current completion level.
117 find_possible_comps
() {
126 # If it is an optional argument, gets all completions,
128 if [ -n "$(sed -n '/^\[.*\]$/p' <<< "$arg")" ]; then
129 local opt_arg
="$(sed -e 's/^\[\(.*\)\]$/\1/' <<< "$arg")"
132 IFS
='|' read -a opt_args
<<< "$opt_arg"
133 comps
="${opt_args[@]} $comps"
134 # If it is in format "\[*", it is a start of nested
135 # option, do not parse.
136 elif [ -n "$(sed -n "/^\
[.
*$
/p
" <<< "$arg")" ]; then
138 # If it is a compulsory argument, adds it to the comps
139 # and break, since all following args are for next stage.
143 IFS
='|' read -a args
<<< "$arg"
144 comps
="${args[@]} $comps"
153 # Given the subcommand format, and the current command line input,
154 # finds keywords of all possible completions.
155 subcmd_find_keyword_based_on_input
() {
163 # finds all combinations by searching for '{}'.
164 # there should only be one '{}', otherwise, the
165 # command format should be changed to multiple commands.
166 mult
="$(sed -n 's/^.*{\(.*\)}.*$/ \1/p' <<< "$format" | tr '|' '\n' | cut -c1-)"
167 if [ -n "$mult" ]; then
171 tmp
="$(sed -e "s@
{\
(.
*\
)}@
$line@
" <<< "$format")"
174 combs
="$(tr '@' '\n' <<< "$combs")"
179 # Now, starts from the first argument, narrows down the
180 # subcommand format combinations.
181 for arg
in "${subcmd_line[@]}"; do
182 local kword possible_comps
184 # Finds next level possible comps.
185 possible_comps
=$
(find_possible_comps
"$combs")
187 kword
="$(arg_to_kwords "$arg" "$possible_comps")"
188 # Returns if could not find 'kword'
189 if [ -z "$kword" ]; then
192 # Trims the 'combs', keeps context only after 'kword'.
193 if [ -n "$combs" ]; then
194 combs
="$(sed -n "s@^.
*\
[\
{0,1\
}$kword|\
{0,1\
}[a-z_
]*\
]\
{0,1\
} @@p
" <<< "$combs")"
197 comps
="$(find_possible_comps "$combs")"
209 # Prints the input to stderr. $_PRINTF_ENABLE must be filled.
211 local stderr_out
="$@"
213 if [ -n "$_PRINTF_ENABLE" ]; then
214 printf "\n$stderr_out" 1>&2
218 # Extracts the bash prompt PS1, outputs it with the input argument
219 # via 'printf_stderr'.
221 # Original idea inspired by:
222 # http://stackoverflow.com/questions/10060500/bash-how-to-evaluate-ps1-ps2
224 # The code below is taken from Peter Amidon. His change makes it more
226 extract_bash_prompt
() {
229 myPS1
="$(sed 's/Begin prompt/\\Begin prompt/; s/End prompt/\\End prompt/' <<< "$PS1")"
230 v
="$(bash --norc --noprofile -i 2>&1 <<< $'PS1=\"'"$myPS1"$'\" \n# Begin prompt\n# End prompt')"
231 v
="${v##*# Begin prompt}"
232 _BASH_PROMPT
="$(tail -n +2 <<< "${v%# End prompt*}" | sed 's/\\Begin prompt/Begin prompt/; s/\\End prompt/End prompt/')"
242 # All completion functions.
246 result
=$
(ovs-vsctl list-br
2>/dev
/null |
grep -- "^$1") || error
="TRUE"
248 if [ -z "$error" ]; then
254 local ports result error
257 all_ports
=$
(ovs-vsctl
--format=table \
260 list Port
2>/dev
/null
) || error
="TRUE"
261 ports
=$
(printf "$all_ports" |
sort |
tr -d '"' |
uniq -u)
262 result
=$
(grep -- "^$1" <<< "$ports")
264 if [ -z "$error" ]; then
270 local bridge bridges result error
272 bridges
=$
(ovs-vsctl list-br
2>/dev
/null
) || error
="TRUE"
273 for bridge
in $bridges; do
276 ifaces
=$
(ovs-vsctl list-ifaces
"${bridge}" 2>/dev
/null
) || error
="TRUE"
277 result
="${result} ${ifaces}"
280 if [ -z "$error" ]; then
286 local dps result error
288 dps
=$
(ovs-appctl dpctl
/dump-dps
2>/dev
/null | cut
-d '@' -f2) || error
="TRUE"
289 result
=$
(grep -- "^$1" <<< "$dps")
291 if [ -z "$error" ]; then
296 # Converts the argument (e.g. bridge/port/interface/dp name) to
297 # the corresponding keywords.
298 # Returns empty string if could not map the arg to any keyword.
301 local possible_kwords
=($2)
302 local non_parsables
=()
306 for kword
in ${possible_kwords[@]}; do
309 match
="$(complete_bridge "$arg")"
312 match
="$(complete_port "$arg")"
315 match
="$(complete_iface "$arg")"
318 match
="$(complete_dp "$arg")"
321 if [ "$arg" = "$kword" ]; then
324 non_parsables
+=("$kword")
330 if [ -n "$match" ]; then
336 # If there is only one non-parsable kword,
337 # just assumes the user input it.
338 if [ "${#non_parsables[@]}" -eq "1" ]; then
339 echo "$non_parsables"
344 # Expands the keywords to the corresponding instance names.
346 local possible_kwords
=($@
)
348 local printf_expand_once
=
351 for kword
in ${possible_kwords[@]}; do
356 match
="$(complete_bridge "")"
359 match
="$(complete_port "")"
362 match
="$(complete_iface "")"
365 match
="$(complete_dp "")"
368 # Treats option as kword as well.
375 match
=$
(echo "$match" |
tr '\n' ' ' |
tr -s ' ' |
sed -e 's/^[ \t]*//')
377 if [ -n "$_PRINTF_ENABLE" ]; then
380 if [ -z "$printf_expand_once" ]; then
381 printf_expand_once
="once"
382 printf -v output_stderr
"\nArgument expansion:\n"
384 printf -v output_stderr
"$output_stderr available completions \
385 for keyword \"%s\": %s " "$kword" "$match"
387 printf_stderr
"$output_stderr"
402 # This function takes the current command line arguments as input,
403 # finds the command format and returns the possible completions.
404 parse_and_compgen
() {
405 local command=$_COMMAND
406 local subcmd_line
=($@
)
407 local subcmd
=${subcmd_line[0]}
413 if [ -n "$_APPCTL_TARGET" ]; then
414 target
="--target $_APPCTL_TARGET"
417 # Extracts the subcommand format.
418 subcmd_format
="$($command $target list-commands 2>/dev/null | tail -n +2 | cut -c3- \
419 | awk -v opt=$subcmd '$1 == opt {print $0}' | tr -s ' ' )"
421 # Finds the possible completions based on input argument.
422 comp_keyword
="$(subcmd_find_keyword_based_on_input "$subcmd_format" \
423 "${subcmd_line[@]}")"
425 # Prints subcommand format and expands the keywords if 'comp_keyword'
427 if [ -n "$comp_keyword" ]; then
428 printf_stderr
"$(printf "\nCommand format
:\n%s
" "$subcmd_format")"
429 comp_wordlist
="$(kwords_to_args "$comp_keyword")"
430 # If there is no expanded completions, returns "NO_EXPAN" to
431 # distinguish from the case of no available completions.
432 if [ -z "$comp_wordlist" ]; then
435 echo "$comp_wordlist"
447 # Takes the current command line arguments and returns the possible
450 # At the beginning, the options are checked and completed. For ovs-appctl
451 # completion, The function looks for the --target option which gives the
452 # target daemon name. If it is not provided, by default, 'ovs-vswitchd'
455 # Then, tries to locate and complete the subcommand. If the subcommand
456 # is provided, the following arguments are passed to the 'parse_and_compgen'
457 # function to figure out the corresponding completion of the subcommand.
459 # Returns the completion arguments on success.
461 local cmd_line_so_far
=($@
)
462 local comp_wordlist _subcmd options i
465 # Parse the command-line args till we find the subcommand.
466 for i
in "${!cmd_line_so_far[@]}"; do
467 # if $i is not greater than $j, it means the previous iteration
468 # skips not-visited args. so, do nothing and catch up.
469 if [ $i -le $j ]; then continue; fi
471 if [[ "${cmd_line_so_far[i]}" =~ ^
--* ]]; then
472 # If --target is found, locate the target daemon.
473 # Else, it is an option command, fill the comp_wordlist with
475 if [ "$_COMMAND" = "ovs-appctl" ] \
476 && [[ "${cmd_line_so_far[i]}" =~ ^
--target$
]]; then
477 _APPCTL_TARGET
="ovs-vswitchd"
479 if [ -n "${cmd_line_so_far[j+1]}" ]; then
482 for daemon
in $_POSSIBLE_TARGETS; do
483 # Greps "$daemon" in argument, since the argument may
484 # be the path to the pid file.
485 if [ "$daemon" = "${cmd_line_so_far[j+1]}" ]; then
486 _APPCTL_TARGET
="$daemon"
493 comp_wordlist
="$_POSSIBLE_TARGETS"
497 options
="$(extract_options $_COMMAND)"
498 # See if we could find the exact option.
499 if [ "${cmd_line_so_far[i]}" = "$(grep -- "${cmd_line_so_far[i]}" <<< "$options")" ]; then
500 # If an argument is required and next argument is non-empty,
501 # skip it. Else, return directly.
502 if [ -n "$(option_require_arg "${cmd_line_so_far[i]}")" ]; then
504 if [ -z "${cmd_line_so_far[j]}" ]; then
505 printf_stderr
"\nOption requires an arugment."
510 # Else, need to keep completing on option.
512 comp_wordlist
="$options"
517 # Takes the first non-option argument as subcmd.
518 _subcmd
="${cmd_line_so_far[i]}"
522 if [ -z "$comp_wordlist" ]; then
523 # If the subcommand is not found, provides all subcmds and options.
524 if [ -z "$_subcmd" ]; then
525 comp_wordlist
="$(extract_subcmds) $(extract_options)"
526 # Else parses the current arguments and finds the possible completions.
528 # $j stores the index of the subcmd in cmd_line_so_far.
529 comp_wordlist
="$(parse_and_compgen "${cmd_line_so_far[@]:$j}")"
533 echo "$comp_wordlist"
541 # The compgen function.
542 _ovs_command_complete
() {
545 _COMMAND
=${COMP_WORDS} # element 0 is the command.
547 cur
=${COMP_WORDS[COMP_CWORD]}
549 # Do not print anything at first [TAB] execution.
550 if [ "$COMP_TYPE" -eq "9" ]; then
553 _PRINTF_ENABLE
="enabled"
556 # Extracts bash prompt PS1.
557 if [ "$1" != "debug" ]; then
561 # Invokes the helper function to get all available completions.
562 # Always not input the 'COMP_WORD' at 'COMP_CWORD', since it is
563 # the one to be completed.
564 _COMP_WORDLIST
="$(ovs_comp_helper \
565 ${COMP_WORDS[@]:1:COMP_CWORD-1})"
567 # This is a hack to prevent autocompleting when there is only one
568 # available completion and printf disabled.
569 if [ -z "$_PRINTF_ENABLE" ] && [ -n "$_COMP_WORDLIST" ]; then
570 _COMP_WORDLIST
="$_COMP_WORDLIST none void no-op"
573 if [ -n "$_PRINTF_ENABLE" ] && [ -n "$_COMP_WORDLIST" ]; then
574 if [ -n "$(echo $_COMP_WORDLIST | tr ' ' '\n' | sed -e '/NO_EXPAN/d' | grep -- "^
$cur")" ]; then
575 printf_stderr
"\nAvailable completions:\n"
577 if [ "$1" != "debug" ]; then
578 # If there is no match between '$cur' and the '$_COMP_WORDLIST'
579 # prints a bash prompt since the 'complete' will not print it.
580 printf_stderr
"\n$_BASH_PROMPT${COMP_WORDS[@]}"
585 if [ "$1" = "debug" ]; then
586 printf_stderr
"$(echo $_COMP_WORDLIST | tr ' ' '\n' | sort -u | sed -e '/NO_EXPAN/d' | grep -- "$cur")\n"
588 if [ -n "$_COMP_WORDLIST" ]; then
589 COMPREPLY
=( $
(compgen
-W "$(echo $_COMP_WORDLIST | tr ' ' '\n' \
590 | sort -u | sed -e '/NO_EXPAN/d')" -- $cur) )
593 # If there is no completions, just complete on file path.
602 if [ "$1" = "debug" ]; then
606 COMP_CWORD
="$(expr $# - 1)"
608 # If the last argument is TAB, it means that the previous
609 # argument is already complete and script should complete
610 # next argument which is not input yet. This hack is for
611 # compromising the fact that bash cannot take unquoted
613 if [ "${COMP_WORDS[$COMP_CWORD]}" = "TAB" ]; then
614 COMP_WORDS
[$COMP_CWORD]=""
617 _ovs_command_complete
"debug"
618 # Normal compgen mode.
620 complete
-F _ovs_command_complete ovs-appctl
621 complete
-F _ovs_command_complete ovs-ofctl
622 complete
-F _ovs_command_complete ovs-dpctl
623 complete
-F _ovs_command_complete ovsdb-tool