Commit | Line | Data |
a0d0e21e |
1 | package Term::Cap; |
2ef86165 |
2 | |
0ca33fed |
3 | # Since the debugger uses Term::ReadLine which uses Term::Cap, we want |
4 | # to load as few modules as possible. This includes Carp.pm. |
085f5cc4 |
5 | sub carp |
6 | { |
0ca33fed |
7 | require Carp; |
8 | goto &Carp::carp; |
9 | } |
10 | |
085f5cc4 |
11 | sub croak |
12 | { |
0ca33fed |
13 | require Carp; |
14 | goto &Carp::croak; |
15 | } |
16 | |
7cae2445 |
17 | use strict; |
85e6fe83 |
18 | |
d2492938 |
19 | use vars qw($VERSION $VMS_TERMCAP); |
7cae2445 |
20 | use vars qw($termpat $state $first $entry); |
2ef86165 |
21 | |
085f5cc4 |
22 | $VERSION = '1.10'; |
b75c8c73 |
23 | |
e14c93b3 |
24 | # Version undef: Thu Dec 14 20:02:42 CST 1995 by sanders@bsdi.com |
25 | # Version 1.00: Thu Nov 30 23:34:29 EST 2000 by schwern@pobox.com |
26 | # [PATCH] $VERSION crusade, strict, tests, etc... all over lib/ |
27 | # Version 1.01: Wed May 23 00:00:00 CST 2001 by d-lewart@uiuc.edu |
28 | # Avoid warnings in Tgetent and Tputs |
2ef86165 |
29 | # Version 1.02: Sat Nov 17 13:50:39 GMT 2001 by jns@gellyfish.com |
30 | # Altered layout of the POD |
31 | # Added Test::More to PREREQ_PM in Makefile.PL |
32 | # Fixed no argument Tgetent() |
2ab0daaa |
33 | # Version 1.03: Wed Nov 28 10:09:38 GMT 2001 |
34 | # VMS Support from Charles Lane <lane@DUPHY4.Physics.Drexel.Edu> |
2608af5d |
35 | # Version 1.04: Thu Nov 29 16:22:03 GMT 2001 |
36 | # Fixed warnings in test |
7cae2445 |
37 | # Version 1.05: Mon Dec 3 15:33:49 GMT 2001 |
38 | # Don't try to fall back on infocmp if it's not there. From chromatic. |
d2492938 |
39 | # Version 1.06: Thu Dec 6 18:43:22 GMT 2001 |
40 | # Preload the default VMS termcap from Charles Lane |
41 | # Don't carp at setting OSPEED unless warnings are on. |
8bb18277 |
42 | # Version 1.07: Wed Jan 2 21:35:09 GMT 2002 |
43 | # Sanity check on infocmp output from Norton Allen |
44 | # Repaired INSTALLDIRS thanks to Michael Schwern |
0ca33fed |
45 | # Version 1.08: Sat Sep 28 11:33:15 BST 2002 |
46 | # Late loading of 'Carp' as per Michael Schwern |
47 | # Version 1.09: Tue Apr 20 12:06:51 BST 2004 |
48 | # Merged in changes from and to Core |
49 | # Core (Fri Aug 30 14:15:55 CEST 2002): |
50 | # Cope with comments lines from 'infocmp' from Brendan O'Dea |
51 | # Allow for EBCDIC in Tgoto magic test. |
085f5cc4 |
52 | # Version 1.10: Thu Oct 18 16:52:20 BST 2007 |
53 | # Don't use try to use $ENV{HOME} if it doesn't exist |
54 | # Give Win32 'dumb' if TERM isn't set |
55 | # Provide fallback 'dumb' termcap entry as last resort |
a687059c |
56 | |
cb1a09d0 |
57 | # TODO: |
58 | # support Berkeley DB termcaps |
cb1a09d0 |
59 | # force $FH into callers package? |
60 | # keep $FH in object at Tgetent time? |
61 | |
62 | =head1 NAME |
63 | |
64 | Term::Cap - Perl termcap interface |
65 | |
66 | =head1 SYNOPSIS |
67 | |
68 | require Term::Cap; |
69 | $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed }; |
70 | $terminal->Trequire(qw/ce ku kd/); |
71 | $terminal->Tgoto('cm', $col, $row, $FH); |
72 | $terminal->Tputs('dl', $count, $FH); |
73 | $terminal->Tpad($string, $count, $FH); |
74 | |
75 | =head1 DESCRIPTION |
76 | |
77 | These are low-level functions to extract and use capabilities from |
78 | a terminal capability (termcap) database. |
79 | |
2ef86165 |
80 | More information on the terminal capabilities will be found in the |
81 | termcap manpage on most Unix-like systems. |
cb1a09d0 |
82 | |
2ef86165 |
83 | =head2 METHODS |
cb1a09d0 |
84 | |
2ef86165 |
85 | =over 4 |
cb1a09d0 |
86 | |
87 | The output strings for B<Tputs> are cached for counts of 1 for performance. |
88 | B<Tgoto> and B<Tpad> do not cache. C<$self-E<gt>{_xx}> is the raw termcap |
89 | data and C<$self-E<gt>{xx}> is the cached version. |
90 | |
91 | print $terminal->Tpad($self->{_xx}, 1); |
92 | |
93 | B<Tgoto>, B<Tputs>, and B<Tpad> return the string and will also |
94 | output the string to $FH if specified. |
95 | |
cb1a09d0 |
96 | |
97 | =cut |
98 | |
d2492938 |
99 | # Preload the default VMS termcap. |
100 | # If a different termcap is required then the text of one can be supplied |
101 | # in $Term::Cap::VMS_TERMCAP before Tgetent is called. |
102 | |
085f5cc4 |
103 | if ( $^O eq 'VMS' ) |
104 | { |
105 | chomp( my @entry = <DATA> ); |
106 | $VMS_TERMCAP = join '', @entry; |
d2492938 |
107 | } |
108 | |
cb1a09d0 |
109 | # Returns a list of termcap files to check. |
d2492938 |
110 | |
085f5cc4 |
111 | sub termcap_path |
112 | { ## private |
cb1a09d0 |
113 | my @termcap_path; |
085f5cc4 |
114 | |
cb1a09d0 |
115 | # $TERMCAP, if it's a filespec |
085f5cc4 |
116 | push( @termcap_path, $ENV{TERMCAP} ) |
117 | if ( |
118 | ( exists $ENV{TERMCAP} ) |
119 | && ( |
120 | ( $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'dos' ) |
121 | ? $ENV{TERMCAP} =~ /^[a-z]:[\\\/]/is |
122 | : $ENV{TERMCAP} =~ /^\//s |
123 | ) |
124 | ); |
125 | if ( ( exists $ENV{TERMPATH} ) && ( $ENV{TERMPATH} ) ) |
126 | { |
127 | |
128 | # Add the users $TERMPATH |
129 | push( @termcap_path, split( /(:|\s+)/, $ENV{TERMPATH} ) ); |
cb1a09d0 |
130 | } |
085f5cc4 |
131 | else |
132 | { |
133 | |
134 | # Defaults |
135 | push( @termcap_path, |
136 | exists $ENV{'HOME'} ? $ENV{'HOME'} . '/.termcap' : undef, |
137 | '/etc/termcap', '/usr/share/misc/termcap', ); |
a687059c |
138 | } |
d2492938 |
139 | |
cb1a09d0 |
140 | # return the list of those termcaps that exist |
085f5cc4 |
141 | return grep { defined $_ && -f $_ } @termcap_path; |
748a9306 |
142 | } |
143 | |
2ef86165 |
144 | =item B<Tgetent> |
145 | |
146 | Returns a blessed object reference which the user can |
147 | then use to send the control strings to the terminal using B<Tputs> |
148 | and B<Tgoto>. |
149 | |
150 | The function extracts the entry of the specified terminal |
151 | type I<TERM> (defaults to the environment variable I<TERM>) from the |
152 | database. |
153 | |
154 | It will look in the environment for a I<TERMCAP> variable. If |
155 | found, and the value does not begin with a slash, and the terminal |
156 | type name is the same as the environment string I<TERM>, the |
157 | I<TERMCAP> string is used instead of reading a termcap file. If |
158 | it does begin with a slash, the string is used as a path name of |
159 | the termcap file to search. If I<TERMCAP> does not begin with a |
160 | slash and name is different from I<TERM>, B<Tgetent> searches the |
161 | files F<$HOME/.termcap>, F</etc/termcap>, and F</usr/share/misc/termcap>, |
162 | in that order, unless the environment variable I<TERMPATH> exists, |
163 | in which case it specifies a list of file pathnames (separated by |
164 | spaces or colons) to be searched B<instead>. Whenever multiple |
165 | files are searched and a tc field occurs in the requested entry, |
166 | the entry it names must be found in the same file or one of the |
167 | succeeding files. If there is a C<:tc=...:> in the I<TERMCAP> |
168 | environment variable string it will continue the search in the |
169 | files as above. |
170 | |
171 | The extracted termcap entry is available in the object |
172 | as C<$self-E<gt>{TERMCAP}>. |
173 | |
174 | It takes a hash reference as an argument with two optional keys: |
175 | |
176 | =over 2 |
177 | |
178 | =item OSPEED |
179 | |
180 | The terminal output bit rate (often mistakenly called the baud rate) |
181 | for this terminal - if not set a warning will be generated |
182 | and it will be defaulted to 9600. I<OSPEED> can be be specified as |
183 | either a POSIX termios/SYSV termio speeds (where 9600 equals 9600) or |
184 | an old DSD-style speed ( where 13 equals 9600). |
185 | |
186 | |
187 | =item TERM |
188 | |
189 | The terminal type whose termcap entry will be used - if not supplied it will |
190 | default to $ENV{TERM}: if that is not set then B<Tgetent> will croak. |
191 | |
192 | =back |
193 | |
194 | It calls C<croak> on failure. |
195 | |
196 | =cut |
197 | |
085f5cc4 |
198 | sub Tgetent |
199 | { ## public -- static method |
cb1a09d0 |
200 | my $class = shift; |
2ef86165 |
201 | my ($self) = @_; |
202 | |
203 | $self = {} unless defined $self; |
204 | bless $self, $class; |
205 | |
085f5cc4 |
206 | my ( $term, $cap, $search, $field, $max, $tmp_term, $TERMCAP ); |
207 | local ( $termpat, $state, $first, $entry ); # used inside eval |
cb1a09d0 |
208 | local $_; |
209 | |
210 | # Compute PADDING factor from OSPEED (to be used by Tpad) |
085f5cc4 |
211 | if ( !$self->{OSPEED} ) |
212 | { |
213 | if ($^W) |
214 | { |
215 | carp "OSPEED was not set, defaulting to 9600"; |
d2492938 |
216 | } |
085f5cc4 |
217 | $self->{OSPEED} = 9600; |
cb1a09d0 |
218 | } |
085f5cc4 |
219 | if ( $self->{OSPEED} < 16 ) |
220 | { |
221 | |
222 | # delays for old style speeds |
223 | my @pad = ( |
224 | 0, 200, 133.3, 90.9, 74.3, 66.7, 50, 33.3, |
225 | 16.7, 8.3, 5.5, 4.1, 2, 1, .5, .2 |
226 | ); |
227 | $self->{PADDING} = $pad[ $self->{OSPEED} ]; |
cb1a09d0 |
228 | } |
085f5cc4 |
229 | else |
230 | { |
231 | $self->{PADDING} = 10000 / $self->{OSPEED}; |
cb1a09d0 |
232 | } |
233 | |
085f5cc4 |
234 | unless ( $self->{TERM} ) |
235 | { |
236 | if ( $ENV{TERM} ) |
237 | { |
238 | $self->{TERM} = $ENV{TERM} ; |
239 | } |
240 | else |
241 | { |
242 | if ( $^O eq 'Win32' ) |
243 | { |
244 | $self->{TERM} = 'dumb'; |
245 | } |
246 | else |
247 | { |
248 | croak "TERM not set"; |
249 | } |
250 | } |
251 | } |
252 | |
253 | $term = $self->{TERM}; # $term is the term type we are looking for |
cb1a09d0 |
254 | |
255 | # $tmp_term is always the next term (possibly :tc=...:) we are looking for |
256 | $tmp_term = $self->{TERM}; |
cb1a09d0 |
257 | |
085f5cc4 |
258 | # protect any pattern metacharacters in $tmp_term |
259 | $termpat = $tmp_term; |
260 | $termpat =~ s/(\W)/\\$1/g; |
261 | |
262 | my $foo = ( exists $ENV{TERMCAP} ? $ENV{TERMCAP} : '' ); |
cb1a09d0 |
263 | |
264 | # $entry is the extracted termcap entry |
085f5cc4 |
265 | if ( ( $foo !~ m:^/:s ) && ( $foo =~ m/(^|\|)${termpat}[:|]/s ) ) |
266 | { |
267 | $entry = $foo; |
cb1a09d0 |
268 | } |
269 | |
2ef86165 |
270 | my @termcap_path = termcap_path(); |
e66fb0c2 |
271 | |
085f5cc4 |
272 | unless ( @termcap_path || $entry ) |
e66fb0c2 |
273 | { |
2ab0daaa |
274 | |
085f5cc4 |
275 | # last resort--fake up a termcap from terminfo |
276 | local $ENV{TERM} = $term; |
277 | |
278 | if ( $^O eq 'VMS' ) |
279 | { |
280 | $entry = $VMS_TERMCAP; |
7cae2445 |
281 | } |
085f5cc4 |
282 | else |
283 | { |
284 | if ( grep { -x "$_/infocmp" } split /:/, $ENV{PATH} ) |
285 | { |
286 | eval { |
287 | my $tmp = `infocmp -C 2>/dev/null`; |
288 | $tmp =~ s/^#.*\n//gm; # remove comments |
289 | if ( ( $tmp !~ m%^/%s ) |
290 | && ( $tmp =~ /(^|\|)${termpat}[:|]/s ) ) |
291 | { |
292 | $entry = $tmp; |
293 | } |
294 | }; |
295 | } |
296 | else |
297 | { |
298 | # this is getting desperate now |
299 | if ( $self->{TERM} eq 'dumb' ) |
300 | { |
301 | $entry = 'dumb|80-column dumb tty::am::co#80::bl=^G:cr=^M:do=^J:sf=^J:'; |
302 | } |
303 | } |
7cae2445 |
304 | } |
2f6e8d9f |
305 | } |
e66fb0c2 |
306 | |
cb1a09d0 |
307 | croak "Can't find a valid termcap file" unless @termcap_path || $entry; |
308 | |
085f5cc4 |
309 | $state = 1; # 0 == finished |
310 | # 1 == next file |
311 | # 2 == search again |
cb1a09d0 |
312 | |
085f5cc4 |
313 | $first = 0; # first entry (keeps term name) |
cb1a09d0 |
314 | |
085f5cc4 |
315 | $max = 32; # max :tc=...:'s |
cb1a09d0 |
316 | |
085f5cc4 |
317 | if ($entry) |
318 | { |
319 | |
320 | # ok, we're starting with $TERMCAP |
321 | $first++; # we're the first entry |
322 | # do we need to continue? |
323 | if ( $entry =~ s/:tc=([^:]+):/:/ ) |
324 | { |
325 | $tmp_term = $1; |
326 | |
327 | # protect any pattern metacharacters in $tmp_term |
328 | $termpat = $tmp_term; |
329 | $termpat =~ s/(\W)/\\$1/g; |
330 | } |
331 | else |
332 | { |
333 | $state = 0; # we're already finished |
334 | } |
cb1a09d0 |
335 | } |
336 | |
337 | # This is eval'ed inside the while loop for each file |
338 | $search = q{ |
54310121 |
339 | while (<TERMCAP>) { |
cb1a09d0 |
340 | next if /^\\t/ || /^#/; |
341 | if ($_ =~ m/(^|\\|)${termpat}[:|]/o) { |
342 | chomp; |
343 | s/^[^:]*:// if $first++; |
344 | $state = 0; |
54310121 |
345 | while ($_ =~ s/\\\\$//) { |
346 | defined(my $x = <TERMCAP>) or last; |
347 | $_ .= $x; chomp; |
348 | } |
cb1a09d0 |
349 | last; |
748a9306 |
350 | } |
cb1a09d0 |
351 | } |
55497cff |
352 | defined $entry or $entry = ''; |
e14c93b3 |
353 | $entry .= $_ if $_; |
cb1a09d0 |
354 | }; |
748a9306 |
355 | |
085f5cc4 |
356 | while ( $state != 0 ) |
357 | { |
358 | if ( $state == 1 ) |
359 | { |
360 | |
361 | # get the next TERMCAP |
362 | $TERMCAP = shift @termcap_path |
363 | || croak "failed termcap lookup on $tmp_term"; |
364 | } |
365 | else |
366 | { |
367 | |
368 | # do the same file again |
369 | # prevent endless recursion |
370 | $max-- || croak "failed termcap loop at $tmp_term"; |
371 | $state = 1; # ok, maybe do a new file next time |
372 | } |
373 | |
374 | open( TERMCAP, "< $TERMCAP\0" ) || croak "open $TERMCAP: $!"; |
375 | eval $search; |
376 | die $@ if $@; |
377 | close TERMCAP; |
cb1a09d0 |
378 | |
085f5cc4 |
379 | # If :tc=...: found then search this file again |
380 | $entry =~ s/:tc=([^:]+):/:/ && ( $tmp_term = $1, $state = 2 ); |
cb1a09d0 |
381 | |
085f5cc4 |
382 | # protect any pattern metacharacters in $tmp_term |
383 | $termpat = $tmp_term; |
384 | $termpat =~ s/(\W)/\\$1/g; |
a687059c |
385 | } |
cb1a09d0 |
386 | |
387 | croak "Can't find $term" if $entry eq ''; |
085f5cc4 |
388 | $entry =~ s/:+\s*:+/:/g; # cleanup $entry |
389 | $entry =~ s/:+/:/g; # cleanup $entry |
390 | $self->{TERMCAP} = $entry; # save it |
391 | # print STDERR "DEBUG: $entry = ", $entry, "\n"; |
a687059c |
392 | |
748a9306 |
393 | # Precompile $entry into the object |
cb1a09d0 |
394 | $entry =~ s/^[^:]*://; |
085f5cc4 |
395 | foreach $field ( split( /:[\s:\\]*/, $entry ) ) |
396 | { |
397 | if ( defined $field && $field =~ /^(\w\w)$/ ) |
398 | { |
399 | $self->{ '_' . $field } = 1 unless defined $self->{ '_' . $1 }; |
400 | |
401 | # print STDERR "DEBUG: flag $1\n"; |
402 | } |
403 | elsif ( defined $field && $field =~ /^(\w\w)\@/ ) |
404 | { |
405 | $self->{ '_' . $1 } = ""; |
406 | |
407 | # print STDERR "DEBUG: unset $1\n"; |
408 | } |
409 | elsif ( defined $field && $field =~ /^(\w\w)#(.*)/ ) |
410 | { |
411 | $self->{ '_' . $1 } = $2 unless defined $self->{ '_' . $1 }; |
412 | |
413 | # print STDERR "DEBUG: numeric $1 = $2\n"; |
414 | } |
415 | elsif ( defined $field && $field =~ /^(\w\w)=(.*)/ ) |
416 | { |
417 | |
418 | # print STDERR "DEBUG: string $1 = $2\n"; |
419 | next if defined $self->{ '_' . ( $cap = $1 ) }; |
420 | $_ = $2; |
421 | s/\\E/\033/g; |
422 | s/\\(\d\d\d)/pack('c',oct($1) & 0177)/eg; |
423 | s/\\n/\n/g; |
424 | s/\\r/\r/g; |
425 | s/\\t/\t/g; |
426 | s/\\b/\b/g; |
427 | s/\\f/\f/g; |
428 | s/\\\^/\377/g; |
429 | s/\^\?/\177/g; |
430 | s/\^(.)/pack('c',ord($1) & 31)/eg; |
431 | s/\\(.)/$1/g; |
432 | s/\377/^/g; |
433 | $self->{ '_' . $cap } = $_; |
434 | } |
435 | |
436 | # else { carp "junk in $term ignored: $field"; } |
a687059c |
437 | } |
cb1a09d0 |
438 | $self->{'_pc'} = "\0" unless defined $self->{'_pc'}; |
439 | $self->{'_bc'} = "\b" unless defined $self->{'_bc'}; |
440 | $self; |
a687059c |
441 | } |
442 | |
cb1a09d0 |
443 | # $terminal->Tpad($string, $cnt, $FH); |
2ef86165 |
444 | |
445 | =item B<Tpad> |
446 | |
447 | Outputs a literal string with appropriate padding for the current terminal. |
448 | |
449 | It takes three arguments: |
450 | |
451 | =over 2 |
452 | |
453 | =item B<$string> |
454 | |
455 | The literal string to be output. If it starts with a number and an optional |
456 | '*' then the padding will be increased by an amount relative to this number, |
457 | if the '*' is present then this amount will me multiplied by $cnt. This part |
458 | of $string is removed before output/ |
459 | |
460 | =item B<$cnt> |
461 | |
462 | Will be used to modify the padding applied to string as described above. |
463 | |
464 | =item B<$FH> |
465 | |
466 | An optional filehandle (or IO::Handle ) that output will be printed to. |
467 | |
468 | =back |
469 | |
470 | The padded $string is returned. |
471 | |
472 | =cut |
473 | |
085f5cc4 |
474 | sub Tpad |
475 | { ## public |
cb1a09d0 |
476 | my $self = shift; |
085f5cc4 |
477 | my ( $string, $cnt, $FH ) = @_; |
478 | my ( $decr, $ms ); |
479 | |
480 | if ( defined $string && $string =~ /(^[\d.]+)(\*?)(.*)$/ ) |
481 | { |
482 | $ms = $1; |
483 | $ms *= $cnt if $2; |
484 | $string = $3; |
485 | $decr = $self->{PADDING}; |
486 | if ( $decr > .1 ) |
487 | { |
488 | $ms += $decr / 2; |
489 | $string .= $self->{'_pc'} x ( $ms / $decr ); |
490 | } |
a687059c |
491 | } |
492 | print $FH $string if $FH; |
493 | $string; |
494 | } |
495 | |
cb1a09d0 |
496 | # $terminal->Tputs($cap, $cnt, $FH); |
2ef86165 |
497 | |
498 | =item B<Tputs> |
499 | |
500 | Output the string for the given capability padded as appropriate without |
501 | any parameter substitution. |
502 | |
503 | It takes three arguments: |
504 | |
505 | =over 2 |
506 | |
507 | =item B<$cap> |
508 | |
509 | The capability whose string is to be output. |
510 | |
511 | =item B<$cnt> |
512 | |
513 | A count passed to Tpad to modify the padding applied to the output string. |
514 | If $cnt is zero or one then the resulting string will be cached. |
515 | |
516 | =item B<$FH> |
517 | |
518 | An optional filehandle (or IO::Handle ) that output will be printed to. |
519 | |
520 | =back |
521 | |
522 | The appropriate string for the capability will be returned. |
523 | |
524 | =cut |
525 | |
085f5cc4 |
526 | sub Tputs |
527 | { ## public |
cb1a09d0 |
528 | my $self = shift; |
085f5cc4 |
529 | my ( $cap, $cnt, $FH ) = @_; |
cb1a09d0 |
530 | my $string; |
748a9306 |
531 | |
2ef86165 |
532 | $cnt = 0 unless $cnt; |
533 | |
085f5cc4 |
534 | if ( $cnt > 1 ) |
535 | { |
536 | $string = Tpad( $self, $self->{ '_' . $cap }, $cnt ); |
537 | } |
538 | else |
539 | { |
540 | |
541 | # cache result because Tpad can be slow |
542 | unless ( exists $self->{$cap} ) |
543 | { |
544 | $self->{$cap} = |
545 | exists $self->{"_$cap"} |
546 | ? Tpad( $self, $self->{"_$cap"}, 1 ) |
547 | : undef; |
548 | } |
549 | $string = $self->{$cap}; |
748a9306 |
550 | } |
551 | print $FH $string if $FH; |
552 | $string; |
553 | } |
554 | |
cb1a09d0 |
555 | # $terminal->Tgoto($cap, $col, $row, $FH); |
2ef86165 |
556 | |
557 | =item B<Tgoto> |
558 | |
559 | B<Tgoto> decodes a cursor addressing string with the given parameters. |
560 | |
561 | There are four arguments: |
562 | |
563 | =over 2 |
564 | |
565 | =item B<$cap> |
566 | |
567 | The name of the capability to be output. |
568 | |
569 | =item B<$col> |
570 | |
571 | The first value to be substituted in the output string ( usually the column |
572 | in a cursor addressing capability ) |
573 | |
574 | =item B<$row> |
575 | |
576 | The second value to be substituted in the output string (usually the row |
577 | in cursor addressing capabilities) |
578 | |
579 | =item B<$FH> |
580 | |
581 | An optional filehandle (or IO::Handle ) to which the output string will be |
582 | printed. |
583 | |
584 | =back |
585 | |
586 | Substitutions are made with $col and $row in the output string with the |
587 | following sprintf() line formats: |
588 | |
589 | %% output `%' |
590 | %d output value as in printf %d |
591 | %2 output value as in printf %2d |
592 | %3 output value as in printf %3d |
593 | %. output value as in printf %c |
594 | %+x add x to value, then do %. |
595 | |
596 | %>xy if value > x then add y, no output |
597 | %r reverse order of two parameters, no output |
598 | %i increment by one, no output |
599 | %B BCD (16*(value/10)) + (value%10), no output |
600 | |
601 | %n exclusive-or all parameters with 0140 (Datamedia 2500) |
602 | %D Reverse coding (value - 2*(value%16)), no output (Delta Data) |
603 | |
604 | The output string will be returned. |
605 | |
606 | =cut |
607 | |
085f5cc4 |
608 | sub Tgoto |
609 | { ## public |
cb1a09d0 |
610 | my $self = shift; |
085f5cc4 |
611 | my ( $cap, $code, $tmp, $FH ) = @_; |
612 | my $string = $self->{ '_' . $cap }; |
cb1a09d0 |
613 | my $result = ''; |
085f5cc4 |
614 | my $after = ''; |
cb1a09d0 |
615 | my $online = 0; |
085f5cc4 |
616 | my @tmp = ( $tmp, $code ); |
617 | my $cnt = $code; |
618 | |
619 | while ( $string =~ /^([^%]*)%(.)(.*)/ ) |
620 | { |
621 | $result .= $1; |
622 | $code = $2; |
623 | $string = $3; |
624 | if ( $code eq 'd' ) |
625 | { |
626 | $result .= sprintf( "%d", shift(@tmp) ); |
627 | } |
628 | elsif ( $code eq '.' ) |
629 | { |
630 | $tmp = shift(@tmp); |
631 | if ( $tmp == 0 || $tmp == 4 || $tmp == 10 ) |
632 | { |
633 | if ($online) |
634 | { |
635 | ++$tmp, $after .= $self->{'_up'} if $self->{'_up'}; |
636 | } |
637 | else |
638 | { |
639 | ++$tmp, $after .= $self->{'_bc'}; |
640 | } |
641 | } |
642 | $result .= sprintf( "%c", $tmp ); |
643 | $online = !$online; |
644 | } |
645 | elsif ( $code eq '+' ) |
646 | { |
647 | $result .= sprintf( "%c", shift(@tmp) + ord($string) ); |
648 | $string = substr( $string, 1, 99 ); |
649 | $online = !$online; |
650 | } |
651 | elsif ( $code eq 'r' ) |
652 | { |
653 | ( $code, $tmp ) = @tmp; |
654 | @tmp = ( $tmp, $code ); |
655 | $online = !$online; |
656 | } |
657 | elsif ( $code eq '>' ) |
658 | { |
659 | ( $code, $tmp, $string ) = unpack( "CCa99", $string ); |
660 | if ( $tmp[$[] > $code ) |
661 | { |
662 | $tmp[$[] += $tmp; |
663 | } |
664 | } |
665 | elsif ( $code eq '2' ) |
666 | { |
667 | $result .= sprintf( "%02d", shift(@tmp) ); |
668 | $online = !$online; |
669 | } |
670 | elsif ( $code eq '3' ) |
671 | { |
672 | $result .= sprintf( "%03d", shift(@tmp) ); |
673 | $online = !$online; |
674 | } |
675 | elsif ( $code eq 'i' ) |
676 | { |
677 | ( $code, $tmp ) = @tmp; |
678 | @tmp = ( $code + 1, $tmp + 1 ); |
679 | } |
680 | else |
681 | { |
682 | return "OOPS"; |
683 | } |
a687059c |
684 | } |
085f5cc4 |
685 | $string = Tpad( $self, $result . $string . $after, $cnt ); |
748a9306 |
686 | print $FH $string if $FH; |
687 | $string; |
688 | } |
689 | |
cb1a09d0 |
690 | # $terminal->Trequire(qw/ce ku kd/); |
2ef86165 |
691 | |
692 | =item B<Trequire> |
693 | |
694 | Takes a list of capabilities as an argument and will croak if one is not |
695 | found. |
696 | |
697 | =cut |
698 | |
085f5cc4 |
699 | sub Trequire |
700 | { ## public |
cb1a09d0 |
701 | my $self = shift; |
085f5cc4 |
702 | my ( $cap, @undefined ); |
703 | foreach $cap (@_) |
704 | { |
705 | push( @undefined, $cap ) |
706 | unless defined $self->{ '_' . $cap } && $self->{ '_' . $cap }; |
748a9306 |
707 | } |
cb1a09d0 |
708 | croak "Terminal does not support: (@undefined)" if @undefined; |
a687059c |
709 | } |
710 | |
2ef86165 |
711 | =back |
712 | |
713 | =head1 EXAMPLES |
714 | |
715 | use Term::Cap; |
716 | |
717 | # Get terminal output speed |
718 | require POSIX; |
719 | my $termios = new POSIX::Termios; |
720 | $termios->getattr; |
721 | my $ospeed = $termios->getospeed; |
722 | |
723 | # Old-style ioctl code to get ospeed: |
724 | # require 'ioctl.pl'; |
725 | # ioctl(TTY,$TIOCGETP,$sgtty); |
726 | # ($ispeed,$ospeed) = unpack('cc',$sgtty); |
727 | |
728 | # allocate and initialize a terminal structure |
729 | $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed }; |
730 | |
731 | # require certain capabilities to be available |
732 | $terminal->Trequire(qw/ce ku kd/); |
733 | |
734 | # Output Routines, if $FH is undefined these just return the string |
cb1a09d0 |
735 | |
2ef86165 |
736 | # Tgoto does the % expansion stuff with the given args |
737 | $terminal->Tgoto('cm', $col, $row, $FH); |
738 | |
739 | # Tputs doesn't do any % expansion. |
740 | $terminal->Tputs('dl', $count = 1, $FH); |
741 | |
742 | =head1 COPYRIGHT AND LICENSE |
743 | |
744 | Please see the README file in distribution. |
745 | |
746 | =head1 AUTHOR |
747 | |
748 | This module is part of the core Perl distribution and is also maintained |
749 | for CPAN by Jonathan Stowe <jns@gellyfish.com>. |
750 | |
751 | =head1 SEE ALSO |
752 | |
753 | termcap(5) |
754 | |
755 | =cut |
2ab0daaa |
756 | |
757 | # Below is a default entry for systems where there are terminals but no |
758 | # termcap |
759 | 1; |
a69e7784 |
760 | __DATA__ |
2ab0daaa |
761 | vt220|vt200|DEC VT220 in vt100 emulation mode: |
762 | am:mi:xn:xo: |
763 | co#80:li#24: |
764 | RA=\E[?7l:SA=\E[?7h: |
765 | ac=kkllmmjjnnwwqquuttvvxx:ae=\E(B:al=\E[L:as=\E(0: |
766 | bl=^G:cd=\E[J:ce=\E[K:cl=\E[H\E[2J:cm=\E[%i%d;%dH: |
767 | cr=^M:cs=\E[%i%d;%dr:dc=\E[P:dl=\E[M:do=\E[B: |
768 | ei=\E[4l:ho=\E[H:im=\E[4h: |
769 | is=\E[1;24r\E[24;1H: |
770 | nd=\E[C: |
771 | kd=\E[B::kl=\E[D:kr=\E[C:ku=\E[A:le=^H: |
772 | mb=\E[5m:md=\E[1m:me=\E[m:mr=\E[7m: |
773 | kb=\0177: |
774 | r2=\E>\E[24;1H\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h\E=:rc=\E8: |
775 | sc=\E7:se=\E[27m:sf=\ED:so=\E[7m:sr=\EM:ta=^I: |
776 | ue=\E[24m:up=\E[A:us=\E[4m:ve=\E[?25h:vi=\E[?25l: |
777 | |