sub interact {
  # We wait some something from the user (STDIN) or from the program (COUT/CERR)
  # Infinite loop, until child (maple) dies or all inputs are closed.
  $rin = $win = $ein = '';
  vec($rin,fileno(STDIN),1) = 1;
  vec($rin,fileno(COUT),1) = 1;
  vec($rin,fileno(CERR),1) = 1;
  use IO::Handle;
  CIN->autoflush(1);
  COUT->autoflush(1);
  print "Enter your Maple commands, finish with Ctrl-D.\n";
  $mapleheader = 1;
  while (0 <= select ($rout=$rin, $wout=$win, $eout=$ein, undef)) {
    if (vec($rout,fileno(STDIN),1)) {
      # Copy STDIN into CIN, closing CIN when STDIN is closed.
      if (eof STDIN) {
        vec($rin,fileno(STDIN),1) = 0;
        close CIN;
      } else {
        $_ = <STDIN>;
        print CIN;
      }
    }
    if (vec($rout,fileno(COUT),1)) {
      if (eof COUT) {
        vec($rin,fileno(COUT),1) = 0;
      } elsif(1) {
        sysread COUT, $_, 80;
        do { $mapleheader = 0; goto noprint } if /Type \? for help/;
        goto noprint if $mapleheader;
        s/^> .*\n//mg;
        s/^\s+//mg;
      #  goto noprint if /^> /;
        goto noprint unless $_;
        goto noprint if /bytes used/;
        print "Maple: $_";
        noprint:
      }
    }
    if (vec($rout,fileno(CERR),1)) {
      if (eof CERR) {
        vec($rin,fileno(CERR),1) = 0;
      } else {
        sysread CERR, $_, 80;
        print "Maple[err]: $_";
      }
    }
  }
}

1;
