From e9c57721db5e7cf2a19d93761d8c13e0c9701953 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 10 Aug 2007 19:02:31 +0000 Subject: [PATCH] * 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. --- ChangeLog | 26 ++++++++++++++++ NEWS | 3 ++ lib/xstrtol-error.c | 72 +++++++++++++++++++++++++++++++++---------- lib/xstrtol.h | 42 +++++++++++++++++-------- modules/xstrtol | 1 + tests/test-xstrtol.c | 2 +- tests/test-xstrtol.sh | 18 +++++------ 7 files changed, 124 insertions(+), 40 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3af255e9e7..15a49cf868 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,29 @@ +2007-08-10 Paul Eggert + + * 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 * lib/xstrtol-error.c: Add missing include. diff --git a/NEWS b/NEWS index 51c3f0b728..dfc1b46485 100644 --- 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 diff --git a/lib/xstrtol-error.c b/lib/xstrtol-error.c index e22af185e0..1d3d856404 100644 --- a/lib/xstrtol-error.c +++ b/lib/xstrtol-error.c @@ -23,39 +23,77 @@ #include #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 (); } diff --git a/lib/xstrtol.h b/lib/xstrtol.h index 96fc4d23e4..336af8d6bc 100644 --- a/lib/xstrtol.h +++ b/lib/xstrtol.h @@ -20,8 +20,7 @@ #ifndef XSTRTOL_H_ # define XSTRTOL_H_ 1 -# include "exitfail.h" - +# include # include # 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_ */ diff --git a/modules/xstrtol b/modules/xstrtol index cf48d8e685..540252adfc 100644 --- a/modules/xstrtol +++ b/modules/xstrtol @@ -11,6 +11,7 @@ m4/xstrtol.m4 Depends-on: exitfail error +getopt gettext-h intprops inttypes diff --git a/tests/test-xstrtol.c b/tests/test-xstrtol.c index 5f09452a54..e5c0c5eeac 100644 --- a/tests/test-xstrtol.c +++ b/tests/test-xstrtol.c @@ -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); diff --git a/tests/test-xstrtol.sh b/tests/test-xstrtol.sh index 7bf883379b..70022236a0 100755 --- a/tests/test-xstrtol.sh +++ b/tests/test-xstrtol.sh @@ -40,19 +40,19 @@ cat > t-xstrtol.xo <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 -- 2.30.2