gbranden pushed a commit to branch master
in repository groff.

commit 3531737df1f0ed203c686e1bab3b979d2a0a71ab
Author: G. Branden Robinson <[email protected]>
AuthorDate: Sat Dec 6 08:56:32 2025 -0600

    src/utils/pfbtops/pfbtops.c: Mildly refactor.
    
    * src/utils/pfbtops/pfbtops.c: Refactor.
      (error): Drop exit(3) call so that we can use this function for
      nonfatal errors or fatal ones that need to exit with a status other
      than `EXIT_FAILURE` (as with usage errors).
    
      (die): New function wraps `error()` and exit(3)s.
    
      (get_text, get_binary, main): Migrate callers from `error()` to
      `die()`.
    
      (error, usage): Explicitly cast unused return value of fprintf(3) to
      `void`.
    
      (main): Employ new strategies for construction of error diagnostics
      reporting unrecognized options.
---
 ChangeLog                   | 14 ++++++++++++++
 src/utils/pfbtops/pfbtops.c | 40 +++++++++++++++++++++++++---------------
 2 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 92ba02f87..d976f3382 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2025-12-06  G. Branden Robinson <[email protected]>
+
+       * src/utils/pfbtops/pfbtops.c: Refactor.
+       (error): Drop exit(3) call so that we can use this function for
+       nonfatal errors or fatal ones that need to exit with a status
+       other than `EXIT_FAILURE` (as with usage errors).
+       (die): New function wraps `error()` and exit(3)s.
+       (get_text, get_binary, main): Migrate callers from `error()` to
+       `die()`.
+       (error, usage): Explicitly cast unused return value of
+       fprintf(3) to `void`.
+       (main): Employ new strategies for construction of error
+       diagnostics reporting unrecognized options.
+
 2025-12-06  G. Branden Robinson <[email protected]>
 
        Handle unrecognized long option arguments better.
diff --git a/src/utils/pfbtops/pfbtops.c b/src/utils/pfbtops/pfbtops.c
index 8d15d173d..f32ef9e39 100644
--- a/src/utils/pfbtops/pfbtops.c
+++ b/src/utils/pfbtops/pfbtops.c
@@ -44,15 +44,20 @@ static char *program_name;
 
 static void error(const char *s)
 {
-  fprintf(stderr, "%s: error: %s\n", program_name, s);
+  (void) fprintf(stderr, "%s: error: %s\n", program_name, s);
+}
+
+static void die(const char *s)
+{
+  error(s);
   exit(EXIT_FAILURE);
 }
 
 static void usage(FILE *stream)
 {
-  fprintf(stream, "usage: %s [pfb-file]\n"
-         "usage: %s {-v | --version}\n"
-         "usage: %s --help\n",
+  (void) fprintf(stream, "usage: %s [pfb-file]\n"
+                "usage: %s {-v | --version}\n"
+                "usage: %s --help\n",
          program_name, program_name, program_name);
   if (stdout == stream)
     fputs("\n"
@@ -108,7 +113,7 @@ static void get_text(int n)
       }
     }
     if (c == EOF)
-      error("end of file in text packet");
+      die("end of file in text packet");
     else if (c == '\r') {
       if (n-- == 0)
        break;
@@ -162,7 +167,7 @@ static void get_binary(int n)
   while (--n >= 0) {
     c = getchar();
     if (c == EOF)
-      error("end of file in binary packet");
+      die("end of file in binary packet");
     if (count >= BYTES_PER_LINE) {
       putchar('\n');
       count = 0;
@@ -197,12 +202,17 @@ int main(int argc, char **argv)
       exit(EXIT_SUCCESS);
       break;
     case '?':
-      if (optopt != 0)
-       fprintf(stderr, "%s: error: unrecognized command-line option"
-               " '%c'\n", program_name, (char) optopt);
+      if (optopt != 0) {
+       char errbuf[] = "unrecognized command-line option 'X'";
+       errbuf[(strchr(errbuf, 'X') - errbuf)] = optopt;
+       error(errbuf);
+      }
       else
-       fprintf(stderr, "unrecognized command-line option '%1'",
-               argv[(optind - 1)]);
+       // We can't use `error()` because we have no idea how big the
+       // user-specified long option is.
+       (void) fprintf(stderr, "%s: error: unrecognized command-line"
+                      " option '%s'\n", program_name,
+                      argv[(optind - 1)]);
       usage(stderr);
       exit(2);
       break;
@@ -235,21 +245,21 @@ int main(int argc, char **argv)
 
     c = getchar();
     if (c != 0x80)
-      error("first byte of packet not 0x80");
+      die("first byte of packet not 0x80");
     type = getchar();
     if (type == 3)
       break;
     if (type != 1 && type != 2)
-      error("bad packet type");
+      die("bad packet type");
     n = 0;
     for (i = 0; i < 4; i++) {
       c = getchar();
       if (c == EOF)
-       error("end of file in packet header");
+       die("end of file in packet header");
       n |= (long)c << (i << 3);
     }
     if (n < 0)
-      error("negative packet length");
+      die("negative packet length");
     if (type == 1)
       get_text(n);
     else

_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit

Reply via email to