Skip to content

Commit 4874dd1

Browse files
committed
fix(cryptsetup): parse --help and man for actions
1 parent 11b4af2 commit 4874dd1

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
@@ -10,6 +10,50 @@ _comp_cmd_cryptsetup__device()
1010
_comp_compgen -c "${cur:-/dev/}" filedir
1111
}
1212

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

0 commit comments

Comments
 (0)