home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / SLAKWARE / D12 / PERL1.TGZ / perl1.tar / usr / lib / perl5 / chat2.inter < prev    next >
Text File  |  1996-06-28  |  13KB  |  496 lines

  1. Article 20992 of comp.lang.perl:
  2. Path: netlabs!news.cerf.net!mvb.saic.com!MathWorks.Com!europa.eng.gtefsd.com!howland.reston.ans.net!cs.utexas.edu!swrinde!ihnp4.ucsd.edu!ames!koriel!male.EBay.Sun.COM!jethro.Corp.Sun.COM!eric
  3. From: eric.arnold@sun.com (Eric Arnold)
  4. Newsgroups: comp.lang.perl
  5. Subject: Re: Need a bidirectional filter for interactive Unix applications
  6. Date: 15 Apr 94 21:24:03 GMT
  7. Organization: Sun Microsystems
  8. Lines: 478
  9. Sender: news@sun.com
  10. Message-ID: <ERIC.94Apr15212403@sun.com>
  11. References: <dgfCo9F2J.Jzw@netcom.com> <1994Apr15.110134.4581@chemabs.uucp>
  12. NNTP-Posting-Host: animus.corp.sun.com
  13. X-Newsreader: prn Ver 1.09
  14. In-reply-to: btf64@cas.org's message of Fri, 15 Apr 1994 11:01:34 GMT
  15.  
  16. In article <1994Apr15.110134.4581@chemabs.uucp>
  17.     btf64@cas.org (Bernard T. French) writes:
  18.  
  19. >In article <dgfCo9F2J.Jzw@netcom.com> dgf@netcom.com (David Feldman) writes:
  20. >>I need to write a bidirectional filter that would (ideally) sit between a
  21. ..
  22. >>program's stdin & stdout to point to a pty pair known to perl. The perl app-
  23. >>lication would talk to the user's crt/keyboard, translate (application-specific)
  24. >>the input & output streams, and pass these as appropriate to/from the pty pair,
  25. ..
  26. >
  27. >     I'm afraid I can't offer you a perl solution, but err..... there is a
  28. >Tcl solution.  There is a Tcl extension called "expect" that is designed to
  29.  
  30. There *is* an old, established Perl solution:  "chat2.pl" which does
  31. everything (well, basically) "expect" does but you get it in the
  32. expressive Perl environment.  "chat2.pl" is delivered with the Perl
  33. source.
  34.  
  35. Randal:  "interact()" still hasn't made it into Perl5alpha8
  36. "chat2.pl", so I've included a version which does.
  37.  
  38. -Eric
  39.  
  40.  
  41. ## chat.pl: chat with a server
  42. ## V2.01.alpha.7 91/06/16
  43. ## Randal L. Schwartz
  44.  
  45. package chat;
  46.  
  47. $sockaddr = 'S n a4 x8';
  48. chop($thishost = `hostname`); $thisaddr = (gethostbyname($thishost))[4];
  49. $thisproc = pack($sockaddr, 2, 0, $thisaddr);
  50.  
  51. # *S = symbol for current I/O, gets assigned *chatsymbol....
  52. $next = "chatsymbol000000"; # next one
  53. $nextpat = "^chatsymbol"; # patterns that match next++, ++, ++, ++
  54.  
  55.  
  56. ## $handle = &chat'open_port("server.address",$port_number);
  57. ## opens a named or numbered TCP server
  58.  
  59. sub open_port { ## public
  60.     local($server, $port) = @_;
  61.  
  62.     local($serveraddr,$serverproc);
  63.  
  64.     *S = ++$next;
  65.     if ($server =~ /^(\d+)+\.(\d+)\.(\d+)\.(\d+)$/) {
  66.         $serveraddr = pack('C4', $1, $2, $3, $4);
  67.     } else {
  68.         local(@x) = gethostbyname($server);
  69.         return undef unless @x;
  70.         $serveraddr = $x[4];
  71.     }
  72.     $serverproc = pack($sockaddr, 2, $port, $serveraddr);
  73.     unless (socket(S, 2, 1, 6)) {
  74.         # XXX hardwired $AF_SOCKET, $SOCK_STREAM, 'tcp'
  75.         # but who the heck would change these anyway? (:-)
  76.         ($!) = ($!, close(S)); # close S while saving $!
  77.         return undef;
  78.     }
  79.     unless (bind(S, $thisproc)) {
  80.         ($!) = ($!, close(S)); # close S while saving $!
  81.         return undef;
  82.     }
  83.     unless (connect(S, $serverproc)) {
  84.         ($!) = ($!, close(S)); # close S while saving $!
  85.         return undef;
  86.     }
  87.     select((select(S), $| = 1)[0]);
  88.     $next; # return symbol for switcharound
  89. }
  90.  
  91. ## ($host, $port, $handle) = &chat'open_listen([$port_number]);
  92. ## opens a TCP port on the current machine, ready to be listened to
  93. ## if $port_number is absent or zero, pick a default port number
  94. ## process must be uid 0 to listen to a low port number
  95.  
  96. sub open_listen { ## public
  97.  
  98.     *S = ++$next;
  99.     local($thisport) = shift || 0;
  100.     local($thisproc_local) = pack($sockaddr, 2, $thisport, $thisaddr);
  101.     local(*NS) = "__" . time;
  102.     unless (socket(NS, 2, 1, 6)) {
  103.         # XXX hardwired $AF_SOCKET, $SOCK_STREAM, 'tcp'
  104.         # but who the heck would change these anyway? (:-)
  105.         ($!) = ($!, close(NS));
  106.         return undef;
  107.     }
  108.     unless (bind(NS, $thisproc_local)) {
  109.         ($!) = ($!, close(NS));
  110.         return undef;
  111.     }
  112.     unless (listen(NS, 1)) {
  113.         ($!) = ($!, close(NS));
  114.         return undef;
  115.     }
  116.     select((select(NS), $| = 1)[0]);
  117.     local($family, $port, @myaddr) =
  118.         unpack("S n C C C C x8", getsockname(NS));
  119.     $S{"needs_accept"} = *NS; # so expect will open it
  120.     (@myaddr, $port, $next); # returning this
  121. }
  122.  
  123. ## $handle = &chat'open_proc("command","arg1","arg2",...);
  124. ## opens a /bin/sh on a pseudo-tty
  125.  
  126. sub open_proc { ## public
  127.     local(@cmd) = @_;
  128.  
  129.     *S = ++$next;
  130.     local(*TTY) = "__TTY" . time;
  131.     local($pty,$tty,$pty_handle) = &_getpty(S,TTY);
  132.  
  133.     #local($pty,$tty,$pty_handle) = &getpty(S,TTY);
  134.     #$Tty = $tty;
  135.  
  136.     die "Cannot find a new pty" unless defined $pty;
  137.     local($pid) = fork;
  138.     die "Cannot fork: $!" unless defined $pid;
  139.     unless ($pid) {
  140.         close STDIN; close STDOUT; close STDERR;
  141.         #close($pty_handle);
  142.         setpgrp(0,$$);
  143.         if (open(DEVTTY, "/dev/tty")) {
  144.             ioctl(DEVTTY,0x20007471,0);        # XXX s/b &TIOCNOTTY
  145.             close DEVTTY;
  146.         }
  147.         open(STDIN,"<&TTY");
  148.         open(STDOUT,">&TTY");
  149.         open(STDERR,">&STDOUT");
  150.         die "Oops" unless fileno(STDERR) == 2;    # sanity
  151.         close(S);
  152.  
  153.         exec @cmd;
  154.         die "Cannot exec @cmd: $!";
  155.     }
  156.     close(TTY);
  157.     $PID{$next} = $pid;
  158.     $next; # return symbol for switcharound
  159.  
  160. }
  161.  
  162. # $S is the read-ahead buffer
  163.  
  164. ## $return = &chat'expect([$handle,] $timeout_time,
  165. ##     $pat1, $body1, $pat2, $body2, ... )
  166. ## $handle is from previous &chat'open_*().
  167. ## $timeout_time is the time (either relative to the current time, or
  168. ## absolute, ala time(2)) at which a timeout event occurs.
  169. ## $pat1, $pat2, and so on are regexs which are matched against the input
  170. ## stream.  If a match is found, the entire matched string is consumed,
  171. ## and the corresponding body eval string is evaled.
  172. ##
  173. ## Each pat is a regular-expression (probably enclosed in single-quotes
  174. ## in the invocation).  ^ and $ will work, respecting the current value of $*.
  175. ## If pat is 'TIMEOUT', the body is executed if the timeout is exceeded.
  176. ## If pat is 'EOF', the body is executed if the process exits before
  177. ## the other patterns are seen.
  178. ##
  179. ## Pats are scanned in the order given, so later pats can contain
  180. ## general defaults that won't be examined unless the earlier pats
  181. ## have failed.
  182. ##
  183. ## The result of eval'ing body is returned as the result of
  184. ## the invocation.  Recursive invocations are not thought
  185. ## through, and may work only accidentally. :-)
  186. ##
  187. ## undef is returned if either a timeout or an eof occurs and no
  188. ## corresponding body has been defined.
  189. ## I/O errors of any sort are treated as eof.
  190.  
  191. $nextsubname = "expectloop000000"; # used for subroutines
  192.  
  193. sub expect { ## public
  194.     if ($_[0] =~ /$nextpat/) {
  195.         *S = shift;
  196.     }
  197.     local($endtime) = shift;
  198.  
  199.     local($timeout,$eof) = (1,1);
  200.     local($caller) = caller;
  201.     local($rmask, $nfound, $timeleft, $thisbuf);
  202.     local($cases, $pattern, $action, $subname);
  203.     $endtime += time if $endtime < 600_000_000;
  204.  
  205.     if (defined $S{"needs_accept"}) { # is it a listen socket?
  206.         local(*NS) = $S{"needs_accept"};
  207.         delete $S{"needs_accept"};
  208.         $S{"needs_close"} = *NS;
  209.         unless(accept(S,NS)) {
  210.             ($!) = ($!, close(S), close(NS));
  211.             return undef;
  212.         }
  213.         select((select(S), $| = 1)[0]);
  214.     }
  215.  
  216.     # now see whether we need to create a new sub:
  217.  
  218.     unless ($subname = $expect_subname{$caller,@_}) {
  219.         # nope.  make a new one:
  220.         $expect_subname{$caller,@_} = $subname = $nextsubname++;
  221.  
  222.         $cases .= <<"EDQ"; # header is funny to make everything elsif's
  223. sub $subname {
  224.     LOOP: {
  225.         if (0) { ; }
  226. EDQ
  227.         while (@_) {
  228.             ($pattern,$action) = splice(@_,0,2);
  229.             if ($pattern =~ /^eof$/i) {
  230.                 $cases .= <<"EDQ";
  231.         elsif (\$eof) {
  232.              package $caller;
  233.             $action;
  234.         }
  235. EDQ
  236.                 $eof = 0;
  237.             } elsif ($pattern =~ /^timeout$/i) {
  238.             $cases .= <<"EDQ";
  239.         elsif (\$timeout) {
  240.              package $caller;
  241.             $action;
  242.         }
  243. EDQ
  244.                 $timeout = 0;
  245.             } else {
  246.                 $pattern =~ s#/#\\/#g;
  247.             $cases .= <<"EDQ";
  248.         elsif (\$S =~ /$pattern/) {
  249.             \$S = \$';
  250.              package $caller;
  251.             $action;
  252.         }
  253. EDQ
  254.             }
  255.         }
  256.         $cases .= <<"EDQ" if $eof;
  257.         elsif (\$eof) {
  258.             undef;
  259.         }
  260. EDQ
  261.         $cases .= <<"EDQ" if $timeout;
  262.         elsif (\$timeout) {
  263.             undef;
  264.         }
  265. EDQ
  266.         $cases .= <<'ESQ';
  267.         else {
  268.             $rmask = "";
  269.             vec($rmask,fileno(S),1) = 1;
  270.             ($nfound, $rmask) =
  271.                  select($rmask, undef, undef, $endtime - time);
  272.             if ($nfound) {
  273.                 $nread = sysread(S, $thisbuf, 1024);
  274.                 if ($nread > 0) {
  275.                     $S .= $thisbuf;
  276.                 } else {
  277.                     $eof++, redo LOOP; # any error is also eof
  278.                 }
  279.             } else {
  280.                 $timeout++, redo LOOP; # timeout
  281.             }
  282.             redo LOOP;
  283.         }
  284.     }
  285. }
  286. ESQ
  287.         eval $cases; die "$cases:\n$@" if $@;
  288.     }
  289.