Cap.pm 21 KB

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