Skip to content

Commit 790e524

Browse files
committed
fix(cryptsetup): parse --help and man for actions
1 parent c5cb01a commit 790e524

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

completions/cryptsetup

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,50 @@ _cryptsetup_device()
1111
_filedir
1212
}
1313

14+
_cryptsetup_action()
15+
{
16+
local ret IFS=$' \t\n'
17+
_comp_dequote "$1" || return 1
18+
local cmd=${ret:-cryptsetup}
19+
local actions=$(
20+
{
21+
LC_ALL=C "$cmd" --help 2>&1 |
22+
sed -n '/^<action> is one of:/,/^[^[:space:]]/s/^[[:space:]]\{1,\}\([^[:space:]]\{1,\}\).*/\1/p'
23+
LC_ALL=C man cryptsetup 2>&1 |
24+
awk '/^[[:space:]]+[[:alnum:]_]+([[:space:]]+(-[^[:space:].]+|<[^<>]+>|\[[^][]+\]|or))*$/ {print $1}'
25+
} | sort -u
26+
)
27+
28+
if [[ ! $actions ]]; then
29+
# The fallback action list is extracted from the following source:
30+
# https://gitlab.com/cryptsetup/cryptsetup/-/blob/main/src/cryptsetup.c#L3154-3208 (search for "Handle aliases")
31+
# https://gitlab.com/cryptsetup/cryptsetup/-/blob/main/src/cryptsetup.c#L2831-2867 (search for "struct actiontype")
32+
# https://gitlab.com/cryptsetup/cryptsetup/-/blob/main/src/cryptsetup_args.h#L28-53 (see the macros "*_ACTION")
33+
actions='benchmark bitlkClose bitlkDump bitlkOpen close config convert
34+
create erase isLuks loopaesClose loopaesOpen luksAddKey
35+
luksChangeKey luksClose luksConfig luksConvertKey luksDump
36+
luksErase luksFormat luksHeaderBackup luksHeaderRestore
37+
luksKillSlot luksOpen luksRemoveKey luksResume luksSuspend luksUUID
38+
open plainClose plainOpen reencrypt refresh remove repair resize
39+
status tcryptClose tcryptDump tcryptOpen token'
40+
41+
# We attempt to filter the supported actions by the strings in the binary.
42+
local path
43+
if path=$(type -P -- "$cmd" 2>/dev/null || type -P -- cryptsetup 2>/dev/null); then
44+
local filtering_pattern
45+
printf -v filtering_pattern '%s\n' $actions
46+
filtering_pattern=${filtering_pattern%$'\n'}
47+
48+
local filtered_actions
49+
filtered_actions=$(strings "$path" | grep -Fx "$filtering_pattern" | sort -u) &&
50+
[[ $filtered_actions ]] &&
51+
actions=$filtered_actions
52+
fi
53+
fi
54+
55+
COMPREPLY=($(compgen -W "$actions" -- "$cur"))
56+
}
57+
1458
_cryptsetup()
1559
{
1660
local cur prev words cword split
@@ -40,14 +84,7 @@ _cryptsetup()
4084
COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
4185
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
4286
else
43-
COMPREPLY=($(compgen -W 'benchmark bitlkClose bitlkDump bitlkOpen
44-
close config convert create erase isLuks loopaesClose
45-
loopaesOpen luksAddKey luksChangeKey luksClose luksConfig
46-
luksConvertKey luksDump luksErase luksFormat luksHeaderBackup
47-
luksHeaderRestore luksKillSlot luksOpen luksRemoveKey
48-
luksResume luksSuspend luksUUID open plainClose plainOpen
49-
reencrypt refresh remove repair resize status tcryptClose
50-
tcryptDump tcryptOpen token' -- "$cur"))
87+
_cryptsetup_action "$1"
5188
fi
5289
else
5390
local args

0 commit comments

Comments
 (0)