]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - shell-completion/bash/systemctl.in
Merge pull request #17549 from yuwata/tiny-fixes
[thirdparty/systemd.git] / shell-completion / bash / systemctl.in
index 8986f4537ab867d611eeee2547ebe24c63bbf842..f6be3827cff6b142c47881599cf6aa039488f235 100644 (file)
@@ -1,5 +1,5 @@
 # systemctl(1) completion                                 -*- shell-script -*-
-# SPDX-License-Identifier: LGPL-2.1+
+# SPDX-License-Identifier: LGPL-2.1-or-later
 #
 # This file is part of systemd.
 #
@@ -7,7 +7,7 @@
 
 __systemctl() {
     local mode=$1; shift 1
-    systemctl $mode --full --no-legend --no-pager "$@" 2>/dev/null
+    systemctl $mode --full --no-legend --no-pager --plain "$@" 2>/dev/null
 }
 
 __systemd_properties() {
@@ -113,14 +113,13 @@ __get_all_unit_files () { { __systemctl $1 list-unit-files "$2*"; } | { while re
 
 __get_machines() {
     local a b
-    { machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; } | \
+    { machinectl list-images --full --no-legend --no-pager; machinectl list --full --no-legend --no-pager; } | \
         { while read a b; do echo " $a"; done; }
 }
 
 _systemctl () {
     local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
-    local cur_orig=$cur
-    local i verb comps mode
+    local i verb comps mode cur_orig
 
     local -A OPTS=(
         [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
@@ -128,7 +127,7 @@ _systemctl () {
                              --quiet -q --system --user --version --runtime --recursive -r --firmware-setup
                              --show-types -i --ignore-inhibitors --plain --failed --value --fail --dry-run --wait'
         [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root
-                             --preset-mode -n --lines -o --output -M --machine --message'
+                             --preset-mode -n --lines -o --output -M --machine --message --timestamp'
     )
 
     if __contains_word "--user" ${COMP_WORDS[*]}; then
@@ -177,6 +176,9 @@ _systemctl () {
             --machine|-M)
                 comps=$( __get_machines )
                 ;;
+            --timestamp)
+                comps='pretty us µs utc us+utc µs+utc'
+                ;;
         esac
         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
         return 0
@@ -212,6 +214,9 @@ _systemctl () {
         [FILE]='link switch-root'
         [TARGETS]='set-default'
         [MACHINES]='list-machines'
+        [LOG_LEVEL]='log-level'
+        [LOG_TARGET]='log-target'
+        [SERVICE_WATCHDOGS]='service-watchdogs'
     )
 
     for ((i=0; i < COMP_CWORD; i++)); do
@@ -223,11 +228,26 @@ _systemctl () {
     done
 
     # When trying to match a unit name with certain special characters in its name (i.e
-    # foo\x2dbar:01) they get escaped by bash along the way, thus causing any possible
-    # match to fail. Let's unescape such characters in the verb we're trying to
-    # autocomplete to avoid this, however, use the original verb (cur_orig)
-    # during the final match (COMPREPLY)
-    cur="$(echo $cur | xargs echo)"
+    # foo\x2dbar:01) they get (un)escaped by bash along the way, thus causing any possible
+    # match to fail.
+    # The following condition solves two cases:
+    # 1) We're trying to complete an already escaped unit name part,
+    #    i.e foo\\x2dba. In this case we need to unescape the name, so it
+    #    gets properly matched with the systemctl output (i.e. foo\x2dba).
+    #    However, we need to keep the original escaped name as well for the
+    #    final match, as the completion machinery does the unescaping
+    #    automagically.
+    # 2) We're trying to complete an unescaped (literal) unit name part,
+    #    i.e. foo\x2dba. That means we don't have to do the unescaping
+    #    required for correct matching with systemctl's output, however,
+    #    we need to escape the name for the final match, where the completion
+    #    expects the string to be escaped.
+    cur_orig=$cur
+    if [[ $cur =~ '\\' ]]; then
+        cur="$(echo $cur | xargs echo)"
+    else
+        cur_orig="$(printf '%q' $cur)"
+    fi
 
     if [[ -z $verb ]]; then
         comps="${VERBS[*]}"
@@ -312,6 +332,12 @@ _systemctl () {
     elif __contains_word "$verb" ${VERBS[TARGETS]}; then
         comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" \
                      | { while read -r a b; do echo " $a"; done; } )
+    elif __contains_word "$verb" ${VERBS[LOG_LEVEL]}; then
+        comps='debug info notice warning err crit alert emerg'
+    elif __contains_word "$verb" ${VERBS[LOG_TARGET]}; then
+        comps='console journal kmsg journal-or-kmsg null'
+    elif __contains_word "$verb" ${VERBS[SERVICE_WATCHDOGS]}; then
+        comps='on off'
     fi
 
     COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )