Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ SUBDIRS = gl lib src
ACLOCAL_AMFLAGS = -I m4


completionsdir = $(datadir)/bash-completion/completions
completions_DATA = \
completion/p11-common \
completion/p11cat \
completion/p11cp \
completion/p11kcv \
completion/p11ls \
completion/p11more \
completion/p11mv \
completion/p11od \
completion/p11setattr

EXTRA_DIST += $(completions_DATA)

install-exec-hook:
$(INSTALL) \
$(srcdir)/with_beid \
Expand Down
69 changes: 69 additions & 0 deletions completion/p11-common
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to have support for slot selection. That would require to modify p11slotinfo and add an option to enumerate slots and token, for completion.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Common functions for p11* completion scripts -*- shell-script -*-
#
# Description:
# This file provides reusable Bash functions to support autocompletion
# for various `p11*` CLI tools (p11cat, p11cp, p11mv, etc.).
#
# Functions parse the command-line arguments dynamically and provide
# object filtering and completion based on loaded tokens via `p11ls`.
#
# Functions:
# - __p11_parse_args(slot_ref, pin_ref, lib_ref)
# Parses -l (lib), -s (slot), -p (pin) from COMP_WORDS into referenced variables
#
# - __p11_complete_filters(cur)
# Uses parsed lib/slot/pin to call p11ls and generate autocompletion candidates
# with existing filters.
#
# Environment:
# These variables are used as fallback if -l, -s or -p are not given:
# PKCS11LIB, PKCS11SLOT, PKCS11PASSWORD
#
# Limitations:
#
# Inline environment variable assignments like the following:
# PKCS11LIB=/usr/lib/softhsm/libsofthsm2.so p11ls -s 0 -p 1234
#
# will NOT be detected by Bash completion logic, because these assignments
# are not part of the shell's environment when autocompletion is triggered.
#
# Instead, define them in the environment using `export`:
# export PKCS11LIB=/usr/lib/softhsm/libsofthsm2.so
# export PKCS11SLOT=0
# export PKCS11PASSWORD=1234
# p11ls <TAB>
#
# This ensures that completion functions can access the variables reliably.
#
# Usage:
# Must be sourced by specific completion scripts, e.g.:
# source /usr/share/bash-completion/completions/p11-common

__p11_parse_args() {
local -n _slot=$1 _pin=$2 _lib=$3
_slot=""; _pin=""; _lib=""

for ((i=0; i < ${#COMP_WORDS[@]}; i++)); do
case "${COMP_WORDS[i]}" in
-s) _slot="${COMP_WORDS[i+1]}" ;;
-p) _pin="${COMP_WORDS[i+1]}" ;;
-l) _lib="${COMP_WORDS[i+1]}" ;;
esac
done

# Fallback to env vars if CLI args not provided
[[ -z "$_lib" && -n "$PKCS11LIB" ]] && _lib="$PKCS11LIB"
[[ -z "$_slot" && -n "$PKCS11SLOT" ]] && _slot="$PKCS11SLOT"
[[ -z "$_pin" && -n "$PKCS11PASSWORD" ]] && _pin="$PKCS11PASSWORD"
}

__p11_complete_filters() {
local slot pin lib cur="$1"
__p11_parse_args slot pin lib

if [[ -n "$lib" && -n "$slot" && -n "$pin" ]]; then
local objects
objects=$(p11ls -l "$lib" -s "$slot" -p "$pin" 2>/dev/null | awk '{print $1}')
COMPREPLY=( $(compgen -W "${objects}" -- "${cur}") )
fi
}
21 changes: 21 additions & 0 deletions completion/p11cat
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# p11cat(1) completion -*- shell-script -*-
#
# Bash autocompletion for `p11cat`.
# Provides dynamic suggestions for PKCS#11 object filters
# based on current lib, slot, and pin arguments.
#
# Depends on:
# - ./p11-common
#
# Usage:
# Autocompletes when user types:
# p11cat -l <lib> -s <slot> -p <pin> <TAB>

source "$(dirname "${BASH_SOURCE[0]}")/p11-common"

_p11cat() {
local cur="${COMP_WORDS[COMP_CWORD]}"
__p11_complete_filters "$cur"
}

complete -F _p11cat p11cat
21 changes: 21 additions & 0 deletions completion/p11cp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# p11cp(1) completion -*- shell-script -*-
#
# Bash autocompletion for `p11cp`.
# Provides dynamic suggestions for the SOURCE argument
# based on current lib, slot, and pin arguments.
#
# Depends on:
# - ./p11-common
#
# Usage:
# Autocompletes when user types:
# p11cp -l <lib> -s <slot> -p <pin> <TAB>

source "$(dirname "${BASH_SOURCE[0]}")/p11-common"

_p11cp() {
local cur="${COMP_WORDS[COMP_CWORD]}"
__p11_complete_filters "$cur"
}

complete -F _p11cp p11cp
21 changes: 21 additions & 0 deletions completion/p11kcv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# p11kcv(1) completion -*- shell-script -*-
#
# Bash autocompletion for `p11kcv`.
# Provides dynamic suggestions for PKCS#11 object filters
# based on current lib, slot, and pin arguments.
#
# Depends on:
# - ./p11-common
#
# Usage:
# Autocompletes when user types:
# p11kcv -l <lib> -s <slot> -p <pin> <TAB>

source "$(dirname "${BASH_SOURCE[0]}")/p11-common"

_p11kcv() {
local cur="${COMP_WORDS[COMP_CWORD]}"
__p11_complete_filters "$cur"
}

complete -F _p11kcv p11kcv
21 changes: 21 additions & 0 deletions completion/p11ls
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# p11ls(1) completion -*- shell-script -*-
#
# Bash autocompletion for `p11ls`.
# Provides dynamic suggestions for PKCS#11 object filters
# based on current lib, slot, and pin arguments.
#
# Depends on:
# - ./p11-common
#
# Usage:
# Autocompletes when user types:
# p11ls -l <lib> -s <slot> -p <pin> <TAB>

source "$(dirname "${BASH_SOURCE[0]}")/p11-common"

_p11ls() {
local cur="${COMP_WORDS[COMP_CWORD]}"
__p11_complete_filters "$cur"
}

complete -F _p11ls p11ls
21 changes: 21 additions & 0 deletions completion/p11more
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# p11more(1) completion -*- shell-script -*-
#
# Bash autocompletion for `p11more`.
# Provides dynamic suggestions for PKCS#11 object filters
# based on current lib, slot, and pin arguments.
#
# Depends on:
# - ./p11-common
#
# Usage:
# Autocompletes when user types:
# p11more -l <lib> -s <slot> -p <pin> <TAB>

source "$(dirname "${BASH_SOURCE[0]}")/p11-common"

_p11more() {
local cur="${COMP_WORDS[COMP_CWORD]}"
__p11_complete_filters "$cur"
}

complete -F _p11more p11more
21 changes: 21 additions & 0 deletions completion/p11mv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# p11mv(1) completion -*- shell-script -*-
#
# Bash autocompletion for `p11mv`.
# Provides dynamic suggestions for the SOURCE argument
# based on current lib, slot, and pin arguments.
#
# Depends on:
# - ./p11-common
#
# Usage:
# Autocompletes when user types:
# p11mv -l <lib> -s <slot> -p <pin> <TAB>

source "$(dirname "${BASH_SOURCE[0]}")/p11-common"

_p11mv() {
local cur="${COMP_WORDS[COMP_CWORD]}"
__p11_complete_filters "$cur"
}

complete -F _p11mv p11mv
21 changes: 21 additions & 0 deletions completion/p11od
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# p11od(1) completion -*- shell-script -*-
#
# Bash autocompletion for `p11od`.
# Provides dynamic suggestions for PKCS#11 object filters
# based on current lib, slot, and pin arguments.
#
# Depends on:
# - ./p11-common
#
# Usage:
# Autocompletes when user types:
# p11od -l <lib> -s <slot> -p <pin> <TAB>

source "$(dirname "${BASH_SOURCE[0]}")/p11-common"

_p11od() {
local cur="${COMP_WORDS[COMP_CWORD]}"
__p11_complete_filters "$cur"
}

complete -F _p11od p11od
21 changes: 21 additions & 0 deletions completion/p11setattr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# p11setattr(1) completion -*- shell-script -*-
#
# Bash autocompletion for `p11setattr`.
# Provides dynamic suggestions for PKCS#11 object filters
# based on current lib, slot, and pin arguments.
#
# Depends on:
# - ./p11-common
#
# Usage:
# Autocompletes when user types:
# p11setattr -l <lib> -s <slot> -p <pin> <TAB>

source "$(dirname "${BASH_SOURCE[0]}")/p11-common"

_p11setattr() {
local cur="${COMP_WORDS[COMP_CWORD]}"
__p11_complete_filters "$cur"
}

complete -F _p11setattr p11setattr