zsh-workers
 help / color / mirror / code / Atom feed
a3801cd405bd7c44312b326de9578aa4887b7a4a blob 2924 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
COMMENT(!MOD!zsh/clone
A builtin that can clone a running shell onto another terminal.
!MOD!)
The tt(zsh/clone) module makes available one builtin command:

startitem()
findex(clone)
cindex(shell, cloning)
cindex(cloning the shell)
cindex(terminal)
item(tt(clone) var(tty))(
Creates a forked instance of the current shell, attached to the specified
var(tty).  In the new shell, the tt(PID), tt(PPID) and tt(TTY) special
parameters are changed appropriately.  tt($!) is set to zero in the new
shell, and to the new shell's PID in the original shell.

The return status of the builtin is zero in both shells if successful,
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;
        while :; do sleep 100000000; done')

Some words of explanation are warranted about this long xterm command
line: when doing clone on a pseudo-terminal, some other session
("session" meant as a unix session group, or SID) is already owning
the terminal. Hence the cloned zsh cannot acquire the pseudo-terminal
as a controlling tty. That means two things:

startitemize()
itemiz(the job control signals will go to the sh-started-by-xterm process
group (that's why we disable INT QUIT and TSTP with trap; otherwise
the while loop could get suspended or killed))
itemiz(the cloned shell will have job control disabled, and the job
control keys (control-C, control-\ and control-Z) will not work.)
enditemize()

This does not apply when cloning to an em(unused) vc.

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:

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()
debug log:

solving a3801cd40 ...
found a3801cd40 in https://inbox.vuxu.org/zsh-workers/20200517101347.g7v6k2g7hoew636q@chazelas.org/
found 02dc5be0e in https://git.vuxu.org/mirror/zsh/
preparing index
index prepared:
100644 02dc5be0e87d145e16b6563456b4613e7ac8f4d5	Doc/Zsh/mod_clone.yo

applying [1/1] https://inbox.vuxu.org/zsh-workers/20200517101347.g7v6k2g7hoew636q@chazelas.org/
diff --git a/Doc/Zsh/mod_clone.yo b/Doc/Zsh/mod_clone.yo
index 02dc5be0e..a3801cd40 100644

Checking patch Doc/Zsh/mod_clone.yo...
Applied patch Doc/Zsh/mod_clone.yo cleanly.

index at:
100644 a3801cd405bd7c44312b326de9578aa4887b7a4a	Doc/Zsh/mod_clone.yo

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).