Tweaks to constant.pm
[p5sagit/p5-mst-13.2.git] / lib / Term / Cap.pm
CommitLineData
a0d0e21e 1package Term::Cap;
cb1a09d0 2use Carp;
85e6fe83 3
cb1a09d0 4# Last updated: Thu Dec 14 20:02:42 CST 1995 by sanders@bsdi.com
a687059c 5
cb1a09d0 6# TODO:
7# support Berkeley DB termcaps
8# should probably be a .xs module
9# force $FH into callers package?
10# keep $FH in object at Tgetent time?
11
12=head1 NAME
13
14Term::Cap - Perl termcap interface
15
16=head1 SYNOPSIS
17
18 require Term::Cap;
19 $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
20 $terminal->Trequire(qw/ce ku kd/);
21 $terminal->Tgoto('cm', $col, $row, $FH);
22 $terminal->Tputs('dl', $count, $FH);
23 $terminal->Tpad($string, $count, $FH);
24
25=head1 DESCRIPTION
26
27These are low-level functions to extract and use capabilities from
28a terminal capability (termcap) database.
29
30The B<Tgetent> function extracts the entry of the specified terminal
31type I<TERM> (defaults to the environment variable I<TERM>) from the
32database.
33
34It will look in the environment for a I<TERMCAP> variable. If
35found, and the value does not begin with a slash, and the terminal
36type name is the same as the environment string I<TERM>, the
37I<TERMCAP> string is used instead of reading a termcap file. If
38it does begin with a slash, the string is used as a path name of
39the termcap file to search. If I<TERMCAP> does not begin with a
40slash and name is different from I<TERM>, B<Tgetent> searches the
41files F<$HOME/.termcap>, F</etc/termcap>, and F</usr/share/misc/termcap>,
42in that order, unless the environment variable I<TERMPATH> exists,
43in which case it specifies a list of file pathnames (separated by
44spaces or colons) to be searched B<instead>. Whenever multiple
45files are searched and a tc field occurs in the requested entry,
46the entry it names must be found in the same file or one of the
47succeeding files. If there is a C<:tc=...:> in the I<TERMCAP>
48environment variable string it will continue the search in the
49files as above.
50
51I<OSPEED> is the terminal output bit rate (often mistakenly called
52the baud rate). I<OSPEED> can be specified as either a POSIX
53termios/SYSV termio speeds (where 9600 equals 9600) or an old
54BSD-style speeds (where 13 equals 9600).
55
56B<Tgetent> returns a blessed object reference which the user can
57then use to send the control strings to the terminal using B<Tputs>
58and B<Tgoto>. It calls C<croak> on failure.
59
60B<Tgoto> decodes a cursor addressing string with the given parameters.
61
62The output strings for B<Tputs> are cached for counts of 1 for performance.
63B<Tgoto> and B<Tpad> do not cache. C<$self-E<gt>{_xx}> is the raw termcap
64data and C<$self-E<gt>{xx}> is the cached version.
65
66 print $terminal->Tpad($self->{_xx}, 1);
67
68B<Tgoto>, B<Tputs>, and B<Tpad> return the string and will also
69output the string to $FH if specified.
70
71The extracted termcap entry is available in the object
72as C<$self-E<gt>{TERMCAP}>.
73
74=head1 EXAMPLES
75
76 # Get terminal output speed
77 require POSIX;
78 my $termios = new POSIX::Termios;
79 $termios->getattr;
80 my $ospeed = $termios->getospeed;
81
82 # Old-style ioctl code to get ospeed:
83 # require 'ioctl.pl';
84 # ioctl(TTY,$TIOCGETP,$sgtty);
85 # ($ispeed,$ospeed) = unpack('cc',$sgtty);
86
87 # allocate and initialize a terminal structure
88 $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
89
90 # require certain capabilities to be available
91 $terminal->Trequire(qw/ce ku kd/);
92
93 # Output Routines, if $FH is undefined these just return the string
94
95 # Tgoto does the % expansion stuff with the given args
96 $terminal->Tgoto('cm', $col, $row, $FH);
97
98 # Tputs doesn't do any % expansion.
99 $terminal->Tputs('dl', $count = 1, $FH);
100
101=cut
102
103# Returns a list of termcap files to check.
104sub termcap_path { ## private
105 my @termcap_path;
106 # $TERMCAP, if it's a filespec
c07a80fd 107 push(@termcap_path, $ENV{TERMCAP}) if ((exists $ENV{TERMCAP}) &&
108 ($ENV{TERMCAP} =~ /^\//));
109 if ((exists $ENV{TERMPATH}) && ($ENV{TERMPATH})) {
cb1a09d0 110 # Add the users $TERMPATH
111 push(@termcap_path, split(/(:|\s+)/, $ENV{TERMPATH}))
112 }
113 else {
114 # Defaults
115 push(@termcap_path,
116 $ENV{'HOME'} . '/.termcap',
117 '/etc/termcap',
118 '/usr/share/misc/termcap',
119 );
a687059c 120 }
cb1a09d0 121 # return the list of those termcaps that exist
748a9306 122 grep(-f, @termcap_path);
123}
124
cb1a09d0 125sub Tgetent { ## public -- static method
126 my $class = shift;
127 my $self = bless shift, $class;
128 my($term,$cap,$search,$field,$max,$tmp_term,$TERMCAP);
129 local($termpat,$state,$first,$entry); # used inside eval
130 local $_;
131
132 # Compute PADDING factor from OSPEED (to be used by Tpad)
133 if (! $self->{OSPEED}) {
134 carp "OSPEED was not set, defaulting to 9600";
135 $self->{OSPEED} = 9600;
136 }
137 if ($self->{OSPEED} < 16) {
138 # delays for old style speeds
139 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);
140 $self->{PADDING} = $pad[$self->{OSPEED}];
141 }
142 else {
143 $self->{PADDING} = 10000 / $self->{OSPEED};
144 }
145
146 $self->{TERM} = ($self->{TERM} || $ENV{TERM} || croak "TERM not set");
147 $term = $self->{TERM}; # $term is the term type we are looking for
148
149 # $tmp_term is always the next term (possibly :tc=...:) we are looking for
150 $tmp_term = $self->{TERM};
151 # protect any pattern metacharacters in $tmp_term
152 $termpat = $tmp_term; $termpat =~ s/(\W)/\\$1/g;
153
c07a80fd 154 my $foo = (exists $ENV{TERMCAP} ? $ENV{TERMCAP} : '');
cb1a09d0 155
156 # $entry is the extracted termcap entry
157 if (($foo !~ m:^/:) && ($foo =~ m/(^|\|)${termpat}[:|]/)) {
158 $entry = $foo;
159 }
160
161 my @termcap_path = termcap_path;
162 croak "Can't find a valid termcap file" unless @termcap_path || $entry;
163
164 $state = 1; # 0 == finished
165 # 1 == next file
166 # 2 == search again
167
168 $first = 0; # first entry (keeps term name)
169
170 $max = 32; # max :tc=...:'s
171
172 if ($entry) {
173 # ok, we're starting with $TERMCAP
174 $first++; # we're the first entry
175 # do we need to continue?
176 if ($entry =~ s/:tc=([^:]+):/:/) {
177 $tmp_term = $1;
178 # protect any pattern metacharacters in $tmp_term
179 $termpat = $tmp_term; $termpat =~ s/(\W)/\\$1/g;
180 }
181 else {
182 $state = 0; # we're already finished
183 }
184 }
185
186 # This is eval'ed inside the while loop for each file
187 $search = q{
54310121 188 while (<TERMCAP>) {
cb1a09d0 189 next if /^\\t/ || /^#/;
190 if ($_ =~ m/(^|\\|)${termpat}[:|]/o) {
191 chomp;
192 s/^[^:]*:// if $first++;
193 $state = 0;
54310121 194 while ($_ =~ s/\\\\$//) {
195 defined(my $x = <TERMCAP>) or last;
196 $_ .= $x; chomp;
197 }
cb1a09d0 198 last;
748a9306 199 }
cb1a09d0 200 }
55497cff 201 defined $entry or $entry = '';
202 $entry .= $_;
cb1a09d0 203 };
748a9306 204
cb1a09d0 205 while ($state != 0) {
206 if ($state == 1) {
207 # get the next TERMCAP
208 $TERMCAP = shift @termcap_path
209 || croak "failed termcap lookup on $tmp_term";
210 }
211 else {
212 # do the same file again
213 # prevent endless recursion
214 $max-- || croak "failed termcap loop at $tmp_term";
215 $state = 1; # ok, maybe do a new file next time
216 }
217
218 open(TERMCAP,"< $TERMCAP\0") || croak "open $TERMCAP: $!";
219 eval $search;
220 die $@ if $@;
221 close TERMCAP;
222
223 # If :tc=...: found then search this file again
224 $entry =~ s/:tc=([^:]+):/:/ && ($tmp_term = $1, $state = 2);
225 # protect any pattern metacharacters in $tmp_term
226 $termpat = $tmp_term; $termpat =~ s/(\W)/\\$1/g;
a687059c 227 }
cb1a09d0 228
229 croak "Can't find $term" if $entry eq '';
230 $entry =~ s/:+\s*:+/:/g; # cleanup $entry
231 $entry =~ s/:+/:/g; # cleanup $entry
232 $self->{TERMCAP} = $entry; # save it
233 # print STDERR "DEBUG: $entry = ", $entry, "\n";
a687059c 234
748a9306 235 # Precompile $entry into the object
cb1a09d0 236 $entry =~ s/^[^:]*://;
748a9306 237 foreach $field (split(/:[\s:\\]*/,$entry)) {
cb1a09d0 238 if ($field =~ /^(\w\w)$/) {
239 $self->{'_' . $field} = 1 unless defined $self->{'_' . $1};
240 # print STDERR "DEBUG: flag $1\n";
748a9306 241 }
242 elsif ($field =~ /^(\w\w)\@/) {
cb1a09d0 243 $self->{'_' . $1} = "";
244 # print STDERR "DEBUG: unset $1\n";
a687059c 245 }
246 elsif ($field =~ /^(\w\w)#(.*)/) {
cb1a09d0 247 $self->{'_' . $1} = $2 unless defined $self->{'_' . $1};
248 # print STDERR "DEBUG: numeric $1 = $2\n";
a687059c 249 }
250 elsif ($field =~ /^(\w\w)=(.*)/) {
cb1a09d0 251 # print STDERR "DEBUG: string $1 = $2\n";
252 next if defined $self->{'_' . ($cap = $1)};
a687059c 253 $_ = $2;
254 s/\\E/\033/g;
ecfc5424 255 s/\\(\d\d\d)/pack('c',oct($1) & 0177)/eg;
a687059c 256 s/\\n/\n/g;
257 s/\\r/\r/g;
258 s/\\t/\t/g;
259 s/\\b/\b/g;
260 s/\\f/\f/g;
261 s/\\\^/\377/g;
262 s/\^\?/\177/g;
63f2c1e1 263 s/\^(.)/pack('c',ord($1) & 31)/eg;
a687059c 264 s/\\(.)/$1/g;
265 s/\377/^/g;
cb1a09d0 266 $self->{'_' . $cap} = $_;
a687059c 267 }
cb1a09d0 268 # else { carp "junk in $term ignored: $field"; }
a687059c 269 }
cb1a09d0 270 $self->{'_pc'} = "\0" unless defined $self->{'_pc'};
271 $self->{'_bc'} = "\b" unless defined $self->{'_bc'};
272 $self;
a687059c 273}
274
cb1a09d0 275# $terminal->Tpad($string, $cnt, $FH);
276sub Tpad { ## public
277 my $self = shift;
278 my($string, $cnt, $FH) = @_;
279 my($decr, $ms);
a687059c 280
a687059c 281 if ($string =~ /(^[\d.]+)(\*?)(.*)$/) {
282 $ms = $1;
748a9306 283 $ms *= $cnt if $2;
a687059c 284 $string = $3;
cb1a09d0 285 $decr = $self->{PADDING};
a687059c 286 if ($decr > .1) {
287 $ms += $decr / 2;
cb1a09d0 288 $string .= $self->{'_pc'} x ($ms / $decr);
a687059c 289 }
290 }
291 print $FH $string if $FH;
292 $string;
293}
294
cb1a09d0 295# $terminal->Tputs($cap, $cnt, $FH);
296sub Tputs { ## public
297 my $self = shift;
298 my($cap, $cnt, $FH) = @_;
299 my $string;
748a9306 300
301 if ($cnt > 1) {
cb1a09d0 302 $string = Tpad($self, $self->{'_' . $cap}, $cnt);
748a9306 303 } else {
cb1a09d0 304 # cache result because Tpad can be slow
305 $string = defined $self->{$cap} ? $self->{$cap} :
306 ($self->{$cap} = Tpad($self, $self->{'_' . $cap}, 1));
748a9306 307 }
308 print $FH $string if $FH;
309 $string;
310}
311
312# %% output `%'
313# %d output value as in printf %d
314# %2 output value as in printf %2d
315# %3 output value as in printf %3d
316# %. output value as in printf %c
317# %+x add x to value, then do %.
318#
319# %>xy if value > x then add y, no output
320# %r reverse order of two parameters, no output
321# %i increment by one, no output
322# %B BCD (16*(value/10)) + (value%10), no output
323#
324# %n exclusive-or all parameters with 0140 (Datamedia 2500)
325# %D Reverse coding (value - 2*(value%16)), no output (Delta Data)
326#
cb1a09d0 327# $terminal->Tgoto($cap, $col, $row, $FH);
328sub Tgoto { ## public
329 my $self = shift;
330 my($cap, $code, $tmp, $FH) = @_;
331 my $string = $self->{'_' . $cap};
332 my $result = '';
333 my $after = '';
334 my $online = 0;
335 my @tmp = ($tmp,$code);
336 my $cnt = $code;
748a9306 337
a687059c 338 while ($string =~ /^([^%]*)%(.)(.*)/) {
339 $result .= $1;
340 $code = $2;
341 $string = $3;
342 if ($code eq 'd') {
9f68db38 343 $result .= sprintf("%d",shift(@tmp));
a687059c 344 }
345 elsif ($code eq '.') {
9f68db38 346 $tmp = shift(@tmp);
a687059c 347 if ($tmp == 0 || $tmp == 4 || $tmp == 10) {
348 if ($online) {
cb1a09d0 349 ++$tmp, $after .= $self->{'_up'} if $self->{'_up'};
a687059c 350 }
351 else {
cb1a09d0 352 ++$tmp, $after .= $self->{'_bc'};
a687059c 353 }
354 }
355 $result .= sprintf("%c",$tmp);
356 $online = !$online;
357 }
358 elsif ($code eq '+') {
9f68db38 359 $result .= sprintf("%c",shift(@tmp)+ord($string));
a687059c 360 $string = substr($string,1,99);
361 $online = !$online;
362 }
363 elsif ($code eq 'r') {
9f68db38 364 ($code,$tmp) = @tmp;
365 @tmp = ($tmp,$code);
a687059c 366 $online = !$online;
367 }
368 elsif ($code eq '>') {
369 ($code,$tmp,$string) = unpack("CCa99",$string);
9f68db38 370 if ($tmp[$[] > $code) {
371 $tmp[$[] += $tmp;
a687059c 372 }
373 }
374 elsif ($code eq '2') {
9f68db38 375 $result .= sprintf("%02d",shift(@tmp));
a687059c 376 $online = !$online;
377 }
378 elsif ($code eq '3') {
9f68db38 379 $result .= sprintf("%03d",shift(@tmp));
a687059c 380 $online = !$online;
381 }
382 elsif ($code eq 'i') {
9f68db38 383 ($code,$tmp) = @tmp;
384 @tmp = ($code+1,$tmp+1);
a687059c 385 }
386 else {
387 return "OOPS";
388 }
389 }
cb1a09d0 390 $string = Tpad($self, $result . $string . $after, $cnt);
748a9306 391 print $FH $string if $FH;
392 $string;
393}
394
cb1a09d0 395# $terminal->Trequire(qw/ce ku kd/);
396sub Trequire { ## public
397 my $self = shift;
398 my($cap,@undefined);
399 foreach $cap (@_) {
400 push(@undefined, $cap)
401 unless defined $self->{'_' . $cap} && $self->{'_' . $cap};
748a9306 402 }
cb1a09d0 403 croak "Terminal does not support: (@undefined)" if @undefined;
a687059c 404}
405
4061;
cb1a09d0 407