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

PATCH: read -d



This adds the -d option to read which both bash and ksh93 have. It
allows you to specify a delimiter which is used to terminate the input
line instead of newline. With input from the terminal, it uses cbreak
mode. If not too sure about the (SHTTY == -1) test and VMIN/VTIME stuff
as I cut and paste them from elsewhere. As with the -s option, the tty
stuff seems to be run even when it isn't reading input from a terminal.
I also removed a bit at the start where it checks for arguments to a
non-existent -b option.

Would it be worth adding the -n option too (also in both bash and
ksh93). It is equivalent to -u0 -k so wouldn't add any new functionality
as such. read -k is a bit of an oddity really in that I can't see what it
does that you can't do in ksh with read -n 1 < /dev/tty. Other remaining
differences to bash/ksh's read are fairly insignificant.

Oliver

Index: Completion/Zsh/Command/_read
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Zsh/Command/_read,v
retrieving revision 1.4
diff -u -r1.4 _read
--- Completion/Zsh/Command/_read	13 Feb 2003 15:04:49 -0000
1.4
+++ Completion/Zsh/Command/_read	14 Feb 2003 17:58:36 -0000
@@ -11,6 +11,7 @@
   '(-p -k -s -u -z)-q[read y or n character from terminal]' \
   '(-q)-k+[specify number of characters to read]:: :_guard "[0-9]#"
"number of characters"' \
   '(-q -z)-t+[test if input is available before reading]:: :_guard
"[0-9.]#" "timeout (seconds)"' \
+  '(-q)-d[specify delimiter to terminate input instead of
newline]:delimiter' \
   '(-q -s -u -p -t)-z[read entry from editor buffer stack]' \
   '(-E)-e[input read is echoed and not assigned]' \
   '(-e)-E[input read is echoed]' \
Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.57
diff -u -r1.57 builtins.yo
--- Doc/Zsh/builtins.yo	13 Feb 2003 13:46:10 -0000	1.57
+++ Doc/Zsh/builtins.yo	14 Feb 2003 17:58:36 -0000
@@ -824,8 +824,8 @@
 alias(r)(fc -e -)
 findex(read)
 vindex(IFS, use of)
-ifzman(xitem(tt(read) [ tt(-rszpqAclneE) ] [ tt(-t) [ var(num) ] ] [
tt(-k) [ var(num) ] ]))
-item(ifnzman(tt(read) [ tt(-rszpqAclneE) ] [ tt(-t) [ var(num) ] ] [
tt(-k) [ var(num) ] ]) [ tt(-u)var(n) ] [ var(name)[tt(?)var(prompt)] ]
[ var(name) ...  ])(
+ifzman(xitem(tt(read) [ tt(-rszpqAclneE) ] [ tt(-t) [ var(num) ] ] [
tt(-k) [ var(num) ] ] [ tt(-d) var(delim) ]))
+item(ifnzman(tt(read) [ tt(-rszpqAclneE) ] [ tt(-t) [ var(num) ] ] [
tt(-k) [ var(num) ] ] [ tt(-d) var(delim) ]) [ 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
@@ -898,6 +898,10 @@
 )
 item(tt(-p))(
 Input is read from the coprocess.
+)
+item(tt(-d) var(delim))(
+Input is terminated by the first character of var(delim) instead of
+by newline.
 )
 item(tt(-t) [ var(num) ])(
 Test if input is available before attempting to read.  If var(num)
Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.96
diff -u -r1.96 builtin.c
--- Src/builtin.c	13 Feb 2003 13:46:10 -0000	1.96
+++ Src/builtin.c	14 Feb 2003 17:58:37 -0000
@@ -106,7 +106,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", 0, bin_fc, 0, -1, BIN_R, "nrl", NULL),
-    BUILTIN("read", 0, bin_read, 0, -1, 0, "cek:%lnpqrst:%zu:AE",
NULL),
+    BUILTIN("read", 0, bin_read, 0, -1, 0, "cd:ek:%lnpqrst:%zu:AE",
NULL),
     BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS |
BINF_PSPECIAL, bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%lptux",
"r"),
     BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "df", "r"),
     BUILTIN("return", BINF_PSPECIAL, bin_break, 0, 1, BIN_RETURN, NULL,
NULL),
@@ -4120,9 +4120,9 @@
     int readchar = -1, val, resettty = 0;
     struct ttyinfo saveti;
     char d;
+    char delim = '\n';
 
-
-    if ((OPT_HASARG(ops,c='k') || OPT_HASARG(ops,c='b'))) {
+    if (OPT_HASARG(ops,c='k')) {
 	char *eptr, *optarg = OPT_ARG(ops,c);
 	nchars = (int)zstrtol(optarg, &eptr, 10);
 	if (*eptr) {
@@ -4223,17 +4223,36 @@
 	    return 1;
 	}
     }
+    if (OPT_ISSET(ops,'d')) {
+        delim = *OPT_ARG(ops,'d');
+	if (SHTTY != -1) {
+	    struct ttyinfo ti;
+	    gettyinfo(&ti);
+	    saveti = ti;
+	    resettty = 1;
+#ifdef HAS_TIO
+	    ti.tio.c_lflag &= ~ICANON;
+	    ti.tio.c_cc[VMIN] = 1;
+	    ti.tio.c_cc[VTIME] = 0;
+#else
+	    ti.sgttyb.sg_flags |= CBREAK;
+#endif
+	    settyinfo(&ti);
+	}
+    }
     if (OPT_ISSET(ops,'s') && SHTTY != -1) {
 	struct ttyinfo ti;
 	gettyinfo(&ti);
-	saveti = ti;
+	if (! resettty) {
+	    saveti = ti;
+	    resettty = 1;
+	}
 #ifdef HAS_TIO
 	ti.tio.c_lflag &= ~ECHO;
 #else
 	ti.sgttyb.sg_flags &= ~ECHO;
 #endif
 	settyinfo(&ti);
-	resettty = 1;
     }
 
     /* handle prompt */
@@ -4352,11 +4371,11 @@
 	    c = zread(izle, &readchar);
 	    /* \ at the end of a line indicates a continuation *
 	     * line, except in raw mode (-r option)            */
-	    if (bslash && c == '\n') {
+	    if (bslash && c == delim) {
 		bslash = 0;
 		continue;
 	    }
-	    if (c == EOF || c == '\n')
+	    if (c == EOF || c == delim)
 		break;
 	    /*
 	     * `first' is non-zero if any separator we encounter is a
@@ -4393,7 +4412,7 @@
 	    }
 	}
 	signal_setmask(s);
-	if (c == '\n' || c == EOF)
+	if (c == delim || c == EOF)
 	    gotnl = 1;
 	*bptr = '\0';
 	/* dispose of word appropriately */
@@ -4455,11 +4474,11 @@
 	    c = zread(izle, &readchar);
 	    /* \ at the end of a line introduces a continuation line,
except in
 	       raw mode (-r option) */
-	    if (bslash && c == '\n') {
+	    if (bslash && c == delim) {
 		bslash = 0;
 		continue;
 	    }
-	    if (c == EOF || (c == '\n' && !zbuf))
+	    if (c == EOF || (c == delim && !zbuf))
 		break;
 	    if (!bslash && isep(c) && bptr == buf) {
 		if (iwsep(c))


This e-mail and any attachment is for authorised use by the intended recipient(s) only.  It may contain proprietary material, confidential information and/or be subject to legal privilege.  It should not be copied, disclosed to, retained or used by, any other party.  If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.  Thank you.



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