[PATCH] Added option to prompt for passwords

From: Eric Pruitt <eric.pruitt_AT_gmail.com>
Date: Mon, 25 Jul 2016 11:38:19 -0700

---
 dmenu.1 |  3 +++
 dmenu.c | 42 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 39 insertions(+), 6 deletions(-)
diff --git a/dmenu.1 b/dmenu.1
index d3ab805..91b5a5b 100644
--- a/dmenu.1
+++ b/dmenu.1
_AT_@ -45,6 +45,9 @@ dmenu appears at the bottom of the screen.
 dmenu grabs the keyboard before reading stdin.  This is faster, but will lock up
 X until stdin reaches end\-of\-file.
 .TP
+.B \-g
+prompt for password.  Typed text is displayed as asterisks and stdin is ignored.
+.TP
 .B \-i
 dmenu matches menu items case insensitively.
 .TP
diff --git a/dmenu.c b/dmenu.c
index 8e84fbd..d28e656 100644
--- a/dmenu.c
+++ b/dmenu.c
_AT_@ -33,7 +33,9 @@ struct item {
 	int out;
 };
 
+static char asterisks[BUFSIZ] = "";
 static char text[BUFSIZ] = "";
+static char passwordentry = 0;
 static int bh, mw, mh;
 static int sw, sh; /* X display screen geometry width, height */
 static int inputw = 0, promptw;
_AT_@ -56,6 +58,7 @@ static Clr *scheme[SchemeLast];
 
 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
 static char *(*fstrstr)(const char *, const char *) = strstr;
+static size_t nextrune(int inc);
 
 static void
 appenditem(struct item *item, struct item **list, struct item **last)
_AT_@ -128,9 +131,30 @@ drawitem(struct item *item, int x, int y, int w)
 static void
 drawmenu(void)
 {
+	char *textbuf = text;
 	unsigned int curpos;
+	size_t visualcursor = cursor;
+	size_t oldcursor, runecount;
 	struct item *item;
-	int x = 0, y = 0, w;
+	int visualcursorset = 0, x = 0, y = 0, w;
+
+	if (passwordentry) {
+		oldcursor = cursor;
+		for (cursor = runecount = 0; text[cursor] != '\0'; runecount++) {
+			asterisks[runecount] = '*';
+			if (cursor >= oldcursor && !visualcursorset) {
+				visualcursorset = 1;
+				visualcursor = runecount;
+			}
+			cursor = nextrune(+1);
+		}
+		if (!visualcursorset) {
+			visualcursor = runecount;
+		}
+		cursor = oldcursor;
+		asterisks[runecount] = '\0';
+		textbuf = asterisks;
+	}
 
 	drw_setscheme(drw, scheme[SchemeNorm]);
 	drw_rect(drw, 0, 0, mw, mh, 1, 1);
_AT_@ -142,9 +166,9 @@ drawmenu(void)
 	/* draw input field */
 	w = (lines > 0 || !matches) ? mw - x : inputw;
 	drw_setscheme(drw, scheme[SchemeNorm]);
-	drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0);
+	drw_text(drw, x, 0, w, bh, lrpad / 2, textbuf, 0);
 
-	drw_font_getexts(drw->fonts, text, cursor, &curpos, NULL);
+	drw_font_getexts(drw->fonts, textbuf, visualcursor, &curpos, NULL);
 	if ((curpos += lrpad / 2 - 1) < w) {
 		drw_setscheme(drw, scheme[SchemeNorm]);
 		drw_rect(drw, x + curpos, 2, 2, bh - 4, 1, 0);
_AT_@ -600,7 +624,7 @@ setup(void)
 static void
 usage(void)
 {
-	fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
+	fputs("usage: dmenu [-b] [-f] [-g] [-i] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
 	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
 	exit(1);
 }
_AT_@ -619,6 +643,8 @@ main(int argc, char *argv[])
 			topbar = 0;
 		else if (!strcmp(argv[i], "-f"))   /* grabs keyboard before reading stdin */
 			fast = 1;
+		else if (!strcmp(argv[i], "-g"))   /* get password*/
+			passwordentry = 1;
 		else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
 			fstrncmp = strncasecmp;
 			fstrstr = cistrstr;
_AT_@ -659,9 +685,13 @@ main(int argc, char *argv[])
 
 	if (fast) {
 		grabkeyboard();
-		readstdin();
+		if (!passwordentry) {
+			readstdin();
+		}
 	} else {
-		readstdin();
+		if (!passwordentry) {
+			readstdin();
+		}
 		grabkeyboard();
 	}
 	setup();
-- 
2.1.4
--1yeeQ81UyVL57Vl7--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 25 2016 - 21:36:04 CEST