📄 cap.pm
字号:
package Term::Cap;# Since the debugger uses Term::ReadLine which uses Term::Cap, we want# to load as few modules as possible. This includes Carp.pm.sub carp { require Carp; goto &Carp::carp;}sub croak { require Carp; goto &Carp::croak;}use strict;use vars qw($VERSION $VMS_TERMCAP);use vars qw($termpat $state $first $entry);$VERSION = '1.09';# Version undef: Thu Dec 14 20:02:42 CST 1995 by sanders@bsdi.com# Version 1.00: Thu Nov 30 23:34:29 EST 2000 by schwern@pobox.com# [PATCH] $VERSION crusade, strict, tests, etc... all over lib/# Version 1.01: Wed May 23 00:00:00 CST 2001 by d-lewart@uiuc.edu# Avoid warnings in Tgetent and Tputs# Version 1.02: Sat Nov 17 13:50:39 GMT 2001 by jns@gellyfish.com# Altered layout of the POD# Added Test::More to PREREQ_PM in Makefile.PL# Fixed no argument Tgetent()# Version 1.03: Wed Nov 28 10:09:38 GMT 2001# VMS Support from Charles Lane <lane@DUPHY4.Physics.Drexel.Edu># Version 1.04: Thu Nov 29 16:22:03 GMT 2001# Fixed warnings in test# Version 1.05: Mon Dec 3 15:33:49 GMT 2001# Don't try to fall back on infocmp if it's not there. From chromatic.# Version 1.06: Thu Dec 6 18:43:22 GMT 2001# Preload the default VMS termcap from Charles Lane# Don't carp at setting OSPEED unless warnings are on.# Version 1.07: Wed Jan 2 21:35:09 GMT 2002# Sanity check on infocmp output from Norton Allen# Repaired INSTALLDIRS thanks to Michael Schwern# Version 1.08: Sat Sep 28 11:33:15 BST 2002# Late loading of 'Carp' as per Michael Schwern# Version 1.09: Tue Apr 20 12:06:51 BST 2004# Merged in changes from and to Core# Core (Fri Aug 30 14:15:55 CEST 2002):# Cope with comments lines from 'infocmp' from Brendan O'Dea# Allow for EBCDIC in Tgoto magic test.# TODO:# support Berkeley DB termcaps# should probably be a .xs module# force $FH into callers package?# keep $FH in object at Tgetent time?=head1 NAMETerm::Cap - Perl termcap interface=head1 SYNOPSIS require Term::Cap; $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed }; $terminal->Trequire(qw/ce ku kd/); $terminal->Tgoto('cm', $col, $row, $FH); $terminal->Tputs('dl', $count, $FH); $terminal->Tpad($string, $count, $FH);=head1 DESCRIPTIONThese are low-level functions to extract and use capabilities froma terminal capability (termcap) database.More information on the terminal capabilities will be found in thetermcap manpage on most Unix-like systems.=head2 METHODS=over 4The output strings for B<Tputs> are cached for counts of 1 for performance.B<Tgoto> and B<Tpad> do not cache. C<$self-E<gt>{_xx}> is the raw termcapdata and C<$self-E<gt>{xx}> is the cached version. print $terminal->Tpad($self->{_xx}, 1);B<Tgoto>, B<Tputs>, and B<Tpad> return the string and will alsooutput the string to $FH if specified.=cut# Preload the default VMS termcap.# If a different termcap is required then the text of one can be supplied# in $Term::Cap::VMS_TERMCAP before Tgetent is called.if ( $^O eq 'VMS') { chomp (my @entry = <DATA>); $VMS_TERMCAP = join '', @entry;}# Returns a list of termcap files to check.sub termcap_path { ## private my @termcap_path; # $TERMCAP, if it's a filespec push(@termcap_path, $ENV{TERMCAP}) if ((exists $ENV{TERMCAP}) && (($^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'dos') ? $ENV{TERMCAP} =~ /^[a-z]:[\\\/]/is : $ENV{TERMCAP} =~ /^\//s)); if ((exists $ENV{TERMPATH}) && ($ENV{TERMPATH})) { # Add the users $TERMPATH push(@termcap_path, split(/(:|\s+)/, $ENV{TERMPATH})) } else { # Defaults push(@termcap_path, $ENV{'HOME'} . '/.termcap', '/etc/termcap', '/usr/share/misc/termcap', ); } # return the list of those termcaps that exist return grep(-f, @termcap_path);}=item B<Tgetent>Returns a blessed object reference which the user canthen use to send the control strings to the terminal using B<Tputs>and B<Tgoto>.The function extracts the entry of the specified terminaltype I<TERM> (defaults to the environment variable I<TERM>) from thedatabase.It will look in the environment for a I<TERMCAP> variable. Iffound, and the value does not begin with a slash, and the terminaltype name is the same as the environment string I<TERM>, theI<TERMCAP> string is used instead of reading a termcap file. Ifit does begin with a slash, the string is used as a path name ofthe termcap file to search. If I<TERMCAP> does not begin with aslash and name is different from I<TERM>, B<Tgetent> searches thefiles F<$HOME/.termcap>, F</etc/termcap>, and F</usr/share/misc/termcap>,in that order, unless the environment variable I<TERMPATH> exists,in which case it specifies a list of file pathnames (separated byspaces or colons) to be searched B<instead>. Whenever multiplefiles are searched and a tc field occurs in the requested entry,the entry it names must be found in the same file or one of thesucceeding files. If there is a C<:tc=...:> in the I<TERMCAP>environment variable string it will continue the search in thefiles as above.The extracted termcap entry is available in the objectas C<$self-E<gt>{TERMCAP}>.It takes a hash reference as an argument with two optional keys:=over 2=item OSPEEDThe terminal output bit rate (often mistakenly called the baud rate)for this terminal - if not set a warning will be generatedand it will be defaulted to 9600. I<OSPEED> can be be specified aseither a POSIX termios/SYSV termio speeds (where 9600 equals 9600) oran old DSD-style speed ( where 13 equals 9600).=item TERMThe terminal type whose termcap entry will be used - if not supplied it willdefault to $ENV{TERM}: if that is not set then B<Tgetent> will croak.=backIt calls C<croak> on failure.=cutsub Tgetent { ## public -- static method my $class = shift; my ($self) = @_; $self = {} unless defined $self; bless $self, $class; my($term,$cap,$search,$field,$max,$tmp_term,$TERMCAP); local($termpat,$state,$first,$entry); # used inside eval local $_; # Compute PADDING factor from OSPEED (to be used by Tpad) if (! $self->{OSPEED}) { if ( $^W ) { carp "OSPEED was not set, defaulting to 9600"; } $self->{OSPEED} = 9600; } if ($self->{OSPEED} < 16) { # delays for old style speeds 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); $self->{PADDING} = $pad[$self->{OSPEED}]; } else { $self->{PADDING} = 10000 / $self->{OSPEED}; } $self->{TERM} = ($self->{TERM} || $ENV{TERM} || croak "TERM not set"); $term = $self->{TERM}; # $term is the term type we are looking for # $tmp_term is always the next term (possibly :tc=...:) we are looking for $tmp_term = $self->{TERM}; # protect any pattern metacharacters in $tmp_term $termpat = $tmp_term; $termpat =~ s/(\W)/\\$1/g; my $foo = (exists $ENV{TERMCAP} ? $ENV{TERMCAP} : ''); # $entry is the extracted termcap entry if (($foo !~ m:^/:s) && ($foo =~ m/(^|\|)${termpat}[:|]/s)) { $entry = $foo; } my @termcap_path = termcap_path(); unless (@termcap_path || $entry) { # last resort--fake up a termcap from terminfo local $ENV{TERM} = $term; if ( $^O eq 'VMS' ) { $entry = $VMS_TERMCAP; } else { if ( grep { -x "$_/infocmp" } split /:/, $ENV{PATH} ) { eval { my $tmp = `infocmp -C 2>/dev/null`; $tmp =~ s/^#.*\n//gm; # remove comments if (( $tmp !~ m%^/%s ) && ( $tmp =~ /(^|\|)${termpat}[:|]/s)) { $entry = $tmp; } }; } } } croak "Can't find a valid termcap file" unless @termcap_path || $entry; $state = 1; # 0 == finished # 1 == next file # 2 == search again $first = 0; # first entry (keeps term name) $max = 32; # max :tc=...:'s if ($entry) { # ok, we're starting with $TERMCAP $first++; # we're the first entry # do we need to continue? if ($entry =~ s/:tc=([^:]+):/:/) { $tmp_term = $1; # protect any pattern metacharacters in $tmp_term $termpat = $tmp_term; $termpat =~ s/(\W)/\\$1/g; } else { $state = 0; # we're already finished } } # This is eval'ed inside the while loop for each file $search = q{ while (<TERMCAP>) { next if /^\\t/ || /^#/; if ($_ =~ m/(^|\\|)${termpat}[:|]/o) { chomp; s/^[^:]*:// if $first++; $state = 0; while ($_ =~ s/\\\\$//) { defined(my $x = <TERMCAP>) or last; $_ .= $x; chomp; } last; } } defined $entry or $entry = ''; $entry .= $_ if $_; }; while ($state != 0) { if ($state == 1) { # get the next TERMCAP $TERMCAP = shift @termcap_path || croak "failed termcap lookup on $tmp_term"; } else { # do the same file again # prevent endless recursion $max-- || croak "failed termcap loop at $tmp_term"; $state = 1; # ok, maybe do a new file next time } open(TERMCAP,"< $TERMCAP\0") || croak "open $TERMCAP: $!"; eval $search; die $@ if $@; close TERMCAP; # If :tc=...: found then search this file again $entry =~ s/:tc=([^:]+):/:/ && ($tmp_term = $1, $state = 2); # protect any pattern metacharacters in $tmp_term $termpat = $tmp_term; $termpat =~ s/(\W)/\\$1/g; } croak "Can't find $term" if $entry eq ''; $entry =~ s/:+\s*:+/:/g; # cleanup $entry $entry =~ s/:+/:/g; # cleanup $entry $self->{TERMCAP} = $entry; # save it # print STDERR "DEBUG: $entry = ", $entry, "\n"; # Precompile $entry into the object $entry =~ s/^[^:]*://; foreach $field (split(/:[\s:\\]*/,$entry)) { if (defined $field && $field =~ /^(\w\w)$/) { $self->{'_' . $field} = 1 unless defined $self->{'_' . $1}; # print STDERR "DEBUG: flag $1\n"; } elsif (defined $field && $field =~ /^(\w\w)\@/) { $self->{'_' . $1} = ""; # print STDERR "DEBUG: unset $1\n"; } elsif (defined $field && $field =~ /^(\w\w)#(.*)/) { $self->{'_' . $1} = $2 unless defined $self->{'_' . $1};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -