Skip to content

Instantly share code, notes, and snippets.

@kuvaldini
Last active May 10, 2026 20:23
Show Gist options
  • Select an option

  • Save kuvaldini/d97c039d9338be575ef757d206016000 to your computer and use it in GitHub Desktop.

Select an option

Save kuvaldini/d97c039d9338be575ef757d206016000 to your computer and use it in GitHub Desktop.
gpg-pinentry-inter
#!/usr/bin/env fish
###########################################
## file: bin/gpg-pinentry-inter ##
## author: kuvaldini at gihub.com ##
## license: CC BY, pls credit the author ##
###########################################
##
## DESCRIPTION
##
## gpg-pinentry-inter is small script - wrapper-selector around actual pinentry program
## finds if gpg process called this pinentry instance has active foreground tty, then
## chains request to pinenty-tty with modified option ttyname;
## else if tty not available for gpg, it is likely called from GUI or via
## non-interactive script, thus calling pinentry-gui.
##
set pine_tty /usr/bin/pinentry-tty
set pine_gui /usr/bin/pinentry-qt
#set pine_tty /usr/bin/pinentry-curses
#set pine_gui /usr/bin/pinentry-gtk3
function tty_of_yongest_gpg_wtih_active_fg_tty
#DEBUG ps ef -Cgpg &>/tmp/pine
ps --sort=-pid -C gpg -opid,tty,stat |
awk '$3!~/Z/ && $3~/\+/ {print $2; found=1;exit} END {exit !found}'
end
if set gpg_active_tty (tty_of_yongest_gpg_wtih_active_fg_tty)
sed -uE "s,(OPTION ttyname=/dev/).*,\1$gpg_active_tty," |
$pine_tty $argv
#DEBUG tee /tmp/pinin | /usr/bin/pinentry-tty $argv |tee /tmp/pinout
else
exec $pine_gui $argv
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment