|
| 1 | +# Common functions for p11* completion scripts -*- shell-script -*- |
| 2 | +# |
| 3 | +# Description: |
| 4 | +# This file provides reusable Bash functions to support autocompletion |
| 5 | +# for various `p11*` CLI tools (p11cat, p11cp, p11mv, etc.). |
| 6 | +# |
| 7 | +# Functions parse the command-line arguments dynamically and provide |
| 8 | +# object filtering and completion based on loaded tokens via `p11ls`. |
| 9 | +# |
| 10 | +# Functions: |
| 11 | +# - __p11_parse_args(slot_ref, pin_ref, lib_ref) |
| 12 | +# Parses -l (lib), -s (slot), -p (pin) from COMP_WORDS into referenced variables |
| 13 | +# |
| 14 | +# - __p11_complete_slots(cur) |
| 15 | +# Uses parsed lib to call p11slotinfo with -L and generate |
| 16 | +# autocompletion candidates for slot numbers used with -s. |
| 17 | +# |
| 18 | +# - __p11_complete_filters(cur) |
| 19 | +# Uses parsed lib/slot/pin to call p11ls and generate autocompletion candidates |
| 20 | +# with existing filters. |
| 21 | +# |
| 22 | +# Environment: |
| 23 | +# These variables are used as fallback if -l, -s or -p are not given: |
| 24 | +# PKCS11LIB, PKCS11SLOT, PKCS11PASSWORD |
| 25 | +# |
| 26 | +# Limitations: |
| 27 | +# |
| 28 | +# Inline environment variable assignments like the following: |
| 29 | +# PKCS11LIB=/usr/lib/softhsm/libsofthsm2.so p11ls -s 0 -p 1234 |
| 30 | +# |
| 31 | +# will NOT be detected by Bash completion logic, because these assignments |
| 32 | +# are not part of the shell's environment when autocompletion is triggered. |
| 33 | +# |
| 34 | +# Instead, define them in the environment using `export`: |
| 35 | +# export PKCS11LIB=/usr/lib/softhsm/libsofthsm2.so |
| 36 | +# export PKCS11SLOT=0 |
| 37 | +# export PKCS11PASSWORD=1234 |
| 38 | +# p11ls <TAB> |
| 39 | +# |
| 40 | +# This ensures that completion functions can access the variables reliably. |
| 41 | +# |
| 42 | +# Usage: |
| 43 | +# Must be sourced by specific completion scripts, e.g.: |
| 44 | +# source /usr/share/bash-completion/completions/p11-common |
| 45 | + |
| 46 | +__p11_parse_args() { |
| 47 | + local -n _slot=$1 _pin=$2 _lib=$3 |
| 48 | + _slot=""; _pin=""; _lib="" |
| 49 | + |
| 50 | + for ((i=0; i < ${#COMP_WORDS[@]}; i++)); do |
| 51 | + case "${COMP_WORDS[i]}" in |
| 52 | + -s) _slot="${COMP_WORDS[i+1]}" ;; |
| 53 | + -p) _pin="${COMP_WORDS[i+1]}" ;; |
| 54 | + -l) _lib="${COMP_WORDS[i+1]}" ;; |
| 55 | + esac |
| 56 | + done |
| 57 | + |
| 58 | + # Fallback to env vars if CLI args not provided |
| 59 | + [[ -z "$_lib" && -n "$PKCS11LIB" ]] && _lib="$PKCS11LIB" |
| 60 | + [[ -z "$_slot" && -n "$PKCS11SLOT" ]] && _slot="$PKCS11SLOT" |
| 61 | + [[ -z "$_pin" && -n "$PKCS11PASSWORD" ]] && _pin="$PKCS11PASSWORD" |
| 62 | +} |
| 63 | + |
| 64 | +__p11_complete_slots() { |
| 65 | + local cur="$1" |
| 66 | + local slot pin lib |
| 67 | + __p11_parse_args slot pin lib |
| 68 | + |
| 69 | + if [[ -n "$lib" ]]; then |
| 70 | + local slots |
| 71 | + slots=$(p11slotinfo -l "$lib" -L 2>/dev/null | awk -F: '{print $1}') |
| 72 | + COMPREPLY=( $(compgen -W "${slots}" -- "${cur}") ) |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +__p11_complete_filters() { |
| 78 | + local slot pin lib cur="$1" |
| 79 | + __p11_parse_args slot pin lib |
| 80 | + |
| 81 | + if [[ -n "$lib" && -n "$slot" && -n "$pin" ]]; then |
| 82 | + local objects |
| 83 | + objects=$(p11ls -l "$lib" -s "$slot" -p "$pin" 2>/dev/null | awk '{print $1}') |
| 84 | + COMPREPLY=( $(compgen -W "${objects}" -- "${cur}") ) |
| 85 | + fi |
| 86 | +} |
0 commit comments