1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
# Term::Cap.pm -- Termcap interface routines
package Term::Cap;
# Converted to package on 25 Feb 1994 <sanders@bsdi.com>
#
# Usage:
# require 'ioctl.pl';
# ioctl(TTY,$TIOCGETP,$sgtty);
# ($ispeed,$ospeed) = unpack('cc',$sgtty);
#
# require Term::Cap;
#
# $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
# sets $term->{'_cm'}, etc.
# $this->Trequire(qw/ce ku kd/);
# die unless entries are defined for the terminal
# $term->Tgoto('cm', $col, $row, $FH);
# $term->Tputs('dl', $cnt = 1, $FH);
# $this->Tpad($string, $cnt = 1, $FH);
# processes a termcap string and adds padding if needed
# if $FH is undefined these just return the string
#
# CHANGES:
# Converted to package
# Allows :tc=...: in $ENV{'TERMCAP'} (flows to default termcap file)
# Now die's properly if it can't open $TERMCAP or if the eval $loop fails
# Tputs() results are cached (use Tgoto or Tpad to avoid)
# Tgoto() will do output if $FH is passed (like Tputs without caching)
# Supports POSIX termios speeds and old style speeds
# Searches termcaps properly (TERMPATH, etc)
# The output routines are optimized for cached Tputs().
# $this->{_xx} is the raw termcap data and $this->{xx} is a
# cached and padded string for count == 1.
#
# internal routines
sub getenv { defined $ENV{$_[0]} ? $ENV{$_[0]} : ''; }
sub termcap_path {
local @termcap_path = ('/etc/termcap', '/usr/share/misc/termcap');
local $v;
if ($v = getenv(TERMPATH)) {
# user specified path
@termcap_path = split(':', $v);
} else {
# default path
@termcap_path = ('/etc/termcap', '/usr/share/misc/termcap');
$v = getenv(HOME);
unshift(@termcap_path, $v . '/.termcap') if $v;
}
# we always search TERMCAP first
$v = getenv(TERMCAP);
unshift(@termcap_path, $v) if $v =~ /^\//;
grep(-f, @termcap_path);
}
sub Tgetent {
local($type) = shift;
local($this) = @_;
local($TERM,$TERMCAP,$term,$entry,$cap,$loop,$field,$entry,$_);
warn "Tgetent: no ospeed set\n" unless $this->{OSPEED} > 0;
$this->{DECR} = 10000 / $this->{OSPEED} if $this->{OSPEED} > 50;
$term = $TERM = $this->{TERM} =
$this->{TERM} || getenv(TERM) || die "Tgetent: TERM not set\n";
$TERMCAP = getenv(TERMCAP);
$TERMCAP = '' if $TERMCAP =~ m:^/: || $TERMCAP !~ /(^|\|)$TERM[:\|]/;
local @termcap_path = &termcap_path;
die "Tgetent: Can't find a valid termcap file\n"
unless @termcap_path || $TERMCAP;
# handle environment TERMCAP, setup for continuation if needed
$entry = $TERMCAP;
$entry =~ s/:tc=([^:]+):/:/ && ($TERM = $1);
if ($TERMCAP eq '' || $1) { # the search goes on
local $first = $TERMCAP eq '' ? 1 : 0; # make it pretty
local $max = 32; # max :tc=...:'s
local $state = 1; # 0 == finished
# 1 == next file
# 2 == search again
do {
if ($state == 1) {
$TERMCAP = shift @termcap_path
|| die "Tgetent: failed lookup on $TERM\n";
} else {
$max-- || die "Tgetent: termcap loop at $TERM\n";
$state = 1; # back to default state
}
open(TERMCAP,"< $TERMCAP\0") || die "Tgetent: $TERMCAP: $!\n";
# print STDERR "Trying... $TERMCAP\n";
$loop = "
while (<TERMCAP>) {
next if /^\t/;
next if /^#/;
if (/(^|\\|)${TERM}[:\\|]/) {
chop;
s/^[^:]*:// unless \$first++;
\$state = 0;
while (chop eq '\\\\') {
\$_ .= <TERMCAP>;
chop;
}
\$_ .= ':';
last;
}
}
\$entry .= \$_;
";
eval $loop;
die $@ if $@;
#print STDERR "$TERM: $_\n--------\n"; # DEBUG
close TERMCAP;
# If :tc=...: found then search this file again
$entry =~ s/:tc=([^:]+):/:/ && ($TERM = $1, $state = 2);
} while $state != 0;
}
die "Tgetent: Can't find $term\n" unless $entry ne '';
$entry =~ s/:\s+:/:/g;
$this->{TERMCAP} = $entry;
#print STDERR $entry, "\n"; # DEBUG
# Precompile $entry into the object
foreach $field (split(/:[\s:\\]*/,$entry)) {
if ($field =~ /^\w\w$/) {
$this->{'_' . $field} = 1 unless defined $this->{'_' . $1};
}
elsif ($field =~ /^(\w\w)\@/) {
$this->{'_' . $1} = "";
}
elsif ($field =~ /^(\w\w)#(.*)/) {
$this->{'_' . $1} = $2 unless defined $this->{'_' . $1};
}
elsif ($field =~ /^(\w\w)=(.*)/) {
next if defined $this->{'_' . ($cap = $1)};
$_ = $2;
s/\\E/\033/g;
s/\\(\d\d\d)/pack('c',oct($1) & 0177)/eg;
s/\\n/\n/g;
s/\\r/\r/g;
s/\\t/\t/g;
s/\\b/\b/g;
s/\\f/\f/g;
s/\\\^/\377/g;
s/\^\?/\177/g;
s/\^(.)/pack('c',ord($1) & 31)/eg;
s/\\(.)/$1/g;
s/\377/^/g;
$this->{'_' . $cap} = $_;
}
# else { warn "Tgetent: junk in $term: $field\n"; }
}
$this->{'_pc'} = "\0" unless defined $this->{'_pc'};
$this->{'_bc'} = "\b" unless defined $this->{'_bc'};
$this;
}
# delays for old style speeds
@Tpad = (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);
# $term->Tpad($string, $cnt, $FH);
sub Tpad {
local($this, $string, $cnt, $FH) = @_;
local($decr, $ms);
if ($string =~ /(^[\d.]+)(\*?)(.*)$/) {
$ms = $1;
$ms *= $cnt if $2;
$string = $3;
$decr = $this->{OSPEED} < 50 ? $Tpad[$this->{OSPEED}] : $this->{DECR};
if ($decr > .1) {
$ms += $decr / 2;
$string .= $this->{'_pc'} x ($ms / $decr);
}
}
print $FH $string if $FH;
$string;
}
# $term->Tputs($cap, $cnt, $FH);
sub Tputs {
local($this, $cap, $cnt, $FH) = @_;
local $string;
if ($cnt > 1) {
$string = Tpad($this, $this->{'_' . $cap}, $cnt);
} else {
$string = defined $this->{$cap} ? $this->{$cap} :
($this->{$cap} = Tpad($this, $this->{'_' . $cap}, 1));
}
print $FH $string if $FH;
$string;
}
# %% output `%'
# %d output value as in printf %d
# %2 output value as in printf %2d
# %3 output value as in printf %3d
# %. output value as in printf %c
# %+x add x to value, then do %.
#
# %>xy if value > x then add y, no output
# %r reverse order of two parameters, no output
# %i increment by one, no output
# %B BCD (16*(value/10)) + (value%10), no output
#
# %n exclusive-or all parameters with 0140 (Datamedia 2500)
# %D Reverse coding (value - 2*(value%16)), no output (Delta Data)
#
# $term->Tgoto($cap, $col, $row, $FH);
sub Tgoto {
local($this, $cap, $code, $tmp, $FH) = @_;
local $string = $this->{'_' . $cap};
local $result = '';
local $after = '';
local $online = 0;
local @tmp = ($tmp,$code);
local $cnt = $code;
while ($string =~ /^([^%]*)%(.)(.*)/) {
$result .= $1;
$code = $2;
$string = $3;
if ($code eq 'd') {
$result .= sprintf("%d",shift(@tmp));
}
elsif ($code eq '.') {
$tmp = shift(@tmp);
if ($tmp == 0 || $tmp == 4 || $tmp == 10) {
if ($online) {
++$tmp, $after .= $this->{'_up'} if $this->{'_up'};
}
else {
++$tmp, $after .= $this->{'_bc'};
}
}
$result .= sprintf("%c",$tmp);
$online = !$online;
}
elsif ($code eq '+') {
$result .= sprintf("%c",shift(@tmp)+ord($string));
$string = substr($string,1,99);
$online = !$online;
}
elsif ($code eq 'r') {
($code,$tmp) = @tmp;
@tmp = ($tmp,$code);
$online = !$online;
}
elsif ($code eq '>') {
($code,$tmp,$string) = unpack("CCa99",$string);
if ($tmp[$[] > $code) {
$tmp[$[] += $tmp;
}
}
elsif ($code eq '2') {
$result .= sprintf("%02d",shift(@tmp));
$online = !$online;
}
elsif ($code eq '3') {
$result .= sprintf("%03d",shift(@tmp));
$online = !$online;
}
elsif ($code eq 'i') {
($code,$tmp) = @tmp;
@tmp = ($code+1,$tmp+1);
}
else {
return "OOPS";
}
}
$string = Tpad($this, $result . $string . $after, $cnt);
print $FH $string if $FH;
$string;
}
# $this->Trequire($cap1, $cap2, ...);
sub Trequire {
local $this = shift;
local $_;
foreach (@_) {
die "Trequire: Terminal does not support: $_\n"
unless defined $this->{'_' . $_} && $this->{'_' . $_};
}
}
1;
|