Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: read -s



Oliver Sturm wrote:
> In bash, I can do "read -s" to emulate a password prompt without any
> echoing.

This is useful and fairly easy, except for the fact that the read
builtin is implemented as a hotch-potch of semi-incompatible and
inconsistent options, which is why I couldn't be bothered to spend the
time to get `-sq' to work together.  It does work with -k; I haven't
attempted from within zle.

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.80
diff -u -r1.80 builtin.c
--- Src/builtin.c	5 Aug 2002 12:35:59 -0000	1.80
+++ Src/builtin.c	14 Aug 2002 15:11:19 -0000
@@ -100,7 +100,7 @@
     BUILTIN("pushln", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, NULL, "-nz"),
     BUILTIN("pwd", 0, bin_pwd, 0, 0, 0, "rLP", NULL),
     BUILTIN("r", BINF_R, bin_fc, 0, -1, BIN_FC, "nrl", NULL),
-    BUILTIN("read", 0, bin_read, 0, -1, 0, "ceklnpqrtzuAE0123456789", NULL),
+    BUILTIN("read", 0, bin_read, 0, -1, 0, "ceklnpqrstzuAE0123456789", NULL),
     BUILTIN("readonly", BINF_TYPEOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL, bin_typeset, 0, -1, 0, "AEFHLRTUZafghilptux", "r"),
     BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "df", "r"),
     BUILTIN("return", BINF_PSPECIAL, bin_break, 0, 1, BIN_RETURN, NULL, NULL),
@@ -3882,7 +3882,8 @@
     char *buf, *bptr, *firstarg, *zbuforig;
     LinkList readll = newlinklist();
     FILE *oshout = NULL;
-    int readchar = -1, val;
+    int readchar = -1, val, resettty = 0;
+    struct ttyinfo saveti;
     char d;
 
 
@@ -3955,6 +3956,18 @@
 	}
 	return 1;
     }
+    if (ops['s'] && SHTTY != -1) {
+	struct ttyinfo ti;
+	gettyinfo(&ti);
+	saveti = ti;
+#ifdef HAS_TIO
+	ti.tio.c_lflag &= ~ECHO;
+#else
+	ti.sgttyb.sg_flags &= ~ECHO;
+#endif
+	settyinfo(&ti);
+	resettty = 1;
+    }
 
     /* handle prompt */
     if (firstarg) {
@@ -4001,8 +4014,10 @@
 	    /* dispose of result appropriately, etc. */
 	    if (isem)
 		while (val > 0 && read(SHTTY, &d, 1) == 1 && d != '\n');
-	    else
+	    else {
 		settyinfo(&shttyinfo);
+		resettty = 0;
+	    }
 	    if (haso) {
 		fclose(shout);	/* close(SHTTY) */
 		shout = oshout;
@@ -4016,6 +4031,8 @@
 	    setsparam(reply, metafy(buf, bptr - buf, META_REALLOC));
 	else
 	    zfree(buf, bptr - buf + 1);
+	if (resettty && SHTTY != -1)
+	    settyinfo(&saveti);
 	return val <= 0;
     }
 
@@ -4046,6 +4063,8 @@
 	if (!ops['e'])
 	    setsparam(reply, ztrdup(readbuf));
 
+	if (resettty && SHTTY != -1)
+	    settyinfo(&saveti);
 	return readbuf[0] == 'n';
     }
 
@@ -4156,6 +4175,8 @@
 	    *pp++ = NULL;
 	    setaparam(reply, p);
 	}
+	if (resettty && SHTTY != -1)
+	    settyinfo(&saveti);
 	return c == EOF;
     }
     buf = bptr = (char *)zalloc(bsiz = 64);
@@ -4202,6 +4223,8 @@
     while (bptr > buf && iwsep(bptr[-1]))
 	bptr--;
     *bptr = '\0';
+    if (resettty && SHTTY != -1)
+	settyinfo(&saveti);
     /* final assignment of reply, etc. */
     if (ops['e'] || ops['E']) {
 	zputs(buf, stdout);
Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.48
diff -u -r1.48 builtins.yo
--- Doc/Zsh/builtins.yo	6 Aug 2002 13:05:51 -0000	1.48
+++ Doc/Zsh/builtins.yo	14 Aug 2002 15:11:19 -0000
@@ -811,8 +811,8 @@
 alias(r)(fc -e -)
 findex(read)
 vindex(IFS, use of)
-ifzman(xitem(tt(read) [ tt(-rzpqAclneEt) ] [ tt(-k) [ var(num) ] ]))
-item(ifnzman(tt(read) [ tt(-rzpqAclneEt) ] [ tt(-k) [ var(num) ] ]) [ tt(-u)var(n) ] [ var(name)[tt(?)var(prompt)] ] [ var(name) ...  ])(
+ifzman(xitem(tt(read) [ tt(-rszpqAclneEt) ] [ tt(-k) [ var(num) ] ]))
+item(ifnzman(tt(read) [ tt(-rszpqAclneEt) ] [ tt(-k) [ var(num) ] ]) [ tt(-u)var(n) ] [ var(name)[tt(?)var(prompt)] ] [ var(name) ...  ])(
 vindex(REPLY, use of)
 vindex(reply, use of)
 Read one line and break it into fields using the characters
@@ -828,6 +828,10 @@
 Raw mode: a `tt(\)' at the end of a line does not signify line
 continuation and backslashes in the line don't quote the following
 character and are not removed.
+)
+item(tt(-s))(
+Don't echo back characters if reading from the terminal.  Currently does
+not work with the tt(-q) option.
 )
 item(tt(-q))(
 Read only one character from the terminal and set var(name) to

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author