zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] [doc] clone module using xterm's slave mode
Date: Sun, 17 May 2020 11:13:47 +0100	[thread overview]
Message-ID: <20200517101347.g7v6k2g7hoew636q@chazelas.org> (raw)

Hi, while testing the patch for the pgrp restore issue, I came
up with a way to clone zsh onto a xterm and attach to the tty
device properly there (using xterm's slave mode).

Here's a doc patch to document that approach (requires socat,
though the same could be done with expect or some perl/python
pty module).

I changed the trap : INT to trap "" INT so the signals are
properly ignored. (trap : INT makes the signal harmless to sh,
but would still kill "sleep" in those sh implementations where
sleep is not builtin).

diff --git a/Doc/Zsh/mod_clone.yo b/Doc/Zsh/mod_clone.yo
index 02dc5be0e..a3801cd40 100644
--- a/Doc/Zsh/mod_clone.yo
+++ b/Doc/Zsh/mod_clone.yo
@@ -20,7 +20,7 @@ and non-zero on error.
 The target of tt(clone) should be an unused terminal, such as an unused virtual
 console or a virtual terminal created by
 
-example(xterm -e sh -c 'trap : INT QUIT TSTP; tty;
+example(xterm -e sh -c 'trap "" INT QUIT TSTP; tty;
         while :; do sleep 100000000; done')
 
 Some words of explanation are warranted about this long xterm command
@@ -39,11 +39,30 @@ enditemize()
 
 This does not apply when cloning to an em(unused) vc.
 
-Cloning to a used (and unprepared) terminal will result in two
-processes reading simultaneously from the same terminal, with
-input bytes going randomly to either process.
+It is possible however to clone zsh onto a xterm and that zsh process
+to lead the session attached to that terminal by using tt(xterm)'s slave
+mode in conjunction with an utility like tt(socat) that can create a
+pseudo-terminal pair:
 
-tt(clone) is mostly useful as a shell built-in replacement for
-openvt.
+example(socat pty,link=pty,wait-slave,echo=0 'exec:xterm -Spty/3,nofork,fdout=3,fdin=3' &
+        clone pty; (($!)) || { IFS= read -r WINDOWID && ((WINDOWID = 0x$WINDOWID));})
+
+Above, tt(socat) creates a pseudo terminal, and makes a tt(pty) symbolic
+link to the slave device in the current directory. As soon as a process
+opens that device, tt(xterm) is started with its file descriptor 3
+connected to the master side.
+
+The cloned zsh is the process that opens that device here. And once
+started, we make it read the X11 Window ID that tt(xterm) oututs there
+into tt($WINDOWID) (the same variable that xterm sets itself when not in
+slave mode) before resuming normal operation. That zsh process becomes
+the session leader attached to the tty device in the same way a zsh
+started with tt(xterm -e zsh) would, and job control works as it should.
+
+Cloning to a used (and unprepared) terminal will result in two processes
+reading simultaneously from the same terminal, with input bytes going
+randomly to either process.
+
+tt(clone) is mostly useful as a shell built-in replacement for openvt.
 )
 enditem()

             reply	other threads:[~2020-05-17 10:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-17 10:13 Stephane Chazelas [this message]
2020-05-17 20:19 ` Mikael Magnusson
2020-05-18  6:14   ` [PATCHv2] " Stephane Chazelas
2020-05-19  1:01 ` [PATCH] " Daniel Shahaf
2020-05-19 18:54   ` Stephane Chazelas
2020-05-19 23:36     ` Daniel Shahaf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200517101347.g7v6k2g7hoew636q@chazelas.org \
    --to=stephane@chazelas.org \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).