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