Skip to content

Commit 54c268d

Browse files
committed
fix(cryptsetup): parse --help and man for actions
1 parent f3469c0 commit 54c268d

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

completions/cryptsetup

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,51 @@ _comp_cmd_cryptsetup__device()
1010
_comp_compgen -c "${cur:-/dev/}" filedir
1111
}
1212

13+
_comp_cmd_cryptsetup__action()
14+
{
15+
local REPLY IFS=$' \t\n'
16+
_comp_dequote "$1" || return 1
17+
local cmd=${REPLY:-cryptsetup}
18+
local -a actions
19+
_comp_split -l 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[@]} == 0)); 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 action_type")
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+
_comp_split -l actions "$filtered_actions"
52+
fi
53+
fi
54+
55+
_comp_compgen -- -W '"${actions[@]}"'
56+
}
57+
1358
_comp_cmd_cryptsetup()
1459
{
1560
local cur prev words cword was_split comp_args
@@ -91,14 +136,7 @@ _comp_cmd_cryptsetup()
91136
_comp_compgen_help
92137
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
93138
else
94-
_comp_compgen -- -W 'benchmark bitlkClose bitlkDump bitlkOpen close
95-
config convert create erase isLuks loopaesClose loopaesOpen
96-
luksAddKey luksChangeKey luksClose luksConfig luksConvertKey
97-
luksDump luksErase luksFormat luksHeaderBackup
98-
luksHeaderRestore luksKillSlot luksOpen luksRemoveKey
99-
luksResume luksSuspend luksUUID open plainClose plainOpen
100-
reencrypt refresh remove repair resize status tcryptClose
101-
tcryptDump tcryptOpen token'
139+
_comp_cmd_cryptsetup__action "$1"
102140
fi
103141
fi
104142

0 commit comments

Comments
 (0)