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

Re: Is this correct? Redirecting stdin/stout to implement a gdb-like "set inferior tty" in zshdb



On Tue, 09 Dec 2008 12:37:16 +0000
Peter Stephenson <pws@xxxxxxx> wrote:
> It would be relatively straightforward to supply vared with an option to
> use as the terminal if there was none.  However it would be messier to
> have it use the given terminal to replace any existing terminal in an
> interactive shell (it *might* work saving and restoring the FDs but it's
> a little fraught since zle not's designed to manage multiple terminals
> at once), so that option would probably be limited to use in
> non-interactive shells.

(Moved to zsh-workers.)

Well, it seems to work regardless of my worries.

Minor change to the existing behaviour:  we now check an opened device
isatty().  As previously the only device we would open was /dev/tty, this
doesn't seem like a problem.

Index: Doc/Zsh/zle.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/zle.yo,v
retrieving revision 1.74
diff -u -r1.74 zle.yo
--- Doc/Zsh/zle.yo	17 Oct 2008 08:31:22 -0000	1.74
+++ Doc/Zsh/zle.yo	9 Dec 2008 14:06:34 -0000
@@ -292,7 +292,8 @@
 cindex(parameters, editing)
 cindex(editing parameters)
 xitem(tt(vared) [ tt(-Aache) ] [ tt(-p) var(prompt) ] [ tt(-r) var(rprompt) ])
-item(  [ -M var(main-keymap) ] [ -m var(vicmd-keymap) ] var(name))(
+xitem(  [ tt(-M) var(main-keymap) ] [ tt(-m) var(vicmd-keymap) ])
+item(  [ tt(-t) var(tty) ] var(name))(
 The value of the parameter var(name) is loaded into the edit
 buffer, and the line editor is invoked.  When the editor exits,
 var(name) is set to the string value returned by the editor.
@@ -326,6 +327,10 @@
 to override tt(viins) and tt(vicmd).  For emacs-style editing, only tt(-M)
 is normally needed but the tt(-m) option may still be used.  On exit, the
 previous keymaps will be restored.
+
+If `tt(-t) var(tty)' is given, var(tty) is the name of a terminal device
+to be used instead of the default tt(/dev/tty).  If var(tty) does not
+refer to a terminal an error is reported.
 )
 findex(zle)
 cindex(widgets, rebinding)
Index: Src/Zle/zle_main.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_main.c,v
retrieving revision 1.120
diff -u -r1.120 zle_main.c
--- Src/Zle/zle_main.c	12 Nov 2008 12:59:07 -0000	1.120
+++ Src/Zle/zle_main.c	9 Dec 2008 14:06:35 -0000
@@ -1449,7 +1449,7 @@
     Value v;
     Param pm = 0;
     int ifl;
-    int type = PM_SCALAR, obreaks = breaks, haso = 0;
+    int type = PM_SCALAR, obreaks = breaks, haso = 0, oSHTTY = 0;
     char *p1, *p2, *main_keymapname, *vicmd_keymapname;
     Keymap main_keymapsave = NULL, vicmd_keymapsave = NULL;
     FILE *oshout = NULL;
@@ -1558,13 +1558,22 @@
 	s = ztrdup(s);
     }
 
-    if (SHTTY == -1) {
+    if (SHTTY == -1 || OPT_ISSET(ops,'t')) {
 	/* need to open /dev/tty specially */
-	if ((SHTTY = open("/dev/tty", O_RDWR|O_NOCTTY)) == -1) {
+	oSHTTY = SHTTY;
+	if ((SHTTY = open(OPT_ISSET(ops,'t') ? OPT_ARG(ops,'t') : "/dev/tty",
+			  O_RDWR|O_NOCTTY)) == -1) {
 	    zwarnnam(name, "can't access terminal");
 	    zsfree(s);
 	    return 1;
 	}
+	if (!isatty(SHTTY)) {
+	    zwarnnam(name, "%s: not a terminal", OPT_ARG(ops,'t'));
+	    close(SHTTY);
+	    SHTTY = oSHTTY;
+	    zsfree(s);
+	    return 1;
+	}
 	oshout = shout;
 	init_shout();
 
@@ -1597,7 +1606,7 @@
     if (haso) {
 	fclose(shout);	/* close(SHTTY) */
 	shout = oshout;
-	SHTTY = -1;
+	SHTTY = oSHTTY;
     }
     if (!t || errflag) {
 	/* error in editing */
@@ -1887,7 +1896,7 @@
 
 static struct builtin bintab[] = {
     BUILTIN("bindkey", 0, bin_bindkey, 0, -1, 0, "evaM:ldDANmrsLRp", NULL),
-    BUILTIN("vared",   0, bin_vared,   1,  1, 0, "aAcehM:m:p:r:", NULL),
+    BUILTIN("vared",   0, bin_vared,   1,  1, 0, "aAcehM:m:p:r:t:", NULL),
     BUILTIN("zle",     0, bin_zle,     0, -1, 0, "aAcCDFgGIKlLmMNRU", NULL),
 };
 


-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



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