home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-osu / termcap.pl < prev    next >
Encoding:
Internet Message Format  |  1990-02-28  |  4.8 KB

  1. Path: tut.cis.ohio-state.edu!pt.cs.cmu.edu!dsl.pitt.edu!pitt!jupiter!al
  2. From: al@ee.pitt.edu (Alan Martello)
  3. Newsgroups: comp.lang.perl
  4. Subject: New version of termcap.pl
  5. Message-ID: <6933@pitt.UUCP>
  6. Date: 24 Feb 90 04:30:36 GMT
  7. Sender: news@pitt.UUCP
  8. Reply-To: al@ee.pitt.edu (Alan Martello)
  9. Distribution: usa
  10. Lines: 187
  11.  
  12. Here is a new version of the library "termcap.pl" which correctly (?)
  13. works on SunOS 4.0.1 under X11R4.  If it fixes anyone else's problems,
  14. great, if not, oh well.  If this has been superseded by something
  15. newer in patches 7 or 8 or another posting, my apologies (after all
  16. what do you want for free?).  I'm posting the entire thing since
  17. the context diffs are only 300 bytes shorter.
  18.  
  19. *******************************************************************
  20.        Alan R. Martello        Electrical Engineering Dept.
  21.         al@ee.pitt.edu           University of Pittsburgh
  22. *******************************************************************
  23. -------------------   CUT HERE ------------------------
  24. ;# $Header: termcap.pl,v 3.0 89/10/18 15:19:58 lwall Locked $
  25. ;#
  26. ;# Usage:
  27. ;#    do 'ioctl.pl';
  28. ;#    ioctl(TTY,$TIOCGETP,$foo);
  29. ;#    ($ispeed,$ospeed) = unpack('cc',$foo);
  30. ;#    do 'termcap.pl';
  31. ;#    do Tgetent('vt100');    # sets $TC{'cm'}, etc.
  32. ;#    do Tgoto($TC{'cm'},$col,$row);
  33. ;#    do Tputs($TC{'dl'},$affcnt,'FILEHANDLE');
  34. ;#
  35. ;#  modified to work "correctly" (?) under X11R4 , SunOS 4.0  2/22/90
  36. sub Tgetent {
  37.     local($TERM) = @_;
  38.     local($TERMCAP,$_,$entry,$loop,$field);
  39.  
  40.     warn "Tgetent: no ospeed set" unless $ospeed;
  41.     foreach $key (keys(TC)) {
  42.     delete $TC{$key};
  43.     }
  44.     $TERM = $ENV{'TERM'} unless $TERM;
  45.     $TERMCAP = $ENV{'TERMCAP'};
  46.     $TERMCAP = '/etc/termcap' unless $TERMCAP;
  47.     if ($TERMCAP !~ m:^/:) {
  48.     if (index($TERMCAP,"|$TERM|") < $[) {
  49.         $TERMCAP = '/etc/termcap';
  50.     }
  51.     }
  52.     if ($TERMCAP =~ m:^/:) {
  53.     $entry = '';
  54.     do {
  55.         $loop = "
  56.         open(TERMCAP,'<$TERMCAP') || die \"Can't open $TERMCAP\";
  57.         while (<TERMCAP>) {
  58.         next if /^#/;
  59.         next if /^\t/;
  60.         if (/\\|$TERM[:\\|]/) {
  61.             chop;
  62.             while (chop eq '\\\\') {
  63.             \$_ .= <TERMCAP>;
  64.             chop;
  65.             }
  66.             \$_ .= ':';
  67.             last;
  68.         }
  69.         }
  70.         close TERMCAP;
  71.         \$entry .= \$_;
  72.         ";
  73.         eval $loop;
  74.     } while s/:tc=([^:]+):/:/, $TERM = $1;
  75.     $TERMCAP = $entry;
  76.     }
  77.  
  78.     foreach $field (split(/:[\s:\\]*/,$TERMCAP)) {
  79.     if ($field =~ /^\w\w$/) {
  80.         $TC{$field} = 1;
  81.     }
  82.     elsif ($field =~ /^(\w\w)#(.*)/) {
  83.         $TC{$1} = $2 if $TC{$1} eq '';
  84.     }
  85.     elsif ($field =~ /^(\w\w)=(.*)/) {
  86.         $entry = $1;
  87.         $_ = $2;
  88.         s/\\E/\033/g;
  89.         s/\\(\d\d\d)/pack('c',$1 & 0177)/eg;
  90.         s/\\n/\n/g;
  91.         s/\\r/\r/g;
  92.         s/\\t/\t/g;
  93.         s/\\b/\b/g;
  94.         s/\\f/\f/g;
  95.         s/\\\^/\377/g;
  96.         s/\^\?/\177/g;
  97.         s/\^(.)/pack('c',$1 & 031)/eg;
  98.         s/\\(.)/$1/g;
  99.         s/\377/^/g;
  100.         $TC{$entry} = $_ if $TC{$entry} eq '';
  101.     }
  102.     }
  103.     $TC{'pc'} = "\0" if $TC{'pc'} eq '';
  104.     $TC{'bc'} = "\b" if $TC{'bc'} eq '';
  105. }
  106.  
  107. @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);
  108.  
  109. sub Tputs {
  110.     local($string,$affcnt,$FH) = @_;
  111.     local($ms);
  112.     if ($string =~ /(^[\d.]+)(\*?)(.*)$/) {
  113.     $ms = $1;
  114.     $ms *= $affcnt if $2;
  115.     $string = $3;
  116.     $decr = $Tputs[$ospeed];
  117.     if ($decr > .1) {
  118.         $ms += $decr / 2;
  119.         $string .= $TC{'pc'} x ($ms / $decr);
  120.     }
  121.     }
  122.     print $FH $string if $FH;
  123.     $string;
  124. }
  125.  
  126. sub Tgoto {
  127.     local($string) = shift(@_);
  128.     local($result) = '';
  129.     local($after) = '';
  130.     local($code,$tmp) = @_;
  131.     local($online) = 0;
  132.     local($tmp_ary);
  133.  
  134.     @tmp_ary = ($tmp,$code);        # swap the order of the parameters
  135.  
  136.     # strip off any leading delay
  137.     if ($string =~ /^\d+(.*)/) {
  138.     $string = $1;
  139.     }
  140.  
  141.     while ($string =~ /^([^%]*)%(.)(.*)/) {
  142.     $result .= $1;
  143.     $code = $2;
  144.     $string = $3;
  145.  
  146.     if ($code eq 'd') {
  147.             $tmp = shift(@tmp_ary);
  148.         $result .= sprintf("%d",$tmp);
  149.     }
  150.     elsif ($code eq '.') {
  151.         $tmp = shift(@tmp_ary);
  152.         if ($tmp == 0 || $tmp == 4 || $tmp == 10) {
  153.         if ($online) {
  154.             ++$tmp, $after .= $TC{'up'} if $TC{'up'};
  155.         }
  156.         else {
  157.             ++$tmp, $after .= $TC{'bc'};
  158.         }
  159.         }
  160.         $result .= sprintf("%c",$tmp);
  161.         $online = !$online;
  162.     }
  163.     elsif ($code eq '+') {
  164.         $result .= sprintf("%c",shift(@tmp_ary)+ord($string));
  165.         $string = substr($string,1,99);
  166.         $online = !$online;
  167.     }
  168.     elsif ($code eq 'r') {
  169.         ($code,$tmp) = @tmp_ary;
  170.         @tmp_ary = ($tmp,$code);
  171.         $online = !$online;
  172.     }
  173.     elsif ($code eq '>') {
  174.         ($code,$tmp,$string) = unpack("CCa99",$string);
  175.         if ($_[$[] > $code) {
  176.         $_[$[] += $tmp;
  177.         }
  178.     }
  179.     elsif ($code eq '2') {
  180.         $result .= sprintf("%02d",shift(@tmp_ary));
  181.         $online = !$online;
  182.     }
  183.     elsif ($code eq '3') {
  184.         $result .= sprintf("%03d",shift(@tmp_ary));
  185.         $online = !$online;
  186.     }
  187.     elsif ($code eq 'i') {
  188.         ($code,$tmp) = @tmp_ary;
  189.         @tmp_ary = ($code+1,$tmp+1);
  190.     }
  191.     else {
  192.         return "OOPS";
  193.     }
  194.     }
  195.     $result . $string . $after;
  196. }
  197.  
  198. 1;
  199.  
  200.