Update to version 1.16
[p5sagit/p5-mst-13.2.git] / lib / chat2.inter
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         $eof = $timeout = 0;
290         do $subname();
291 }
292
293 ## &chat'print([$handle,] @data)
294 ## $handle is from previous &chat'open().
295 ## like print $handle @data
296
297 sub print { ## public
298         if ($_[0] =~ /$nextpat/) {
299                 *S = shift;
300         }
301         print S @_;
302 }
303
304 ## &chat'close([$handle,])
305 ## $handle is from previous &chat'open().
306 ## like close $handle
307
308 sub close { ## public
309         local($pid);
310         if ($_[0] =~ /$nextpat/) {
311                 $pid = $PID{$_[0]};
312                 *S = shift;
313         } else {
314                 $pid = $PID{$next};
315         }
316         close(S);
317         waitpid($pid,0);
318         if (defined $S{"needs_close"}) { # is it a listen socket?
319                 local(*NS) = $S{"needs_close"};
320                 delete $S{"needs_close"};
321                 close(NS);
322         }
323 }
324
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).
330
331 sub select { ## public
332         local($timeout) = shift;
333         local(@handles) = @_;
334         local(%handlename) = ();
335         local(%ready) = ();
336         local($caller) = caller;
337         local($rmask) = "";
338         for (@handles) {
339                 if (/$nextpat/o) { # one of ours... see if ready
340                         local(*SYM) = $_;
341                         if (length($SYM)) {
342                                 $timeout = 0; # we have a winner
343                                 $ready{$_}++;
344                         }
345                         $handlename{fileno($_)} = $_;
346                 } else {
347                         $handlename{fileno(/'/ ? $_ : "$caller\'$_")} = $_;
348                 }
349         }
350         for (sort keys %handlename) {
351                 vec($rmask, $_, 1) = 1;
352         }
353         select($rmask, undef, undef, $timeout);
354         for (sort keys %handlename) {
355                 $ready{$handlename{$_}}++ if vec($rmask,$_,1);
356         }
357         sort keys %ready;
358 }
359
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.
364
365 sub _getpty { ## private
366         local($_PTY,$_TTY) = @_;
367         $_PTY =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
368         $_TTY =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
369         local($pty,$tty);
370         for $bank (112..127) {
371                 next unless -e sprintf("/dev/pty%c0", $bank);
372                 for $unit (48..57) {
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);
381                 }
382         }
383         undef;
384 }
385
386
387 sub getpty {
388   local( $pty_handle, $tty_handle ) = @_;
389
390 print "--------in getpty----------\n";
391   $pty_handle =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
392   $pty_handle =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
393
394   #$pty_handle = ++$next_handle;
395   chop( @ptys = `ls /dev/pty*` );
396
397   for $pty ( @ptys )
398   {
399     open($pty_handle,"+>$pty") || next;
400     select((select($pty_handle), $| = 1)[0]);
401     ($tty = $pty) =~ s/pty/tty/;
402
403     open($tty_handle,"+>$tty") || next;
404     select((select($tty_handle), $| = 1)[0]);
405     ($tty = $pty) =~ s/pty/tty/;
406
407     return ($pty, $tty, $pty_handle );
408   }
409   return undef;
410 }
411
412
413
414 # from: Randal L. Schwartz
415
416 # Usage:
417 #
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");
423
424 sub interact
425 {
426   local( $chathandle ) = @_;
427
428   &chat'print($chathandle, "stty sane\n");
429   select(STDOUT) ; $| = 1; # unbuffer STDOUT
430
431   #print "tty=$Tty,whoami=",`whoami`,"\n";
432   #&change_utmp( "", $Tty, "eric", "", time() );
433
434   {
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";
442             print "got=($text)";
443             #print $text;
444     }
445     if (grep($_ eq STDIN, @ready)) {
446             print "checking STDIN\n";
447             last unless sysread(STDIN,$buf,1024) > 0;
448             print "STDIN OK\n";
449             &chat'print($chathandle,$buf);
450     }
451     redo;
452   }
453   #&change_utmp( $Tty, "$Tty", "", "", 0 );
454   print "leaving interact, \$!=$!\n";
455 }
456
457 ## $handle = &chat'open_duphandle(handle);
458 ## duplicates an input file handle to conform to chat format
459
460 sub open_duphandle { ## public
461     *S = ++$next;
462     open(S,"<&$_[0]");
463     $next; # return symbol for switcharound
464 }
465
466 #Here is an example which uses this routine.
467 #
468 #    # The following lines makes stdin unbuffered
469 #    
470 #    $BSD = -f '/vmunix';
471 #    
472 #    if ($BSD) {
473 #        system "stty cbreak </dev/tty >/dev/tty 2>&1";
474 #    }
475 #    else {
476 #        system "stty", '-icanon';
477 #        system "stty", 'eol', '^A';
478 #    }
479 #
480 #    require 'mychat2.pl';
481 #    
482 #    &chat'open_duphandle(STDIN);
483 #    
484 #    print 
485 #    &chat'expect(3,
486 #      '[A-Z]', '" :-)"',
487 #      '.', '" :-("',
488 #      TIMEOUT,  '"-o-"',
489 #      EOF, '"\$\$"'),
490 #    "\n";
491
492
493 1;
494
495