[PATCH] Enable the use of regex in show command

mmh at riseup.net mmh at riseup.net
Thu Dec 5 18:50:37 CET 2019


From: mmh <mmh at riseup.net>

Instead of line numbers regular expressions can be passed to --clip and
--qrcode as well. Any string which contains characters other than digits
is interpretd as a regular expression. The decrypted password file in
question is then piped through sed using "^regex *" as a pattern and the
first line found is clipped/qrencoded.

Signed-off-by: mmh <mmh at riseup.net>
---
 man/pass.1            |  7 +++++--
 src/password-store.sh | 11 +++++++----
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/man/pass.1 b/man/pass.1
index a555dcb..6bcdae6 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -94,7 +94,7 @@ List names of passwords inside the tree that match \fIpass-names\fP by using the
 .BR tree (1)
 program. This command is alternatively named \fBsearch\fP.
 .TP
-\fBshow\fP [ \fI--clip\fP[=\fIline-number\fP], \fI-c\fP[\fIline-number\fP] ] [ \fI--qrcode\fP[=\fIline-number\fP], \fI-q\fP[\fIline-number\fP] ] \fIpass-name\fP
+\fBshow\fP [ \fI--clip\fP[=\fIline-number\fP or \fIregex\fP], \fI-c\fP[\fIline-number\fP or \fIregex\fP] ] [ \fI--qrcode\fP[=\fIline-number\fP or \fIregex\fP], \fI-q\fP[\fIline-number\fP or \fIregex\fP] ] \fIpass-name\fP
 Decrypt and print a password named \fIpass-name\fP. If \fI--clip\fP or \fI-c\fP
 is specified, do not print the password but instead copy the first (or otherwise specified)
 line to the clipboard using
@@ -104,7 +104,9 @@ or
 and then restore the clipboard after 45 (or \fIPASSWORD_STORE_CLIP_TIME\fP) seconds. If \fI--qrcode\fP
 or \fI-q\fP is specified, do not print the password but instead display a QR code using
 .BR qrencode (1)
-either to the terminal or graphically if supported.
+either to the terminal or graphically if supported. If the argument to \fI--clip\fP or \fI--qrcode\fP contains other characters than digits it is interpreted as a regex rather than a line-number and it is passed to
+.BR sed (1)
+as "^regex *". The regex must not contain "|".
 .TP
 \fBinsert\fP [ \fI--echo\fP, \fI-e\fP | \fI--multiline\fP, \fI-m\fP ] [ \fI--force\fP, \fI-f\fP ] \fIpass-name\fP
 Insert a new password into the password store called \fIpass-name\fP. This will
@@ -467,6 +469,7 @@ The \fBinit\fP command will keep signatures of \fB.gpg-id\fP files up to date.
 The location of the text editor used by \fBedit\fP.
 .SH SEE ALSO
 .BR gpg2 (1),
+.BR sed (1),
 .BR tr (1),
 .BR git (1),
 .BR xclip (1),
diff --git a/src/password-store.sh b/src/password-store.sh
index 77f3eda..c435c40 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -282,7 +282,7 @@ cmd_usage() {
 	        List passwords.
 	    $PROGRAM find pass-names...
 	    	List passwords that match pass-names.
-	    $PROGRAM [show] [--clip[=line-number],-c[line-number]] pass-name
+	    $PROGRAM [show] [--clip[=line-number or regex],-c[line-number or regex]] [--qrcode[=line-number or regex],-q[line-number or regex]] pass-name
 	        Show existing password and optionally put it on the clipboard.
 	        If put on the clipboard, it will be cleared in $CLIP_TIME seconds.
 	    $PROGRAM grep [GREPOPTIONS] search-string
@@ -374,7 +374,7 @@ cmd_show() {
 		--) shift; break ;;
 	esac done
 
-	[[ $err -ne 0 || ( $qrcode -eq 1 && $clip -eq 1 ) ]] && die "Usage: $PROGRAM $COMMAND [--clip[=line-number],-c[line-number]] [--qrcode[=line-number],-q[line-number]] [pass-name]"
+	[[ $err -ne 0 || ( $qrcode -eq 1 && $clip -eq 1 ) ]] && die "Usage: $PROGRAM $COMMAND [--clip[=line-number or regex],-c[line-number or regex]] [--qrcode[=line-number or regex],-q[line-number or regex]] [pass-name]"
 
 	local pass
 	local path="$1"
@@ -385,8 +385,11 @@ cmd_show() {
 			pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | $BASE64)" || exit $?
 			echo "$pass" | $BASE64 -d
 		else
-			[[ $selected_line =~ ^[0-9]+$ ]] || die "Clip location '$selected_line' is not a number."
-			pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +${selected_line} | head -n 1)" || exit $?
+			if [[ $selected_line =~ ^[0-9]+$ ]]; then
+				pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +${selected_line} | head -n 1)" || exit $?
+			else
+				pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | sed -n "s|^${selected_line} *||p" | head -n 1)" || exit $?
+			fi
 			[[ -n $pass ]] || die "There is no password to put on the clipboard at line ${selected_line}."
 			if [[ $clip -eq 1 ]]; then
 				clip "$pass" "$path"
-- 
2.24.0



More information about the Password-Store mailing list