#!/bin/bash # # Software Updater - autocompletion script # # Copyright © 2016 Intel Corporation. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 2 or later of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # This program creates a _swupd function to be used by completion built-in # bash command to complete swup. # It reads options and subcommands from swup --help and then it reads # options for each subcommand recursively. SCRIPTNAME="swupd.bash" COMPLETIONFUNCTION="_swupd" SWUPDCOMMAND="./swupd" optionsfromhelp="" subcommandsfromhelp=() #Checks if swupd commad exists in current shell if [ ! -e $SWUPDCOMMAND ] ; then echo "Error: No swupd found" exit 1 fi currenttype="" nexttype="" #reads "swupd --help" line by line while read word1 word2 rest do #For each line checks in what section is if [[ "$word1 $word2" == "Help Options:" ]]; then currenttype="options" continue fi if [[ "$word1" == "Subcommands:" ]]; then currenttype="subcommand" continue fi if [[ "$word1" == "" ]]; then currenttype="" continue fi case $currenttype in #In case of subcommand it inserts first word of line in an array ("subcommand") subcommandsfromhelp+=("$word1") ;; #For options it reads short and long options, remove the commma from short ("options") optionsfromhelp="${optionsfromhelp} ${word1%,} ${word2}" ;; #For the rest do nothing ("") : ;; esac done < <($SWUPDCOMMAND --help 2>&1) # Construct sed script #Convert all tabs to spaces SEDSCRIPT=$'s/\t/ /g' #get rid of anything that does not start with a - SEDSCRIPT+=';/^ *-/!d' #remove leading spaces SEDSCRIPT+=';s/^ *//' # remove commas, any extra spaces between options SEDSCRIPT+=';s/, *-/ -/' # Strip trailing junk, anything after = or the second word SEDSCRIPT+=';s/=.*//' SEDSCRIPT+=';s/^\([^ ]* [^ ]*\).*/\1/' # See if there is only one option on line SEDSCRIPT+=';/ -/!s/ .*//' #Autocomplete script creation { cat <. # #declares the completion function ${COMPLETIONFUNCTION}() { # \$1 is the command being completed, \$2 is the current word being expanded local opts IFS=\$' \t\n' local -i i installed COMPREPLY=() for ((i=COMP_CWORD-1;i>=0;i--)) do case "\${COMP_WORDS[\$i]}" in EOM #This adds options for all subcommands printf '\t ("$1")\n\t\topts="%s"\n' "$optionsfromhelp ${subcommandsfromhelp[*]}" printf '\t break;;\n' for i in "${subcommandsfromhelp[@]}"; do printf '\t ("%s")\n' "$i" C="$($SWUPDCOMMAND $i --help 2>&1 | sed -e "$SEDSCRIPT" | tr '\n' ' ')" printf '\t\topts="%s"\n' "$C" printf '\t\tbreak;;\n' done # The tail of the script cat >> $SCRIPTNAME << 'EOM' esac done # Add in additional completion options if we need to if (( $i >= 0 )) then case "${COMP_WORDS[$i]}" in ("bundle-add") if [ -r /var/lib/swupd/version ] && installed=$( $SCRIPTNAME