On Mon, Jan 01, 2018 at 09:07:25PM -0700, Todd C. Miller wrote:
> On Mon, 01 Jan 2018 19:54:07 -0600, Scott Cheloha wrote:
> 
> > Hey,
> >
> > In the mg(1) *compile* buffer, currently you get incorrect
> > output like:
> >
> >     Command exited abnormally with code 256 at [...]
> >
> > Using the W* macros in <sys/wait.h> corrects this:
> >
> >     Command exited abnormally with code 1 at [...]
> 
> Is it worth using an explicit message if the command was terminated
> by a signal?

Like in lieu of 128+WTERMSIG?  I don't personally see my jobs in mg
get killed all that often, but if I did I think I'd prefer something
with the signal name, sure.

While we're at it, I'd like to move the timestamp left so it's separate
from the other output.  I'd also like to always print the exit status,
as "abnormally" is inapplicable for programs like diff and grep.

Thoughts?

--
Scott Cheloha

Index: usr.bin/mg/grep.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/grep.c,v
retrieving revision 1.45
diff -u -p -r1.45 grep.c
--- usr.bin/mg/grep.c   12 Oct 2017 14:12:00 -0000      1.45
+++ usr.bin/mg/grep.c   3 Jan 2018 01:24:09 -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, signo;
        char     cwd[NFILEN], qcmd[NFILEN];
        char     timestr[NTIME];
        time_t   t;
@@ -226,17 +228,19 @@ 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)) {
+               addlinef(bp, "[%s] Command exited with status %d",
+                   timestr, WEXITSTATUS(ret));
+       } else {
+               signo = WTERMSIG(ret);
+               addlinef(bp, "[%s] Command killed by %s: %s",
+                   timestr, sys_signame[signo], strsignal(signo));
+       }
 
        bp->b_dotp = bfirstlp(bp);
        bp->b_modes[0] = name_mode("fundamental");
        bp->b_modes[1] = name_mode("compile");
        bp->b_nmodes = 1;
-
        compile_buffer = bp;
 
        if (chdir(cwd) == -1) {

Reply via email to