Skip to content

Commit

Permalink
_pids, _pgids, _pnames: improve shfmt formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed May 23, 2020
1 parent b4490f2 commit 2bfb0a7
Showing 1 changed file with 54 additions and 58 deletions.
112 changes: 54 additions & 58 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -1098,82 +1098,78 @@ _expand()
esac
}

# This function completes on process IDs.
# AIX and Solaris ps prefers X/Open syntax.
[[ $OSTYPE == *@(solaris|aix)* ]] &&
# Process ID related functions.
# for AIX and Solaris we use X/Open syntax, BSD for others.
if [[ $OSTYPE == *@(solaris|aix)* ]]; then
# This function completes on process IDs.
_pids()
{
COMPREPLY=($(compgen -W '$(command ps -efo pid | command sed 1d)' -- "$cur"))
} ||
_pids()
{
COMPREPLY=($(compgen -W '$(command ps axo pid=)' -- "$cur"))
}
}

# This function completes on process group IDs.
# AIX and SunOS prefer X/Open, all else should be BSD.
[[ $OSTYPE == *@(solaris|aix)* ]] &&
_pgids()
{
COMPREPLY=($(compgen -W '$(command ps -efo pgid | command sed 1d)' -- "$cur"))
} ||
_pgids()
{
COMPREPLY=($(compgen -W '$(command ps axo pgid=)' -- "$cur"))
}

# This function completes on process names.
# AIX and SunOS prefer X/Open, all else should be BSD.
# @param $1 if -s, don't try to avoid truncated command names
[[ $OSTYPE == *@(solaris|aix)* ]] &&
}
_pnames()
{
COMPREPLY=($(compgen -X '<defunct>' -W '$(command ps -efo comm | \
command sed -e 1d -e "s:.*/::" -e "s/^-//" | sort -u)' -- "$cur"))
} ||
_pnames()
}
else
_pids()
{
local -a procs
if [[ ${1-} == -s ]]; then
procs=($(command ps axo comm | command sed -e 1d))
else
local line i=-1 ifs=$IFS
IFS=$'\n'
local -a psout=($(command ps axo command=))
IFS=$ifs
COMPREPLY=($(compgen -W '$(command ps axo pid=)' -- "$cur"))
}
_pgids()
{
COMPREPLY=($(compgen -W '$(command ps axo pgid=)' -- "$cur"))
}
# @param $1 if -s, don't try to avoid truncated command names
_pnames()
{
local -a procs
if [[ ${1-} == -s ]]; then
procs=($(command ps axo comm | command sed -e 1d))
else
local line i=-1 ifs=$IFS
IFS=$'\n'
local -a psout=($(command ps axo command=))
IFS=$ifs
for line in "${psout[@]}"; do
if ((i == -1)); then
# First line, see if it has COMMAND column header. For example
# the busybox ps does that, i.e. doesn't respect axo command=
if [[ $line =~ ^(.*[[:space:]])COMMAND([[:space:]]|$) ]]; then
# It does; store its index.
i=${#BASH_REMATCH[1]}
else
# Nope, fall through to "regular axo command=" parsing.
break
fi
else
#
line=${line:i} # take command starting from found index
line=${line%% *} # trim arguments
procs+=($line)
fi
done
if ((i == -1)); then
# Regular axo command= parsing
for line in "${psout[@]}"; do
if ((i == -1)); then
# First line, see if it has COMMAND column header. For example
# the busybox ps does that, i.e. doesn't respect axo command=
if [[ $line =~ ^(.*[[:space:]])COMMAND([[:space:]]|$) ]]; then
# It does; store its index.
i=${#BASH_REMATCH[1]}
else
# Nope, fall through to "regular axo command=" parsing.
break
fi
if [[ $line =~ ^[[(](.+)[])]$ ]]; then
procs+=(${BASH_REMATCH[1]})
else
#
line=${line:i} # take command starting from found index
line=${line%% *} # trim arguments
line=${line%% *} # trim arguments
line=${line##@(*/|-)} # trim leading path and -
procs+=($line)
fi
done
if ((i == -1)); then
# Regular axo command= parsing
for line in "${psout[@]}"; do
if [[ $line =~ ^[[(](.+)[])]$ ]]; then
procs+=(${BASH_REMATCH[1]})
else
line=${line%% *} # trim arguments
line=${line##@(*/|-)} # trim leading path and -
procs+=($line)
fi
done
fi
fi
COMPREPLY=($(compgen -X "<defunct>" -W '${procs[@]}' -- "$cur"))
}
fi
COMPREPLY=($(compgen -X "<defunct>" -W '${procs[@]}' -- "$cur"))
}
fi

# This function completes on user IDs
#
Expand Down

0 comments on commit 2bfb0a7

Please sign in to comment.