Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -2194,28 +2194,35 @@ _comp_compgen_xinetd_services()

# This function completes on services
#
# @return 0 if at least one completion is generated, or 1 otherwise.
# @since 2.12
_comp_compgen_services()
{
local sysvdirs
_comp_sysvdirs || return 1

local services
_comp_expand_glob services '${sysvdirs[0]}/!($_comp_backup_glob|functions|README)'
local status=1
_comp_compgen -U sysvdirs -C "${sysvdirs[0]}" -- -f -X "@($_comp_backup_glob|functions|README)" &&
status=0

local _generated=$({
systemctl list-units --full --all ||
systemctl list-unit-files
} 2>/dev/null |
_comp_awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }')
_comp_split -la services "$_generated"
} 2>/dev/null | _comp_awk '
# Example output from systemctl:
# httpd.service loaded active running The Apache HTTP Server
# * iptables.service not-found inactive dead iptables.service
sub(/\.service$/, "", $1) { print $1; next }
sub(/\.service$/, "", $2) { print $2 }
')
_comp_compgen -a split -l -- "$_generated" && status=0

if [[ -x /sbin/upstart-udev-bridge ]]; then
_comp_split -la services "$(initctl list 2>/dev/null | cut -d' ' -f1)"
_comp_compgen -a split -l -- "$(initctl list 2>/dev/null | cut -d' ' -f1)" &&
status=0
fi

((${#services[@]})) || return 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a quick skim it seems removing this changes the behavior of _comp_compgen_services so that it no longer returns 1 when there are no services found. We don't seem to rely on that behavior in any in-tree uses, but should we not preserve it as the function is part of the API?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I checked the uses of this function inside bash-completion, but I didn't care about the possibility of external dependencies.

I updated it: c4c79ad..a02d487.

_comp_compgen -U services -U sysvdirs -- -W '"${services[@]#${sysvdirs[0]}/}"'
return "$status"
}

# This completes on a list of all available service scripts for the
Expand Down
6 changes: 3 additions & 3 deletions completions-core/invoke-rc.d.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _comp_cmd_invoke_rc_d()
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

local sysvdir services options
local sysvdir options

[[ -d /etc/rc.d/init.d ]] && sysvdir=/etc/rc.d/init.d ||
sysvdir=/etc/init.d
Expand All @@ -25,8 +25,8 @@ _comp_cmd_invoke_rc_d()
command grep -E -e "$regex_options" |
sort | uniq -u
)"
_comp_expand_glob services '"$sysvdir"/!(README*|*.sh|$_comp_backup_glob)' &&
_comp_compgen -a -- -W '"${services[@]#"$sysvdir"/}"'
# shellcheck disable=SC2154
_comp_compgen -aC "$sysvdir" -- -f -X "@(README*|*.sh|$_comp_backup_glob)"
elif [[ -x $sysvdir/$prev ]]; then
_comp_compgen_split -- "$(command sed -e 'y/|/ /' \
-ne 's/^.*Usage:[ ]*[^ ]*[ ]*{*\([^}"]*\).*$/\1/p' \
Expand Down
4 changes: 2 additions & 2 deletions completions-core/update-rc.d.bash
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ _comp_cmd_update_rc_d()
[[ -d /etc/rc.d/init.d ]] && sysvdir=/etc/rc.d/init.d ||
sysvdir=/etc/init.d

_comp_expand_glob services '"$sysvdir"/!(README*|*.sh|$_comp_backup_glob)' &&
services=("${services[@]#$sysvdir/}")
# shellcheck disable=SC2154
_comp_compgen -v services -C "$sysvdir" -- -f -X "@(README*|*.sh|$_comp_backup_glob)"
options=(-f -n)

if [[ $cword -eq 1 || $prev == -* ]]; then
Expand Down