Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.7 KiB

  1. ;# $RCSfile: termcap.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:16 $
  2. ;#
  3. ;# Usage:
  4. ;# require 'ioctl.pl';
  5. ;# ioctl(TTY,$TIOCGETP,$foo);
  6. ;# ($ispeed,$ospeed) = unpack('cc',$foo);
  7. ;# require 'termcap.pl';
  8. ;# &Tgetent('vt100'); # sets $TC{'cm'}, etc.
  9. ;# &Tputs(&Tgoto($TC{'cm'},$col,$row), 0, 'FILEHANDLE');
  10. ;# &Tputs($TC{'dl'},$affcnt,'FILEHANDLE');
  11. ;#
  12. sub Tgetent {
  13. local($TERM) = @_;
  14. local($TERMCAP,$_,$entry,$loop,$field);
  15. warn "Tgetent: no ospeed set" unless $ospeed;
  16. foreach $key (keys %TC) {
  17. delete $TC{$key};
  18. }
  19. $TERM = $ENV{'TERM'} unless $TERM;
  20. $TERM =~ s/(\W)/\\$1/g;
  21. $TERMCAP = $ENV{'TERMCAP'};
  22. $TERMCAP = '/etc/termcap' unless $TERMCAP;
  23. if ($TERMCAP !~ m:^/:) {
  24. if ($TERMCAP !~ /(^|\|)$TERM[:\|]/) {
  25. $TERMCAP = '/etc/termcap';
  26. }
  27. }
  28. if ($TERMCAP =~ m:^/:) {
  29. $entry = '';
  30. do {
  31. $loop = "
  32. open(TERMCAP,'<$TERMCAP') || die \"Can't open $TERMCAP\";
  33. while (<TERMCAP>) {
  34. next if /^#/;
  35. next if /^\t/;
  36. if (/(^|\\|)${TERM}[:\\|]/) {
  37. chop;
  38. while (chop eq '\\\\') {
  39. \$_ .= <TERMCAP>;
  40. chop;
  41. }
  42. \$_ .= ':';
  43. last;
  44. }
  45. }
  46. close TERMCAP;
  47. \$entry .= \$_;
  48. ";
  49. eval $loop;
  50. } while s/:tc=([^:]+):/:/ && ($TERM = $1);
  51. $TERMCAP = $entry;
  52. }
  53. foreach $field (split(/:[\s:\\]*/,$TERMCAP)) {
  54. if ($field =~ /^\w\w$/) {
  55. $TC{$field} = 1;
  56. }
  57. elsif ($field =~ /^(\w\w)#(.*)/) {
  58. $TC{$1} = $2 if $TC{$1} eq '';
  59. }
  60. elsif ($field =~ /^(\w\w)=(.*)/) {
  61. $entry = $1;
  62. $_ = $2;
  63. s/\\E/\033/g;
  64. s/\\(200)/pack('c',0)/eg; # NUL character
  65. s/\\(0\d\d)/pack('c',oct($1))/eg; # octal
  66. s/\\(0x[0-9A-Fa-f][0-9A-Fa-f])/pack('c',hex($1))/eg; # hex
  67. s/\\(\d\d\d)/pack('c',$1 & 0177)/eg;
  68. s/\\n/\n/g;
  69. s/\\r/\r/g;
  70. s/\\t/\t/g;
  71. s/\\b/\b/g;
  72. s/\\f/\f/g;
  73. s/\\\^/\377/g;
  74. s/\^\?/\177/g;
  75. s/\^(.)/pack('c',ord($1) & 31)/eg;
  76. s/\\(.)/$1/g;
  77. s/\377/^/g;
  78. $TC{$entry} = $_ if $TC{$entry} eq '';
  79. }
  80. }
  81. $TC{'pc'} = "\0" if $TC{'pc'} eq '';
  82. $TC{'bc'} = "\b" if $TC{'bc'} eq '';
  83. }
  84. @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);
  85. sub Tputs {
  86. local($string,$affcnt,$FH) = @_;
  87. local($ms);
  88. if ($string =~ /(^[\d.]+)(\*?)(.*)$/) {
  89. $ms = $1;
  90. $ms *= $affcnt if $2;
  91. $string = $3;
  92. $decr = $Tputs[$ospeed];
  93. if ($decr > .1) {
  94. $ms += $decr / 2;
  95. $string .= $TC{'pc'} x ($ms / $decr);
  96. }
  97. }
  98. print $FH $string if $FH;
  99. $string;
  100. }
  101. sub Tgoto {
  102. local($string) = shift(@_);
  103. local($result) = '';
  104. local($after) = '';
  105. local($code,$tmp) = @_;
  106. local(@tmp);
  107. @tmp = ($tmp,$code);
  108. local($online) = 0;
  109. while ($string =~ /^([^%]*)%(.)(.*)/) {
  110. $result .= $1;
  111. $code = $2;
  112. $string = $3;
  113. if ($code eq 'd') {
  114. $result .= sprintf("%d",shift(@tmp));
  115. }
  116. elsif ($code eq '.') {
  117. $tmp = shift(@tmp);
  118. if ($tmp == 0 || $tmp == 4 || $tmp == 10) {
  119. if ($online) {
  120. ++$tmp, $after .= $TC{'up'} if $TC{'up'};
  121. }
  122. else {
  123. ++$tmp, $after .= $TC{'bc'};
  124. }
  125. }
  126. $result .= sprintf("%c",$tmp);
  127. $online = !$online;
  128. }
  129. elsif ($code eq '+') {
  130. $result .= sprintf("%c",shift(@tmp)+ord($string));
  131. $string = substr($string,1,99);
  132. $online = !$online;
  133. }
  134. elsif ($code eq 'r') {
  135. ($code,$tmp) = @tmp;
  136. @tmp = ($tmp,$code);
  137. $online = !$online;
  138. }
  139. elsif ($code eq '>') {
  140. ($code,$tmp,$string) = unpack("CCa99",$string);
  141. if ($tmp[$[] > $code) {
  142. $tmp[$[] += $tmp;
  143. }
  144. }
  145. elsif ($code eq '2') {
  146. $result .= sprintf("%02d",shift(@tmp));
  147. $online = !$online;
  148. }
  149. elsif ($code eq '3') {
  150. $result .= sprintf("%03d",shift(@tmp));
  151. $online = !$online;
  152. }
  153. elsif ($code eq 'i') {
  154. ($code,$tmp) = @tmp;
  155. @tmp = ($code+1,$tmp+1);
  156. }
  157. else {
  158. return "OOPS";
  159. }
  160. }
  161. $result . $string . $after;
  162. }
  163. 1;