autoconf-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

FYI: more exit code tuning (Was: Re: exit status 63)


From: Alexandre Duret-Lutz
Subject: FYI: more exit code tuning (Was: Re: exit status 63)
Date: Sun, 25 May 2003 13:00:59 +0200
User-agent: Gnus/5.090016 (Oort Gnus v0.16) Emacs/21.3 (gnu/linux)

>>> "adl" == Alexandre Duret-Lutz <address@hidden> writes:

 adl> Patrick Welche <address@hidden> writes:
 >> autoupdate: /usr/pkg/bin/gm4 failed with exit status: 63
 >> tools.at:602: exit code was 1, expected 63
 >> 12. tools.at:588: 12. tools.at:588: FAILED near `tools.at:602'

 adl> Hi Patrick,

 adl> Thank you for the report.

 adl> Could you tell us more about your environment?  (OS? Perl version?)

I was able to reproduce this on a NetBSD 1.6_RC1 box with Perl 5.6.1
installed.  There, calling POSIX::WEXITSTATUS will alter the value
of $!, causing what you saw.

I've installed the following patch, which also fixes the `xqx' function.

2003-05-25  Alexandre Duret-Lutz  <address@hidden>

        * lib/Autom4te/General.pm (END): Print diagnostics to STDERR.
        (handle_exec_errors): New function.  Work around $! being
        altered by WEXITSTATUS.
        (xqx, xsystem): Use handle_exec_errors.

Index: lib/Autom4te/General.pm
===================================================================
RCS file: /cvsroot/autoconf/autoconf/lib/Autom4te/General.pm,v
retrieving revision 1.25
diff -u -r1.25 General.pm
--- lib/Autom4te/General.pm     23 May 2003 18:16:57 -0000      1.25
+++ lib/Autom4te/General.pm     25 May 2003 10:54:35 -0000
@@ -121,14 +121,14 @@
        {
          if (! unlink <$tmp/*>)
            {
-             print "$me: cannot empty $tmp: $!\n";
+             print STDERR "$me: cannot empty $tmp: $!\n";
              $? = 1;
              return;
            }
        }
       if (! rmdir $tmp)
        {
-         print "$me: cannot remove $tmp: $!\n";
+         print STDERR "$me: cannot remove $tmp: $!\n";
          $? = 1;
          return;
        }
@@ -138,7 +138,7 @@
   # E.g., even --version or --help.  So it's best to do it unconditionally.
   if (! close STDOUT)
     {
-      print "$me: closing standard output: $!\n";
+      print STDERR "$me: closing standard output: $!\n";
       $? = 1;
       return;
     }
@@ -488,23 +488,57 @@
     if $verbose;
 }
 
+# handle_exec_errors ($COMMAND)
+# -----------------------------
+# Display an error message for $COMMAND, based on the content of $? and $!.
+sub handle_exec_errors ($)
+{
+  my ($command) = @_;
+
+  $command = (split (' ', $command))[0];
+  if ($!)
+    {
+      error "failed to run $command: $!";
+    }
+  else
+    {
+      use POSIX qw (WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
+
+      if (WIFEXITED ($?))
+       {
+         my $status = WEXITSTATUS ($?);
+         # WIFEXITED and WEXITSTATUS can alter $!, reset it so that
+         # error() actually propagates the command's exit status, not $!.
+         $! = 0;
+         error "$command failed with exit status: $status";
+       }
+      elsif (WIFSIGNALED ($?))
+       {
+         my $signal = WTERMSIG ($?);
+         # In this case we prefer to exit with status 1.
+         $! = 1;
+         error "$command terminated by signal: $signal";
+       }
+      else
+       {
+         error "$command exited abnormally";
+       }
+    }
+}
 
 # xqx ($COMMAND)
 # --------------
 # Same as `qx' (but in scalar context), but fails on errors.
 sub xqx ($)
 {
-  use POSIX qw (WIFEXITED WEXITSTATUS);
-
   my ($command) = @_;
 
   verbose "running: $command";
-  my $res = `$command`;
 
-  error ((split (' ', $command))[0]
-        . " failed with exit status: "
-        . WEXITSTATUS ($?))
-    if WIFEXITED ($?) && WEXITSTATUS ($?) != 0;
+  $! = 0;
+  my $res = `$command`;
+  handle_exec_errors $command
+    if $?;
 
   return $res;
 }
@@ -514,26 +548,13 @@
 # ------------------
 sub xsystem ($)
 {
-  use POSIX qw (WEXITSTATUS);
-
   my ($command) = @_;
 
   verbose "running: $command";
 
   $! = 0;
-
-  if (system $command)
-  {
-    $command = (split (' ', $command))[0];
-    if ($!)
-      {
-       error "failed to run $command: $!";
-      }
-    else
-      {
-       error "$command failed with exit status: " . WEXITSTATUS ($?);
-      }
-  }
+  handle_exec_errors $command
+    if system $command;
 }
 
 
-- 
Alexandre Duret-Lutz





reply via email to

[Prev in Thread] Current Thread [Next in Thread]