Skip to content

Commit

Permalink
fix(cryptsetup): parse --help and man for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jun 17, 2022
1 parent c5cb01a commit 9dd9fb5
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions completions/cryptsetup
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,50 @@ _cryptsetup_device()
_filedir
}

_cryptsetup_action()
{
local ret IFS=$' \t\n'
_comp_dequote "$1" || return 1
local cmd=${ret:-cryptsetup}
local actions=$(
{
LC_ALL=C "$cmd" --help 2>&1 |
sed -n '/^<action> is one of:/,/^[^[:space:]]/s/^[[:space:]]\{1,\}\([^[:space:]]\{1,\}\).*/\1/p'
LC_ALL=C man cryptsetup 2>&1 |
awk '/^[[:space:]]+[[:alnum:]_]+([[:space:]]+(-[^[:space:].]+|<[^<>]+>|\[[^][]+\]|or))*$/ {print $1}'
} | sort -u
)

if [[ ! $actions ]]; then
# The fallback action list is extracted from the following source:
# https://gitlab.com/cryptsetup/cryptsetup/-/blob/main/src/cryptsetup.c#L3154-3208 (search for "Handle aliases")
# https://gitlab.com/cryptsetup/cryptsetup/-/blob/main/src/cryptsetup.c#L2831-2867 (search for "struct action_type")
# https://gitlab.com/cryptsetup/cryptsetup/-/blob/main/src/cryptsetup_args.h#L28-53 (see the macros "*_ACTION")
actions='benchmark bitlkClose bitlkDump bitlkOpen close config convert
create erase isLuks loopaesClose loopaesOpen luksAddKey
luksChangeKey luksClose luksConfig luksConvertKey luksDump
luksErase luksFormat luksHeaderBackup luksHeaderRestore
luksKillSlot luksOpen luksRemoveKey luksResume luksSuspend luksUUID
open plainClose plainOpen reencrypt refresh remove repair resize
status tcryptClose tcryptDump tcryptOpen token'

# We attempt to filter the supported actions by the strings in the binary.
local path
if path=$(type -P -- "$cmd" 2>/dev/null || type -P -- cryptsetup 2>/dev/null); then
local filtering_pattern
printf -v filtering_pattern '%s\n' $actions
filtering_pattern=${filtering_pattern%$'\n'}

local filtered_actions
filtered_actions=$(strings "$path" | grep -Fx "$filtering_pattern" | sort -u) &&
[[ $filtered_actions ]] &&
actions=$filtered_actions
fi
fi

COMPREPLY=($(compgen -W "$actions" -- "$cur"))
}

_cryptsetup()
{
local cur prev words cword split
Expand Down Expand Up @@ -40,14 +84,7 @@ _cryptsetup()
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
else
COMPREPLY=($(compgen -W 'benchmark bitlkClose bitlkDump bitlkOpen
close config convert create erase isLuks loopaesClose
loopaesOpen luksAddKey luksChangeKey luksClose luksConfig
luksConvertKey luksDump luksErase luksFormat luksHeaderBackup
luksHeaderRestore luksKillSlot luksOpen luksRemoveKey
luksResume luksSuspend luksUUID open plainClose plainOpen
reencrypt refresh remove repair resize status tcryptClose
tcryptDump tcryptOpen token' -- "$cur"))
_cryptsetup_action "$1"
fi
else
local args
Expand Down

0 comments on commit 9dd9fb5

Please sign in to comment.