home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / convex / contact next >
Encoding:
Text File  |  1991-03-13  |  5.5 KB  |  266 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # bug
  4. # replacement for contact, which is just too annoying to live
  5.  
  6. eval "exec perl -S $0 $*"
  7.     if $running_under_some_shell_and_would_really_prefer_perl;
  8.  
  9.  
  10. if (!@ARGV || @ARGV > 2) {
  11.     die <<EOF;
  12. usage: $0 program [priority]
  13.  
  14.     $0 is a replacement for the contact program.  
  15.     Besides figuring out paths and versions for you, and using MH if you
  16.     do, its principal feature is that it plops you right into your editor
  17.     instead of asking you annoying questions.
  18.  
  19.                    --tchrist
  20.  
  21. EOF
  22. }
  23.  
  24.  
  25. @SIG{'INT',     'QUIT', 'HUP',          'TERM'}     = (
  26.      'INTR',    'INTR', 'CLEANUP',     'CLEANUP');
  27.  
  28.  
  29. $editor = $ENV{'EDITOR'} || 'vi';
  30. $pager = $ENV{'PAGER'} || 'less';
  31.  
  32.  
  33. $user   =  getlogin 
  34.     || $ENV{'USER'}
  35.     || (getpwuid($<))[0]
  36.     || "an unknown uid";
  37.  
  38. $home   = $ENV{'HOME'}
  39.        || (getpwuid($<))[7]
  40.        || '/';
  41.  
  42.  
  43. if (&get_cpuid) {
  44.     &get_hostname || die "will not continue without hostname\n";
  45. } else { 
  46.     $alien++;
  47.     $cpuid = 147; # default to sushi, like rb
  48.     $hostname = 'sushi';
  49. }
  50. &get_userinfo;
  51.  
  52. if ( $ENV{'PATH'} =~ m#/mh/bin# && -e "$home/.mh_profile" )  {
  53.     $usemh++;
  54.     $ccline = "Fcc: +bug_reports";
  55. } else {
  56.     $ccline = "Cc: $user";
  57.  
  58.  
  59. @PATH =  (    '/usr/ucb', 
  60.            '/usr/convex',
  61.         '/usr/bin',
  62.         '/bin',
  63.         '/etc',
  64.         '/usr/etc',
  65.         '/usr/toolbox',
  66.         '/usr/bin/X11',
  67.         '/usr/lib',
  68.         '/usr/adm',
  69.     );
  70.  
  71. @PRIORITY = split(/\n/, <<EOP);
  72.  
  73. Critical    - work cannot proceed until the problem is resolved.
  74. Serious     - work can proceed around the problem, with difficulty.
  75. Necessary   - problem has to be fixed.
  76. Annoying    - problem is bothersome.
  77. Enhancement - requested enhancement.
  78. Informative - for informational purposes only.
  79. EOP
  80.  
  81. ($product, $priority) = @ARGV;
  82. $priority = 3 unless $priority;
  83.  
  84. if ($product !~ m#^/#) {
  85.     for (@PATH) {
  86.     next unless -x "$_/$product";
  87.     $product = "$_/$product";
  88.     last;
  89.     } 
  90. $version = (!$alien && (`vers $product 2>/dev/null` =~ /:\s*(\S+)/)[0])
  91.         || "<unknown version>";
  92.  
  93. $TMP = "/tmp/bug.$$";
  94.  
  95. open (TMP, ">$TMP") || die "can't create $TMP: $!";
  96. $tmpmade++;
  97. select(TMP);
  98.  
  99. print <<EOM;
  100. To: contact
  101. Subject: CPU-$cpuid ($hostname)
  102. $ccline
  103.  
  104. User-Information:
  105. $userinfo
  106.  
  107. Product: $product
  108.  
  109. Version: $version
  110.  
  111. Summary: <put one line summary here>
  112.  
  113. Description:
  114.     <put multi-line description here>
  115.  
  116. Priority: $priority ($PRIORITY[$priority])
  117.  
  118. Repeat-By: 
  119.     <put multi-line how-to-repro desc here>
  120.  
  121. Comments:
  122.     <put any multi-line comments  here>
  123.  
  124. EOM
  125.  
  126. close(TMP) || die "can't close $TMP: $!";
  127.  
  128. select (STDOUT);
  129.  
  130. if ($usemh) {
  131.     system 'whatnow', $TMP;
  132.     exit unless $?;
  133.     warn "whatnow failed -- reverting to $editor\n";
  134. }
  135.  
  136. # otherwise go the hard way
  137.  
  138. system $editor, $TMP;
  139.  
  140. $| = 1;
  141.  
  142.  
  143. for (;;) {
  144.     print 'Send, abort, edit, or list? ';
  145.     chop ($action = <STDIN>);
  146.     redo unless $action;
  147.  
  148.     $action =~ s/(\W)/\\$1/g;
  149.  
  150.     last if 'send' =~ /^$action/i;
  151.  
  152.     if ('abort' =~ /^$action/i) {
  153.         do CLEANUP();
  154.     } elsif ('edit' =~ /^$action/i)  {
  155.         system $editor, $TMP;
  156.     } elsif ('list' =~ /^$action/i)  {
  157.         system $pager, $TMP;
  158.     } else {
  159.         $action =~ s/\\//g;
  160.         print "Unknown action: \"$action\"\n";
  161.     }
  162. }
  163.  
  164. if (system "/usr/lib/sendmail -oi -t < $TMP") {
  165.     warn "couldn't mail contact report\n";
  166.     &CLEANUP;
  167.  
  168. print "Problem report submitted.\n";
  169.  
  170. exit 0;
  171.  
  172. #--------------------------------------------------------
  173.  
  174. sub get_hostname {
  175.     $hostname = "\0" x 100;   # pre-nulled buffer for hostname
  176.     $SYS_gethostname = 87;
  177.     $SIG{'SYS'} = 'NOSYS';
  178.     $syscall = 'gethostname';
  179.  
  180.     if (syscall($SYS_gethostname, $hostname, length($hostname)) < 0) {
  181.     warn "couldn't get hostname cheaply";
  182.     chop($hostname = `hostname`);
  183.     } else {
  184.     $hostname =~ s/[\0\s]*$//;  # get rid of nasty nulls
  185.     }
  186.     $SIG{'SYS'} = 'NOSYS';
  187.     $syscall = '';
  188.     $hostname;
  189.  
  190. sub get_cpuid {
  191.     $SYS_getsysinfo = 203;
  192.     $SYSINFO_SIZE = 12;
  193.     $sysinfo = "\0" x $SYSINFO_SIZE;
  194.     $SIG{'SYS'} = 'NOSYS';
  195.     $syscall = 'getsysinfo';
  196.     if (syscall($SYS_getsysinfo, $SYSINFO_SIZE, $sysinfo) < 0) {
  197.     warn "odd, couldn't get cpuid cheaply";
  198.     chop($cpuid = `cpuid`);
  199.     &NOSYS if $? || !$cpuid;
  200.     } else {
  201.     ( $cpuid, $cpu_type, $cpu_count, $cpu_flags0, $cpu_flags1 )
  202.         = unpack('SCCLL', $sysinfo);
  203.     } 
  204.     $SIG{'SYS'} = 'DEFAULT';
  205.     $syscall = '';
  206.  
  207.     $cpuid;  # retval
  208.  
  209. sub get_userinfo {
  210.     $FILE = $home . "/.contact";
  211.     if (-e $FILE) {
  212.     open FILE || die "can't open $FILE: $!";
  213.     undef $/;
  214.     chop($userinfo = <FILE>);
  215.     $/ = "\n";
  216.     } else {
  217.     $gcos = (getpwnam($user))[6];
  218.     ($name, $office, $ophone, $hphone) = split(/,/, $gcos);
  219.     $userinfo  = "$name (CONVEX employee)\nOffice: $office\nExtension: x$ophone";
  220.     } 
  221.  
  222.  
  223. sub INTR {
  224.     local($more);
  225.     print STDERR "\nInterrupted\007 -- continue? ";
  226.     chop($more = <STDIN>);
  227.     &CLEANUP() if $more && ('quit' =~ /^$more/i || 'no' =~ /^$more/i);
  228.     print STDERR "(continuing)\n";
  229.  
  230.  
  231. sub CLEANUP {
  232.     if ($tmpmade) {
  233.         $DEAD = "$home/dead.report";
  234.         close TMP;
  235.         open TMP || die "can't reopen $TMP: $!";
  236.         open(DEAD,">$DEAD") || die "can't open $DEAD: $!";
  237.         print DEAD <TMP>;
  238.         close TMP;
  239.         close DEAD;
  240.         print STDERR "Your squished bug report has been saved in $DEAD\n";
  241.         unlink $TMP;
  242.     }
  243.     exit 1;
  244. }
  245.  
  246. sub NOSYS {
  247.     warn <<EOF;
  248. $0: You don't have a $syscall syscall?! 
  249.  
  250.             *** WARNING WARNING WARNING ***
  251.     This doesn't seem like a Convex box to me.  I'm going to lie and
  252.     pretend you're on sushi, like rb does.  Make sure you get the version
  253.     and path right.  They're almost surely wrong right now.
  254.  
  255. EOF
  256.     sleep 3;
  257.