tzwrapper.cc: fixed use of iterator after erase
[barry.git] / contrib / barry_freebsd / ptyexec
blob0ce93253c4f75aed4baeb183a9be22f6ad7e6903
1 #!/bin/sh -- # A comment mentioning perl
2 eval 'exec perl -S $0 ${1+"$@"}'
3 if 0;
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.
16 @cmd = @ARGV;
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
26 exit 1;
28 sub try {
29 my ($dev) = @_;
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:
37 my $pid = fork();
38 die "cannot fork, $!" if ! defined($pid);
40 if ( ! $pid ) {
41 # child remaps his stdio to the pseudoterm:
42 open(STDIN, "<&PTY") || die "reopen STDIN, $!";
43 open(STDOUT, ">&PTY") || die "reopen STDOUT, $!";
44 close(PTY);
46 # and then runs the command:
47 exec @cmd;
48 # exec failed:
49 die "exec: " . join(' ', @cmd) . ": $!";
50 } else {
51 # parent tells the user what we have set up:
52 close(PTY);
53 sleep 1;
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";
61 exit 0;