Skip to content

feat: add bash/zsh completion for p11* tools#66

Open
mdevolde wants to merge 1 commit intoMastercard:masterfrom
mdevolde:completion
Open

feat: add bash/zsh completion for p11* tools#66
mdevolde wants to merge 1 commit intoMastercard:masterfrom
mdevolde:completion

Conversation

@mdevolde
Copy link
Copy Markdown

This PR adds bash completion support for several p11* CLI tools.

Features

  • Shared logic in completion/p11-common
  • Specific completions for: p11cat, p11cp, p11kcv, p11ls, p11more, p11mv, p11od, p11setattr
  • Supports PKCS11LIB, PKCS11SLOT, PKCS11PASSWORD as environment fallback
  • Documented usage, environment handling, and limitations

Makefile

  • Adds completion scripts to EXTRA_DIST
  • Installs them to ${datadir}/bash-completion/completions

Limitations

  • Does not support inline env variables (e.g. PKCS11LIB=... p11cat)
  • Zsh is not yet supported, could be addressed in a later contribution

@keldonin keldonin requested a review from Copilot June 25, 2025 14:01
@keldonin keldonin added the enhancement New feature or request label Jun 25, 2025
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds Bash autocompletion support for various p11* CLI tools by introducing a shared common script and individual completion scripts, and updating the Makefile.am to install them.

  • Introduce completion/p11-common with reusable parsing and completion functions.
  • Add dedicated completion scripts for p11cat, p11cp, p11kcv, p11ls, p11more, p11mv, p11od, and p11setattr.
  • Update Makefile.am to include these scripts in EXTRA_DIST and install them under bash-completion/completions.

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
completion/p11-common Shared functions for parsing -l, -s, -p and completing filters
completion/p11cat Autocompletion for p11cat
completion/p11cp Autocompletion for p11cp
completion/p11kcv Autocompletion for p11kcv
completion/p11ls Autocompletion for p11ls
completion/p11more Autocompletion for p11more
completion/p11mv Autocompletion for p11mv
completion/p11od Autocompletion for p11od
completion/p11setattr Autocompletion for p11setattr
Makefile.am Registers and installs completion scripts
Comments suppressed due to low confidence (2)

completion/p11mv:3

  • The comment refers to p11cp but this is the p11mv completion script; update it to p11mv.
# Bash autocompletion for `p11cp`.

completion/p11-common:60

  • Consider adding unit or integration tests for __p11_complete_filters to ensure completion logic works correctly across different argument combinations.
__p11_complete_filters() {

Comment thread completion/p11-common Outdated
Comment thread Makefile.am Outdated
@keldonin keldonin self-assigned this Nov 25, 2025
Comment thread completion/bash/p11-common
Copy link
Copy Markdown
Contributor

@keldonin keldonin left a comment

Choose a reason for hiding this comment

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

Good stuff.

A few requests:

  • It should support ZSH as well
  • If possible, infer slots as well - that would require to adapt p11slotinfoto yield a list of slots and token, for completion.

@mdevolde mdevolde marked this pull request as draft November 26, 2025 13:37
@mdevolde mdevolde force-pushed the completion branch 3 times, most recently from ceb7a20 to 3cd0cb7 Compare December 23, 2025 23:29
@mdevolde
Copy link
Copy Markdown
Author

mdevolde commented Dec 23, 2025

I added:

  • Completion on all p11 commands
  • Slot completion
  • Token label completion
  • Added a dedicated func in p11slotinfo to handle slot and token completion
  • Completion of -i and -w parameters with PKCS11 objects
  • Consideration of token labels for PKCS11 object completion
  • Added support for PKCS11TOKENLABEL as env fallback
  • Basic file completion for relevant parameters (e.g., -l or -o)
  • Completion of certain parameters that only accept a predefined list of strings (such as -k on p11keygen, for example).
  • Completion of parameters after writing a -.

I did not add completion for:

  • -b (p11kcv, p11keygen), -n (p11kcv), -c (p11keycomp), -u (p11mkcert), -e (p11keygen) -> These parameters are just numbers, for which completion is not relevant.
  • -p (all), -J (p11keygen, p11rewrap, p11wrap) -> These parameters are PINs or tokens, for which completion is not possible.
  • -d (p11mkcert, p11req), -W (p11keygen, p11rewrap, p11wrap) -> Since the user must enter a custom and structured string himself, completion is not relevant.

I wonder if it is relevant to do completion for:

  • -m (all) -> Should I just suggest completion with directories, or should I suggest completion with NSS prefixes before directories?
  • -a (p11wrap) -> There is a given list of accepted algorithms, but some accept parameters, and these parameters cannot be completed. Should I just provide the algorithms, without the parameters?
  • -e (p11mkcert, p11req) -> There are three accepted prefixes (DNS, email, IP), followed by a value decided by the user. Should I provide completion for these prefixes?

For now, it's still just completion on bash, not zsh.

I also fixed an error in the README, where an obsolete environment variable was listed.

@mdevolde mdevolde marked this pull request as ready for review December 26, 2025 23:31
@mdevolde
Copy link
Copy Markdown
Author

Since my last update, I have:

  • Added directory completion for -m.
  • Added completion for -a algorithms (p11wrap), without adding extra space after, to allow for opening parentheses and specifying parameters.
  • Added prefix completion for -e (p11mkcert, p11req), without adding an extra space after, to allow specifying a value.

I also added ZSH compatibility for completions.
Currently, bash completions are used by ZSH. To get more advanced ZSH completion features, such as descriptions, you need to write additional dedicated scripts.

@mdevolde mdevolde requested a review from keldonin December 26, 2025 23:38
@mdevolde mdevolde changed the title feat: add bash completion for p11* tools feat: add bash/zsh completion for p11* tools Dec 27, 2025
@mdevolde
Copy link
Copy Markdown
Author

Noticed and fixed a compatibility bug for ZSH with bashcompinit (local -n).

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 27 out of 27 changed files in this pull request and generated 9 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/p11slotinfo.c Outdated
Comment thread src/p11slotinfo.c
Comment thread lib/pkcs11_slotinfo.c
Comment thread lib/pkcs11_slotinfo.c Outdated
Comment thread completion/bash/p11-common Outdated
Comment thread completion/bash/p11-common Outdated
Comment thread Makefile.am Outdated
Comment thread completion/zsh/_p11tools.in
Comment thread completion/zsh/_p11tools.in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants