1 #!/bin/sh -- # A comment mentioning perl
2 eval 'exec perl -S $0 ${1+"$@"}'
5 # ptyexec: connect the STDIN and STDOUT of some command to a
6 # pseudo terminal (e.g. /dev/ptyp2 (master) and /dev/ttyp2 (slave))
8 # based on "ssh-ppp" (ppp over secure shell VPN hack)
10 # Usage: ptyexec <command and args to run ...>
12 # Runs the command in background; writes to output what pseudoterminal
13 # has been connected to it.
18 die
"no command" unless @cmd
;
20 # loop over possible pseudoterminals until we can open one:
21 foreach
$m1 ("p" ..
"z") {
22 foreach
$m2 ("0"..
"9", "a"..
"f") {
23 &try
("$m1$m2"); # exit takes place in try() on success
31 my
$master = "/dev/pty$dev";
32 my
$slave = "/dev/tty$dev";
34 # try to open the pseudoterminal read/write:
35 if ( open
(PTY
, "+>$master") ) {
36 # now fork off a child in background to exec @cmd:
38 die
"cannot fork, $!" if ! defined
($pid);
41 # child remaps his stdio to the pseudoterm:
42 open
(STDIN
, "<&PTY") || die
"reopen STDIN, $!";
43 open
(STDOUT
, ">&PTY") || die
"reopen STDOUT, $!";
46 # and then runs the command:
49 die
"exec: " .
join(' ', @cmd
) .
": $!";
51 # parent tells the user what we have set up:
54 print STDOUT
"PID:$pid\n";
55 if ( ! kill 0, $pid ) {
56 # see if the child is still alive:
57 print STDOUT
"WARNING: NO CHILD PID\n";
59 print STDOUT
"MASTER: $master\n";
60 print STDOUT
"SLAVE: $slave\n";