From nobody Thu May 15 09:55:14 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1510589214749694.2878260476042; Mon, 13 Nov 2017 08:06:54 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7136D3D962; Mon, 13 Nov 2017 16:06:53 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4F9AA7F5FC; Mon, 13 Nov 2017 16:06:53 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 1B92A1800FC5; Mon, 13 Nov 2017 16:06:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vADG6pqG016777 for ; Mon, 13 Nov 2017 11:06:51 -0500 Received: by smtp.corp.redhat.com (Postfix) id 51CE383879; Mon, 13 Nov 2017 16:06:51 +0000 (UTC) Received: from localhost.localdomain (ovpn-204-194.brq.redhat.com [10.40.204.194]) by smtp.corp.redhat.com (Postfix) with ESMTP id B81608387C for ; Mon, 13 Nov 2017 16:06:50 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Mon, 13 Nov 2017 17:06:29 +0100 Message-Id: <7de94d20a7887fc4440482c1c79049cc84f73a79.1510588897.git.mprivozn@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 09/12] tools: Provide bash autompletion file X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 13 Nov 2017 16:06:53 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Michal Privoznik --- configure.ac | 3 ++ libvirt.spec.in | 2 ++ m4/virt-bash-completion.m4 | 74 ++++++++++++++++++++++++++++++++++++++++++= ++++ tools/Makefile.am | 22 ++++++++++++-- tools/bash-completion/vsh | 67 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 m4/virt-bash-completion.m4 create mode 100644 tools/bash-completion/vsh diff --git a/configure.ac b/configure.ac index b2d991c3b..9103612bb 100644 --- a/configure.ac +++ b/configure.ac @@ -242,6 +242,7 @@ LIBVIRT_ARG_APPARMOR LIBVIRT_ARG_ATTR LIBVIRT_ARG_AUDIT LIBVIRT_ARG_AVAHI +LIBVIRT_ARG_BASH_COMPLETION LIBVIRT_ARG_BLKID LIBVIRT_ARG_CAPNG LIBVIRT_ARG_CURL @@ -278,6 +279,7 @@ LIBVIRT_CHECK_ATOMIC LIBVIRT_CHECK_ATTR LIBVIRT_CHECK_AUDIT LIBVIRT_CHECK_AVAHI +LIBVIRT_CHECK_BASH_COMPLETION LIBVIRT_CHECK_BLKID LIBVIRT_CHECK_CAPNG LIBVIRT_CHECK_CURL @@ -976,6 +978,7 @@ LIBVIRT_RESULT_APPARMOR LIBVIRT_RESULT_ATTR LIBVIRT_RESULT_AUDIT LIBVIRT_RESULT_AVAHI +LIBVIRT_RESULT_BASH_COMPLETION LIBVIRT_RESULT_BLKID LIBVIRT_RESULT_CAPNG LIBVIRT_RESULT_CURL diff --git a/libvirt.spec.in b/libvirt.spec.in index b00689cab..67bbd128c 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -2043,6 +2043,8 @@ exit 0 %{_datadir}/systemtap/tapset/libvirt_qemu_probes*.stp %{_datadir}/systemtap/tapset/libvirt_functions.stp =20 +%{_datadir}/bash-completion/completions/vsh + =20 %if %{with_systemd} %{_unitdir}/libvirt-guests.service diff --git a/m4/virt-bash-completion.m4 b/m4/virt-bash-completion.m4 new file mode 100644 index 000000000..e1ef58740 --- /dev/null +++ b/m4/virt-bash-completion.m4 @@ -0,0 +1,74 @@ +dnl Bash completion support +dnl +dnl Copyright (C) 2017 Red Hat, Inc. +dnl +dnl This library is free software; you can redistribute it and/or +dnl modify it under the terms of the GNU Lesser General Public +dnl License as published by the Free Software Foundation; either +dnl version 2.1 of the License, or (at your option) any later version. +dnl +dnl This library is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +dnl Lesser General Public License for more details. +dnl +dnl You should have received a copy of the GNU Lesser General Public +dnl License along with this library. If not, see +dnl . +dnl +dnl Inspired by libguestfs code. +dnl + +AC_DEFUN([LIBVIRT_ARG_BASH_COMPLETION],[ + LIBVIRT_ARG_WITH_FEATURE([BASH_COMPLETION], [bash-completion], [check], = [2.0]) + LIBVIRT_ARG_WITH([BASH_COMPLETIONS_DIR], + [directory containing bash completions scripts], + [check]) +]) + +AC_DEFUN([LIBVIRT_CHECK_BASH_COMPLETION], [ + AC_REQUIRE([LIBVIRT_CHECK_READLINE]) + + if test "x$with_readline" !=3D "xyes" ; then + if test "x$with_bash_completion" !=3D "xyes" ; then + with_bash_completion=3Dno + else + AC_MSG_ERROR([readline is required for bash completion support]) + fi + else + if test "x$with_bash_completion" =3D "xcheck" ; then + with_bash_completion=3Dyes + fi + fi + + LIBVIRT_CHECK_PKG([BASH_COMPLETION], [bash-completion], [2.0]) + + if test "x$with_bash_completion" =3D "xyes" ; then + if test "x$with_bash_completions_dir" =3D "xcheck"; then + AC_MSG_CHECKING([for bash-completions directory]) + BASH_COMPLETIONS_DIR=3D"$($PKG_CONFIG --variable=3Dcompletionsdir ba= sh-completion)" + AC_MSG_RESULT([$BASH_COMPLETIONS_DIR]) + + dnl Replace bash completions's exec_prefix with our own. + dnl Note that ${exec_prefix} is kept verbatim at this point in time, + dnl and will only be expanded later, when make is called: this makes + dnl it possible to override such prefix at compilation or installati= on + dnl time + bash_completions_prefix=3D"$($PKG_CONFIG --variable=3Dprefix bash-co= mpletion)" + if test "x$bash_completions_prefix" =3D "x" ; then + bash_completions_prefix=3D"/usr" + fi + + BASH_COMPLETIONS_DIR=3D'${exec_prefix}'"${BASH_COMPLETIONS_DIR#$bash= _completions_prefix}" + elif test "x$with_bash_completions_dir" =3D "xno" || test "x$with_bash= _completions_dir" =3D "xyes"; then + AC_MSG_ERROR([bash-completions-dir must be used only with valid path= ]) + else + BASH_COMPLETIONS_DIR=3D$with_bash_completions_dir + fi + AC_SUBST([BASH_COMPLETIONS_DIR]) + fi +]) + +AC_DEFUN([LIBVIRT_RESULT_BASH_COMPLETION],[ + LIBVIRT_RESULT_LIB([BASH_COMPLETION]) +]) diff --git a/tools/Makefile.am b/tools/Makefile.am index 7513a73ac..34a81e69c 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -66,6 +66,7 @@ EXTRA_DIST =3D \ libvirt-guests.sysconf \ virt-login-shell.conf \ virsh-edit.c \ + bash-completion/vsh \ $(PODFILES) \ $(MANINFILES) \ $(NULL) @@ -332,9 +333,11 @@ POD2MAN =3D pod2man -c "Virtualization Support" -r "$(= PACKAGE)-$(VERSION)" < $< > $@-t && \ mv $@-t $@ =20 -install-data-local: install-init install-systemd install-nss +install-data-local: install-init install-systemd install-nss \ + install-bash-completion =20 -uninstall-local: uninstall-init uninstall-systemd uninstall-nss +uninstall-local: uninstall-init uninstall-systemd uninstall-nss \ + uninstall-bash-completion =20 install-sysconfig: $(MKDIR_P) $(DESTDIR)$(sysconfdir)/sysconfig @@ -420,6 +423,21 @@ libvirt-guests.service: libvirt-guests.service.in $(to= p_builddir)/config.status mv $@-t $@ =20 =20 +if WITH_BASH_COMPLETION +install-bash-completion: + $(MKDIR_P) "$(DESTDIR)$(BASH_COMPLETIONS_DIR)" + $(INSTALL_SCRIPT) $(srcdir)/bash-completion/vsh \ + "$(DESTDIR)$(BASH_COMPLETIONS_DIR)/vsh" + +uninstall-bash-completion: + rm -f $(DESTDIR)$(BASH_COMPLETIONS_DIR)/vsh + rmdir $(DESTDIR)$(BASH_COMPLETIONS_DIR) ||: +else ! WITH_BASH_COMPLETION +install-bash-completion: +uninstall-bash-completion: +endif ! WITH_BASH_COMPLETION + + EXTRA_DIST +=3D \ wireshark/util/genxdrstub.pl \ wireshark/util/make-dissector-reg diff --git a/tools/bash-completion/vsh b/tools/bash-completion/vsh new file mode 100644 index 000000000..8493cad28 --- /dev/null +++ b/tools/bash-completion/vsh @@ -0,0 +1,67 @@ +# +# virsh & virt-admin completion command +# + +_vsh_complete() +{ + local words cword c=3D0 i=3D0 cur RO URI CMDLINE INPUT A + + # Here, $COMP_WORDS is an array of words on the bash + # command line that user wants to complete. However, when + # parsing command line, the default set of word breaks is + # applied. This doesn't work for us as it mangles libvirt + # arguments, e.g. connection URI (with the default set it's + # split into multiple items within the array). Fortunately, + # there's a fixup function for the array. + _get_comp_words_by_ref -n "\"'><=3D;|&(:" -w words -i cword + COMP_WORDS=3D( "${words[@]}" ) + COMP_CWORD=3D${cword} + cur=3D${COMP_WORDS[$COMP_CWORD]} + + # See what URI is user trying to connect to and if they are + # connecting RO. Honour that. + while [ $c -le $COMP_CWORD ]; do + word=3D"${COMP_WORDS[c]}" + case "$word" in + -r|--readonly) RO=3D1 ;; + -c|--connect) c=3D$((++c)); URI=3D${COMP_WORDS[c]} ;; + *) if [ $c -ne 0 ] && [ $i -eq 0 ]; then i=3D$c; break; fi ;; + esac + c=3D$((++c)) + done + + CMDLINE=3D + if [ -n "${RO}" ]; then + CMDLINE=3D"${CMDLINE} -r" + fi + if [ -n "${URI}" ]; then + CMDLINE=3D"${CMDLINE} -c ${URI}" + fi + + INPUT=3D( "${COMP_WORDS[@]:$i:$COMP_CWORD}" ) + + # Uncomment these lines for easy debug. +# echo; +# echo "RO=3D${flag_ro}"; +# echo "URI=3D${URI}"; +# echo "CMDLINE=3D${CMDLINE}"; +# echo "INPUT[${#INPUT[@]}]=3D**${INPUT[@]}**"; +# echo "cur=3D${cur}"; +# echo; +# return 0; + + # Small shortcut here. According to manpage: + # When the function is executed, the first argument ($1) is + # the name of the command whose arguments are being + # completed. + # Therefore, we might just run $1. + A=3D($($1 ${CMDLINE} complete -- "${INPUT[@]}" 2>/dev/null)) + + COMPREPLY=3D($(compgen -W "${A[*]%--}" -- ${cur})) + __ltrim_colon_completions "$cur" + return 0 +} && +complete -o default -o filenames -F _vsh_complete virsh && +complete -o default -o filenames -F _vsh_complete virt-admin + +# vim: ft=3Dsh:et:ts=3D4:sw=3D4:tw=3D80 --=20 2.13.6 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list