Hi, As stated here, we should use a library for bash autocompletions (maybe even with include guards). http://cgit.freedesktop.org/systemd/systemd/commit/shell-completion/bash/localectl?id=a72d698d0d9ff9c158155b44cdc77376df31a317
Explanation: Using autotools make the autocompletions to /usr/share/bash-completions/completions (as the exit from pkg-config --variable=completionsdir bash-completion). We ended up having multiple function definitions and even messing around with the global bash function namespace, as the functions are called __get_something being possible to redefine other binary bash function. With this patch: 1- All the files in bash completions, except busctl and systemctl.in, use this common library. 2- They are prefixed with the namespace __systemdh. --- shell-completion/bash/bootctl | 12 ++--- shell-completion/bash/coredumpctl | 21 ++++---- shell-completion/bash/hostnamectl | 14 ++---- shell-completion/bash/journalctl | 10 ++-- shell-completion/bash/localectl | 18 +++---- shell-completion/bash/loginctl | 36 ++++++-------- shell-completion/bash/machinectl | 26 ++++------ shell-completion/bash/systemd-analyze | 31 ++++-------- shell-completion/bash/systemd-cat | 10 ++-- shell-completion/bash/systemd-cgls | 17 ++----- shell-completion/bash/systemd-cgtop | 8 +-- shell-completion/bash/systemd-delta | 10 ++-- shell-completion/bash/systemd-detect-virt | 8 +-- shell-completion/bash/systemd-helpers | 83 +++++++++++++++++++++++++++++++ shell-completion/bash/systemd-nspawn | 43 +++------------- shell-completion/bash/systemd-run | 60 ++++++++++------------ shell-completion/bash/timedatectl | 18 +++---- shell-completion/bash/udevadm | 20 +++----- Makefile.am | 1 + 19 files changed, 206 insertions(+), 240 deletions(-) create mode 100644 shell-completion/bash/systemd-helpers diff --git a/shell-completion/bash/bootctl b/shell-completion/bash/bootctl index c86ec7e..be627b2 100644 --- a/shell-completion/bash/bootctl +++ b/shell-completion/bash/bootctl @@ -17,12 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word () { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers _bootctl() { local i verb comps @@ -41,7 +37,7 @@ _bootctl() { ) for ((i=0; i < COMP_CWORD; i++)); do - if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then + if __systemdh_contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then verb=${COMP_WORDS[i]} break fi @@ -49,7 +45,7 @@ _bootctl() { if [[ -z $verb ]]; then comps=${VERBS[*]} - elif __contains_word "$verb" ${VERBS[STANDALONE]}; then + elif __systemdh_contains_word "$verb" ${VERBS[STANDALONE]}; then comps='' fi diff --git a/shell-completion/bash/coredumpctl b/shell-completion/bash/coredumpctl index e23152d..0c4cda3 100644 --- a/shell-completion/bash/coredumpctl +++ b/shell-completion/bash/coredumpctl @@ -17,12 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word () { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers __journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC} ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID} COREDUMP_EXE @@ -34,6 +30,7 @@ __journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC} _KERNEL_{DEVICE,SUBSYSTEM} _UDEV_{SYSNAME,DEVNODE,DEVLINK} __CURSOR __{REALTIME,MONOTONIC}_TIMESTAMP) + _coredumpctl() { local i verb comps local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} @@ -44,15 +41,15 @@ _coredumpctl() { [DUMP]='dump gdb' ) - if __contains_word "$prev" '--output -o'; then + if __systemdh_contains_word "$prev" '--output -o'; then comps=$( compgen -A file -- "$cur" ) compopt -o filenames - elif __contains_word "$prev" '--FIELD -F'; then + elif __systemdh_contains_word "$prev" '--FIELD -F'; then comps=$( compgen -W '${__journal_fields[*]}' -- "$cur" ) elif [[ $cur = -* ]]; then comps=${OPTS} - elif __contains_word "$prev" ${VERBS[*]} && - ! __contains_word ${COMP_WORDS[COMP_CWORD-2]} '--output -o -F --field'; then + elif __systemdh_contains_word "$prev" ${VERBS[*]} && + ! __systemdh_contains_word ${COMP_WORDS[COMP_CWORD-2]} '--output -o -F --field'; then compopt -o nospace COMPREPLY=( $(compgen -W '${__journal_fields[*]}' -S= -- "$cur") ) return 0 @@ -65,7 +62,7 @@ _coredumpctl() { comps=${field_vals[*]} else for ((i=0; i <= COMP_CWORD; i++)); do - if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then + if __systemdh_contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then verb=${COMP_WORDS[i]} break fi @@ -73,7 +70,7 @@ _coredumpctl() { if [[ -z $verb ]]; then comps=${VERBS[*]} - elif __contains_word "$verb" ${VERBS[LIST]} ${VERBS[DUMP]}; then + elif __systemdh_contains_word "$verb" ${VERBS[LIST]} ${VERBS[DUMP]}; then comps='' fi fi diff --git a/shell-completion/bash/hostnamectl b/shell-completion/bash/hostnamectl index 7a0850d..a74931f 100644 --- a/shell-completion/bash/hostnamectl +++ b/shell-completion/bash/hostnamectl @@ -17,12 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word () { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers _hostnamectl() { local i verb comps @@ -43,7 +39,7 @@ _hostnamectl() { ) for ((i=0; i < COMP_CWORD; i++)); do - if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then + if __systemdh_contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then verb=${COMP_WORDS[i]} break fi @@ -51,9 +47,9 @@ _hostnamectl() { if [[ -z $verb ]]; then comps=${VERBS[*]} - elif __contains_word "$verb" ${VERBS[CHASSIS]}; then + elif __systemdh_contains_word "$verb" ${VERBS[CHASSIS]}; then comps='desktop laptop server tablet handset watch embedded vm container' - elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[ICONS]} ${VERBS[NAME]}; then + elif __systemdh_contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[ICONS]} ${VERBS[NAME]}; then comps='' fi diff --git a/shell-completion/bash/journalctl b/shell-completion/bash/journalctl index 2efa100..d66fa1a 100644 --- a/shell-completion/bash/journalctl +++ b/shell-completion/bash/journalctl @@ -17,12 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word () { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers __journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC} ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID} COREDUMP_EXE @@ -51,7 +47,7 @@ _journalctl() { --verify-key' ) - if __contains_word "$prev" ${OPTS[ARG]} ${OPTS[ARGUNKNOWN]}; then + if __systemdh_contains_word "$prev" ${OPTS[ARG]} ${OPTS[ARGUNKNOWN]}; then case $prev in --boot|--this-boot|-b) comps=$(journalctl -F '_BOOT_ID' 2>/dev/null) diff --git a/shell-completion/bash/localectl b/shell-completion/bash/localectl index c9e22af..647ff89 100644 --- a/shell-completion/bash/localectl +++ b/shell-completion/bash/localectl @@ -17,12 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word () { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers __locale_fields=( LANG LANGUAGE LC_CTYPE LC_NUMERIC LC_TIME \ LC_COLLATE LC_MONETARY LC_MESSAGES LC_PAPER \ @@ -36,7 +32,7 @@ _localectl() { local OPTS='-h --help --version --no-convert --no-pager --no-ask-password -H --host' - if __contains_word "$prev" $OPTS; then + if __systemdh_contains_word "$prev" $OPTS; then case $prev in --host|-H) comps='' @@ -59,7 +55,7 @@ _localectl() { ) for ((i=0; i < COMP_CWORD; i++)); do - if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then + if __systemdh_contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then verb=${COMP_WORDS[i]} break fi @@ -67,7 +63,7 @@ _localectl() { if [[ -z $verb ]]; then comps=${VERBS[*]} - elif __contains_word "$verb" ${VERBS[LOCALES]}; then + elif __systemdh_contains_word "$verb" ${VERBS[LOCALES]}; then if [[ $cur = *=* ]]; then mapfile -t locale_vals < <(command localectl list-locales 2>/dev/null) COMPREPLY=( $(compgen -W '${locale_vals[*]}' -- "${cur#=}") ) @@ -79,9 +75,9 @@ _localectl() { COMPREPLY=( $(compgen -W '${__locale_fields[*]}' -S= -- "$cur") ) fi return 0 - elif __contains_word "$verb" ${VERBS[KEYMAPS]}; then + elif __systemdh_contains_word "$verb" ${VERBS[KEYMAPS]}; then comps=$(command localectl list-keymaps) - elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[X11]}; then + elif __systemdh_contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[X11]}; then comps='' fi diff --git a/shell-completion/bash/loginctl b/shell-completion/bash/loginctl index e7adb93..5b1aa47 100644 --- a/shell-completion/bash/loginctl +++ b/shell-completion/bash/loginctl @@ -17,16 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word () { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} - -__get_all_sessions () { loginctl --no-legend list-sessions | { while read -r a b; do printf "%s\n" "$a"; done; } ; } -__get_all_users () { loginctl --no-legend list-users | { while read -r a b; do printf "%s\n" "$b"; done; } ; } -__get_all_seats () { loginctl --no-legend list-seats | { while read -r a b; do printf "%s\n" "$a"; done; } ; } +#Common functions +. ./systemd-helpers _loginctl () { local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} @@ -37,7 +29,7 @@ _loginctl () { [ARG]='--host -H --kill-who --property -p --signal -s' ) - if __contains_word "$prev" ${OPTS[ARG]}; then + if __systemdh_contains_word "$prev" ${OPTS[ARG]}; then case $prev in --signal|-s) comps=$(compgen -A signal) @@ -71,8 +63,8 @@ _loginctl () { ) for ((i=0; i < COMP_CWORD; i++)); do - if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && - ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then + if __systemdh_contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && + ! __systemdh_contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then verb=${COMP_WORDS[i]} break fi @@ -81,21 +73,21 @@ _loginctl () { if [[ -z $verb ]]; then comps="${VERBS[*]}" - elif __contains_word "$verb" ${VERBS[SESSIONS]}; then - comps=$( __get_all_sessions ) + elif __systemdh_contains_word "$verb" ${VERBS[SESSIONS]}; then + comps=$( __systemdh_get_sessions ) - elif __contains_word "$verb" ${VERBS[USERS]}; then - comps=$( __get_all_users ) + elif __systemdh_contains_word "$verb" ${VERBS[USERS]}; then + comps=$( __systemdh_get_users ) - elif __contains_word "$verb" ${VERBS[SEATS]}; then - comps=$( __get_all_seats ) + elif __systemdh_contains_word "$verb" ${VERBS[SEATS]}; then + comps=$( __systemdh_get_seats ) - elif __contains_word "$verb" ${VERBS[STANDALONE]}; then + elif __systemdh_contains_word "$verb" ${VERBS[STANDALONE]}; then comps='' - elif __contains_word "$verb" ${VERBS[ATTACH]}; then + elif __systemdh_contains_word "$verb" ${VERBS[ATTACH]}; then if [[ $prev = $verb ]]; then - comps=$( __get_all_seats ) + comps=$( __systemdh_get_seats ) else comps=$(compgen -A file -- "$cur" ) compopt -o filenames diff --git a/shell-completion/bash/machinectl b/shell-completion/bash/machinectl index 3789492..300761a 100644 --- a/shell-completion/bash/machinectl +++ b/shell-completion/bash/machinectl @@ -17,17 +17,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word() { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers -__get_machines() { - local a b - machinectl list --no-legend --no-pager | { while read a b; do echo " $a"; done; }; -} _machinectl() { local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} @@ -46,14 +38,14 @@ _machinectl() { _init_completion || return for ((i=0; i <= COMP_CWORD; i++)); do - if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && - ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then + if __systemdh_contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && + ! __systemdh_contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then verb=${COMP_WORDS[i]} break fi done - if __contains_word "$prev" ${OPTS[ARG]}; then + if __systemdh_contains_word "$prev" ${OPTS[ARG]}; then case $prev in --signal|-s) comps=$(compgen -A signal) @@ -65,7 +57,7 @@ _machinectl() { comps=$(compgen -A hostname) ;; --machine|-M) - comps=$( __get_machines ) + comps=$( __systemdh_get_machines ) ;; --property|-p) comps='' @@ -83,11 +75,11 @@ _machinectl() { if [[ -z $verb ]]; then comps=${VERBS[*]} - elif __contains_word "$verb" ${VERBS[STANDALONE]}; then + elif __systemdh_contains_word "$verb" ${VERBS[STANDALONE]}; then comps='' - elif __contains_word "$verb" ${VERBS[MACHINES]}; then - comps=$( __get_machines ) + elif __systemdh_contains_word "$verb" ${VERBS[MACHINES]}; then + comps=$( __systemdh_get_machines ) fi COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) diff --git a/shell-completion/bash/systemd-analyze b/shell-completion/bash/systemd-analyze index 77d1b54..b351a2f 100644 --- a/shell-completion/bash/systemd-analyze +++ b/shell-completion/bash/systemd-analyze @@ -18,17 +18,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word () { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} - -__get_machines() { - local a b - machinectl list --no-legend --no-pager | { while read a b; do echo " $a"; done; }; -} +#Common functions +. ./systemd-helpers _systemd_analyze() { local i verb comps @@ -50,20 +41,20 @@ _systemd_analyze() { _init_completion || return for ((i=0; i < COMP_CWORD; i++)); do - if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && - ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then + if __systemdh_contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && + ! __systemdh_contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then verb=${COMP_WORDS[i]} break fi done - if __contains_word "$prev" ${OPTS[ARG]}; then + if __systemdh_contains_word "$prev" ${OPTS[ARG]}; then case $prev in --host|-H) comps=$(compgen -A hostname) ;; --machine|-M) - comps=$( __get_machines ) + comps=$( __systemdh_get_machines ) ;; esac COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) @@ -78,29 +69,29 @@ _systemd_analyze() { if [[ -z $verb ]]; then comps=${VERBS[*]} - elif __contains_word "$verb" ${VERBS[STANDALONE]}; then + elif __systemdh_contains_word "$verb" ${VERBS[STANDALONE]}; then if [[ $cur = -* ]]; then comps='--help --version --system --user' fi - elif __contains_word "$verb" ${VERBS[CRITICAL_CHAIN]}; then + elif __systemdh_contains_word "$verb" ${VERBS[CRITICAL_CHAIN]}; then if [[ $cur = -* ]]; then comps='--help --version --system --user --fuzz' fi - elif __contains_word "$verb" ${VERBS[DOT]}; then + elif __systemdh_contains_word "$verb" ${VERBS[DOT]}; then if [[ $cur = -* ]]; then comps='--help --version --system --user --from-pattern --to-pattern --order --require' fi - elif __contains_word "$verb" ${VERBS[LOG_LEVEL]}; then + elif __systemdh_contains_word "$verb" ${VERBS[LOG_LEVEL]}; then if [[ $cur = -* ]]; then comps='--help --version --system --user' else comps='debug info notice warning err crit alert emerg' fi - elif __contains_word "$verb" ${VERBS[VERIFY]}; then + elif __systemdh_contains_word "$verb" ${VERBS[VERIFY]}; then if [[ $cur = -* ]]; then comps='--help --version --system --user --no-man' else diff --git a/shell-completion/bash/systemd-cat b/shell-completion/bash/systemd-cat index 8d84042..e904f0c 100644 --- a/shell-completion/bash/systemd-cat +++ b/shell-completion/bash/systemd-cat @@ -17,12 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word() { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers _systemd_cat() { local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} @@ -35,7 +31,7 @@ _systemd_cat() { _init_completion || return - if __contains_word "$prev" ${OPTS[ARG]}; then + if __systemdh_contains_word "$prev" ${OPTS[ARG]}; then case $prev in --identifier|-t) comps='' diff --git a/shell-completion/bash/systemd-cgls b/shell-completion/bash/systemd-cgls index 0570438..91f2de3 100644 --- a/shell-completion/bash/systemd-cgls +++ b/shell-completion/bash/systemd-cgls @@ -17,17 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word() { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} - -__get_machines() { - local a b - machinectl list --no-legend --no-pager | { while read a b; do echo " $a"; done; }; -} +#Common functions +. ./systemd-helpers _systemd_cgls() { local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} @@ -40,10 +31,10 @@ _systemd_cgls() { _init_completion || return - if __contains_word "$prev" ${OPTS[ARG]}; then + if __systemdh_contains_word "$prev" ${OPTS[ARG]}; then case $prev in --machine|-M) - comps=$( __get_machines ) + comps=$( __systemdh_get_machines ) ;; esac COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) diff --git a/shell-completion/bash/systemd-cgtop b/shell-completion/bash/systemd-cgtop index d7ea42d..89e5619 100644 --- a/shell-completion/bash/systemd-cgtop +++ b/shell-completion/bash/systemd-cgtop @@ -17,12 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word() { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers _systemd_cgtop() { local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} diff --git a/shell-completion/bash/systemd-delta b/shell-completion/bash/systemd-delta index cb17328..ab305a6 100644 --- a/shell-completion/bash/systemd-delta +++ b/shell-completion/bash/systemd-delta @@ -17,12 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word() { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers _systemd-delta() { local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} @@ -36,7 +32,7 @@ _systemd-delta() { _init_completion || return - if __contains_word "$prev" ${OPTS[ARG]}; then + if __systemdh_contains_word "$prev" ${OPTS[ARG]}; then case $prev in --diff) comps='yes no' diff --git a/shell-completion/bash/systemd-detect-virt b/shell-completion/bash/systemd-detect-virt index df06c29..183b196 100644 --- a/shell-completion/bash/systemd-detect-virt +++ b/shell-completion/bash/systemd-detect-virt @@ -17,12 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word() { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers _systemd_detect_virt() { local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} diff --git a/shell-completion/bash/systemd-helpers b/shell-completion/bash/systemd-helpers new file mode 100644 index 0000000..51ee556 --- /dev/null +++ b/shell-completion/bash/systemd-helpers @@ -0,0 +1,83 @@ +# systemd-helpers completion -*- shell-script -*- +# +# This file is part of systemd. +# +# Copyright 2015 Carlos Morata Castillo [email protected] +# +# systemd is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# systemd 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 Lesser General Public License +# along with systemd; If not, see <http://www.gnu.org/licenses/>. + +__systemdh_contains_word () { + local w word=$1; shift + for w in "$@"; do + [[ $w = "$word" ]] && return + done +} + +__systemdh_get_users() { + local a b + loginctl list-users --no-legend --no-pager | while read a b; + do + echo " $b"; + done +} + +__systemdh_get_sessions () { + local a b + loginctl --no-legend list-sessions | while read -r a b; + do + printf "%s\n" "$a"; + done +} + +__systemdh_get_seats() { + local a b + loginctl --no-legend list-seats | while read -r a b; + do + printf "%s\n" "$a"; + done +} + +__systemdh_get_machines() { + local a b + machinectl list --no-legend --no-pager | while read a b; + do + echo " $a"; + done +} + +__systemdh_get_env() { + local a + env | while read a; + do + echo " ${a%%=*}"; + done +} + +__systemdh_get_interfaces(){ + cut -f 1 -d ' ' /proc/net/dev | tail -n +3 | tr -s '\n' | tr -d ':' | xargs +} + +__systemdh_get_slices() { + local a b + #add -all? + systemctl $1 list-units -t slice --no-legend --no-pager | while read a b; + do + echo " $a"; + done +} + +__systemdh_get_sysdevs() { + local -a devs=(/sys/bus/*/devices/*/ /sys/class/*/*/) + printf '%s\n' "${devs[@]%/}" +} diff --git a/shell-completion/bash/systemd-nspawn b/shell-completion/bash/systemd-nspawn index 83e34ef..1f7ee2b 100644 --- a/shell-completion/bash/systemd-nspawn +++ b/shell-completion/bash/systemd-nspawn @@ -17,36 +17,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word() { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} - -__get_users() { - local a b - loginctl list-users --no-legend --no-pager | { while read a b; do echo " $b"; done; }; -} - -__get_slices() { - local a b - systemctl list-units -t slice --no-legend --no-pager | { while read a b; do echo " $a"; done; }; -} +#Common functions +. ./systemd-helpers -__get_machines() { - local a b - machinectl list --no-legend --no-pager | { while read a b; do echo " $a"; done; }; -} - -__get_env() { - local a - env | { while read a; do echo " ${a%%=*}"; done; }; -} - -__get_interfaces(){ - cut -f 1 -d ' ' /proc/net/dev | tail -n +3 | tr -s '\n' | tr -d ':' | xargs -} _systemd_nspawn() { local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} @@ -62,14 +35,14 @@ _systemd_nspawn() { _init_completion || return - if __contains_word "$prev" ${OPTS[ARG]}; then + if __systemdh_contains_word "$prev" ${OPTS[ARG]}; then case $prev in --directory|-D) compopt -o nospace comps=$(compgen -S/ -A directory -- "$cur" ) ;; --user|-u) - comps=$( __get_users ) + comps=$( __systemdh_get_users ) ;; --uuid) comps='' @@ -96,13 +69,13 @@ _systemd_nspawn() { comps=$(compgen -S/ -A directory -- "$cur" ) ;; --machine|-M) - comps=$( __get_machines ) + comps=$( __systemdh_get_machines ) ;; --slice|-S) - comps=$( __get_slices ) + comps=$( __systemdh_get_slices ) ;; --setenv) - comps=$( __get_env ) + comps=$( __systemdh_get_env ) ;; --selinux-context|-Z) comps='' @@ -114,7 +87,7 @@ _systemd_nspawn() { comps='yes no' ;; --network-interface) - comps=$(__get_interfaces) + comps=$(__systemdh_get_interfaces) ;; --network-bridge) comps='' diff --git a/shell-completion/bash/systemd-run b/shell-completion/bash/systemd-run index 712655c..f459760 100644 --- a/shell-completion/bash/systemd-run +++ b/shell-completion/bash/systemd-run @@ -17,46 +17,36 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__systemctl() { - local mode=$1; shift 1 - systemctl $mode --full --no-legend "$@" -} - -__get_slice_units () { __systemctl $1 list-units --all -t slice \ - | { while read -r a b c d; do echo " $a"; done; }; } - -__get_machines() { - local a b - machinectl list --no-legend --no-pager | { while read a b; do echo " $a"; done; }; -} +#Common functions +. ./systemd-helpers _systemd_run() { local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} local OPTS='-h --help --version --user --system --scope --unit --description --slice - -r --remain-after-exit --send-sighup -H --host -M --machine --service-type - --uid --gid --nice --setenv -p --property' + -r --remain-after-exit --send-sighup -H --host -M --machine --service-type + --uid --gid --nice --setenv -p --property' - local mode=--system - local i - for (( i=1; i <= COMP_CWORD; i++ )); do - if [[ ${COMP_WORDS[i]} != -* ]]; then - local root_command=${COMP_WORDS[i]} - _command_offset $i - return + local mode=--system + local i + for (( i=1; i <= COMP_CWORD; i++ )); do + if [[ ${COMP_WORDS[i]} != -* ]]; then + local root_command=${COMP_WORDS[i]} + _command_offset $i + return fi [[ ${COMP_WORDS[i]} == "--user" ]] && mode=--user [[ $i -lt $COMP_CWORD && ${COMP_WORDS[i]} == @(--unit|--description|--slice|--service-type|-H|--host|-M|--machine|-p|--property) ]] && ((i++)) - done + done case "$prev" in --unit|--description) - # argument required but no completions available +# argument required but no completions available return ;; --slice) - local comps=$(__get_slice_units $mode) + local comps=$(__systemdh_get_slices $mode) COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) return 0 @@ -69,13 +59,13 @@ _systemd_run() { ;; -p|--property) local comps='CPUAccounting= MemoryAccounting= BlockIOAccounting= SendSIGHUP= - SendSIGKILL= MemoryLimit= CPUShares= BlockIOWeight= User= Group= - DevicePolicy= KillMode= DeviceAllow= BlockIOReadBandwidth= - BlockIOWriteBandwidth= BlockIODeviceWeight= Nice= Environment= - KillSignal= LimitCPU= LimitFSIZE= LimitDATA= LimitSTACK= - LimitCORE= LimitRSS= LimitNOFILE= LimitAS= LimitNPROC= - LimitMEMLOCK= LimitLOCKS= LimitSIGPENDING= LimitMSGQUEUE= - LimitNICE= LimitRTPRIO= LimitRTTIME=' + SendSIGKILL= MemoryLimit= CPUShares= BlockIOWeight= User= Group= + DevicePolicy= KillMode= DeviceAllow= BlockIOReadBandwidth= + BlockIOWriteBandwidth= BlockIODeviceWeight= Nice= Environment= + KillSignal= LimitCPU= LimitFSIZE= LimitDATA= LimitSTACK= + LimitCORE= LimitRSS= LimitNOFILE= LimitAS= LimitNPROC= + LimitMEMLOCK= LimitLOCKS= LimitSIGPENDING= LimitMSGQUEUE= + LimitNICE= LimitRTPRIO= LimitRTTIME=' COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) return 0 @@ -87,15 +77,15 @@ _systemd_run() { return 0 ;; -M|--machine) - local comps=$( __get_machines ) + local comps=$( __systemdh_get_machines ) COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) return 0 ;; - esac + esac - COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) - return 0 + COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) + return 0 } complete -F _systemd_run systemd-run diff --git a/shell-completion/bash/timedatectl b/shell-completion/bash/timedatectl index 1a0acc6..e07661d 100644 --- a/shell-completion/bash/timedatectl +++ b/shell-completion/bash/timedatectl @@ -17,12 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word () { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers _timedatectl() { local i verb comps @@ -30,7 +26,7 @@ _timedatectl() { local OPTS='-h --help --version --adjust-system-clock --no-pager --no-ask-password -H --host' - if __contains_word "$prev" $OPTS; then + if __systemdh_contains_word "$prev" $OPTS; then case $prev in --host|-H) comps='' @@ -53,7 +49,7 @@ _timedatectl() { ) for ((i=0; i < COMP_CWORD; i++)); do - if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then + if __systemdh_contains_word "${COMP_WORDS[i]}" ${VERBS[*]}; then verb=${COMP_WORDS[i]} break fi @@ -61,11 +57,11 @@ _timedatectl() { if [[ -z $verb ]]; then comps=${VERBS[*]} - elif __contains_word "$verb" ${VERBS[BOOLEAN]}; then + elif __systemdh_contains_word "$verb" ${VERBS[BOOLEAN]}; then comps='true false' - elif __contains_word "$verb" ${VERBS[TIMEZONES]}; then + elif __systemdh_contains_word "$verb" ${VERBS[TIMEZONES]}; then comps=$(command timedatectl list-timezones) - elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[TIME]}; then + elif __systemdh_contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[TIME]}; then comps='' fi diff --git a/shell-completion/bash/udevadm b/shell-completion/bash/udevadm index b828b8d..b24b7be 100644 --- a/shell-completion/bash/udevadm +++ b/shell-completion/bash/udevadm @@ -17,17 +17,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with systemd; If not, see <http://www.gnu.org/licenses/>. -__contains_word () { - local w word=$1; shift - for w in "$@"; do - [[ $w = "$word" ]] && return - done -} +#Common functions +. ./systemd-helpers -__get_all_sysdevs() { - local -a devs=(/sys/bus/*/devices/*/ /sys/class/*/*/) - printf '%s\n' "${devs[@]%/}" -} _udevadm() { local i verb comps @@ -37,8 +29,8 @@ _udevadm() { local verbs=(info trigger settle control monitor hwdb test-builtin test) for ((i=0; i < COMP_CWORD; i++)); do - if __contains_word "${COMP_WORDS[i]}" "${verbs[@]}" && - ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then + if __systemdh_contains_word "${COMP_WORDS[i]}" "${verbs[@]}" && + ! __systemdh_contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then verb=${COMP_WORDS[i]} break fi @@ -54,7 +46,7 @@ _udevadm() { if [[ $cur = -* ]]; then comps='--help --query= --path= --name= --root --attribute-walk --export-db --cleanup-db' else - comps=$( __get_all_sysdevs ) + comps=$( __systemdh_get_sysdevs ) fi ;; 'trigger') @@ -79,7 +71,7 @@ _udevadm() { if [[ $cur = -* ]]; then comps='--help --action=' else - comps=$( __get_all_sysdevs ) + comps=$( __systemdh_get_sysdevs ) fi ;; 'test-builtin') diff --git a/Makefile.am b/Makefile.am index aa5580b..30f1f0f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -406,6 +406,7 @@ systemgenerator_PROGRAMS = \ systemd-debug-generator dist_bashcompletion_DATA = \ + shell-completion/bash/systemd-helpers \ shell-completion/bash/busctl \ shell-completion/bash/journalctl \ shell-completion/bash/systemd-analyze \ -- 2.1.0 _______________________________________________ systemd-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/systemd-devel
