| 1 | #!/usr/bin/perl | ||
| 2 | # | ||
| 3 | # Example script showing how to use Term::VT102 with Net::Telnet. Telnets to | ||
| 4 | # localhost and dumps what Term::VT102 thinks should be on the screen. Or | ||
| 5 | # you can pass it a host and a port and it will telnet there instead. | ||
| 6 | # | ||
| 7 | # Note that this script doesn't pass the terminal size through to the remote | ||
| 8 | # end, so you might have to do "stty rows 24 cols 80" to make things work | ||
| 9 | # (the default is generally 80x24 anyway though). | ||
| 10 | # | ||
| 11 | # Logs all terminal output to STDERR if STDERR is redirected to a file. | ||
| 12 | # | ||
| 13 | |||
| 14 | use Net::Telnet qw(TELOPT_TTYPE); | ||
| 15 | use Term::VT102; | ||
| 16 | use IO::Handle; | ||
| 17 | use strict; | ||
| 18 | |||
| 19 | $| = 1; | ||
| 20 | |||
| 21 | my ($host, $port) = @ARGV; | ||
| 22 | |||
| 23 | $host = 'localhost' if (not defined $host); | ||
| 24 | $port = 23 if (not defined $port); | ||
| 25 | |||
| 26 | my $t = new Net::Telnet ( | ||
| 27 | 'Host' => $host, | ||
| 28 | 'Port' => $port, | ||
| 29 | 'Errmode' => 'return', | ||
| 30 | 'Timeout' => 1, | ||
| 31 | 'Output_record_separator' => '', | ||
| 32 | ); | ||
| 33 | |||
| 34 | die "failed to connect to $host:$port" if (not defined $t); | ||
| 35 | |||
| 36 | $t->option_callback (\&opt_callback); | ||
| 37 | $t->option_accept ('Do' => TELOPT_TTYPE); | ||
| 38 | $t->suboption_callback (\&subopt_callback); | ||
| 39 | |||
| 40 | my $vt = Term::VT102->new ( | ||
| 41 | 'cols' => 80, | ||
| 42 | 'rows' => 24, | ||
| 43 | ); | ||
| 44 | |||
| 45 | # Convert linefeeds to linefeed + carriage return. | ||
| 46 | # | ||
| 47 | $vt->option_set ('LFTOCRLF', 1); | ||
| 48 | |||
| 49 | # Make sure line wrapping is switched on. | ||
| 50 | # | ||
| 51 | $vt->option_set ('LINEWRAP', 1); | ||
| 52 | |||
| 53 | # Set up the callback for OUTPUT; this callback function simply sends | ||
| 54 | # whatever the Term::VT102 module wants to send back to the terminal and | ||
| 55 | # sends it to Net::Telnet - see its definition below. | ||
| 56 | # | ||
| 57 | $vt->callback_set ('OUTPUT', \&vt_output, $t); | ||
| 58 | |||
| 59 | # Set up a callback for row changes, so we can process updates and display | ||
| 60 | # them without having to redraw the whole screen every time. We catch CLEAR, | ||
| 61 | # SCROLL_UP, and SCROLL_DOWN with another function that triggers a | ||
| 62 | # whole-screen repaint. You could process SCROLL_UP and SCROLL_DOWN more | ||
| 63 | # elegantly, but this is just an example. | ||
| 64 | # | ||
| 65 | my $changedrows = {}; | ||
| 66 | $vt->callback_set ('ROWCHANGE', \&vt_rowchange, $changedrows); | ||
| 67 | $vt->callback_set ('CLEAR', \&vt_changeall, $changedrows); | ||
| 68 | $vt->callback_set ('SCROLL_UP', \&vt_changeall, $changedrows); | ||
| 69 | $vt->callback_set ('SCROLL_DOWN', \&vt_changeall, $changedrows); | ||
| 70 | |||
| 71 | my ($telnetbuf, $io, $stdinbuf, $prevxy); | ||
| 72 | |||
| 73 | $io = new IO::Handle; | ||
| 74 | $io->fdopen (fileno(STDIN), 'r'); | ||
| 75 | $io->blocking (0); | ||
| 76 | |||
| 77 | system 'stty raw -echo'; | ||
| 78 | |||
| 79 | $prevxy = ''; | ||
| 80 | |||
| 81 | while (1) { | ||
| 82 | last if ($t->eof ()); | ||
| 83 | |||
| 84 | my ($rin, $win, $ein, $rout, $wout, $eout, $nr, $delay, $didout); | ||
| 85 | |||
| 86 | ($rin, $win, $ein) = ('', '', ''); | ||
| 87 | vec ($rin, fileno ($t), 1) = 1; | ||
| 88 | vec ($rin, fileno ($io), 1) = 1; | ||
| 89 | |||
| 90 | # If we have any changed rows on the screen still waiting to be | ||
| 91 | # output, we only wait a short time for activity, otherwise we wait | ||
| 92 | # a full second. This is so that batched-up screen updates get | ||
| 93 | # processed in a timely fashion. | ||
| 94 | # | ||
| 95 | $delay = 1; | ||
| 96 | $delay = 0.05 if ((scalar keys %$changedrows) > 0); | ||
| 97 | |||
| 98 | select ($rout=$rin, $wout=$win, $eout=$ein, $delay); | ||
| 99 | |||
| 100 | $telnetbuf = undef; | ||
| 101 | |||
| 102 | if (vec ($rout, fileno ($t), 1)) { | ||
| 103 | $telnetbuf = $t->get ('Timeout' => 1); | ||
| 104 | if (defined $telnetbuf) { | ||
| 105 | $vt->process ($telnetbuf); | ||
| 106 | print STDERR $telnetbuf if (! -t STDERR); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | $telnetbuf = '' if (not defined $telnetbuf); | ||
| 110 | |||
| 111 | # Do your stuff here - use $vt->row_plaintext() to see what's on various | ||
| 112 | # rows of the screen, for instance, or before this main loop you could set | ||
| 113 | # up a ROWCHANGE callback which checks the changed row, or whatever. | ||
| 114 | # | ||
| 115 | # In this example, we just pass standard input to the telnet stream, we take | ||
| 116 | # the data coming back from Net::Telnet and pass it to the Term::VT102 | ||
| 117 | # object, any changed rows of which we dump to the screen. | ||
| 118 | |||
| 119 | # Read key presses from standard input and pass them to Net::Telnet. | ||
| 120 | # | ||
| 121 | $stdinbuf = ''; | ||
| 122 | if (vec ($rout, fileno ($io), 1)) { | ||
| 123 | if (defined $io->sysread ($stdinbuf, 16)) { | ||
| 124 | $t->print ($stdinbuf); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | # Dump what Term::VT102 thinks is on the screen. We only output rows | ||
| 129 | # we know have changed, to avoid generating too much output. | ||
| 130 | # | ||
| 131 | $didout = 0; | ||
| 132 | foreach my $row (sort keys %$changedrows) { | ||
| 133 | printf "\e[%dH%s\r", $row, $vt->row_sgrtext ($row); | ||
| 134 | delete $changedrows->{$row}; | ||
| 135 | $didout ++; | ||
| 136 | } | ||
| 137 | if (($didout > 0) || ($prevxy != ''.$vt->x.','.$vt->y)) { | ||
| 138 | printf "\e[%d;%dH", $vt->y, ($vt->x > $vt->cols ? $vt->cols : $vt->x); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | $t->close (); | ||
| 143 | print "\e[24H\r\n"; | ||
| 144 | |||
| 145 | system 'stty sane'; | ||
| 146 | |||
| 147 | |||
| 148 | # Callback for "DO" handling - for Net::Telnet. | ||
| 149 | # | ||
| 150 | sub opt_callback { | ||
| 151 | my ($obj,$opt,$is_remote,$is_enabled,$was_enabled,$buf_position) = @_; | ||
| 152 | |||
| 153 | if ($opt == TELOPT_TTYPE and $is_enabled and !$is_remote) { | ||
| 154 | # | ||
| 155 | # Perhaps do something if we get TELOPT_TTYPE switched on? | ||
| 156 | # | ||
| 157 | } | ||
| 158 | |||
| 159 | return 1; | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | # Callback for sub-option handling - for Net::Telnet. | ||
| 164 | # | ||
| 165 | sub subopt_callback { | ||
| 166 | my ($obj, $opt, $parameters) = @_; | ||
| 167 | my ($ors_old, $otm_old); | ||
| 168 | |||
| 169 | # Respond to TELOPT_TTYPE with "I'm a VT100". | ||
| 170 | # | ||
| 171 | if ($opt == TELOPT_TTYPE) { | ||
| 172 | $ors_old = $obj->output_record_separator (''); | ||
| 173 | $otm_old = $obj->telnetmode (0); | ||
| 174 | $obj->print ( | ||
| 175 | "\xff\xfa", | ||
| 176 | pack ('CC', $opt, 0), | ||
| 177 | 'vt100', | ||
| 178 | "\xff\xf0" | ||
| 179 | ); | ||
| 180 | $obj->telnetmode ($otm_old); | ||
| 181 | $obj->output_record_separator ($ors_old); | ||
| 182 | } | ||
| 183 | |||
| 184 | return 1; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | # Callback for OUTPUT events - for Term::VT102. | ||
| 189 | # | ||
| 190 | sub vt_output { | ||
| 191 | my ($vtobject, $type, $arg1, $arg2, $private) = @_; | ||
| 192 | |||
| 193 | if ($type eq 'OUTPUT') { | ||
| 194 | $private->print ($arg1); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | # Callback for ROWCHANGE events. This just sets a time value for the changed | ||
| 200 | # row using the private data as a hash reference - the time represents the | ||
| 201 | # earliest that row was changed since the last screen update. | ||
| 202 | # | ||
| 203 | sub vt_rowchange { | ||
| 204 | my ($vtobject, $type, $arg1, $arg2, $private) = @_; | ||
| 205 | $private->{$arg1} = time if (not exists $private->{$arg1}); | ||
| 206 | } | ||
| 207 | |||
| 208 | |||
| 209 | # Callback to trigger a full-screen repaint. | ||
| 210 | # | ||
| 211 | sub vt_changeall { | ||
| 212 | my ($vtobject, $type, $arg1, $arg2, $private) = @_; | ||
| 213 | for (my $row = 1; $row <= $vtobject->rows; $row++) { | ||
| 214 | $private->{$row} = 0; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | # EOF |