* NEWS: In xstrtol, remove STRTOL_FATAL_ERROR and add xstrtol_fatal.
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 10 Aug 2007 19:02:31 +0000 (19:02 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 10 Aug 2007 19:02:31 +0000 (19:02 +0000)
* lib/xstrtol.h: Don't include exitfail.h; that's now internal to
xstrtol.c.  Include getopt.h, since xstrtol_fatal's signature
depends on it.
(xstrtol_error): Remove.
(xstrtol_fatal): New decl, replacing the functionality of xstrtol_error
but with a different signature.
(ATTRIBUTE_NORETURN, __attribute__): New macros.
* lib/xstrtol-error.c: Include exitfail.h.
(xstrtol_fatal): New function, with a different signature from the
old xstrtol_error, so that the caller need not worry about passing
in an exit status, or about storage management of the option argument.
(xstrtol_error): Now a static function.  Redo signature to
implement xstrtol_fatal.  Output the correct number of hyphens in
front of the option so that the caller need not worry about
storage management.
(N_): New macro.
(_): Remove; not used now.
* modules/xstrtol: Depend on getopt.
* tests/test-xstrtol.c (main): Use new xstrtol_error function instead
of old STRTOL_FATAL_ERROR macro.
* tests/test-xstrtol.sh (t-xstrtol.xo): Adjust to match new behavior
of test program.

ChangeLog
NEWS
lib/xstrtol-error.c
lib/xstrtol.h
modules/xstrtol
tests/test-xstrtol.c
tests/test-xstrtol.sh

index 3af255e..15a49cf 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,29 @@
+2007-08-10  Paul Eggert  <eggert@cs.ucla.edu>
+
+       * NEWS: In xstrtol, remove STRTOL_FATAL_ERROR and add xstrtol_fatal.
+       * lib/xstrtol.h: Don't include exitfail.h; that's now internal to
+       xstrtol.c.  Include getopt.h, since xstrtol_fatal's signature
+       depends on it.
+       (xstrtol_error): Remove.
+       (xstrtol_fatal): New decl, replacing the functionality of xstrtol_error
+       but with a different signature.
+       (ATTRIBUTE_NORETURN, __attribute__): New macros.
+       * lib/xstrtol-error.c: Include exitfail.h.
+       (xstrtol_fatal): New function, with a different signature from the
+       old xstrtol_error, so that the caller need not worry about passing
+       in an exit status, or about storage management of the option argument.
+       (xstrtol_error): Now a static function.  Redo signature to
+       implement xstrtol_fatal.  Output the correct number of hyphens in
+       front of the option so that the caller need not worry about
+       storage management.
+       (N_): New macro.
+       (_): Remove; not used now.
+       * modules/xstrtol: Depend on getopt.
+       * tests/test-xstrtol.c (main): Use new xstrtol_error function instead
+       of old STRTOL_FATAL_ERROR macro.
+       * tests/test-xstrtol.sh (t-xstrtol.xo): Adjust to match new behavior
+       of test program.
+
 2007-08-08  Eric Blake  <ebb9@byu.net>
 
        * lib/xstrtol-error.c: Add missing include.
diff --git a/NEWS b/NEWS
index 51c3f0b..dfc1b46 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,9 @@ User visible incompatible changes
 
 Date        Modules         Changes
 
+2007-08-10  xstrtol         The STRTOL_FATAL_ERROR macro is removed.
+                            Use the new xstrtol_fatal function instead.
+
 2007-08-04  human           The function human_options no longer reports an
                             error to standard error; that is now the
                             caller's responsibility.  It returns an
index e22af18..1d3d856 100644 (file)
 #include <stdlib.h>
 
 #include "error.h"
+#include "exitfail.h"
 #include "gettext.h"
 
-#define _(str) gettext (str)
+#define N_(msgid) msgid
 
-/* Report an error for an out-of-range integer argument.
-   EXIT_CODE is the exit code (0 for a non-fatal error).
-   OPTION is the option that takes the argument
-    (usually starting with one or two minus signs).
-   ARG is the option's argument.
-   ERR is the error code returned by one of the xstrto* functions.  */
-void
-xstrtol_error (int exit_code, char const *option, char const *arg,
-              strtol_error err)
+/* Report an error for an invalid integer in an option argument.
+
+   ERR is the error code returned by one of the xstrto* functions.
+
+   Use OPT_IDX to decide whether to print the short option string "C"
+   or "-C" or a long option string derived from LONG_OPTION.  OPT_IDX
+   is -2 if the short option "C" was used, without any leading "-"; it
+   is -1 if the short option "-C" was used; otherwise it is an index
+   into LONG_OPTIONS, which should have a name preceded by two '-'
+   characters.
+
+   ARG is the option-argument containing the integer.
+
+   After reporting an error, exit with status EXIT_STATUS if it is
+   nonzero.  */
+
+static void
+xstrtol_error (enum strtol_error err,
+              int opt_idx, char c, struct option const *long_options,
+              char const *arg,
+              int exit_status)
 {
+  char const *hyphens = "--";
+  char const *msgid;
+  char const *option;
+  char option_buffer[2];
+
   switch (err)
     {
     default:
       abort ();
 
     case LONGINT_INVALID:
-      error (exit_code, 0, _("invalid %s argument `%s'"),
-            option, arg);
+      msgid = N_("invalid %s%s argument `%s'");
       break;
 
     case LONGINT_INVALID_SUFFIX_CHAR:
-    case LONGINT_INVALID_SUFFIX_CHAR | LONGINT_OVERFLOW:
-      error (exit_code, 0, _("invalid suffix in %s argument `%s'"),
-            option, arg);
+    case LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW:
+      msgid = N_("invalid suffix in %s%s argument `%s'");
       break;
 
     case LONGINT_OVERFLOW:
-      error (exit_code, 0, _("%s argument `%s' too large"),
-            option, arg);
+      msgid = N_("%s%s argument `%s' too large");
       break;
     }
+
+  if (opt_idx < 0)
+    {
+      hyphens -= opt_idx;
+      option_buffer[0] = c;
+      option_buffer[1] = '\0';
+      option = option_buffer;
+    }
+  else
+    option = long_options[opt_idx].name;
+
+  error (exit_failure, 0, gettext (msgid), hyphens, option, arg);
+}
+
+/* Like xstrtol_error, except exit with a failure status.  */
+
+void
+xstrtol_fatal (enum strtol_error err,
+              int opt_idx, char c, struct option const *long_options,
+              char const *arg)
+{
+  xstrtol_error (err, opt_idx, c, long_options, arg, exit_failure);
+  abort ();
 }
index 96fc4d2..336af8d 100644 (file)
@@ -20,8 +20,7 @@
 #ifndef XSTRTOL_H_
 # define XSTRTOL_H_ 1
 
-# include "exitfail.h"
-
+# include <getopt.h>
 # include <inttypes.h>
 
 # ifndef _STRTOL_ERROR
@@ -48,16 +47,33 @@ _DECLARE_XSTRTOL (xstrtoul, unsigned long int)
 _DECLARE_XSTRTOL (xstrtoimax, intmax_t)
 _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
 
-/* Report an error for an out-of-range integer argument.
-   EXIT_CODE is the exit code (0 for a non-fatal error).
-   OPTION is the option that takes the argument
-    (usually starting with one or two minus signs).
-   ARG is the option's argument.
-   ERR is the error code returned by one of the xstrto* functions.  */
-void xstrtol_error (int exit_code, char const *option, char const *arg,
-                   strtol_error err);
-
-# define STRTOL_FATAL_ERROR(Option, Arg, Err)                          \
-  xstrtol_error (exit_failure, Option, Arg, Err)
+#ifndef __attribute__
+# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
+#  define __attribute__(x)
+# endif
+#endif
+
+#ifndef ATTRIBUTE_NORETURN
+# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
+#endif
+
+/* Report an error for an invalid integer in an option argument.
+
+   ERR is the error code returned by one of the xstrto* functions.
+
+   Use OPT_IDX to decide whether to print the short option string "C"
+   or "-C" or a long option string derived from LONG_OPTION.  OPT_IDX
+   is -2 if the short option "C" was used, without any leading "-"; it
+   is -1 if the short option "-C" was used; otherwise it is an index
+   into LONG_OPTIONS, which should have a name preceded by two '-'
+   characters.
+
+   ARG is the option-argument containing the integer.
+
+   After reporting an error, exit with a failure status.  */
+
+void xstrtol_fatal (enum strtol_error,
+                   int, char, struct option const *,
+                   char const *) ATTRIBUTE_NORETURN;
 
 #endif /* not XSTRTOL_H_ */
index cf48d8e..540252a 100644 (file)
@@ -11,6 +11,7 @@ m4/xstrtol.m4
 Depends-on:
 exitfail
 error
+getopt
 gettext-h
 intprops
 inttypes
index 5f09452..e5c0c5e 100644 (file)
@@ -52,7 +52,7 @@ main (int argc, char **argv)
        }
       else
        {
-         STRTOL_FATAL_ERROR ("arg", argv[i], s_err);
+         xstrtol_fatal (s_err, -2, 'X', NULL, argv[i]);
        }
     }
   exit (0);
index 7bf8833..7002223 100755 (executable)
@@ -40,19 +40,19 @@ cat > t-xstrtol.xo <<EOF
 1->1 ()
 -1->-1 ()
 1k->1024 ()
-invalid suffix in arg argument \`${too_big}h'
-arg argument \`$too_big' too large
-invalid arg argument \`x'
-invalid suffix in arg argument \`9x'
+invalid suffix in X argument \`${too_big}h'
+X argument \`$too_big' too large
+invalid X argument \`x'
+invalid suffix in X argument \`9x'
 010->8 ()
 MiB->1048576 ()
 1->1 ()
-invalid arg argument \`-1'
+invalid X argument \`-1'
 1k->1024 ()
-invalid suffix in arg argument \`${too_big}h'
-arg argument \`$too_big' too large
-invalid arg argument \`x'
-invalid suffix in arg argument \`9x'
+invalid suffix in X argument \`${too_big}h'
+X argument \`$too_big' too large
+invalid X argument \`x'
+invalid suffix in X argument \`9x'
 010->8 ()
 MiB->1048576 ()
 EOF