This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[PATCH 3/3] Provide wrappers for IN6ADDR_ANY and IN6ADDR_LOOPBACK
[perl5.git] / lib / termcap.pl
CommitLineData
0111154e
Z
1warn "Legacy library @{[(caller(0))[6]]} will be removed from the Perl core distribution in the next major release. Please install it from the CPAN distribution Perl4::CoreLibs. It is being used at @{[(caller)[1]]}, line @{[(caller)[2]]}.\n";
2
a0d0e21e 3;# $RCSfile: termcap.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:16 $
a6d71656
GS
4#
5# This library is no longer being maintained, and is included for backward
6# compatibility with Perl 4 programs which may require it.
9748ec9a
S
7# This legacy library is deprecated and will be removed in a future
8# release of perl.
a6d71656
GS
9#
10# In particular, this should not be used as an example of modern Perl
11# programming techniques.
12#
13# Suggested alternative: Term::Cap
14#
9748ec9a 15
a687059c
LW
16;#
17;# Usage:
e929a76b 18;# require 'ioctl.pl';
a687059c
LW
19;# ioctl(TTY,$TIOCGETP,$foo);
20;# ($ispeed,$ospeed) = unpack('cc',$foo);
a0d0e21e 21;# require 'termcap.pl';
9f68db38
LW
22;# &Tgetent('vt100'); # sets $TC{'cm'}, etc.
23;# &Tputs(&Tgoto($TC{'cm'},$col,$row), 0, 'FILEHANDLE');
24;# &Tputs($TC{'dl'},$affcnt,'FILEHANDLE');
a687059c
LW
25;#
26sub Tgetent {
27 local($TERM) = @_;
28 local($TERMCAP,$_,$entry,$loop,$field);
29
a60e505a 30 # warn "Tgetent: no ospeed set" unless $ospeed;
40da2db3 31 foreach $key (keys %TC) {
a687059c
LW
32 delete $TC{$key};
33 }
34 $TERM = $ENV{'TERM'} unless $TERM;
a0d0e21e 35 $TERM =~ s/(\W)/\\$1/g;
a687059c
LW
36 $TERMCAP = $ENV{'TERMCAP'};
37 $TERMCAP = '/etc/termcap' unless $TERMCAP;
38 if ($TERMCAP !~ m:^/:) {
7c0587c8 39 if ($TERMCAP !~ /(^|\|)$TERM[:\|]/) {
a687059c
LW
40 $TERMCAP = '/etc/termcap';
41 }
42 }
43 if ($TERMCAP =~ m:^/:) {
44 $entry = '';
45 do {
46 $loop = "
47 open(TERMCAP,'<$TERMCAP') || die \"Can't open $TERMCAP\";
48 while (<TERMCAP>) {
49 next if /^#/;
50 next if /^\t/;
a0d0e21e 51 if (/(^|\\|)${TERM}[:\\|]/) {
a687059c
LW
52 chop;
53 while (chop eq '\\\\') {
54 \$_ .= <TERMCAP>;
55 chop;
56 }
57 \$_ .= ':';
58 last;
59 }
60 }
61 close TERMCAP;
62 \$entry .= \$_;
63 ";
64 eval $loop;
9f68db38 65 } while s/:tc=([^:]+):/:/ && ($TERM = $1);
a687059c
LW
66 $TERMCAP = $entry;
67 }
68
69 foreach $field (split(/:[\s:\\]*/,$TERMCAP)) {
70 if ($field =~ /^\w\w$/) {
71 $TC{$field} = 1;
72 }
73 elsif ($field =~ /^(\w\w)#(.*)/) {
a0d0e21e 74 $TC{$1} = $2 if $TC{$1} eq '';
a687059c
LW
75 }
76 elsif ($field =~ /^(\w\w)=(.*)/) {
77 $entry = $1;
78 $_ = $2;
79 s/\\E/\033/g;
55497cff 80 s/\\(200)/pack('c',0)/eg; # NUL character
81 s/\\(0\d\d)/pack('c',oct($1))/eg; # octal
82 s/\\(0x[0-9A-Fa-f][0-9A-Fa-f])/pack('c',hex($1))/eg; # hex
a687059c
LW
83 s/\\(\d\d\d)/pack('c',$1 & 0177)/eg;
84 s/\\n/\n/g;
85 s/\\r/\r/g;
86 s/\\t/\t/g;
87 s/\\b/\b/g;
88 s/\\f/\f/g;
89 s/\\\^/\377/g;
90 s/\^\?/\177/g;
63f2c1e1 91 s/\^(.)/pack('c',ord($1) & 31)/eg;
a687059c
LW
92 s/\\(.)/$1/g;
93 s/\377/^/g;
a0d0e21e 94 $TC{$entry} = $_ if $TC{$entry} eq '';
a687059c
LW
95 }
96 }
a0d0e21e
LW
97 $TC{'pc'} = "\0" if $TC{'pc'} eq '';
98 $TC{'bc'} = "\b" if $TC{'bc'} eq '';
a687059c
LW
99}
100
101@Tputs = (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);
102
103sub Tputs {
104 local($string,$affcnt,$FH) = @_;
105 local($ms);
106 if ($string =~ /(^[\d.]+)(\*?)(.*)$/) {
107 $ms = $1;
108 $ms *= $affcnt if $2;
109 $string = $3;
110 $decr = $Tputs[$ospeed];
111 if ($decr > .1) {
112 $ms += $decr / 2;
113 $string .= $TC{'pc'} x ($ms / $decr);
114 }
115 }
116 print $FH $string if $FH;
117 $string;
118}
119
120sub Tgoto {
121 local($string) = shift(@_);
122 local($result) = '';
123 local($after) = '';
124 local($code,$tmp) = @_;
9f68db38
LW
125 local(@tmp);
126 @tmp = ($tmp,$code);
a687059c
LW
127 local($online) = 0;
128 while ($string =~ /^([^%]*)%(.)(.*)/) {
129 $result .= $1;
130 $code = $2;
131 $string = $3;
132 if ($code eq 'd') {
9f68db38 133 $result .= sprintf("%d",shift(@tmp));
a687059c
LW
134 }
135 elsif ($code eq '.') {
9f68db38 136 $tmp = shift(@tmp);
a687059c
LW
137 if ($tmp == 0 || $tmp == 4 || $tmp == 10) {
138 if ($online) {
139 ++$tmp, $after .= $TC{'up'} if $TC{'up'};
140 }
141 else {
142 ++$tmp, $after .= $TC{'bc'};
143 }
144 }
145 $result .= sprintf("%c",$tmp);
146 $online = !$online;
147 }
148 elsif ($code eq '+') {
9f68db38 149 $result .= sprintf("%c",shift(@tmp)+ord($string));
a687059c
LW
150 $string = substr($string,1,99);
151 $online = !$online;
152 }
153 elsif ($code eq 'r') {
9f68db38
LW
154 ($code,$tmp) = @tmp;
155 @tmp = ($tmp,$code);
a687059c
LW
156 $online = !$online;
157 }
158 elsif ($code eq '>') {
159 ($code,$tmp,$string) = unpack("CCa99",$string);
859172fe
Z
160 if ($tmp[0] > $code) {
161 $tmp[0] += $tmp;
a687059c
LW
162 }
163 }
164 elsif ($code eq '2') {
9f68db38 165 $result .= sprintf("%02d",shift(@tmp));
a687059c
LW
166 $online = !$online;
167 }
168 elsif ($code eq '3') {
9f68db38 169 $result .= sprintf("%03d",shift(@tmp));
a687059c
LW
170 $online = !$online;
171 }
172 elsif ($code eq 'i') {
9f68db38
LW
173 ($code,$tmp) = @tmp;
174 @tmp = ($code+1,$tmp+1);
a687059c
LW
175 }
176 else {
177 return "OOPS";
178 }
179 }
180 $result . $string . $after;
181}
182
1831;