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