# bash completion for rpm -*- shell-script -*- # helper functions proc _rpm_installed_packages { if [[ -r /var/log/rpmpkgs && \ /var/log/rpmpkgs -nt /var/lib/rpm/Packages ]] { # using RHL 7.2 or later - this is quicker than querying the DB setglobal COMPREPLY = '( '$( compgen -W "$( sed -ne \ 's|^\([^[:space:]]\{1,\}\)-[^[:space:]-]\{1,\}-[^[:space:]-]\{1,\}\.rpm$|\1|p' \ /var/log/rpmpkgs )" -- "$cur" ) ) } elif type rpmqpack &>/dev/null { # SUSE's rpmqpack is faster than rpm -qa setglobal COMPREPLY = '( '$( compgen -W '$( rpmqpack )' -- "$cur" ) ) } else { setglobal COMPREPLY = '( '$( ${1:-rpm} -qa --nodigest --nosignature \ --queryformat='%{NAME} ' "$cur*" 2>/dev/null ) ) } } proc _rpm_groups { var IFS = '$'\n'' setglobal COMPREPLY = '( '$( compgen -W "$( ${1:-rpm} -qa --nodigest --nosignature \ --queryformat='%{GROUP}\n' 2>/dev/null )" -- "$cur" ) ) } proc _rpm_macros { # get a list of macros setglobal COMPREPLY = '( '$( compgen -W "$( ${1:-rpm} --showrc | sed -ne \ 's/^-\{0,1\}[0-9]\{1,\}[:=][[:space:]]\{1,\}\([^[:space:](]\{3,\}\).*/%\1/p' )" \ -- "$cur" ) ) } proc _rpm_buildarchs { setglobal COMPREPLY = '( '$( compgen -W "$( ${1:-rpm} --showrc | sed -ne \ 's/^\s*compatible\s\s*build\s\s*archs\s*:\s*\(.*\)/\1/ p' )" \ -- "$cur" ) ) } # rpm(8) completion # proc _rpm { var cur = '', prev = '', words = '', cword = '', split = '' _init_completion -s || return if [[ $cword -eq 1 ]] { # first parameter on line match $cur { with --* setglobal COMPREPLY = '( '$( compgen -W '--help --version --initdb --checksig --addsign --delsign --rebuilddb --showrc --setperms --setugids --eval --install --upgrade --query --freshen --erase --verify --querytags --import' \ -- "$cur" ) ) with * setglobal COMPREPLY = '( '$( compgen -W '-e -E -F -i -q -t -U -V' \ -- "$cur" ) ) } return 0 } match $prev { with --dbpath|--excludepath|--prefix|--relocate|--root|-r _filedir -d return 0 with --eval|-E _rpm_macros $1 return 0 with --pipe compopt -o filenames setglobal COMPREPLY = '( '$( compgen -c -- "$cur" ) ) return 0 with --rcfile _filedir return 0 with --specfile # complete on .spec files _filedir spec return 0 with --whatenchances|--whatprovides|--whatrecommends|--whatrequires|\ --whatsuggests|--whatsupplements if [[ "$cur" == */* ]] { _filedir } else { # complete on capabilities var IFS = '$'\n',' fmt = '' match $prev { with *enhances set fmt = 'ENHANCENAME' with *provides set fmt = 'PROVIDENAME' with *recommends set fmt = 'RECOMMENDNAME' with *requires set fmt = 'REQUIRENAME' with *suggests set fmt = 'SUGGESTNAME' with *supplements set fmt = 'SUPPLEMENTNAME' } setglobal COMPREPLY = '( '$( compgen -W "$( $1 -qa --nodigest --nosignature \ --queryformat=\"%{$fmt}\\n\" 2>/dev/null | command grep -vF '(none)' )" -- "$cur" ) ) } return 0 with --define|-D|--fileid|--hdrid|--pkgid # argument required but no completions available return 0 } $split && return 0 # options common to all modes var opts = '"--define= --eval= --macros= --nodigest --nosignature --rcfile= --quiet --pipe --verbose'" match $(words[1]) { with -[iFU]*|--install|--freshen|--upgrade if [[ "$cur" == -* ]] { setglobal COMPREPLY = '( '$( compgen -W "$opts --percent --force --test --replacepkgs --replacefiles --root --excludedocs --includedocs --noscripts --ignorearch --dbpath --prefix= --ignoreos --nodeps --allfiles --ftpproxy --ftpport --justdb --httpproxy --httpport --noorder --relocate= --badreloc --notriggers --excludepath= --ignoresize --oldpackage --queryformat --repackage --nosuggests" -- "$cur" ) ) } else { _filedir '[rs]pm' } with -e|--erase if [[ "$cur" == -* ]] { setglobal COMPREPLY = '( '$( compgen -W "$opts --allmatches --noscripts --notriggers --nodeps --test --repackage" -- "$cur" ) ) } else { _rpm_installed_packages $1 } with -q*|--query # options common to all query types set opts = '" --changelog --configfiles --conflicts --docfiles --dump --enhances --filesbypkg --filecaps --fileclass --filecolor --fileprovide --filerequire --filesbypkg --info --list --obsoletes --pipe --provides --queryformat= --requires --scripts --suggests --triggers --xml --recommends --supplements'" if [[ ${words[@]} == *\ -@(*([^ -])f|-file )* ]] { # -qf completion if [[ "$cur" == -* ]] { setglobal COMPREPLY = '( '$( compgen -W "$opts --dbpath --fscontext --last --root --state" -- "$cur" ) ) } else { _filedir } } elif [[ ${words[@]} == *\ -@(*([^ -])g|-group )* ]] { # -qg completion _rpm_groups $1 } elif [[ ${words[@]} == *\ -@(*([^ -])p|-package )* ]] { # -qp; uninstalled package completion if [[ "$cur" == -* ]] { setglobal COMPREPLY = '( '$( compgen -W "$opts --ftpport --ftpproxy --httpport --httpproxy --nomanifest" -- "$cur" ) ) } else { _filedir '[rs]pm' } } else { # -q; installed package completion if [[ "$cur" == -* ]] { setglobal COMPREPLY = '( '$( compgen -W "$opts --all --file --fileid --dbpath --fscontext --ftswalk --group --hdrid --last --package --pkgid --root= --specfile --state --triggeredby --whatenhances --whatprovides --whatrecommends --whatrequires --whatsuggests --whatsupplements" \ -- "$cur" ) ) } elif [[ ${words[@]} != *\ -@(*([^ -])a|-all )* ]] { _rpm_installed_packages $1 } } with -K*|--checksig if [[ "$cur" == -* ]] { setglobal COMPREPLY = '( '$( compgen -W "$opts --nopgp --nogpg --nomd5" \ -- "$cur" ) ) } else { _filedir '[rs]pm' } with -[Vy]*|--verify if [[ "$cur" == -* ]] { setglobal COMPREPLY = '( '$( compgen -W "$opts --root= --dbpath --nodeps --nogroup --nolinkto --nomode --nomtime --nordev --nouser --nofiles --noscripts --nomd5 --querytags --specfile --whatenhances --whatprovides --whatrecommends --whatrequires --whatsuggests --whatsupplements" \ -- "$cur" ) ) # check whether we're doing file completion } elif [[ ${words[@]} == *\ -@(*([^ -])f|-file )* ]] { _filedir } elif [[ ${words[@]} == *\ -@(*([^ -])g|-group )* ]] { _rpm_groups $1 } elif [[ ${words[@]} == *\ -@(*([^ -])p|-package )* ]] { _filedir '[rs]pm' } else { _rpm_installed_packages $1 } with --resign|--addsign|--delsign _filedir '[rs]pm' with --setperms|--setgids _rpm_installed_packages $1 with --import|--dbpath|--root if [[ "$cur" == -* ]] { setglobal COMPREPLY = '( '$( compgen -W '--import --dbpath --root=' \ -- "$cur" ) ) } else { _filedir } } [[ $COMPREPLY == *= ]] && compopt -o nospace return 0 } && complete -F _rpm rpm proc _rpmbuild { var cur = '', prev = '', words = '', cword = '', split = '' _init_completion -s || return var rpm = $(1%build*) [[ $rpm == $1 ]] || ! type $rpm &>/dev/null && set rpm = '' match $prev { with --buildroot|--root|-r|--dbpath _filedir -d return 0 with --target _rpm_buildarchs return 0 with --eval|-E _rpm_macros $rpm return 0 with --macros|--rcfile _filedir return 0 with --buildpolicy var cfgdir = $[ $rpm --eval '%{_rpmconfigdir}' !2 >/dev/null] if [[ $cfgdir ]] { setglobal COMPREPLY = '( '$( compgen -W "$( command ls $cfgdir 2>/dev/null \ | sed -ne 's/^brp-//p' )" -- "$cur" ) ) } with --define|-D|--with|--without return 0 } $split && return 0 if [[ $cur == -* ]] { setglobal COMPREPLY = '( '$( compgen -W "$( _parse_help "$1" )" -- "$cur" ) ) [[ $COMPREPLY == *= ]] && compopt -o nospace return 0 } # Figure out file extensions to complete var word = '', ext = '' for word in [$(words[@])] { match $word { with -b?|--clean|--nobuild set ext = 'spec' break with -t?|--tarbuild set ext = ''@(t?(ar.)@([gx]z|bz?(2))|tar?(.@(lzma|Z)))'' break with --rebuild|--recompile set ext = ''@(?(no)src.r|s)pm'' break } } [[ -n $ext ]] && _filedir $ext } && complete -F _rpmbuild rpmbuild rpmbuild-md5 # ex: ts=4 sw=4 et filetype=sh