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
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
16 In article <1994Apr15.110134.4581@chemabs.uucp>
17 btf64@cas.org (Bernard T. French) writes:
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
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,
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
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
35 Randal: "interact()" still hasn't made it into Perl5alpha8
36 "chat2.pl", so I've included a version which does.
41 ## chat.pl: chat with a server
42 ## V2.01.alpha.7 91/06/16
47 $sockaddr = 'S n a4 x8';
48 chop($thishost = `hostname`); $thisaddr = (gethostbyname($thishost))[4];
49 $thisproc = pack($sockaddr, 2, 0, $thisaddr);
51 # *S = symbol for current I/O, gets assigned *chatsymbol....
52 $next = "chatsymbol000000"; # next one
53 $nextpat = "^chatsymbol"; # patterns that match next++, ++, ++, ++
56 ## $handle = &chat'open_port("server.address",$port_number);
57 ## opens a named or numbered TCP server
59 sub open_port { ## public
60 local($server, $port) = @_;
62 local($serveraddr,$serverproc);
65 if ($server =~ /^(\d+)+\.(\d+)\.(\d+)\.(\d+)$/) {
66 $serveraddr = pack('C4', $1, $2, $3, $4);
68 local(@x) = gethostbyname($server);
69 return undef unless @x;
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 $!
79 unless (bind(S, $thisproc)) {
80 ($!) = ($!, close(S)); # close S while saving $!
83 unless (connect(S, $serverproc)) {
84 ($!) = ($!, close(S)); # close S while saving $!
87 select((select(S), $| = 1)[0]);
88 $next; # return symbol for switcharound
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
96 sub open_listen { ## public
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));
108 unless (bind(NS, $thisproc_local)) {
109 ($!) = ($!, close(NS));
112 unless (listen(NS, 1)) {
113 ($!) = ($!, close(NS));
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
123 ## $handle = &chat'open_proc("command","arg1","arg2",...);
124 ## opens a /bin/sh on a pseudo-tty
126 sub open_proc { ## public
130 local(*TTY) = "__TTY" . time;
131 local($pty,$tty,$pty_handle) = &_getpty(S,TTY);
133 #local($pty,$tty,$pty_handle) = &getpty(S,TTY);
136 die "Cannot find a new pty" unless defined $pty;
138 die "Cannot fork: $!" unless defined $pid;
140 close STDIN; close STDOUT; close STDERR;
143 if (open(DEVTTY, "/dev/tty")) {
144 ioctl(DEVTTY,0x20007471,0); # XXX s/b &TIOCNOTTY
148 open(STDOUT,">&TTY");
149 open(STDERR,">&STDOUT");
150 die "Oops" unless fileno(STDERR) == 2; # sanity
154 die "Cannot exec @cmd: $!";
158 $next; # return symbol for switcharound
162 # $S is the read-ahead buffer
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.
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.
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
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. :-)
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.
191 $nextsubname = "expectloop000000"; # used for subroutines
193 sub expect { ## public
194 if ($_[0] =~ /$nextpat/) {
197 local($endtime) = shift;
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;
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));
213 select((select(S), $| = 1)[0]);
216 # now see whether we need to create a new sub:
218 unless ($subname = $expect_subname{$caller,@_}) {
219 # nope. make a new one:
220 $expect_subname{$caller,@_} = $subname = $nextsubname++;
222 $cases .= <<"EDQ"; # header is funny to make everything elsif's
228 ($pattern,$action) = splice(@_,0,2);
229 if ($pattern =~ /^eof$/i) {
237 } elsif ($pattern =~ /^timeout$/i) {
246 $pattern =~ s#/#\\/#g;
248 elsif (\$S =~ /$pattern/) {
256 $cases .= <<"EDQ" if $eof;
261 $cases .= <<"EDQ" if $timeout;
269 vec($rmask,fileno(S),1) = 1;
271 select($rmask, undef, undef, $endtime - time);
273 $nread = sysread(S, $thisbuf, 1024);
277 $eof++, redo LOOP; # any error is also eof
280 $timeout++, redo LOOP; # timeout
287 eval $cases; die "$cases:\n$@" if $@;
293 ## &chat'print([$handle,] @data)
294 ## $handle is from previous &chat'open().
295 ## like print $handle @data
297 sub print { ## public
298 if ($_[0] =~ /$nextpat/) {
304 ## &chat'close([$handle,])
305 ## $handle is from previous &chat'open().
306 ## like close $handle
308 sub close { ## public
310 if ($_[0] =~ /$nextpat/) {
318 if (defined $S{"needs_close"}) { # is it a listen socket?
319 local(*NS) = $S{"needs_close"};
320 delete $S{"needs_close"};
325 ## @ready_handles = &chat'select($timeout, @handles)
326 ## select()'s the handles with a timeout value of $timeout seconds.
327 ## Returns an array of handles that are ready for I/O.
328 ## Both user handles and chat handles are supported (but beware of
329 ## stdio's buffering for user handles).
331 sub select { ## public
332 local($timeout) = shift;
333 local(@handles) = @_;
334 local(%handlename) = ();
336 local($caller) = caller;
339 if (/$nextpat/o) { # one of ours... see if ready
342 $timeout = 0; # we have a winner
345 $handlename{fileno($_)} = $_;
347 $handlename{fileno(/'/ ? $_ : "$caller\'$_")} = $_;
350 for (sort keys %handlename) {
351 vec($rmask, $_, 1) = 1;
353 select($rmask, undef, undef, $timeout);
354 for (sort keys %handlename) {
355 $ready{$handlename{$_}}++ if vec($rmask,$_,1);
360 # ($pty,$tty) = $chat'_getpty(PTY,TTY):
361 # internal procedure to get the next available pty.
362 # opens pty on handle PTY, and matching tty on handle TTY.
363 # returns undef if can't find a pty.
365 sub _getpty { ## private
366 local($_PTY,$_TTY) = @_;
367 $_PTY =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
368 $_TTY =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
370 for $bank (112..127) {
371 next unless -e sprintf("/dev/pty%c0", $bank);
373 $pty = sprintf("/dev/pty%c%c", $bank, $unit);
374 open($_PTY,"+>$pty") || next;
375 select((select($_PTY), $| = 1)[0]);
376 ($tty = $pty) =~ s/pty/tty/;
377 open($_TTY,"+>$tty") || next;
378 select((select($_TTY), $| = 1)[0]);
379 system "stty nl>$tty";
380 return ($pty,$tty,$_PTY);
388 local( $pty_handle, $tty_handle ) = @_;
390 print "--------in getpty----------\n";
391 $pty_handle =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
392 $pty_handle =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
394 #$pty_handle = ++$next_handle;
395 chop( @ptys = `ls /dev/pty*` );
399 open($pty_handle,"+>$pty") || next;
400 select((select($pty_handle), $| = 1)[0]);
401 ($tty = $pty) =~ s/pty/tty/;
403 open($tty_handle,"+>$tty") || next;
404 select((select($tty_handle), $| = 1)[0]);
405 ($tty = $pty) =~ s/pty/tty/;
407 return ($pty, $tty, $pty_handle );
414 # from: Randal L. Schwartz
418 # ($chathandle = &chat'open_proc("/bin/sh")) || die "cannot open shell";
419 # system("stty cbreak raw -echo >/dev/tty\n");
420 # &chat'interact($chathandle);
421 # &chat'close($chathandle);
422 # system("stty -cbreak -raw echo >/dev/tty\n");
426 local( $chathandle ) = @_;
428 &chat'print($chathandle, "stty sane\n");
429 select(STDOUT) ; $| = 1; # unbuffer STDOUT
431 #print "tty=$Tty,whoami=",`whoami`,"\n";
432 #&change_utmp( "", $Tty, "eric", "", time() );
435 @ready = &chat'select(30, STDIN,$chathandle);
436 print "after select, ready=",join(",",@ready),"\n";
437 #(warn "[waiting]"), redo unless @ready;
438 if (grep($_ eq $chathandle, @ready)) {
439 print "checking $chathandle\n";
440 last unless $text = &chat'expect($chathandle,0,'[\s\S]+','$&');
441 print "$chathandle OK\n";
445 if (grep($_ eq STDIN, @ready)) {
446 print "checking STDIN\n";
447 last unless sysread(STDIN,$buf,1024) > 0;
449 &chat'print($chathandle,$buf);
453 #&change_utmp( $Tty, "$Tty", "", "", 0 );
454 print "leaving interact, \$!=$!\n";
457 ## $handle = &chat'open_duphandle(handle);
458 ## duplicates an input file handle to conform to chat format
460 sub open_duphandle { ## public
463 $next; # return symbol for switcharound
466 #Here is an example which uses this routine.
468 # # The following lines makes stdin unbuffered
470 # $BSD = -f '/vmunix';
473 # system "stty cbreak </dev/tty >/dev/tty 2>&1";
476 # system "stty", '-icanon';
477 # system "stty", 'eol', '^A';
480 # require 'mychat2.pl';
482 # &chat'open_duphandle(STDIN);