home *** CD-ROM | disk | FTP | other *** search
/ ftp.cse.unsw.edu.au / 2014.06.ftp.cse.unsw.edu.au.tar / ftp.cse.unsw.edu.au / pub / doc / languages / perl / nutshell / ch6 / zap < prev   
Encoding:
Text File  |  1992-10-18  |  1.4 KB  |  72 lines

  1. #!/usr/bin/perl
  2.  
  3. # Usage: zap [-signal] pattern
  4.  
  5. # Configuration parameters.
  6.  
  7. $sig = 'TERM';
  8.  
  9. $BSD = -f '/vmunix';
  10. $pscmd = $BSD ? "ps -auxww" : "ps -ef";
  11.  
  12. open(TTYIN, "</dev/tty") || die "can't read /dev/tty: $!";
  13. open(TTYOUT, ">/dev/tty") || die "can't write /dev/tty: $!";
  14. select(TTYOUT);
  15. $| = 1;
  16. select(STDOUT);
  17. $SIG{'INT'} = 'cleanup';
  18.  
  19. if ($#ARGV >= $[ && $ARGV[0] =~ /^-/) {
  20.     if ($ARGV[0] =~ /-(\w+)$/) {
  21.     $sig = $1;
  22.     } else {
  23.     print STDERR "$0: illegal argument $ARGV[0] ignored\n";
  24.     }
  25.     shift;
  26. }
  27.  
  28. if ($BSD) {
  29.     system "stty cbreak </dev/tty >/dev/tty 2>&1";
  30. }
  31. else {
  32.     system "stty", 'cbreak',
  33.     system "stty", 'eol', '^A';
  34. }
  35.  
  36. open(PS, "$pscmd|") || die "can't run $pscmd: $!";
  37. $title = <PS>;
  38. print TTYOUT $title;
  39.  
  40. # Catch any errors with eval.  A bad pattern, for instance.
  41.  
  42. eval <<'EOF';
  43. while ($cand = <PS>) {
  44.     chop($cand);
  45.     ($user, $pid) = split(' ', $cand);
  46.     next if $pid == $$;
  47.     $found = !@ARGV;
  48.     foreach $pat (@ARGV) {
  49.     $found = 1 if $cand =~ $pat;
  50.     }
  51.     next if (!$found);
  52.     print TTYOUT "$cand? ";
  53.     read(TTYIN, $ans, 1);
  54.     print TTYOUT "\n" if ($ans ne "\n");
  55.     if ($ans =~ /^y/i) { kill $sig, $pid; }
  56.     if ($ans =~ /^q/i) { last; }
  57. }
  58. EOF
  59. &cleanup;
  60.  
  61. sub cleanup {
  62.     if ($BSD) {
  63.     system "stty -cbreak </dev/tty >/dev/tty 2>&1";
  64.     }
  65.     else {
  66.     system "stty", 'icanon';
  67.     system "stty", 'eol', '^@';
  68.     }
  69.     print "\n";
  70.     exit;
  71. }
  72.