On Tue, Jan 09, 2018 at 10:05:22AM -0600, Scott Cheloha wrote:
> After discussing it with jca@ and trying a few variations I've settled
> on the attached diff.
> 
> We indicate if sh(1) was signalled because we have that information at
> hand.  Otherwise we report the exit status as we always have.

... reasoning being that there isn't a nice, portable way to determine
if a given return value >128 is a valid signal, it effectively never
happens anyway, and using the exec builtin for the command run in
compile mode in order to reliably retrieve the signal number would break
conditional execution chains, which are useful and supported by devel/emacs.

> ok?
> 
> Index: grep.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/mg/grep.c,v
> retrieving revision 1.45
> diff -u -p -r1.45 grep.c
> --- grep.c    12 Oct 2017 14:12:00 -0000      1.45
> +++ grep.c    9 Jan 2018 15:23:35 -0000
> @@ -4,6 +4,8 @@
>  
>  #include <sys/queue.h>
>  #include <sys/types.h>
> +#include <sys/wait.h>
> +
>  #include <ctype.h>
>  #include <libgen.h>
>  #include <limits.h>
> @@ -180,7 +182,7 @@ compile_mode(const char *name, const cha
>       char    *buf;
>       size_t   sz;
>       ssize_t  len;
> -     int      ret, n;
> +     int      ret, n, status;
>       char     cwd[NFILEN], qcmd[NFILEN];
>       char     timestr[NTIME];
>       time_t   t;
> @@ -226,11 +228,16 @@ compile_mode(const char *name, const cha
>       t = time(NULL);
>       strftime(timestr, sizeof(timestr), "%a %b %e %T %Y", localtime(&t));
>       addline(bp, "");
> -     if (ret != 0)
> -             addlinef(bp, "Command exited abnormally with code %d"
> -                 " at %s", ret, timestr);
> -     else
> -             addlinef(bp, "Command finished at %s", timestr);
> +     if (WIFEXITED(ret)) {
> +             status = WEXITSTATUS(ret);
> +             if (status == 0)
> +                     addlinef(bp, "Command finished at %s", timestr);
> +             else
> +                     addlinef(bp, "Command exited abnormally with code %d "
> +                         "at %s", status, timestr);
> +     } else
> +             addlinef(bp, "Subshell killed by signal %d at %s",
> +                 WTERMSIG(ret), timestr);
>  
>       bp->b_dotp = bfirstlp(bp);
>       bp->b_modes[0] = name_mode("fundamental");

Reply via email to