| 1 | ;#!/usr/bin/perl | ||
| 2 | # | ||
| 3 | # Example script showing how to use Term::VT102 with an SSH command. SSHs to | ||
| 4 | # localhost and runs a shell, and dumps what Term::VT102 thinks should be on | ||
| 5 | # the screen. | ||
| 6 | # | ||
| 7 | # Logs all terminal output to STDERR if STDERR is redirected to a file. | ||
| 8 | # | ||
| 9 | |||
| 10 | use Term::VT102; | ||
| 11 | use IO::Handle; | ||
| 12 | use POSIX ':sys_wait_h'; | ||
| 13 | use IO::Pty; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | $| = 1; | ||
| 17 | |||
| 18 | my $cmd = 'ssh -v -t localhost'; | ||
| 19 | |||
| 20 | # Create the terminal object. | ||
| 21 | # | ||
| 22 | my $vt = Term::VT102->new ( | ||
| 23 | 'cols' => 80, | ||
| 24 | 'rows' => 24, | ||
| 25 | ); | ||
| 26 | |||
| 27 | # Convert linefeeds to linefeed + carriage return. | ||
| 28 | # | ||
| 29 | $vt->option_set ('LFTOCRLF', 1); | ||
| 30 | |||
| 31 | # Make sure line wrapping is switched on. | ||
| 32 | # | ||
| 33 | $vt->option_set ('LINEWRAP', 1); | ||
| 34 | |||
| 35 | # Create a pty for the SSH command to run on. | ||
| 36 | # | ||
| 37 | my $pty = new IO::Pty; | ||
| 38 | my $tty_name = $pty->ttyname (); | ||
| 39 | if (not defined $tty_name) { | ||
| 40 | die "Could not assign a pty"; | ||
| 41 | } | ||
| 42 | $pty->autoflush (); | ||
| 43 | |||
| 44 | # Run the SSH command in a child process. | ||
| 45 | # | ||
| 46 | my $pid = fork; | ||
| 47 | if (not defined $pid) { | ||
| 48 | die "Cannot fork: $!"; | ||
| 49 | } elsif ($pid == 0) { | ||
| 50 | # | ||
| 51 | # Child process - set up stdin/out/err and run the command. | ||
| 52 | # | ||
| 53 | |||
| 54 | # Become process group leader. | ||
| 55 | # | ||
| 56 | if (not POSIX::setsid ()) { | ||
| 57 | warn "Couldn't perform setsid: $!"; | ||
| 58 | } | ||
| 59 | |||
| 60 | # Get details of the slave side of the pty. | ||
| 61 | # | ||
| 62 | my $tty = $pty->slave (); | ||
| 63 | $tty_name = $tty->ttyname(); | ||
| 64 | |||
| 65 | # Linux specific - commented out, we'll just use stty below. | ||
| 66 | # | ||
| 67 | # # Set the window size - this may only work on Linux. | ||
| 68 | # # | ||
| 69 | # my $winsize = pack ('SSSS', $vt->rows, $vt->cols, 0, 0); | ||
| 70 | # ioctl ($tty, &IO::Tty::Constant::TIOCSWINSZ, $winsize); | ||
| 71 | |||
| 72 | # File descriptor shuffling - close the pty master, then close | ||
| 73 | # stdin/out/err and reopen them to point to the pty slave. | ||
| 74 | # | ||
| 75 | close ($pty); | ||
| 76 | close (STDIN); | ||
| 77 | close (STDOUT); | ||
| 78 | open (STDIN, "<&" . $tty->fileno ()) | ||
| 79 | || die "Couldn't reopen " . $tty_name . " for reading: $!"; | ||
| 80 | open (STDOUT, ">&" . $tty->fileno()) | ||
| 81 | || die "Couldn't reopen " . $tty_name . " for writing: $!"; | ||
| 82 | close (STDERR); | ||
| 83 | open (STDERR, ">&" . $tty->fileno()) | ||
| 84 | || die "Couldn't redirect STDERR: $!"; | ||
| 85 | |||
| 86 | # Set sane terminal parameters. | ||
| 87 | # | ||
| 88 | system 'stty sane'; | ||
| 89 | |||
| 90 | # Set the terminal size with stty. | ||
| 91 | # | ||
| 92 | system 'stty rows ' . $vt->rows; | ||
| 93 | system 'stty cols ' . $vt->cols; | ||
| 94 | |||
| 95 | # Finally, run the command, and die if we can't. | ||
| 96 | # | ||
| 97 | exec $cmd; | ||
| 98 | die "Cannot exec '$cmd': $!"; | ||
| 99 | } | ||
| 100 | |||
| 101 | my ($cmdbuf, $stdinbuf, $iot, $eof, $prevxy, $died); | ||
| 102 | |||
| 103 | # IO::Handle for standard input - unbuffered. | ||
| 104 | # | ||
| 105 | $iot = new IO::Handle; | ||
| 106 | $iot->fdopen (fileno(STDIN), 'r'); | ||
| 107 | |||
| 108 | # Removed - from Perl 5.8.0, setvbuf isn't available by default. | ||
| 109 | # $iot->setvbuf (undef, _IONBF, 0); | ||
| 110 | |||
| 111 | # Set up the callback for OUTPUT; this callback function simply sends | ||
| 112 | # whatever the Term::VT102 module wants to send back to the terminal and | ||
| 113 | # sends it to the child process - see its definition below. | ||
| 114 | # | ||
| 115 | $vt->callback_set ('OUTPUT', \&vt_output, $pty); | ||
| 116 | |||
| 117 | # Set up a callback for row changes, so we can process updates and display | ||
| 118 | # them without having to redraw the whole screen every time. We catch CLEAR, | ||
| 119 | # SCROLL_UP, and SCROLL_DOWN with another function that triggers a | ||
| 120 | # whole-screen repaint. You could process SCROLL_UP and SCROLL_DOWN more | ||
| 121 | # elegantly, but this is just an example. | ||
| 122 | # | ||
| 123 | my $changedrows = {}; | ||
| 124 | $vt->callback_set ('ROWCHANGE', \&vt_rowchange, $changedrows); | ||
| 125 | $vt->callback_set ('CLEAR', \&vt_changeall, $changedrows); | ||
| 126 | $vt->callback_set ('SCROLL_UP', \&vt_changeall, $changedrows); | ||
| 127 | $vt->callback_set ('SCROLL_DOWN', \&vt_changeall, $changedrows); | ||
| 128 | |||
| 129 | # Set stdin's terminal to raw mode so we can pass all keypresses straight | ||
| 130 | # through immediately. | ||
| 131 | # | ||
| 132 | system 'stty raw -echo'; | ||
| 133 | |||
| 134 | $eof = 0; | ||
| 135 | $prevxy = ''; | ||
| 136 | $died = 0; | ||
| 137 | |||
| 138 | while (not $eof) { | ||
| 139 | my ($rin, $win, $ein, $rout, $wout, $eout, $nr, $didout); | ||
| 140 | |||
| 141 | ($rin, $win, $ein) = ('', '', ''); | ||
| 142 | vec ($rin, $pty->fileno, 1) = 1; | ||
| 143 | vec ($rin, $iot->fileno, 1) = 1; | ||
| 144 | |||
| 145 | select ($rout=$rin, $wout=$win, $eout=$ein, 1); | ||
| 146 | |||
| 147 | # Read from the SSH command if there is anything coming in, and | ||
| 148 | # pass any data on to the Term::VT102 object. | ||
| 149 | # | ||
| 150 | $cmdbuf = ''; | ||
| 151 | $nr = 0; | ||
| 152 | if (vec ($rout, $pty->fileno, 1)) { | ||
| 153 | $nr = $pty->sysread ($cmdbuf, 1024); | ||
| 154 | $eof = 1 if ((defined $nr) && ($nr == 0)); | ||
| 155 | if ((defined $nr) && ($nr > 0)) { | ||
| 156 | $vt->process ($cmdbuf); | ||
| 157 | syswrite STDERR, $cmdbuf if (! -t STDERR); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | # End processing if we've gone 1 round after SSH died with no | ||
| 162 | # output. | ||
| 163 | # | ||
| 164 | $eof = 1 if ($died && $cmdbuf eq ''); | ||
| 165 | |||
| 166 | # Do your stuff here - use $vt->row_plaintext() to see what's on various | ||
| 167 | # rows of the screen, for instance, or before this main loop you could set | ||
| 168 | # up a ROWCHANGE callback which checks the changed row, or whatever. | ||
| 169 | # | ||
| 170 | # In this example, we just pass standard input to the SSH command, and we | ||
| 171 | # take the data coming back from SSH and pass it to the Term::VT102 object, | ||
| 172 | # and then we repeatedly dump the Term::VT102 screen. | ||
| 173 | |||
| 174 | # Read key presses from standard input and pass them to the command | ||
| 175 | # running in the child process. | ||
| 176 | # | ||
| 177 | $stdinbuf = ''; | ||
| 178 | if (vec ($rout, $iot->fileno, 1)) { | ||
| 179 | $nr = $iot->sysread ($stdinbuf, 16); | ||
| 180 | $eof = 1 if ((defined $nr) && ($nr == 0)); | ||
| 181 | $pty->syswrite ($stdinbuf, $nr) if ((defined $nr) && ($nr > 0)); | ||
| 182 | } | ||
| 183 | |||
| 184 | # Dump what Term::VT102 thinks is on the screen. We only output rows | ||
| 185 | # we know have changed, to avoid generating too much output. | ||
| 186 | # | ||
| 187 | $didout = 0; | ||
| 188 | foreach my $row (sort keys %$changedrows) { | ||
| 189 | printf "\e[%dH%s\r", $row, $vt->row_sgrtext ($row); | ||
| 190 | delete $changedrows->{$row}; | ||
| 191 | $didout ++; | ||
| 192 | } | ||
| 193 | if (($didout > 0) || ($prevxy != ''.$vt->x.','.$vt->y)) { | ||
| 194 | printf "\e[%d;%dH", $vt->y, ($vt->x > $vt->cols ? $vt->cols : $vt->x); | ||
| 195 | } | ||
| 196 | |||
| 197 | # Make sure the child process has not died. | ||
| 198 | # | ||
| 199 | $died = 1 if (waitpid ($pid, &WNOHANG) > 0); | ||
| 200 | } | ||
| 201 | |||
| 202 | print "\e[24H\r\n"; | ||
| 203 | $pty->close; | ||
| 204 | |||
| 205 | # Reset the terminal parameters. | ||
| 206 | # | ||
| 207 | system 'stty sane'; | ||
| 208 | |||
| 209 | |||
| 210 | # Callback for OUTPUT events - for Term::VT102. | ||
| 211 | # | ||
| 212 | sub vt_output { | ||
| 213 | my ($vtobject, $type, $arg1, $arg2, $private) = @_; | ||
| 214 | |||
| 215 | if ($type eq 'OUTPUT') { | ||
| 216 | $pty->syswrite ($arg1, length $arg1); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | |||
| 221 | # Callback for ROWCHANGE events. This just sets a time value for the changed | ||
| 222 | # row using the private data as a hash reference - the time represents the | ||
| 223 | # earliest that row was changed since the last screen update. | ||
| 224 | # | ||
| 225 | sub vt_rowchange { | ||
| 226 | my ($vtobject, $type, $arg1, $arg2, $private) = @_; | ||
| 227 | $private->{$arg1} = time if (not exists $private->{$arg1}); | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | # Callback to trigger a full-screen repaint. | ||
| 232 | # | ||
| 233 | sub vt_changeall { | ||
| 234 | my ($vtobject, $type, $arg1, $arg2, $private) = @_; | ||
| 235 | for (my $row = 1; $row <= $vtobject->rows; $row++) { | ||
| 236 | $private->{$row} = 0; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | # EOF |