Subject: Bug#3201: screen ^a:exec ! doesn't clear TIOCPKT on filtered pty

From: [email-masked] (Mark W. Eichin)
Date: 03 Jun 1996 12:51:59 -0400

package: screen
version: 3.7.1-3

Demonstration: Fire up screen. Hit "^a:exec !cat -v". Run some
commands. Note that you get leading ^@ mixed in. (If you're running
tcsh, bash, or some other interactive program, you'll get ^@ before
the echo of every keystroke, which makes it *really* clear.)

Analysis: screen creates all pty's in packet mode (ioctl(fd,TIOCPKT,&on))
and expects to read the leading character off of each. This makes
sense, as it allows screen to do clever things with flow control.
	However, the winexec() and following ForkWindow() in window.c
don't clear packet mode before handing the master filedescriptor over
to the forked program, leading to the above bug. It looks like code to
set TIOCPKT mode off could be added in ForkWindow, and set it back in
FreePseudoWin, but I'm not sure where to find the file descriptor for
the master pty at that point (the master for the *pseudo* is easy to
find, we need the master for the underlying window - which may be
w->w_ptyfd, I haven't tried hacking it though.)

Other comments: This has nothing to do with the linux bug where
TIOCPKT mode survives the close of a pty. It is generic to any system
with TIOCPKT; I was just able to reproduce it (with screen-3.6.1)
under SunOS 4.1.4.

I'll try and send along a patch (if my guess about w_ptyfd is right,
the comments in window.h seem to confirm this) but would appreciate
other input.
			_Mark_ <[email-masked]>
			The Herd of Kittens

---

From: [email-masked] (Mark W. Eichin)
Date: 03 Jun 1996 13:46:19 -0400

> the comments in window.h seem to confirm this) but would appreciate

This appears to work.

--- window.c~	Mon Nov 20 13:03:05 1995
+++ window.c	Mon Jun  3 13:18:57 1996
@@ -880,6 +880,12 @@ char **av;
 	FreePseudowin(w);
 	return -1;
       }
+    if (ioctl(w->w_ptyfd, TIOCPKT, (char *)&flag))
+      {
+	Msg(errno, "TIOCPKT ioctl on parent");
+	FreePseudowin(w);
+	return -1;
+      }
   }
 #endif /* TIOCPKT */
   pwin->p_pid = ForkWindow(av, (char *)0, (char *)0, t, w);
@@ -903,6 +909,14 @@ struct win *w;
     close(pwin->p_ptyfd);
   free((char *)pwin);
   w->w_pwin = NULL;
+#ifdef TIOCPKT
+  {
+    int flag = 1;
+
+    if (ioctl(w->w_ptyfd, TIOCPKT, (char *)&flag))
+      Msg(errno, "TIOCPKT reset on parent failed");
+  }
+#endif /* TIOCPKT */
 }

 #endif /* PSEUDOS */

---

From: [email-masked] (joost witteveen)
Date: Thu, 6 Jun 1996 21:03:09 +0200 (MET DST)

> [...]
> [and later a patch].

Gosh! You're the kind of person I think I should be very polite to
(might come in handy, some time).

Sorry for the delay in responding (3 days), but I was too busy to
read debian-devel for that time, and for some reason the
bug-tracking system stopped forwarding the bugs to me.

I did, however, within 15 min of reading your patch, apply it
and recompile. I have one question however:

You speak of ^@ that fill the screen (yes, I'm using bash).
when trying to reproduce the bug, I get the ^@, but also:
  ^M    at the end of every line of output
  ^H ^H each time I hit backspace
  (and maybe more)
Your patch indeed corrects the ^@'s, but the ^M, and ^H ^H remain
(though they may be less bad). (also the cursor keys don't work, ...)

I still like your patch though. I just would like your opinion
on whether you know something around the other oddities too.

Anyway, as the 1.1 release is now "imminent", I'll upload this
3.7.1-4 with your patch in about 3 hours from now, unless you
reply before that. (I can always upload 3.7.1-5, if you know how
to correct the other oddities).

One administrative note: I have to admit that I don't know
too much about sockets and all that (I only know the basics).
This makes me not the ideal screen maintainer (I started
maintaining it because it was orphaned before). If you would
like to take screen over from me, please tell me.

Thanks,

--
joost witteveen
            [email-masked]
          [email-masked]
--
Use Debian Linux!

---

From: [email-masked] (Mark W. Eichin)
Date: 06 Jun 1996 15:36:40 -0400

> (might come in handy, some time).

:-) flattery will get you everywhere, or something like that...

>   ^H ^H each time I hit backspace

The ^m and ^h (and other sequences) are correct -- in fact, the whole
reason to filter with "cat -v" is to get things like that to show
up. (In the backspace example, typically ^h only moves the cursor, it
doesn't erase -- so you need to ^H then space over the character, then
^h again... for more details, look at the termcap or terminfo pages.)
It's just the ^@ that was wrong... and I only noticed it in screen
because I was trying to duplicate an emacs bug -- they both use
pseudo-tty's, and it *looked* like the same problem, but turned out to
be totally coincidental. (It turns out that this use of screen could
be very useful in debugging ncurses problems, though running the
programs under emacs would probably be better...)

> like to take screen over from me, please tell me.

Not particularly :-) Most of the packages I maintain are either things
that I use personally and weren't packaged already, or things that
were orphaned and I wanted to see newer versions of (like emacs.) If
you ever find that you don't have time to upgrade screen, let me know,
but otherwise you're doing fine. (Don't forget to forward the patch
"upstream", though :-)
			_Mark_ <[email-masked]>
			The Herd of Kittens