groff-commit
[Top][All Lists]
Advanced

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

[groff] 60/60: [pre-grohtml]: Report system(3) failure better.


From: G. Branden Robinson
Subject: [groff] 60/60: [pre-grohtml]: Report system(3) failure better.
Date: Wed, 11 Sep 2024 03:38:36 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 6230f5614d24cbe3810187d1817575e750a9825b
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Wed Sep 11 02:05:17 2024 -0500

    [pre-grohtml]: Report system(3) failure better.
    
    * src/preproc/html/pre-html.cpp (html_system): Improve report of failing
      system(3) commands and fix code style nits.  Use standard constant
      symbol `STDOUT_FILENO` in favor of a "1" literal.  Parenthesize
      complex expressions.  Recast diagnostic message.  Stop misreporting
      the `int` return value of `system()` as the command's "(exit) status".
      The integer _encodes_ the exit status in its lowest seven bits.
      Distinguish the cases of the command exiting with a failing status,
      being signalled, and being stopped, using standard POSIX macros; see
      wait(2).
---
 ChangeLog                     | 12 ++++++++++++
 src/preproc/html/pre-html.cpp | 30 ++++++++++++++++++++++--------
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 06b75151c..098616397 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2024-09-11  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/preproc/html/pre-html.cpp (html_system): Improve report of
+       failing system(3) commands and fix code style nits.  Use
+       standard constant symbol `STDOUT_FILENO` in favor of a "1"
+       literal.  Parenthesize complex expressions.  Recast diagnostic
+       message.  Stop misreporting the `int` return value of `system()`
+       as the command's "(exit) status".  The integer _encodes_ the
+       exit status in its lowest seven bits.  Distinguish the cases of
+       the command exiting with a failing status, being signalled, and
+       being stopped, using standard POSIX macros; see wait(2).
+
 2024-09-11  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/preproc/html/pre-html.cpp (imageList::createPage)
diff --git a/src/preproc/html/pre-html.cpp b/src/preproc/html/pre-html.cpp
index 68e5f79ea..16f24cb81 100644
--- a/src/preproc/html/pre-html.cpp
+++ b/src/preproc/html/pre-html.cpp
@@ -377,19 +377,33 @@ void html_system(const char *s, int redirect_stdout)
   }
 #endif
   {
-    int saved_stdout = dup(1);
+    int saved_stdout = dup(STDOUT_FILENO);
     int fdnull = open(NULL_DEV, O_WRONLY|O_BINARY, 0666);
-    if (redirect_stdout && saved_stdout > 1 && fdnull > 1)
-      dup2(fdnull, 1);
+    if (redirect_stdout && (saved_stdout > STDOUT_FILENO)
+       && (fdnull > STDOUT_FILENO))
+      dup2(fdnull, STDOUT_FILENO);
     if (fdnull >= 0)
       close(fdnull);
     int status = system(s);
     if (redirect_stdout)
-      dup2(saved_stdout, 1);
-    if (status == -1)
-      fprintf(stderr, "Calling '%s' failed\n", s);
-    else if (status)
-      fprintf(stderr, "Calling '%s' returned status %d\n", s, status);
+      dup2(saved_stdout, STDOUT_FILENO);
+    if (-1 == status)
+      fprintf(stderr, "%s: unable to execute command '%s': %s\n",
+             program_name, s, strerror(errno));
+    else if (status > 0) {
+      if (WIFEXITED(status))
+       fprintf(stderr, "%s: command '%s' returned status %d\n",
+               program_name, s, WEXITSTATUS(status));
+      else if (WIFSIGNALED(status))
+       fprintf(stderr, "%s: command '%s' exited by signal: %s\n",
+               program_name, s, strsignal(WTERMSIG(status)));
+      else if (WIFSTOPPED(status))
+       fprintf(stderr, "%s: command '%s' stopped: %s\n",
+               program_name, s, strsignal(WSTOPSIG(status)));
+      else
+       fprintf(stderr, "%s: command '%s' exited abnormally\n",
+               program_name, s);
+    }
     close(saved_stdout);
   }
 }



reply via email to

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